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