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