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);
101 int db_trap_glue(struct trapframe *); /* Called from trap_subr.S */
104 struct powerpc_exception {
110 #include <sys/dtrace_bsd.h>
112 int (*dtrace_invop_jump_addr)(struct trapframe *);
115 static struct powerpc_exception powerpc_exceptions[] = {
116 { EXC_CRIT, "critical input" },
117 { EXC_RST, "system reset" },
118 { EXC_MCHK, "machine check" },
119 { EXC_DSI, "data storage interrupt" },
120 { EXC_DSE, "data segment exception" },
121 { EXC_ISI, "instruction storage interrupt" },
122 { EXC_ISE, "instruction segment exception" },
123 { EXC_EXI, "external interrupt" },
124 { EXC_ALI, "alignment" },
125 { EXC_PGM, "program" },
126 { EXC_FPU, "floating-point unavailable" },
127 { EXC_APU, "auxiliary proc unavailable" },
128 { EXC_DECR, "decrementer" },
129 { EXC_FIT, "fixed-interval timer" },
130 { EXC_WDOG, "watchdog timer" },
131 { EXC_SC, "system call" },
132 { EXC_TRC, "trace" },
133 { EXC_FPA, "floating-point assist" },
134 { EXC_DEBUG, "debug" },
135 { EXC_PERF, "performance monitoring" },
136 { EXC_VEC, "altivec unavailable" },
137 { EXC_VSX, "vsx unavailable" },
138 { EXC_ITMISS, "instruction tlb miss" },
139 { EXC_DLMISS, "data load tlb miss" },
140 { EXC_DSMISS, "data store tlb miss" },
141 { EXC_BPT, "instruction breakpoint" },
142 { EXC_SMI, "system management" },
143 { EXC_VECAST_G4, "altivec assist" },
144 { EXC_THRM, "thermal management" },
145 { EXC_RUNMODETRC, "run mode/trace" },
150 trapname(u_int vector)
152 struct powerpc_exception *pe;
154 for (pe = powerpc_exceptions; pe->vector != EXC_LAST; pe++) {
155 if (pe->vector == vector)
163 trap(struct trapframe *frame)
179 type = ucode = frame->exc;
181 user = frame->srr1 & PSL_PR;
183 CTR3(KTR_TRAP, "trap: %s type=%s (%s)", td->td_name,
184 trapname(type), user ? "user" : "kernel");
188 * A trap can occur while DTrace executes a probe. Before
189 * executing the probe, DTrace blocks re-scheduling and sets
190 * a flag in its per-cpu flags to indicate that it doesn't
191 * want to fault. On returning from the probe, the no-fault
192 * flag is cleared and finally re-scheduling is enabled.
194 * If the DTrace kernel module has registered a trap handler,
195 * call it and if it returns non-zero, assume that it has
196 * handled the trap and modified the trap frame so that this
197 * function can return normally.
199 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type) != 0)
205 td->td_frame = frame;
206 if (td->td_cowgen != p->p_cowgen)
207 thread_cow_update(td);
209 /* User Mode Traps */
213 frame->srr1 &= ~PSL_SE;
218 #if defined(__powerpc64__) && defined(AIM)
221 if (handle_user_slb_spill(&p->p_vmspace->vm_pmap,
222 (type == EXC_ISE) ? frame->srr0 : frame->dar) != 0){
230 sig = trap_pfault(frame, 1);
240 KASSERT((td->td_pcb->pcb_flags & PCB_FPU) != PCB_FPU,
241 ("FPU already enabled for thread"));
246 KASSERT((td->td_pcb->pcb_flags & PCB_VEC) != PCB_VEC,
247 ("Altivec already enabled for thread"));
252 KASSERT((td->td_pcb->pcb_flags & PCB_VSX) != PCB_VSX,
253 ("VSX already enabled for thread"));
254 if (!(td->td_pcb->pcb_flags & PCB_VEC))
256 if (!(td->td_pcb->pcb_flags & PCB_FPU))
258 td->td_pcb->pcb_flags |= PCB_VSX;
266 * We get a VPU assist exception for IEEE mode
267 * vector operations on denormalized floats.
268 * Emulating this is a giant pain, so for now,
269 * just switch off IEEE mode and treat them as
274 td->td_pcb->pcb_vec.vscr |= ALTIVEC_VSCR_NJ;
279 if (fix_unaligned(td, frame) != 0) {
287 case EXC_DEBUG: /* Single stepping */
288 mtspr(SPR_DBSR, mfspr(SPR_DBSR));
289 frame->srr1 &= ~PSL_DE;
290 frame->cpu.booke.dbcr0 &= ~(DBCR0_IDM | DBCR0_IC);
296 /* Identify the trap reason */
298 if (frame->srr1 & EXC_PGM_TRAP) {
300 if (frame->cpu.booke.esr & ESR_PTR) {
303 inst = fuword32((const void *)frame->srr0);
304 if (inst == 0x0FFFDDDD &&
305 dtrace_pid_probe_ptr != NULL) {
306 (*dtrace_pid_probe_ptr)(frame);
313 sig = ppc_instr_emulate(frame, td->td_pcb);
315 if (frame->srr1 & EXC_PGM_PRIV)
317 else if (frame->srr1 & EXC_PGM_ILLEGAL)
319 } else if (sig == SIGFPE)
320 ucode = FPE_FLTINV; /* Punt for now, invalid operation. */
326 * Note that this may not be recoverable for the user
327 * process, depending on the type of machine check,
328 * but it at least prevents the kernel from dying.
338 /* Kernel Mode Traps */
340 KASSERT(cold || td->td_ucred != NULL,
341 ("kernel trap doesn't have ucred"));
346 if (frame->srr1 & EXC_PGM_TRAP) {
348 if (frame->cpu.booke.esr & ESR_PTR) {
350 if (*(uint32_t *)frame->srr0 == EXC_DTRACE) {
351 if (dtrace_invop_jump_addr != NULL) {
352 dtrace_invop_jump_addr(frame);
359 if (db_trap_glue(frame))
363 #if defined(__powerpc64__) && defined(AIM)
365 if ((frame->dar & SEGMENT_MASK) == USER_ADDR) {
366 __asm __volatile ("slbmte %0, %1" ::
367 "r"(td->td_pcb->pcb_cpu.aim.usr_vsid),
374 if (trap_pfault(frame, 0) == 0)
378 if (handle_onfault(frame))
388 if (p->p_sysent->sv_transtrap != NULL)
389 sig = (p->p_sysent->sv_transtrap)(sig, type);
390 ksiginfo_init_trap(&ksi);
392 ksi.ksi_code = (int) ucode; /* XXX, not POSIX */
393 /* ksi.ksi_addr = ? */
394 ksi.ksi_trapno = type;
395 trapsignal(td, &ksi);
402 trap_fatal(struct trapframe *frame)
405 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
407 if ((debugger_on_panic || kdb_active) &&
408 kdb_trap(frame->exc, 0, frame))
411 panic("%s trap", trapname(frame->exc));
415 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
423 printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
424 user ? "user" : "kernel");
426 printf(" exception = 0x%x (%s)\n", vector, trapname(vector));
431 printf(" virtual address = 0x%" PRIxPTR "\n", frame->dar);
433 printf(" dsisr = 0x%lx\n",
434 (u_long)frame->cpu.aim.dsisr);
440 printf(" virtual address = 0x%" PRIxPTR "\n", frame->srr0);
446 printf(" msssr0 = 0x%lx\n",
447 (u_long)mfspr(SPR_MSSSR0));
449 pa = mfspr(SPR_MCARU);
450 pa = (pa << 32) | (u_register_t)mfspr(SPR_MCAR);
451 printf(" mcsr = 0x%lx\n", (u_long)mfspr(SPR_MCSR));
452 printf(" mcar = 0x%jx\n", (uintmax_t)pa);
457 printf(" esr = 0x%" PRIxPTR "\n",
458 frame->cpu.booke.esr);
460 printf(" srr0 = 0x%" PRIxPTR "\n", frame->srr0);
461 printf(" srr1 = 0x%lx\n", (u_long)frame->srr1);
462 printf(" lr = 0x%" PRIxPTR "\n", frame->lr);
463 printf(" curthread = %p\n", curthread);
464 if (curthread != NULL)
465 printf(" pid = %d, comm = %s\n",
466 curthread->td_proc->p_pid, curthread->td_name);
471 * Handles a fatal fault when we have onfault state to recover. Returns
472 * non-zero if there was onfault recovery state available.
475 handle_onfault(struct trapframe *frame)
481 fb = td->td_pcb->pcb_onfault;
483 frame->srr0 = (*fb)->_jb[FAULTBUF_LR];
484 frame->fixreg[1] = (*fb)->_jb[FAULTBUF_R1];
485 frame->fixreg[2] = (*fb)->_jb[FAULTBUF_R2];
486 frame->fixreg[3] = 1;
487 frame->cr = (*fb)->_jb[FAULTBUF_CR];
488 bcopy(&(*fb)->_jb[FAULTBUF_R14], &frame->fixreg[14],
489 18 * sizeof(register_t));
490 td->td_pcb->pcb_onfault = NULL; /* Returns twice, not thrice */
497 cpu_fetch_syscall_args(struct thread *td)
500 struct trapframe *frame;
501 struct syscall_args *sa;
507 frame = td->td_frame;
510 sa->code = frame->fixreg[0];
511 params = (caddr_t)(frame->fixreg + FIRSTARG);
514 if (sa->code == SYS_syscall) {
516 * code is first argument,
517 * followed by actual args.
519 sa->code = *(register_t *) params;
520 params += sizeof(register_t);
522 } else if (sa->code == SYS___syscall) {
524 * Like syscall, but code is a quad,
525 * so as to maintain quad alignment
526 * for the rest of the args.
528 if (SV_PROC_FLAG(p, SV_ILP32)) {
529 params += sizeof(register_t);
530 sa->code = *(register_t *) params;
531 params += sizeof(register_t);
534 sa->code = *(register_t *) params;
535 params += sizeof(register_t);
540 if (p->p_sysent->sv_mask)
541 sa->code &= p->p_sysent->sv_mask;
542 if (sa->code >= p->p_sysent->sv_size)
543 sa->callp = &p->p_sysent->sv_table[0];
545 sa->callp = &p->p_sysent->sv_table[sa->code];
547 sa->narg = sa->callp->sy_narg;
549 if (SV_PROC_FLAG(p, SV_ILP32)) {
550 argsz = sizeof(uint32_t);
552 for (i = 0; i < n; i++)
553 sa->args[i] = ((u_register_t *)(params))[i] &
556 argsz = sizeof(uint64_t);
558 for (i = 0; i < n; i++)
559 sa->args[i] = ((u_register_t *)(params))[i];
563 error = copyin(MOREARGS(frame->fixreg[1]), sa->args + n,
564 (sa->narg - n) * argsz);
569 if (SV_PROC_FLAG(p, SV_ILP32) && sa->narg > n) {
570 /* Expand the size of arguments copied from the stack */
572 for (i = sa->narg; i >= n; i--)
573 sa->args[i] = ((uint32_t *)(&sa->args[n]))[i-n];
578 td->td_retval[0] = 0;
579 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
584 #include "../../kern/subr_syscall.c"
587 syscall(struct trapframe *frame)
593 td->td_frame = frame;
595 #if defined(__powerpc64__) && defined(AIM)
597 * Speculatively restore last user SLB segment, which we know is
598 * invalid already, since we are likely to do copyin()/copyout().
600 __asm __volatile ("slbmte %0, %1; isync" ::
601 "r"(td->td_pcb->pcb_cpu.aim.usr_vsid), "r"(USER_SLB_SLBE));
604 error = syscallenter(td);
605 syscallret(td, error);
608 #if defined(__powerpc64__) && defined(AIM)
609 /* Handle kernel SLB faults -- runs in real mode, all seat belts off */
611 handle_kernel_slb_spill(int type, register_t dar, register_t srr0)
613 struct slb *slbcache;
618 addr = (type == EXC_ISE) ? srr0 : dar;
619 slbcache = PCPU_GET(slb);
620 esid = (uintptr_t)addr >> ADDR_SR_SHFT;
621 slbe = (esid << SLBE_ESID_SHIFT) | SLBE_VALID;
623 /* See if the hardware flushed this somehow (can happen in LPARs) */
624 for (i = 0; i < n_slbs; i++)
625 if (slbcache[i].slbe == (slbe | (uint64_t)i))
628 /* Not in the map, needs to actually be added */
629 slbv = kernel_va_to_slbv(addr);
630 if (slbcache[USER_SLB_SLOT].slbe == 0) {
631 for (i = 0; i < n_slbs; i++) {
632 if (i == USER_SLB_SLOT)
634 if (!(slbcache[i].slbe & SLBE_VALID))
639 slbcache[USER_SLB_SLOT].slbe = 1;
642 /* Sacrifice a random SLB entry that is not the user entry */
644 if (i == USER_SLB_SLOT)
648 /* Write new entry */
649 slbcache[i].slbv = slbv;
650 slbcache[i].slbe = slbe | (uint64_t)i;
652 /* Trap handler will restore from cache on exit */
656 handle_user_slb_spill(pmap_t pm, vm_offset_t addr)
658 struct slb *user_entry;
662 esid = (uintptr_t)addr >> ADDR_SR_SHFT;
665 user_entry = user_va_to_slb_entry(pm, addr);
667 if (user_entry == NULL) {
668 /* allocate_vsid auto-spills it */
669 (void)allocate_user_vsid(pm, esid, 0);
672 * Check that another CPU has not already mapped this.
673 * XXX: Per-thread SLB caches would be better.
675 for (i = 0; i < pm->pm_slb_len; i++)
676 if (pm->pm_slb[i] == user_entry)
679 if (i == pm->pm_slb_len)
680 slb_insert_user(pm, user_entry);
689 trap_pfault(struct trapframe *frame, int user)
703 if (frame->exc == EXC_ISI) {
705 ftype = VM_PROT_EXECUTE;
706 if (frame->srr1 & SRR1_ISI_PFAULT)
707 ftype |= VM_PROT_READ;
711 if (frame->cpu.booke.esr & ESR_ST)
713 if (frame->cpu.aim.dsisr & DSISR_STORE)
715 ftype = VM_PROT_WRITE;
717 ftype = VM_PROT_READ;
721 KASSERT(p->p_vmspace != NULL, ("trap_pfault: vmspace NULL"));
722 map = &p->p_vmspace->vm_map;
725 if (eva < VM_MAXUSER_ADDRESS) {
727 if ((eva >> ADDR_SR_SHFT) == (USER_ADDR >> ADDR_SR_SHFT)) {
729 map = &p->p_vmspace->vm_map;
732 user_sr = td->td_pcb->pcb_cpu.aim.usr_segm;
733 eva &= ADDR_PIDX | ADDR_POFF;
734 eva |= user_sr << ADDR_SR_SHFT;
740 va = trunc_page(eva);
742 /* Fault in the page. */
743 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
745 * XXXDTRACE: add dtrace_doubletrap_func here?
748 if (rv == KERN_SUCCESS)
751 if (!user && handle_onfault(frame))
758 * For now, this only deals with the particular unaligned access case
759 * that gcc tends to generate. Eventually it should handle all of the
760 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
764 fix_unaligned(struct thread *td, struct trapframe *frame)
766 struct thread *fputhread;
774 indicator = (frame->cpu.booke.esr & (ESR_ST|ESR_SPE));
775 if (indicator & ESR_SPE) {
776 if (copyin((void *)frame->srr0, &inst, sizeof(inst)) != 0)
778 reg = EXC_ALI_SPE_REG(inst);
779 fpr = (double *)td->td_pcb->pcb_vec.vr[reg];
780 fputhread = PCPU_GET(vecthread);
782 /* Juggle the SPE to ensure that we've initialized
783 * the registers, and that their current state is in
786 if (fputhread != td) {
793 if (!(indicator & ESR_ST)) {
794 if (copyin((void *)frame->dar, fpr,
795 sizeof(double)) != 0)
797 frame->fixreg[reg] = td->td_pcb->pcb_vec.vr[reg][1];
800 td->td_pcb->pcb_vec.vr[reg][1] = frame->fixreg[reg];
801 if (copyout(fpr, (void *)frame->dar,
802 sizeof(double)) != 0)
808 indicator = EXC_ALI_OPCODE_INDICATOR(frame->cpu.aim.dsisr);
813 reg = EXC_ALI_RST(frame->cpu.aim.dsisr);
814 fpr = &td->td_pcb->pcb_fpu.fpr[reg].fpr;
815 fputhread = PCPU_GET(fputhread);
817 /* Juggle the FPU to ensure that we've initialized
818 * the FPRs, and that their current state is in
821 if (fputhread != td) {
828 if (indicator == EXC_ALI_LFD) {
829 if (copyin((void *)frame->dar, fpr,
830 sizeof(double)) != 0)
834 if (copyout(fpr, (void *)frame->dar,
835 sizeof(double)) != 0)
848 db_trap_glue(struct trapframe *frame)
851 if (!(frame->srr1 & PSL_PR)
852 && (frame->exc == EXC_TRC || frame->exc == EXC_RUNMODETRC
854 || (frame->exc == EXC_PGM
855 && (frame->srr1 & EXC_PGM_TRAP))
857 || (frame->exc == EXC_DEBUG)
858 || (frame->cpu.booke.esr & ESR_PTR)
860 || frame->exc == EXC_BPT
861 || frame->exc == EXC_DSI)) {
862 int type = frame->exc;
864 /* Ignore DTrace traps. */
865 if (*(uint32_t *)frame->srr0 == EXC_DTRACE)
868 if (type == EXC_PGM && (frame->srr1 & EXC_PGM_TRAP)) {
870 if (type == EXC_DEBUG ||
871 (frame->cpu.booke.esr & ESR_PTR)) {
875 return (kdb_trap(type, 0, frame));