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