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