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