1 /* $NetBSD: fault.c,v 1.45 2003/11/20 14:44:36 scw Exp $ */
4 * Copyright 2004 Olivier Houchard
5 * Copyright 2003 Wasabi Systems, Inc.
8 * Written by Steve C. Woodford for Wasabi Systems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the NetBSD Project by
21 * Wasabi Systems, Inc.
22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 * Copyright (c) 1994-1997 Mark Brinicombe.
40 * Copyright (c) 1994 Brini.
41 * All rights reserved.
43 * This code is derived from software written for Brini by Mark Brinicombe
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by Brini.
56 * 4. The name of the company nor the name of the author may be used to
57 * endorse or promote products derived from this software without specific
58 * prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
61 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
62 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
64 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * RiscBSD kernel project
81 #include <sys/cdefs.h>
82 __FBSDID("$FreeBSD$");
84 #include <sys/param.h>
85 #include <sys/systm.h>
88 #include <sys/mutex.h>
89 #include <sys/signalvar.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_extern.h>
97 #include <machine/acle-compat.h>
98 #include <machine/cpu.h>
99 #include <machine/frame.h>
100 #include <machine/machdep.h>
101 #include <machine/pcb.h>
102 #include <machine/vmparam.h>
109 #include <sys/dtrace_bsd.h>
112 extern char fusubailout[];
115 int last_fault_code; /* For the benefit of pmap_fault_fixup() */
123 int (*func)(struct trapframe *, u_int, u_int, struct thread *,
128 static int dab_fatal(struct trapframe *, u_int, u_int, struct thread *,
130 static int dab_align(struct trapframe *, u_int, u_int, struct thread *,
132 static int dab_buserr(struct trapframe *, u_int, u_int, struct thread *,
134 static void prefetch_abort_handler(struct trapframe *);
136 static const struct data_abort data_aborts[] = {
137 {dab_fatal, "Vector Exception"},
138 {dab_align, "Alignment Fault 1"},
139 {dab_fatal, "Terminal Exception"},
140 {dab_align, "Alignment Fault 3"},
141 {dab_buserr, "External Linefetch Abort (S)"},
142 {NULL, "Translation Fault (S)"},
143 #if (ARM_MMU_V6 + ARM_MMU_V7) != 0
144 {NULL, "Translation Flag Fault"},
146 {dab_buserr, "External Linefetch Abort (P)"},
148 {NULL, "Translation Fault (P)"},
149 {dab_buserr, "External Non-Linefetch Abort (S)"},
150 {NULL, "Domain Fault (S)"},
151 {dab_buserr, "External Non-Linefetch Abort (P)"},
152 {NULL, "Domain Fault (P)"},
153 {dab_buserr, "External Translation Abort (L1)"},
154 {NULL, "Permission Fault (S)"},
155 {dab_buserr, "External Translation Abort (L2)"},
156 {NULL, "Permission Fault (P)"}
159 /* Determine if a fault came from user mode */
160 #define TRAP_USERMODE(tf) ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
162 /* Determine if 'x' is a permission fault */
163 #define IS_PERMISSION_FAULT(x) \
164 (((1 << ((x) & FAULT_TYPE_MASK)) & \
165 ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0)
168 call_trapsignal(struct thread *td, int sig, u_long code)
172 ksiginfo_init_trap(&ksi);
174 ksi.ksi_code = (int)code;
175 trapsignal(td, &ksi);
179 abort_handler(struct trapframe *tf, int type)
184 u_int user, far, fsr;
193 return (prefetch_abort_handler(tf));
195 /* Grab FAR/FSR before enabling interrupts */
196 far = cpu_faultaddress();
197 fsr = cpu_faultstatus();
199 printf("data abort: fault address=%p (from pc=%p lr=%p)\n",
200 (void*)far, (void*)tf->tf_pc, (void*)tf->tf_svc_lr);
203 /* Update vmmeter statistics */
211 PCPU_INC(cnt.v_trap);
212 /* Data abort came from user mode? */
213 user = TRAP_USERMODE(tf);
218 if (td->td_cowgen != td->td_proc->p_cowgen)
219 thread_cow_update(td);
222 /* Grab the current pcb */
224 /* Re-enable interrupts if they were enabled previously */
225 if (td->td_md.md_spinlock_count == 0) {
226 if (__predict_true(tf->tf_spsr & PSR_I) == 0)
227 enable_interrupts(PSR_I);
228 if (__predict_true(tf->tf_spsr & PSR_F) == 0)
229 enable_interrupts(PSR_F);
233 /* Invoke the appropriate handler, if necessary */
234 if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) {
235 if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far,
243 * At this point, we're dealing with one of the following data aborts:
245 * FAULT_TRANS_S - Translation -- Section
246 * FAULT_TRANS_P - Translation -- Page
247 * FAULT_DOMAIN_S - Domain -- Section
248 * FAULT_DOMAIN_P - Domain -- Page
249 * FAULT_PERM_S - Permission -- Section
250 * FAULT_PERM_P - Permission -- Page
252 * These are the main virtual memory-related faults signalled by
256 /* fusubailout is used by [fs]uswintr to avoid page faulting */
257 if (__predict_false(pcb->pcb_onfault == fusubailout)) {
259 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
264 * Make sure the Program Counter is sane. We could fall foul of
265 * someone executing Thumb code, in which case the PC might not
266 * be word-aligned. This would cause a kernel alignment fault
267 * further down if we have to decode the current instruction.
268 * XXX: It would be nice to be able to support Thumb at some point.
270 if (__predict_false((tf->tf_pc & 3) != 0)) {
273 * Give the user an illegal instruction signal.
275 /* Deliver a SIGILL to the process */
282 * The kernel never executes Thumb code.
284 printf("\ndata_abort_fault: Misaligned Kernel-mode "
285 "Program Counter\n");
286 dab_fatal(tf, fsr, far, td, &ksig);
289 va = trunc_page((vm_offset_t)far);
292 * It is only a kernel address space fault iff:
294 * 2. pcb_onfault not set or
295 * 3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction.
297 if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
298 (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) &&
299 __predict_true((pcb->pcb_onfault == NULL ||
300 (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) {
303 /* Was the fault due to the FPE/IPKDB ? */
304 if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
307 * Force exit via userret()
308 * This is necessary as the FPE is an extension to
309 * userland that actually runs in a priveledged mode
310 * but uses USR mode permissions for its accesses.
313 ksig.signb = SIGSEGV;
318 map = &td->td_proc->p_vmspace->vm_map;
322 * We need to know whether the page should be mapped as R or R/W. On
323 * armv6 and later the fault status register indicates whether the
324 * access was a read or write. Prior to armv6, we know that a
325 * permission fault can only be the result of a write to a read-only
326 * location, so we can deal with those quickly. Otherwise we need to
327 * disassemble the faulting instruction to determine if it was a write.
330 ftype = (fsr & FAULT_WNR) ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
332 if (IS_PERMISSION_FAULT(fsr))
333 ftype = VM_PROT_WRITE;
335 u_int insn = ReadWord(tf->tf_pc);
337 if (((insn & 0x0c100000) == 0x04000000) || /* STR/STRB */
338 ((insn & 0x0e1000b0) == 0x000000b0) || /* STRH/STRD */
339 ((insn & 0x0a100000) == 0x08000000)) { /* STM/CDT */
340 ftype = VM_PROT_WRITE;
342 if ((insn & 0x0fb00ff0) == 0x01000090) /* SWP */
343 ftype = VM_PROT_READ | VM_PROT_WRITE;
345 ftype = VM_PROT_READ;
351 * See if the fault is as a result of ref/mod emulation,
352 * or domain mismatch.
355 last_fault_code = fsr;
357 if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK,
358 NULL, "Kernel page fault") != 0)
359 goto fatal_pagefault;
361 if (pmap_fault_fixup(vmspace_pmap(td->td_proc->p_vmspace), va, ftype,
366 onfault = pcb->pcb_onfault;
367 pcb->pcb_onfault = NULL;
368 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
369 pcb->pcb_onfault = onfault;
370 if (__predict_true(error == 0))
374 if (pcb->pcb_onfault) {
376 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
380 printf("\nvm_fault(%p, %x, %x, 0) -> %x\n", map, va, ftype,
382 dab_fatal(tf, fsr, far, td, &ksig);
386 if (error == ENOMEM) {
387 printf("VM: pid %d (%s), uid %d killed: "
388 "out of swap\n", td->td_proc->p_pid, td->td_name,
389 (td->td_proc->p_ucred) ?
390 td->td_proc->p_ucred->cr_uid : -1);
391 ksig.signb = SIGKILL;
393 ksig.signb = SIGSEGV;
397 call_trapsignal(td, ksig.signb, ksig.code);
399 /* If returning to user mode, make sure to invoke userret() */
405 * dab_fatal() handles the following data aborts:
407 * FAULT_WRTBUF_0 - Vector Exception
408 * FAULT_WRTBUF_1 - Terminal Exception
410 * We should never see these on a properly functioning system.
412 * This function is also called by the other handlers if they
413 * detect a fatal problem.
415 * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
418 dab_fatal(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
424 if (!TRAP_USERMODE(tf)) {
425 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(tf, far & FAULT_TYPE_MASK))
430 mode = TRAP_USERMODE(tf) ? "user" : "kernel";
432 disable_interrupts(PSR_I|PSR_F);
434 printf("Fatal %s mode data abort: '%s'\n", mode,
435 data_aborts[fsr & FAULT_TYPE_MASK].desc);
436 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr);
437 if ((fsr & FAULT_IMPRECISE) == 0)
438 printf("%08x, ", far);
441 printf("spsr=%08x\n", tf->tf_spsr);
443 printf("Fatal %s mode prefetch abort at 0x%08x\n",
445 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr);
448 printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n",
449 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3);
450 printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n",
451 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
452 printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n",
453 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
454 printf("r12=%08x, ", tf->tf_r12);
456 if (TRAP_USERMODE(tf))
457 printf("usp=%08x, ulr=%08x",
458 tf->tf_usr_sp, tf->tf_usr_lr);
460 printf("ssp=%08x, slr=%08x",
461 tf->tf_svc_sp, tf->tf_svc_lr);
462 printf(", pc =%08x\n\n", tf->tf_pc);
465 if (debugger_on_panic || kdb_active)
466 if (kdb_trap(fsr, 0, tf))
469 panic("Fatal abort");
474 * dab_align() handles the following data aborts:
476 * FAULT_ALIGN_0 - Alignment fault
477 * FAULT_ALIGN_1 - Alignment fault
479 * These faults are fatal if they happen in kernel mode. Otherwise, we
480 * deliver a bus error to the process.
483 dab_align(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
487 /* Alignment faults are always fatal if they occur in kernel mode */
488 if (!TRAP_USERMODE(tf)) {
489 if (!td || !td->td_pcb->pcb_onfault)
490 dab_fatal(tf, fsr, far, td, ksig);
492 tf->tf_pc = (int)td->td_pcb->pcb_onfault;
496 /* pcb_onfault *must* be NULL at this point */
498 /* Deliver a bus error signal to the process */
500 ksig->signb = SIGBUS;
507 * dab_buserr() handles the following data aborts:
509 * FAULT_BUSERR_0 - External Abort on Linefetch -- Section
510 * FAULT_BUSERR_1 - External Abort on Linefetch -- Page
511 * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
512 * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
513 * FAULT_BUSTRNL1 - External abort on Translation -- Level 1
514 * FAULT_BUSTRNL2 - External abort on Translation -- Level 2
516 * If pcb_onfault is set, flag the fault and return to the handler.
517 * If the fault occurred in user mode, give the process a SIGBUS.
519 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
520 * can be flagged as imprecise in the FSR. This causes a real headache
521 * since some of the machine state is lost. In this case, tf->tf_pc
522 * may not actually point to the offending instruction. In fact, if
523 * we've taken a double abort fault, it generally points somewhere near
524 * the top of "data_abort_entry" in exception.S.
526 * In all other cases, these data aborts are considered fatal.
529 dab_buserr(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
532 struct pcb *pcb = td->td_pcb;
535 if ((fsr & FAULT_IMPRECISE) != 0 &&
536 (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
538 * Oops, an imprecise, double abort fault. We've lost the
539 * r14_abt/spsr_abt values corresponding to the original
540 * abort, and the spsr saved in the trapframe indicates
543 tf->tf_spsr &= ~PSR_MODE;
546 * We use a simple heuristic to determine if the double abort
547 * happened as a result of a kernel or user mode access.
548 * If the current trapframe is at the top of the kernel stack,
549 * the fault _must_ have come from user mode.
551 if (tf != ((struct trapframe *)pcb->pcb_regs.sf_sp) - 1) {
553 * Kernel mode. We're either about to die a
554 * spectacular death, or pcb_onfault will come
555 * to our rescue. Either way, the current value
556 * of tf->tf_pc is irrelevant.
558 tf->tf_spsr |= PSR_SVC32_MODE;
559 if (pcb->pcb_onfault == NULL)
560 printf("\nKernel mode double abort!\n");
563 * User mode. We've lost the program counter at the
564 * time of the fault (not that it was accurate anyway;
565 * it's not called an imprecise fault for nothing).
566 * About all we can do is copy r14_usr to tf_pc and
567 * hope for the best. The process is about to get a
568 * SIGBUS, so it's probably history anyway.
570 tf->tf_spsr |= PSR_USR32_MODE;
571 tf->tf_pc = tf->tf_usr_lr;
575 /* FAR is invalid for imprecise exceptions */
576 if ((fsr & FAULT_IMPRECISE) != 0)
578 #endif /* __XSCALE__ */
580 if (pcb->pcb_onfault) {
582 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
587 * At this point, if the fault happened in kernel mode, we're toast
589 if (!TRAP_USERMODE(tf))
590 dab_fatal(tf, fsr, far, td, ksig);
592 /* Deliver a bus error signal to the process */
593 ksig->signb = SIGBUS;
601 * void prefetch_abort_handler(struct trapframe *tf)
603 * Abort handler called when instruction execution occurs at
604 * a non existent or restricted (access permissions) memory page.
605 * If the address is invalid and we were in SVC mode then panic as
606 * the kernel should never prefetch abort.
607 * If the address is invalid and the page is mapped then the user process
608 * does no have read permission so send it a signal.
609 * Otherwise fault the page in and try again.
612 prefetch_abort_handler(struct trapframe *tf)
617 vm_offset_t fault_pc, va;
623 /* Update vmmeter statistics */
627 printf("prefetch abort handler: %p %p\n", (void*)tf->tf_pc,
628 (void*)tf->tf_usr_lr);
633 PCPU_INC(cnt.v_trap);
635 if (TRAP_USERMODE(tf)) {
637 if (td->td_cowgen != td->td_proc->p_cowgen)
638 thread_cow_update(td);
640 fault_pc = tf->tf_pc;
641 if (td->td_md.md_spinlock_count == 0) {
642 if (__predict_true(tf->tf_spsr & PSR_I) == 0)
643 enable_interrupts(PSR_I);
644 if (__predict_true(tf->tf_spsr & PSR_F) == 0)
645 enable_interrupts(PSR_F);
648 /* Prefetch aborts cannot happen in kernel mode */
649 if (__predict_false(!TRAP_USERMODE(tf)))
650 dab_fatal(tf, 0, tf->tf_pc, NULL, &ksig);
654 /* Ok validate the address, can only execute in USER space */
655 if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS ||
656 (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
657 ksig.signb = SIGSEGV;
662 map = &td->td_proc->p_vmspace->vm_map;
663 va = trunc_page(fault_pc);
666 * See if the pmap can handle this fault on its own...
669 last_fault_code = -1;
671 if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1))
674 error = vm_fault(map, va, VM_PROT_READ | VM_PROT_EXECUTE,
676 if (__predict_true(error == 0))
679 if (error == ENOMEM) {
680 printf("VM: pid %d (%s), uid %d killed: "
681 "out of swap\n", td->td_proc->p_pid, td->td_name,
682 (td->td_proc->p_ucred) ?
683 td->td_proc->p_ucred->cr_uid : -1);
684 ksig.signb = SIGKILL;
686 ksig.signb = SIGSEGV;
691 call_trapsignal(td, ksig.signb, ksig.code);
698 extern int badaddr_read_1(const uint8_t *, uint8_t *);
699 extern int badaddr_read_2(const uint16_t *, uint16_t *);
700 extern int badaddr_read_4(const uint32_t *, uint32_t *);
702 * Tentatively read an 8, 16, or 32-bit value from 'addr'.
703 * If the read succeeds, the value is written to 'rptr' and zero is returned.
704 * Else, return EFAULT.
707 badaddr_read(void *addr, size_t size, void *rptr)
716 cpu_drain_writebuf();
718 /* Read from the test address. */
720 case sizeof(uint8_t):
721 rv = badaddr_read_1(addr, &u.v1);
723 *(uint8_t *) rptr = u.v1;
726 case sizeof(uint16_t):
727 rv = badaddr_read_2(addr, &u.v2);
729 *(uint16_t *) rptr = u.v2;
732 case sizeof(uint32_t):
733 rv = badaddr_read_4(addr, &u.v4);
735 *(uint32_t *) rptr = u.v4;
739 panic("badaddr: invalid size (%lu)", (u_long) size);
742 /* Return EFAULT if the address was invalid, else zero */