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