Home | History | Annotate | Line # | Download | only in arm32
fault.c revision 1.86
      1 /*	$NetBSD: fault.c,v 1.86 2012/11/12 18:00:35 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.86 2012/11/12 18:00:35 skrll 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 <arm/arm32/katelib.h>
    102 
    103 #include <machine/intr.h>
    104 #include <machine/pcb.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 'x' is a permission fault */
    159 #define	IS_PERMISSION_FAULT(x)					\
    160 	(((1 << ((x) & FAULT_TYPE_MASK)) &			\
    161 	  ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0)
    162 
    163 #if 0
    164 /* maybe one day we'll do emulations */
    165 #define	TRAPSIGNAL(l,k)	(*(l)->l_proc->p_emul->e_trapsignal)((l), (k))
    166 #else
    167 #define	TRAPSIGNAL(l,k)	trapsignal((l), (k))
    168 #endif
    169 
    170 static inline void
    171 call_trapsignal(struct lwp *l, ksiginfo_t *ksi)
    172 {
    173 
    174 	TRAPSIGNAL(l, ksi);
    175 }
    176 
    177 static inline int
    178 data_abort_fixup(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l)
    179 {
    180 #ifdef CPU_ABORT_FIXUP_REQUIRED
    181 	int error;
    182 
    183 	/* Call the CPU specific data abort fixup routine */
    184 	error = cpu_dataabt_fixup(tf);
    185 	if (__predict_true(error != ABORT_FIXUP_FAILED))
    186 		return (error);
    187 
    188 	/*
    189 	 * Oops, couldn't fix up the instruction
    190 	 */
    191 	printf("%s: fixup for %s mode data abort failed.\n", __func__,
    192 	    TRAP_USERMODE(tf) ? "user" : "kernel");
    193 #ifdef THUMB_CODE
    194 	if (tf->tf_spsr & PSR_T_bit) {
    195 		printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ",
    196 		    tf->tf_pc, *((uint16 *)(tf->tf_pc & ~1)),
    197 		    *((uint16 *)((tf->tf_pc + 2) & ~1)));
    198 	}
    199 	else
    200 #endif
    201 	{
    202 		printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
    203 		    *((u_int *)tf->tf_pc));
    204 	}
    205 	disassemble(tf->tf_pc);
    206 
    207 	/* Die now if this happened in kernel mode */
    208 	if (!TRAP_USERMODE(tf))
    209 		dab_fatal(tf, fsr, far, l, NULL);
    210 
    211 	return (error);
    212 #else
    213 	return (ABORT_FIXUP_OK);
    214 #endif /* CPU_ABORT_FIXUP_REQUIRED */
    215 }
    216 
    217 void
    218 data_abort_handler(trapframe_t *tf)
    219 {
    220 	struct vm_map *map;
    221 	struct lwp * const l = curlwp;
    222 	struct cpu_info * const ci = curcpu();
    223 	u_int far, fsr;
    224 	vm_prot_t ftype;
    225 	void *onfault;
    226 	vaddr_t va;
    227 	int error;
    228 	ksiginfo_t ksi;
    229 
    230 	UVMHIST_FUNC("data_abort_handler");
    231 
    232 	/* Grab FAR/FSR before enabling interrupts */
    233 	far = cpu_faultaddress();
    234 	fsr = cpu_faultstatus();
    235 
    236 	UVMHIST_CALLED(maphist);
    237 	/* Update vmmeter statistics */
    238 	ci->ci_data.cpu_ntrap++;
    239 
    240 	/* Re-enable interrupts if they were enabled previously */
    241 	KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
    242 	if (__predict_true((tf->tf_spsr & IF32_bits) != IF32_bits))
    243 		restore_interrupts(tf->tf_spsr & IF32_bits);
    244 
    245 	/* Get the current lwp structure */
    246 
    247 	UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, far=0x%x, fsr=0x%x)",
    248 	    tf->tf_pc, l, far, fsr);
    249 
    250 	/* Data abort came from user mode? */
    251 	bool user = (TRAP_USERMODE(tf) != 0);
    252 	if (user)
    253 		LWP_CACHE_CREDS(l, l->l_proc);
    254 
    255 	/* Grab the current pcb */
    256 	struct pcb * const pcb = lwp_getpcb(l);
    257 
    258 	curcpu()->ci_abt_evs[fsr & FAULT_TYPE_MASK].ev_count++;
    259 
    260 	/* Invoke the appropriate handler, if necessary */
    261 	if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) {
    262 #ifdef DIAGNOSTIC
    263 		printf("%s: data_aborts fsr=0x%x far=0x%x\n",
    264 		    __func__, fsr, far);
    265 #endif
    266 		if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far,
    267 		    l, &ksi))
    268 			goto do_trapsignal;
    269 		goto out;
    270 	}
    271 
    272 	/*
    273 	 * At this point, we're dealing with one of the following data aborts:
    274 	 *
    275 	 *  FAULT_TRANS_S  - Translation -- Section
    276 	 *  FAULT_TRANS_P  - Translation -- Page
    277 	 *  FAULT_DOMAIN_S - Domain -- Section
    278 	 *  FAULT_DOMAIN_P - Domain -- Page
    279 	 *  FAULT_PERM_S   - Permission -- Section
    280 	 *  FAULT_PERM_P   - Permission -- Page
    281 	 *
    282 	 * These are the main virtual memory-related faults signalled by
    283 	 * the MMU.
    284 	 */
    285 
    286 	/* fusubailout is used by [fs]uswintr to avoid page faulting */
    287 	if (__predict_false(pcb->pcb_onfault == fusubailout)) {
    288 		tf->tf_r0 = EFAULT;
    289 		tf->tf_pc = (intptr_t) pcb->pcb_onfault;
    290 		return;
    291 	}
    292 
    293 	if (user) {
    294 		lwp_settrapframe(l, tf);
    295 	}
    296 
    297 	/*
    298 	 * Make sure the Program Counter is sane. We could fall foul of
    299 	 * someone executing Thumb code, in which case the PC might not
    300 	 * be word-aligned. This would cause a kernel alignment fault
    301 	 * further down if we have to decode the current instruction.
    302 	 */
    303 #ifdef THUMB_CODE
    304 	/*
    305 	 * XXX: It would be nice to be able to support Thumb in the kernel
    306 	 * at some point.
    307 	 */
    308 	if (__predict_false(!user && (tf->tf_pc & 3) != 0)) {
    309 		printf("\n%s: Misaligned Kernel-mode Program Counter\n",
    310 		    __func__);
    311 		dab_fatal(tf, fsr, far, l, NULL);
    312 	}
    313 #else
    314 	if (__predict_false((tf->tf_pc & 3) != 0)) {
    315 		if (user) {
    316 			/*
    317 			 * Give the user an illegal instruction signal.
    318 			 */
    319 			/* Deliver a SIGILL to the process */
    320 			KSI_INIT_TRAP(&ksi);
    321 			ksi.ksi_signo = SIGILL;
    322 			ksi.ksi_code = ILL_ILLOPC;
    323 			ksi.ksi_addr = (uint32_t *)(intptr_t) far;
    324 			ksi.ksi_trap = fsr;
    325 			goto do_trapsignal;
    326 		}
    327 
    328 		/*
    329 		 * The kernel never executes Thumb code.
    330 		 */
    331 		printf("\n%s: Misaligned Kernel-mode Program Counter\n",
    332 		    __func__);
    333 		dab_fatal(tf, fsr, far, l, NULL);
    334 	}
    335 #endif
    336 
    337 	/* See if the CPU state needs to be fixed up */
    338 	switch (data_abort_fixup(tf, fsr, far, l)) {
    339 	case ABORT_FIXUP_RETURN:
    340 		return;
    341 	case ABORT_FIXUP_FAILED:
    342 		/* Deliver a SIGILL to the process */
    343 		KSI_INIT_TRAP(&ksi);
    344 		ksi.ksi_signo = SIGILL;
    345 		ksi.ksi_code = ILL_ILLOPC;
    346 		ksi.ksi_addr = (uint32_t *)(intptr_t) far;
    347 		ksi.ksi_trap = fsr;
    348 		goto do_trapsignal;
    349 	default:
    350 		break;
    351 	}
    352 
    353 	va = trunc_page((vaddr_t)far);
    354 
    355 	/*
    356 	 * It is only a kernel address space fault iff:
    357 	 *	1. user == 0  and
    358 	 *	2. pcb_onfault not set or
    359 	 *	3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction.
    360 	 */
    361 	if (!user && (va >= VM_MIN_KERNEL_ADDRESS ||
    362 	    (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) &&
    363 	    __predict_true((pcb->pcb_onfault == NULL ||
    364 	     (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) {
    365 		map = kernel_map;
    366 
    367 		/* Was the fault due to the FPE/IPKDB ? */
    368 		if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
    369 			KSI_INIT_TRAP(&ksi);
    370 			ksi.ksi_signo = SIGSEGV;
    371 			ksi.ksi_code = SEGV_ACCERR;
    372 			ksi.ksi_addr = (uint32_t *)(intptr_t) far;
    373 			ksi.ksi_trap = fsr;
    374 
    375 			/*
    376 			 * Force exit via userret()
    377 			 * This is necessary as the FPE is an extension to
    378 			 * userland that actually runs in a priveledged mode
    379 			 * but uses USR mode permissions for its accesses.
    380 			 */
    381 			user = true;
    382 			goto do_trapsignal;
    383 		}
    384 	} else {
    385 		map = &l->l_proc->p_vmspace->vm_map;
    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 			    ((insn & 0x0f9000f0) == 0x01800090))   /* STREX[BDH] */
    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 = (uint32_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 * const mode = TRAP_USERMODE(tf) ? "user" : "kernel";
    527 
    528 	if (l != NULL) {
    529 		printf("Fatal %s mode data abort: '%s'\n", mode,
    530 		    data_aborts[fsr & FAULT_TYPE_MASK].desc);
    531 		printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr);
    532 		if ((fsr & FAULT_IMPRECISE) == 0)
    533 			printf("%08x, ", far);
    534 		else
    535 			printf("Invalid,  ");
    536 		printf("spsr=%08x\n", tf->tf_spsr);
    537 	} else {
    538 		printf("Fatal %s mode prefetch abort at 0x%08x\n",
    539 		    mode, tf->tf_pc);
    540 		printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr);
    541 	}
    542 
    543 	printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n",
    544 	    tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3);
    545 	printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n",
    546 	    tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
    547 	printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n",
    548 	    tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
    549 	printf("r12=%08x, ", tf->tf_r12);
    550 
    551 	if (TRAP_USERMODE(tf))
    552 		printf("usp=%08x, ulr=%08x",
    553 		    tf->tf_usr_sp, tf->tf_usr_lr);
    554 	else
    555 		printf("ssp=%08x, slr=%08x",
    556 		    tf->tf_svc_sp, tf->tf_svc_lr);
    557 	printf(", pc =%08x\n\n", tf->tf_pc);
    558 
    559 #if defined(DDB) || defined(KGDB)
    560 	kdb_trap(T_FAULT, tf);
    561 #endif
    562 	panic("Fatal abort");
    563 	/*NOTREACHED*/
    564 }
    565 
    566 /*
    567  * dab_align() handles the following data aborts:
    568  *
    569  *  FAULT_ALIGN_0 - Alignment fault
    570  *  FAULT_ALIGN_0 - Alignment fault
    571  *
    572  * These faults are fatal if they happen in kernel mode. Otherwise, we
    573  * deliver a bus error to the process.
    574  */
    575 static int
    576 dab_align(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
    577 {
    578 	/* Alignment faults are always fatal if they occur in kernel mode */
    579 	if (!TRAP_USERMODE(tf))
    580 		dab_fatal(tf, fsr, far, l, NULL);
    581 
    582 	/* pcb_onfault *must* be NULL at this point */
    583 	KDASSERT(((struct pcb *)lwp_getpcb(l))->pcb_onfault == NULL);
    584 
    585 	/* See if the CPU state needs to be fixed up */
    586 	(void) data_abort_fixup(tf, fsr, far, l);
    587 
    588 	/* Deliver a bus error signal to the process */
    589 	KSI_INIT_TRAP(ksi);
    590 	ksi->ksi_signo = SIGBUS;
    591 	ksi->ksi_code = BUS_ADRALN;
    592 	ksi->ksi_addr = (uint32_t *)(intptr_t)far;
    593 	ksi->ksi_trap = fsr;
    594 
    595 	lwp_settrapframe(l, tf);
    596 
    597 	return (1);
    598 }
    599 
    600 /*
    601  * dab_buserr() handles the following data aborts:
    602  *
    603  *  FAULT_BUSERR_0 - External Abort on Linefetch -- Section
    604  *  FAULT_BUSERR_1 - External Abort on Linefetch -- Page
    605  *  FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
    606  *  FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
    607  *  FAULT_BUSTRNL1 - External abort on Translation -- Level 1
    608  *  FAULT_BUSTRNL2 - External abort on Translation -- Level 2
    609  *
    610  * If pcb_onfault is set, flag the fault and return to the handler.
    611  * If the fault occurred in user mode, give the process a SIGBUS.
    612  *
    613  * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
    614  * can be flagged as imprecise in the FSR. This causes a real headache
    615  * since some of the machine state is lost. In this case, tf->tf_pc
    616  * may not actually point to the offending instruction. In fact, if
    617  * we've taken a double abort fault, it generally points somewhere near
    618  * the top of "data_abort_entry" in exception.S.
    619  *
    620  * In all other cases, these data aborts are considered fatal.
    621  */
    622 static int
    623 dab_buserr(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l,
    624     ksiginfo_t *ksi)
    625 {
    626 	struct pcb *pcb = lwp_getpcb(l);
    627 
    628 #ifdef __XSCALE__
    629 	if ((fsr & FAULT_IMPRECISE) != 0 &&
    630 	    (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
    631 		/*
    632 		 * Oops, an imprecise, double abort fault. We've lost the
    633 		 * r14_abt/spsr_abt values corresponding to the original
    634 		 * abort, and the spsr saved in the trapframe indicates
    635 		 * ABT mode.
    636 		 */
    637 		tf->tf_spsr &= ~PSR_MODE;
    638 
    639 		/*
    640 		 * We use a simple heuristic to determine if the double abort
    641 		 * happened as a result of a kernel or user mode access.
    642 		 * If the current trapframe is at the top of the kernel stack,
    643 		 * the fault _must_ have come from user mode.
    644 		 */
    645 		if (tf != ((trapframe_t *)pcb->pcb_sp) - 1) {
    646 			/*
    647 			 * Kernel mode. We're either about to die a
    648 			 * spectacular death, or pcb_onfault will come
    649 			 * to our rescue. Either way, the current value
    650 			 * of tf->tf_pc is irrelevant.
    651 			 */
    652 			tf->tf_spsr |= PSR_SVC32_MODE;
    653 			if (pcb->pcb_onfault == NULL)
    654 				printf("\nKernel mode double abort!\n");
    655 		} else {
    656 			/*
    657 			 * User mode. We've lost the program counter at the
    658 			 * time of the fault (not that it was accurate anyway;
    659 			 * it's not called an imprecise fault for nothing).
    660 			 * About all we can do is copy r14_usr to tf_pc and
    661 			 * hope for the best. The process is about to get a
    662 			 * SIGBUS, so it's probably history anyway.
    663 			 */
    664 			tf->tf_spsr |= PSR_USR32_MODE;
    665 			tf->tf_pc = tf->tf_usr_lr;
    666 #ifdef THUMB_CODE
    667 			tf->tf_spsr &= ~PSR_T_bit;
    668 			if (tf->tf_usr_lr & 1)
    669 				tf->tf_spsr |= PSR_T_bit;
    670 #endif
    671 		}
    672 	}
    673 
    674 	/* FAR is invalid for imprecise exceptions */
    675 	if ((fsr & FAULT_IMPRECISE) != 0)
    676 		far = 0;
    677 #endif /* __XSCALE__ */
    678 
    679 	if (pcb->pcb_onfault) {
    680 		KDASSERT(TRAP_USERMODE(tf) == 0);
    681 		tf->tf_r0 = EFAULT;
    682 		tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
    683 		return (0);
    684 	}
    685 
    686 	/* See if the CPU state needs to be fixed up */
    687 	(void) data_abort_fixup(tf, fsr, far, l);
    688 
    689 	/*
    690 	 * At this point, if the fault happened in kernel mode, we're toast
    691 	 */
    692 	if (!TRAP_USERMODE(tf))
    693 		dab_fatal(tf, fsr, far, l, NULL);
    694 
    695 	/* Deliver a bus error signal to the process */
    696 	KSI_INIT_TRAP(ksi);
    697 	ksi->ksi_signo = SIGBUS;
    698 	ksi->ksi_code = BUS_ADRERR;
    699 	ksi->ksi_addr = (uint32_t *)(intptr_t)far;
    700 	ksi->ksi_trap = fsr;
    701 
    702 	lwp_settrapframe(l, tf);
    703 
    704 	return (1);
    705 }
    706 
    707 static inline int
    708 prefetch_abort_fixup(trapframe_t *tf)
    709 {
    710 #ifdef CPU_ABORT_FIXUP_REQUIRED
    711 	int error;
    712 
    713 	/* Call the CPU specific prefetch abort fixup routine */
    714 	error = cpu_prefetchabt_fixup(tf);
    715 	if (__predict_true(error != ABORT_FIXUP_FAILED))
    716 		return (error);
    717 
    718 	/*
    719 	 * Oops, couldn't fix up the instruction
    720 	 */
    721 	printf("%s: fixup for %s mode prefetch abort failed.\n", __func__,
    722 	    TRAP_USERMODE(tf) ? "user" : "kernel");
    723 #ifdef THUMB_CODE
    724 	if (tf->tf_spsr & PSR_T_bit) {
    725 		printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ",
    726 		    tf->tf_pc, *((uint16 *)(tf->tf_pc & ~1)),
    727 		    *((uint16 *)((tf->tf_pc + 2) & ~1)));
    728 	}
    729 	else
    730 #endif
    731 	{
    732 		printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
    733 		    *((u_int *)tf->tf_pc));
    734 	}
    735 	disassemble(tf->tf_pc);
    736 
    737 	/* Die now if this happened in kernel mode */
    738 	if (!TRAP_USERMODE(tf))
    739 		dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
    740 
    741 	return (error);
    742 #else
    743 	return (ABORT_FIXUP_OK);
    744 #endif /* CPU_ABORT_FIXUP_REQUIRED */
    745 }
    746 
    747 /*
    748  * void prefetch_abort_handler(trapframe_t *tf)
    749  *
    750  * Abort handler called when instruction execution occurs at
    751  * a non existent or restricted (access permissions) memory page.
    752  * If the address is invalid and we were in SVC mode then panic as
    753  * the kernel should never prefetch abort.
    754  * If the address is invalid and the page is mapped then the user process
    755  * does no have read permission so send it a signal.
    756  * Otherwise fault the page in and try again.
    757  */
    758 void
    759 prefetch_abort_handler(trapframe_t *tf)
    760 {
    761 	struct lwp *l;
    762 	struct pcb *pcb;
    763 	struct vm_map *map;
    764 	vaddr_t fault_pc, va;
    765 	ksiginfo_t ksi;
    766 	int error, user;
    767 
    768 	UVMHIST_FUNC("prefetch_abort_handler"); UVMHIST_CALLED(maphist);
    769 
    770 	/* Update vmmeter statistics */
    771 	curcpu()->ci_data.cpu_ntrap++;
    772 
    773 	l = curlwp;
    774 	pcb = lwp_getpcb(l);
    775 
    776 	if ((user = TRAP_USERMODE(tf)) != 0)
    777 		LWP_CACHE_CREDS(l, l->l_proc);
    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 	KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
    785 	if (__predict_true((tf->tf_spsr & I32_bit) != IF32_bits))
    786 		restore_interrupts(tf->tf_spsr & IF32_bits);
    787 
    788 	/* See if the CPU state needs to be fixed up */
    789 	switch (prefetch_abort_fixup(tf)) {
    790 	case ABORT_FIXUP_RETURN:
    791 		KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
    792 		return;
    793 	case ABORT_FIXUP_FAILED:
    794 		/* Deliver a SIGILL to the process */
    795 		KSI_INIT_TRAP(&ksi);
    796 		ksi.ksi_signo = SIGILL;
    797 		ksi.ksi_code = ILL_ILLOPC;
    798 		ksi.ksi_addr = (uint32_t *)(intptr_t) tf->tf_pc;
    799 		lwp_settrapframe(l, tf);
    800 		goto do_trapsignal;
    801 	default:
    802 		break;
    803 	}
    804 
    805 	/* Prefetch aborts cannot happen in kernel mode */
    806 	if (__predict_false(!user))
    807 		dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
    808 
    809 	/* Get fault address */
    810 	fault_pc = tf->tf_pc;
    811 	lwp_settrapframe(l, 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 = (uint32_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(curcpu()->ci_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 	KASSERT(pcb->pcb_onfault == NULL);
    848 	error = uvm_fault(map, va, VM_PROT_READ);
    849 
    850 	if (__predict_true(error == 0)) {
    851 		UVMHIST_LOG (maphist, " <- uvm", 0, 0, 0, 0);
    852 		goto out;
    853 	}
    854 	KSI_INIT_TRAP(&ksi);
    855 
    856 	UVMHIST_LOG (maphist, " <- fatal (%d)", error, 0, 0, 0);
    857 	if (error == ENOMEM) {
    858 		printf("UVM: pid %d (%s), uid %d killed: "
    859 		    "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
    860 		    l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1);
    861 		ksi.ksi_signo = SIGKILL;
    862 	} else
    863 		ksi.ksi_signo = SIGSEGV;
    864 
    865 	ksi.ksi_code = SEGV_MAPERR;
    866 	ksi.ksi_addr = (uint32_t *)(intptr_t) fault_pc;
    867 	ksi.ksi_trap = fault_pc;
    868 
    869 do_trapsignal:
    870 	call_trapsignal(l, &ksi);
    871 
    872 out:
    873 	KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
    874 	userret(l);
    875 }
    876 
    877 /*
    878  * Tentatively read an 8, 16, or 32-bit value from 'addr'.
    879  * If the read succeeds, the value is written to 'rptr' and zero is returned.
    880  * Else, return EFAULT.
    881  */
    882 int
    883 badaddr_read(void *addr, size_t size, void *rptr)
    884 {
    885 	extern int badaddr_read_1(const uint8_t *, uint8_t *);
    886 	extern int badaddr_read_2(const uint16_t *, uint16_t *);
    887 	extern int badaddr_read_4(const uint32_t *, uint32_t *);
    888 	union {
    889 		uint8_t v1;
    890 		uint16_t v2;
    891 		uint32_t v4;
    892 	} u;
    893 	int rv, s;
    894 
    895 	cpu_drain_writebuf();
    896 
    897 	s = splhigh();
    898 
    899 	/* Read from the test address. */
    900 	switch (size) {
    901 	case sizeof(uint8_t):
    902 		rv = badaddr_read_1(addr, &u.v1);
    903 		if (rv == 0 && rptr)
    904 			*(uint8_t *) rptr = u.v1;
    905 		break;
    906 
    907 	case sizeof(uint16_t):
    908 		rv = badaddr_read_2(addr, &u.v2);
    909 		if (rv == 0 && rptr)
    910 			*(uint16_t *) rptr = u.v2;
    911 		break;
    912 
    913 	case sizeof(uint32_t):
    914 		rv = badaddr_read_4(addr, &u.v4);
    915 		if (rv == 0 && rptr)
    916 			*(uint32_t *) rptr = u.v4;
    917 		break;
    918 
    919 	default:
    920 		panic("%s: invalid size (%zu)", __func__, size);
    921 	}
    922 
    923 	splx(s);
    924 
    925 	/* Return EFAULT if the address was invalid, else zero */
    926 	return (rv);
    927 }
    928