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