2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the University of Utah, and William Jolitz.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * 386 Trap and System call handling
45 #include "opt_clock.h"
48 #include "opt_ktrace.h"
50 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/pioctl.h>
55 #include <sys/kernel.h>
58 #include <sys/mutex.h>
59 #include <sys/ptrace.h>
60 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
62 #include <sys/syscall.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
66 #include <sys/vmmeter.h>
68 #include <sys/ktrace.h>
70 #include <security/audit/audit.h>
73 #include <vm/vm_param.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_extern.h>
80 #include <machine/cpu.h>
81 #include <machine/intr_machdep.h>
82 #include <machine/md_var.h>
84 #define IDTVEC(name) __CONCAT(X,name)
86 extern inthand_t IDTVEC(int0x80_syscall), IDTVEC(rsvd);
87 extern const char *freebsd32_syscallnames[];
89 void ia32_syscall(struct trapframe *frame); /* Called from asm code */
92 ia32_syscall(struct trapframe *frame)
97 struct thread *td = curthread;
98 struct proc *p = td->td_proc;
99 register_t orig_tf_rflags;
108 * note: PCPU_LAZY_INC() can only be used if we can afford
109 * occassional inaccuracy in the count.
111 PCPU_LAZY_INC(cnt.v_syscall);
114 td->td_frame = frame;
115 if (td->td_ucred != p->p_ucred)
116 cred_update_thread(td);
117 params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t);
118 code = frame->tf_rax;
119 orig_tf_rflags = frame->tf_rflags;
121 if (p->p_sysent->sv_prepsyscall) {
123 * The prep code is MP aware.
125 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, ¶ms);
128 * Need to check if this is a 32 bit or 64 bit syscall.
129 * fuword is MP aware.
131 if (code == SYS_syscall) {
133 * Code is first argument, followed by actual args.
135 code = fuword32(params);
136 params += sizeof(int);
137 } else if (code == SYS___syscall) {
139 * Like syscall, but code is a quad, so as to maintain
140 * quad alignment for the rest of the arguments.
141 * We use a 32-bit fetch in case params is not
144 code = fuword32(params);
145 params += sizeof(quad_t);
149 if (p->p_sysent->sv_mask)
150 code &= p->p_sysent->sv_mask;
152 if (code >= p->p_sysent->sv_size)
153 callp = &p->p_sysent->sv_table[0];
155 callp = &p->p_sysent->sv_table[code];
157 narg = callp->sy_narg;
160 * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
162 if (params != NULL && narg != 0)
163 error = copyin(params, (caddr_t)args,
164 (u_int)(narg * sizeof(int)));
168 for (i = 0; i < narg; i++)
172 if (KTRPOINT(td, KTR_SYSCALL))
173 ktrsyscall(code, narg, args64);
175 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
176 td->td_proc->p_pid, td->td_proc->p_comm, code);
179 td->td_retval[0] = 0;
180 td->td_retval[1] = frame->tf_rdx;
182 STOPEVENT(p, S_SCE, narg);
184 PTRACESTOP_SC(p, td, S_PT_SCE);
186 AUDIT_SYSCALL_ENTER(code, td);
187 error = (*callp->sy_call)(td, args64);
188 AUDIT_SYSCALL_EXIT(error, td);
193 frame->tf_rax = td->td_retval[0];
194 frame->tf_rdx = td->td_retval[1];
195 frame->tf_rflags &= ~PSL_C;
200 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
201 * int 0x80 is 2 bytes. We saved this in tf_err.
203 frame->tf_rip -= frame->tf_err;
210 if (p->p_sysent->sv_errsize) {
211 if (error >= p->p_sysent->sv_errsize)
212 error = -1; /* XXX */
214 error = p->p_sysent->sv_errtbl[error];
216 frame->tf_rax = error;
217 frame->tf_rflags |= PSL_C;
224 if (orig_tf_rflags & PSL_T) {
225 frame->tf_rflags &= ~PSL_T;
226 ksiginfo_init_trap(&ksi);
227 ksi.ksi_signo = SIGTRAP;
228 ksi.ksi_code = TRAP_TRACE;
229 ksi.ksi_addr = (void *)frame->tf_rip;
230 trapsignal(td, &ksi);
234 * Check for misbehavior.
236 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
237 (code >= 0 && code < SYS_MAXSYSCALL) ? freebsd32_syscallnames[code] : "???");
238 KASSERT(td->td_critnest == 0,
239 ("System call %s returning in a critical section",
240 (code >= 0 && code < SYS_MAXSYSCALL) ? freebsd32_syscallnames[code] : "???"));
241 KASSERT(td->td_locks == 0,
242 ("System call %s returning with %d locks held",
243 (code >= 0 && code < SYS_MAXSYSCALL) ? freebsd32_syscallnames[code] : "???",
247 * Handle reschedule and other end-of-syscall issues
251 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
252 td->td_proc->p_pid, td->td_proc->p_comm, code);
254 if (KTRPOINT(td, KTR_SYSRET))
255 ktrsysret(code, error, td->td_retval[0]);
259 * This works because errno is findable through the
260 * register set. If we ever support an emulation where this
261 * is not the case, this code will need to be revisited.
263 STOPEVENT(p, S_SCX, code);
265 PTRACESTOP_SC(p, td, S_PT_SCX);
270 ia32_syscall_enable(void *dummy)
273 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0);
277 ia32_syscall_disable(void *dummy)
280 setidt(IDT_SYSCALL, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
283 SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL);
284 SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL);