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"
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
45 #include <sys/mutex.h>
46 #include <sys/pioctl.h>
47 #include <sys/ptrace.h>
48 #include <sys/reboot.h>
49 #include <sys/syscall.h>
50 #include <sys/sysent.h>
51 #include <sys/systm.h>
53 #include <sys/signalvar.h>
55 #include <sys/ktrace.h>
57 #include <sys/vmmeter.h>
59 #include <security/audit/audit.h>
63 #include <vm/vm_extern.h>
64 #include <vm/vm_param.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_page.h>
69 #include <machine/cpu.h>
70 #include <machine/db_machdep.h>
71 #include <machine/frame.h>
72 #include <machine/pcb.h>
73 #include <machine/pmap.h>
74 #include <machine/psl.h>
75 #include <machine/trap.h>
76 #include <machine/spr.h>
79 #include <powerpc/fpu/fpu_extern.h>
86 #define FAULTBUF_CTR 4
87 #define FAULTBUF_XER 5
88 #define FAULTBUF_R13 6
90 static void trap_fatal(struct trapframe *frame);
91 static void printtrap(u_int vector, struct trapframe *frame, int isfatal,
93 static int trap_pfault(struct trapframe *frame, int user);
94 static int fix_unaligned(struct thread *td, struct trapframe *frame);
95 static int handle_onfault(struct trapframe *frame);
96 static void syscall(struct trapframe *frame);
98 int setfault(faultbuf); /* defined in locore.S */
100 /* Why are these not defined in a header? */
101 int badaddr(void *, size_t);
102 int badaddr_read(void *, size_t, int *);
104 extern char *syscallnames[];
106 struct powerpc_exception {
111 static struct powerpc_exception powerpc_exceptions[] = {
112 { EXC_CRIT, "critical input" },
113 { EXC_MCHK, "machine check" },
114 { EXC_DSI, "data storage interrupt" },
115 { EXC_ISI, "instruction storage interrupt" },
116 { EXC_EXI, "external interrupt" },
117 { EXC_ALI, "alignment" },
118 { EXC_PGM, "program" },
119 { EXC_SC, "system call" },
120 { EXC_APU, "auxiliary proc unavailable" },
121 { EXC_DECR, "decrementer" },
122 { EXC_FIT, "fixed-interval timer" },
123 { EXC_WDOG, "watchdog timer" },
124 { EXC_DTMISS, "data tlb miss" },
125 { EXC_ITMISS, "instruction tlb miss" },
126 { EXC_DEBUG, "debug" },
127 { EXC_PERF, "performance monitoring" },
132 trapname(u_int vector)
134 struct powerpc_exception *pe;
136 for (pe = powerpc_exceptions; pe->vector != EXC_LAST; pe++) {
137 if (pe->vector == vector)
145 trap(struct trapframe *frame)
152 PCPU_INC(cnt.v_trap);
154 td = PCPU_GET(curthread);
159 user = (frame->srr1 & PSL_PR) ? 1 : 0;
161 CTR3(KTR_TRAP, "trap: %s type=%s (%s)", p->p_comm,
162 trapname(type), user ? "user" : "kernel");
165 td->td_frame = frame;
166 if (td->td_ucred != p->p_ucred)
167 cred_update_thread(td);
169 /* User Mode Traps */
173 sig = trap_pfault(frame, 1);
181 if (fix_unaligned(td, frame) != 0)
187 case EXC_DEBUG: /* Single stepping */
188 mtspr(SPR_DBSR, mfspr(SPR_DBSR));
189 frame->srr1 &= ~PSL_DE;
190 frame->cpu.booke.dbcr0 &= ~(DBCR0_IDM || DBCR0_IC);
194 case EXC_PGM: /* Program exception */
196 sig = fpu_emulate(frame,
197 (struct fpreg *)&td->td_pcb->pcb_fpu);
199 /* XXX SIGILL for non-trap instructions. */
208 /* Kernel Mode Traps */
209 KASSERT(cold || td->td_ucred != NULL,
210 ("kernel trap doesn't have ucred"));
214 mtspr(SPR_DBSR, mfspr(SPR_DBSR));
215 kdb_trap(frame->exc, 0, frame);
219 if (trap_pfault(frame, 0) == 0)
224 if (handle_onfault(frame))
229 if (frame->cpu.booke.esr & ESR_PTR)
230 kdb_trap(EXC_PGM, 0, frame);
240 if (p->p_sysent->sv_transtrap != NULL)
241 sig = (p->p_sysent->sv_transtrap)(sig, type);
242 ksiginfo_init_trap(&ksi);
244 ksi.ksi_code = type; /* XXX, not POSIX */
245 /* ksi.ksi_addr = ? */
246 ksi.ksi_trapno = type;
247 trapsignal(td, &ksi);
251 mtx_assert(&Giant, MA_NOTOWNED);
255 trap_fatal(struct trapframe *frame)
258 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
260 if ((debugger_on_panic || kdb_active) &&
261 kdb_trap(frame->exc, 0, frame))
264 panic("%s trap", trapname(frame->exc));
268 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
273 printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
274 user ? "user" : "kernel");
276 printf(" exception = 0x%x (%s)\n", vector, trapname(vector));
281 va = frame->cpu.booke.dear;
290 printf(" virtual address = 0x%08x\n", va);
291 printf(" srr0 = 0x%08x\n", frame->srr0);
292 printf(" srr1 = 0x%08x\n", frame->srr1);
293 printf(" curthread = %p\n", curthread);
294 if (curthread != NULL)
295 printf(" pid = %d, comm = %s\n",
296 curthread->td_proc->p_pid, curthread->td_proc->p_comm);
301 * Handles a fatal fault when we have onfault state to recover. Returns
302 * non-zero if there was onfault recovery state available.
305 handle_onfault(struct trapframe *frame)
311 fb = td->td_pcb->pcb_onfault;
313 frame->srr0 = (*fb)[FAULTBUF_LR];
314 frame->fixreg[1] = (*fb)[FAULTBUF_R1];
315 frame->fixreg[2] = (*fb)[FAULTBUF_R2];
316 frame->fixreg[3] = 1;
317 frame->cr = (*fb)[FAULTBUF_CR];
318 frame->ctr = (*fb)[FAULTBUF_CTR];
319 frame->xer = (*fb)[FAULTBUF_XER];
320 bcopy(&(*fb)[FAULTBUF_R13], &frame->fixreg[13],
321 19 * sizeof(register_t));
328 syscall(struct trapframe *frame)
331 struct sysent *callp;
339 td = PCPU_GET(curthread);
342 PCPU_INC(cnt.v_syscall);
344 code = frame->fixreg[0];
345 params = (caddr_t)(frame->fixreg + FIRSTARG);
348 if (p->p_sysent->sv_prepsyscall) {
350 * The prep code is MP aware.
352 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, ¶ms);
353 } else if (code == SYS_syscall) {
355 * code is first argument,
356 * followed by actual args.
358 code = *(u_int *) params;
359 params += sizeof(register_t);
361 } else if (code == SYS___syscall) {
363 * Like syscall, but code is a quad,
364 * so as to maintain quad alignment
365 * for the rest of the args.
367 params += sizeof(register_t);
368 code = *(u_int *) params;
369 params += sizeof(register_t);
373 if (p->p_sysent->sv_mask)
374 code &= p->p_sysent->sv_mask;
376 if (code >= p->p_sysent->sv_size)
377 callp = &p->p_sysent->sv_table[0];
379 callp = &p->p_sysent->sv_table[code];
381 narg = callp->sy_narg;
384 bcopy(params, args, n * sizeof(register_t));
385 error = copyin(MOREARGS(frame->fixreg[1]), args + n,
386 (narg - n) * sizeof(register_t));
387 params = (caddr_t)args;
391 CTR5(KTR_SYSC, "syscall: p=%s %s(%x %x %x)", p->p_comm,
393 frame->fixreg[FIRSTARG],
394 frame->fixreg[FIRSTARG+1],
395 frame->fixreg[FIRSTARG+2]);
398 if (KTRPOINT(td, KTR_SYSCALL))
399 ktrsyscall(code, narg, (register_t *)params);
405 td->td_retval[0] = 0;
406 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
408 STOPEVENT(p, S_SCE, narg);
410 PTRACESTOP_SC(p, td, S_PT_SCE);
412 AUDIT_SYSCALL_ENTER(code, td);
413 error = (*callp->sy_call)(td, params);
414 AUDIT_SYSCALL_EXIT(error, td);
416 CTR3(KTR_SYSC, "syscall: p=%s %s ret=%x", p->p_comm,
417 syscallnames[code], td->td_retval[0]);
422 if (frame->fixreg[0] == SYS___syscall && SYS_lseek) {
424 * 64-bit return, 32-bit syscall. Fixup byte order
426 frame->fixreg[FIRSTARG] = 0;
427 frame->fixreg[FIRSTARG + 1] = td->td_retval[0];
429 frame->fixreg[FIRSTARG] = td->td_retval[0];
430 frame->fixreg[FIRSTARG + 1] = td->td_retval[1];
432 /* XXX: Magic number */
433 frame->cr &= ~0x10000000;
437 * Set user's pc back to redo the system call.
445 if (p->p_sysent->sv_errsize) {
446 if (error >= p->p_sysent->sv_errsize)
447 error = -1; /* XXX */
449 error = p->p_sysent->sv_errtbl[error];
451 frame->fixreg[FIRSTARG] = error;
452 /* XXX: Magic number: Carry Flag Equivalent? */
453 frame->cr |= 0x10000000;
458 * Check for misbehavior.
460 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
461 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
462 KASSERT(td->td_critnest == 0,
463 ("System call %s returning in a critical section",
464 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
465 KASSERT(td->td_locks == 0,
466 ("System call %s returning with %d locks held",
467 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
471 if (KTRPOINT(td, KTR_SYSRET))
472 ktrsysret(code, error, td->td_retval[0]);
476 * Does the comment in the i386 code about errno apply here?
478 STOPEVENT(p, S_SCX, code);
480 PTRACESTOP_SC(p, td, S_PT_SCX);
484 trap_pfault(struct trapframe *frame, int user)
496 if (frame->exc == EXC_ISI) {
498 ftype = VM_PROT_READ | VM_PROT_EXECUTE;
501 eva = frame->cpu.booke.dear;
502 if (frame->cpu.booke.esr & ESR_ST)
503 ftype = VM_PROT_WRITE;
505 ftype = VM_PROT_READ;
509 KASSERT(p->p_vmspace != NULL, ("trap_pfault: vmspace NULL"));
510 map = &p->p_vmspace->vm_map;
512 if (eva < VM_MAXUSER_ADDRESS) {
514 if (p->p_vmspace == NULL)
517 map = &p->p_vmspace->vm_map;
523 va = trunc_page(eva);
525 if (map != kernel_map) {
527 * Keep swapout from messing with us during this
534 /* Fault in the user page: */
535 rv = vm_fault(map, va, ftype,
536 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY : VM_FAULT_NORMAL);
543 * Don't have to worry about process locking or stacks in the
546 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
549 if (rv == KERN_SUCCESS)
552 if (!user && handle_onfault(frame))
555 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
559 badaddr(void *addr, size_t size)
562 return (badaddr_read(addr, size, NULL));
566 badaddr_read(void *addr, size_t size, int *rptr)
572 /* Get rid of any stale machine checks that have been waiting. */
573 __asm __volatile ("sync; isync");
575 td = PCPU_GET(curthread);
578 td->td_pcb->pcb_onfault = 0;
579 __asm __volatile ("sync");
583 __asm __volatile ("sync");
587 x = *(volatile int8_t *)addr;
590 x = *(volatile int16_t *)addr;
593 x = *(volatile int32_t *)addr;
596 panic("badaddr: invalid size (%d)", size);
599 /* Make sure we took the machine check, if we caused one. */
600 __asm __volatile ("sync; isync");
602 td->td_pcb->pcb_onfault = 0;
603 __asm __volatile ("sync"); /* To be sure. */
605 /* Use the value to avoid reorder. */
613 * For now, this only deals with the particular unaligned access case
614 * that gcc tends to generate. Eventually it should handle all of the
615 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
619 fix_unaligned(struct thread *td, struct trapframe *frame)
622 struct thread *fputhread;
626 indicator = EXC_ALI_OPCODE_INDICATOR(frame->dsisr);
631 reg = EXC_ALI_RST(frame->dsisr);
632 fpr = &td->td_pcb->pcb_fpu.fpr[reg];
633 fputhread = PCPU_GET(fputhread);
634 /* Juggle the FPU to ensure that we've initialized
635 * the FPRs, and that their current state is in
638 if (fputhread != td) {
645 if (indicator == EXC_ALI_LFD) {
646 if (copyin((void *)frame->dar, fpr,
647 sizeof(double)) != 0)
651 if (copyout(fpr, (void *)frame->dar,
652 sizeof(double)) != 0)
664 int db_trap_glue(struct trapframe *);
666 db_trap_glue(struct trapframe *tf)
668 if (!(tf->srr1 & PSL_PR))
669 return (kdb_trap(tf->exc, 0, tf));