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