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>
52 extern char *syscallnames[];
56 ia32_syscall(struct trapframe *tf)
68 PCPU_LAZY_INC(cnt.v_syscall);
71 params = (caddr_t)(tf->tf_special.sp & ((1L<<32)-1)) +
73 code = tf->tf_scratch.gr8; /* eax */
74 eflags = ia64_get_eflag();
77 if (p->p_sysent->sv_prepsyscall == NULL) {
78 if (code == SYS_syscall) {
79 /* Code is first argument, followed by actual args. */
80 code = fuword32(params);
81 params += sizeof(int);
82 } else if (code == SYS___syscall) {
84 * Like syscall, but code is a quad, so as to maintain
85 * quad alignment for the rest of the arguments. We
86 * use a 32-bit fetch in case params is not aligned.
88 code = fuword32(params);
89 params += sizeof(quad_t);
92 (*p->p_sysent->sv_prepsyscall)(tf, args, &code, ¶ms);
94 if (p->p_sysent->sv_mask)
95 code &= p->p_sysent->sv_mask;
97 if (code >= p->p_sysent->sv_size)
98 callp = &p->p_sysent->sv_table[0];
100 callp = &p->p_sysent->sv_table[code];
102 narg = callp->sy_narg & SYF_ARGMASK;
104 /* copyin and the ktrsyscall()/ktrsysret() code is MP-aware */
105 if (params != NULL && narg != 0)
106 error = copyin(params, (caddr_t)args, narg * sizeof(int));
110 for (i = 0; i < narg; i++)
114 if (KTRPOINT(td, KTR_SYSCALL))
115 ktrsyscall(code, narg, args64);
117 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
118 td->td_proc->p_pid, td->td_proc->p_comm, code);
121 * Try to run the syscall without Giant if the syscall
124 if ((callp->sy_narg & SYF_MPSAFE) == 0)
128 td->td_retval[0] = 0;
129 td->td_retval[1] = tf->tf_scratch.gr10; /* edx */
131 STOPEVENT(p, S_SCE, narg);
133 PTRACESTOP_SC(p, td, S_PT_SCE);
135 AUDIT_SYSCALL_ENTER(code, td);
136 error = (*callp->sy_call)(td, args64);
137 AUDIT_SYSCALL_EXIT(error, td);
142 tf->tf_scratch.gr8 = td->td_retval[0]; /* eax */
143 tf->tf_scratch.gr10 = td->td_retval[1]; /* edx */
144 ia64_set_eflag(ia64_get_eflag() & ~PSL_C);
149 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
150 * int 0x80 is 2 bytes. XXX Assume int 0x80.
152 tf->tf_special.iip -= 2;
159 if (p->p_sysent->sv_errsize) {
160 if (error >= p->p_sysent->sv_errsize)
161 error = -1; /* XXX */
163 error = p->p_sysent->sv_errtbl[error];
165 tf->tf_scratch.gr8 = error;
166 ia64_set_eflag(ia64_get_eflag() | PSL_C);
171 * Release Giant if we previously set it.
173 if ((callp->sy_narg & SYF_MPSAFE) == 0)
179 if ((eflags & PSL_T) && !(eflags & PSL_VM)) {
180 ia64_set_eflag(ia64_get_eflag() & ~PSL_T);
181 trapsignal(td, SIGTRAP, 0);
185 * End of syscall tracing.
187 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
188 td->td_proc->p_pid, td->td_proc->p_comm, code);
190 if (KTRPOINT(td, KTR_SYSRET))
191 ktrsysret(code, error, td->td_retval[0]);
195 * This works because errno is findable through the
196 * register set. If we ever support an emulation where this
197 * is not the case, this code will need to be revisited.
199 STOPEVENT(p, S_SCX, code);
201 PTRACESTOP_SC(p, td, S_PT_SCX);
203 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
204 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
205 mtx_assert(&sched_lock, MA_NOTOWNED);
206 mtx_assert(&Giant, MA_NOTOWNED);
210 * ia32_trap() is called from exception.S to handle the IA-32 specific
211 * interruption vectors.
214 ia32_trap(int vector, struct trapframe *tf)
222 KASSERT(TRAPF_USERMODE(tf), ("%s: In kernel mode???", __func__));
224 ia64_set_fpsr(IA64_FPSR_DEFAULT);
225 PCPU_LAZY_INC(cnt.v_trap);
229 sticks = td->td_sticks;
231 if (td->td_ucred != p->p_ucred)
232 cred_update_thread(td);
236 case IA64_VEC_IA32_EXCEPTION:
237 switch ((tf->tf_special.isr >> 16) & 0xffff) {
238 case IA32_EXCEPTION_DIVIDE:
242 case IA32_EXCEPTION_DEBUG:
243 case IA32_EXCEPTION_BREAK:
246 case IA32_EXCEPTION_OVERFLOW:
250 case IA32_EXCEPTION_BOUND:
254 case IA32_EXCEPTION_DNA:
258 case IA32_EXCEPTION_NOT_PRESENT:
259 case IA32_EXCEPTION_STACK_FAULT:
260 case IA32_EXCEPTION_GPFAULT:
261 ucode = (tf->tf_special.isr & 0xffff) + BUS_SEGM_FAULT;
264 case IA32_EXCEPTION_FPERROR:
268 case IA32_EXCEPTION_ALIGNMENT_CHECK:
269 ucode = tf->tf_special.ifa; /* VA */
272 case IA32_EXCEPTION_STREAMING_SIMD:
277 trap_panic(vector, tf);
282 case IA64_VEC_IA32_INTERCEPT:
283 /* XXX Maybe need to emulate ia32 instruction. */
284 trap_panic(vector, tf);
286 case IA64_VEC_IA32_INTERRUPT:
287 /* INT n instruction - probably a syscall. */
288 if (((tf->tf_special.isr >> 16) & 0xffff) == 0x80) {
292 ucode = (tf->tf_special.isr >> 16) & 0xffff;
297 /* Should never happen of course. */
298 trap_panic(vector, tf);
302 KASSERT(sig != 0, ("%s: signal not set", __func__));
304 trapsignal(td, sig, ucode);
307 userret(td, tf, sticks);
308 mtx_assert(&Giant, MA_NOTOWNED);