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