xref: /freebsd/sys/amd64/ia32/ia32_syscall.c (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
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/mutex.h>
59 #include <sys/proc.h>
60 #include <sys/ptrace.h>
61 #include <sys/resourcevar.h>
62 #include <sys/signalvar.h>
63 #include <sys/syscall.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysent.h>
66 #include <sys/uio.h>
67 #include <sys/vmmeter.h>
68 #include <security/audit/audit.h>
69 
70 #include <vm/vm.h>
71 #include <vm/vm_param.h>
72 #include <vm/pmap.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_extern.h>
77 
78 #include <machine/cpu.h>
79 #include <machine/intr_machdep.h>
80 #include <machine/md_var.h>
81 
82 #include <compat/freebsd32/freebsd32_signal.h>
83 #include <compat/freebsd32/freebsd32_util.h>
84 #include <compat/ia32/ia32_signal.h>
85 #include <machine/psl.h>
86 #include <machine/segments.h>
87 #include <machine/specialreg.h>
88 #include <machine/sysarch.h>
89 #include <machine/frame.h>
90 #include <machine/md_var.h>
91 #include <machine/pcb.h>
92 #include <machine/cpufunc.h>
93 
94 #define	IDTVEC(name)	__CONCAT(X,name)
95 
96 extern inthand_t IDTVEC(int0x80_syscall), IDTVEC(int0x80_syscall_pti),
97     IDTVEC(rsvd), IDTVEC(rsvd_pti);
98 
99 void ia32_syscall(struct trapframe *frame);	/* Called from asm code */
100 
101 void
102 ia32_set_syscall_retval(struct thread *td, int error)
103 {
104 
105 	cpu_set_syscall_retval(td, error);
106 }
107 
108 int
109 ia32_fetch_syscall_args(struct thread *td)
110 {
111 	struct proc *p;
112 	struct trapframe *frame;
113 	struct syscall_args *sa;
114 	caddr_t params;
115 	u_int32_t args[8], tmp;
116 	int error, i;
117 #ifdef COMPAT_43
118 	u_int32_t eip;
119 	int cs;
120 #endif
121 
122 	p = td->td_proc;
123 	frame = td->td_frame;
124 	sa = &td->td_sa;
125 
126 #ifdef COMPAT_43
127 	if (__predict_false(frame->tf_cs == 7 && frame->tf_rip == 2)) {
128 		/*
129 		 * In lcall $7,$0 after int $0x80.  Convert the user
130 		 * frame to what it would be for a direct int 0x80 instead
131 		 * of lcall $7,$0, by popping the lcall return address.
132 		 */
133 		error = fueword32((void *)frame->tf_rsp, &eip);
134 		if (error == -1)
135 			return (EFAULT);
136 		cs = fuword16((void *)(frame->tf_rsp + sizeof(u_int32_t)));
137 		if (cs == -1)
138 			return (EFAULT);
139 
140 		/*
141 		 * Unwind in-kernel frame after all stack frame pieces
142 		 * were successfully read.
143 		 */
144 		frame->tf_rip = eip;
145 		frame->tf_cs = cs;
146 		frame->tf_rsp += 2 * sizeof(u_int32_t);
147 		frame->tf_err = 7;		/* size of lcall $7,$0 */
148 	}
149 #endif
150 
151 	params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t);
152 	sa->code = frame->tf_rax;
153 
154 	/*
155 	 * Need to check if this is a 32 bit or 64 bit syscall.
156 	 */
157 	if (sa->code == SYS_syscall) {
158 		/*
159 		 * Code is first argument, followed by actual args.
160 		 */
161 		error = fueword32(params, &tmp);
162 		if (error == -1)
163 			return (EFAULT);
164 		sa->code = tmp;
165 		params += sizeof(int);
166 	} else if (sa->code == SYS___syscall) {
167 		/*
168 		 * Like syscall, but code is a quad, so as to maintain
169 		 * quad alignment for the rest of the arguments.
170 		 * We use a 32-bit fetch in case params is not
171 		 * aligned.
172 		 */
173 		error = fueword32(params, &tmp);
174 		if (error == -1)
175 			return (EFAULT);
176 		sa->code = tmp;
177 		params += sizeof(quad_t);
178 	}
179  	if (sa->code >= p->p_sysent->sv_size)
180  		sa->callp = &p->p_sysent->sv_table[0];
181   	else
182  		sa->callp = &p->p_sysent->sv_table[sa->code];
183 	sa->narg = sa->callp->sy_narg;
184 
185 	if (params != NULL && sa->narg != 0)
186 		error = copyin(params, (caddr_t)args,
187 		    (u_int)(sa->narg * sizeof(int)));
188 	else
189 		error = 0;
190 
191 	for (i = 0; i < sa->narg; i++)
192 		sa->args[i] = args[i];
193 
194 	if (error == 0) {
195 		td->td_retval[0] = 0;
196 		td->td_retval[1] = frame->tf_rdx;
197 	}
198 
199 	return (error);
200 }
201 
202 #include "../../kern/subr_syscall.c"
203 
204 void
205 ia32_syscall(struct trapframe *frame)
206 {
207 	struct thread *td;
208 	register_t orig_tf_rflags;
209 	ksiginfo_t ksi;
210 
211 	orig_tf_rflags = frame->tf_rflags;
212 	td = curthread;
213 	td->td_frame = frame;
214 
215 	syscallenter(td);
216 
217 	/*
218 	 * Traced syscall.
219 	 */
220 	if (orig_tf_rflags & PSL_T) {
221 		frame->tf_rflags &= ~PSL_T;
222 		ksiginfo_init_trap(&ksi);
223 		ksi.ksi_signo = SIGTRAP;
224 		ksi.ksi_code = TRAP_TRACE;
225 		ksi.ksi_addr = (void *)frame->tf_rip;
226 		trapsignal(td, &ksi);
227 	}
228 
229 	syscallret(td);
230 	amd64_syscall_ret_flush_l1d(td->td_errno);
231 }
232 
233 static void
234 ia32_syscall_enable(void *dummy)
235 {
236 
237  	setidt(IDT_SYSCALL, pti ? &IDTVEC(int0x80_syscall_pti) :
238 	    &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0);
239 }
240 
241 static void
242 ia32_syscall_disable(void *dummy)
243 {
244 
245  	setidt(IDT_SYSCALL, pti ? &IDTVEC(rsvd_pti) : &IDTVEC(rsvd),
246 	    SDT_SYSIGT, SEL_KPL, 0);
247 }
248 
249 SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL);
250 SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL);
251 
252 #ifdef COMPAT_43
253 int
254 setup_lcall_gate(void)
255 {
256 	struct i386_ldt_args uap;
257 	struct user_segment_descriptor desc;
258 	uint32_t lcall_addr;
259 	int error;
260 
261 	bzero(&uap, sizeof(uap));
262 	uap.start = 0;
263 	uap.num = 1;
264 	lcall_addr = curproc->p_sysent->sv_psstrings - sz_lcall_tramp;
265 	bzero(&desc, sizeof(desc));
266 	desc.sd_type = SDT_MEMERA;
267 	desc.sd_dpl = SEL_UPL;
268 	desc.sd_p = 1;
269 	desc.sd_def32 = 1;
270 	desc.sd_gran = 1;
271 	desc.sd_lolimit = 0xffff;
272 	desc.sd_hilimit = 0xf;
273 	desc.sd_lobase = lcall_addr;
274 	desc.sd_hibase = lcall_addr >> 24;
275 	error = amd64_set_ldt(curthread, &uap, &desc);
276 	if (error != 0)
277 		return (error);
278 
279 	return (0);
280 }
281 #endif
282