fault.c revision 1.59 1 /* $NetBSD: fault.c,v 1.59 2006/05/14 21:55:09 elad Exp $ */
2
3 /*
4 * Copyright 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Steve C. Woodford for Wasabi Systems, Inc.
8 *
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.
24 *
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.
36 */
37 /*
38 * Copyright (c) 1994-1997 Mark Brinicombe.
39 * Copyright (c) 1994 Brini.
40 * All rights reserved.
41 *
42 * This code is derived from software written for Brini by Mark Brinicombe
43 *
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.
58 *
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.
70 *
71 * RiscBSD kernel project
72 *
73 * fault.c
74 *
75 * Fault handlers
76 *
77 * Created : 28/11/94
78 */
79
80 #include "opt_ddb.h"
81 #include "opt_kgdb.h"
82
83 #include <sys/types.h>
84 __KERNEL_RCSID(0, "$NetBSD: fault.c,v 1.59 2006/05/14 21:55:09 elad Exp $");
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/proc.h>
89 #include <sys/savar.h>
90 #include <sys/user.h>
91 #include <sys/kernel.h>
92
93 #include <uvm/uvm_extern.h>
94 #include <uvm/uvm_stat.h>
95 #ifdef UVMHIST
96 #include <uvm/uvm.h>
97 #endif
98
99 #include <arm/cpuconf.h>
100
101 #include <machine/frame.h>
102 #include <arm/arm32/katelib.h>
103 #include <machine/cpu.h>
104 #include <machine/intr.h>
105 #if defined(DDB) || defined(KGDB)
106 #include <machine/db_machdep.h>
107 #ifdef KGDB
108 #include <sys/kgdb.h>
109 #endif
110 #if !defined(DDB)
111 #define kdb_trap kgdb_trap
112 #endif
113 #endif
114
115 #include <arch/arm/arm/disassem.h>
116 #include <arm/arm32/machdep.h>
117
118 extern char fusubailout[];
119
120 #ifdef DEBUG
121 int last_fault_code; /* For the benefit of pmap_fault_fixup() */
122 #endif
123
124 #if defined(CPU_ARM3) || defined(CPU_ARM6) || \
125 defined(CPU_ARM7) || defined(CPU_ARM7TDMI)
126 /* These CPUs may need data/prefetch abort fixups */
127 #define CPU_ABORT_FIXUP_REQUIRED
128 #endif
129
130 struct data_abort {
131 int (*func)(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
132 const char *desc;
133 };
134
135 static int dab_fatal(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
136 static int dab_align(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
137 static int dab_buserr(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
138
139 static const struct data_abort data_aborts[] = {
140 {dab_fatal, "Vector Exception"},
141 {dab_align, "Alignment Fault 1"},
142 {dab_fatal, "Terminal Exception"},
143 {dab_align, "Alignment Fault 3"},
144 {dab_buserr, "External Linefetch Abort (S)"},
145 {NULL, "Translation Fault (S)"},
146 {dab_buserr, "External Linefetch Abort (P)"},
147 {NULL, "Translation Fault (P)"},
148 {dab_buserr, "External Non-Linefetch Abort (S)"},
149 {NULL, "Domain Fault (S)"},
150 {dab_buserr, "External Non-Linefetch Abort (P)"},
151 {NULL, "Domain Fault (P)"},
152 {dab_buserr, "External Translation Abort (L1)"},
153 {NULL, "Permission Fault (S)"},
154 {dab_buserr, "External Translation Abort (L2)"},
155 {NULL, "Permission Fault (P)"}
156 };
157
158 /* Determine if a fault came from user mode */
159 #define TRAP_USERMODE(tf) ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
160
161 /* Determine if 'x' is a permission fault */
162 #define IS_PERMISSION_FAULT(x) \
163 (((1 << ((x) & FAULT_TYPE_MASK)) & \
164 ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0)
165
166 #if 0
167 /* maybe one day we'll do emulations */
168 #define TRAPSIGNAL(l,k) (*(l)->l_proc->p_emul->e_trapsignal)((l), (k))
169 #else
170 #define TRAPSIGNAL(l,k) trapsignal((l), (k))
171 #endif
172
173 static inline void
174 call_trapsignal(struct lwp *l, ksiginfo_t *ksi)
175 {
176
177 KERNEL_PROC_LOCK(l);
178 TRAPSIGNAL(l, ksi);
179 KERNEL_PROC_UNLOCK(l);
180 }
181
182 static inline int
183 data_abort_fixup(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l)
184 {
185 #ifdef CPU_ABORT_FIXUP_REQUIRED
186 int error;
187
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);
192
193 /*
194 * Oops, couldn't fix up the instruction
195 */
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));
203 }
204 else
205 #endif
206 {
207 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
208 *((u_int *)tf->tf_pc));
209 }
210 disassemble(tf->tf_pc);
211
212 /* Die now if this happened in kernel mode */
213 if (!TRAP_USERMODE(tf))
214 dab_fatal(tf, fsr, far, l, NULL);
215
216 return (error);
217 #else
218 return (ABORT_FIXUP_OK);
219 #endif /* CPU_ABORT_FIXUP_REQUIRED */
220 }
221
222 void
223 data_abort_handler(trapframe_t *tf)
224 {
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;
234
235 UVMHIST_FUNC("data_abort_handler");
236
237 /* Grab FAR/FSR before enabling interrupts */
238 far = cpu_faultaddress();
239 fsr = cpu_faultstatus();
240
241 UVMHIST_CALLED(maphist);
242 /* Update vmmeter statistics */
243 uvmexp.traps++;
244
245 /* Re-enable interrupts if they were enabled previously */
246 if (__predict_true((tf->tf_spsr & I32_bit) == 0))
247 enable_interrupts(I32_bit);
248
249 /* Get the current lwp structure or lwp0 if there is none */
250 l = (curlwp != NULL) ? curlwp : &lwp0;
251
252 UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, far=0x%x, fsr=0x%x)",
253 tf->tf_pc, l, far, fsr);
254
255 /* Data abort came from user mode? */
256 user = TRAP_USERMODE(tf);
257
258 /* Grab the current pcb */
259 pcb = &l->l_addr->u_pcb;
260
261 /* Invoke the appropriate handler, if necessary */
262 if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) {
263 if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far,
264 l, &ksi))
265 goto do_trapsignal;
266 goto out;
267 }
268
269 /*
270 * At this point, we're dealing with one of the following data aborts:
271 *
272 * FAULT_TRANS_S - Translation -- Section
273 * FAULT_TRANS_P - Translation -- Page
274 * FAULT_DOMAIN_S - Domain -- Section
275 * FAULT_DOMAIN_P - Domain -- Page
276 * FAULT_PERM_S - Permission -- Section
277 * FAULT_PERM_P - Permission -- Page
278 *
279 * These are the main virtual memory-related faults signalled by
280 * the MMU.
281 */
282
283 /* fusubailout is used by [fs]uswintr to avoid page faulting */
284 if (__predict_false(pcb->pcb_onfault == fusubailout)) {
285 tf->tf_r0 = EFAULT;
286 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
287 return;
288 }
289
290 if (user)
291 l->l_addr->u_pcb.pcb_tf = tf;
292
293 /*
294 * Make sure the Program Counter is sane. We could fall foul of
295 * someone executing Thumb code, in which case the PC might not
296 * be word-aligned. This would cause a kernel alignment fault
297 * further down if we have to decode the current instruction.
298 */
299 #ifdef THUMB_CODE
300 /*
301 * XXX: It would be nice to be able to support Thumb in the kernel
302 * at some point.
303 */
304 if (__predict_false(!user && (tf->tf_pc & 3) != 0)) {
305 printf("\ndata_abort_fault: Misaligned Kernel-mode "
306 "Program Counter\n");
307 dab_fatal(tf, fsr, far, l, NULL);
308 }
309 #else
310 if (__predict_false((tf->tf_pc & 3) != 0)) {
311 if (user) {
312 /*
313 * Give the user an illegal instruction signal.
314 */
315 /* Deliver a SIGILL to the process */
316 KSI_INIT_TRAP(&ksi);
317 ksi.ksi_signo = SIGILL;
318 ksi.ksi_code = ILL_ILLOPC;
319 ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
320 ksi.ksi_trap = fsr;
321 goto do_trapsignal;
322 }
323
324 /*
325 * The kernel never executes Thumb code.
326 */
327 printf("\ndata_abort_fault: Misaligned Kernel-mode "
328 "Program Counter\n");
329 dab_fatal(tf, fsr, far, l, NULL);
330 }
331 #endif
332
333 /* See if the CPU state needs to be fixed up */
334 switch (data_abort_fixup(tf, fsr, far, l)) {
335 case ABORT_FIXUP_RETURN:
336 return;
337 case ABORT_FIXUP_FAILED:
338 /* Deliver a SIGILL to the process */
339 KSI_INIT_TRAP(&ksi);
340 ksi.ksi_signo = SIGILL;
341 ksi.ksi_code = ILL_ILLOPC;
342 ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
343 ksi.ksi_trap = fsr;
344 goto do_trapsignal;
345 default:
346 break;
347 }
348
349 va = trunc_page((vaddr_t)far);
350
351 /*
352 * It is only a kernel address space fault iff:
353 * 1. user == 0 and
354 * 2. pcb_onfault not set or
355 * 3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction.
356 */
357 if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
358 (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) &&
359 __predict_true((pcb->pcb_onfault == NULL ||
360 (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) {
361 map = kernel_map;
362
363 /* Was the fault due to the FPE/IPKDB ? */
364 if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
365 KSI_INIT_TRAP(&ksi);
366 ksi.ksi_signo = SIGSEGV;
367 ksi.ksi_code = SEGV_ACCERR;
368 ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
369 ksi.ksi_trap = fsr;
370
371 /*
372 * Force exit via userret()
373 * This is necessary as the FPE is an extension to
374 * userland that actually runs in a priveledged mode
375 * but uses USR mode permissions for its accesses.
376 */
377 user = 1;
378 goto do_trapsignal;
379 }
380 } else {
381 map = &l->l_proc->p_vmspace->vm_map;
382 if (l->l_flag & L_SA) {
383 l->l_savp->savp_faultaddr = (vaddr_t)far;
384 l->l_flag |= L_SA_PAGEFAULT;
385 }
386 }
387
388 /*
389 * We need to know whether the page should be mapped
390 * as R or R/W. The MMU does not give us the info as
391 * to whether the fault was caused by a read or a write.
392 *
393 * However, we know that a permission fault can only be
394 * the result of a write to a read-only location, so
395 * we can deal with those quickly.
396 *
397 * Otherwise we need to disassemble the instruction
398 * responsible to determine if it was a write.
399 */
400 if (IS_PERMISSION_FAULT(fsr))
401 ftype = VM_PROT_WRITE;
402 else {
403 #ifdef THUMB_CODE
404 /* Fast track the ARM case. */
405 if (__predict_false(tf->tf_spsr & PSR_T_bit)) {
406 u_int insn = fusword((void *)(tf->tf_pc & ~1));
407 u_int insn_f8 = insn & 0xf800;
408 u_int insn_fe = insn & 0xfe00;
409
410 if (insn_f8 == 0x6000 || /* STR(1) */
411 insn_f8 == 0x7000 || /* STRB(1) */
412 insn_f8 == 0x8000 || /* STRH(1) */
413 insn_f8 == 0x9000 || /* STR(3) */
414 insn_f8 == 0xc000 || /* STM */
415 insn_fe == 0x5000 || /* STR(2) */
416 insn_fe == 0x5200 || /* STRH(2) */
417 insn_fe == 0x5400) /* STRB(2) */
418 ftype = VM_PROT_WRITE;
419 else
420 ftype = VM_PROT_READ;
421 }
422 else
423 #endif
424 {
425 u_int insn = ReadWord(tf->tf_pc);
426
427 if (((insn & 0x0c100000) == 0x04000000) || /* STR[B] */
428 ((insn & 0x0e1000b0) == 0x000000b0) || /* STR[HD]*/
429 ((insn & 0x0a100000) == 0x08000000)) /* STM/CDT*/
430 ftype = VM_PROT_WRITE;
431 else if ((insn & 0x0fb00ff0) == 0x01000090)/* SWP */
432 ftype = VM_PROT_READ | VM_PROT_WRITE;
433 else
434 ftype = VM_PROT_READ;
435 }
436 }
437
438 /*
439 * See if the fault is as a result of ref/mod emulation,
440 * or domain mismatch.
441 */
442 #ifdef DEBUG
443 last_fault_code = fsr;
444 #endif
445 if (pmap_fault_fixup(map->pmap, va, ftype, user)) {
446 if (map != kernel_map)
447 l->l_flag &= ~L_SA_PAGEFAULT;
448 UVMHIST_LOG(maphist, " <- ref/mod emul", 0, 0, 0, 0);
449 goto out;
450 }
451
452 if (__predict_false(current_intr_depth > 0)) {
453 if (pcb->pcb_onfault) {
454 tf->tf_r0 = EINVAL;
455 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
456 return;
457 }
458 printf("\nNon-emulated page fault with intr_depth > 0\n");
459 dab_fatal(tf, fsr, far, l, NULL);
460 }
461
462 onfault = pcb->pcb_onfault;
463 pcb->pcb_onfault = NULL;
464 error = uvm_fault(map, va, ftype);
465 pcb->pcb_onfault = onfault;
466
467 if (map != kernel_map)
468 l->l_flag &= ~L_SA_PAGEFAULT;
469
470 if (__predict_true(error == 0)) {
471 if (user)
472 uvm_grow(l->l_proc, va); /* Record any stack growth */
473 UVMHIST_LOG(maphist, " <- uvm", 0, 0, 0, 0);
474 goto out;
475 }
476
477 if (user == 0) {
478 if (pcb->pcb_onfault) {
479 tf->tf_r0 = error;
480 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
481 return;
482 }
483
484 printf("\nuvm_fault(%p, %lx, %x) -> %x\n", map, va, ftype,
485 error);
486 dab_fatal(tf, fsr, far, l, NULL);
487 }
488
489 KSI_INIT_TRAP(&ksi);
490
491 if (error == ENOMEM) {
492 printf("UVM: pid %d (%s), uid %d killed: "
493 "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
494 l->l_proc->p_cred ? kauth_cred_geteuid(l->l_proc->p_cred) : -1);
495 ksi.ksi_signo = SIGKILL;
496 } else
497 ksi.ksi_signo = SIGSEGV;
498
499 ksi.ksi_code = (error == EACCES) ? SEGV_ACCERR : SEGV_MAPERR;
500 ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
501 ksi.ksi_trap = fsr;
502 UVMHIST_LOG(maphist, " <- erorr (%d)", error, 0, 0, 0);
503
504 do_trapsignal:
505 call_trapsignal(l, &ksi);
506 out:
507 /* If returning to user mode, make sure to invoke userret() */
508 if (user)
509 userret(l);
510 }
511
512 /*
513 * dab_fatal() handles the following data aborts:
514 *
515 * FAULT_WRTBUF_0 - Vector Exception
516 * FAULT_WRTBUF_1 - Terminal Exception
517 *
518 * We should never see these on a properly functioning system.
519 *
520 * This function is also called by the other handlers if they
521 * detect a fatal problem.
522 *
523 * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
524 */
525 static int
526 dab_fatal(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
527 {
528 const char *mode;
529
530 mode = TRAP_USERMODE(tf) ? "user" : "kernel";
531
532 if (l != NULL) {
533 printf("Fatal %s mode data abort: '%s'\n", mode,
534 data_aborts[fsr & FAULT_TYPE_MASK].desc);
535 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr);
536 if ((fsr & FAULT_IMPRECISE) == 0)
537 printf("%08x, ", far);
538 else
539 printf("Invalid, ");
540 printf("spsr=%08x\n", tf->tf_spsr);
541 } else {
542 printf("Fatal %s mode prefetch abort at 0x%08x\n",
543 mode, tf->tf_pc);
544 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr);
545 }
546
547 printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n",
548 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3);
549 printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n",
550 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
551 printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n",
552 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
553 printf("r12=%08x, ", tf->tf_r12);
554
555 if (TRAP_USERMODE(tf))
556 printf("usp=%08x, ulr=%08x",
557 tf->tf_usr_sp, tf->tf_usr_lr);
558 else
559 printf("ssp=%08x, slr=%08x",
560 tf->tf_svc_sp, tf->tf_svc_lr);
561 printf(", pc =%08x\n\n", tf->tf_pc);
562
563 #if defined(DDB) || defined(KGDB)
564 kdb_trap(T_FAULT, tf);
565 #endif
566 panic("Fatal abort");
567 /*NOTREACHED*/
568 }
569
570 /*
571 * dab_align() handles the following data aborts:
572 *
573 * FAULT_ALIGN_0 - Alignment fault
574 * FAULT_ALIGN_0 - Alignment fault
575 *
576 * These faults are fatal if they happen in kernel mode. Otherwise, we
577 * deliver a bus error to the process.
578 */
579 static int
580 dab_align(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
581 {
582
583 /* Alignment faults are always fatal if they occur in kernel mode */
584 if (!TRAP_USERMODE(tf))
585 dab_fatal(tf, fsr, far, l, NULL);
586
587 /* pcb_onfault *must* be NULL at this point */
588 KDASSERT(l->l_addr->u_pcb.pcb_onfault == NULL);
589
590 /* See if the CPU state needs to be fixed up */
591 (void) data_abort_fixup(tf, fsr, far, l);
592
593 /* Deliver a bus error signal to the process */
594 KSI_INIT_TRAP(ksi);
595 ksi->ksi_signo = SIGBUS;
596 ksi->ksi_code = BUS_ADRALN;
597 ksi->ksi_addr = (u_int32_t *)(intptr_t)far;
598 ksi->ksi_trap = fsr;
599
600 l->l_addr->u_pcb.pcb_tf = tf;
601
602 return (1);
603 }
604
605 /*
606 * dab_buserr() handles the following data aborts:
607 *
608 * FAULT_BUSERR_0 - External Abort on Linefetch -- Section
609 * FAULT_BUSERR_1 - External Abort on Linefetch -- Page
610 * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
611 * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
612 * FAULT_BUSTRNL1 - External abort on Translation -- Level 1
613 * FAULT_BUSTRNL2 - External abort on Translation -- Level 2
614 *
615 * If pcb_onfault is set, flag the fault and return to the handler.
616 * If the fault occurred in user mode, give the process a SIGBUS.
617 *
618 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
619 * can be flagged as imprecise in the FSR. This causes a real headache
620 * since some of the machine state is lost. In this case, tf->tf_pc
621 * may not actually point to the offending instruction. In fact, if
622 * we've taken a double abort fault, it generally points somewhere near
623 * the top of "data_abort_entry" in exception.S.
624 *
625 * In all other cases, these data aborts are considered fatal.
626 */
627 static int
628 dab_buserr(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l,
629 ksiginfo_t *ksi)
630 {
631 struct pcb *pcb = &l->l_addr->u_pcb;
632
633 #ifdef __XSCALE__
634 if ((fsr & FAULT_IMPRECISE) != 0 &&
635 (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
636 /*
637 * Oops, an imprecise, double abort fault. We've lost the
638 * r14_abt/spsr_abt values corresponding to the original
639 * abort, and the spsr saved in the trapframe indicates
640 * ABT mode.
641 */
642 tf->tf_spsr &= ~PSR_MODE;
643
644 /*
645 * We use a simple heuristic to determine if the double abort
646 * happened as a result of a kernel or user mode access.
647 * If the current trapframe is at the top of the kernel stack,
648 * the fault _must_ have come from user mode.
649 */
650 if (tf != ((trapframe_t *)pcb->pcb_un.un_32.pcb32_sp) - 1) {
651 /*
652 * Kernel mode. We're either about to die a
653 * spectacular death, or pcb_onfault will come
654 * to our rescue. Either way, the current value
655 * of tf->tf_pc is irrelevant.
656 */
657 tf->tf_spsr |= PSR_SVC32_MODE;
658 if (pcb->pcb_onfault == NULL)
659 printf("\nKernel mode double abort!\n");
660 } else {
661 /*
662 * User mode. We've lost the program counter at the
663 * time of the fault (not that it was accurate anyway;
664 * it's not called an imprecise fault for nothing).
665 * About all we can do is copy r14_usr to tf_pc and
666 * hope for the best. The process is about to get a
667 * SIGBUS, so it's probably history anyway.
668 */
669 tf->tf_spsr |= PSR_USR32_MODE;
670 tf->tf_pc = tf->tf_usr_lr;
671 #ifdef THUMB_CODE
672 tf->tf_spsr &= ~PSR_T_bit;
673 if (tf->tf_usr_lr & 1)
674 tf->tf_spsr |= PSR_T_bit;
675 #endif
676 }
677 }
678
679 /* FAR is invalid for imprecise exceptions */
680 if ((fsr & FAULT_IMPRECISE) != 0)
681 far = 0;
682 #endif /* __XSCALE__ */
683
684 if (pcb->pcb_onfault) {
685 KDASSERT(TRAP_USERMODE(tf) == 0);
686 tf->tf_r0 = EFAULT;
687 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
688 return (0);
689 }
690
691 /* See if the CPU state needs to be fixed up */
692 (void) data_abort_fixup(tf, fsr, far, l);
693
694 /*
695 * At this point, if the fault happened in kernel mode, we're toast
696 */
697 if (!TRAP_USERMODE(tf))
698 dab_fatal(tf, fsr, far, l, NULL);
699
700 /* Deliver a bus error signal to the process */
701 KSI_INIT_TRAP(ksi);
702 ksi->ksi_signo = SIGBUS;
703 ksi->ksi_code = BUS_ADRERR;
704 ksi->ksi_addr = (u_int32_t *)(intptr_t)far;
705 ksi->ksi_trap = fsr;
706
707 l->l_addr->u_pcb.pcb_tf = tf;
708
709 return (1);
710 }
711
712 static inline int
713 prefetch_abort_fixup(trapframe_t *tf)
714 {
715 #ifdef CPU_ABORT_FIXUP_REQUIRED
716 int error;
717
718 /* Call the CPU specific prefetch abort fixup routine */
719 error = cpu_prefetchabt_fixup(tf);
720 if (__predict_true(error != ABORT_FIXUP_FAILED))
721 return (error);
722
723 /*
724 * Oops, couldn't fix up the instruction
725 */
726 printf(
727 "prefetch_abort_fixup: fixup for %s mode prefetch abort failed.\n",
728 TRAP_USERMODE(tf) ? "user" : "kernel");
729 #ifdef THUMB_CODE
730 if (tf->tf_spsr & PSR_T_bit) {
731 printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ",
732 tf->tf_pc, *((u_int16 *)(tf->tf_pc & ~1),
733 *((u_int16 *)((tf->tf_pc + 2) & ~1));
734 }
735 else
736 #endif
737 {
738 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
739 *((u_int *)tf->tf_pc));
740 }
741 disassemble(tf->tf_pc);
742
743 /* Die now if this happened in kernel mode */
744 if (!TRAP_USERMODE(tf))
745 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
746
747 return (error);
748 #else
749 return (ABORT_FIXUP_OK);
750 #endif /* CPU_ABORT_FIXUP_REQUIRED */
751 }
752
753 /*
754 * void prefetch_abort_handler(trapframe_t *tf)
755 *
756 * Abort handler called when instruction execution occurs at
757 * a non existent or restricted (access permissions) memory page.
758 * If the address is invalid and we were in SVC mode then panic as
759 * the kernel should never prefetch abort.
760 * If the address is invalid and the page is mapped then the user process
761 * does no have read permission so send it a signal.
762 * Otherwise fault the page in and try again.
763 */
764 void
765 prefetch_abort_handler(trapframe_t *tf)
766 {
767 struct lwp *l;
768 struct vm_map *map;
769 vaddr_t fault_pc, va;
770 ksiginfo_t ksi;
771 int error;
772
773 UVMHIST_FUNC("prefetch_abort_handler"); UVMHIST_CALLED(maphist);
774
775 /* Update vmmeter statistics */
776 uvmexp.traps++;
777
778 /*
779 * Enable IRQ's (disabled by the abort) This always comes
780 * from user mode so we know interrupts were not disabled.
781 * But we check anyway.
782 */
783 if (__predict_true((tf->tf_spsr & I32_bit) == 0))
784 enable_interrupts(I32_bit);
785
786 /* See if the CPU state needs to be fixed up */
787 switch (prefetch_abort_fixup(tf)) {
788 case ABORT_FIXUP_RETURN:
789 return;
790 case ABORT_FIXUP_FAILED:
791 /* Deliver a SIGILL to the process */
792 KSI_INIT_TRAP(&ksi);
793 ksi.ksi_signo = SIGILL;
794 ksi.ksi_code = ILL_ILLOPC;
795 ksi.ksi_addr = (u_int32_t *)(intptr_t) tf->tf_pc;
796 l = curlwp;
797 l->l_addr->u_pcb.pcb_tf = tf;
798 goto do_trapsignal;
799 default:
800 break;
801 }
802
803 /* Prefetch aborts cannot happen in kernel mode */
804 if (__predict_false(!TRAP_USERMODE(tf)))
805 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
806
807 /* Get fault address */
808 fault_pc = tf->tf_pc;
809 l = curlwp;
810 l->l_addr->u_pcb.pcb_tf = tf;
811 UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, tf=0x%x)", fault_pc, l, tf,
812 0);
813
814 /* Ok validate the address, can only execute in USER space */
815 if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS ||
816 (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
817 KSI_INIT_TRAP(&ksi);
818 ksi.ksi_signo = SIGSEGV;
819 ksi.ksi_code = SEGV_ACCERR;
820 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
821 ksi.ksi_trap = fault_pc;
822 goto do_trapsignal;
823 }
824
825 map = &l->l_proc->p_vmspace->vm_map;
826 va = trunc_page(fault_pc);
827
828 /*
829 * See if the pmap can handle this fault on its own...
830 */
831 #ifdef DEBUG
832 last_fault_code = -1;
833 #endif
834 if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1)) {
835 UVMHIST_LOG (maphist, " <- emulated", 0, 0, 0, 0);
836 goto out;
837 }
838
839 #ifdef DIAGNOSTIC
840 if (__predict_false(current_intr_depth > 0)) {
841 printf("\nNon-emulated prefetch abort with intr_depth > 0\n");
842 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
843 }
844 #endif
845 if (map != kernel_map && l->l_flag & L_SA) {
846 l->l_savp->savp_faultaddr = fault_pc;
847 l->l_flag |= L_SA_PAGEFAULT;
848 }
849
850 error = uvm_fault(map, va, VM_PROT_READ);
851
852 if (map != kernel_map)
853 l->l_flag &= ~L_SA_PAGEFAULT;
854
855 if (__predict_true(error == 0)) {
856 UVMHIST_LOG (maphist, " <- uvm", 0, 0, 0, 0);
857 goto out;
858 }
859 KSI_INIT_TRAP(&ksi);
860
861 UVMHIST_LOG (maphist, " <- fatal (%d)", error, 0, 0, 0);
862 if (error == ENOMEM) {
863 printf("UVM: pid %d (%s), uid %d killed: "
864 "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
865 l->l_proc->p_cred ? kauth_cred_geteuid(l->l_proc->p_cred) : -1);
866 ksi.ksi_signo = SIGKILL;
867 } else
868 ksi.ksi_signo = SIGSEGV;
869
870 ksi.ksi_code = SEGV_MAPERR;
871 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
872 ksi.ksi_trap = fault_pc;
873
874 do_trapsignal:
875 call_trapsignal(l, &ksi);
876
877 out:
878 userret(l);
879 }
880
881 /*
882 * Tentatively read an 8, 16, or 32-bit value from 'addr'.
883 * If the read succeeds, the value is written to 'rptr' and zero is returned.
884 * Else, return EFAULT.
885 */
886 int
887 badaddr_read(void *addr, size_t size, void *rptr)
888 {
889 extern int badaddr_read_1(const uint8_t *, uint8_t *);
890 extern int badaddr_read_2(const uint16_t *, uint16_t *);
891 extern int badaddr_read_4(const uint32_t *, uint32_t *);
892 union {
893 uint8_t v1;
894 uint16_t v2;
895 uint32_t v4;
896 } u;
897 struct pcb *curpcb_save;
898 int rv, s;
899
900 cpu_drain_writebuf();
901
902 /*
903 * We might be called at interrupt time, so arrange to steal
904 * lwp0's PCB temporarily, if required, so that pcb_onfault
905 * handling works correctly.
906 */
907 s = splhigh();
908 if ((curpcb_save = curpcb) == NULL)
909 curpcb = &lwp0.l_addr->u_pcb;
910
911 /* Read from the test address. */
912 switch (size) {
913 case sizeof(uint8_t):
914 rv = badaddr_read_1(addr, &u.v1);
915 if (rv == 0 && rptr)
916 *(uint8_t *) rptr = u.v1;
917 break;
918
919 case sizeof(uint16_t):
920 rv = badaddr_read_2(addr, &u.v2);
921 if (rv == 0 && rptr)
922 *(uint16_t *) rptr = u.v2;
923 break;
924
925 case sizeof(uint32_t):
926 rv = badaddr_read_4(addr, &u.v4);
927 if (rv == 0 && rptr)
928 *(uint32_t *) rptr = u.v4;
929 break;
930
931 default:
932 curpcb = curpcb_save;
933 panic("badaddr: invalid size (%lu)", (u_long) size);
934 }
935
936 /* Restore curpcb */
937 curpcb = curpcb_save;
938 splx(s);
939
940 /* Return EFAULT if the address was invalid, else zero */
941 return (rv);
942 }
943