xref: /freebsd/sys/amd64/ia32/ia32_syscall.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (C) 1994, David Greenman
5  * Copyright (c) 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the University of Utah, and William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 /*
44  * 386 Trap and System call handling
45  */
46 
47 #include "opt_clock.h"
48 #include "opt_cpu.h"
49 #include "opt_isa.h"
50 
51 #include <sys/param.h>
52 #include <sys/bus.h>
53 #include <sys/systm.h>
54 #include <sys/proc.h>
55 #include <sys/kernel.h>
56 #include <sys/ktr.h>
57 #include <sys/lock.h>
58 #include <sys/msan.h>
59 #include <sys/mutex.h>
60 #include <sys/proc.h>
61 #include <sys/ptrace.h>
62 #include <sys/resourcevar.h>
63 #include <sys/signalvar.h>
64 #include <sys/syscall.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
67 #include <sys/uio.h>
68 #include <sys/vmmeter.h>
69 #include <security/audit/audit.h>
70 
71 #include <vm/vm.h>
72 #include <vm/vm_param.h>
73 #include <vm/pmap.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_extern.h>
78 
79 #include <machine/cpu.h>
80 #include <machine/intr_machdep.h>
81 #include <machine/md_var.h>
82 
83 #include <compat/freebsd32/freebsd32_signal.h>
84 #include <compat/freebsd32/freebsd32_util.h>
85 #include <compat/ia32/ia32_signal.h>
86 #include <machine/psl.h>
87 #include <machine/segments.h>
88 #include <machine/specialreg.h>
89 #include <machine/sysarch.h>
90 #include <machine/frame.h>
91 #include <machine/md_var.h>
92 #include <machine/pcb.h>
93 #include <machine/cpufunc.h>
94 
95 #include "vdso_ia32_offsets.h"
96 
97 extern const char _binary_elf_vdso32_so_1_start[];
98 extern const char _binary_elf_vdso32_so_1_end[];
99 extern char _binary_elf_vdso32_so_1_size;
100 
101 #define	IDTVEC(name)	__CONCAT(X,name)
102 
103 extern inthand_t IDTVEC(int0x80_syscall), IDTVEC(int0x80_syscall_pti),
104     IDTVEC(rsvd), IDTVEC(rsvd_pti);
105 
106 void ia32_syscall(struct trapframe *frame);	/* Called from asm code */
107 
108 void
109 ia32_set_syscall_retval(struct thread *td, int error)
110 {
111 
112 	cpu_set_syscall_retval(td, error);
113 }
114 
115 int
116 ia32_fetch_syscall_args(struct thread *td)
117 {
118 	struct proc *p;
119 	struct trapframe *frame;
120 	struct syscall_args *sa;
121 	caddr_t params;
122 	u_int32_t args[8], tmp;
123 	int error, i;
124 #ifdef COMPAT_43
125 	u_int32_t eip;
126 	int cs;
127 #endif
128 
129 	p = td->td_proc;
130 	frame = td->td_frame;
131 	sa = &td->td_sa;
132 
133 #ifdef COMPAT_43
134 	if (__predict_false(frame->tf_cs == 7 && frame->tf_rip == 2)) {
135 		/*
136 		 * In lcall $7,$0 after int $0x80.  Convert the user
137 		 * frame to what it would be for a direct int 0x80 instead
138 		 * of lcall $7,$0, by popping the lcall return address.
139 		 */
140 		error = fueword32((void *)frame->tf_rsp, &eip);
141 		if (error == -1)
142 			return (EFAULT);
143 		cs = fuword16((void *)(frame->tf_rsp + sizeof(u_int32_t)));
144 		if (cs == -1)
145 			return (EFAULT);
146 
147 		/*
148 		 * Unwind in-kernel frame after all stack frame pieces
149 		 * were successfully read.
150 		 */
151 		frame->tf_rip = eip;
152 		frame->tf_cs = cs;
153 		frame->tf_rsp += 2 * sizeof(u_int32_t);
154 		frame->tf_err = 7;		/* size of lcall $7,$0 */
155 	}
156 #endif
157 
158 	params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t);
159 	sa->code = frame->tf_rax;
160 	sa->original_code = sa->code;
161 
162 	/*
163 	 * Need to check if this is a 32 bit or 64 bit syscall.
164 	 */
165 	if (sa->code == SYS_syscall) {
166 		/*
167 		 * Code is first argument, followed by actual args.
168 		 */
169 		error = fueword32(params, &tmp);
170 		if (error == -1)
171 			return (EFAULT);
172 		sa->code = tmp;
173 		params += sizeof(int);
174 	} else if (sa->code == SYS___syscall) {
175 		/*
176 		 * Like syscall, but code is a quad, so as to maintain
177 		 * quad alignment for the rest of the arguments.
178 		 * We use a 32-bit fetch in case params is not
179 		 * aligned.
180 		 */
181 		error = fueword32(params, &tmp);
182 		if (error == -1)
183 			return (EFAULT);
184 		sa->code = tmp;
185 		params += sizeof(quad_t);
186 	}
187  	if (sa->code >= p->p_sysent->sv_size)
188  		sa->callp = &p->p_sysent->sv_table[0];
189   	else
190  		sa->callp = &p->p_sysent->sv_table[sa->code];
191 
192 	if (params != NULL && sa->callp->sy_narg != 0)
193 		error = copyin(params, (caddr_t)args,
194 		    (u_int)(sa->callp->sy_narg * sizeof(int)));
195 	else
196 		error = 0;
197 
198 	for (i = 0; i < sa->callp->sy_narg; i++)
199 		sa->args[i] = args[i];
200 
201 	if (error == 0) {
202 		td->td_retval[0] = 0;
203 		td->td_retval[1] = frame->tf_rdx;
204 	}
205 
206 	return (error);
207 }
208 
209 #include "../../kern/subr_syscall.c"
210 
211 void
212 ia32_syscall(struct trapframe *frame)
213 {
214 	struct thread *td;
215 	register_t orig_tf_rflags;
216 	ksiginfo_t ksi;
217 
218 	kmsan_mark(frame, sizeof(*frame), KMSAN_STATE_INITED);
219 
220 	orig_tf_rflags = frame->tf_rflags;
221 	td = curthread;
222 	td->td_frame = frame;
223 
224 	syscallenter(td);
225 
226 	/*
227 	 * Traced syscall.
228 	 */
229 	if (orig_tf_rflags & PSL_T) {
230 		frame->tf_rflags &= ~PSL_T;
231 		ksiginfo_init_trap(&ksi);
232 		ksi.ksi_signo = SIGTRAP;
233 		ksi.ksi_code = TRAP_TRACE;
234 		ksi.ksi_addr = (void *)frame->tf_rip;
235 		trapsignal(td, &ksi);
236 	}
237 
238 	syscallret(td);
239 	amd64_syscall_ret_flush_l1d(td->td_errno);
240 }
241 
242 static void
243 ia32_syscall_enable(void *dummy)
244 {
245 
246  	setidt(IDT_SYSCALL, pti ? &IDTVEC(int0x80_syscall_pti) :
247 	    &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0);
248 }
249 
250 static void
251 ia32_syscall_disable(void *dummy)
252 {
253 
254  	setidt(IDT_SYSCALL, pti ? &IDTVEC(rsvd_pti) : &IDTVEC(rsvd),
255 	    SDT_SYSIGT, SEL_KPL, 0);
256 }
257 
258 SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL);
259 SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL);
260 
261 #ifdef COMPAT_43
262 int
263 setup_lcall_gate(void)
264 {
265 	struct i386_ldt_args uap;
266 	struct user_segment_descriptor desc;
267 	uint32_t lcall_addr;
268 	int error;
269 
270 	bzero(&uap, sizeof(uap));
271 	uap.start = 0;
272 	uap.num = 1;
273 	lcall_addr = PROC_PS_STRINGS(curproc) -
274 	    (_binary_elf_vdso32_so_1_end - _binary_elf_vdso32_so_1_start) +
275 	    VDSO_LCALL_TRAMP_OFFSET;
276 	bzero(&desc, sizeof(desc));
277 	desc.sd_type = SDT_MEMERA;
278 	desc.sd_dpl = SEL_UPL;
279 	desc.sd_p = 1;
280 	desc.sd_def32 = 1;
281 	desc.sd_gran = 1;
282 	desc.sd_lolimit = 0xffff;
283 	desc.sd_hilimit = 0xf;
284 	desc.sd_lobase = lcall_addr;
285 	desc.sd_hibase = lcall_addr >> 24;
286 	error = amd64_set_ldt(curthread, &uap, &desc);
287 	if (error != 0)
288 		return (error);
289 
290 	return (0);
291 }
292 #endif
293