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