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/psl.h>
72 #include <machine/trap.h>
73 #include <machine/spr.h>
74 #include <machine/sr.h>
76 /* Below matches setjmp.S */
77 #define FAULTBUF_LR 21
80 #define FAULTBUF_CR 22
81 #define FAULTBUF_R14 3
83 #define MOREARGS(sp) ((caddr_t)((uintptr_t)(sp) + \
84 sizeof(struct callframe) - 3*sizeof(register_t))) /* more args go here */
86 static void trap_fatal(struct trapframe *frame);
87 static void printtrap(u_int vector, struct trapframe *frame, int isfatal,
89 static int trap_pfault(struct trapframe *frame, int user);
90 static int fix_unaligned(struct thread *td, struct trapframe *frame);
91 static int handle_onfault(struct trapframe *frame);
92 static void syscall(struct trapframe *frame);
94 #if defined(__powerpc64__) && defined(AIM)
95 void handle_kernel_slb_spill(int, register_t, register_t);
96 static int handle_user_slb_spill(pmap_t pm, vm_offset_t addr);
100 extern vm_offset_t __startkernel;
103 int db_trap_glue(struct trapframe *); /* Called from trap_subr.S */
106 struct powerpc_exception {
112 #include <sys/dtrace_bsd.h>
114 int (*dtrace_invop_jump_addr)(struct trapframe *);
117 static struct powerpc_exception powerpc_exceptions[] = {
118 { EXC_CRIT, "critical input" },
119 { EXC_RST, "system reset" },
120 { EXC_MCHK, "machine check" },
121 { EXC_DSI, "data storage interrupt" },
122 { EXC_DSE, "data segment exception" },
123 { EXC_ISI, "instruction storage interrupt" },
124 { EXC_ISE, "instruction segment exception" },
125 { EXC_EXI, "external interrupt" },
126 { EXC_ALI, "alignment" },
127 { EXC_PGM, "program" },
128 { EXC_HEA, "hypervisor emulation assistance" },
129 { EXC_FPU, "floating-point unavailable" },
130 { EXC_APU, "auxiliary proc unavailable" },
131 { EXC_DECR, "decrementer" },
132 { EXC_FIT, "fixed-interval timer" },
133 { EXC_WDOG, "watchdog timer" },
134 { EXC_SC, "system call" },
135 { EXC_TRC, "trace" },
136 { EXC_FPA, "floating-point assist" },
137 { EXC_DEBUG, "debug" },
138 { EXC_PERF, "performance monitoring" },
139 { EXC_VEC, "altivec unavailable" },
140 { EXC_VSX, "vsx unavailable" },
141 { EXC_FAC, "facility unavailable" },
142 { EXC_ITMISS, "instruction tlb miss" },
143 { EXC_DLMISS, "data load tlb miss" },
144 { EXC_DSMISS, "data store tlb miss" },
145 { EXC_BPT, "instruction breakpoint" },
146 { EXC_SMI, "system management" },
147 { EXC_VECAST_G4, "altivec assist" },
148 { EXC_THRM, "thermal management" },
149 { EXC_RUNMODETRC, "run mode/trace" },
153 #define ESR_BITMASK \
155 "\040b0\037b1\036b2\035b3\034PIL\033PRR\032PTR\031FP" \
156 "\030ST\027b9\026DLK\025ILK\024b12\023b13\022BO\021PIE" \
157 "\020b16\017b17\016b18\015b19\014b20\013b21\012b22\011b23" \
158 "\010SPE\007EPID\006b26\005b27\004b28\003b29\002b30\001b31"
159 #define MCSR_BITMASK \
161 "\040MCP\037ICERR\036DCERR\035TLBPERR\034L2MMU_MHIT\033b5\032b6\031b7" \
162 "\030b8\027b9\026b10\025NMI\024MAV\023MEA\022b14\021IF" \
163 "\020LD\017ST\016LDG\015b19\014b20\013b21\012b22\011b23" \
164 "\010b24\007b25\006b26\005b27\004b28\003b29\002TLBSYNC\001BSL2_ERR"
165 #define MSSSR_BITMASK \
167 "\040b0\037b1\036b2\035b3\034b4\033b5\032b6\031b7" \
168 "\030b8\027b9\026b10\025b11\024b12\023L2TAG\022L2DAT\021L3TAG" \
169 "\020L3DAT\017APE\016DPE\015TEA\014b20\013b21\012b22\011b23" \
170 "\010b24\007b25\006b26\005b27\004b28\003b29\002b30\001b31"
174 trapname(u_int vector)
176 struct powerpc_exception *pe;
178 for (pe = powerpc_exceptions; pe->vector != EXC_LAST; pe++) {
179 if (pe->vector == vector)
187 frame_is_trap_inst(struct trapframe *frame)
190 return (frame->exc == EXC_PGM && frame->srr1 & EXC_PGM_TRAP);
192 return (frame->exc == EXC_DEBUG || frame->cpu.booke.esr & ESR_PTR);
197 trap(struct trapframe *frame)
213 type = ucode = frame->exc;
215 user = frame->srr1 & PSL_PR;
217 CTR3(KTR_TRAP, "trap: %s type=%s (%s)", td->td_name,
218 trapname(type), user ? "user" : "kernel");
222 * A trap can occur while DTrace executes a probe. Before
223 * executing the probe, DTrace blocks re-scheduling and sets
224 * a flag in its per-cpu flags to indicate that it doesn't
225 * want to fault. On returning from the probe, the no-fault
226 * flag is cleared and finally re-scheduling is enabled.
228 * If the DTrace kernel module has registered a trap handler,
229 * call it and if it returns non-zero, assume that it has
230 * handled the trap and modified the trap frame so that this
231 * function can return normally.
233 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type) != 0)
239 td->td_frame = frame;
240 if (td->td_cowgen != p->p_cowgen)
241 thread_cow_update(td);
243 /* User Mode Traps */
247 frame->srr1 &= ~PSL_SE;
252 #if defined(__powerpc64__) && defined(AIM)
255 if (handle_user_slb_spill(&p->p_vmspace->vm_pmap,
256 (type == EXC_ISE) ? frame->srr0 : frame->dar) != 0){
264 sig = trap_pfault(frame, 1);
274 KASSERT((td->td_pcb->pcb_flags & PCB_FPU) != PCB_FPU,
275 ("FPU already enabled for thread"));
280 KASSERT((td->td_pcb->pcb_flags & PCB_VEC) != PCB_VEC,
281 ("Altivec already enabled for thread"));
286 KASSERT((td->td_pcb->pcb_flags & PCB_VSX) != PCB_VSX,
287 ("VSX already enabled for thread"));
288 if (!(td->td_pcb->pcb_flags & PCB_VEC))
290 if (!(td->td_pcb->pcb_flags & PCB_FPU))
292 td->td_pcb->pcb_flags |= PCB_VSX;
306 * We get a VPU assist exception for IEEE mode
307 * vector operations on denormalized floats.
308 * Emulating this is a giant pain, so for now,
309 * just switch off IEEE mode and treat them as
314 td->td_pcb->pcb_vec.vscr |= ALTIVEC_VSCR_NJ;
319 if (fix_unaligned(td, frame) != 0) {
327 case EXC_DEBUG: /* Single stepping */
328 mtspr(SPR_DBSR, mfspr(SPR_DBSR));
329 frame->srr1 &= ~PSL_DE;
330 frame->cpu.booke.dbcr0 &= ~(DBCR0_IDM | DBCR0_IC);
336 /* Identify the trap reason */
337 if (frame_is_trap_inst(frame)) {
339 inst = fuword32((const void *)frame->srr0);
340 if (inst == 0x0FFFDDDD &&
341 dtrace_pid_probe_ptr != NULL) {
342 (*dtrace_pid_probe_ptr)(frame);
349 sig = ppc_instr_emulate(frame, td->td_pcb);
351 if (frame->srr1 & EXC_PGM_PRIV)
353 else if (frame->srr1 & EXC_PGM_ILLEGAL)
355 } else if (sig == SIGFPE)
356 ucode = FPE_FLTINV; /* Punt for now, invalid operation. */
362 * Note that this may not be recoverable for the user
363 * process, depending on the type of machine check,
364 * but it at least prevents the kernel from dying.
374 /* Kernel Mode Traps */
376 KASSERT(cold || td->td_ucred != NULL,
377 ("kernel trap doesn't have ucred"));
381 if (frame_is_trap_inst(frame)) {
382 if (*(uint32_t *)frame->srr0 == EXC_DTRACE) {
383 if (dtrace_invop_jump_addr != NULL) {
384 dtrace_invop_jump_addr(frame);
391 if (db_trap_glue(frame))
395 #if defined(__powerpc64__) && defined(AIM)
397 if (td->td_pcb->pcb_cpu.aim.usr_vsid != 0 &&
398 (frame->dar & SEGMENT_MASK) == USER_ADDR) {
399 __asm __volatile ("slbmte %0, %1" ::
400 "r"(td->td_pcb->pcb_cpu.aim.usr_vsid),
407 if (trap_pfault(frame, 0) == 0)
411 if (handle_onfault(frame))
421 if (p->p_sysent->sv_transtrap != NULL)
422 sig = (p->p_sysent->sv_transtrap)(sig, type);
423 ksiginfo_init_trap(&ksi);
425 ksi.ksi_code = (int) ucode; /* XXX, not POSIX */
426 /* ksi.ksi_addr = ? */
427 ksi.ksi_trapno = type;
428 trapsignal(td, &ksi);
435 trap_fatal(struct trapframe *frame)
438 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
440 if ((debugger_on_panic || kdb_active) &&
441 kdb_trap(frame->exc, 0, frame))
444 panic("%s trap", trapname(frame->exc));
448 cpu_printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
457 printf(" dsisr = 0x%lx\n",
458 (u_long)frame->cpu.aim.dsisr);
463 printf(" msssr0 = 0x%b\n",
464 (int)mfspr(SPR_MSSSR0), MSSSR_BITMASK);
472 pa = mfspr(SPR_MCARU);
473 pa = (pa << 32) | (u_register_t)mfspr(SPR_MCAR);
474 printf(" mcsr = 0x%b\n",
475 (int)mfspr(SPR_MCSR), MCSR_BITMASK);
476 printf(" mcar = 0x%jx\n", (uintmax_t)pa);
478 printf(" esr = 0x%b\n",
479 (int)frame->cpu.booke.esr, ESR_BITMASK);
484 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
488 printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
489 user ? "user" : "kernel");
491 printf(" exception = 0x%x (%s)\n", vector, trapname(vector));
496 printf(" virtual address = 0x%" PRIxPTR "\n", frame->dar);
501 printf(" virtual address = 0x%" PRIxPTR "\n", frame->srr0);
506 cpu_printtrap(vector, frame, isfatal, user);
507 printf(" srr0 = 0x%" PRIxPTR " (0x%" PRIxPTR ")\n",
508 frame->srr0, frame->srr0 - (register_t)(__startkernel - KERNBASE));
509 printf(" srr1 = 0x%lx\n", (u_long)frame->srr1);
510 printf(" lr = 0x%" PRIxPTR " (0x%" PRIxPTR ")\n",
511 frame->lr, frame->lr - (register_t)(__startkernel - KERNBASE));
512 printf(" curthread = %p\n", curthread);
513 if (curthread != NULL)
514 printf(" pid = %d, comm = %s\n",
515 curthread->td_proc->p_pid, curthread->td_name);
520 * Handles a fatal fault when we have onfault state to recover. Returns
521 * non-zero if there was onfault recovery state available.
524 handle_onfault(struct trapframe *frame)
530 fb = td->td_pcb->pcb_onfault;
532 frame->srr0 = (*fb)->_jb[FAULTBUF_LR];
533 frame->fixreg[1] = (*fb)->_jb[FAULTBUF_R1];
534 frame->fixreg[2] = (*fb)->_jb[FAULTBUF_R2];
535 frame->fixreg[3] = 1;
536 frame->cr = (*fb)->_jb[FAULTBUF_CR];
537 bcopy(&(*fb)->_jb[FAULTBUF_R14], &frame->fixreg[14],
538 18 * sizeof(register_t));
539 td->td_pcb->pcb_onfault = NULL; /* Returns twice, not thrice */
546 cpu_fetch_syscall_args(struct thread *td)
549 struct trapframe *frame;
550 struct syscall_args *sa;
556 frame = td->td_frame;
559 sa->code = frame->fixreg[0];
560 params = (caddr_t)(frame->fixreg + FIRSTARG);
563 if (sa->code == SYS_syscall) {
565 * code is first argument,
566 * followed by actual args.
568 sa->code = *(register_t *) params;
569 params += sizeof(register_t);
571 } else if (sa->code == SYS___syscall) {
573 * Like syscall, but code is a quad,
574 * so as to maintain quad alignment
575 * for the rest of the args.
577 if (SV_PROC_FLAG(p, SV_ILP32)) {
578 params += sizeof(register_t);
579 sa->code = *(register_t *) params;
580 params += sizeof(register_t);
583 sa->code = *(register_t *) params;
584 params += sizeof(register_t);
589 if (p->p_sysent->sv_mask)
590 sa->code &= p->p_sysent->sv_mask;
591 if (sa->code >= p->p_sysent->sv_size)
592 sa->callp = &p->p_sysent->sv_table[0];
594 sa->callp = &p->p_sysent->sv_table[sa->code];
596 sa->narg = sa->callp->sy_narg;
598 if (SV_PROC_FLAG(p, SV_ILP32)) {
599 argsz = sizeof(uint32_t);
601 for (i = 0; i < n; i++)
602 sa->args[i] = ((u_register_t *)(params))[i] &
605 argsz = sizeof(uint64_t);
607 for (i = 0; i < n; i++)
608 sa->args[i] = ((u_register_t *)(params))[i];
612 error = copyin(MOREARGS(frame->fixreg[1]), sa->args + n,
613 (sa->narg - n) * argsz);
618 if (SV_PROC_FLAG(p, SV_ILP32) && sa->narg > n) {
619 /* Expand the size of arguments copied from the stack */
621 for (i = sa->narg; i >= n; i--)
622 sa->args[i] = ((uint32_t *)(&sa->args[n]))[i-n];
627 td->td_retval[0] = 0;
628 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
633 #include "../../kern/subr_syscall.c"
636 syscall(struct trapframe *frame)
642 td->td_frame = frame;
644 #if defined(__powerpc64__) && defined(AIM)
646 * Speculatively restore last user SLB segment, which we know is
647 * invalid already, since we are likely to do copyin()/copyout().
649 if (td->td_pcb->pcb_cpu.aim.usr_vsid != 0)
650 __asm __volatile ("slbmte %0, %1; isync" ::
651 "r"(td->td_pcb->pcb_cpu.aim.usr_vsid), "r"(USER_SLB_SLBE));
654 error = syscallenter(td);
655 syscallret(td, error);
658 #if defined(__powerpc64__) && defined(AIM)
659 /* Handle kernel SLB faults -- runs in real mode, all seat belts off */
661 handle_kernel_slb_spill(int type, register_t dar, register_t srr0)
663 struct slb *slbcache;
668 addr = (type == EXC_ISE) ? srr0 : dar;
669 slbcache = PCPU_GET(aim.slb);
670 esid = (uintptr_t)addr >> ADDR_SR_SHFT;
671 slbe = (esid << SLBE_ESID_SHIFT) | SLBE_VALID;
673 /* See if the hardware flushed this somehow (can happen in LPARs) */
674 for (i = 0; i < n_slbs; i++)
675 if (slbcache[i].slbe == (slbe | (uint64_t)i))
678 /* Not in the map, needs to actually be added */
679 slbv = kernel_va_to_slbv(addr);
680 if (slbcache[USER_SLB_SLOT].slbe == 0) {
681 for (i = 0; i < n_slbs; i++) {
682 if (i == USER_SLB_SLOT)
684 if (!(slbcache[i].slbe & SLBE_VALID))
689 slbcache[USER_SLB_SLOT].slbe = 1;
692 /* Sacrifice a random SLB entry that is not the user entry */
694 if (i == USER_SLB_SLOT)
698 /* Write new entry */
699 slbcache[i].slbv = slbv;
700 slbcache[i].slbe = slbe | (uint64_t)i;
702 /* Trap handler will restore from cache on exit */
706 handle_user_slb_spill(pmap_t pm, vm_offset_t addr)
708 struct slb *user_entry;
712 if (pm->pm_slb == NULL)
715 esid = (uintptr_t)addr >> ADDR_SR_SHFT;
718 user_entry = user_va_to_slb_entry(pm, addr);
720 if (user_entry == NULL) {
721 /* allocate_vsid auto-spills it */
722 (void)allocate_user_vsid(pm, esid, 0);
725 * Check that another CPU has not already mapped this.
726 * XXX: Per-thread SLB caches would be better.
728 for (i = 0; i < pm->pm_slb_len; i++)
729 if (pm->pm_slb[i] == user_entry)
732 if (i == pm->pm_slb_len)
733 slb_insert_user(pm, user_entry);
742 trap_pfault(struct trapframe *frame, int user)
753 if (frame->exc == EXC_ISI) {
755 ftype = VM_PROT_EXECUTE;
756 if (frame->srr1 & SRR1_ISI_PFAULT)
757 ftype |= VM_PROT_READ;
761 if (frame->cpu.booke.esr & ESR_ST)
763 if (frame->cpu.aim.dsisr & DSISR_STORE)
765 ftype = VM_PROT_WRITE;
767 ftype = VM_PROT_READ;
771 KASSERT(p->p_vmspace != NULL, ("trap_pfault: vmspace NULL"));
772 map = &p->p_vmspace->vm_map;
774 rv = pmap_decode_kernel_ptr(eva, &is_user, &eva);
779 map = &p->p_vmspace->vm_map;
783 va = trunc_page(eva);
785 /* Fault in the page. */
786 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
788 * XXXDTRACE: add dtrace_doubletrap_func here?
791 if (rv == KERN_SUCCESS)
794 if (!user && handle_onfault(frame))
801 * For now, this only deals with the particular unaligned access case
802 * that gcc tends to generate. Eventually it should handle all of the
803 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
807 fix_unaligned(struct thread *td, struct trapframe *frame)
809 struct thread *fputhread;
817 indicator = (frame->cpu.booke.esr & (ESR_ST|ESR_SPE));
818 if (indicator & ESR_SPE) {
819 if (copyin((void *)frame->srr0, &inst, sizeof(inst)) != 0)
821 reg = EXC_ALI_SPE_REG(inst);
822 fpr = (double *)td->td_pcb->pcb_vec.vr[reg];
823 fputhread = PCPU_GET(vecthread);
825 /* Juggle the SPE to ensure that we've initialized
826 * the registers, and that their current state is in
829 if (fputhread != td) {
836 if (!(indicator & ESR_ST)) {
837 if (copyin((void *)frame->dar, fpr,
838 sizeof(double)) != 0)
840 frame->fixreg[reg] = td->td_pcb->pcb_vec.vr[reg][1];
843 td->td_pcb->pcb_vec.vr[reg][1] = frame->fixreg[reg];
844 if (copyout(fpr, (void *)frame->dar,
845 sizeof(double)) != 0)
851 indicator = EXC_ALI_OPCODE_INDICATOR(frame->cpu.aim.dsisr);
856 reg = EXC_ALI_RST(frame->cpu.aim.dsisr);
857 fpr = &td->td_pcb->pcb_fpu.fpr[reg].fpr;
858 fputhread = PCPU_GET(fputhread);
860 /* Juggle the FPU to ensure that we've initialized
861 * the FPRs, and that their current state is in
864 if (fputhread != td) {
871 if (indicator == EXC_ALI_LFD) {
872 if (copyin((void *)frame->dar, fpr,
873 sizeof(double)) != 0)
877 if (copyout(fpr, (void *)frame->dar,
878 sizeof(double)) != 0)
891 db_trap_glue(struct trapframe *frame)
894 if (!(frame->srr1 & PSL_PR)
895 && (frame->exc == EXC_TRC || frame->exc == EXC_RUNMODETRC
896 || frame_is_trap_inst(frame)
897 || frame->exc == EXC_BPT
898 || frame->exc == EXC_DSI)) {
899 int type = frame->exc;
901 /* Ignore DTrace traps. */
902 if (*(uint32_t *)frame->srr0 == EXC_DTRACE)
904 if (frame_is_trap_inst(frame)) {
907 return (kdb_trap(type, 0, frame));