2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * Redistribution and use in source and binary forms, with or without
7 * 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.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $NetBSD: trap.c,v 1.58 2002/03/04 04:07:35 dbj Exp $
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_fpu_emu.h"
39 #include <sys/param.h>
44 #include <sys/mutex.h>
45 #include <sys/pioctl.h>
46 #include <sys/ptrace.h>
47 #include <sys/reboot.h>
48 #include <sys/syscall.h>
49 #include <sys/sysent.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
53 #include <sys/signalvar.h>
54 #include <sys/vmmeter.h>
56 #include <security/audit/audit.h>
60 #include <vm/vm_extern.h>
61 #include <vm/vm_param.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_page.h>
66 #include <machine/cpu.h>
67 #include <machine/db_machdep.h>
68 #include <machine/frame.h>
69 #include <machine/pcb.h>
70 #include <machine/pmap.h>
71 #include <machine/psl.h>
72 #include <machine/trap.h>
73 #include <machine/spr.h>
79 #define FAULTBUF_CTR 4
80 #define FAULTBUF_XER 5
81 #define FAULTBUF_R13 6
83 static void trap_fatal(struct trapframe *frame);
84 static void printtrap(u_int vector, struct trapframe *frame, int isfatal,
86 static int trap_pfault(struct trapframe *frame, int user);
87 static int fix_unaligned(struct thread *td, struct trapframe *frame);
88 static int handle_onfault(struct trapframe *frame);
89 static void syscall(struct trapframe *frame);
91 struct powerpc_exception {
96 static struct powerpc_exception powerpc_exceptions[] = {
97 { EXC_CRIT, "critical input" },
98 { EXC_MCHK, "machine check" },
99 { EXC_DSI, "data storage interrupt" },
100 { EXC_ISI, "instruction storage interrupt" },
101 { EXC_EXI, "external interrupt" },
102 { EXC_ALI, "alignment" },
103 { EXC_PGM, "program" },
104 { EXC_SC, "system call" },
105 { EXC_APU, "auxiliary proc unavailable" },
106 { EXC_DECR, "decrementer" },
107 { EXC_FIT, "fixed-interval timer" },
108 { EXC_WDOG, "watchdog timer" },
109 { EXC_DTMISS, "data tlb miss" },
110 { EXC_ITMISS, "instruction tlb miss" },
111 { EXC_DEBUG, "debug" },
112 { EXC_PERF, "performance monitoring" },
117 trapname(u_int vector)
119 struct powerpc_exception *pe;
121 for (pe = powerpc_exceptions; pe->vector != EXC_LAST; pe++) {
122 if (pe->vector == vector)
130 trap(struct trapframe *frame)
144 PCPU_INC(cnt.v_trap);
151 user = (frame->srr1 & PSL_PR) ? 1 : 0;
153 CTR3(KTR_TRAP, "trap: %s type=%s (%s)", p->p_comm,
154 trapname(type), user ? "user" : "kernel");
157 td->td_frame = frame;
158 if (td->td_ucred != p->p_ucred)
159 cred_update_thread(td);
161 /* User Mode Traps */
165 sig = trap_pfault(frame, 1);
173 if (fix_unaligned(td, frame) != 0)
179 case EXC_DEBUG: /* Single stepping */
180 mtspr(SPR_DBSR, mfspr(SPR_DBSR));
181 frame->srr1 &= ~PSL_DE;
182 frame->cpu.booke.dbcr0 &= ~(DBCR0_IDM || DBCR0_IC);
186 case EXC_PGM: /* Program exception */
187 sig = ppc_instr_emulate(frame, td->td_pcb);
194 /* Kernel Mode Traps */
195 KASSERT(cold || td->td_ucred != NULL,
196 ("kernel trap doesn't have ucred"));
200 mtspr(SPR_DBSR, mfspr(SPR_DBSR));
201 kdb_trap(frame->exc, 0, frame);
205 if (trap_pfault(frame, 0) == 0)
210 if (handle_onfault(frame))
215 if (frame->cpu.booke.esr & ESR_PTR)
216 kdb_trap(EXC_PGM, 0, frame);
226 if (p->p_sysent->sv_transtrap != NULL)
227 sig = (p->p_sysent->sv_transtrap)(sig, type);
228 ksiginfo_init_trap(&ksi);
230 ksi.ksi_code = type; /* XXX, not POSIX */
231 /* ksi.ksi_addr = ? */
232 ksi.ksi_trapno = type;
233 trapsignal(td, &ksi);
240 trap_fatal(struct trapframe *frame)
243 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
245 if ((debugger_on_panic || kdb_active) &&
246 kdb_trap(frame->exc, 0, frame))
249 panic("%s trap", trapname(frame->exc));
253 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
258 printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
259 user ? "user" : "kernel");
261 printf(" exception = 0x%x (%s)\n", vector, trapname(vector));
266 va = frame->cpu.booke.dear;
275 printf(" virtual address = 0x%08x\n", va);
276 printf(" srr0 = 0x%08x\n", frame->srr0);
277 printf(" srr1 = 0x%08x\n", frame->srr1);
278 printf(" curthread = %p\n", curthread);
279 if (curthread != NULL)
280 printf(" pid = %d, comm = %s\n",
281 curthread->td_proc->p_pid, curthread->td_proc->p_comm);
286 * Handles a fatal fault when we have onfault state to recover. Returns
287 * non-zero if there was onfault recovery state available.
290 handle_onfault(struct trapframe *frame)
296 fb = td->td_pcb->pcb_onfault;
298 frame->srr0 = (*fb)[FAULTBUF_LR];
299 frame->fixreg[1] = (*fb)[FAULTBUF_R1];
300 frame->fixreg[2] = (*fb)[FAULTBUF_R2];
301 frame->fixreg[3] = 1;
302 frame->cr = (*fb)[FAULTBUF_CR];
303 frame->ctr = (*fb)[FAULTBUF_CTR];
304 frame->xer = (*fb)[FAULTBUF_XER];
305 bcopy(&(*fb)[FAULTBUF_R13], &frame->fixreg[13],
306 19 * sizeof(register_t));
313 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
316 struct trapframe *frame;
321 frame = td->td_frame;
323 sa->code = frame->fixreg[0];
324 params = (caddr_t)(frame->fixreg + FIRSTARG);
327 if (sa->code == SYS_syscall) {
329 * code is first argument,
330 * followed by actual args.
332 sa->code = *(u_int *) params;
333 params += sizeof(register_t);
335 } else if (sa->code == SYS___syscall) {
337 * Like syscall, but code is a quad,
338 * so as to maintain quad alignment
339 * for the rest of the args.
341 params += sizeof(register_t);
342 sa->code = *(u_int *) params;
343 params += sizeof(register_t);
347 if (p->p_sysent->sv_mask)
348 sa->code &= p->p_sysent->sv_mask;
349 if (sa->code >= p->p_sysent->sv_size)
350 sa->callp = &p->p_sysent->sv_table[0];
352 sa->callp = &p->p_sysent->sv_table[sa->code];
353 sa->narg = sa->callp->sy_narg;
355 bcopy(params, sa->args, n * sizeof(register_t));
357 error = copyin(MOREARGS(frame->fixreg[1]), sa->args + n,
358 (sa->narg - n) * sizeof(register_t));
363 td->td_retval[0] = 0;
364 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
369 #include "../../kern/subr_syscall.c"
372 syscall(struct trapframe *frame)
375 struct syscall_args sa;
379 td->td_frame = frame;
381 error = syscallenter(td, &sa);
382 syscallret(td, error, &sa);
386 trap_pfault(struct trapframe *frame, int user)
398 if (frame->exc == EXC_ISI) {
400 ftype = VM_PROT_READ | VM_PROT_EXECUTE;
403 eva = frame->cpu.booke.dear;
404 if (frame->cpu.booke.esr & ESR_ST)
405 ftype = VM_PROT_WRITE;
407 ftype = VM_PROT_READ;
411 KASSERT(p->p_vmspace != NULL, ("trap_pfault: vmspace NULL"));
412 map = &p->p_vmspace->vm_map;
414 if (eva < VM_MAXUSER_ADDRESS) {
416 if (p->p_vmspace == NULL)
419 map = &p->p_vmspace->vm_map;
425 va = trunc_page(eva);
427 if (map != kernel_map) {
429 * Keep swapout from messing with us during this
436 /* Fault in the user page: */
437 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
444 * Don't have to worry about process locking or stacks in the
447 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
450 if (rv == KERN_SUCCESS)
453 if (!user && handle_onfault(frame))
456 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
460 * For now, this only deals with the particular unaligned access case
461 * that gcc tends to generate. Eventually it should handle all of the
462 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
466 fix_unaligned(struct thread *td, struct trapframe *frame)
469 struct thread *fputhread;
473 indicator = EXC_ALI_OPCODE_INDICATOR(frame->dsisr);
478 reg = EXC_ALI_RST(frame->dsisr);
479 fpr = &td->td_pcb->pcb_fpu.fpr[reg];
480 fputhread = PCPU_GET(fputhread);
481 /* Juggle the FPU to ensure that we've initialized
482 * the FPRs, and that their current state is in
485 if (fputhread != td) {
492 if (indicator == EXC_ALI_LFD) {
493 if (copyin((void *)frame->dar, fpr,
494 sizeof(double)) != 0)
498 if (copyout(fpr, (void *)frame->dar,
499 sizeof(double)) != 0)
511 int db_trap_glue(struct trapframe *);
513 db_trap_glue(struct trapframe *tf)
515 if (!(tf->srr1 & PSL_PR))
516 return (kdb_trap(tf->exc, 0, tf));