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 #include <compat/ia32/ia32_util.h>
54 ia32_set_syscall_retval(struct thread *td, int error)
63 tf->tf_scratch.gr8 = td->td_retval[0]; /* eax */
64 tf->tf_scratch.gr10 = td->td_retval[1]; /* edx */
65 ia64_set_eflag(ia64_get_eflag() & ~PSL_C);
70 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
71 * int 0x80 is 2 bytes. XXX Assume int 0x80.
73 tf->tf_special.iip -= 2;
81 if (p->p_sysent->sv_errsize) {
82 if (error >= p->p_sysent->sv_errsize)
85 error = p->p_sysent->sv_errtbl[error];
87 tf->tf_scratch.gr8 = error;
88 ia64_set_eflag(ia64_get_eflag() | PSL_C);
94 ia32_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
105 params = (caddr_t)(tf->tf_special.sp & ((1L<<32)-1)) +
107 sa->code = tf->tf_scratch.gr8; /* eax */
109 if (sa->code == SYS_syscall) {
110 /* Code is first argument, followed by actual args. */
111 sa->code = fuword32(params);
112 params += sizeof(int);
113 } else if (sa->code == SYS___syscall) {
115 * Like syscall, but code is a quad, so as to maintain
116 * quad alignment for the rest of the arguments. We
117 * use a 32-bit fetch in case params is not aligned.
119 sa->code = fuword32(params);
120 params += sizeof(quad_t);
123 if (p->p_sysent->sv_mask)
124 sa->code &= p->p_sysent->sv_mask;
125 if (sa->code >= p->p_sysent->sv_size)
126 sa->callp = &p->p_sysent->sv_table[0];
128 sa->callp = &p->p_sysent->sv_table[sa->code];
129 sa->narg = sa->callp->sy_narg;
131 if (params != NULL && sa->narg != 0)
132 error = copyin(params, (caddr_t)args, sa->narg * sizeof(int));
135 sa->args = &sa->args32[0];
138 for (i = 0; i < sa->narg; i++)
139 sa->args32[i] = args[i];
140 td->td_retval[0] = 0;
141 td->td_retval[1] = tf->tf_scratch.gr10; /* edx */
148 ia32_syscall(struct trapframe *tf)
151 struct syscall_args sa;
157 eflags = ia64_get_eflag();
159 error = syscallenter(td, &sa);
164 if ((eflags & PSL_T) && !(eflags & PSL_VM)) {
165 ia64_set_eflag(ia64_get_eflag() & ~PSL_T);
166 ksiginfo_init_trap(&ksi);
167 ksi.ksi_signo = SIGTRAP;
168 ksi.ksi_code = TRAP_TRACE;
169 ksi.ksi_addr = (void *)tf->tf_special.iip;
170 trapsignal(td, &ksi);
173 syscallret(td, error, &sa);
177 * ia32_trap() is called from exception.S to handle the IA-32 specific
178 * interruption vectors.
181 ia32_trap(int vector, struct trapframe *tf)
189 KASSERT(TRAPF_USERMODE(tf), ("%s: In kernel mode???", __func__));
191 ia64_set_fpsr(IA64_FPSR_DEFAULT);
192 PCPU_INC(cnt.v_trap);
198 if (td->td_ucred != p->p_ucred)
199 cred_update_thread(td);
203 case IA64_VEC_IA32_EXCEPTION:
204 switch ((tf->tf_special.isr >> 16) & 0xffff) {
205 case IA32_EXCEPTION_DIVIDE:
209 case IA32_EXCEPTION_DEBUG:
210 case IA32_EXCEPTION_BREAK:
213 case IA32_EXCEPTION_OVERFLOW:
217 case IA32_EXCEPTION_BOUND:
221 case IA32_EXCEPTION_DNA:
225 case IA32_EXCEPTION_NOT_PRESENT:
226 case IA32_EXCEPTION_STACK_FAULT:
227 case IA32_EXCEPTION_GPFAULT:
228 ucode = (tf->tf_special.isr & 0xffff) + BUS_SEGM_FAULT;
231 case IA32_EXCEPTION_FPERROR:
235 case IA32_EXCEPTION_ALIGNMENT_CHECK:
236 ucode = tf->tf_special.ifa; /* VA */
239 case IA32_EXCEPTION_STREAMING_SIMD:
244 trap_panic(vector, tf);
249 case IA64_VEC_IA32_INTERCEPT:
250 /* XXX Maybe need to emulate ia32 instruction. */
251 trap_panic(vector, tf);
253 case IA64_VEC_IA32_INTERRUPT:
254 /* INT n instruction - probably a syscall. */
255 if (((tf->tf_special.isr >> 16) & 0xffff) == 0x80) {
259 ucode = (tf->tf_special.isr >> 16) & 0xffff;
264 /* Should never happen of course. */
265 trap_panic(vector, tf);
269 KASSERT(sig != 0, ("%s: signal not set", __func__));
271 ksiginfo_init_trap(&ksi);
273 ksi.ksi_code = (int)ucode; /* XXX */
275 trapsignal(td, &ksi);
279 mtx_assert(&Giant, MA_NOTOWNED);