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