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