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_ktrace.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>
54 #include <sys/ktrace.h>
56 #include <sys/vmmeter.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/cpu.h>
69 #include <machine/db_machdep.h>
70 #include <machine/fpu.h>
71 #include <machine/frame.h>
72 #include <machine/pcb.h>
73 #include <machine/pmap.h>
74 #include <machine/psl.h>
75 #include <machine/trap.h>
76 #include <machine/spr.h>
77 #include <machine/sr.h>
79 void trap(struct trapframe *);
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 handle_onfault(struct trapframe *frame);
87 static void syscall(struct trapframe *frame);
89 static __inline void setusr(u_int);
91 int setfault(faultbuf); /* defined in locore.S */
93 /* Why are these not defined in a header? */
94 int badaddr(void *, size_t);
95 int badaddr_read(void *, size_t, int *);
97 extern char *syscallnames[];
99 struct powerpc_exception {
104 static struct powerpc_exception powerpc_exceptions[] = {
105 { 0x0100, "system reset" },
106 { 0x0200, "machine check" },
107 { 0x0300, "data storage interrupt" },
108 { 0x0400, "instruction storage interrupt" },
109 { 0x0500, "external interrupt" },
110 { 0x0600, "alignment" },
111 { 0x0700, "program" },
112 { 0x0800, "floating-point unavailable" },
113 { 0x0900, "decrementer" },
114 { 0x0c00, "system call" },
116 { 0x0e00, "floating-point assist" },
117 { 0x0f00, "performance monitoring" },
118 { 0x0f20, "altivec unavailable" },
119 { 0x1000, "instruction tlb miss" },
120 { 0x1100, "data load tlb miss" },
121 { 0x1200, "data store tlb miss" },
122 { 0x1300, "instruction breakpoint" },
123 { 0x1400, "system management" },
124 { 0x1600, "altivec assist" },
125 { 0x1700, "thermal management" },
126 { 0x2000, "run mode/trace" },
131 trapname(u_int vector)
133 struct powerpc_exception *pe;
135 for (pe = powerpc_exceptions; pe->vector != 0x3000; pe++) {
136 if (pe->vector == vector)
144 trap(struct trapframe *frame)
152 PCPU_INC(cnt.v_trap);
154 td = PCPU_GET(curthread);
157 type = ucode = frame->exc;
159 user = frame->srr1 & PSL_PR;
161 CTR3(KTR_TRAP, "trap: %s type=%s (%s)", p->p_comm,
162 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 frame->srr1 &= ~PSL_SE;
180 sig = trap_pfault(frame, 1);
188 KASSERT((td->td_pcb->pcb_flags & PCB_FPU) != PCB_FPU,
189 ("FPU already enabled for thread"));
195 if ((vecthread = PCPU_GET(vecthread)) != NULL) {
196 KASSERT(vecthread != td,
197 ("altivec already enabled"));
200 PCPU_SET(vecthread, td);
201 td->td_pcb->pcb_veccpu = PCPU_GET(cpuid);
203 frame->srr1 |= PSL_VEC;
213 if (fix_unaligned(td, frame) != 0)
220 /* XXX temporarily */
221 /* XXX: Magic Number? */
222 if (frame->srr1 & 0x0002000)
232 /* Kernel Mode Traps */
234 KASSERT(cold || td->td_ucred != NULL,
235 ("kernel trap doesn't have ucred"));
238 if (trap_pfault(frame, 0) == 0)
242 if (handle_onfault(frame))
252 if (td != PCPU_GET(vecthread) ||
253 td->td_pcb->pcb_veccpu != PCPU_GET(cpuid))
254 frame->srr1 &= ~PSL_VEC;
258 if (p->p_sysent->sv_transtrap != NULL)
259 sig = (p->p_sysent->sv_transtrap)(sig, type);
260 ksiginfo_init_trap(&ksi);
262 ksi.ksi_code = (int) ucode; /* XXX, not POSIX */
263 /* ksi.ksi_addr = ? */
264 ksi.ksi_trapno = type;
265 trapsignal(td, &ksi);
269 mtx_assert(&Giant, MA_NOTOWNED);
273 trap_fatal(struct trapframe *frame)
276 printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
278 if ((debugger_on_panic || kdb_active) &&
279 kdb_trap(frame->exc, 0, frame))
282 panic("%s trap", trapname(frame->exc));
286 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
290 printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
291 user ? "user" : "kernel");
293 printf(" exception = 0x%x (%s)\n", vector >> 8,
297 printf(" virtual address = 0x%x\n", frame->dar);
300 printf(" virtual address = 0x%x\n", frame->srr0);
303 printf(" srr0 = 0x%x\n", frame->srr0);
304 printf(" srr1 = 0x%x\n", frame->srr1);
305 printf(" curthread = %p\n", curthread);
306 if (curthread != NULL)
307 printf(" pid = %d, comm = %s\n",
308 curthread->td_proc->p_pid, curthread->td_proc->p_comm);
313 * Handles a fatal fault when we have onfault state to recover. Returns
314 * non-zero if there was onfault recovery state available.
317 handle_onfault(struct trapframe *frame)
323 fb = td->td_pcb->pcb_onfault;
325 frame->srr0 = (*fb)[0];
326 frame->fixreg[1] = (*fb)[1];
327 frame->fixreg[2] = (*fb)[2];
328 frame->fixreg[3] = 1;
329 frame->cr = (*fb)[3];
330 bcopy(&(*fb)[4], &frame->fixreg[13],
331 19 * sizeof(register_t));
338 syscall(struct trapframe *frame)
341 struct sysent *callp;
349 td = PCPU_GET(curthread);
352 PCPU_INC(cnt.v_syscall);
355 if (p->p_flag & P_SA)
356 thread_user_enter(td);
359 code = frame->fixreg[0];
360 params = (caddr_t)(frame->fixreg + FIRSTARG);
363 if (p->p_sysent->sv_prepsyscall) {
365 * The prep code is MP aware.
367 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, ¶ms);
368 } else if (code == SYS_syscall) {
370 * code is first argument,
371 * followed by actual args.
373 code = *(u_int *) params;
374 params += sizeof(register_t);
376 } else if (code == SYS___syscall) {
378 * Like syscall, but code is a quad,
379 * so as to maintain quad alignment
380 * for the rest of the args.
382 params += sizeof(register_t);
383 code = *(u_int *) params;
384 params += sizeof(register_t);
388 if (p->p_sysent->sv_mask)
389 code &= p->p_sysent->sv_mask;
391 if (code >= p->p_sysent->sv_size)
392 callp = &p->p_sysent->sv_table[0];
394 callp = &p->p_sysent->sv_table[code];
396 narg = callp->sy_narg;
399 bcopy(params, args, n * sizeof(register_t));
400 error = copyin(MOREARGS(frame->fixreg[1]), args + n,
401 (narg - n) * sizeof(register_t));
402 params = (caddr_t)args;
406 CTR5(KTR_SYSC, "syscall: p=%s %s(%x %x %x)", p->p_comm,
408 frame->fixreg[FIRSTARG],
409 frame->fixreg[FIRSTARG+1],
410 frame->fixreg[FIRSTARG+2]);
413 if (KTRPOINT(td, KTR_SYSCALL))
414 ktrsyscall(code, narg, (register_t *)params);
420 td->td_retval[0] = 0;
421 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
423 STOPEVENT(p, S_SCE, narg);
425 PTRACESTOP_SC(p, td, S_PT_SCE);
427 AUDIT_SYSCALL_ENTER(code, td);
428 error = (*callp->sy_call)(td, params);
429 AUDIT_SYSCALL_EXIT(error, td);
431 CTR3(KTR_SYSC, "syscall: p=%s %s ret=%x", p->p_comm,
432 syscallnames[code], td->td_retval[0]);
436 if ((frame->fixreg[0] == SYS___syscall) &&
437 (code != SYS_lseek)) {
439 * 64-bit return, 32-bit syscall. Fixup byte order
441 frame->fixreg[FIRSTARG] = 0;
442 frame->fixreg[FIRSTARG + 1] = td->td_retval[0];
444 frame->fixreg[FIRSTARG] = td->td_retval[0];
445 frame->fixreg[FIRSTARG + 1] = td->td_retval[1];
447 /* XXX: Magic number */
448 frame->cr &= ~0x10000000;
452 * Set user's pc back to redo the system call.
460 if (p->p_sysent->sv_errsize) {
461 if (error >= p->p_sysent->sv_errsize)
462 error = -1; /* XXX */
464 error = p->p_sysent->sv_errtbl[error];
466 frame->fixreg[FIRSTARG] = error;
467 /* XXX: Magic number: Carry Flag Equivalent? */
468 frame->cr |= 0x10000000;
473 * Check for misbehavior.
475 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
476 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
477 KASSERT(td->td_critnest == 0,
478 ("System call %s returning in a critical section",
479 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
480 KASSERT(td->td_locks == 0,
481 ("System call %s returning with %d locks held",
482 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
486 if (KTRPOINT(td, KTR_SYSRET))
487 ktrsysret(code, error, td->td_retval[0]);
491 * Does the comment in the i386 code about errno apply here?
493 STOPEVENT(p, S_SCX, code);
495 PTRACESTOP_SC(p, td, S_PT_SCX);
499 trap_pfault(struct trapframe *frame, int user)
511 if (frame->exc == EXC_ISI) {
513 ftype = VM_PROT_READ | VM_PROT_EXECUTE;
516 if (frame->dsisr & DSISR_STORE)
517 ftype = VM_PROT_WRITE;
519 ftype = VM_PROT_READ;
523 map = &p->p_vmspace->vm_map;
525 if ((eva >> ADDR_SR_SHFT) == USER_SR) {
526 if (p->p_vmspace == NULL)
532 eva &= ADDR_PIDX | ADDR_POFF;
533 eva |= user_sr << ADDR_SR_SHFT;
534 map = &p->p_vmspace->vm_map;
539 va = trunc_page(eva);
541 if (map != kernel_map) {
543 * Keep swapout from messing with us during this
550 /* Fault in the user page: */
551 rv = vm_fault(map, va, ftype,
552 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
560 * Don't have to worry about process locking or stacks in the
563 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
566 if (rv == KERN_SUCCESS)
569 if (!user && handle_onfault(frame))
576 setusr(u_int content)
578 __asm __volatile ("isync; mtsr %0,%1; isync"
579 :: "n"(USER_SR), "r"(content));
583 badaddr(void *addr, size_t size)
585 return (badaddr_read(addr, size, NULL));
589 badaddr_read(void *addr, size_t size, int *rptr)
595 /* Get rid of any stale machine checks that have been waiting. */
596 __asm __volatile ("sync; isync");
598 td = PCPU_GET(curthread);
601 td->td_pcb->pcb_onfault = 0;
602 __asm __volatile ("sync");
606 __asm __volatile ("sync");
610 x = *(volatile int8_t *)addr;
613 x = *(volatile int16_t *)addr;
616 x = *(volatile int32_t *)addr;
619 panic("badaddr: invalid size (%d)", size);
622 /* Make sure we took the machine check, if we caused one. */
623 __asm __volatile ("sync; isync");
625 td->td_pcb->pcb_onfault = 0;
626 __asm __volatile ("sync"); /* To be sure. */
628 /* Use the value to avoid reorder. */
636 * For now, this only deals with the particular unaligned access case
637 * that gcc tends to generate. Eventually it should handle all of the
638 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
642 fix_unaligned(struct thread *td, struct trapframe *frame)
644 struct thread *fputhread;
648 indicator = EXC_ALI_OPCODE_INDICATOR(frame->dsisr);
653 reg = EXC_ALI_RST(frame->dsisr);
654 fpr = &td->td_pcb->pcb_fpu.fpr[reg];
655 fputhread = PCPU_GET(fputhread);
657 /* Juggle the FPU to ensure that we've initialized
658 * the FPRs, and that their current state is in
661 if (fputhread != td) {
668 if (indicator == EXC_ALI_LFD) {
669 if (copyin((void *)frame->dar, fpr,
670 sizeof(double)) != 0)
674 if (copyout(fpr, (void *)frame->dar,
675 sizeof(double)) != 0)