]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/ia64/ia32/ia32_trap.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / ia64 / ia32 / ia32_trap.c
1 /*-
2  * Copyright (c) 2004 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
14  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/ktr.h>
33 #include <sys/sysproto.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/pioctl.h>
38 #include <sys/proc.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>
48
49 #include <security/audit/audit.h>
50
51 #include <compat/ia32/ia32_util.h>
52
53 void
54 ia32_set_syscall_retval(struct thread *td, int error)
55 {
56         struct proc *p;
57         struct trapframe *tf;
58
59         tf = td->td_frame;
60
61         switch (error) {
62         case 0:
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);
66                 break;
67
68         case ERESTART:
69                 /*
70                  * Reconstruct pc, assuming lcall $X,y is 7 bytes,
71                  * int 0x80 is 2 bytes. XXX Assume int 0x80.
72                  */
73                 tf->tf_special.iip -= 2;
74                 break;
75
76         case EJUSTRETURN:
77                 break;
78
79         default:
80                 p = td->td_proc;
81                 if (p->p_sysent->sv_errsize) {
82                         if (error >= p->p_sysent->sv_errsize)
83                                 error = -1;     /* XXX */
84                         else
85                                 error = p->p_sysent->sv_errtbl[error];
86                 }
87                 tf->tf_scratch.gr8 = error;
88                 ia64_set_eflag(ia64_get_eflag() | PSL_C);
89                 break;
90         }
91 }
92
93 int
94 ia32_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
95 {
96         struct trapframe *tf;
97         struct proc *p;
98         uint32_t args[8];
99         caddr_t params;
100         int error, i;
101
102         tf = td->td_frame;
103         p = td->td_proc;
104
105         params = (caddr_t)(tf->tf_special.sp & ((1L<<32)-1)) +
106             sizeof(uint32_t);
107         sa->code = tf->tf_scratch.gr8;          /* eax */
108
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) {
114                 /*
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.
118                  */
119                 sa->code = fuword32(params);
120                 params += sizeof(quad_t);
121         }
122
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];
127         else
128                 sa->callp = &p->p_sysent->sv_table[sa->code];
129         sa->narg = sa->callp->sy_narg;
130
131         if (params != NULL && sa->narg != 0)
132                 error = copyin(params, (caddr_t)args, sa->narg * sizeof(int));
133         else
134                 error = 0;
135         sa->args = &sa->args32[0];
136
137         if (error == 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 */
142         }
143
144         return (error);
145 }
146
147 static void
148 ia32_syscall(struct trapframe *tf)
149 {
150         struct thread *td;
151         struct syscall_args sa;
152         register_t eflags;
153         int error;
154         ksiginfo_t ksi;
155
156         td = curthread;
157         eflags = ia64_get_eflag();
158
159         error = syscallenter(td, &sa);
160
161         /*
162          * Traced syscall.
163          */
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);
171         }
172
173         syscallret(td, error, &sa);
174 }
175
176 /*
177  * ia32_trap() is called from exception.S to handle the IA-32 specific
178  * interruption vectors.
179  */
180 void
181 ia32_trap(int vector, struct trapframe *tf)
182 {
183         struct proc *p;
184         struct thread *td;
185         uint64_t ucode;
186         int sig;
187         ksiginfo_t ksi;
188
189         KASSERT(TRAPF_USERMODE(tf), ("%s: In kernel mode???", __func__));
190
191         ia64_set_fpsr(IA64_FPSR_DEFAULT);
192         PCPU_INC(cnt.v_trap);
193
194         td = curthread;
195         td->td_frame = tf;
196         td->td_pticks = 0;
197         p = td->td_proc;
198         if (td->td_ucred != p->p_ucred)
199                 cred_update_thread(td);
200         sig = 0;
201         ucode = 0;
202         switch (vector) {
203         case IA64_VEC_IA32_EXCEPTION:
204                 switch ((tf->tf_special.isr >> 16) & 0xffff) {
205                 case IA32_EXCEPTION_DIVIDE:
206                         ucode = FPE_INTDIV;
207                         sig = SIGFPE;
208                         break;
209                 case IA32_EXCEPTION_DEBUG:
210                 case IA32_EXCEPTION_BREAK:
211                         sig = SIGTRAP;
212                         break;
213                 case IA32_EXCEPTION_OVERFLOW:
214                         ucode = FPE_INTOVF;
215                         sig = SIGFPE;
216                         break;
217                 case IA32_EXCEPTION_BOUND:
218                         ucode = FPE_FLTSUB;
219                         sig = SIGFPE;
220                         break;
221                 case IA32_EXCEPTION_DNA:
222                         ucode = 0;
223                         sig = SIGFPE;
224                         break;
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;
229                         sig = SIGBUS;
230                         break;
231                 case IA32_EXCEPTION_FPERROR:
232                         ucode = 0;      /* XXX */
233                         sig = SIGFPE;
234                         break;
235                 case IA32_EXCEPTION_ALIGNMENT_CHECK:
236                         ucode = tf->tf_special.ifa;     /* VA */
237                         sig = SIGBUS;
238                         break;
239                 case IA32_EXCEPTION_STREAMING_SIMD:
240                         ucode = 0; /* XXX */
241                         sig = SIGFPE;
242                         break;
243                 default:
244                         trap_panic(vector, tf);
245                         break;
246                 }
247                 break;
248
249         case IA64_VEC_IA32_INTERCEPT:
250                 /* XXX Maybe need to emulate ia32 instruction. */
251                 trap_panic(vector, tf);
252
253         case IA64_VEC_IA32_INTERRUPT:
254                 /* INT n instruction - probably a syscall. */
255                 if (((tf->tf_special.isr >> 16) & 0xffff) == 0x80) {
256                         ia32_syscall(tf);
257                         goto out;
258                 }
259                 ucode = (tf->tf_special.isr >> 16) & 0xffff;
260                 sig = SIGILL;
261                 break;
262
263         default:
264                 /* Should never happen of course. */
265                 trap_panic(vector, tf);
266                 break;
267         }
268
269         KASSERT(sig != 0, ("%s: signal not set", __func__));
270
271         ksiginfo_init_trap(&ksi);
272         ksi.ksi_signo = sig;
273         ksi.ksi_code = (int)ucode; /* XXX */
274         /* ksi.ksi_addr */
275         trapsignal(td, &ksi);
276
277 out:
278         userret(td, tf);
279         mtx_assert(&Giant, MA_NOTOWNED);
280         do_ast(tf);
281 }