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