xref: /freebsd/sys/amd64/ia32/ia32_syscall.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*-
2  * Copyright (C) 1994, David Greenman
3  * Copyright (c) 1990, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the University of Utah, and William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * 386 Trap and System call handling
43  */
44 
45 #include "opt_clock.h"
46 #include "opt_compat.h"
47 #include "opt_cpu.h"
48 #include "opt_isa.h"
49 
50 #include <sys/param.h>
51 #include <sys/bus.h>
52 #include <sys/systm.h>
53 #include <sys/proc.h>
54 #include <sys/pioctl.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(rsvd);
97 
98 void ia32_syscall(struct trapframe *frame);	/* Called from asm code */
99 
100 void
101 ia32_set_syscall_retval(struct thread *td, int error)
102 {
103 
104 	cpu_set_syscall_retval(td, error);
105 }
106 
107 int
108 ia32_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
109 {
110 	struct proc *p;
111 	struct trapframe *frame;
112 	caddr_t params;
113 	u_int32_t args[8];
114 	int error, i;
115 
116 	p = td->td_proc;
117 	frame = td->td_frame;
118 
119 	params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t);
120 	sa->code = frame->tf_rax;
121 
122 	/*
123 	 * Need to check if this is a 32 bit or 64 bit syscall.
124 	 */
125 	if (sa->code == SYS_syscall) {
126 		/*
127 		 * Code is first argument, followed by actual args.
128 		 */
129 		sa->code = fuword32(params);
130 		params += sizeof(int);
131 	} else if (sa->code == SYS___syscall) {
132 		/*
133 		 * Like syscall, but code is a quad, so as to maintain
134 		 * quad alignment for the rest of the arguments.
135 		 * We use a 32-bit fetch in case params is not
136 		 * aligned.
137 		 */
138 		sa->code = fuword32(params);
139 		params += sizeof(quad_t);
140 	}
141  	if (p->p_sysent->sv_mask)
142  		sa->code &= p->p_sysent->sv_mask;
143  	if (sa->code >= p->p_sysent->sv_size)
144  		sa->callp = &p->p_sysent->sv_table[0];
145   	else
146  		sa->callp = &p->p_sysent->sv_table[sa->code];
147 	sa->narg = sa->callp->sy_narg;
148 
149 	if (params != NULL && sa->narg != 0)
150 		error = copyin(params, (caddr_t)args,
151 		    (u_int)(sa->narg * sizeof(int)));
152 	else
153 		error = 0;
154 
155 	for (i = 0; i < sa->narg; i++)
156 		sa->args[i] = args[i];
157 
158 	if (error == 0) {
159 		td->td_retval[0] = 0;
160 		td->td_retval[1] = frame->tf_rdx;
161 	}
162 
163 	return (error);
164 }
165 
166 #include "../../kern/subr_syscall.c"
167 
168 void
169 ia32_syscall(struct trapframe *frame)
170 {
171 	struct thread *td;
172 	struct syscall_args sa;
173 	register_t orig_tf_rflags;
174 	int error;
175 	ksiginfo_t ksi;
176 
177 	orig_tf_rflags = frame->tf_rflags;
178 	td = curthread;
179 	td->td_frame = frame;
180 
181 	error = syscallenter(td, &sa);
182 
183 	/*
184 	 * Traced syscall.
185 	 */
186 	if (orig_tf_rflags & PSL_T) {
187 		frame->tf_rflags &= ~PSL_T;
188 		ksiginfo_init_trap(&ksi);
189 		ksi.ksi_signo = SIGTRAP;
190 		ksi.ksi_code = TRAP_TRACE;
191 		ksi.ksi_addr = (void *)frame->tf_rip;
192 		trapsignal(td, &ksi);
193 	}
194 
195 	syscallret(td, error, &sa);
196 }
197 
198 static void
199 ia32_syscall_enable(void *dummy)
200 {
201 
202  	setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0);
203 }
204 
205 static void
206 ia32_syscall_disable(void *dummy)
207 {
208 
209  	setidt(IDT_SYSCALL, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
210 }
211 
212 SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL);
213 SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL);
214 
215 #ifdef COMPAT_43
216 int
217 setup_lcall_gate(void)
218 {
219 	struct i386_ldt_args uap;
220 	struct user_segment_descriptor descs[2];
221 	struct gate_descriptor *ssd;
222 	uint32_t lcall_addr;
223 	int error;
224 
225 	bzero(&uap, sizeof(uap));
226 	uap.start = 0;
227 	uap.num = 2;
228 
229 	/*
230 	 * This is the easiest way to cut the space for system
231 	 * descriptor in ldt.  Manually adjust the descriptor type to
232 	 * the call gate later.
233 	 */
234 	bzero(&descs[0], sizeof(descs));
235 	descs[0].sd_type = SDT_SYSNULL;
236 	descs[1].sd_type = SDT_SYSNULL;
237 	error = amd64_set_ldt(curthread, &uap, descs);
238 	if (error != 0)
239 		return (error);
240 
241 	lcall_addr = curproc->p_sysent->sv_psstrings - sz_lcall_tramp;
242 	mtx_lock(&dt_lock);
243 	ssd = (struct gate_descriptor *)(curproc->p_md.md_ldt->ldt_base);
244 	bzero(ssd, sizeof(*ssd));
245 	ssd->gd_looffset = lcall_addr;
246 	ssd->gd_hioffset = lcall_addr >> 16;
247 	ssd->gd_selector = _ucode32sel;
248 	ssd->gd_type = SDT_SYSCGT;
249 	ssd->gd_dpl = SEL_UPL;
250 	ssd->gd_p = 1;
251 	mtx_unlock(&dt_lock);
252 
253 	return (0);
254 }
255 #endif
256