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_hwpmc_hooks.h"
38 #include "opt_kdtrace.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>
54 #include <sys/vmmeter.h>
56 #include <sys/pmckern.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/_inttypes.h>
70 #include <machine/altivec.h>
71 #include <machine/cpu.h>
72 #include <machine/db_machdep.h>
73 #include <machine/fpu.h>
74 #include <machine/frame.h>
75 #include <machine/pcb.h>
76 #include <machine/pmap.h>
77 #include <machine/psl.h>
78 #include <machine/trap.h>
79 #include <machine/spr.h>
80 #include <machine/sr.h>
82 static void trap_fatal(struct trapframe *frame);
83 static void printtrap(u_int vector, struct trapframe *frame, int isfatal,
85 static int trap_pfault(struct trapframe *frame, int user);
86 static int fix_unaligned(struct thread *td, struct trapframe *frame);
87 static int ppc_instr_emulate(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 int setfault(faultbuf); /* defined in locore.S */
99 /* Why are these not defined in a header? */
100 int badaddr(void *, size_t);
101 int badaddr_read(void *, size_t, int *);
103 struct powerpc_exception {
109 #include <sys/dtrace_bsd.h>
112 * This is a hook which is initialised by the dtrace module
113 * to handle traps which might occur during DTrace probe
116 dtrace_trap_func_t dtrace_trap_func;
118 dtrace_doubletrap_func_t dtrace_doubletrap_func;
121 * This is a hook which is initialised by the systrace module
122 * when it is loaded. This keeps the DTrace syscall provider
123 * implementation opaque.
125 systrace_probe_func_t systrace_probe_func;
128 * These hooks are necessary for the pid, usdt and fasttrap providers.
130 dtrace_fasttrap_probe_ptr_t dtrace_fasttrap_probe_ptr;
131 dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr;
132 dtrace_return_probe_ptr_t dtrace_return_probe_ptr;
133 int (*dtrace_invop_jump_addr)(struct trapframe *);
136 static struct powerpc_exception powerpc_exceptions[] = {
137 { 0x0100, "system reset" },
138 { 0x0200, "machine check" },
139 { 0x0300, "data storage interrupt" },
140 { 0x0380, "data segment exception" },
141 { 0x0400, "instruction storage interrupt" },
142 { 0x0480, "instruction segment exception" },
143 { 0x0500, "external interrupt" },
144 { 0x0600, "alignment" },
145 { 0x0700, "program" },
146 { 0x0800, "floating-point unavailable" },
147 { 0x0900, "decrementer" },
148 { 0x0c00, "system call" },
150 { 0x0e00, "floating-point assist" },
151 { 0x0f00, "performance monitoring" },
152 { 0x0f20, "altivec unavailable" },
153 { 0x1000, "instruction tlb miss" },
154 { 0x1100, "data load tlb miss" },
155 { 0x1200, "data store tlb miss" },
156 { 0x1300, "instruction breakpoint" },
157 { 0x1400, "system management" },
158 { 0x1600, "altivec assist" },
159 { 0x1700, "thermal management" },
160 { 0x2000, "run mode/trace" },
165 trapname(u_int vector)
167 struct powerpc_exception *pe;
169 for (pe = powerpc_exceptions; pe->vector != 0x3000; pe++) {
170 if (pe->vector == vector)
178 trap(struct trapframe *frame)
186 PCPU_INC(cnt.v_trap);
191 type = ucode = frame->exc;
193 user = frame->srr1 & PSL_PR;
195 CTR3(KTR_TRAP, "trap: %s type=%s (%s)", td->td_name,
196 trapname(type), user ? "user" : "kernel");
199 if (type == EXC_PERF && (pmc_intr != NULL)) {
200 (*pmc_intr)(PCPU_GET(cpuid), frame);
208 * A trap can occur while DTrace executes a probe. Before
209 * executing the probe, DTrace blocks re-scheduling and sets
210 * a flag in it's per-cpu flags to indicate that it doesn't
211 * want to fault. On returning from the probe, the no-fault
212 * flag is cleared and finally re-scheduling is enabled.
214 * If the DTrace kernel module has registered a trap handler,
215 * call it and if it returns non-zero, assume that it has
216 * handled the trap and modified the trap frame so that this
217 * function can return normally.
220 * XXXDTRACE: add fasttrap and pid probes handlers here (if ever)
222 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
228 td->td_frame = frame;
229 if (td->td_ucred != p->p_ucred)
230 cred_update_thread(td);
232 /* User Mode Traps */
236 frame->srr1 &= ~PSL_SE;
243 if (handle_user_slb_spill(&p->p_vmspace->vm_pmap,
244 (type == EXC_ISE) ? frame->srr0 :
245 frame->cpu.aim.dar) != 0)
251 sig = trap_pfault(frame, 1);
259 KASSERT((td->td_pcb->pcb_flags & PCB_FPU) != PCB_FPU,
260 ("FPU already enabled for thread"));
265 KASSERT((td->td_pcb->pcb_flags & PCB_VEC) != PCB_VEC,
266 ("Altivec already enabled for thread"));
273 * We get a VPU assist exception for IEEE mode
274 * vector operations on denormalized floats.
275 * Emulating this is a giant pain, so for now,
276 * just switch off IEEE mode and treat them as
281 td->td_pcb->pcb_vec.vscr |= ALTIVEC_VSCR_NJ;
286 if (fix_unaligned(td, frame) != 0)
293 /* Identify the trap reason */
294 if (frame->srr1 & EXC_PGM_TRAP)
296 else if (ppc_instr_emulate(frame) == 0)
306 /* Kernel Mode Traps */
308 KASSERT(cold || td->td_ucred != NULL,
309 ("kernel trap doesn't have ucred"));
313 if (frame->srr1 & EXC_PGM_TRAP) {
314 if (*(uintptr_t *)frame->srr0 == 0x7c810808) {
315 if (dtrace_invop_jump_addr != NULL) {
316 dtrace_invop_jump_addr(frame);
325 if ((frame->cpu.aim.dar & SEGMENT_MASK) == USER_ADDR) {
326 __asm __volatile ("slbmte %0, %1" ::
327 "r"(td->td_pcb->pcb_cpu.aim.usr_vsid),
334 if (trap_pfault(frame, 0) == 0)
338 if (handle_onfault(frame))
348 if (p->p_sysent->sv_transtrap != NULL)
349 sig = (p->p_sysent->sv_transtrap)(sig, type);
350 ksiginfo_init_trap(&ksi);
352 ksi.ksi_code = (int) ucode; /* XXX, not POSIX */
353 /* ksi.ksi_addr = ? */
354 ksi.ksi_trapno = type;
355 trapsignal(td, &ksi);
362 trap_fatal(struct trapframe *frame)
365 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
367 if ((debugger_on_panic || kdb_active) &&
368 kdb_trap(frame->exc, 0, frame))
371 panic("%s trap", trapname(frame->exc));
375 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
379 printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
380 user ? "user" : "kernel");
382 printf(" exception = 0x%x (%s)\n", vector, trapname(vector));
386 printf(" virtual address = 0x%" PRIxPTR "\n",
388 printf(" dsisr = 0x%" PRIxPTR "\n",
389 frame->cpu.aim.dsisr);
393 printf(" virtual address = 0x%" PRIxPTR "\n", frame->srr0);
396 printf(" srr0 = 0x%" PRIxPTR "\n", frame->srr0);
397 printf(" srr1 = 0x%" PRIxPTR "\n", frame->srr1);
398 printf(" lr = 0x%" PRIxPTR "\n", frame->lr);
399 printf(" curthread = %p\n", curthread);
400 if (curthread != NULL)
401 printf(" pid = %d, comm = %s\n",
402 curthread->td_proc->p_pid, curthread->td_name);
407 * Handles a fatal fault when we have onfault state to recover. Returns
408 * non-zero if there was onfault recovery state available.
411 handle_onfault(struct trapframe *frame)
417 fb = td->td_pcb->pcb_onfault;
419 frame->srr0 = (*fb)[0];
420 frame->fixreg[1] = (*fb)[1];
421 frame->fixreg[2] = (*fb)[2];
422 frame->fixreg[3] = 1;
423 frame->cr = (*fb)[3];
424 bcopy(&(*fb)[4], &frame->fixreg[13],
425 19 * sizeof(register_t));
432 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
435 struct trapframe *frame;
441 frame = td->td_frame;
443 sa->code = frame->fixreg[0];
444 params = (caddr_t)(frame->fixreg + FIRSTARG);
447 if (sa->code == SYS_syscall) {
449 * code is first argument,
450 * followed by actual args.
452 sa->code = *(register_t *) params;
453 params += sizeof(register_t);
455 } else if (sa->code == SYS___syscall) {
457 * Like syscall, but code is a quad,
458 * so as to maintain quad alignment
459 * for the rest of the args.
461 if (SV_PROC_FLAG(p, SV_ILP32)) {
462 params += sizeof(register_t);
463 sa->code = *(register_t *) params;
464 params += sizeof(register_t);
467 sa->code = *(register_t *) params;
468 params += sizeof(register_t);
473 if (p->p_sysent->sv_mask)
474 sa->code &= p->p_sysent->sv_mask;
475 if (sa->code >= p->p_sysent->sv_size)
476 sa->callp = &p->p_sysent->sv_table[0];
478 sa->callp = &p->p_sysent->sv_table[sa->code];
480 sa->narg = sa->callp->sy_narg;
482 if (SV_PROC_FLAG(p, SV_ILP32)) {
483 argsz = sizeof(uint32_t);
485 for (i = 0; i < n; i++)
486 sa->args[i] = ((u_register_t *)(params))[i] &
489 argsz = sizeof(uint64_t);
491 for (i = 0; i < n; i++)
492 sa->args[i] = ((u_register_t *)(params))[i];
496 error = copyin(MOREARGS(frame->fixreg[1]), sa->args + n,
497 (sa->narg - n) * argsz);
502 if (SV_PROC_FLAG(p, SV_ILP32) && sa->narg > n) {
503 /* Expand the size of arguments copied from the stack */
505 for (i = sa->narg; i >= n; i--)
506 sa->args[i] = ((uint32_t *)(&sa->args[n]))[i-n];
511 td->td_retval[0] = 0;
512 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
517 #include "../../kern/subr_syscall.c"
520 syscall(struct trapframe *frame)
523 struct syscall_args sa;
527 td->td_frame = frame;
531 * Speculatively restore last user SLB segment, which we know is
532 * invalid already, since we are likely to do copyin()/copyout().
534 __asm __volatile ("slbmte %0, %1; isync" ::
535 "r"(td->td_pcb->pcb_cpu.aim.usr_vsid), "r"(USER_SLB_SLBE));
538 error = syscallenter(td, &sa);
539 syscallret(td, error, &sa);
543 /* Handle kernel SLB faults -- runs in real mode, all seat belts off */
545 handle_kernel_slb_spill(int type, register_t dar, register_t srr0)
547 struct slb *slbcache;
552 addr = (type == EXC_ISE) ? srr0 : dar;
553 slbcache = PCPU_GET(slb);
554 esid = (uintptr_t)addr >> ADDR_SR_SHFT;
555 slbe = (esid << SLBE_ESID_SHIFT) | SLBE_VALID;
557 /* See if the hardware flushed this somehow (can happen in LPARs) */
558 for (i = 0; i < n_slbs; i++)
559 if (slbcache[i].slbe == (slbe | (uint64_t)i))
562 /* Not in the map, needs to actually be added */
563 slbv = kernel_va_to_slbv(addr);
564 if (slbcache[USER_SLB_SLOT].slbe == 0) {
565 for (i = 0; i < n_slbs; i++) {
566 if (i == USER_SLB_SLOT)
568 if (!(slbcache[i].slbe & SLBE_VALID))
573 slbcache[USER_SLB_SLOT].slbe = 1;
576 /* Sacrifice a random SLB entry that is not the user entry */
578 if (i == USER_SLB_SLOT)
582 /* Write new entry */
583 slbcache[i].slbv = slbv;
584 slbcache[i].slbe = slbe | (uint64_t)i;
586 /* Trap handler will restore from cache on exit */
590 handle_user_slb_spill(pmap_t pm, vm_offset_t addr)
592 struct slb *user_entry;
596 esid = (uintptr_t)addr >> ADDR_SR_SHFT;
599 user_entry = user_va_to_slb_entry(pm, addr);
601 if (user_entry == NULL) {
602 /* allocate_vsid auto-spills it */
603 (void)allocate_user_vsid(pm, esid, 0);
606 * Check that another CPU has not already mapped this.
607 * XXX: Per-thread SLB caches would be better.
609 for (i = 0; i < pm->pm_slb_len; i++)
610 if (pm->pm_slb[i] == user_entry)
613 if (i == pm->pm_slb_len)
614 slb_insert_user(pm, user_entry);
623 trap_pfault(struct trapframe *frame, int user)
635 if (frame->exc == EXC_ISI) {
637 ftype = VM_PROT_EXECUTE;
638 if (frame->srr1 & SRR1_ISI_PFAULT)
639 ftype |= VM_PROT_READ;
641 eva = frame->cpu.aim.dar;
642 if (frame->cpu.aim.dsisr & DSISR_STORE)
643 ftype = VM_PROT_WRITE;
645 ftype = VM_PROT_READ;
649 map = &p->p_vmspace->vm_map;
651 if ((eva >> ADDR_SR_SHFT) == (USER_ADDR >> ADDR_SR_SHFT)) {
652 if (p->p_vmspace == NULL)
655 map = &p->p_vmspace->vm_map;
657 user_sr = td->td_pcb->pcb_cpu.aim.usr_segm;
658 eva &= ADDR_PIDX | ADDR_POFF;
659 eva |= user_sr << ADDR_SR_SHFT;
664 va = trunc_page(eva);
666 if (map != kernel_map) {
668 * Keep swapout from messing with us during this
675 /* Fault in the user page: */
676 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
682 * XXXDTRACE: add dtrace_doubletrap_func here?
686 * Don't have to worry about process locking or stacks in the
689 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
692 if (rv == KERN_SUCCESS)
695 if (!user && handle_onfault(frame))
702 badaddr(void *addr, size_t size)
704 return (badaddr_read(addr, size, NULL));
708 badaddr_read(void *addr, size_t size, int *rptr)
714 /* Get rid of any stale machine checks that have been waiting. */
715 __asm __volatile ("sync; isync");
720 td->td_pcb->pcb_onfault = 0;
721 __asm __volatile ("sync");
725 __asm __volatile ("sync");
729 x = *(volatile int8_t *)addr;
732 x = *(volatile int16_t *)addr;
735 x = *(volatile int32_t *)addr;
738 panic("badaddr: invalid size (%zd)", size);
741 /* Make sure we took the machine check, if we caused one. */
742 __asm __volatile ("sync; isync");
744 td->td_pcb->pcb_onfault = 0;
745 __asm __volatile ("sync"); /* To be sure. */
747 /* Use the value to avoid reorder. */
755 * For now, this only deals with the particular unaligned access case
756 * that gcc tends to generate. Eventually it should handle all of the
757 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
761 fix_unaligned(struct thread *td, struct trapframe *frame)
763 struct thread *fputhread;
767 indicator = EXC_ALI_OPCODE_INDICATOR(frame->cpu.aim.dsisr);
772 reg = EXC_ALI_RST(frame->cpu.aim.dsisr);
773 fpr = &td->td_pcb->pcb_fpu.fpr[reg];
774 fputhread = PCPU_GET(fputhread);
776 /* Juggle the FPU to ensure that we've initialized
777 * the FPRs, and that their current state is in
780 if (fputhread != td) {
787 if (indicator == EXC_ALI_LFD) {
788 if (copyin((void *)frame->cpu.aim.dar, fpr,
789 sizeof(double)) != 0)
793 if (copyout(fpr, (void *)frame->cpu.aim.dar,
794 sizeof(double)) != 0)
805 ppc_instr_emulate(struct trapframe *frame)
810 instr = fuword32((void *)frame->srr0);
812 if ((instr & 0xfc1fffff) == 0x7c1f42a6) { /* mfpvr */
813 reg = (instr & ~0xfc1fffff) >> 21;
814 frame->fixreg[reg] = mfpvr();