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_fpu_emu.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 <security/audit/audit.h>
59 #include <vm/vm_extern.h>
60 #include <vm/vm_param.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_page.h>
65 #include <machine/cpu.h>
66 #include <machine/db_machdep.h>
67 #include <machine/frame.h>
68 #include <machine/pcb.h>
69 #include <machine/pmap.h>
70 #include <machine/psl.h>
71 #include <machine/trap.h>
72 #include <machine/spr.h>
75 #include <powerpc/fpu/fpu_extern.h>
82 #define FAULTBUF_CTR 4
83 #define FAULTBUF_XER 5
84 #define FAULTBUF_R13 6
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 int setfault(faultbuf); /* defined in locore.S */
96 /* Why are these not defined in a header? */
97 int badaddr(void *, size_t);
98 int badaddr_read(void *, size_t, int *);
100 struct powerpc_exception {
105 static struct powerpc_exception powerpc_exceptions[] = {
106 { EXC_CRIT, "critical input" },
107 { EXC_MCHK, "machine check" },
108 { EXC_DSI, "data storage interrupt" },
109 { EXC_ISI, "instruction storage interrupt" },
110 { EXC_EXI, "external interrupt" },
111 { EXC_ALI, "alignment" },
112 { EXC_PGM, "program" },
113 { EXC_SC, "system call" },
114 { EXC_APU, "auxiliary proc unavailable" },
115 { EXC_DECR, "decrementer" },
116 { EXC_FIT, "fixed-interval timer" },
117 { EXC_WDOG, "watchdog timer" },
118 { EXC_DTMISS, "data tlb miss" },
119 { EXC_ITMISS, "instruction tlb miss" },
120 { EXC_DEBUG, "debug" },
121 { EXC_PERF, "performance monitoring" },
126 trapname(u_int vector)
128 struct powerpc_exception *pe;
130 for (pe = powerpc_exceptions; pe->vector != EXC_LAST; pe++) {
131 if (pe->vector == vector)
139 trap(struct trapframe *frame)
153 PCPU_INC(cnt.v_trap);
160 user = (frame->srr1 & PSL_PR) ? 1 : 0;
162 CTR3(KTR_TRAP, "trap: %s type=%s (%s)", p->p_comm,
163 trapname(type), user ? "user" : "kernel");
166 td->td_frame = frame;
167 if (td->td_ucred != p->p_ucred)
168 cred_update_thread(td);
170 /* User Mode Traps */
174 sig = trap_pfault(frame, 1);
182 if (fix_unaligned(td, frame) != 0)
188 case EXC_DEBUG: /* Single stepping */
189 mtspr(SPR_DBSR, mfspr(SPR_DBSR));
190 frame->srr1 &= ~PSL_DE;
191 frame->cpu.booke.dbcr0 &= ~(DBCR0_IDM || DBCR0_IC);
195 case EXC_PGM: /* Program exception */
197 sig = fpu_emulate(frame,
198 (struct fpreg *)&td->td_pcb->pcb_fpu);
200 /* XXX SIGILL for non-trap instructions. */
209 /* Kernel Mode Traps */
210 KASSERT(cold || td->td_ucred != NULL,
211 ("kernel trap doesn't have ucred"));
215 mtspr(SPR_DBSR, mfspr(SPR_DBSR));
216 kdb_trap(frame->exc, 0, frame);
220 if (trap_pfault(frame, 0) == 0)
225 if (handle_onfault(frame))
230 if (frame->cpu.booke.esr & ESR_PTR)
231 kdb_trap(EXC_PGM, 0, frame);
241 if (p->p_sysent->sv_transtrap != NULL)
242 sig = (p->p_sysent->sv_transtrap)(sig, type);
243 ksiginfo_init_trap(&ksi);
245 ksi.ksi_code = type; /* XXX, not POSIX */
246 /* ksi.ksi_addr = ? */
247 ksi.ksi_trapno = type;
248 trapsignal(td, &ksi);
255 trap_fatal(struct trapframe *frame)
258 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
260 if ((debugger_on_panic || kdb_active) &&
261 kdb_trap(frame->exc, 0, frame))
264 panic("%s trap", trapname(frame->exc));
268 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
273 printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
274 user ? "user" : "kernel");
276 printf(" exception = 0x%x (%s)\n", vector, trapname(vector));
281 va = frame->cpu.booke.dear;
290 printf(" virtual address = 0x%08x\n", va);
291 printf(" srr0 = 0x%08x\n", frame->srr0);
292 printf(" srr1 = 0x%08x\n", frame->srr1);
293 printf(" curthread = %p\n", curthread);
294 if (curthread != NULL)
295 printf(" pid = %d, comm = %s\n",
296 curthread->td_proc->p_pid, curthread->td_proc->p_comm);
301 * Handles a fatal fault when we have onfault state to recover. Returns
302 * non-zero if there was onfault recovery state available.
305 handle_onfault(struct trapframe *frame)
311 fb = td->td_pcb->pcb_onfault;
313 frame->srr0 = (*fb)[FAULTBUF_LR];
314 frame->fixreg[1] = (*fb)[FAULTBUF_R1];
315 frame->fixreg[2] = (*fb)[FAULTBUF_R2];
316 frame->fixreg[3] = 1;
317 frame->cr = (*fb)[FAULTBUF_CR];
318 frame->ctr = (*fb)[FAULTBUF_CTR];
319 frame->xer = (*fb)[FAULTBUF_XER];
320 bcopy(&(*fb)[FAULTBUF_R13], &frame->fixreg[13],
321 19 * sizeof(register_t));
328 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
331 struct trapframe *frame;
336 frame = td->td_frame;
338 sa->code = frame->fixreg[0];
339 params = (caddr_t)(frame->fixreg + FIRSTARG);
342 if (sa->code == SYS_syscall) {
344 * code is first argument,
345 * followed by actual args.
347 sa->code = *(u_int *) params;
348 params += sizeof(register_t);
350 } else if (sa->code == SYS___syscall) {
352 * Like syscall, but code is a quad,
353 * so as to maintain quad alignment
354 * for the rest of the args.
356 params += sizeof(register_t);
357 sa->code = *(u_int *) params;
358 params += sizeof(register_t);
362 if (p->p_sysent->sv_mask)
363 sa->code &= p->p_sysent->sv_mask;
364 if (sa->code >= p->p_sysent->sv_size)
365 sa->callp = &p->p_sysent->sv_table[0];
367 sa->callp = &p->p_sysent->sv_table[sa->code];
368 sa->narg = sa->callp->sy_narg;
370 bcopy(params, sa->args, n * sizeof(register_t));
372 error = copyin(MOREARGS(frame->fixreg[1]), sa->args + n,
373 (sa->narg - n) * sizeof(register_t));
378 td->td_retval[0] = 0;
379 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
384 #include "../../kern/subr_syscall.c"
387 syscall(struct trapframe *frame)
390 struct syscall_args sa;
394 td->td_frame = frame;
396 error = syscallenter(td, &sa);
397 syscallret(td, error, &sa);
401 trap_pfault(struct trapframe *frame, int user)
413 if (frame->exc == EXC_ISI) {
415 ftype = VM_PROT_READ | VM_PROT_EXECUTE;
418 eva = frame->cpu.booke.dear;
419 if (frame->cpu.booke.esr & ESR_ST)
420 ftype = VM_PROT_WRITE;
422 ftype = VM_PROT_READ;
426 KASSERT(p->p_vmspace != NULL, ("trap_pfault: vmspace NULL"));
427 map = &p->p_vmspace->vm_map;
429 if (eva < VM_MAXUSER_ADDRESS) {
431 if (p->p_vmspace == NULL)
434 map = &p->p_vmspace->vm_map;
440 va = trunc_page(eva);
442 if (map != kernel_map) {
444 * Keep swapout from messing with us during this
451 /* Fault in the user page: */
452 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
459 * Don't have to worry about process locking or stacks in the
462 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
465 if (rv == KERN_SUCCESS)
468 if (!user && handle_onfault(frame))
471 return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
475 badaddr(void *addr, size_t size)
478 return (badaddr_read(addr, size, NULL));
482 badaddr_read(void *addr, size_t size, int *rptr)
488 /* Get rid of any stale machine checks that have been waiting. */
489 __asm __volatile ("sync; isync");
494 td->td_pcb->pcb_onfault = 0;
495 __asm __volatile ("sync");
499 __asm __volatile ("sync");
503 x = *(volatile int8_t *)addr;
506 x = *(volatile int16_t *)addr;
509 x = *(volatile int32_t *)addr;
512 panic("badaddr: invalid size (%d)", size);
515 /* Make sure we took the machine check, if we caused one. */
516 __asm __volatile ("sync; isync");
518 td->td_pcb->pcb_onfault = 0;
519 __asm __volatile ("sync"); /* To be sure. */
521 /* Use the value to avoid reorder. */
529 * For now, this only deals with the particular unaligned access case
530 * that gcc tends to generate. Eventually it should handle all of the
531 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
535 fix_unaligned(struct thread *td, struct trapframe *frame)
538 struct thread *fputhread;
542 indicator = EXC_ALI_OPCODE_INDICATOR(frame->dsisr);
547 reg = EXC_ALI_RST(frame->dsisr);
548 fpr = &td->td_pcb->pcb_fpu.fpr[reg];
549 fputhread = PCPU_GET(fputhread);
550 /* Juggle the FPU to ensure that we've initialized
551 * the FPRs, and that their current state is in
554 if (fputhread != td) {
561 if (indicator == EXC_ALI_LFD) {
562 if (copyin((void *)frame->dar, fpr,
563 sizeof(double)) != 0)
567 if (copyout(fpr, (void *)frame->dar,
568 sizeof(double)) != 0)
580 int db_trap_glue(struct trapframe *);
582 db_trap_glue(struct trapframe *tf)
584 if (!(tf->srr1 & PSL_PR))
585 return (kdb_trap(tf->exc, 0, tf));