Home | History | Annotate | Line # | Download | only in arm32
fault.c revision 1.78.12.1
      1  1.78.12.1       mrg /*	$NetBSD: fault.c,v 1.78.12.1 2012/02/18 07:31:23 mrg Exp $	*/
      2        1.1     chris 
      3        1.1     chris /*
      4       1.27       scw  * Copyright 2003 Wasabi Systems, Inc.
      5       1.27       scw  * All rights reserved.
      6       1.27       scw  *
      7       1.27       scw  * Written by Steve C. Woodford for Wasabi Systems, Inc.
      8       1.27       scw  *
      9       1.27       scw  * Redistribution and use in source and binary forms, with or without
     10       1.27       scw  * modification, are permitted provided that the following conditions
     11       1.27       scw  * are met:
     12       1.27       scw  * 1. Redistributions of source code must retain the above copyright
     13       1.27       scw  *    notice, this list of conditions and the following disclaimer.
     14       1.27       scw  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.27       scw  *    notice, this list of conditions and the following disclaimer in the
     16       1.27       scw  *    documentation and/or other materials provided with the distribution.
     17       1.27       scw  * 3. All advertising materials mentioning features or use of this software
     18       1.27       scw  *    must display the following acknowledgement:
     19       1.27       scw  *      This product includes software developed for the NetBSD Project by
     20       1.27       scw  *      Wasabi Systems, Inc.
     21       1.27       scw  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22       1.27       scw  *    or promote products derived from this software without specific prior
     23       1.27       scw  *    written permission.
     24       1.27       scw  *
     25       1.27       scw  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26       1.27       scw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27       1.27       scw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28       1.27       scw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29       1.27       scw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30       1.27       scw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31       1.27       scw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32       1.27       scw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33       1.27       scw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34       1.27       scw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35       1.27       scw  * POSSIBILITY OF SUCH DAMAGE.
     36       1.27       scw  */
     37       1.27       scw /*
     38        1.1     chris  * Copyright (c) 1994-1997 Mark Brinicombe.
     39        1.1     chris  * Copyright (c) 1994 Brini.
     40        1.1     chris  * All rights reserved.
     41        1.1     chris  *
     42        1.1     chris  * This code is derived from software written for Brini by Mark Brinicombe
     43        1.1     chris  *
     44        1.1     chris  * Redistribution and use in source and binary forms, with or without
     45        1.1     chris  * modification, are permitted provided that the following conditions
     46        1.1     chris  * are met:
     47        1.1     chris  * 1. Redistributions of source code must retain the above copyright
     48        1.1     chris  *    notice, this list of conditions and the following disclaimer.
     49        1.1     chris  * 2. Redistributions in binary form must reproduce the above copyright
     50        1.1     chris  *    notice, this list of conditions and the following disclaimer in the
     51        1.1     chris  *    documentation and/or other materials provided with the distribution.
     52        1.1     chris  * 3. All advertising materials mentioning features or use of this software
     53        1.1     chris  *    must display the following acknowledgement:
     54        1.1     chris  *	This product includes software developed by Brini.
     55        1.1     chris  * 4. The name of the company nor the name of the author may be used to
     56        1.1     chris  *    endorse or promote products derived from this software without specific
     57        1.1     chris  *    prior written permission.
     58        1.1     chris  *
     59        1.1     chris  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
     60        1.1     chris  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     61        1.1     chris  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62        1.1     chris  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     63        1.1     chris  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     64        1.1     chris  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     65        1.1     chris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66        1.1     chris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67        1.1     chris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68        1.1     chris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69        1.1     chris  * SUCH DAMAGE.
     70        1.1     chris  *
     71        1.1     chris  * RiscBSD kernel project
     72        1.1     chris  *
     73        1.1     chris  * fault.c
     74        1.1     chris  *
     75        1.1     chris  * Fault handlers
     76        1.1     chris  *
     77        1.1     chris  * Created      : 28/11/94
     78        1.1     chris  */
     79        1.1     chris 
     80        1.1     chris #include "opt_ddb.h"
     81       1.28    briggs #include "opt_kgdb.h"
     82       1.72      matt #include "opt_sa.h"
     83        1.1     chris 
     84        1.1     chris #include <sys/types.h>
     85  1.78.12.1       mrg __KERNEL_RCSID(0, "$NetBSD: fault.c,v 1.78.12.1 2012/02/18 07:31:23 mrg Exp $");
     86       1.21     bjh21 
     87        1.1     chris #include <sys/param.h>
     88        1.1     chris #include <sys/systm.h>
     89        1.1     chris #include <sys/proc.h>
     90        1.1     chris #include <sys/kernel.h>
     91       1.60      yamt #include <sys/kauth.h>
     92       1.71    cegger 
     93       1.71    cegger #include <sys/savar.h>
     94       1.65      matt #include <sys/cpu.h>
     95        1.1     chris 
     96        1.1     chris #include <uvm/uvm_extern.h>
     97       1.50  rearnsha #include <uvm/uvm_stat.h>
     98       1.50  rearnsha #ifdef UVMHIST
     99       1.50  rearnsha #include <uvm/uvm.h>
    100       1.50  rearnsha #endif
    101       1.18   thorpej 
    102       1.18   thorpej #include <arm/cpuconf.h>
    103        1.1     chris 
    104        1.1     chris #include <machine/frame.h>
    105        1.5   thorpej #include <arm/arm32/katelib.h>
    106        1.2      matt #include <machine/intr.h>
    107       1.28    briggs #if defined(DDB) || defined(KGDB)
    108        1.1     chris #include <machine/db_machdep.h>
    109       1.28    briggs #ifdef KGDB
    110       1.28    briggs #include <sys/kgdb.h>
    111       1.28    briggs #endif
    112       1.28    briggs #if !defined(DDB)
    113       1.28    briggs #define kdb_trap	kgdb_trap
    114       1.28    briggs #endif
    115        1.1     chris #endif
    116        1.1     chris 
    117        1.1     chris #include <arch/arm/arm/disassem.h>
    118        1.7     chris #include <arm/arm32/machdep.h>
    119        1.7     chris 
    120        1.1     chris extern char fusubailout[];
    121        1.1     chris 
    122       1.27       scw #ifdef DEBUG
    123       1.27       scw int last_fault_code;	/* For the benefit of pmap_fault_fixup() */
    124       1.27       scw #endif
    125       1.27       scw 
    126       1.39       scw #if defined(CPU_ARM3) || defined(CPU_ARM6) || \
    127       1.39       scw     defined(CPU_ARM7) || defined(CPU_ARM7TDMI)
    128       1.39       scw /* These CPUs may need data/prefetch abort fixups */
    129       1.39       scw #define	CPU_ABORT_FIXUP_REQUIRED
    130       1.39       scw #endif
    131        1.7     chris 
    132       1.39       scw struct data_abort {
    133       1.39       scw 	int (*func)(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
    134       1.39       scw 	const char *desc;
    135       1.39       scw };
    136        1.1     chris 
    137       1.39       scw static int dab_fatal(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
    138       1.39       scw static int dab_align(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
    139       1.39       scw static int dab_buserr(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *);
    140       1.39       scw 
    141       1.39       scw static const struct data_abort data_aborts[] = {
    142       1.39       scw 	{dab_fatal,	"Vector Exception"},
    143       1.39       scw 	{dab_align,	"Alignment Fault 1"},
    144       1.39       scw 	{dab_fatal,	"Terminal Exception"},
    145       1.39       scw 	{dab_align,	"Alignment Fault 3"},
    146       1.39       scw 	{dab_buserr,	"External Linefetch Abort (S)"},
    147       1.39       scw 	{NULL,		"Translation Fault (S)"},
    148       1.39       scw 	{dab_buserr,	"External Linefetch Abort (P)"},
    149       1.39       scw 	{NULL,		"Translation Fault (P)"},
    150       1.39       scw 	{dab_buserr,	"External Non-Linefetch Abort (S)"},
    151       1.39       scw 	{NULL,		"Domain Fault (S)"},
    152       1.39       scw 	{dab_buserr,	"External Non-Linefetch Abort (P)"},
    153       1.39       scw 	{NULL,		"Domain Fault (P)"},
    154       1.39       scw 	{dab_buserr,	"External Translation Abort (L1)"},
    155       1.39       scw 	{NULL,		"Permission Fault (S)"},
    156       1.39       scw 	{dab_buserr,	"External Translation Abort (L2)"},
    157       1.39       scw 	{NULL,		"Permission Fault (P)"}
    158       1.39       scw };
    159        1.1     chris 
    160       1.39       scw /* Determine if a fault came from user mode */
    161       1.39       scw #define	TRAP_USERMODE(tf)	((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
    162       1.39       scw 
    163       1.39       scw /* Determine if 'x' is a permission fault */
    164       1.39       scw #define	IS_PERMISSION_FAULT(x)					\
    165       1.39       scw 	(((1 << ((x) & FAULT_TYPE_MASK)) &			\
    166       1.39       scw 	  ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0)
    167        1.1     chris 
    168       1.39       scw #if 0
    169       1.39       scw /* maybe one day we'll do emulations */
    170       1.39       scw #define	TRAPSIGNAL(l,k)	(*(l)->l_proc->p_emul->e_trapsignal)((l), (k))
    171       1.39       scw #else
    172       1.39       scw #define	TRAPSIGNAL(l,k)	trapsignal((l), (k))
    173        1.1     chris #endif
    174        1.3   thorpej 
    175       1.56     perry static inline void
    176       1.39       scw call_trapsignal(struct lwp *l, ksiginfo_t *ksi)
    177        1.3   thorpej {
    178        1.3   thorpej 
    179       1.39       scw 	TRAPSIGNAL(l, ksi);
    180       1.39       scw }
    181        1.3   thorpej 
    182       1.56     perry static inline int
    183       1.39       scw data_abort_fixup(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l)
    184       1.39       scw {
    185       1.39       scw #ifdef CPU_ABORT_FIXUP_REQUIRED
    186       1.39       scw 	int error;
    187        1.3   thorpej 
    188       1.48       wiz 	/* Call the CPU specific data abort fixup routine */
    189       1.39       scw 	error = cpu_dataabt_fixup(tf);
    190       1.39       scw 	if (__predict_true(error != ABORT_FIXUP_FAILED))
    191       1.39       scw 		return (error);
    192        1.3   thorpej 
    193       1.39       scw 	/*
    194       1.39       scw 	 * Oops, couldn't fix up the instruction
    195       1.39       scw 	 */
    196  1.78.12.1       mrg 	printf("%s: fixup for %s mode data abort failed.\n", __func__,
    197       1.39       scw 	    TRAP_USERMODE(tf) ? "user" : "kernel");
    198       1.51  rearnsha #ifdef THUMB_CODE
    199       1.51  rearnsha 	if (tf->tf_spsr & PSR_T_bit) {
    200       1.51  rearnsha 		printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ",
    201       1.66     chris 		    tf->tf_pc, *((u_int16 *)(tf->tf_pc & ~1)),
    202       1.66     chris 		    *((u_int16 *)((tf->tf_pc + 2) & ~1)));
    203       1.51  rearnsha 	}
    204       1.51  rearnsha 	else
    205       1.51  rearnsha #endif
    206       1.51  rearnsha 	{
    207       1.51  rearnsha 		printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
    208       1.51  rearnsha 		    *((u_int *)tf->tf_pc));
    209       1.51  rearnsha 	}
    210       1.39       scw 	disassemble(tf->tf_pc);
    211       1.39       scw 
    212       1.39       scw 	/* Die now if this happened in kernel mode */
    213       1.39       scw 	if (!TRAP_USERMODE(tf))
    214       1.39       scw 		dab_fatal(tf, fsr, far, l, NULL);
    215        1.3   thorpej 
    216       1.39       scw 	return (error);
    217       1.39       scw #else
    218       1.39       scw 	return (ABORT_FIXUP_OK);
    219       1.39       scw #endif /* CPU_ABORT_FIXUP_REQUIRED */
    220        1.3   thorpej }
    221        1.3   thorpej 
    222        1.1     chris void
    223       1.39       scw data_abort_handler(trapframe_t *tf)
    224        1.1     chris {
    225       1.39       scw 	struct vm_map *map;
    226       1.39       scw 	struct pcb *pcb;
    227       1.26   thorpej 	struct lwp *l;
    228       1.39       scw 	u_int user, far, fsr;
    229       1.39       scw 	vm_prot_t ftype;
    230        1.1     chris 	void *onfault;
    231       1.27       scw 	vaddr_t va;
    232       1.39       scw 	int error;
    233       1.34      matt 	ksiginfo_t ksi;
    234        1.3   thorpej 
    235       1.50  rearnsha 	UVMHIST_FUNC("data_abort_handler");
    236       1.50  rearnsha 
    237       1.39       scw 	/* Grab FAR/FSR before enabling interrupts */
    238       1.39       scw 	far = cpu_faultaddress();
    239       1.39       scw 	fsr = cpu_faultstatus();
    240        1.1     chris 
    241       1.50  rearnsha 	UVMHIST_CALLED(maphist);
    242       1.39       scw 	/* Update vmmeter statistics */
    243       1.78      matt 	curcpu()->ci_data.cpu_ntrap++;
    244        1.1     chris 
    245       1.39       scw 	/* Re-enable interrupts if they were enabled previously */
    246       1.72      matt 	KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
    247       1.72      matt 	if (__predict_true((tf->tf_spsr & IF32_bits) != IF32_bits))
    248       1.72      matt 		restore_interrupts(tf->tf_spsr & IF32_bits);
    249        1.1     chris 
    250       1.67      matt 	/* Get the current lwp structure */
    251       1.67      matt 	KASSERT(curlwp != NULL);
    252       1.67      matt 	l = curlwp;
    253        1.1     chris 
    254       1.50  rearnsha 	UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, far=0x%x, fsr=0x%x)",
    255       1.50  rearnsha 	    tf->tf_pc, l, far, fsr);
    256       1.50  rearnsha 
    257       1.39       scw 	/* Data abort came from user mode? */
    258       1.61        ad 	if ((user = TRAP_USERMODE(tf)) != 0)
    259       1.61        ad 		LWP_CACHE_CREDS(l, l->l_proc);
    260        1.1     chris 
    261       1.39       scw 	/* Grab the current pcb */
    262       1.74   mbalmer 	pcb = lwp_getpcb(l);
    263        1.1     chris 
    264       1.39       scw 	/* Invoke the appropriate handler, if necessary */
    265       1.39       scw 	if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) {
    266  1.78.12.1       mrg #ifdef DIAGNOSTIC
    267  1.78.12.1       mrg 		printf("%s: data_aborts fsr=0x%x far=0x%x\n",
    268  1.78.12.1       mrg 		    __func__, fsr, far);
    269  1.78.12.1       mrg #endif
    270       1.39       scw 		if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far,
    271       1.39       scw 		    l, &ksi))
    272       1.39       scw 			goto do_trapsignal;
    273       1.39       scw 		goto out;
    274       1.39       scw 	}
    275        1.1     chris 
    276        1.1     chris 	/*
    277       1.39       scw 	 * At this point, we're dealing with one of the following data aborts:
    278       1.39       scw 	 *
    279       1.39       scw 	 *  FAULT_TRANS_S  - Translation -- Section
    280       1.39       scw 	 *  FAULT_TRANS_P  - Translation -- Page
    281       1.39       scw 	 *  FAULT_DOMAIN_S - Domain -- Section
    282       1.39       scw 	 *  FAULT_DOMAIN_P - Domain -- Page
    283       1.39       scw 	 *  FAULT_PERM_S   - Permission -- Section
    284       1.39       scw 	 *  FAULT_PERM_P   - Permission -- Page
    285       1.39       scw 	 *
    286       1.39       scw 	 * These are the main virtual memory-related faults signalled by
    287       1.39       scw 	 * the MMU.
    288        1.1     chris 	 */
    289        1.1     chris 
    290        1.1     chris 	/* fusubailout is used by [fs]uswintr to avoid page faulting */
    291       1.39       scw 	if (__predict_false(pcb->pcb_onfault == fusubailout)) {
    292       1.39       scw 		tf->tf_r0 = EFAULT;
    293       1.39       scw 		tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
    294        1.1     chris 		return;
    295        1.1     chris 	}
    296        1.1     chris 
    297       1.73     rmind 	if (user) {
    298       1.73     rmind 		pcb->pcb_tf = tf;
    299       1.73     rmind 	}
    300        1.1     chris 
    301       1.40       scw 	/*
    302       1.40       scw 	 * Make sure the Program Counter is sane. We could fall foul of
    303       1.40       scw 	 * someone executing Thumb code, in which case the PC might not
    304       1.40       scw 	 * be word-aligned. This would cause a kernel alignment fault
    305       1.40       scw 	 * further down if we have to decode the current instruction.
    306       1.40       scw 	 */
    307       1.51  rearnsha #ifdef THUMB_CODE
    308       1.51  rearnsha 	/*
    309       1.51  rearnsha 	 * XXX: It would be nice to be able to support Thumb in the kernel
    310       1.51  rearnsha 	 * at some point.
    311       1.51  rearnsha 	 */
    312       1.51  rearnsha 	if (__predict_false(!user && (tf->tf_pc & 3) != 0)) {
    313  1.78.12.1       mrg 		printf("\n%s: Misaligned Kernel-mode Program Counter\n",
    314  1.78.12.1       mrg 		    __func__);
    315       1.51  rearnsha 		dab_fatal(tf, fsr, far, l, NULL);
    316       1.51  rearnsha 	}
    317       1.51  rearnsha #else
    318       1.40       scw 	if (__predict_false((tf->tf_pc & 3) != 0)) {
    319       1.40       scw 		if (user) {
    320       1.40       scw 			/*
    321       1.40       scw 			 * Give the user an illegal instruction signal.
    322       1.40       scw 			 */
    323       1.40       scw 			/* Deliver a SIGILL to the process */
    324       1.40       scw 			KSI_INIT_TRAP(&ksi);
    325       1.40       scw 			ksi.ksi_signo = SIGILL;
    326       1.40       scw 			ksi.ksi_code = ILL_ILLOPC;
    327       1.40       scw 			ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
    328       1.40       scw 			ksi.ksi_trap = fsr;
    329       1.40       scw 			goto do_trapsignal;
    330       1.40       scw 		}
    331       1.40       scw 
    332       1.40       scw 		/*
    333       1.40       scw 		 * The kernel never executes Thumb code.
    334       1.40       scw 		 */
    335  1.78.12.1       mrg 		printf("\n%s: Misaligned Kernel-mode Program Counter\n",
    336  1.78.12.1       mrg 		    __func__);
    337       1.40       scw 		dab_fatal(tf, fsr, far, l, NULL);
    338       1.27       scw 	}
    339       1.51  rearnsha #endif
    340       1.27       scw 
    341       1.48       wiz 	/* See if the CPU state needs to be fixed up */
    342       1.41       scw 	switch (data_abort_fixup(tf, fsr, far, l)) {
    343       1.41       scw 	case ABORT_FIXUP_RETURN:
    344       1.41       scw 		return;
    345       1.41       scw 	case ABORT_FIXUP_FAILED:
    346       1.41       scw 		/* Deliver a SIGILL to the process */
    347       1.41       scw 		KSI_INIT_TRAP(&ksi);
    348       1.41       scw 		ksi.ksi_signo = SIGILL;
    349       1.41       scw 		ksi.ksi_code = ILL_ILLOPC;
    350       1.41       scw 		ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
    351       1.41       scw 		ksi.ksi_trap = fsr;
    352       1.41       scw 		goto do_trapsignal;
    353       1.41       scw 	default:
    354       1.41       scw 		break;
    355       1.41       scw 	}
    356       1.41       scw 
    357       1.39       scw 	va = trunc_page((vaddr_t)far);
    358        1.1     chris 
    359       1.27       scw 	/*
    360       1.27       scw 	 * It is only a kernel address space fault iff:
    361       1.27       scw 	 *	1. user == 0  and
    362       1.27       scw 	 *	2. pcb_onfault not set or
    363       1.41       scw 	 *	3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction.
    364       1.27       scw 	 */
    365       1.39       scw 	if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
    366       1.41       scw 	    (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) &&
    367       1.41       scw 	    __predict_true((pcb->pcb_onfault == NULL ||
    368       1.41       scw 	     (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) {
    369       1.39       scw 		map = kernel_map;
    370       1.39       scw 
    371       1.27       scw 		/* Was the fault due to the FPE/IPKDB ? */
    372       1.39       scw 		if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
    373       1.35   thorpej 			KSI_INIT_TRAP(&ksi);
    374       1.34      matt 			ksi.ksi_signo = SIGSEGV;
    375       1.39       scw 			ksi.ksi_code = SEGV_ACCERR;
    376       1.39       scw 			ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
    377       1.39       scw 			ksi.ksi_trap = fsr;
    378       1.27       scw 
    379       1.27       scw 			/*
    380       1.27       scw 			 * Force exit via userret()
    381       1.39       scw 			 * This is necessary as the FPE is an extension to
    382       1.39       scw 			 * userland that actually runs in a priveledged mode
    383       1.39       scw 			 * but uses USR mode permissions for its accesses.
    384       1.27       scw 			 */
    385       1.39       scw 			user = 1;
    386       1.39       scw 			goto do_trapsignal;
    387       1.27       scw 		}
    388       1.70  wrstuden 	} else {
    389       1.39       scw 		map = &l->l_proc->p_vmspace->vm_map;
    390       1.72      matt #ifdef KERN_SA
    391       1.70  wrstuden 		if ((l->l_flag & LW_SA) && (~l->l_pflag & LP_SA_NOBLOCK)) {
    392       1.70  wrstuden 			l->l_savp->savp_faultaddr = (vaddr_t)far;
    393       1.70  wrstuden 			l->l_pflag |= LP_SA_PAGEFAULT;
    394       1.70  wrstuden 		}
    395       1.72      matt #endif
    396       1.70  wrstuden 	}
    397        1.1     chris 
    398       1.27       scw 	/*
    399       1.27       scw 	 * We need to know whether the page should be mapped
    400       1.27       scw 	 * as R or R/W. The MMU does not give us the info as
    401       1.27       scw 	 * to whether the fault was caused by a read or a write.
    402       1.39       scw 	 *
    403       1.39       scw 	 * However, we know that a permission fault can only be
    404       1.39       scw 	 * the result of a write to a read-only location, so
    405       1.39       scw 	 * we can deal with those quickly.
    406       1.39       scw 	 *
    407       1.39       scw 	 * Otherwise we need to disassemble the instruction
    408       1.39       scw 	 * responsible to determine if it was a write.
    409       1.27       scw 	 */
    410       1.39       scw 	if (IS_PERMISSION_FAULT(fsr))
    411       1.27       scw 		ftype = VM_PROT_WRITE;
    412       1.39       scw 	else {
    413       1.51  rearnsha #ifdef THUMB_CODE
    414       1.51  rearnsha 		/* Fast track the ARM case.  */
    415       1.51  rearnsha 		if (__predict_false(tf->tf_spsr & PSR_T_bit)) {
    416       1.51  rearnsha 			u_int insn = fusword((void *)(tf->tf_pc & ~1));
    417       1.51  rearnsha 			u_int insn_f8 = insn & 0xf800;
    418       1.51  rearnsha 			u_int insn_fe = insn & 0xfe00;
    419       1.51  rearnsha 
    420       1.51  rearnsha 			if (insn_f8 == 0x6000 || /* STR(1) */
    421       1.51  rearnsha 			    insn_f8 == 0x7000 || /* STRB(1) */
    422       1.51  rearnsha 			    insn_f8 == 0x8000 || /* STRH(1) */
    423       1.51  rearnsha 			    insn_f8 == 0x9000 || /* STR(3) */
    424       1.51  rearnsha 			    insn_f8 == 0xc000 || /* STM */
    425       1.51  rearnsha 			    insn_fe == 0x5000 || /* STR(2) */
    426       1.51  rearnsha 			    insn_fe == 0x5200 || /* STRH(2) */
    427       1.51  rearnsha 			    insn_fe == 0x5400)   /* STRB(2) */
    428       1.51  rearnsha 				ftype = VM_PROT_WRITE;
    429       1.51  rearnsha 			else
    430       1.51  rearnsha 				ftype = VM_PROT_READ;
    431       1.51  rearnsha 		}
    432       1.51  rearnsha 		else
    433       1.51  rearnsha #endif
    434       1.51  rearnsha 		{
    435       1.51  rearnsha 			u_int insn = ReadWord(tf->tf_pc);
    436       1.39       scw 
    437       1.51  rearnsha 			if (((insn & 0x0c100000) == 0x04000000) || /* STR[B] */
    438       1.51  rearnsha 			    ((insn & 0x0e1000b0) == 0x000000b0) || /* STR[HD]*/
    439       1.51  rearnsha 			    ((insn & 0x0a100000) == 0x08000000))   /* STM/CDT*/
    440       1.51  rearnsha 				ftype = VM_PROT_WRITE;
    441       1.51  rearnsha 			else if ((insn & 0x0fb00ff0) == 0x01000090)/* SWP */
    442       1.51  rearnsha 				ftype = VM_PROT_READ | VM_PROT_WRITE;
    443       1.51  rearnsha 			else
    444       1.51  rearnsha 				ftype = VM_PROT_READ;
    445       1.51  rearnsha 		}
    446       1.39       scw 	}
    447       1.39       scw 
    448       1.39       scw 	/*
    449       1.39       scw 	 * See if the fault is as a result of ref/mod emulation,
    450       1.39       scw 	 * or domain mismatch.
    451       1.39       scw 	 */
    452       1.39       scw #ifdef DEBUG
    453       1.39       scw 	last_fault_code = fsr;
    454        1.1     chris #endif
    455       1.42    briggs 	if (pmap_fault_fixup(map->pmap, va, ftype, user)) {
    456       1.72      matt #ifdef KERN_SA
    457       1.70  wrstuden 		if (map != kernel_map)
    458       1.70  wrstuden 			l->l_pflag &= ~LP_SA_PAGEFAULT;
    459       1.72      matt #endif
    460       1.50  rearnsha 		UVMHIST_LOG(maphist, " <- ref/mod emul", 0, 0, 0, 0);
    461       1.27       scw 		goto out;
    462       1.42    briggs 	}
    463        1.1     chris 
    464       1.67      matt 	if (__predict_false(curcpu()->ci_intr_depth > 0)) {
    465       1.45       scw 		if (pcb->pcb_onfault) {
    466       1.45       scw 			tf->tf_r0 = EINVAL;
    467       1.45       scw 			tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
    468       1.45       scw 			return;
    469       1.45       scw 		}
    470       1.39       scw 		printf("\nNon-emulated page fault with intr_depth > 0\n");
    471       1.39       scw 		dab_fatal(tf, fsr, far, l, NULL);
    472       1.27       scw 	}
    473        1.1     chris 
    474       1.27       scw 	onfault = pcb->pcb_onfault;
    475       1.27       scw 	pcb->pcb_onfault = NULL;
    476       1.57        he 	error = uvm_fault(map, va, ftype);
    477       1.27       scw 	pcb->pcb_onfault = onfault;
    478       1.39       scw 
    479       1.72      matt #ifdef KERN_SA
    480       1.70  wrstuden 	if (map != kernel_map)
    481       1.70  wrstuden 		l->l_pflag &= ~LP_SA_PAGEFAULT;
    482       1.72      matt #endif
    483       1.70  wrstuden 
    484       1.39       scw 	if (__predict_true(error == 0)) {
    485       1.39       scw 		if (user)
    486       1.39       scw 			uvm_grow(l->l_proc, va); /* Record any stack growth */
    487       1.77       chs 		else
    488       1.77       chs 			ucas_ras_check(tf);
    489       1.50  rearnsha 		UVMHIST_LOG(maphist, " <- uvm", 0, 0, 0, 0);
    490       1.27       scw 		goto out;
    491       1.27       scw 	}
    492       1.39       scw 
    493       1.27       scw 	if (user == 0) {
    494       1.27       scw 		if (pcb->pcb_onfault) {
    495       1.39       scw 			tf->tf_r0 = error;
    496       1.39       scw 			tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
    497       1.39       scw 			return;
    498        1.1     chris 		}
    499       1.39       scw 
    500       1.58  drochner 		printf("\nuvm_fault(%p, %lx, %x) -> %x\n", map, va, ftype,
    501       1.39       scw 		    error);
    502       1.39       scw 		dab_fatal(tf, fsr, far, l, NULL);
    503       1.27       scw 	}
    504        1.1     chris 
    505       1.43       scw 	KSI_INIT_TRAP(&ksi);
    506       1.43       scw 
    507       1.39       scw 	if (error == ENOMEM) {
    508       1.39       scw 		printf("UVM: pid %d (%s), uid %d killed: "
    509       1.39       scw 		    "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
    510       1.62        ad 		    l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1);
    511       1.43       scw 		ksi.ksi_signo = SIGKILL;
    512       1.43       scw 	} else
    513       1.43       scw 		ksi.ksi_signo = SIGSEGV;
    514       1.34      matt 
    515       1.39       scw 	ksi.ksi_code = (error == EACCES) ? SEGV_ACCERR : SEGV_MAPERR;
    516       1.39       scw 	ksi.ksi_addr = (u_int32_t *)(intptr_t) far;
    517       1.39       scw 	ksi.ksi_trap = fsr;
    518       1.69        is 	UVMHIST_LOG(maphist, " <- error (%d)", error, 0, 0, 0);
    519       1.39       scw 
    520       1.39       scw do_trapsignal:
    521       1.39       scw 	call_trapsignal(l, &ksi);
    522       1.39       scw out:
    523       1.39       scw 	/* If returning to user mode, make sure to invoke userret() */
    524       1.39       scw 	if (user)
    525       1.39       scw 		userret(l);
    526       1.39       scw }
    527       1.39       scw 
    528       1.39       scw /*
    529       1.39       scw  * dab_fatal() handles the following data aborts:
    530       1.39       scw  *
    531       1.39       scw  *  FAULT_WRTBUF_0 - Vector Exception
    532       1.39       scw  *  FAULT_WRTBUF_1 - Terminal Exception
    533       1.39       scw  *
    534       1.39       scw  * We should never see these on a properly functioning system.
    535       1.39       scw  *
    536       1.39       scw  * This function is also called by the other handlers if they
    537       1.39       scw  * detect a fatal problem.
    538       1.39       scw  *
    539       1.39       scw  * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
    540       1.39       scw  */
    541       1.39       scw static int
    542       1.39       scw dab_fatal(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
    543       1.39       scw {
    544       1.39       scw 	const char *mode;
    545       1.39       scw 
    546       1.39       scw 	mode = TRAP_USERMODE(tf) ? "user" : "kernel";
    547       1.39       scw 
    548       1.39       scw 	if (l != NULL) {
    549       1.39       scw 		printf("Fatal %s mode data abort: '%s'\n", mode,
    550       1.39       scw 		    data_aborts[fsr & FAULT_TYPE_MASK].desc);
    551       1.44       scw 		printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr);
    552       1.39       scw 		if ((fsr & FAULT_IMPRECISE) == 0)
    553       1.44       scw 			printf("%08x, ", far);
    554       1.39       scw 		else
    555       1.44       scw 			printf("Invalid,  ");
    556       1.44       scw 		printf("spsr=%08x\n", tf->tf_spsr);
    557       1.39       scw 	} else {
    558       1.44       scw 		printf("Fatal %s mode prefetch abort at 0x%08x\n",
    559       1.44       scw 		    mode, tf->tf_pc);
    560       1.44       scw 		printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr);
    561       1.44       scw 	}
    562       1.44       scw 
    563       1.44       scw 	printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n",
    564       1.44       scw 	    tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3);
    565       1.44       scw 	printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n",
    566       1.44       scw 	    tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
    567       1.44       scw 	printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n",
    568       1.44       scw 	    tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
    569       1.44       scw 	printf("r12=%08x, ", tf->tf_r12);
    570       1.44       scw 
    571       1.44       scw 	if (TRAP_USERMODE(tf))
    572       1.44       scw 		printf("usp=%08x, ulr=%08x",
    573       1.44       scw 		    tf->tf_usr_sp, tf->tf_usr_lr);
    574       1.44       scw 	else
    575       1.44       scw 		printf("ssp=%08x, slr=%08x",
    576       1.44       scw 		    tf->tf_svc_sp, tf->tf_svc_lr);
    577       1.44       scw 	printf(", pc =%08x\n\n", tf->tf_pc);
    578       1.34      matt 
    579       1.39       scw #if defined(DDB) || defined(KGDB)
    580       1.39       scw 	kdb_trap(T_FAULT, tf);
    581       1.34      matt #endif
    582       1.39       scw 	panic("Fatal abort");
    583       1.39       scw 	/*NOTREACHED*/
    584       1.39       scw }
    585       1.39       scw 
    586       1.39       scw /*
    587       1.39       scw  * dab_align() handles the following data aborts:
    588       1.39       scw  *
    589       1.39       scw  *  FAULT_ALIGN_0 - Alignment fault
    590       1.39       scw  *  FAULT_ALIGN_0 - Alignment fault
    591       1.39       scw  *
    592       1.39       scw  * These faults are fatal if they happen in kernel mode. Otherwise, we
    593       1.39       scw  * deliver a bus error to the process.
    594       1.39       scw  */
    595       1.39       scw static int
    596       1.39       scw dab_align(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi)
    597       1.39       scw {
    598       1.73     rmind 	struct pcb *pcb = lwp_getpcb(l);
    599       1.39       scw 
    600       1.39       scw 	/* Alignment faults are always fatal if they occur in kernel mode */
    601       1.39       scw 	if (!TRAP_USERMODE(tf))
    602       1.39       scw 		dab_fatal(tf, fsr, far, l, NULL);
    603       1.39       scw 
    604       1.39       scw 	/* pcb_onfault *must* be NULL at this point */
    605       1.73     rmind 	KDASSERT(pcb->pcb_onfault == NULL);
    606       1.39       scw 
    607       1.48       wiz 	/* See if the CPU state needs to be fixed up */
    608       1.39       scw 	(void) data_abort_fixup(tf, fsr, far, l);
    609       1.39       scw 
    610       1.39       scw 	/* Deliver a bus error signal to the process */
    611       1.39       scw 	KSI_INIT_TRAP(ksi);
    612       1.39       scw 	ksi->ksi_signo = SIGBUS;
    613       1.39       scw 	ksi->ksi_code = BUS_ADRALN;
    614       1.39       scw 	ksi->ksi_addr = (u_int32_t *)(intptr_t)far;
    615       1.39       scw 	ksi->ksi_trap = fsr;
    616       1.39       scw 
    617       1.73     rmind 	pcb->pcb_tf = tf;
    618       1.39       scw 
    619       1.39       scw 	return (1);
    620       1.39       scw }
    621       1.39       scw 
    622       1.39       scw /*
    623       1.39       scw  * dab_buserr() handles the following data aborts:
    624       1.39       scw  *
    625       1.39       scw  *  FAULT_BUSERR_0 - External Abort on Linefetch -- Section
    626       1.39       scw  *  FAULT_BUSERR_1 - External Abort on Linefetch -- Page
    627       1.39       scw  *  FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
    628       1.39       scw  *  FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
    629       1.39       scw  *  FAULT_BUSTRNL1 - External abort on Translation -- Level 1
    630       1.39       scw  *  FAULT_BUSTRNL2 - External abort on Translation -- Level 2
    631       1.39       scw  *
    632       1.39       scw  * If pcb_onfault is set, flag the fault and return to the handler.
    633       1.39       scw  * If the fault occurred in user mode, give the process a SIGBUS.
    634       1.39       scw  *
    635       1.39       scw  * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
    636       1.39       scw  * can be flagged as imprecise in the FSR. This causes a real headache
    637       1.39       scw  * since some of the machine state is lost. In this case, tf->tf_pc
    638       1.39       scw  * may not actually point to the offending instruction. In fact, if
    639       1.39       scw  * we've taken a double abort fault, it generally points somewhere near
    640       1.39       scw  * the top of "data_abort_entry" in exception.S.
    641       1.39       scw  *
    642       1.39       scw  * In all other cases, these data aborts are considered fatal.
    643       1.39       scw  */
    644       1.39       scw static int
    645       1.39       scw dab_buserr(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l,
    646       1.39       scw     ksiginfo_t *ksi)
    647       1.39       scw {
    648       1.73     rmind 	struct pcb *pcb = lwp_getpcb(l);
    649       1.39       scw 
    650       1.39       scw #ifdef __XSCALE__
    651       1.39       scw 	if ((fsr & FAULT_IMPRECISE) != 0 &&
    652       1.39       scw 	    (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
    653       1.39       scw 		/*
    654       1.39       scw 		 * Oops, an imprecise, double abort fault. We've lost the
    655       1.39       scw 		 * r14_abt/spsr_abt values corresponding to the original
    656       1.39       scw 		 * abort, and the spsr saved in the trapframe indicates
    657       1.39       scw 		 * ABT mode.
    658       1.39       scw 		 */
    659       1.39       scw 		tf->tf_spsr &= ~PSR_MODE;
    660       1.39       scw 
    661       1.39       scw 		/*
    662       1.39       scw 		 * We use a simple heuristic to determine if the double abort
    663       1.39       scw 		 * happened as a result of a kernel or user mode access.
    664       1.39       scw 		 * If the current trapframe is at the top of the kernel stack,
    665       1.39       scw 		 * the fault _must_ have come from user mode.
    666       1.39       scw 		 */
    667       1.39       scw 		if (tf != ((trapframe_t *)pcb->pcb_un.un_32.pcb32_sp) - 1) {
    668       1.39       scw 			/*
    669       1.39       scw 			 * Kernel mode. We're either about to die a
    670       1.39       scw 			 * spectacular death, or pcb_onfault will come
    671       1.39       scw 			 * to our rescue. Either way, the current value
    672       1.39       scw 			 * of tf->tf_pc is irrelevant.
    673       1.39       scw 			 */
    674       1.39       scw 			tf->tf_spsr |= PSR_SVC32_MODE;
    675       1.39       scw 			if (pcb->pcb_onfault == NULL)
    676       1.39       scw 				printf("\nKernel mode double abort!\n");
    677       1.39       scw 		} else {
    678       1.39       scw 			/*
    679       1.39       scw 			 * User mode. We've lost the program counter at the
    680       1.39       scw 			 * time of the fault (not that it was accurate anyway;
    681       1.39       scw 			 * it's not called an imprecise fault for nothing).
    682       1.39       scw 			 * About all we can do is copy r14_usr to tf_pc and
    683       1.39       scw 			 * hope for the best. The process is about to get a
    684       1.39       scw 			 * SIGBUS, so it's probably history anyway.
    685       1.39       scw 			 */
    686       1.39       scw 			tf->tf_spsr |= PSR_USR32_MODE;
    687       1.39       scw 			tf->tf_pc = tf->tf_usr_lr;
    688       1.51  rearnsha #ifdef THUMB_CODE
    689       1.51  rearnsha 			tf->tf_spsr &= ~PSR_T_bit;
    690       1.51  rearnsha 			if (tf->tf_usr_lr & 1)
    691       1.51  rearnsha 				tf->tf_spsr |= PSR_T_bit;
    692       1.51  rearnsha #endif
    693       1.39       scw 		}
    694       1.39       scw 	}
    695       1.39       scw 
    696       1.39       scw 	/* FAR is invalid for imprecise exceptions */
    697       1.39       scw 	if ((fsr & FAULT_IMPRECISE) != 0)
    698       1.39       scw 		far = 0;
    699       1.39       scw #endif /* __XSCALE__ */
    700       1.39       scw 
    701       1.39       scw 	if (pcb->pcb_onfault) {
    702       1.39       scw 		KDASSERT(TRAP_USERMODE(tf) == 0);
    703       1.39       scw 		tf->tf_r0 = EFAULT;
    704       1.39       scw 		tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
    705       1.39       scw 		return (0);
    706       1.39       scw 	}
    707       1.39       scw 
    708       1.48       wiz 	/* See if the CPU state needs to be fixed up */
    709       1.39       scw 	(void) data_abort_fixup(tf, fsr, far, l);
    710       1.39       scw 
    711       1.39       scw 	/*
    712       1.39       scw 	 * At this point, if the fault happened in kernel mode, we're toast
    713       1.39       scw 	 */
    714       1.39       scw 	if (!TRAP_USERMODE(tf))
    715       1.39       scw 		dab_fatal(tf, fsr, far, l, NULL);
    716       1.39       scw 
    717       1.39       scw 	/* Deliver a bus error signal to the process */
    718       1.39       scw 	KSI_INIT_TRAP(ksi);
    719       1.39       scw 	ksi->ksi_signo = SIGBUS;
    720       1.39       scw 	ksi->ksi_code = BUS_ADRERR;
    721       1.39       scw 	ksi->ksi_addr = (u_int32_t *)(intptr_t)far;
    722       1.39       scw 	ksi->ksi_trap = fsr;
    723       1.39       scw 
    724       1.73     rmind 	pcb->pcb_tf = tf;
    725       1.27       scw 
    726       1.39       scw 	return (1);
    727        1.1     chris }
    728        1.1     chris 
    729       1.56     perry static inline int
    730       1.39       scw prefetch_abort_fixup(trapframe_t *tf)
    731       1.39       scw {
    732       1.39       scw #ifdef CPU_ABORT_FIXUP_REQUIRED
    733       1.39       scw 	int error;
    734       1.39       scw 
    735       1.48       wiz 	/* Call the CPU specific prefetch abort fixup routine */
    736       1.39       scw 	error = cpu_prefetchabt_fixup(tf);
    737       1.39       scw 	if (__predict_true(error != ABORT_FIXUP_FAILED))
    738       1.39       scw 		return (error);
    739       1.39       scw 
    740       1.39       scw 	/*
    741       1.39       scw 	 * Oops, couldn't fix up the instruction
    742       1.39       scw 	 */
    743  1.78.12.1       mrg 	printf("%s: fixup for %s mode prefetch abort failed.\n", __func__,
    744       1.39       scw 	    TRAP_USERMODE(tf) ? "user" : "kernel");
    745       1.51  rearnsha #ifdef THUMB_CODE
    746       1.51  rearnsha 	if (tf->tf_spsr & PSR_T_bit) {
    747       1.51  rearnsha 		printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ",
    748       1.75       wiz 		    tf->tf_pc, *((u_int16 *)(tf->tf_pc & ~1)),
    749       1.75       wiz 		    *((u_int16 *)((tf->tf_pc + 2) & ~1)));
    750       1.51  rearnsha 	}
    751       1.51  rearnsha 	else
    752       1.51  rearnsha #endif
    753       1.51  rearnsha 	{
    754       1.51  rearnsha 		printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
    755       1.51  rearnsha 		    *((u_int *)tf->tf_pc));
    756       1.51  rearnsha 	}
    757       1.39       scw 	disassemble(tf->tf_pc);
    758       1.39       scw 
    759       1.39       scw 	/* Die now if this happened in kernel mode */
    760       1.39       scw 	if (!TRAP_USERMODE(tf))
    761       1.39       scw 		dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
    762       1.39       scw 
    763       1.39       scw 	return (error);
    764       1.39       scw #else
    765       1.39       scw 	return (ABORT_FIXUP_OK);
    766       1.39       scw #endif /* CPU_ABORT_FIXUP_REQUIRED */
    767       1.39       scw }
    768        1.1     chris 
    769        1.1     chris /*
    770       1.39       scw  * void prefetch_abort_handler(trapframe_t *tf)
    771        1.1     chris  *
    772        1.1     chris  * Abort handler called when instruction execution occurs at
    773        1.1     chris  * a non existent or restricted (access permissions) memory page.
    774        1.1     chris  * If the address is invalid and we were in SVC mode then panic as
    775        1.1     chris  * the kernel should never prefetch abort.
    776        1.1     chris  * If the address is invalid and the page is mapped then the user process
    777        1.1     chris  * does no have read permission so send it a signal.
    778        1.1     chris  * Otherwise fault the page in and try again.
    779        1.1     chris  */
    780        1.1     chris void
    781       1.39       scw prefetch_abort_handler(trapframe_t *tf)
    782        1.1     chris {
    783       1.26   thorpej 	struct lwp *l;
    784       1.73     rmind 	struct pcb *pcb;
    785       1.14   thorpej 	struct vm_map *map;
    786       1.14   thorpej 	vaddr_t fault_pc, va;
    787       1.39       scw 	ksiginfo_t ksi;
    788       1.61        ad 	int error, user;
    789       1.39       scw 
    790       1.50  rearnsha 	UVMHIST_FUNC("prefetch_abort_handler"); UVMHIST_CALLED(maphist);
    791       1.50  rearnsha 
    792       1.39       scw 	/* Update vmmeter statistics */
    793       1.78      matt 	curcpu()->ci_data.cpu_ntrap++;
    794        1.1     chris 
    795       1.61        ad 	l = curlwp;
    796       1.73     rmind 	pcb = lwp_getpcb(l);
    797       1.61        ad 
    798       1.61        ad 	if ((user = TRAP_USERMODE(tf)) != 0)
    799       1.61        ad 		LWP_CACHE_CREDS(l, l->l_proc);
    800       1.61        ad 
    801        1.1     chris 	/*
    802        1.1     chris 	 * Enable IRQ's (disabled by the abort) This always comes
    803        1.1     chris 	 * from user mode so we know interrupts were not disabled.
    804        1.1     chris 	 * But we check anyway.
    805        1.1     chris 	 */
    806       1.72      matt 	KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
    807       1.72      matt 	if (__predict_true((tf->tf_spsr & I32_bit) != IF32_bits))
    808       1.72      matt 		restore_interrupts(tf->tf_spsr & IF32_bits);
    809        1.1     chris 
    810       1.48       wiz 	/* See if the CPU state needs to be fixed up */
    811       1.39       scw 	switch (prefetch_abort_fixup(tf)) {
    812       1.39       scw 	case ABORT_FIXUP_RETURN:
    813       1.72      matt 		KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
    814        1.1     chris 		return;
    815       1.39       scw 	case ABORT_FIXUP_FAILED:
    816       1.39       scw 		/* Deliver a SIGILL to the process */
    817       1.39       scw 		KSI_INIT_TRAP(&ksi);
    818       1.39       scw 		ksi.ksi_signo = SIGILL;
    819       1.39       scw 		ksi.ksi_code = ILL_ILLOPC;
    820       1.39       scw 		ksi.ksi_addr = (u_int32_t *)(intptr_t) tf->tf_pc;
    821       1.73     rmind 		pcb->pcb_tf = tf;
    822       1.39       scw 		goto do_trapsignal;
    823       1.39       scw 	default:
    824       1.39       scw 		break;
    825        1.1     chris 	}
    826        1.1     chris 
    827       1.39       scw 	/* Prefetch aborts cannot happen in kernel mode */
    828       1.61        ad 	if (__predict_false(!user))
    829       1.39       scw 		dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
    830        1.1     chris 
    831        1.4   thorpej 	/* Get fault address */
    832       1.39       scw 	fault_pc = tf->tf_pc;
    833       1.73     rmind 	pcb->pcb_tf = tf;
    834       1.50  rearnsha 	UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, tf=0x%x)", fault_pc, l, tf,
    835       1.50  rearnsha 	    0);
    836       1.14   thorpej 
    837        1.1     chris 	/* Ok validate the address, can only execute in USER space */
    838       1.39       scw 	if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS ||
    839       1.39       scw 	    (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
    840       1.35   thorpej 		KSI_INIT_TRAP(&ksi);
    841       1.34      matt 		ksi.ksi_signo = SIGSEGV;
    842       1.34      matt 		ksi.ksi_code = SEGV_ACCERR;
    843       1.39       scw 		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
    844       1.34      matt 		ksi.ksi_trap = fault_pc;
    845       1.39       scw 		goto do_trapsignal;
    846       1.39       scw 	}
    847       1.34      matt 
    848       1.39       scw 	map = &l->l_proc->p_vmspace->vm_map;
    849       1.39       scw 	va = trunc_page(fault_pc);
    850        1.1     chris 
    851       1.27       scw 	/*
    852       1.27       scw 	 * See if the pmap can handle this fault on its own...
    853       1.27       scw 	 */
    854       1.39       scw #ifdef DEBUG
    855       1.39       scw 	last_fault_code = -1;
    856       1.39       scw #endif
    857       1.50  rearnsha 	if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1)) {
    858       1.50  rearnsha 		UVMHIST_LOG (maphist, " <- emulated", 0, 0, 0, 0);
    859       1.39       scw 		goto out;
    860       1.50  rearnsha 	}
    861       1.27       scw 
    862       1.39       scw #ifdef DIAGNOSTIC
    863       1.67      matt 	if (__predict_false(l->l_cpu->ci_intr_depth > 0)) {
    864       1.39       scw 		printf("\nNon-emulated prefetch abort with intr_depth > 0\n");
    865       1.39       scw 		dab_fatal(tf, 0, tf->tf_pc, NULL, NULL);
    866       1.39       scw 	}
    867        1.1     chris #endif
    868       1.72      matt 
    869       1.72      matt #ifdef KERN_SA
    870       1.72      matt 	if (map != kernel_map && (l->l_flag & LW_SA)) {
    871       1.70  wrstuden 		l->l_savp->savp_faultaddr = fault_pc;
    872       1.70  wrstuden 		l->l_pflag |= LP_SA_PAGEFAULT;
    873       1.70  wrstuden 	}
    874       1.72      matt #endif
    875       1.70  wrstuden 
    876       1.76       chs 	KASSERT(pcb->pcb_onfault == NULL);
    877       1.57        he 	error = uvm_fault(map, va, VM_PROT_READ);
    878       1.53      joff 
    879       1.72      matt #ifdef KERN_SA
    880       1.70  wrstuden 	if (map != kernel_map)
    881       1.70  wrstuden 		l->l_pflag &= ~LP_SA_PAGEFAULT;
    882       1.72      matt #endif
    883       1.70  wrstuden 
    884       1.50  rearnsha 	if (__predict_true(error == 0)) {
    885       1.50  rearnsha 		UVMHIST_LOG (maphist, " <- uvm", 0, 0, 0, 0);
    886       1.39       scw 		goto out;
    887       1.50  rearnsha 	}
    888       1.43       scw 	KSI_INIT_TRAP(&ksi);
    889       1.43       scw 
    890       1.50  rearnsha 	UVMHIST_LOG (maphist, " <- fatal (%d)", error, 0, 0, 0);
    891       1.39       scw 	if (error == ENOMEM) {
    892       1.39       scw 		printf("UVM: pid %d (%s), uid %d killed: "
    893       1.39       scw 		    "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm,
    894       1.62        ad 		    l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1);
    895       1.43       scw 		ksi.ksi_signo = SIGKILL;
    896       1.43       scw 	} else
    897       1.43       scw 		ksi.ksi_signo = SIGSEGV;
    898        1.1     chris 
    899       1.39       scw 	ksi.ksi_code = SEGV_MAPERR;
    900       1.39       scw 	ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
    901       1.39       scw 	ksi.ksi_trap = fault_pc;
    902       1.39       scw 
    903       1.39       scw do_trapsignal:
    904       1.39       scw 	call_trapsignal(l, &ksi);
    905       1.39       scw 
    906       1.39       scw out:
    907       1.72      matt 	KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0);
    908       1.39       scw 	userret(l);
    909       1.39       scw }
    910       1.39       scw 
    911       1.39       scw /*
    912       1.39       scw  * Tentatively read an 8, 16, or 32-bit value from 'addr'.
    913       1.39       scw  * If the read succeeds, the value is written to 'rptr' and zero is returned.
    914       1.39       scw  * Else, return EFAULT.
    915       1.39       scw  */
    916       1.39       scw int
    917       1.39       scw badaddr_read(void *addr, size_t size, void *rptr)
    918       1.39       scw {
    919       1.39       scw 	extern int badaddr_read_1(const uint8_t *, uint8_t *);
    920       1.39       scw 	extern int badaddr_read_2(const uint16_t *, uint16_t *);
    921       1.39       scw 	extern int badaddr_read_4(const uint32_t *, uint32_t *);
    922       1.39       scw 	union {
    923       1.39       scw 		uint8_t v1;
    924       1.39       scw 		uint16_t v2;
    925       1.39       scw 		uint32_t v4;
    926       1.39       scw 	} u;
    927       1.47       scw 	struct pcb *curpcb_save;
    928       1.47       scw 	int rv, s;
    929       1.39       scw 
    930       1.39       scw 	cpu_drain_writebuf();
    931       1.39       scw 
    932       1.47       scw 	/*
    933       1.47       scw 	 * We might be called at interrupt time, so arrange to steal
    934       1.47       scw 	 * lwp0's PCB temporarily, if required, so that pcb_onfault
    935       1.47       scw 	 * handling works correctly.
    936       1.47       scw 	 */
    937       1.47       scw 	s = splhigh();
    938       1.47       scw 	if ((curpcb_save = curpcb) == NULL)
    939       1.73     rmind 		curpcb = lwp_getpcb(&lwp0);
    940       1.47       scw 
    941       1.39       scw 	/* Read from the test address. */
    942       1.39       scw 	switch (size) {
    943       1.39       scw 	case sizeof(uint8_t):
    944       1.39       scw 		rv = badaddr_read_1(addr, &u.v1);
    945       1.39       scw 		if (rv == 0 && rptr)
    946       1.39       scw 			*(uint8_t *) rptr = u.v1;
    947       1.39       scw 		break;
    948       1.39       scw 
    949       1.39       scw 	case sizeof(uint16_t):
    950       1.39       scw 		rv = badaddr_read_2(addr, &u.v2);
    951       1.39       scw 		if (rv == 0 && rptr)
    952       1.39       scw 			*(uint16_t *) rptr = u.v2;
    953       1.39       scw 		break;
    954       1.39       scw 
    955       1.39       scw 	case sizeof(uint32_t):
    956       1.39       scw 		rv = badaddr_read_4(addr, &u.v4);
    957       1.39       scw 		if (rv == 0 && rptr)
    958       1.39       scw 			*(uint32_t *) rptr = u.v4;
    959       1.39       scw 		break;
    960       1.39       scw 
    961       1.39       scw 	default:
    962       1.47       scw 		curpcb = curpcb_save;
    963  1.78.12.1       mrg 		panic("%s: invalid size (%lu)", __func__, (u_long)size);
    964       1.34      matt 	}
    965       1.39       scw 
    966       1.47       scw 	/* Restore curpcb */
    967       1.47       scw 	curpcb = curpcb_save;
    968       1.47       scw 	splx(s);
    969       1.47       scw 
    970       1.39       scw 	/* Return EFAULT if the address was invalid, else zero */
    971       1.39       scw 	return (rv);
    972        1.1     chris }
    973