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"
39 #include <sys/param.h>
44 #include <sys/mutex.h>
45 #include <sys/pioctl.h>
46 #include <sys/ptrace.h>
47 #include <sys/reboot.h>
48 #include <sys/syscall.h>
49 #include <sys/sysent.h>
50 #include <sys/systm.h>
52 #include <sys/signalvar.h>
53 #include <sys/vmmeter.h>
55 #include <sys/pmckern.h>
58 #include <security/audit/audit.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_param.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_page.h>
68 #include <machine/_inttypes.h>
69 #include <machine/altivec.h>
70 #include <machine/cpu.h>
71 #include <machine/db_machdep.h>
72 #include <machine/fpu.h>
73 #include <machine/frame.h>
74 #include <machine/pcb.h>
75 #include <machine/pmap.h>
76 #include <machine/psl.h>
77 #include <machine/trap.h>
78 #include <machine/spr.h>
79 #include <machine/sr.h>
81 static void trap_fatal(struct trapframe *frame);
82 static void printtrap(u_int vector, struct trapframe *frame, int isfatal,
84 static int trap_pfault(struct trapframe *frame, int user);
85 static int fix_unaligned(struct thread *td, struct trapframe *frame);
86 static int ppc_instr_emulate(struct trapframe *frame);
87 static int handle_onfault(struct trapframe *frame);
88 static void syscall(struct trapframe *frame);
91 void handle_kernel_slb_spill(int, register_t, register_t);
92 static int handle_user_slb_spill(pmap_t pm, vm_offset_t addr);
96 int setfault(faultbuf); /* defined in locore.S */
98 /* Why are these not defined in a header? */
99 int badaddr(void *, size_t);
100 int badaddr_read(void *, size_t, int *);
102 struct powerpc_exception {
107 static struct powerpc_exception powerpc_exceptions[] = {
108 { 0x0100, "system reset" },
109 { 0x0200, "machine check" },
110 { 0x0300, "data storage interrupt" },
111 { 0x0380, "data segment exception" },
112 { 0x0400, "instruction storage interrupt" },
113 { 0x0480, "instruction segment exception" },
114 { 0x0500, "external interrupt" },
115 { 0x0600, "alignment" },
116 { 0x0700, "program" },
117 { 0x0800, "floating-point unavailable" },
118 { 0x0900, "decrementer" },
119 { 0x0c00, "system call" },
121 { 0x0e00, "floating-point assist" },
122 { 0x0f00, "performance monitoring" },
123 { 0x0f20, "altivec unavailable" },
124 { 0x1000, "instruction tlb miss" },
125 { 0x1100, "data load tlb miss" },
126 { 0x1200, "data store tlb miss" },
127 { 0x1300, "instruction breakpoint" },
128 { 0x1400, "system management" },
129 { 0x1600, "altivec assist" },
130 { 0x1700, "thermal management" },
131 { 0x2000, "run mode/trace" },
136 trapname(u_int vector)
138 struct powerpc_exception *pe;
140 for (pe = powerpc_exceptions; pe->vector != 0x3000; pe++) {
141 if (pe->vector == vector)
149 trap(struct trapframe *frame)
157 PCPU_INC(cnt.v_trap);
162 type = ucode = frame->exc;
164 user = frame->srr1 & PSL_PR;
166 CTR3(KTR_TRAP, "trap: %s type=%s (%s)", td->td_name,
167 trapname(type), user ? "user" : "kernel");
170 if (type == EXC_PERF && (pmc_intr != NULL)) {
172 (*pmc_intr)(PCPU_GET(cpuid), frame);
181 td->td_frame = frame;
182 if (td->td_ucred != p->p_ucred)
183 cred_update_thread(td);
185 /* User Mode Traps */
189 frame->srr1 &= ~PSL_SE;
196 if (handle_user_slb_spill(&p->p_vmspace->vm_pmap,
197 (type == EXC_ISE) ? frame->srr0 :
198 frame->cpu.aim.dar) != 0)
204 sig = trap_pfault(frame, 1);
212 KASSERT((td->td_pcb->pcb_flags & PCB_FPU) != PCB_FPU,
213 ("FPU already enabled for thread"));
218 KASSERT((td->td_pcb->pcb_flags & PCB_VEC) != PCB_VEC,
219 ("Altivec already enabled for thread"));
226 * We get a VPU assist exception for IEEE mode
227 * vector operations on denormalized floats.
228 * Emulating this is a giant pain, so for now,
229 * just switch off IEEE mode and treat them as
234 td->td_pcb->pcb_vec.vscr |= ALTIVEC_VSCR_NJ;
239 if (fix_unaligned(td, frame) != 0)
246 /* Identify the trap reason */
247 if (frame->srr1 & EXC_PGM_TRAP)
249 else if (ppc_instr_emulate(frame) == 0)
259 /* Kernel Mode Traps */
261 KASSERT(cold || td->td_ucred != NULL,
262 ("kernel trap doesn't have ucred"));
266 if ((frame->cpu.aim.dar & SEGMENT_MASK) == USER_ADDR) {
267 __asm __volatile ("slbmte %0, %1" ::
268 "r"(td->td_pcb->pcb_cpu.aim.usr_vsid),
275 if (trap_pfault(frame, 0) == 0)
279 if (handle_onfault(frame))
289 if (p->p_sysent->sv_transtrap != NULL)
290 sig = (p->p_sysent->sv_transtrap)(sig, type);
291 ksiginfo_init_trap(&ksi);
293 ksi.ksi_code = (int) ucode; /* XXX, not POSIX */
294 /* ksi.ksi_addr = ? */
295 ksi.ksi_trapno = type;
296 trapsignal(td, &ksi);
300 mtx_assert(&Giant, MA_NOTOWNED);
304 trap_fatal(struct trapframe *frame)
307 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
309 if ((debugger_on_panic || kdb_active) &&
310 kdb_trap(frame->exc, 0, frame))
313 panic("%s trap", trapname(frame->exc));
317 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
321 printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
322 user ? "user" : "kernel");
324 printf(" exception = 0x%x (%s)\n", vector, trapname(vector));
328 printf(" virtual address = 0x%" PRIxPTR "\n",
333 printf(" virtual address = 0x%" PRIxPTR "\n", frame->srr0);
336 printf(" srr0 = 0x%" PRIxPTR "\n", frame->srr0);
337 printf(" srr1 = 0x%" PRIxPTR "\n", frame->srr1);
338 printf(" lr = 0x%" PRIxPTR "\n", frame->lr);
339 printf(" curthread = %p\n", curthread);
340 if (curthread != NULL)
341 printf(" pid = %d, comm = %s\n",
342 curthread->td_proc->p_pid, curthread->td_name);
347 * Handles a fatal fault when we have onfault state to recover. Returns
348 * non-zero if there was onfault recovery state available.
351 handle_onfault(struct trapframe *frame)
357 fb = td->td_pcb->pcb_onfault;
359 frame->srr0 = (*fb)[0];
360 frame->fixreg[1] = (*fb)[1];
361 frame->fixreg[2] = (*fb)[2];
362 frame->fixreg[3] = 1;
363 frame->cr = (*fb)[3];
364 bcopy(&(*fb)[4], &frame->fixreg[13],
365 19 * sizeof(register_t));
372 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
375 struct trapframe *frame;
381 frame = td->td_frame;
383 sa->code = frame->fixreg[0];
384 params = (caddr_t)(frame->fixreg + FIRSTARG);
387 if (sa->code == SYS_syscall) {
389 * code is first argument,
390 * followed by actual args.
392 sa->code = *(register_t *) params;
393 params += sizeof(register_t);
395 } else if (sa->code == SYS___syscall) {
397 * Like syscall, but code is a quad,
398 * so as to maintain quad alignment
399 * for the rest of the args.
401 if (SV_PROC_FLAG(p, SV_ILP32)) {
402 params += sizeof(register_t);
403 sa->code = *(register_t *) params;
404 params += sizeof(register_t);
407 sa->code = *(register_t *) params;
408 params += sizeof(register_t);
413 if (p->p_sysent->sv_mask)
414 sa->code &= p->p_sysent->sv_mask;
415 if (sa->code >= p->p_sysent->sv_size)
416 sa->callp = &p->p_sysent->sv_table[0];
418 sa->callp = &p->p_sysent->sv_table[sa->code];
420 sa->narg = sa->callp->sy_narg;
422 if (SV_PROC_FLAG(p, SV_ILP32)) {
423 argsz = sizeof(uint32_t);
425 for (i = 0; i < n; i++)
426 sa->args[i] = ((u_register_t *)(params))[i] &
429 argsz = sizeof(uint64_t);
431 for (i = 0; i < n; i++)
432 sa->args[i] = ((u_register_t *)(params))[i];
436 error = copyin(MOREARGS(frame->fixreg[1]), sa->args + n,
437 (sa->narg - n) * argsz);
442 if (SV_PROC_FLAG(p, SV_ILP32) && sa->narg > n) {
443 /* Expand the size of arguments copied from the stack */
445 for (i = sa->narg; i >= n; i--)
446 sa->args[i] = ((uint32_t *)(&sa->args[n]))[i-n];
451 td->td_retval[0] = 0;
452 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
457 #include "../../kern/subr_syscall.c"
460 syscall(struct trapframe *frame)
463 struct syscall_args sa;
467 td->td_frame = frame;
471 * Speculatively restore last user SLB segment, which we know is
472 * invalid already, since we are likely to do copyin()/copyout().
474 __asm __volatile ("slbmte %0, %1; isync" ::
475 "r"(td->td_pcb->pcb_cpu.aim.usr_vsid), "r"(USER_SLB_SLBE));
478 error = syscallenter(td, &sa);
479 syscallret(td, error, &sa);
483 /* Handle kernel SLB faults -- runs in real mode, all seat belts off */
485 handle_kernel_slb_spill(int type, register_t dar, register_t srr0)
487 struct slb *slbcache;
492 addr = (type == EXC_ISE) ? srr0 : dar;
493 slbcache = PCPU_GET(slb);
494 esid = (uintptr_t)addr >> ADDR_SR_SHFT;
495 slbe = (esid << SLBE_ESID_SHIFT) | SLBE_VALID;
497 /* See if the hardware flushed this somehow (can happen in LPARs) */
498 for (i = 0; i < n_slbs; i++)
499 if (slbcache[i].slbe == (slbe | (uint64_t)i))
502 /* Not in the map, needs to actually be added */
503 slbv = kernel_va_to_slbv(addr);
504 if (slbcache[USER_SLB_SLOT].slbe == 0) {
505 for (i = 0; i < n_slbs; i++) {
506 if (i == USER_SLB_SLOT)
508 if (!(slbcache[i].slbe & SLBE_VALID))
513 slbcache[USER_SLB_SLOT].slbe = 1;
516 /* Sacrifice a random SLB entry that is not the user entry */
518 if (i == USER_SLB_SLOT)
522 /* Write new entry */
523 slbcache[i].slbv = slbv;
524 slbcache[i].slbe = slbe | (uint64_t)i;
526 /* Trap handler will restore from cache on exit */
530 handle_user_slb_spill(pmap_t pm, vm_offset_t addr)
532 struct slb *user_entry;
536 esid = (uintptr_t)addr >> ADDR_SR_SHFT;
539 user_entry = user_va_to_slb_entry(pm, addr);
541 if (user_entry == NULL) {
542 /* allocate_vsid auto-spills it */
543 (void)allocate_user_vsid(pm, esid, 0);
546 * Check that another CPU has not already mapped this.
547 * XXX: Per-thread SLB caches would be better.
549 for (i = 0; i < pm->pm_slb_len; i++)
550 if (pm->pm_slb[i] == user_entry)
553 if (i == pm->pm_slb_len)
554 slb_insert_user(pm, user_entry);
563 trap_pfault(struct trapframe *frame, int user)
575 if (frame->exc == EXC_ISI) {
577 ftype = VM_PROT_EXECUTE;
578 if (frame->srr1 & SRR1_ISI_PFAULT)
579 ftype |= VM_PROT_READ;
581 eva = frame->cpu.aim.dar;
582 if (frame->cpu.aim.dsisr & DSISR_STORE)
583 ftype = VM_PROT_WRITE;
585 ftype = VM_PROT_READ;
589 map = &p->p_vmspace->vm_map;
591 if ((eva >> ADDR_SR_SHFT) == (USER_ADDR >> ADDR_SR_SHFT)) {
592 if (p->p_vmspace == NULL)
595 map = &p->p_vmspace->vm_map;
597 user_sr = td->td_pcb->pcb_cpu.aim.usr_segm;
598 eva &= ADDR_PIDX | ADDR_POFF;
599 eva |= user_sr << ADDR_SR_SHFT;
604 va = trunc_page(eva);
606 if (map != kernel_map) {
608 * Keep swapout from messing with us during this
615 /* Fault in the user page: */
616 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
623 * Don't have to worry about process locking or stacks in the
626 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
629 if (rv == KERN_SUCCESS)
632 if (!user && handle_onfault(frame))
639 badaddr(void *addr, size_t size)
641 return (badaddr_read(addr, size, NULL));
645 badaddr_read(void *addr, size_t size, int *rptr)
651 /* Get rid of any stale machine checks that have been waiting. */
652 __asm __volatile ("sync; isync");
657 td->td_pcb->pcb_onfault = 0;
658 __asm __volatile ("sync");
662 __asm __volatile ("sync");
666 x = *(volatile int8_t *)addr;
669 x = *(volatile int16_t *)addr;
672 x = *(volatile int32_t *)addr;
675 panic("badaddr: invalid size (%zd)", size);
678 /* Make sure we took the machine check, if we caused one. */
679 __asm __volatile ("sync; isync");
681 td->td_pcb->pcb_onfault = 0;
682 __asm __volatile ("sync"); /* To be sure. */
684 /* Use the value to avoid reorder. */
692 * For now, this only deals with the particular unaligned access case
693 * that gcc tends to generate. Eventually it should handle all of the
694 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
698 fix_unaligned(struct thread *td, struct trapframe *frame)
700 struct thread *fputhread;
704 indicator = EXC_ALI_OPCODE_INDICATOR(frame->cpu.aim.dsisr);
709 reg = EXC_ALI_RST(frame->cpu.aim.dsisr);
710 fpr = &td->td_pcb->pcb_fpu.fpr[reg];
711 fputhread = PCPU_GET(fputhread);
713 /* Juggle the FPU to ensure that we've initialized
714 * the FPRs, and that their current state is in
717 if (fputhread != td) {
724 if (indicator == EXC_ALI_LFD) {
725 if (copyin((void *)frame->cpu.aim.dar, fpr,
726 sizeof(double)) != 0)
730 if (copyout(fpr, (void *)frame->cpu.aim.dar,
731 sizeof(double)) != 0)
742 ppc_instr_emulate(struct trapframe *frame)
747 instr = fuword32((void *)frame->srr0);
749 if ((instr & 0xfc1fffff) == 0x7c1f42a6) { /* mfpvr */
750 reg = (instr & ~0xfc1fffff) >> 21;
751 frame->fixreg[reg] = mfpvr();