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