No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / arm / arm32 / fault.c
blobf385da557729a8d8dc35640b14c09d588aaaefab
1 /* $NetBSD: fault.c,v 1.73 2009/11/21 20:32:18 rmind Exp $ */
3 /*
4 * Copyright 2003 Wasabi Systems, Inc.
5 * All rights reserved.
7 * Written by Steve C. Woodford for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 * Copyright (c) 1994-1997 Mark Brinicombe.
39 * Copyright (c) 1994 Brini.
40 * All rights reserved.
42 * This code is derived from software written for Brini by Mark Brinicombe
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by Brini.
55 * 4. The name of the company nor the name of the author may be used to
56 * endorse or promote products derived from this software without specific
57 * prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
60 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
61 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
63 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
64 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
65 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
71 * RiscBSD kernel project
73 * fault.c
75 * Fault handlers
77 * Created : 28/11/94
80 #include "opt_ddb.h"
81 #include "opt_kgdb.h"
82 #include "opt_sa.h"
84 #include <sys/types.h>
85 __KERNEL_RCSID(0, "$NetBSD: fault.c,v 1.73 2009/11/21 20:32:18 rmind Exp $");
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/proc.h>
90 #include <sys/kernel.h>
91 #include <sys/kauth.h>
93 #include <sys/savar.h>
94 #include <sys/cpu.h>
96 #include <uvm/uvm_extern.h>
97 #include <uvm/uvm_stat.h>
98 #ifdef UVMHIST
99 #include <uvm/uvm.h>
100 #endif
102 #include <arm/cpuconf.h>
104 #include <machine/frame.h>
105 #include <arm/arm32/katelib.h>
106 #include <machine/intr.h>
107 #if defined(DDB) || defined(KGDB)
108 #include <machine/db_machdep.h>
109 #ifdef KGDB
110 #include <sys/kgdb.h>
111 #endif
112 #if !defined(DDB)
113 #define kdb_trap kgdb_trap
114 #endif
115 #endif
117 #include <arch/arm/arm/disassem.h>
118 #include <arm/arm32/machdep.h>
120 extern char fusubailout[];
122 #ifdef DEBUG
123 int last_fault_code; /* For the benefit of pmap_fault_fixup() */
124 #endif
126 #if defined(CPU_ARM3) || defined(CPU_ARM6) || \
127 defined(CPU_ARM7) || defined(CPU_ARM7TDMI)
128 /* These CPUs may need data/prefetch abort fixups */
129 #define CPU_ABORT_FIXUP_REQUIRED
130 #endif
132 struct data_abort {
133 int (*func)(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
134 const char *desc;
137 static int dab_fatal(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
138 static int dab_align(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
139 static int dab_buserr(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
141 static const struct data_abort data_aborts[] = {
142 {dab_fatal, "Vector Exception"},
143 {dab_align, "Alignment Fault 1"},
144 {dab_fatal, "Terminal Exception"},
145 {dab_align, "Alignment Fault 3"},
146 {dab_buserr, "External Linefetch Abort (S)"},
147 {NULL, "Translation Fault (S)"},
148 {dab_buserr, "External Linefetch Abort (P)"},
149 {NULL, "Translation Fault (P)"},
150 {dab_buserr, "External Non-Linefetch Abort (S)"},
151 {NULL, "Domain Fault (S)"},
152 {dab_buserr, "External Non-Linefetch Abort (P)"},
153 {NULL, "Domain Fault (P)"},
154 {dab_buserr, "External Translation Abort (L1)"},
155 {NULL, "Permission Fault (S)"},
156 {dab_buserr, "External Translation Abort (L2)"},
157 {NULL, "Permission Fault (P)"}
160 /* Determine if a fault came from user mode */
161 #define TRAP_USERMODE(tf) ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
163 /* Determine if 'x' is a permission fault */
164 #define IS_PERMISSION_FAULT(x) \
165 (((1 << ((x) & FAULT_TYPE_MASK)) & \
166 ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0)
168 #if 0
169 /* maybe one day we'll do emulations */
170 #define TRAPSIGNAL(l,k) (*(l)->l_proc->p_emul->e_trapsignal)((l), (k))
171 #else
172 #define TRAPSIGNAL(l,k) trapsignal((l), (k))
173 #endif
175 static inline void
176 call_trapsignal(struct lwp *l, ksiginfo_t *ksi)
179 TRAPSIGNAL(l, ksi);
182 static inline int
183 data_abort_fixup(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l)
185 #ifdef CPU_ABORT_FIXUP_REQUIRED
186 int error;
188 /* Call the CPU specific data abort fixup routine */
189 error = cpu_dataabt_fixup(tf);
190 if (__predict_true(error != ABORT_FIXUP_FAILED))
191 return (error);
194 * Oops, couldn't fix up the instruction
196 printf("data_abort_fixup: fixup for %s mode data abort failed.\n",
197 TRAP_USERMODE(tf) ? "user" : "kernel");
198 #ifdef THUMB_CODE
199 if (tf->tf_spsr & PSR_T_bit) {
200 printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ",
201 tf->tf_pc, *((u_int16 *)(tf->tf_pc & ~1)),
202 *((u_int16 *)((tf->tf_pc + 2) & ~1)));
204 else
205 #endif
207 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
208 *((u_int *)tf->tf_pc));
210 disassemble(tf->tf_pc);
212 /* Die now if this happened in kernel mode */
213 if (!TRAP_USERMODE(tf))
214 dab_fatal(tf, fsr, far, l, NULL);
216 return (error);
217 #else
218 return (ABORT_FIXUP_OK);
219 #endif /* CPU_ABORT_FIXUP_REQUIRED */
222 void
223 data_abort_handler(trapframe_t *tf)
225 struct vm_map *map;
226 struct pcb *pcb;
227 struct lwp *l;
228 u_int user, far, fsr;
229 vm_prot_t ftype;
230 void *onfault;
231 vaddr_t va;
232 int error;
233 ksiginfo_t ksi;
235 UVMHIST_FUNC("data_abort_handler");
237 /* Grab FAR/FSR before enabling interrupts */
238 far = cpu_faultaddress();
239 fsr = cpu_faultstatus();
241 UVMHIST_CALLED(maphist);
242 /* Update vmmeter statistics */
243 uvmexp.traps++;
245 /* Re-enable interrupts if they were enabled previously */
246 KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
247 if (__predict_true((tf->tf_spsr & IF32_bits) != IF32_bits))
248 restore_interrupts(tf->tf_spsr & IF32_bits);
250 /* Get the current lwp structure */
251 KASSERT(curlwp != NULL);
252 l = curlwp;
254 UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, far=0x%x, fsr=0x%x)",
255 tf->tf_pc, l, far, fsr);
257 /* Data abort came from user mode? */
258 if ((user = TRAP_USERMODE(tf)) != 0)
259 LWP_CACHE_CREDS(l, l->l_proc);
261 /* Grab the current pcb */
262 pcb = lwp_getpcb(l);
264 /* Invoke the appropriate handler, if necessary */
265 if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) {
266 if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far,
267 l, &ksi))
268 goto do_trapsignal;
269 goto out;
273 * At this point, we're dealing with one of the following data aborts:
275 * FAULT_TRANS_S - Translation -- Section
276 * FAULT_TRANS_P - Translation -- Page
277 * FAULT_DOMAIN_S - Domain -- Section
278 * FAULT_DOMAIN_P - Domain -- Page
279 * FAULT_PERM_S - Permission -- Section
280 * FAULT_PERM_P - Permission -- Page
282 * These are the main virtual memory-related faults signalled by
283 * the MMU.
286 /* fusubailout is used by [fs]uswintr to avoid page faulting */
287 if (__predict_false(pcb->pcb_onfault == fusubailout)) {
288 tf->tf_r0 = EFAULT;
289 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
290 return;
293 if (user) {
294 pcb->pcb_tf = tf;
298 * Make sure the Program Counter is sane. We could fall foul of
299 * someone executing Thumb code, in which case the PC might not
300 * be word-aligned. This would cause a kernel alignment fault
301 * further down if we have to decode the current instruction.
303 #ifdef THUMB_CODE
305 * XXX: It would be nice to be able to support Thumb in the kernel
306 * at some point.
308 if (__predict_false(!user && (tf->tf_pc & 3) != 0)) {
309 printf("\ndata_abort_fault: Misaligned Kernel-mode "
310 "Program Counter\n");
311 dab_fatal(tf, fsr, far, l, NULL);
313 #else
314 if (__predict_false((tf->tf_pc & 3) != 0)) {
315 if (user) {
317 * Give the user an illegal instruction signal.
319 /* Deliver a SIGILL to the process */
320 KSI_INIT_TRAP(&ksi);
321 ksi.ksi_signo = SIGILL;
322 ksi.ksi_code = ILL_ILLOPC;
323 ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
324 ksi.ksi_trap = fsr;
325 goto do_trapsignal;
329 * The kernel never executes Thumb code.
331 printf("\ndata_abort_fault: Misaligned Kernel-mode "
332 "Program Counter\n");
333 dab_fatal(tf, fsr, far, l, NULL);
335 #endif
337 /* See if the CPU state needs to be fixed up */
338 switch (data_abort_fixup(tf, fsr, far, l)) {
339 case ABORT_FIXUP_RETURN:
340 return;
341 case ABORT_FIXUP_FAILED:
342 /* Deliver a SIGILL to the process */
343 KSI_INIT_TRAP(&ksi);
344 ksi.ksi_signo = SIGILL;
345 ksi.ksi_code = ILL_ILLOPC;
346 ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
347 ksi.ksi_trap = fsr;
348 goto do_trapsignal;
349 default:
350 break;
353 va = trunc_page((vaddr_t)far);
356 * It is only a kernel address space fault iff:
357 * 1. user == 0 and
358 * 2. pcb_onfault not set or
359 * 3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction.
361 if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
362 (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) &&
363 __predict_true((pcb->pcb_onfault == NULL ||
364 (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) {
365 map = kernel_map;
367 /* Was the fault due to the FPE/IPKDB ? */
368 if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
369 KSI_INIT_TRAP(&ksi);
370 ksi.ksi_signo = SIGSEGV;
371 ksi.ksi_code = SEGV_ACCERR;
372 ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
373 ksi.ksi_trap = fsr;
376 * Force exit via userret()
377 * This is necessary as the FPE is an extension to
378 * userland that actually runs in a priveledged mode
379 * but uses USR mode permissions for its accesses.
381 user = 1;
382 goto do_trapsignal;
384 } else {
385 map = &l->l_proc->p_vmspace->vm_map;
386 #ifdef KERN_SA
387 if ((l->l_flag & LW_SA) && (~l->l_pflag & LP_SA_NOBLOCK)) {
388 l->l_savp->savp_faultaddr = (vaddr_t)far;
389 l->l_pflag |= LP_SA_PAGEFAULT;
391 #endif
395 * We need to know whether the page should be mapped
396 * as R or R/W. The MMU does not give us the info as
397 * to whether the fault was caused by a read or a write.
399 * However, we know that a permission fault can only be
400 * the result of a write to a read-only location, so
401 * we can deal with those quickly.
403 * Otherwise we need to disassemble the instruction
404 * responsible to determine if it was a write.
406 if (IS_PERMISSION_FAULT(fsr))
407 ftype = VM_PROT_WRITE;
408 else {
409 #ifdef THUMB_CODE
410 /* Fast track the ARM case. */
411 if (__predict_false(tf->tf_spsr & PSR_T_bit)) {
412 u_int insn = fusword((void *)(tf->tf_pc & ~1));
413 u_int insn_f8 = insn & 0xf800;
414 u_int insn_fe = insn & 0xfe00;
416 if (insn_f8 == 0x6000 || /* STR(1) */
417 insn_f8 == 0x7000 || /* STRB(1) */
418 insn_f8 == 0x8000 || /* STRH(1) */
419 insn_f8 == 0x9000 || /* STR(3) */
420 insn_f8 == 0xc000 || /* STM */
421 insn_fe == 0x5000 || /* STR(2) */
422 insn_fe == 0x5200 || /* STRH(2) */
423 insn_fe == 0x5400) /* STRB(2) */
424 ftype = VM_PROT_WRITE;
425 else
426 ftype = VM_PROT_READ;
428 else
429 #endif
431 u_int insn = ReadWord(tf->tf_pc);
433 if (((insn & 0x0c100000) == 0x04000000) || /* STR[B] */
434 ((insn & 0x0e1000b0) == 0x000000b0) || /* STR[HD]*/
435 ((insn & 0x0a100000) == 0x08000000)) /* STM/CDT*/
436 ftype = VM_PROT_WRITE;
437 else if ((insn & 0x0fb00ff0) == 0x01000090)/* SWP */
438 ftype = VM_PROT_READ | VM_PROT_WRITE;
439 else
440 ftype = VM_PROT_READ;
445 * See if the fault is as a result of ref/mod emulation,
446 * or domain mismatch.
448 #ifdef DEBUG
449 last_fault_code = fsr;
450 #endif
451 if (pmap_fault_fixup(map->pmap, va, ftype, user)) {
452 #ifdef KERN_SA
453 if (map != kernel_map)
454 l->l_pflag &= ~LP_SA_PAGEFAULT;
455 #endif
456 UVMHIST_LOG(maphist, " <- ref/mod emul", 0, 0, 0, 0);
457 goto out;
460 if (__predict_false(curcpu()->ci_intr_depth > 0)) {
461 if (pcb->pcb_onfault) {
462 tf->tf_r0 = EINVAL;
463 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
464 return;
466 printf("\nNon-emulated page fault with intr_depth > 0\n");
467 dab_fatal(tf, fsr, far, l, NULL);
470 onfault = pcb->pcb_onfault;
471 pcb->pcb_onfault = NULL;
472 error = uvm_fault(map, va, ftype);
473 pcb->pcb_onfault = onfault;
475 #ifdef KERN_SA
476 if (map != kernel_map)
477 l->l_pflag &= ~LP_SA_PAGEFAULT;
478 #endif
480 if (__predict_true(error == 0)) {
481 if (user)
482 uvm_grow(l->l_proc, va); /* Record any stack growth */
483 UVMHIST_LOG(maphist, " <- uvm", 0, 0, 0, 0);
484 goto out;
487 if (user == 0) {
488 if (pcb->pcb_onfault) {
489 tf->tf_r0 = error;
490 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
491 return;
494 printf("\nuvm_fault(%p, %lx, %x) -> %x\n", map, va, ftype,
495 error);
496 dab_fatal(tf, fsr, far, l, NULL);
499 KSI_INIT_TRAP(&ksi);
501 if (error == ENOMEM) {
502 printf("UVM: pid %d (%s), uid %d killed: "
503 "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
504 l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1);
505 ksi.ksi_signo = SIGKILL;
506 } else
507 ksi.ksi_signo = SIGSEGV;
509 ksi.ksi_code = (error == EACCES) ? SEGV_ACCERR : SEGV_MAPERR;
510 ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
511 ksi.ksi_trap = fsr;
512 UVMHIST_LOG(maphist, " <- error (%d)", error, 0, 0, 0);
514 do_trapsignal:
515 call_trapsignal(l, &ksi);
516 out:
517 /* If returning to user mode, make sure to invoke userret() */
518 if (user)
519 userret(l);
523 * dab_fatal() handles the following data aborts:
525 * FAULT_WRTBUF_0 - Vector Exception
526 * FAULT_WRTBUF_1 - Terminal Exception
528 * We should never see these on a properly functioning system.
530 * This function is also called by the other handlers if they
531 * detect a fatal problem.
533 * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
535 static int
536 dab_fatal(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
538 const char *mode;
540 mode = TRAP_USERMODE(tf) ? "user" : "kernel";
542 if (l != NULL) {
543 printf("Fatal %s mode data abort: '%s'\n", mode,
544 data_aborts[fsr & FAULT_TYPE_MASK].desc);
545 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr);
546 if ((fsr & FAULT_IMPRECISE) == 0)
547 printf("%08x, ", far);
548 else
549 printf("Invalid, ");
550 printf("spsr=%08x\n", tf->tf_spsr);
551 } else {
552 printf("Fatal %s mode prefetch abort at 0x%08x\n",
553 mode, tf->tf_pc);
554 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr);
557 printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n",
558 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3);
559 printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n",
560 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
561 printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n",
562 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
563 printf("r12=%08x, ", tf->tf_r12);
565 if (TRAP_USERMODE(tf))
566 printf("usp=%08x, ulr=%08x",
567 tf->tf_usr_sp, tf->tf_usr_lr);
568 else
569 printf("ssp=%08x, slr=%08x",
570 tf->tf_svc_sp, tf->tf_svc_lr);
571 printf(", pc =%08x\n\n", tf->tf_pc);
573 #if defined(DDB) || defined(KGDB)
574 kdb_trap(T_FAULT, tf);
575 #endif
576 panic("Fatal abort");
577 /*NOTREACHED*/
581 * dab_align() handles the following data aborts:
583 * FAULT_ALIGN_0 - Alignment fault
584 * FAULT_ALIGN_0 - Alignment fault
586 * These faults are fatal if they happen in kernel mode. Otherwise, we
587 * deliver a bus error to the process.
589 static int
590 dab_align(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
592 struct pcb *pcb = lwp_getpcb(l);
594 /* Alignment faults are always fatal if they occur in kernel mode */
595 if (!TRAP_USERMODE(tf))
596 dab_fatal(tf, fsr, far, l, NULL);
598 /* pcb_onfault *must* be NULL at this point */
599 KDASSERT(pcb->pcb_onfault == NULL);
601 /* See if the CPU state needs to be fixed up */
602 (void) data_abort_fixup(tf, fsr, far, l);
604 /* Deliver a bus error signal to the process */
605 KSI_INIT_TRAP(ksi);
606 ksi->ksi_signo = SIGBUS;
607 ksi->ksi_code = BUS_ADRALN;
608 ksi->ksi_addr = (u_int32_t *)(intptr_t)far;
609 ksi->ksi_trap = fsr;
611 pcb->pcb_tf = tf;
613 return (1);
617 * dab_buserr() handles the following data aborts:
619 * FAULT_BUSERR_0 - External Abort on Linefetch -- Section
620 * FAULT_BUSERR_1 - External Abort on Linefetch -- Page
621 * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
622 * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
623 * FAULT_BUSTRNL1 - External abort on Translation -- Level 1
624 * FAULT_BUSTRNL2 - External abort on Translation -- Level 2
626 * If pcb_onfault is set, flag the fault and return to the handler.
627 * If the fault occurred in user mode, give the process a SIGBUS.
629 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
630 * can be flagged as imprecise in the FSR. This causes a real headache
631 * since some of the machine state is lost. In this case, tf->tf_pc
632 * may not actually point to the offending instruction. In fact, if
633 * we've taken a double abort fault, it generally points somewhere near
634 * the top of "data_abort_entry" in exception.S.
636 * In all other cases, these data aborts are considered fatal.
638 static int
639 dab_buserr(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l,
640 ksiginfo_t *ksi)
642 struct pcb *pcb = lwp_getpcb(l);
644 #ifdef __XSCALE__
645 if ((fsr & FAULT_IMPRECISE) != 0 &&
646 (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
648 * Oops, an imprecise, double abort fault. We've lost the
649 * r14_abt/spsr_abt values corresponding to the original
650 * abort, and the spsr saved in the trapframe indicates
651 * ABT mode.
653 tf->tf_spsr &= ~PSR_MODE;
656 * We use a simple heuristic to determine if the double abort
657 * happened as a result of a kernel or user mode access.
658 * If the current trapframe is at the top of the kernel stack,
659 * the fault _must_ have come from user mode.
661 if (tf != ((trapframe_t *)pcb->pcb_un.un_32.pcb32_sp) - 1) {
663 * Kernel mode. We're either about to die a
664 * spectacular death, or pcb_onfault will come
665 * to our rescue. Either way, the current value
666 * of tf->tf_pc is irrelevant.
668 tf->tf_spsr |= PSR_SVC32_MODE;
669 if (pcb->pcb_onfault == NULL)
670 printf("\nKernel mode double abort!\n");
671 } else {
673 * User mode. We've lost the program counter at the
674 * time of the fault (not that it was accurate anyway;
675 * it's not called an imprecise fault for nothing).
676 * About all we can do is copy r14_usr to tf_pc and
677 * hope for the best. The process is about to get a
678 * SIGBUS, so it's probably history anyway.
680 tf->tf_spsr |= PSR_USR32_MODE;
681 tf->tf_pc = tf->tf_usr_lr;
682 #ifdef THUMB_CODE
683 tf->tf_spsr &= ~PSR_T_bit;
684 if (tf->tf_usr_lr & 1)
685 tf->tf_spsr |= PSR_T_bit;
686 #endif
690 /* FAR is invalid for imprecise exceptions */
691 if ((fsr & FAULT_IMPRECISE) != 0)
692 far = 0;
693 #endif /* __XSCALE__ */
695 if (pcb->pcb_onfault) {
696 KDASSERT(TRAP_USERMODE(tf) == 0);
697 tf->tf_r0 = EFAULT;
698 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
699 return (0);
702 /* See if the CPU state needs to be fixed up */
703 (void) data_abort_fixup(tf, fsr, far, l);
706 * At this point, if the fault happened in kernel mode, we're toast
708 if (!TRAP_USERMODE(tf))
709 dab_fatal(tf, fsr, far, l, NULL);
711 /* Deliver a bus error signal to the process */
712 KSI_INIT_TRAP(ksi);
713 ksi->ksi_signo = SIGBUS;
714 ksi->ksi_code = BUS_ADRERR;
715 ksi->ksi_addr = (u_int32_t *)(intptr_t)far;
716 ksi->ksi_trap = fsr;
718 pcb->pcb_tf = tf;
720 return (1);
723 static inline int
724 prefetch_abort_fixup(trapframe_t *tf)
726 #ifdef CPU_ABORT_FIXUP_REQUIRED
727 int error;
729 /* Call the CPU specific prefetch abort fixup routine */
730 error = cpu_prefetchabt_fixup(tf);
731 if (__predict_true(error != ABORT_FIXUP_FAILED))
732 return (error);
735 * Oops, couldn't fix up the instruction
737 printf(
738 "prefetch_abort_fixup: fixup for %s mode prefetch abort failed.\n",
739 TRAP_USERMODE(tf) ? "user" : "kernel");
740 #ifdef THUMB_CODE
741 if (tf->tf_spsr & PSR_T_bit) {
742 printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ",
743 tf->tf_pc, *((u_int16 *)(tf->tf_pc & ~1),
744 *((u_int16 *)((tf->tf_pc + 2) & ~1));
746 else
747 #endif
749 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
750 *((u_int *)tf->tf_pc));
752 disassemble(tf->tf_pc);
754 /* Die now if this happened in kernel mode */
755 if (!TRAP_USERMODE(tf))
756 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
758 return (error);
759 #else
760 return (ABORT_FIXUP_OK);
761 #endif /* CPU_ABORT_FIXUP_REQUIRED */
765 * void prefetch_abort_handler(trapframe_t *tf)
767 * Abort handler called when instruction execution occurs at
768 * a non existent or restricted (access permissions) memory page.
769 * If the address is invalid and we were in SVC mode then panic as
770 * the kernel should never prefetch abort.
771 * If the address is invalid and the page is mapped then the user process
772 * does no have read permission so send it a signal.
773 * Otherwise fault the page in and try again.
775 void
776 prefetch_abort_handler(trapframe_t *tf)
778 struct lwp *l;
779 struct pcb *pcb;
780 struct vm_map *map;
781 vaddr_t fault_pc, va;
782 ksiginfo_t ksi;
783 int error, user;
785 UVMHIST_FUNC("prefetch_abort_handler"); UVMHIST_CALLED(maphist);
787 /* Update vmmeter statistics */
788 uvmexp.traps++;
790 l = curlwp;
791 pcb = lwp_getpcb(l);
793 if ((user = TRAP_USERMODE(tf)) != 0)
794 LWP_CACHE_CREDS(l, l->l_proc);
797 * Enable IRQ's (disabled by the abort) This always comes
798 * from user mode so we know interrupts were not disabled.
799 * But we check anyway.
801 KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
802 if (__predict_true((tf->tf_spsr & I32_bit) != IF32_bits))
803 restore_interrupts(tf->tf_spsr & IF32_bits);
805 /* See if the CPU state needs to be fixed up */
806 switch (prefetch_abort_fixup(tf)) {
807 case ABORT_FIXUP_RETURN:
808 KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
809 return;
810 case ABORT_FIXUP_FAILED:
811 /* Deliver a SIGILL to the process */
812 KSI_INIT_TRAP(&ksi);
813 ksi.ksi_signo = SIGILL;
814 ksi.ksi_code = ILL_ILLOPC;
815 ksi.ksi_addr = (u_int32_t *)(intptr_t) tf->tf_pc;
816 pcb->pcb_tf = tf;
817 goto do_trapsignal;
818 default:
819 break;
822 /* Prefetch aborts cannot happen in kernel mode */
823 if (__predict_false(!user))
824 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
826 /* Get fault address */
827 fault_pc = tf->tf_pc;
828 pcb->pcb_tf = tf;
829 UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, tf=0x%x)", fault_pc, l, tf,
832 /* Ok validate the address, can only execute in USER space */
833 if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS ||
834 (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
835 KSI_INIT_TRAP(&ksi);
836 ksi.ksi_signo = SIGSEGV;
837 ksi.ksi_code = SEGV_ACCERR;
838 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
839 ksi.ksi_trap = fault_pc;
840 goto do_trapsignal;
843 map = &l->l_proc->p_vmspace->vm_map;
844 va = trunc_page(fault_pc);
847 * See if the pmap can handle this fault on its own...
849 #ifdef DEBUG
850 last_fault_code = -1;
851 #endif
852 if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1)) {
853 UVMHIST_LOG (maphist, " <- emulated", 0, 0, 0, 0);
854 goto out;
857 #ifdef DIAGNOSTIC
858 if (__predict_false(l->l_cpu->ci_intr_depth > 0)) {
859 printf("\nNon-emulated prefetch abort with intr_depth > 0\n");
860 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
862 #endif
864 #ifdef KERN_SA
865 if (map != kernel_map && (l->l_flag & LW_SA)) {
866 l->l_savp->savp_faultaddr = fault_pc;
867 l->l_pflag |= LP_SA_PAGEFAULT;
869 #endif
871 error = uvm_fault(map, va, VM_PROT_READ);
873 #ifdef KERN_SA
874 if (map != kernel_map)
875 l->l_pflag &= ~LP_SA_PAGEFAULT;
876 #endif
878 if (__predict_true(error == 0)) {
879 UVMHIST_LOG (maphist, " <- uvm", 0, 0, 0, 0);
880 goto out;
882 KSI_INIT_TRAP(&ksi);
884 UVMHIST_LOG (maphist, " <- fatal (%d)", error, 0, 0, 0);
885 if (error == ENOMEM) {
886 printf("UVM: pid %d (%s), uid %d killed: "
887 "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
888 l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1);
889 ksi.ksi_signo = SIGKILL;
890 } else
891 ksi.ksi_signo = SIGSEGV;
893 ksi.ksi_code = SEGV_MAPERR;
894 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
895 ksi.ksi_trap = fault_pc;
897 do_trapsignal:
898 call_trapsignal(l, &ksi);
900 out:
901 KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
902 userret(l);
906 * Tentatively read an 8, 16, or 32-bit value from 'addr'.
907 * If the read succeeds, the value is written to 'rptr' and zero is returned.
908 * Else, return EFAULT.
911 badaddr_read(void *addr, size_t size, void *rptr)
913 extern int badaddr_read_1(const uint8_t *, uint8_t *);
914 extern int badaddr_read_2(const uint16_t *, uint16_t *);
915 extern int badaddr_read_4(const uint32_t *, uint32_t *);
916 union {
917 uint8_t v1;
918 uint16_t v2;
919 uint32_t v4;
920 } u;
921 struct pcb *curpcb_save;
922 int rv, s;
924 cpu_drain_writebuf();
927 * We might be called at interrupt time, so arrange to steal
928 * lwp0's PCB temporarily, if required, so that pcb_onfault
929 * handling works correctly.
931 s = splhigh();
932 if ((curpcb_save = curpcb) == NULL)
933 curpcb = lwp_getpcb(&lwp0);
935 /* Read from the test address. */
936 switch (size) {
937 case sizeof(uint8_t):
938 rv = badaddr_read_1(addr, &u.v1);
939 if (rv == 0 && rptr)
940 *(uint8_t *) rptr = u.v1;
941 break;
943 case sizeof(uint16_t):
944 rv = badaddr_read_2(addr, &u.v2);
945 if (rv == 0 && rptr)
946 *(uint16_t *) rptr = u.v2;
947 break;
949 case sizeof(uint32_t):
950 rv = badaddr_read_4(addr, &u.v4);
951 if (rv == 0 && rptr)
952 *(uint32_t *) rptr = u.v4;
953 break;
955 default:
956 curpcb = curpcb_save;
957 panic("badaddr: invalid size (%lu)", (u_long) size);
960 /* Restore curpcb */
961 curpcb = curpcb_save;
962 splx(s);
964 /* Return EFAULT if the address was invalid, else zero */
965 return (rv);