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 <sys/param.h>
42 #include <sys/mutex.h>
43 #include <sys/pioctl.h>
44 #include <sys/ptrace.h>
45 #include <sys/reboot.h>
46 #include <sys/syscall.h>
47 #include <sys/sysent.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
51 #include <sys/signalvar.h>
52 #include <sys/vmmeter.h>
54 #include <security/audit/audit.h>
58 #include <vm/vm_extern.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_page.h>
64 #include <machine/_inttypes.h>
65 #include <machine/altivec.h>
66 #include <machine/cpu.h>
67 #include <machine/db_machdep.h>
68 #include <machine/fpu.h>
69 #include <machine/frame.h>
70 #include <machine/pcb.h>
71 #include <machine/pmap.h>
72 #include <machine/psl.h>
73 #include <machine/trap.h>
74 #include <machine/spr.h>
75 #include <machine/sr.h>
81 #define FAULTBUF_R13 4
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);
92 void handle_kernel_slb_spill(int, register_t, register_t);
93 static int handle_user_slb_spill(pmap_t pm, vm_offset_t addr);
97 struct powerpc_exception {
103 #include <sys/dtrace_bsd.h>
105 int (*dtrace_invop_jump_addr)(struct trapframe *);
108 static struct powerpc_exception powerpc_exceptions[] = {
109 { EXC_CRIT, "critical input" },
110 { EXC_RST, "system reset" },
111 { EXC_MCHK, "machine check" },
112 { EXC_DSI, "data storage interrupt" },
113 { EXC_DSE, "data segment exception" },
114 { EXC_ISI, "instruction storage interrupt" },
115 { EXC_ISE, "instruction segment exception" },
116 { EXC_EXI, "external interrupt" },
117 { EXC_ALI, "alignment" },
118 { EXC_PGM, "program" },
119 { EXC_FPU, "floating-point unavailable" },
120 { EXC_APU, "auxiliary proc unavailable" },
121 { EXC_DECR, "decrementer" },
122 { EXC_FIT, "fixed-interval timer" },
123 { EXC_WDOG, "watchdog timer" },
124 { EXC_SC, "system call" },
125 { EXC_TRC, "trace" },
126 { EXC_FPA, "floating-point assist" },
127 { EXC_DEBUG, "debug" },
128 { EXC_PERF, "performance monitoring" },
129 { EXC_VEC, "altivec unavailable" },
130 { EXC_VSX, "vsx unavailable" },
131 { EXC_ITMISS, "instruction tlb miss" },
132 { EXC_DLMISS, "data load tlb miss" },
133 { EXC_DSMISS, "data store tlb miss" },
134 { EXC_BPT, "instruction breakpoint" },
135 { EXC_SMI, "system management" },
136 { EXC_VECAST_G4, "altivec assist" },
137 { EXC_THRM, "thermal management" },
138 { EXC_RUNMODETRC, "run mode/trace" },
143 trapname(u_int vector)
145 struct powerpc_exception *pe;
147 for (pe = powerpc_exceptions; pe->vector != EXC_LAST; pe++) {
148 if (pe->vector == vector)
156 trap(struct trapframe *frame)
167 PCPU_INC(cnt.v_trap);
172 type = ucode = frame->exc;
174 user = frame->srr1 & PSL_PR;
176 CTR3(KTR_TRAP, "trap: %s type=%s (%s)", td->td_name,
177 trapname(type), user ? "user" : "kernel");
181 * A trap can occur while DTrace executes a probe. Before
182 * executing the probe, DTrace blocks re-scheduling and sets
183 * a flag in its per-cpu flags to indicate that it doesn't
184 * want to fault. On returning from the probe, the no-fault
185 * flag is cleared and finally re-scheduling is enabled.
187 * If the DTrace kernel module has registered a trap handler,
188 * call it and if it returns non-zero, assume that it has
189 * handled the trap and modified the trap frame so that this
190 * function can return normally.
192 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type) != 0)
198 td->td_frame = frame;
199 if (td->td_ucred != p->p_ucred)
200 cred_update_thread(td);
202 /* User Mode Traps */
206 frame->srr1 &= ~PSL_SE;
214 if (handle_user_slb_spill(&p->p_vmspace->vm_pmap,
215 (type == EXC_ISE) ? frame->srr0 : frame->dar) != 0){
223 sig = trap_pfault(frame, 1);
233 KASSERT((td->td_pcb->pcb_flags & PCB_FPU) != PCB_FPU,
234 ("FPU already enabled for thread"));
239 KASSERT((td->td_pcb->pcb_flags & PCB_VEC) != PCB_VEC,
240 ("Altivec already enabled for thread"));
245 KASSERT((td->td_pcb->pcb_flags & PCB_VSX) != PCB_VSX,
246 ("VSX already enabled for thread"));
247 if (!(td->td_pcb->pcb_flags & PCB_VEC))
249 if (!(td->td_pcb->pcb_flags & PCB_FPU))
251 td->td_pcb->pcb_flags |= PCB_VSX;
258 * We get a VPU assist exception for IEEE mode
259 * vector operations on denormalized floats.
260 * Emulating this is a giant pain, so for now,
261 * just switch off IEEE mode and treat them as
266 td->td_pcb->pcb_vec.vscr |= ALTIVEC_VSCR_NJ;
271 if (fix_unaligned(td, frame) != 0) {
279 case EXC_DEBUG: /* Single stepping */
280 mtspr(SPR_DBSR, mfspr(SPR_DBSR));
281 frame->srr1 &= ~PSL_DE;
282 frame->cpu.booke.dbcr0 &= ~(DBCR0_IDM || DBCR0_IC);
288 /* Identify the trap reason */
290 if (frame->srr1 & EXC_PGM_TRAP) {
292 if (frame->cpu.booke.esr & ESR_PTR) {
295 inst = fuword32((const void *)frame->srr0);
296 if (inst == 0x0FFFDDDD &&
297 dtrace_pid_probe_ptr != NULL) {
299 fill_regs(td, ®s);
300 (*dtrace_pid_probe_ptr)(®s);
307 sig = ppc_instr_emulate(frame, td->td_pcb);
309 if (frame->srr1 & EXC_PGM_PRIV)
311 else if (frame->srr1 & EXC_PGM_ILLEGAL)
313 } else if (sig == SIGFPE)
314 ucode = FPE_FLTINV; /* Punt for now, invalid operation. */
320 * Note that this may not be recoverable for the user
321 * process, depending on the type of machine check,
322 * but it at least prevents the kernel from dying.
332 /* Kernel Mode Traps */
334 KASSERT(cold || td->td_ucred != NULL,
335 ("kernel trap doesn't have ucred"));
339 if (frame->srr1 & EXC_PGM_TRAP) {
340 if (*(uint32_t *)frame->srr0 == EXC_DTRACE) {
341 if (dtrace_invop_jump_addr != NULL) {
342 dtrace_invop_jump_addr(frame);
351 if ((frame->dar & SEGMENT_MASK) == USER_ADDR) {
352 __asm __volatile ("slbmte %0, %1" ::
353 "r"(td->td_pcb->pcb_cpu.aim.usr_vsid),
360 if (trap_pfault(frame, 0) == 0)
364 if (handle_onfault(frame))
374 if (p->p_sysent->sv_transtrap != NULL)
375 sig = (p->p_sysent->sv_transtrap)(sig, type);
376 ksiginfo_init_trap(&ksi);
378 ksi.ksi_code = (int) ucode; /* XXX, not POSIX */
379 /* ksi.ksi_addr = ? */
380 ksi.ksi_trapno = type;
381 trapsignal(td, &ksi);
388 trap_fatal(struct trapframe *frame)
391 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
393 if ((debugger_on_panic || kdb_active) &&
394 kdb_trap(frame->exc, 0, frame))
397 panic("%s trap", trapname(frame->exc));
401 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
405 printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
406 user ? "user" : "kernel");
408 printf(" exception = 0x%x (%s)\n", vector, trapname(vector));
413 printf(" virtual address = 0x%" PRIxPTR "\n", frame->dar);
415 printf(" dsisr = 0x%" PRIxPTR "\n",
416 frame->cpu.aim.dsisr);
422 printf(" virtual address = 0x%" PRIxPTR "\n", frame->srr0);
426 printf(" esr = 0x%" PRIxPTR "\n",
427 frame->cpu.booke.esr);
429 printf(" srr0 = 0x%" PRIxPTR "\n", frame->srr0);
430 printf(" srr1 = 0x%" PRIxPTR "\n", frame->srr1);
431 printf(" lr = 0x%" PRIxPTR "\n", frame->lr);
432 printf(" curthread = %p\n", curthread);
433 if (curthread != NULL)
434 printf(" pid = %d, comm = %s\n",
435 curthread->td_proc->p_pid, curthread->td_name);
440 * Handles a fatal fault when we have onfault state to recover. Returns
441 * non-zero if there was onfault recovery state available.
444 handle_onfault(struct trapframe *frame)
450 fb = td->td_pcb->pcb_onfault;
452 frame->srr0 = (*fb)[FAULTBUF_LR];
453 frame->fixreg[1] = (*fb)[FAULTBUF_R1];
454 frame->fixreg[2] = (*fb)[FAULTBUF_R2];
455 frame->fixreg[3] = 1;
456 frame->cr = (*fb)[FAULTBUF_CR];
457 bcopy(&(*fb)[FAULTBUF_R13], &frame->fixreg[13],
458 19 * sizeof(register_t));
465 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
468 struct trapframe *frame;
474 frame = td->td_frame;
476 sa->code = frame->fixreg[0];
477 params = (caddr_t)(frame->fixreg + FIRSTARG);
480 if (sa->code == SYS_syscall) {
482 * code is first argument,
483 * followed by actual args.
485 sa->code = *(register_t *) params;
486 params += sizeof(register_t);
488 } else if (sa->code == SYS___syscall) {
490 * Like syscall, but code is a quad,
491 * so as to maintain quad alignment
492 * for the rest of the args.
494 if (SV_PROC_FLAG(p, SV_ILP32)) {
495 params += sizeof(register_t);
496 sa->code = *(register_t *) params;
497 params += sizeof(register_t);
500 sa->code = *(register_t *) params;
501 params += sizeof(register_t);
506 if (p->p_sysent->sv_mask)
507 sa->code &= p->p_sysent->sv_mask;
508 if (sa->code >= p->p_sysent->sv_size)
509 sa->callp = &p->p_sysent->sv_table[0];
511 sa->callp = &p->p_sysent->sv_table[sa->code];
513 sa->narg = sa->callp->sy_narg;
515 if (SV_PROC_FLAG(p, SV_ILP32)) {
516 argsz = sizeof(uint32_t);
518 for (i = 0; i < n; i++)
519 sa->args[i] = ((u_register_t *)(params))[i] &
522 argsz = sizeof(uint64_t);
524 for (i = 0; i < n; i++)
525 sa->args[i] = ((u_register_t *)(params))[i];
529 error = copyin(MOREARGS(frame->fixreg[1]), sa->args + n,
530 (sa->narg - n) * argsz);
535 if (SV_PROC_FLAG(p, SV_ILP32) && sa->narg > n) {
536 /* Expand the size of arguments copied from the stack */
538 for (i = sa->narg; i >= n; i--)
539 sa->args[i] = ((uint32_t *)(&sa->args[n]))[i-n];
544 td->td_retval[0] = 0;
545 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
550 #include "../../kern/subr_syscall.c"
553 syscall(struct trapframe *frame)
556 struct syscall_args sa;
560 td->td_frame = frame;
564 * Speculatively restore last user SLB segment, which we know is
565 * invalid already, since we are likely to do copyin()/copyout().
567 __asm __volatile ("slbmte %0, %1; isync" ::
568 "r"(td->td_pcb->pcb_cpu.aim.usr_vsid), "r"(USER_SLB_SLBE));
571 error = syscallenter(td, &sa);
572 syscallret(td, error, &sa);
576 /* Handle kernel SLB faults -- runs in real mode, all seat belts off */
578 handle_kernel_slb_spill(int type, register_t dar, register_t srr0)
580 struct slb *slbcache;
585 addr = (type == EXC_ISE) ? srr0 : dar;
586 slbcache = PCPU_GET(slb);
587 esid = (uintptr_t)addr >> ADDR_SR_SHFT;
588 slbe = (esid << SLBE_ESID_SHIFT) | SLBE_VALID;
590 /* See if the hardware flushed this somehow (can happen in LPARs) */
591 for (i = 0; i < n_slbs; i++)
592 if (slbcache[i].slbe == (slbe | (uint64_t)i))
595 /* Not in the map, needs to actually be added */
596 slbv = kernel_va_to_slbv(addr);
597 if (slbcache[USER_SLB_SLOT].slbe == 0) {
598 for (i = 0; i < n_slbs; i++) {
599 if (i == USER_SLB_SLOT)
601 if (!(slbcache[i].slbe & SLBE_VALID))
606 slbcache[USER_SLB_SLOT].slbe = 1;
609 /* Sacrifice a random SLB entry that is not the user entry */
611 if (i == USER_SLB_SLOT)
615 /* Write new entry */
616 slbcache[i].slbv = slbv;
617 slbcache[i].slbe = slbe | (uint64_t)i;
619 /* Trap handler will restore from cache on exit */
623 handle_user_slb_spill(pmap_t pm, vm_offset_t addr)
625 struct slb *user_entry;
629 esid = (uintptr_t)addr >> ADDR_SR_SHFT;
632 user_entry = user_va_to_slb_entry(pm, addr);
634 if (user_entry == NULL) {
635 /* allocate_vsid auto-spills it */
636 (void)allocate_user_vsid(pm, esid, 0);
639 * Check that another CPU has not already mapped this.
640 * XXX: Per-thread SLB caches would be better.
642 for (i = 0; i < pm->pm_slb_len; i++)
643 if (pm->pm_slb[i] == user_entry)
646 if (i == pm->pm_slb_len)
647 slb_insert_user(pm, user_entry);
656 trap_pfault(struct trapframe *frame, int user)
670 if (frame->exc == EXC_ISI) {
672 ftype = VM_PROT_EXECUTE;
673 if (frame->srr1 & SRR1_ISI_PFAULT)
674 ftype |= VM_PROT_READ;
678 if (frame->cpu.booke.esr & ESR_ST)
680 if (frame->cpu.aim.dsisr & DSISR_STORE)
682 ftype = VM_PROT_WRITE;
684 ftype = VM_PROT_READ;
688 KASSERT(p->p_vmspace != NULL, ("trap_pfault: vmspace NULL"));
689 map = &p->p_vmspace->vm_map;
692 if (eva < VM_MAXUSER_ADDRESS) {
694 if ((eva >> ADDR_SR_SHFT) == (USER_ADDR >> ADDR_SR_SHFT)) {
696 if (p->p_vmspace == NULL)
699 map = &p->p_vmspace->vm_map;
702 user_sr = td->td_pcb->pcb_cpu.aim.usr_segm;
703 eva &= ADDR_PIDX | ADDR_POFF;
704 eva |= user_sr << ADDR_SR_SHFT;
710 va = trunc_page(eva);
712 if (map != kernel_map) {
714 * Keep swapout from messing with us during this
721 /* Fault in the user page: */
722 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
728 * XXXDTRACE: add dtrace_doubletrap_func here?
732 * Don't have to worry about process locking or stacks in the
735 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
738 if (rv == KERN_SUCCESS)
741 if (!user && handle_onfault(frame))
748 * For now, this only deals with the particular unaligned access case
749 * that gcc tends to generate. Eventually it should handle all of the
750 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
754 fix_unaligned(struct thread *td, struct trapframe *frame)
756 struct thread *fputhread;
760 indicator = EXC_ALI_OPCODE_INDICATOR(frame->cpu.aim.dsisr);
765 reg = EXC_ALI_RST(frame->cpu.aim.dsisr);
766 fpr = &td->td_pcb->pcb_fpu.fpr[reg].fpr;
767 fputhread = PCPU_GET(fputhread);
769 /* Juggle the FPU to ensure that we've initialized
770 * the FPRs, and that their current state is in
773 if (fputhread != td) {
780 if (indicator == EXC_ALI_LFD) {
781 if (copyin((void *)frame->dar, fpr,
782 sizeof(double)) != 0)
786 if (copyout(fpr, (void *)frame->dar,
787 sizeof(double)) != 0)
798 int db_trap_glue(struct trapframe *); /* Called from trap_subr.S */
801 db_trap_glue(struct trapframe *frame)
803 if (!(frame->srr1 & PSL_PR)
804 && (frame->exc == EXC_TRC || frame->exc == EXC_RUNMODETRC
806 || (frame->exc == EXC_PGM
807 && (frame->srr1 & 0x20000))
809 || (frame->exc == EXC_DEBUG)
811 || frame->exc == EXC_BPT
812 || frame->exc == EXC_DSI)) {
813 int type = frame->exc;
815 /* Ignore DTrace traps. */
816 if (*(uint32_t *)frame->srr0 == EXC_DTRACE)
819 if (type == EXC_PGM && (frame->srr1 & 0x20000)) {
821 if (frame->cpu.booke.esr & ESR_PTR) {
825 return (kdb_trap(type, 0, frame));