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