Home | History | Annotate | Line # | Download | only in arm32
fault.c revision 1.39
      1 /*	$NetBSD: fault.c,v 1.39 2003/10/31 16:30:15 scw 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.39 2003/10/31 16:30:15 scw 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_false((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 	/* See if the cpu state needs to be fixed up */
    274 	switch (data_abort_fixup(tf, fsr, far, l)) {
    275 	case ABORT_FIXUP_RETURN:
    276 		return;
    277 	case ABORT_FIXUP_FAILED:
    278 		/* Deliver a SIGILL to the process */
    279 		KSI_INIT_TRAP(&ksi);
    280 		ksi.ksi_signo = SIGILL;
    281 		ksi.ksi_code = ILL_ILLOPC;
    282 		ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
    283 		ksi.ksi_trap = fsr;
    284 		goto do_trapsignal;
    285 	default:
    286 		break;
    287 	}
    288 
    289 	va = trunc_page((vaddr_t)far);
    290 
    291 	/*
    292 	 * It is only a kernel address space fault iff:
    293 	 *	1. user == 0  and
    294 	 *	2. pcb_onfault not set or
    295 	 *	3. pcb_onfault set but supervisor space fault
    296 	 * The last can occur during an exec() copyin where the
    297 	 * argument space is lazy-allocated.
    298 	 */
    299 	if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
    300 	    (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
    301 		map = kernel_map;
    302 
    303 		/* Was the fault due to the FPE/IPKDB ? */
    304 		if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
    305 			KSI_INIT_TRAP(&ksi);
    306 			ksi.ksi_signo = SIGSEGV;
    307 			ksi.ksi_code = SEGV_ACCERR;
    308 			ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
    309 			ksi.ksi_trap = fsr;
    310 
    311 			/*
    312 			 * Force exit via userret()
    313 			 * This is necessary as the FPE is an extension to
    314 			 * userland that actually runs in a priveledged mode
    315 			 * but uses USR mode permissions for its accesses.
    316 			 */
    317 			user = 1;
    318 			goto do_trapsignal;
    319 		}
    320 	} else {
    321 		map = &l->l_proc->p_vmspace->vm_map;
    322 		if (l->l_flag & L_SA) {
    323 			KDASSERT(l->l_proc->p_sa != NULL);
    324 			l->l_proc->p_sa->sa_vp_faultaddr = (vaddr_t)far;
    325 			l->l_flag |= L_SA_PAGEFAULT;
    326 		}
    327 	}
    328 
    329 	/*
    330 	 * We need to know whether the page should be mapped
    331 	 * as R or R/W. The MMU does not give us the info as
    332 	 * to whether the fault was caused by a read or a write.
    333 	 *
    334 	 * However, we know that a permission fault can only be
    335 	 * the result of a write to a read-only location, so
    336 	 * we can deal with those quickly.
    337 	 *
    338 	 * Otherwise we need to disassemble the instruction
    339 	 * responsible to determine if it was a write.
    340 	 */
    341 	if (IS_PERMISSION_FAULT(fsr))
    342 		ftype = VM_PROT_WRITE;
    343 	else {
    344 		u_int insn = ReadWord(tf->tf_pc);
    345 
    346 		if (((insn & 0x0c100000) == 0x04000000) ||	/* STR/STRB */
    347 		    ((insn & 0x0e1000b0) == 0x000000b0) ||	/* STRH/STRD */
    348 		    ((insn & 0x0a100000) == 0x08000000))	/* STM/CDT */
    349 			ftype = VM_PROT_WRITE;
    350 		else
    351 		if ((insn & 0x0fb00ff0) == 0x01000090)		/* SWP */
    352 			ftype = VM_PROT_READ | VM_PROT_WRITE;
    353 		else
    354 			ftype = VM_PROT_READ;
    355 	}
    356 
    357 	/*
    358 	 * See if the fault is as a result of ref/mod emulation,
    359 	 * or domain mismatch.
    360 	 */
    361 #ifdef DEBUG
    362 	last_fault_code = fsr;
    363 #endif
    364 	if (pmap_fault_fixup(map->pmap, va, ftype, user))
    365 		goto out;
    366 
    367 #ifdef DIAGNOSTIC
    368 	if (__predict_false(current_intr_depth > 0)) {
    369 		printf("\nNon-emulated page fault with intr_depth > 0\n");
    370 		dab_fatal(tf, fsr, far, l, NULL);
    371 	}
    372 #endif
    373 
    374 	onfault = pcb->pcb_onfault;
    375 	pcb->pcb_onfault = NULL;
    376 	error = uvm_fault(map, va, 0, ftype);
    377 	pcb->pcb_onfault = onfault;
    378 
    379 	if (map != kernel_map)
    380 		l->l_flag &= ~L_SA_PAGEFAULT;
    381 
    382 	if (__predict_true(error == 0)) {
    383 		if (user)
    384 			uvm_grow(l->l_proc, va); /* Record any stack growth */
    385 		goto out;
    386 	}
    387 
    388 	if (user == 0) {
    389 		if (pcb->pcb_onfault) {
    390 			tf->tf_r0 = error;
    391 			tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
    392 			return;
    393 		}
    394 
    395 		printf("\nuvm_fault(%p, %lx, %x, 0) -> %x\n", map, va, ftype,
    396 		    error);
    397 		dab_fatal(tf, fsr, far, l, NULL);
    398 	}
    399 
    400 	if (error == ENOMEM) {
    401 		printf("UVM: pid %d (%s), uid %d killed: "
    402 		    "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
    403 		    (l->l_proc->p_cred && l->l_proc->p_ucred) ?
    404 		     l->l_proc->p_ucred->cr_uid : -1);
    405 	}
    406 
    407 	KSI_INIT_TRAP(&ksi);
    408 	ksi.ksi_signo = SIGSEGV;
    409 	ksi.ksi_code = (error == EACCES) ? SEGV_ACCERR : SEGV_MAPERR;
    410 	ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
    411 	ksi.ksi_trap = fsr;
    412 	ksi.ksi_errno = error;
    413 
    414 do_trapsignal:
    415 	call_trapsignal(l, &ksi);
    416 out:
    417 	/* If returning to user mode, make sure to invoke userret() */
    418 	if (user)
    419 		userret(l);
    420 }
    421 
    422 /*
    423  * dab_fatal() handles the following data aborts:
    424  *
    425  *  FAULT_WRTBUF_0 - Vector Exception
    426  *  FAULT_WRTBUF_1 - Terminal Exception
    427  *
    428  * We should never see these on a properly functioning system.
    429  *
    430  * This function is also called by the other handlers if they
    431  * detect a fatal problem.
    432  *
    433  * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
    434  */
    435 static int
    436 dab_fatal(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
    437 {
    438 	const char *mode;
    439 
    440 	mode = TRAP_USERMODE(tf) ? "user" : "kernel";
    441 
    442 	if (l != NULL) {
    443 		printf("Fatal %s mode data abort: '%s'\n", mode,
    444 		    data_aborts[fsr & FAULT_TYPE_MASK].desc);
    445 		printf("\ttrapframe: %p, PC: 0x%08x, ", tf, tf->tf_pc);
    446 		if ((fsr & FAULT_IMPRECISE) == 0)
    447 			printf("FSR: 0x%x, FAR: 0x%08x\n", fsr, far);
    448 		else
    449 			printf("Imprecise fault. FSR/FAR invalid\n");
    450 	} else {
    451 		printf("Fatal %s mode prefetch abort\n", mode);
    452 		printf("\ttrapframe: %p, PC: 0x%08x\n", tf, tf->tf_pc);
    453 	}
    454 
    455 #if defined(DDB) || defined(KGDB)
    456 	kdb_trap(T_FAULT, tf);
    457 #endif
    458 	panic("Fatal abort");
    459 	/*NOTREACHED*/
    460 }
    461 
    462 /*
    463  * dab_align() handles the following data aborts:
    464  *
    465  *  FAULT_ALIGN_0 - Alignment fault
    466  *  FAULT_ALIGN_0 - Alignment fault
    467  *
    468  * These faults are fatal if they happen in kernel mode. Otherwise, we
    469  * deliver a bus error to the process.
    470  */
    471 static int
    472 dab_align(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
    473 {
    474 
    475 	/* Alignment faults are always fatal if they occur in kernel mode */
    476 	if (!TRAP_USERMODE(tf))
    477 		dab_fatal(tf, fsr, far, l, NULL);
    478 
    479 	/* pcb_onfault *must* be NULL at this point */
    480 	KDASSERT(l->l_addr->u_pcb.pcb_onfault == NULL);
    481 
    482 	/* See if the cpu state needs to be fixed up */
    483 	(void) data_abort_fixup(tf, fsr, far, l);
    484 
    485 	/* Deliver a bus error signal to the process */
    486 	KSI_INIT_TRAP(ksi);
    487 	ksi->ksi_signo = SIGBUS;
    488 	ksi->ksi_code = BUS_ADRALN;
    489 	ksi->ksi_addr = (u_int32_t *)(intptr_t)far;
    490 	ksi->ksi_trap = fsr;
    491 
    492 	l->l_addr->u_pcb.pcb_tf = tf;
    493 
    494 	return (1);
    495 }
    496 
    497 /*
    498  * dab_buserr() handles the following data aborts:
    499  *
    500  *  FAULT_BUSERR_0 - External Abort on Linefetch -- Section
    501  *  FAULT_BUSERR_1 - External Abort on Linefetch -- Page
    502  *  FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
    503  *  FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
    504  *  FAULT_BUSTRNL1 - External abort on Translation -- Level 1
    505  *  FAULT_BUSTRNL2 - External abort on Translation -- Level 2
    506  *
    507  * If pcb_onfault is set, flag the fault and return to the handler.
    508  * If the fault occurred in user mode, give the process a SIGBUS.
    509  *
    510  * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
    511  * can be flagged as imprecise in the FSR. This causes a real headache
    512  * since some of the machine state is lost. In this case, tf->tf_pc
    513  * may not actually point to the offending instruction. In fact, if
    514  * we've taken a double abort fault, it generally points somewhere near
    515  * the top of "data_abort_entry" in exception.S.
    516  *
    517  * In all other cases, these data aborts are considered fatal.
    518  */
    519 static int
    520 dab_buserr(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l,
    521     ksiginfo_t *ksi)
    522 {
    523 	struct pcb *pcb = &l->l_addr->u_pcb;
    524 
    525 #ifdef __XSCALE__
    526 	if ((fsr & FAULT_IMPRECISE) != 0 &&
    527 	    (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
    528 		/*
    529 		 * Oops, an imprecise, double abort fault. We've lost the
    530 		 * r14_abt/spsr_abt values corresponding to the original
    531 		 * abort, and the spsr saved in the trapframe indicates
    532 		 * ABT mode.
    533 		 */
    534 		tf->tf_spsr &= ~PSR_MODE;
    535 
    536 		/*
    537 		 * We use a simple heuristic to determine if the double abort
    538 		 * happened as a result of a kernel or user mode access.
    539 		 * If the current trapframe is at the top of the kernel stack,
    540 		 * the fault _must_ have come from user mode.
    541 		 */
    542 		if (tf != ((trapframe_t *)pcb->pcb_un.un_32.pcb32_sp) - 1) {
    543 			/*
    544 			 * Kernel mode. We're either about to die a
    545 			 * spectacular death, or pcb_onfault will come
    546 			 * to our rescue. Either way, the current value
    547 			 * of tf->tf_pc is irrelevant.
    548 			 */
    549 			tf->tf_spsr |= PSR_SVC32_MODE;
    550 			if (pcb->pcb_onfault == NULL)
    551 				printf("\nKernel mode double abort!\n");
    552 		} else {
    553 			/*
    554 			 * User mode. We've lost the program counter at the
    555 			 * time of the fault (not that it was accurate anyway;
    556 			 * it's not called an imprecise fault for nothing).
    557 			 * About all we can do is copy r14_usr to tf_pc and
    558 			 * hope for the best. The process is about to get a
    559 			 * SIGBUS, so it's probably history anyway.
    560 			 */
    561 			tf->tf_spsr |= PSR_USR32_MODE;
    562 			tf->tf_pc = tf->tf_usr_lr;
    563 		}
    564 	}
    565 
    566 	/* FAR is invalid for imprecise exceptions */
    567 	if ((fsr & FAULT_IMPRECISE) != 0)
    568 		far = 0;
    569 #endif /* __XSCALE__ */
    570 
    571 	if (pcb->pcb_onfault) {
    572 		KDASSERT(TRAP_USERMODE(tf) == 0);
    573 		tf->tf_r0 = EFAULT;
    574 		tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
    575 		return (0);
    576 	}
    577 
    578 	/* See if the cpu state needs to be fixed up */
    579 	(void) data_abort_fixup(tf, fsr, far, l);
    580 
    581 	/*
    582 	 * At this point, if the fault happened in kernel mode, we're toast
    583 	 */
    584 	if (!TRAP_USERMODE(tf))
    585 		dab_fatal(tf, fsr, far, l, NULL);
    586 
    587 	/* Deliver a bus error signal to the process */
    588 	KSI_INIT_TRAP(ksi);
    589 	ksi->ksi_signo = SIGBUS;
    590 	ksi->ksi_code = BUS_ADRERR;
    591 	ksi->ksi_addr = (u_int32_t *)(intptr_t)far;
    592 	ksi->ksi_trap = fsr;
    593 
    594 	l->l_addr->u_pcb.pcb_tf = tf;
    595 
    596 	return (1);
    597 }
    598 
    599 static __inline int
    600 prefetch_abort_fixup(trapframe_t *tf)
    601 {
    602 #ifdef CPU_ABORT_FIXUP_REQUIRED
    603 	int error;
    604 
    605 	/* Call the cpu specific prefetch abort fixup routine */
    606 	error = cpu_prefetchabt_fixup(tf);
    607 	if (__predict_true(error != ABORT_FIXUP_FAILED))
    608 		return (error);
    609 
    610 	/*
    611 	 * Oops, couldn't fix up the instruction
    612 	 */
    613 	printf(
    614 	    "prefetch_abort_fixup: fixup for %s mode prefetch abort failed.\n",
    615 	    TRAP_USERMODE(tf) ? "user" : "kernel");
    616 	printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
    617 	    *((u_int *)tf->tf_pc));
    618 	disassemble(tf->tf_pc);
    619 
    620 	/* Die now if this happened in kernel mode */
    621 	if (!TRAP_USERMODE(tf))
    622 		dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
    623 
    624 	return (error);
    625 #else
    626 	return (ABORT_FIXUP_OK);
    627 #endif /* CPU_ABORT_FIXUP_REQUIRED */
    628 }
    629 
    630 /*
    631  * void prefetch_abort_handler(trapframe_t *tf)
    632  *
    633  * Abort handler called when instruction execution occurs at
    634  * a non existent or restricted (access permissions) memory page.
    635  * If the address is invalid and we were in SVC mode then panic as
    636  * the kernel should never prefetch abort.
    637  * If the address is invalid and the page is mapped then the user process
    638  * does no have read permission so send it a signal.
    639  * Otherwise fault the page in and try again.
    640  */
    641 void
    642 prefetch_abort_handler(trapframe_t *tf)
    643 {
    644 	struct lwp *l;
    645 	struct vm_map *map;
    646 	vaddr_t fault_pc, va;
    647 	ksiginfo_t ksi;
    648 	int error;
    649 
    650 	/* Update vmmeter statistics */
    651 	uvmexp.traps++;
    652 
    653 	/*
    654 	 * Enable IRQ's (disabled by the abort) This always comes
    655 	 * from user mode so we know interrupts were not disabled.
    656 	 * But we check anyway.
    657 	 */
    658 	if (__predict_false((tf->tf_spsr & I32_bit) == 0))
    659 		enable_interrupts(I32_bit);
    660 
    661 	/* See if the cpu state needs to be fixed up */
    662 	switch (prefetch_abort_fixup(tf)) {
    663 	case ABORT_FIXUP_RETURN:
    664 		return;
    665 	case ABORT_FIXUP_FAILED:
    666 		/* Deliver a SIGILL to the process */
    667 		KSI_INIT_TRAP(&ksi);
    668 		ksi.ksi_signo = SIGILL;
    669 		ksi.ksi_code = ILL_ILLOPC;
    670 		ksi.ksi_addr = (u_int32_t *)(intptr_t) tf->tf_pc;
    671 		l = curlwp;
    672 		l->l_addr->u_pcb.pcb_tf = tf;
    673 		goto do_trapsignal;
    674 	default:
    675 		break;
    676 	}
    677 
    678 	/* Prefetch aborts cannot happen in kernel mode */
    679 	if (__predict_false(!TRAP_USERMODE(tf)))
    680 		dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
    681 
    682 	/* Get fault address */
    683 	fault_pc = tf->tf_pc;
    684 	l = curlwp;
    685 	l->l_addr->u_pcb.pcb_tf = tf;
    686 
    687 	/* Ok validate the address, can only execute in USER space */
    688 	if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS ||
    689 	    (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
    690 		KSI_INIT_TRAP(&ksi);
    691 		ksi.ksi_signo = SIGSEGV;
    692 		ksi.ksi_code = SEGV_ACCERR;
    693 		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
    694 		ksi.ksi_trap = fault_pc;
    695 		goto do_trapsignal;
    696 	}
    697 
    698 	map = &l->l_proc->p_vmspace->vm_map;
    699 	va = trunc_page(fault_pc);
    700 
    701 	/*
    702 	 * See if the pmap can handle this fault on its own...
    703 	 */
    704 #ifdef DEBUG
    705 	last_fault_code = -1;
    706 #endif
    707 	if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1))
    708 		goto out;
    709 
    710 #ifdef DIAGNOSTIC
    711 	if (__predict_false(current_intr_depth > 0)) {
    712 		printf("\nNon-emulated prefetch abort with intr_depth > 0\n");
    713 		dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
    714 	}
    715 #endif
    716 
    717 	error = uvm_fault(map, va, 0, VM_PROT_READ);
    718 	if (__predict_true(error == 0))
    719 		goto out;
    720 
    721 	if (error == ENOMEM) {
    722 		printf("UVM: pid %d (%s), uid %d killed: "
    723 		    "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
    724 		    (l->l_proc->p_cred && l->l_proc->p_ucred) ?
    725 		     l->l_proc->p_ucred->cr_uid : -1);
    726 	}
    727 
    728 	KSI_INIT_TRAP(&ksi);
    729 	ksi.ksi_signo = SIGSEGV;
    730 	ksi.ksi_code = SEGV_MAPERR;
    731 	ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
    732 	ksi.ksi_trap = fault_pc;
    733 	ksi.ksi_errno = error;
    734 
    735 do_trapsignal:
    736 	call_trapsignal(l, &ksi);
    737 
    738 out:
    739 	userret(l);
    740 }
    741 
    742 /*
    743  * Tentatively read an 8, 16, or 32-bit value from 'addr'.
    744  * If the read succeeds, the value is written to 'rptr' and zero is returned.
    745  * Else, return EFAULT.
    746  */
    747 int
    748 badaddr_read(void *addr, size_t size, void *rptr)
    749 {
    750 	extern int badaddr_read_1(const uint8_t *, uint8_t *);
    751 	extern int badaddr_read_2(const uint16_t *, uint16_t *);
    752 	extern int badaddr_read_4(const uint32_t *, uint32_t *);
    753 	union {
    754 		uint8_t v1;
    755 		uint16_t v2;
    756 		uint32_t v4;
    757 	} u;
    758 	int rv;
    759 
    760 	cpu_drain_writebuf();
    761 
    762 	/* Read from the test address. */
    763 	switch (size) {
    764 	case sizeof(uint8_t):
    765 		rv = badaddr_read_1(addr, &u.v1);
    766 		if (rv == 0 && rptr)
    767 			*(uint8_t *) rptr = u.v1;
    768 		break;
    769 
    770 	case sizeof(uint16_t):
    771 		rv = badaddr_read_2(addr, &u.v2);
    772 		if (rv == 0 && rptr)
    773 			*(uint16_t *) rptr = u.v2;
    774 		break;
    775 
    776 	case sizeof(uint32_t):
    777 		rv = badaddr_read_4(addr, &u.v4);
    778 		if (rv == 0 && rptr)
    779 			*(uint32_t *) rptr = u.v4;
    780 		break;
    781 
    782 	default:
    783 		panic("badaddr: invalid size (%lu)", (u_long) size);
    784 	}
    785 
    786 	/* Return EFAULT if the address was invalid, else zero */
    787 	return (rv);
    788 }
    789