2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/sysproto.h>
34 #include <sys/kernel.h>
36 #include <sys/mutex.h>
37 #include <sys/pioctl.h>
39 #include <sys/ptrace.h>
40 #include <sys/signalvar.h>
41 #include <sys/syscall.h>
42 #include <sys/sysent.h>
43 #include <machine/cpu.h>
44 #include <machine/fpu.h>
45 #include <machine/frame.h>
46 #include <machine/md_var.h>
47 #include <i386/include/psl.h>
49 #include <security/audit/audit.h>
51 extern char *syscallnames[];
54 ia32_syscall(struct trapframe *tf)
67 PCPU_INC(cnt.v_syscall);
70 params = (caddr_t)(tf->tf_special.sp & ((1L<<32)-1)) +
72 code = tf->tf_scratch.gr8; /* eax */
73 eflags = ia64_get_eflag();
76 if (p->p_sysent->sv_prepsyscall == NULL) {
77 if (code == SYS_syscall) {
78 /* Code is first argument, followed by actual args. */
79 code = fuword32(params);
80 params += sizeof(int);
81 } else if (code == SYS___syscall) {
83 * Like syscall, but code is a quad, so as to maintain
84 * quad alignment for the rest of the arguments. We
85 * use a 32-bit fetch in case params is not aligned.
87 code = fuword32(params);
88 params += sizeof(quad_t);
91 (*p->p_sysent->sv_prepsyscall)(tf, args, &code, ¶ms);
93 if (p->p_sysent->sv_mask)
94 code &= p->p_sysent->sv_mask;
96 if (code >= p->p_sysent->sv_size)
97 callp = &p->p_sysent->sv_table[0];
99 callp = &p->p_sysent->sv_table[code];
101 narg = callp->sy_narg;
103 /* copyin and the ktrsyscall()/ktrsysret() code is MP-aware */
104 if (params != NULL && narg != 0)
105 error = copyin(params, (caddr_t)args, narg * sizeof(int));
109 for (i = 0; i < narg; i++)
113 if (KTRPOINT(td, KTR_SYSCALL))
114 ktrsyscall(code, narg, args64);
116 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
117 td->td_proc->p_pid, td->td_proc->p_comm, code);
120 td->td_retval[0] = 0;
121 td->td_retval[1] = tf->tf_scratch.gr10; /* edx */
123 STOPEVENT(p, S_SCE, narg);
125 PTRACESTOP_SC(p, td, S_PT_SCE);
127 AUDIT_SYSCALL_ENTER(code, td);
128 error = (*callp->sy_call)(td, args64);
129 AUDIT_SYSCALL_EXIT(error, td);
134 tf->tf_scratch.gr8 = td->td_retval[0]; /* eax */
135 tf->tf_scratch.gr10 = td->td_retval[1]; /* edx */
136 ia64_set_eflag(ia64_get_eflag() & ~PSL_C);
141 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
142 * int 0x80 is 2 bytes. XXX Assume int 0x80.
144 tf->tf_special.iip -= 2;
151 if (p->p_sysent->sv_errsize) {
152 if (error >= p->p_sysent->sv_errsize)
153 error = -1; /* XXX */
155 error = p->p_sysent->sv_errtbl[error];
157 tf->tf_scratch.gr8 = error;
158 ia64_set_eflag(ia64_get_eflag() | PSL_C);
165 if ((eflags & PSL_T) && !(eflags & PSL_VM)) {
166 ia64_set_eflag(ia64_get_eflag() & ~PSL_T);
167 ksiginfo_init_trap(&ksi);
168 ksi.ksi_signo = SIGTRAP;
169 ksi.ksi_code = TRAP_TRACE;
170 ksi.ksi_addr = (void *)tf->tf_special.iip;
171 trapsignal(td, &ksi);
175 * Check for misbehavior.
177 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
178 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
179 KASSERT(td->td_critnest == 0,
180 ("System call %s returning in a critical section",
181 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
182 KASSERT(td->td_locks == 0,
183 ("System call %s returning with %d locks held",
184 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
188 * End of syscall tracing.
190 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
191 td->td_proc->p_pid, td->td_proc->p_comm, code);
193 if (KTRPOINT(td, KTR_SYSRET))
194 ktrsysret(code, error, td->td_retval[0]);
198 * This works because errno is findable through the
199 * register set. If we ever support an emulation where this
200 * is not the case, this code will need to be revisited.
202 STOPEVENT(p, S_SCX, code);
204 PTRACESTOP_SC(p, td, S_PT_SCX);
208 * ia32_trap() is called from exception.S to handle the IA-32 specific
209 * interruption vectors.
212 ia32_trap(int vector, struct trapframe *tf)
220 KASSERT(TRAPF_USERMODE(tf), ("%s: In kernel mode???", __func__));
222 ia64_set_fpsr(IA64_FPSR_DEFAULT);
223 PCPU_INC(cnt.v_trap);
229 if (td->td_ucred != p->p_ucred)
230 cred_update_thread(td);
234 case IA64_VEC_IA32_EXCEPTION:
235 switch ((tf->tf_special.isr >> 16) & 0xffff) {
236 case IA32_EXCEPTION_DIVIDE:
240 case IA32_EXCEPTION_DEBUG:
241 case IA32_EXCEPTION_BREAK:
244 case IA32_EXCEPTION_OVERFLOW:
248 case IA32_EXCEPTION_BOUND:
252 case IA32_EXCEPTION_DNA:
256 case IA32_EXCEPTION_NOT_PRESENT:
257 case IA32_EXCEPTION_STACK_FAULT:
258 case IA32_EXCEPTION_GPFAULT:
259 ucode = (tf->tf_special.isr & 0xffff) + BUS_SEGM_FAULT;
262 case IA32_EXCEPTION_FPERROR:
266 case IA32_EXCEPTION_ALIGNMENT_CHECK:
267 ucode = tf->tf_special.ifa; /* VA */
270 case IA32_EXCEPTION_STREAMING_SIMD:
275 trap_panic(vector, tf);
280 case IA64_VEC_IA32_INTERCEPT:
281 /* XXX Maybe need to emulate ia32 instruction. */
282 trap_panic(vector, tf);
284 case IA64_VEC_IA32_INTERRUPT:
285 /* INT n instruction - probably a syscall. */
286 if (((tf->tf_special.isr >> 16) & 0xffff) == 0x80) {
290 ucode = (tf->tf_special.isr >> 16) & 0xffff;
295 /* Should never happen of course. */
296 trap_panic(vector, tf);
300 KASSERT(sig != 0, ("%s: signal not set", __func__));
302 ksiginfo_init_trap(&ksi);
304 ksi.ksi_code = (int)ucode; /* XXX */
306 trapsignal(td, &ksi);
310 mtx_assert(&Giant, MA_NOTOWNED);