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