Home | History | Annotate | Line # | Download | only in aarch64
trap.c revision 1.35
      1  1.35  riastrad /* $NetBSD: trap.c,v 1.35 2020/08/01 02:06:59 riastradh Exp $ */
      2   1.1      matt 
      3   1.1      matt /*-
      4   1.1      matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5   1.1      matt  * All rights reserved.
      6   1.1      matt  *
      7   1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      matt  * by Matt Thomas of 3am Software Foundry.
      9   1.1      matt  *
     10   1.1      matt  * Redistribution and use in source and binary forms, with or without
     11   1.1      matt  * modification, are permitted provided that the following conditions
     12   1.1      matt  * are met:
     13   1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14   1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15   1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      matt  *    documentation and/or other materials provided with the distribution.
     18   1.1      matt  *
     19   1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      matt  */
     31   1.1      matt 
     32   1.1      matt #include <sys/cdefs.h>
     33   1.1      matt 
     34  1.35  riastrad __KERNEL_RCSID(1, "$NetBSD: trap.c,v 1.35 2020/08/01 02:06:59 riastradh Exp $");
     35   1.1      matt 
     36   1.4       ryo #include "opt_arm_intr_impl.h"
     37   1.4       ryo #include "opt_compat_netbsd32.h"
     38  1.22  jmcneill #include "opt_dtrace.h"
     39   1.4       ryo 
     40   1.1      matt #include <sys/param.h>
     41   1.8       ryo #include <sys/kauth.h>
     42   1.1      matt #include <sys/types.h>
     43   1.4       ryo #include <sys/atomic.h>
     44   1.1      matt #include <sys/cpu.h>
     45  1.28       ryo #include <sys/evcnt.h>
     46   1.4       ryo #ifdef KDB
     47   1.4       ryo #include <sys/kdb.h>
     48   1.4       ryo #endif
     49   1.3  nisimura #include <sys/proc.h>
     50   1.3  nisimura #include <sys/systm.h>
     51   1.3  nisimura #include <sys/signal.h>
     52   1.3  nisimura #include <sys/signalvar.h>
     53   1.3  nisimura #include <sys/siginfo.h>
     54  1.28       ryo #include <sys/xcall.h>
     55   1.1      matt 
     56   1.4       ryo #ifdef ARM_INTR_IMPL
     57   1.4       ryo #include ARM_INTR_IMPL
     58   1.4       ryo #else
     59   1.4       ryo #error ARM_INTR_IMPL not defined
     60   1.4       ryo #endif
     61   1.4       ryo 
     62   1.4       ryo #ifndef ARM_IRQ_HANDLER
     63   1.4       ryo #error ARM_IRQ_HANDLER not defined
     64   1.4       ryo #endif
     65   1.4       ryo 
     66   1.4       ryo #include <aarch64/userret.h>
     67   1.4       ryo #include <aarch64/frame.h>
     68   1.4       ryo #include <aarch64/machdep.h>
     69   1.4       ryo #include <aarch64/armreg.h>
     70   1.1      matt #include <aarch64/locore.h>
     71   1.1      matt 
     72   1.4       ryo #ifdef KDB
     73   1.4       ryo #include <machine/db_machdep.h>
     74   1.4       ryo #endif
     75   1.4       ryo #ifdef DDB
     76   1.4       ryo #include <ddb/db_output.h>
     77   1.4       ryo #include <machine/db_machdep.h>
     78   1.4       ryo #endif
     79  1.22  jmcneill #ifdef KDTRACE_HOOKS
     80  1.22  jmcneill #include <sys/dtrace_bsd.h>
     81  1.22  jmcneill #endif
     82   1.4       ryo 
     83   1.8       ryo #ifdef DDB
     84   1.8       ryo int sigill_debug = 0;
     85   1.8       ryo #endif
     86   1.4       ryo 
     87  1.22  jmcneill #ifdef KDTRACE_HOOKS
     88  1.22  jmcneill dtrace_doubletrap_func_t	dtrace_doubletrap_func = NULL;
     89  1.22  jmcneill dtrace_trap_func_t		dtrace_trap_func = NULL;
     90  1.22  jmcneill int (*dtrace_invop_jump_addr)(struct trapframe *);
     91  1.22  jmcneill #endif
     92  1.22  jmcneill 
     93  1.28       ryo enum emul_arm_result {
     94  1.28       ryo 	EMUL_ARM_SUCCESS = 0,
     95  1.28       ryo 	EMUL_ARM_UNKNOWN,
     96  1.28       ryo 	EMUL_ARM_FAULT,
     97  1.28       ryo };
     98  1.28       ryo 
     99   1.4       ryo const char * const trap_names[] = {
    100   1.4       ryo 	[ESR_EC_UNKNOWN]	= "Unknown Reason (Illegal Instruction)",
    101   1.4       ryo 	[ESR_EC_SERROR]		= "SError Interrupt",
    102   1.4       ryo 	[ESR_EC_WFX]		= "WFI or WFE instruction execution",
    103   1.4       ryo 	[ESR_EC_ILL_STATE]	= "Illegal Execution State",
    104   1.4       ryo 
    105  1.25      maxv 	[ESR_EC_BTE_A64]	= "Branch Target Exception",
    106  1.25      maxv 
    107   1.4       ryo 	[ESR_EC_SYS_REG]	= "MSR/MRS/SYS instruction",
    108   1.4       ryo 	[ESR_EC_SVC_A64]	= "SVC Instruction Execution",
    109   1.4       ryo 	[ESR_EC_HVC_A64]	= "HVC Instruction Execution",
    110   1.4       ryo 	[ESR_EC_SMC_A64]	= "SMC Instruction Execution",
    111   1.4       ryo 
    112   1.4       ryo 	[ESR_EC_INSN_ABT_EL0]	= "Instruction Abort (EL0)",
    113   1.4       ryo 	[ESR_EC_INSN_ABT_EL1]	= "Instruction Abort (EL1)",
    114   1.4       ryo 	[ESR_EC_DATA_ABT_EL0]	= "Data Abort (EL0)",
    115   1.4       ryo 	[ESR_EC_DATA_ABT_EL1]	= "Data Abort (EL1)",
    116   1.4       ryo 
    117   1.4       ryo 	[ESR_EC_PC_ALIGNMENT]	= "Misaligned PC",
    118   1.4       ryo 	[ESR_EC_SP_ALIGNMENT]	= "Misaligned SP",
    119   1.4       ryo 
    120   1.4       ryo 	[ESR_EC_FP_ACCESS]	= "Access to SIMD/FP Registers",
    121   1.4       ryo 	[ESR_EC_FP_TRAP_A64]	= "FP Exception",
    122   1.4       ryo 
    123   1.4       ryo 	[ESR_EC_BRKPNT_EL0]	= "Breakpoint Exception (EL0)",
    124   1.4       ryo 	[ESR_EC_BRKPNT_EL1]	= "Breakpoint Exception (EL1)",
    125   1.4       ryo 	[ESR_EC_SW_STEP_EL0]	= "Software Step (EL0)",
    126   1.4       ryo 	[ESR_EC_SW_STEP_EL1]	= "Software Step (EL1)",
    127   1.4       ryo 	[ESR_EC_WTCHPNT_EL0]	= "Watchpoint (EL0)",
    128   1.4       ryo 	[ESR_EC_WTCHPNT_EL1]	= "Watchpoint (EL1)",
    129   1.4       ryo 	[ESR_EC_BKPT_INSN_A64]	= "BKPT Instruction Execution",
    130   1.4       ryo 
    131   1.4       ryo 	[ESR_EC_CP15_RT]	= "A32: MCR/MRC access to CP15",
    132   1.4       ryo 	[ESR_EC_CP15_RRT]	= "A32: MCRR/MRRC access to CP15",
    133   1.4       ryo 	[ESR_EC_CP14_RT]	= "A32: MCR/MRC access to CP14",
    134   1.4       ryo 	[ESR_EC_CP14_DT]	= "A32: LDC/STC access to CP14",
    135   1.4       ryo 	[ESR_EC_CP14_RRT]	= "A32: MRRC access to CP14",
    136   1.4       ryo 	[ESR_EC_SVC_A32]	= "A32: SVC Instruction Execution",
    137   1.4       ryo 	[ESR_EC_HVC_A32]	= "A32: HVC Instruction Execution",
    138   1.4       ryo 	[ESR_EC_SMC_A32]	= "A32: SMC Instruction Execution",
    139   1.4       ryo 	[ESR_EC_FPID]		= "A32: MCR/MRC access to CP10",
    140   1.4       ryo 	[ESR_EC_FP_TRAP_A32]	= "A32: FP Exception",
    141   1.4       ryo 	[ESR_EC_BKPT_INSN_A32]	= "A32: BKPT Instruction Execution",
    142   1.4       ryo 	[ESR_EC_VECTOR_CATCH]	= "A32: Vector Catch Exception"
    143   1.4       ryo };
    144   1.4       ryo 
    145   1.6  christos const char *
    146   1.4       ryo eclass_trapname(uint32_t eclass)
    147   1.3  nisimura {
    148   1.4       ryo 	static char trapnamebuf[sizeof("Unknown trap 0x????????")];
    149   1.4       ryo 
    150   1.4       ryo 	if (eclass >= __arraycount(trap_names) || trap_names[eclass] == NULL) {
    151   1.4       ryo 		snprintf(trapnamebuf, sizeof(trapnamebuf),
    152   1.6  christos 		    "Unknown trap %#02x", eclass);
    153   1.4       ryo 		return trapnamebuf;
    154   1.4       ryo 	}
    155   1.4       ryo 	return trap_names[eclass];
    156   1.3  nisimura }
    157   1.3  nisimura 
    158   1.1      matt void
    159   1.4       ryo userret(struct lwp *l)
    160   1.1      matt {
    161   1.1      matt 	mi_userret(l);
    162   1.1      matt }
    163   1.2  nisimura 
    164   1.3  nisimura void
    165   1.4       ryo trap_doast(struct trapframe *tf)
    166   1.3  nisimura {
    167   1.3  nisimura 	struct lwp * const l = curlwp;
    168   1.4       ryo 
    169   1.4       ryo 	/*
    170   1.4       ryo 	 * allow to have a chance of context switch just prior to user
    171   1.4       ryo 	 * exception return.
    172   1.4       ryo 	 */
    173   1.4       ryo #ifdef __HAVE_PREEMPTION
    174   1.4       ryo 	kpreempt_disable();
    175   1.4       ryo #endif
    176   1.4       ryo 	struct cpu_info * const ci = curcpu();
    177   1.4       ryo 
    178   1.4       ryo 	ci->ci_data.cpu_ntrap++;
    179   1.4       ryo 
    180   1.4       ryo 	KDASSERT(ci->ci_cpl == IPL_NONE);
    181   1.4       ryo #ifdef __HAVE_PREEMPTION
    182   1.4       ryo 	kpreempt_enable();
    183   1.4       ryo #endif
    184   1.4       ryo 
    185   1.4       ryo 	if (l->l_pflag & LP_OWEUPC) {
    186   1.4       ryo 		l->l_pflag &= ~LP_OWEUPC;
    187   1.4       ryo 		ADDUPROF(l);
    188   1.3  nisimura 	}
    189   1.4       ryo 
    190   1.4       ryo 	userret(l);
    191   1.4       ryo }
    192   1.4       ryo 
    193   1.4       ryo void
    194   1.4       ryo trap_el1h_sync(struct trapframe *tf)
    195   1.4       ryo {
    196   1.4       ryo 	const uint32_t esr = tf->tf_esr;
    197   1.4       ryo 	const uint32_t eclass = __SHIFTOUT(esr, ESR_EC); /* exception class */
    198   1.4       ryo 
    199   1.4       ryo 	/* re-enable traps and interrupts */
    200   1.4       ryo 	if (!(tf->tf_spsr & SPSR_I))
    201   1.4       ryo 		daif_enable(DAIF_D|DAIF_A|DAIF_I|DAIF_F);
    202   1.4       ryo 	else
    203   1.4       ryo 		daif_enable(DAIF_D|DAIF_A);
    204   1.4       ryo 
    205  1.22  jmcneill #ifdef KDTRACE_HOOKS
    206  1.22  jmcneill 	if (dtrace_trap_func != NULL && (*dtrace_trap_func)(tf, eclass))
    207  1.22  jmcneill 		return;
    208  1.22  jmcneill #endif
    209  1.22  jmcneill 
    210   1.4       ryo 	switch (eclass) {
    211   1.4       ryo 	case ESR_EC_INSN_ABT_EL1:
    212   1.4       ryo 	case ESR_EC_DATA_ABT_EL1:
    213   1.6  christos 		data_abort_handler(tf, eclass);
    214   1.4       ryo 		break;
    215   1.4       ryo 
    216  1.22  jmcneill 	case ESR_EC_BKPT_INSN_A64:
    217  1.22  jmcneill #ifdef KDTRACE_HOOKS
    218  1.22  jmcneill 		if (__SHIFTOUT(esr, ESR_ISS) == 0x40d &&
    219  1.22  jmcneill 		    dtrace_invop_jump_addr != 0) {
    220  1.22  jmcneill 			(*dtrace_invop_jump_addr)(tf);
    221  1.22  jmcneill 			break;
    222  1.22  jmcneill 		}
    223  1.22  jmcneill 		/* FALLTHROUGH */
    224  1.22  jmcneill #endif
    225   1.4       ryo 	case ESR_EC_BRKPNT_EL1:
    226   1.4       ryo 	case ESR_EC_SW_STEP_EL1:
    227   1.4       ryo 	case ESR_EC_WTCHPNT_EL1:
    228   1.4       ryo #ifdef DDB
    229   1.4       ryo 		if (eclass == ESR_EC_BRKPNT_EL1)
    230   1.4       ryo 			kdb_trap(DB_TRAP_BREAKPOINT, tf);
    231   1.4       ryo 		else if (eclass == ESR_EC_BKPT_INSN_A64)
    232   1.4       ryo 			kdb_trap(DB_TRAP_BKPT_INSN, tf);
    233   1.4       ryo 		else if (eclass == ESR_EC_WTCHPNT_EL1)
    234   1.4       ryo 			kdb_trap(DB_TRAP_WATCHPOINT, tf);
    235   1.4       ryo 		else if (eclass == ESR_EC_SW_STEP_EL1)
    236   1.4       ryo 			kdb_trap(DB_TRAP_SW_STEP, tf);
    237   1.4       ryo 		else
    238   1.4       ryo 			kdb_trap(DB_TRAP_UNKNOWN, tf);
    239   1.4       ryo #else
    240   1.4       ryo 		panic("No debugger in kernel");
    241   1.4       ryo #endif
    242   1.4       ryo 		break;
    243   1.4       ryo 
    244   1.4       ryo 	case ESR_EC_FP_ACCESS:
    245  1.35  riastrad 		if ((curlwp->l_flag & (LW_SYSTEM|LW_SYSTEM_FPU)) ==
    246  1.35  riastrad 		    (LW_SYSTEM|LW_SYSTEM_FPU)) {
    247  1.35  riastrad 			fpu_load(curlwp);
    248  1.35  riastrad 			break;
    249  1.35  riastrad 		}
    250  1.35  riastrad 		/*FALLTHROUGH*/
    251   1.4       ryo 	case ESR_EC_FP_TRAP_A64:
    252   1.4       ryo 	case ESR_EC_PC_ALIGNMENT:
    253   1.4       ryo 	case ESR_EC_SP_ALIGNMENT:
    254   1.4       ryo 	case ESR_EC_ILL_STATE:
    255  1.27      maxv 	case ESR_EC_BTE_A64:
    256   1.4       ryo 	default:
    257  1.13       ryo 		panic("Trap: fatal %s: pc=%016" PRIx64 " sp=%016" PRIx64
    258  1.13       ryo 		    " esr=%08x", eclass_trapname(eclass), tf->tf_pc, tf->tf_sp,
    259   1.6  christos 		    esr);
    260   1.4       ryo 		break;
    261   1.3  nisimura 	}
    262   1.3  nisimura }
    263   1.3  nisimura 
    264  1.28       ryo /*
    265  1.28       ryo  * There are some systems with different cache line sizes for each cpu.
    266  1.28       ryo  * Userland programs can be preempted between CPUs at any time, so in such
    267  1.28       ryo  * a system, the minimum cache line size must be visible to userland.
    268  1.28       ryo  */
    269  1.28       ryo #define CTR_EL0_USR_MASK	\
    270  1.28       ryo 	(CTR_EL0_DIC | CTR_EL0_IDC | CTR_EL0_DMIN_LINE | CTR_EL0_IMIN_LINE)
    271  1.28       ryo uint64_t ctr_el0_usr __read_mostly;
    272  1.28       ryo 
    273  1.28       ryo static xcfunc_t
    274  1.28       ryo configure_cpu_traps0(void *arg1, void *arg2)
    275  1.28       ryo {
    276  1.28       ryo 	struct cpu_info * const ci = curcpu();
    277  1.28       ryo 	uint64_t sctlr;
    278  1.28       ryo 	uint64_t ctr_el0_raw = reg_ctr_el0_read();
    279  1.28       ryo 
    280  1.28       ryo #ifdef DEBUG_FORCE_TRAP_CTR_EL0
    281  1.28       ryo 	goto need_ctr_trap;
    282  1.28       ryo #endif
    283  1.28       ryo 
    284  1.28       ryo 	if ((__SHIFTOUT(ctr_el0_raw, CTR_EL0_DMIN_LINE) >
    285  1.28       ryo 	     __SHIFTOUT(ctr_el0_usr, CTR_EL0_DMIN_LINE)) ||
    286  1.28       ryo 	    (__SHIFTOUT(ctr_el0_raw, CTR_EL0_IMIN_LINE) >
    287  1.28       ryo 	     __SHIFTOUT(ctr_el0_usr, CTR_EL0_IMIN_LINE)))
    288  1.28       ryo 		goto need_ctr_trap;
    289  1.28       ryo 
    290  1.28       ryo 	if ((__SHIFTOUT(ctr_el0_raw, CTR_EL0_DIC) == 1 &&
    291  1.28       ryo 	     __SHIFTOUT(ctr_el0_usr, CTR_EL0_DIC) == 0) ||
    292  1.28       ryo 	    (__SHIFTOUT(ctr_el0_raw, CTR_EL0_IDC) == 1 &&
    293  1.28       ryo 	     __SHIFTOUT(ctr_el0_usr, CTR_EL0_IDC) == 0))
    294  1.28       ryo 		goto need_ctr_trap;
    295  1.28       ryo 
    296  1.28       ryo #if 0 /* XXX: To do or not to do */
    297  1.28       ryo 	/*
    298  1.28       ryo 	 * IDC==0, but (LoC==0 || LoUIS==LoUU==0)?
    299  1.28       ryo 	 * Would it be better to show IDC=1 to userland?
    300  1.28       ryo 	 */
    301  1.28       ryo 	if (__SHIFTOUT(ctr_el0_raw, CTR_EL0_IDC) == 0 &&
    302  1.28       ryo 	    __SHIFTOUT(ctr_el0_usr, CTR_EL0_IDC) == 1)
    303  1.28       ryo 		goto need_ctr_trap;
    304  1.28       ryo #endif
    305  1.28       ryo 
    306  1.28       ryo 	return 0;
    307  1.28       ryo 
    308  1.28       ryo  need_ctr_trap:
    309  1.28       ryo 	evcnt_attach_dynamic(&ci->ci_uct_trap, EVCNT_TYPE_MISC, NULL,
    310  1.28       ryo 	    ci->ci_cpuname, "ctr_el0 trap");
    311  1.28       ryo 
    312  1.28       ryo 	/* trap CTR_EL0 access from EL0 on this cpu */
    313  1.28       ryo 	sctlr = reg_sctlr_el1_read();
    314  1.28       ryo 	sctlr &= ~SCTLR_UCT;
    315  1.28       ryo 	reg_sctlr_el1_write(sctlr);
    316  1.28       ryo 
    317  1.28       ryo 	return 0;
    318  1.28       ryo }
    319  1.28       ryo 
    320  1.28       ryo void
    321  1.28       ryo configure_cpu_traps(void)
    322  1.28       ryo {
    323  1.28       ryo 	CPU_INFO_ITERATOR cii;
    324  1.28       ryo 	struct cpu_info *ci;
    325  1.28       ryo 	uint64_t where;
    326  1.28       ryo 
    327  1.28       ryo 	/* remember minimum cache line size out of all CPUs */
    328  1.28       ryo 	for (CPU_INFO_FOREACH(cii, ci)) {
    329  1.28       ryo 		uint64_t ctr_el0_cpu = ci->ci_id.ac_ctr;
    330  1.28       ryo 		uint64_t clidr = ci->ci_id.ac_clidr;
    331  1.28       ryo 
    332  1.28       ryo 		if (__SHIFTOUT(clidr, CLIDR_LOC) == 0 ||
    333  1.28       ryo 		    (__SHIFTOUT(clidr, CLIDR_LOUIS) == 0 &&
    334  1.28       ryo 		     __SHIFTOUT(clidr, CLIDR_LOUU) == 0)) {
    335  1.28       ryo 			/* this means the same as IDC=1 */
    336  1.28       ryo 			ctr_el0_cpu |= CTR_EL0_IDC;
    337  1.28       ryo 		}
    338  1.28       ryo 
    339  1.28       ryo 		/*
    340  1.28       ryo 		 * if DIC==1, there is no need to icache sync. however,
    341  1.28       ryo 		 * to calculate the minimum cacheline, in this case
    342  1.28       ryo 		 * ICacheLine is treated as the maximum.
    343  1.28       ryo 		 */
    344  1.28       ryo 		if (__SHIFTOUT(ctr_el0_cpu, CTR_EL0_DIC) == 1)
    345  1.28       ryo 			ctr_el0_cpu |= CTR_EL0_IMIN_LINE;
    346  1.28       ryo 
    347  1.29       ryo 		/* Neoverse N1 erratum 1542419 */
    348  1.29       ryo 		if (CPU_ID_NEOVERSEN1_P(ci->ci_id.ac_midr) &&
    349  1.29       ryo 		    __SHIFTOUT(ctr_el0_cpu, CTR_EL0_DIC) == 1)
    350  1.29       ryo 			ctr_el0_cpu &= ~CTR_EL0_DIC;
    351  1.29       ryo 
    352  1.28       ryo 		if (cii == 0) {
    353  1.28       ryo 			ctr_el0_usr = ctr_el0_cpu;
    354  1.28       ryo 			continue;
    355  1.28       ryo 		}
    356  1.28       ryo 
    357  1.28       ryo 		/* keep minimum cache line size, and worst DIC/IDC */
    358  1.28       ryo 		ctr_el0_usr &= (ctr_el0_cpu & CTR_EL0_DIC) | ~CTR_EL0_DIC;
    359  1.28       ryo 		ctr_el0_usr &= (ctr_el0_cpu & CTR_EL0_IDC) | ~CTR_EL0_IDC;
    360  1.28       ryo 		if (__SHIFTOUT(ctr_el0_cpu, CTR_EL0_DMIN_LINE) <
    361  1.28       ryo 		    __SHIFTOUT(ctr_el0_usr, CTR_EL0_DMIN_LINE)) {
    362  1.28       ryo 			ctr_el0_usr &= ~CTR_EL0_DMIN_LINE;
    363  1.28       ryo 			ctr_el0_usr |= ctr_el0_cpu & CTR_EL0_DMIN_LINE;
    364  1.28       ryo 		}
    365  1.28       ryo 		if ((ctr_el0_cpu & CTR_EL0_DIC) == 0 &&
    366  1.28       ryo 		    (__SHIFTOUT(ctr_el0_cpu, CTR_EL0_IMIN_LINE) <
    367  1.28       ryo 		    __SHIFTOUT(ctr_el0_usr, CTR_EL0_IMIN_LINE))) {
    368  1.28       ryo 			ctr_el0_usr &= ~CTR_EL0_IMIN_LINE;
    369  1.28       ryo 			ctr_el0_usr |= ctr_el0_cpu & CTR_EL0_IMIN_LINE;
    370  1.28       ryo 		}
    371  1.28       ryo 	}
    372  1.28       ryo 
    373  1.28       ryo 	where = xc_broadcast(0,
    374  1.28       ryo 	    (xcfunc_t)configure_cpu_traps0, NULL, NULL);
    375  1.28       ryo 	xc_wait(where);
    376  1.28       ryo }
    377  1.28       ryo 
    378  1.28       ryo static enum emul_arm_result
    379  1.28       ryo emul_aarch64_insn(struct trapframe *tf)
    380  1.28       ryo {
    381  1.28       ryo 	uint32_t insn;
    382  1.28       ryo 
    383  1.32       ryo 	if (ufetch_32((uint32_t *)tf->tf_pc, &insn)) {
    384  1.32       ryo 		tf->tf_far = reg_far_el1_read();
    385  1.28       ryo 		return EMUL_ARM_FAULT;
    386  1.32       ryo 	}
    387  1.28       ryo 
    388  1.28       ryo 	if ((insn & 0xffffffe0) == 0xd53b0020) {
    389  1.28       ryo 		/* mrs x?,ctr_el0 */
    390  1.28       ryo 		unsigned int Xt = insn & 31;
    391  1.28       ryo 		if (Xt != 31) {	/* !xzr */
    392  1.28       ryo 			uint64_t ctr_el0 = reg_ctr_el0_read();
    393  1.28       ryo 			ctr_el0 &= ~CTR_EL0_USR_MASK;
    394  1.28       ryo 			ctr_el0 |= (ctr_el0_usr & CTR_EL0_USR_MASK);
    395  1.28       ryo 			tf->tf_reg[Xt] = ctr_el0;
    396  1.28       ryo 		}
    397  1.28       ryo 		curcpu()->ci_uct_trap.ev_count++;
    398  1.28       ryo 
    399  1.28       ryo 	} else {
    400  1.28       ryo 		return EMUL_ARM_UNKNOWN;
    401  1.28       ryo 	}
    402  1.28       ryo 
    403  1.28       ryo 	tf->tf_pc += 4;
    404  1.28       ryo 	return EMUL_ARM_SUCCESS;
    405  1.28       ryo }
    406  1.28       ryo 
    407   1.3  nisimura void
    408   1.4       ryo trap_el0_sync(struct trapframe *tf)
    409   1.3  nisimura {
    410   1.4       ryo 	struct lwp * const l = curlwp;
    411   1.4       ryo 	const uint32_t esr = tf->tf_esr;
    412   1.4       ryo 	const uint32_t eclass = __SHIFTOUT(esr, ESR_EC); /* exception class */
    413   1.4       ryo 
    414  1.14       ryo 	/* disable trace */
    415  1.14       ryo 	reg_mdscr_el1_write(reg_mdscr_el1_read() & ~MDSCR_SS);
    416   1.4       ryo 	/* enable traps and interrupts */
    417   1.4       ryo 	daif_enable(DAIF_D|DAIF_A|DAIF_I|DAIF_F);
    418   1.4       ryo 
    419   1.4       ryo 	switch (eclass) {
    420   1.4       ryo 	case ESR_EC_INSN_ABT_EL0:
    421   1.4       ryo 	case ESR_EC_DATA_ABT_EL0:
    422   1.6  christos 		data_abort_handler(tf, eclass);
    423   1.4       ryo 		userret(l);
    424   1.4       ryo 		break;
    425   1.4       ryo 
    426   1.4       ryo 	case ESR_EC_SVC_A64:
    427   1.4       ryo 		(*l->l_proc->p_md.md_syscall)(tf);
    428   1.4       ryo 		break;
    429   1.4       ryo 	case ESR_EC_FP_ACCESS:
    430   1.4       ryo 		fpu_load(l);
    431   1.4       ryo 		userret(l);
    432   1.4       ryo 		break;
    433   1.4       ryo 	case ESR_EC_FP_TRAP_A64:
    434   1.4       ryo 		do_trapsignal(l, SIGFPE, FPE_FLTUND, NULL, esr); /* XXX */
    435   1.4       ryo 		userret(l);
    436   1.4       ryo 		break;
    437   1.4       ryo 
    438   1.4       ryo 	case ESR_EC_PC_ALIGNMENT:
    439   1.5  christos 		do_trapsignal(l, SIGBUS, BUS_ADRALN, (void *)tf->tf_pc, esr);
    440   1.4       ryo 		userret(l);
    441   1.4       ryo 		break;
    442   1.4       ryo 	case ESR_EC_SP_ALIGNMENT:
    443   1.5  christos 		do_trapsignal(l, SIGBUS, BUS_ADRALN, (void *)tf->tf_sp, esr);
    444   1.4       ryo 		userret(l);
    445   1.4       ryo 		break;
    446   1.4       ryo 
    447   1.4       ryo 	case ESR_EC_BKPT_INSN_A64:
    448   1.4       ryo 	case ESR_EC_BRKPNT_EL0:
    449   1.4       ryo 	case ESR_EC_WTCHPNT_EL0:
    450   1.5  christos 		do_trapsignal(l, SIGTRAP, TRAP_BRKPT, (void *)tf->tf_pc, esr);
    451   1.4       ryo 		userret(l);
    452   1.4       ryo 		break;
    453  1.14       ryo 	case ESR_EC_SW_STEP_EL0:
    454  1.14       ryo 		/* disable trace, and send trace trap */
    455  1.14       ryo 		tf->tf_spsr &= ~SPSR_SS;
    456  1.14       ryo 		do_trapsignal(l, SIGTRAP, TRAP_TRACE, (void *)tf->tf_pc, esr);
    457  1.14       ryo 		userret(l);
    458  1.14       ryo 		break;
    459   1.4       ryo 
    460  1.28       ryo 	case ESR_EC_SYS_REG:
    461  1.28       ryo 		switch (emul_aarch64_insn(tf)) {
    462  1.28       ryo 		case EMUL_ARM_SUCCESS:
    463  1.28       ryo 			break;
    464  1.28       ryo 		case EMUL_ARM_UNKNOWN:
    465  1.28       ryo 			goto unknown;
    466  1.28       ryo 		case EMUL_ARM_FAULT:
    467  1.28       ryo 			do_trapsignal(l, SIGSEGV, SEGV_MAPERR,
    468  1.32       ryo 			    (void *)tf->tf_far, esr);
    469  1.28       ryo 			break;
    470  1.28       ryo 		}
    471  1.28       ryo 		userret(l);
    472  1.28       ryo 		break;
    473  1.28       ryo 
    474   1.4       ryo 	default:
    475   1.4       ryo 	case ESR_EC_UNKNOWN:
    476  1.28       ryo  unknown:
    477   1.8       ryo #ifdef DDB
    478   1.8       ryo 		if (sigill_debug) {
    479   1.8       ryo 			/* show illegal instruction */
    480  1.11       ryo 			printf("TRAP: pid %d (%s), uid %d: %s:"
    481  1.11       ryo 			    " esr=0x%lx: pc=0x%lx: %s\n",
    482   1.8       ryo 			    curlwp->l_proc->p_pid, curlwp->l_proc->p_comm,
    483   1.8       ryo 			    l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1,
    484  1.11       ryo 			    eclass_trapname(eclass), tf->tf_esr, tf->tf_pc,
    485  1.31       ryo 			    strdisasm(tf->tf_pc, tf->tf_spsr));
    486   1.8       ryo 		}
    487   1.8       ryo #endif
    488   1.4       ryo 		/* illegal or not implemented instruction */
    489   1.5  christos 		do_trapsignal(l, SIGILL, ILL_ILLTRP, (void *)tf->tf_pc, esr);
    490   1.4       ryo 		userret(l);
    491   1.4       ryo 		break;
    492   1.4       ryo 	}
    493   1.3  nisimura }
    494   1.3  nisimura 
    495   1.4       ryo void
    496   1.4       ryo interrupt(struct trapframe *tf)
    497   1.4       ryo {
    498   1.4       ryo 	struct cpu_info * const ci = curcpu();
    499   1.2  nisimura 
    500  1.12       ryo #ifdef STACKCHECKS
    501  1.12       ryo 	struct lwp *l = curlwp;
    502  1.12       ryo 	void *sp = (void *)reg_sp_read();
    503  1.12       ryo 	if (l->l_addr >= sp) {
    504  1.12       ryo 		panic("lwp/interrupt stack overflow detected."
    505  1.12       ryo 		    " lwp=%p, sp=%p, l_addr=%p", l, sp, l->l_addr);
    506  1.12       ryo 	}
    507  1.12       ryo #endif
    508  1.12       ryo 
    509  1.14       ryo 	/* disable trace */
    510  1.14       ryo 	reg_mdscr_el1_write(reg_mdscr_el1_read() & ~MDSCR_SS);
    511  1.14       ryo 
    512   1.4       ryo 	/* enable traps */
    513   1.4       ryo 	daif_enable(DAIF_D|DAIF_A);
    514   1.2  nisimura 
    515   1.4       ryo 	ci->ci_intr_depth++;
    516   1.4       ryo 	ARM_IRQ_HANDLER(tf);
    517   1.4       ryo 	ci->ci_intr_depth--;
    518   1.2  nisimura 
    519   1.4       ryo 	cpu_dosoftints();
    520   1.4       ryo }
    521   1.2  nisimura 
    522  1.21       rin #ifdef COMPAT_NETBSD32
    523  1.21       rin 
    524  1.21       rin /*
    525  1.21       rin  * 32-bit length Thumb instruction. See ARMv7 DDI0406A A6.3.
    526  1.21       rin  */
    527  1.21       rin #define THUMB_32BIT(hi) (((hi) & 0xe000) == 0xe000 && ((hi) & 0x1800))
    528  1.21       rin 
    529  1.31       ryo int
    530  1.31       ryo fetch_arm_insn(uint64_t pc, uint64_t spsr, uint32_t *insn)
    531  1.21       rin {
    532  1.21       rin 
    533  1.21       rin 	/* THUMB? */
    534  1.31       ryo 	if (spsr & SPSR_A32_T) {
    535  1.31       ryo 		uint16_t *p = (uint16_t *)(pc & ~1UL); /* XXX */
    536  1.21       rin 		uint16_t hi, lo;
    537  1.21       rin 
    538  1.31       ryo 		if (ufetch_16(p, &hi))
    539  1.26       rin 			return -1;
    540  1.26       rin 
    541  1.21       rin 		if (!THUMB_32BIT(hi)) {
    542  1.21       rin 			/* 16-bit Thumb instruction */
    543  1.21       rin 			*insn = hi;
    544  1.21       rin 			return 2;
    545  1.21       rin 		}
    546  1.21       rin 
    547  1.26       rin 		/* 32-bit Thumb instruction */
    548  1.31       ryo 		if (ufetch_16(p + 1, &lo))
    549  1.26       rin 			return -1;
    550  1.21       rin 
    551  1.21       rin 		*insn = ((uint32_t)hi << 16) | lo;
    552  1.21       rin 		return 4;
    553  1.21       rin 	}
    554  1.21       rin 
    555  1.31       ryo 	if (ufetch_32((uint32_t *)pc, insn))
    556  1.26       rin 		return -1;
    557  1.26       rin 
    558  1.21       rin 	return 4;
    559  1.21       rin }
    560  1.21       rin 
    561  1.32       ryo static bool
    562  1.32       ryo arm_cond_match(uint32_t insn, uint64_t spsr)
    563  1.32       ryo {
    564  1.32       ryo 	bool invert = (insn >> 28) & 1;
    565  1.32       ryo 	bool match;
    566  1.32       ryo 
    567  1.32       ryo 	switch (insn >> 29) {
    568  1.32       ryo 	case 0:	/* EQ or NE */
    569  1.32       ryo 		match = spsr & SPSR_Z;
    570  1.32       ryo 		break;
    571  1.32       ryo 	case 1:	/* CS/HI or CC/LO */
    572  1.32       ryo 		match = spsr & SPSR_C;
    573  1.32       ryo 		break;
    574  1.32       ryo 	case 2:	/* MI or PL */
    575  1.32       ryo 		match = spsr & SPSR_N;
    576  1.32       ryo 		break;
    577  1.32       ryo 	case 3:	/* VS or VC */
    578  1.32       ryo 		match = spsr & SPSR_V;
    579  1.32       ryo 		break;
    580  1.32       ryo 	case 4:	/* HI or LS */
    581  1.32       ryo 		match = ((spsr & (SPSR_C | SPSR_Z)) == SPSR_C);
    582  1.32       ryo 		break;
    583  1.32       ryo 	case 5:	/* GE or LT */
    584  1.32       ryo 		match = (!(spsr & SPSR_N) == !(spsr & SPSR_V));
    585  1.32       ryo 		break;
    586  1.32       ryo 	case 6:	/* GT or LE */
    587  1.32       ryo 		match = !(spsr & SPSR_Z) &&
    588  1.32       ryo 		    (!(spsr & SPSR_N) == !(spsr & SPSR_V));
    589  1.32       ryo 		break;
    590  1.32       ryo 	case 7:	/* AL */
    591  1.32       ryo 		match = true;
    592  1.32       ryo 		break;
    593  1.32       ryo 	}
    594  1.32       ryo 	return (!match != !invert);
    595  1.32       ryo }
    596  1.32       ryo 
    597  1.33       ryo uint8_t atomic_swap_8(volatile uint8_t *, uint8_t);
    598  1.33       ryo 
    599  1.33       ryo static int
    600  1.33       ryo emul_arm_swp(uint32_t insn, struct trapframe *tf)
    601  1.33       ryo {
    602  1.33       ryo 	struct faultbuf fb;
    603  1.33       ryo 	vaddr_t vaddr;
    604  1.33       ryo 	uint32_t val;
    605  1.33       ryo 	int Rn, Rd, Rm, error;
    606  1.33       ryo 
    607  1.33       ryo 	Rn = __SHIFTOUT(insn, 0x000f0000);
    608  1.33       ryo 	Rd = __SHIFTOUT(insn, 0x0000f000);
    609  1.33       ryo 	Rm = __SHIFTOUT(insn, 0x0000000f);
    610  1.33       ryo 
    611  1.33       ryo 	vaddr = tf->tf_reg[Rn] & 0xffffffff;
    612  1.33       ryo 	val = tf->tf_reg[Rm];
    613  1.33       ryo 
    614  1.33       ryo 	/* fault if insn is swp, and unaligned access */
    615  1.33       ryo 	if ((insn & 0x00400000) == 0 && (vaddr & 3) != 0) {
    616  1.33       ryo 		tf->tf_far = vaddr;
    617  1.33       ryo 		return EFAULT;
    618  1.33       ryo 	}
    619  1.33       ryo 
    620  1.33       ryo 	/* vaddr will always point to userspace, since it has only 32bit */
    621  1.33       ryo 	if ((error = cpu_set_onfault(&fb)) == 0) {
    622  1.33       ryo 		if (insn & 0x00400000) {
    623  1.33       ryo 			/* swpb */
    624  1.34       ryo 			val = atomic_swap_8((uint8_t *)vaddr, val);
    625  1.33       ryo 		} else {
    626  1.33       ryo 			/* swp */
    627  1.34       ryo 			val = atomic_swap_32((uint32_t *)vaddr, val);
    628  1.33       ryo 		}
    629  1.33       ryo 		cpu_unset_onfault();
    630  1.33       ryo 		tf->tf_reg[Rd] = val;
    631  1.33       ryo 	} else {
    632  1.33       ryo 		tf->tf_far = reg_far_el1_read();
    633  1.33       ryo 	}
    634  1.33       ryo 	return error;
    635  1.33       ryo }
    636  1.33       ryo 
    637  1.32       ryo static enum emul_arm_result
    638  1.32       ryo emul_thumb_insn(struct trapframe *tf, uint32_t insn, int insn_size)
    639  1.32       ryo {
    640  1.32       ryo 	/* T32-16bit or 32bit instructions */
    641  1.32       ryo 	switch (insn_size) {
    642  1.32       ryo 	case 2:
    643  1.32       ryo 		/* Breakpoint used by GDB */
    644  1.32       ryo 		if (insn == 0xdefe) {
    645  1.32       ryo 			do_trapsignal(curlwp, SIGTRAP, TRAP_BRKPT,
    646  1.32       ryo 			    (void *)tf->tf_pc, 0);
    647  1.32       ryo 			return EMUL_ARM_SUCCESS;
    648  1.32       ryo 		}
    649  1.32       ryo 		/* XXX: some T32 IT instruction deprecated should be emulated */
    650  1.32       ryo 		break;
    651  1.32       ryo 	case 4:
    652  1.32       ryo 		break;
    653  1.32       ryo 	default:
    654  1.32       ryo 		return EMUL_ARM_FAULT;
    655  1.32       ryo 	}
    656  1.32       ryo 	return EMUL_ARM_UNKNOWN;
    657  1.32       ryo }
    658  1.32       ryo 
    659  1.26       rin static enum emul_arm_result
    660  1.21       rin emul_arm_insn(struct trapframe *tf)
    661  1.21       rin {
    662  1.21       rin 	uint32_t insn;
    663  1.21       rin 	int insn_size;
    664  1.21       rin 
    665  1.31       ryo 	insn_size = fetch_arm_insn(tf->tf_pc, tf->tf_spsr, &insn);
    666  1.32       ryo 	tf->tf_far = reg_far_el1_read();
    667  1.21       rin 
    668  1.32       ryo 	if (tf->tf_spsr & SPSR_A32_T)
    669  1.32       ryo 		return emul_thumb_insn(tf, insn, insn_size);
    670  1.32       ryo 	if (insn_size != 4)
    671  1.32       ryo 		return EMUL_ARM_FAULT;
    672  1.21       rin 
    673  1.32       ryo 	/* Breakpoint used by GDB */
    674  1.32       ryo 	if (insn == 0xe6000011 || insn == 0xe7ffdefe) {
    675  1.32       ryo 		do_trapsignal(curlwp, SIGTRAP, TRAP_BRKPT,
    676  1.32       ryo 		    (void *)tf->tf_pc, 0);
    677  1.32       ryo 		return EMUL_ARM_SUCCESS;
    678  1.32       ryo 	}
    679  1.30       rin 
    680  1.32       ryo 	/* Unconditional instruction extension space? */
    681  1.32       ryo 	if ((insn & 0xf0000000) == 0xf0000000)
    682  1.32       ryo 		goto unknown_insn;
    683  1.21       rin 
    684  1.33       ryo 	/* swp,swpb */
    685  1.33       ryo 	if ((insn & 0x0fb00ff0) == 0x01000090) {
    686  1.33       ryo 		if (arm_cond_match(insn, tf->tf_spsr)) {
    687  1.33       ryo 			if (emul_arm_swp(insn, tf) != 0)
    688  1.33       ryo 				return EMUL_ARM_FAULT;
    689  1.33       ryo 		}
    690  1.33       ryo 		goto emulated;
    691  1.33       ryo 	}
    692  1.33       ryo 
    693  1.32       ryo 	/*
    694  1.32       ryo 	 * Emulate ARMv6 instructions with cache operations
    695  1.32       ryo 	 * register (c7), that can be used in user mode.
    696  1.32       ryo 	 */
    697  1.32       ryo 	switch (insn & 0x0fff0fff) {
    698  1.32       ryo 	case 0x0e070f95:
    699  1.32       ryo 		if (arm_cond_match(insn, tf->tf_spsr)) {
    700  1.21       rin 			/*
    701  1.21       rin 			 * mcr p15, 0, <Rd>, c7, c5, 4
    702  1.21       rin 			 * (flush prefetch buffer)
    703  1.21       rin 			 */
    704  1.21       rin 			__asm __volatile("isb sy" ::: "memory");
    705  1.32       ryo 		}
    706  1.32       ryo 		goto emulated;
    707  1.32       ryo 	case 0x0e070f9a:
    708  1.32       ryo 		if (arm_cond_match(insn, tf->tf_spsr)) {
    709  1.21       rin 			/*
    710  1.21       rin 			 * mcr p15, 0, <Rd>, c7, c10, 4
    711  1.21       rin 			 * (data synchronization barrier)
    712  1.21       rin 			 */
    713  1.21       rin 			__asm __volatile("dsb sy" ::: "memory");
    714  1.32       ryo 		}
    715  1.32       ryo 		goto emulated;
    716  1.32       ryo 	case 0x0e070fba:
    717  1.32       ryo 		if (arm_cond_match(insn, tf->tf_spsr)) {
    718  1.21       rin 			/*
    719  1.21       rin 			 * mcr p15, 0, <Rd>, c7, c10, 5
    720  1.21       rin 			 * (data memory barrier)
    721  1.21       rin 			 */
    722  1.21       rin 			__asm __volatile("dmb sy" ::: "memory");
    723  1.21       rin 		}
    724  1.32       ryo 		goto emulated;
    725  1.32       ryo 	default:
    726  1.21       rin 		break;
    727  1.21       rin 	}
    728  1.21       rin 
    729  1.32       ryo  unknown_insn:
    730  1.21       rin 	/* unknown, or unsupported instruction */
    731  1.26       rin 	return EMUL_ARM_UNKNOWN;
    732  1.21       rin 
    733  1.21       rin  emulated:
    734  1.21       rin 	tf->tf_pc += insn_size;
    735  1.26       rin 	return EMUL_ARM_SUCCESS;
    736  1.21       rin }
    737  1.21       rin #endif /* COMPAT_NETBSD32 */
    738  1.21       rin 
    739   1.2  nisimura void
    740   1.4       ryo trap_el0_32sync(struct trapframe *tf)
    741   1.2  nisimura {
    742   1.4       ryo 	struct lwp * const l = curlwp;
    743   1.4       ryo 	const uint32_t esr = tf->tf_esr;
    744   1.4       ryo 	const uint32_t eclass = __SHIFTOUT(esr, ESR_EC); /* exception class */
    745   1.4       ryo 
    746  1.14       ryo 	/* disable trace */
    747  1.14       ryo 	reg_mdscr_el1_write(reg_mdscr_el1_read() & ~MDSCR_SS);
    748   1.4       ryo 	/* enable traps and interrupts */
    749   1.4       ryo 	daif_enable(DAIF_D|DAIF_A|DAIF_I|DAIF_F);
    750   1.4       ryo 
    751   1.4       ryo 	switch (eclass) {
    752  1.11       ryo #ifdef COMPAT_NETBSD32
    753  1.11       ryo 	case ESR_EC_INSN_ABT_EL0:
    754  1.11       ryo 	case ESR_EC_DATA_ABT_EL0:
    755  1.11       ryo 		data_abort_handler(tf, eclass);
    756  1.11       ryo 		userret(l);
    757  1.11       ryo 		break;
    758  1.11       ryo 
    759  1.11       ryo 	case ESR_EC_SVC_A32:
    760  1.11       ryo 		(*l->l_proc->p_md.md_syscall)(tf);
    761  1.11       ryo 		break;
    762  1.19     skrll 
    763   1.4       ryo 	case ESR_EC_FP_ACCESS:
    764   1.4       ryo 		fpu_load(l);
    765   1.4       ryo 		userret(l);
    766   1.4       ryo 		break;
    767  1.19     skrll 
    768  1.11       ryo 	case ESR_EC_FP_TRAP_A32:
    769  1.11       ryo 		do_trapsignal(l, SIGFPE, FPE_FLTUND, NULL, esr); /* XXX */
    770   1.4       ryo 		userret(l);
    771  1.18  jmcneill 		break;
    772   1.4       ryo 
    773   1.4       ryo 	case ESR_EC_PC_ALIGNMENT:
    774   1.5  christos 		do_trapsignal(l, SIGBUS, BUS_ADRALN, (void *)tf->tf_pc, esr);
    775   1.4       ryo 		userret(l);
    776   1.4       ryo 		break;
    777  1.19     skrll 
    778   1.4       ryo 	case ESR_EC_SP_ALIGNMENT:
    779  1.11       ryo 		do_trapsignal(l, SIGBUS, BUS_ADRALN,
    780  1.11       ryo 		    (void *)tf->tf_reg[13], esr); /* sp is r13 on AArch32 */
    781   1.4       ryo 		userret(l);
    782   1.4       ryo 		break;
    783   1.4       ryo 
    784  1.11       ryo 	case ESR_EC_BKPT_INSN_A32:
    785  1.11       ryo 		do_trapsignal(l, SIGTRAP, TRAP_BRKPT, (void *)tf->tf_pc, esr);
    786  1.11       ryo 		userret(l);
    787   1.4       ryo 		break;
    788  1.11       ryo 
    789  1.21       rin 	case ESR_EC_UNKNOWN:
    790  1.26       rin 		switch (emul_arm_insn(tf)) {
    791  1.26       rin 		case EMUL_ARM_SUCCESS:
    792  1.26       rin 			break;
    793  1.26       rin 		case EMUL_ARM_UNKNOWN:
    794  1.21       rin 			goto unknown;
    795  1.26       rin 		case EMUL_ARM_FAULT:
    796  1.26       rin 			do_trapsignal(l, SIGSEGV, SEGV_MAPERR,
    797  1.32       ryo 			    (void *)tf->tf_far, esr);
    798  1.26       rin 			break;
    799  1.26       rin 		}
    800  1.21       rin 		userret(l);
    801  1.21       rin 		break;
    802  1.21       rin 
    803   1.4       ryo 	case ESR_EC_CP15_RT:
    804   1.4       ryo 	case ESR_EC_CP15_RRT:
    805   1.4       ryo 	case ESR_EC_CP14_RT:
    806   1.4       ryo 	case ESR_EC_CP14_DT:
    807   1.4       ryo 	case ESR_EC_CP14_RRT:
    808  1.21       rin unknown:
    809   1.4       ryo #endif /* COMPAT_NETBSD32 */
    810   1.4       ryo 	default:
    811  1.11       ryo #ifdef DDB
    812  1.11       ryo 		if (sigill_debug) {
    813  1.11       ryo 			/* show illegal instruction */
    814  1.11       ryo 			printf("TRAP: pid %d (%s), uid %d: %s:"
    815  1.11       ryo 			    " esr=0x%lx: pc=0x%lx: %s\n",
    816  1.11       ryo 			    curlwp->l_proc->p_pid, curlwp->l_proc->p_comm,
    817  1.11       ryo 			    l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1,
    818  1.11       ryo 			    eclass_trapname(eclass), tf->tf_esr, tf->tf_pc,
    819  1.31       ryo 			    strdisasm(tf->tf_pc, tf->tf_spsr));
    820  1.11       ryo 		}
    821  1.11       ryo #endif
    822  1.11       ryo 		/* illegal or not implemented instruction */
    823   1.5  christos 		do_trapsignal(l, SIGILL, ILL_ILLTRP, (void *)tf->tf_pc, esr);
    824   1.4       ryo 		userret(l);
    825   1.4       ryo 		break;
    826   1.4       ryo 	}
    827   1.4       ryo }
    828   1.4       ryo 
    829   1.4       ryo #define bad_trap_panic(trapfunc)	\
    830   1.4       ryo void					\
    831   1.4       ryo trapfunc(struct trapframe *tf)		\
    832   1.4       ryo {					\
    833   1.4       ryo 	panic("%s", __func__);		\
    834   1.4       ryo }
    835   1.4       ryo bad_trap_panic(trap_el1t_sync)
    836   1.4       ryo bad_trap_panic(trap_el1t_irq)
    837   1.4       ryo bad_trap_panic(trap_el1t_fiq)
    838   1.4       ryo bad_trap_panic(trap_el1t_error)
    839   1.4       ryo bad_trap_panic(trap_el1h_fiq)
    840   1.4       ryo bad_trap_panic(trap_el1h_error)
    841   1.4       ryo bad_trap_panic(trap_el0_fiq)
    842   1.4       ryo bad_trap_panic(trap_el0_error)
    843   1.4       ryo bad_trap_panic(trap_el0_32fiq)
    844   1.4       ryo bad_trap_panic(trap_el0_32error)
    845   1.2  nisimura 
    846   1.4       ryo void
    847   1.4       ryo cpu_jump_onfault(struct trapframe *tf, const struct faultbuf *fb, int val)
    848   1.4       ryo {
    849   1.2  nisimura 	tf->tf_reg[19] = fb->fb_reg[FB_X19];
    850   1.2  nisimura 	tf->tf_reg[20] = fb->fb_reg[FB_X20];
    851   1.2  nisimura 	tf->tf_reg[21] = fb->fb_reg[FB_X21];
    852   1.2  nisimura 	tf->tf_reg[22] = fb->fb_reg[FB_X22];
    853   1.2  nisimura 	tf->tf_reg[23] = fb->fb_reg[FB_X23];
    854   1.2  nisimura 	tf->tf_reg[24] = fb->fb_reg[FB_X24];
    855   1.2  nisimura 	tf->tf_reg[25] = fb->fb_reg[FB_X25];
    856   1.2  nisimura 	tf->tf_reg[26] = fb->fb_reg[FB_X26];
    857   1.2  nisimura 	tf->tf_reg[27] = fb->fb_reg[FB_X27];
    858   1.2  nisimura 	tf->tf_reg[28] = fb->fb_reg[FB_X28];
    859   1.2  nisimura 	tf->tf_reg[29] = fb->fb_reg[FB_X29];
    860   1.2  nisimura 	tf->tf_sp = fb->fb_reg[FB_SP];
    861   1.4       ryo 	tf->tf_pc = fb->fb_reg[FB_LR];
    862   1.4       ryo 	tf->tf_reg[0] = val;
    863   1.2  nisimura }
    864   1.2  nisimura 
    865   1.6  christos #ifdef TRAP_SIGDEBUG
    866   1.6  christos static void
    867   1.6  christos frame_dump(const struct trapframe *tf)
    868   1.6  christos {
    869   1.6  christos 	const struct reg *r = &tf->tf_regs;
    870   1.6  christos 
    871   1.6  christos 	printf("trapframe %p\n", tf);
    872   1.6  christos 	for (size_t i = 0; i < __arraycount(r->r_reg); i++) {
    873   1.7  christos 		printf(" r%.2zu %#018" PRIx64 "%c", i, r->r_reg[i],
    874   1.6  christos 		    " \n"[i && (i & 1) == 0]);
    875   1.6  christos 	}
    876   1.6  christos 
    877   1.6  christos 	printf("\n");
    878   1.6  christos 	printf("   sp %#018" PRIx64 "    pc %#018" PRIx64 "\n",
    879   1.6  christos 	    r->r_sp, r->r_pc);
    880   1.6  christos 	printf(" spsr %#018" PRIx64 " tpidr %#018" PRIx64 "\n",
    881   1.6  christos 	    r->r_spsr, r->r_tpidr);
    882   1.6  christos 	printf("  esr %#018" PRIx64 "   far %#018" PRIx64 "\n",
    883   1.6  christos 	    tf->tf_esr, tf->tf_far);
    884   1.6  christos 
    885   1.6  christos 	printf("\n");
    886   1.6  christos 	hexdump(printf, "Stack dump", tf, 256);
    887   1.6  christos }
    888   1.6  christos 
    889   1.6  christos static void
    890   1.6  christos sigdebug(const struct trapframe *tf, const ksiginfo_t *ksi)
    891   1.6  christos {
    892   1.6  christos 	struct lwp *l = curlwp;
    893   1.6  christos 	struct proc *p = l->l_proc;
    894   1.6  christos 	const uint32_t eclass = __SHIFTOUT(ksi->ksi_trap, ESR_EC);
    895   1.6  christos 
    896   1.6  christos 	printf("pid %d.%d (%s): signal %d (trap %#x) "
    897   1.6  christos 	    "@pc %#" PRIx64 ", addr %p, error=%s\n",
    898   1.6  christos 	    p->p_pid, l->l_lid, p->p_comm, ksi->ksi_signo, ksi->ksi_trap,
    899   1.6  christos 	    tf->tf_regs.r_pc, ksi->ksi_addr, eclass_trapname(eclass));
    900   1.6  christos 	frame_dump(tf);
    901   1.6  christos }
    902   1.6  christos #endif
    903   1.6  christos 
    904   1.6  christos void do_trapsignal1(
    905   1.6  christos #ifdef TRAP_SIGDEBUG
    906   1.6  christos     const char *func,
    907   1.6  christos     size_t line,
    908   1.6  christos     struct trapframe *tf,
    909   1.6  christos #endif
    910   1.6  christos     struct lwp *l, int signo, int code, void *addr, int trap)
    911   1.6  christos {
    912   1.6  christos 	ksiginfo_t ksi;
    913   1.6  christos 
    914   1.6  christos 	KSI_INIT_TRAP(&ksi);
    915   1.6  christos 	ksi.ksi_signo = signo;
    916   1.6  christos 	ksi.ksi_code = code;
    917   1.6  christos 	ksi.ksi_addr = addr;
    918   1.6  christos 	ksi.ksi_trap = trap;
    919   1.6  christos #ifdef TRAP_SIGDEBUG
    920   1.6  christos 	printf("%s, %zu: ", func, line);
    921   1.6  christos 	sigdebug(tf, &ksi);
    922   1.6  christos #endif
    923   1.6  christos 	(*l->l_proc->p_emul->e_trapsignal)(l, &ksi);
    924   1.6  christos }
    925  1.23        ad 
    926  1.23        ad bool
    927  1.23        ad cpu_intr_p(void)
    928  1.23        ad {
    929  1.23        ad 	uint64_t ncsw;
    930  1.23        ad 	int idepth;
    931  1.23        ad 	lwp_t *l;
    932  1.23        ad 
    933  1.23        ad #ifdef __HAVE_PIC_FAST_SOFTINTS
    934  1.23        ad 	/* XXX Copied from cpu.h.  Looks incomplete - needs fixing. */
    935  1.23        ad 	if (ci->ci_cpl < IPL_VM)
    936  1.23        ad 		return false;
    937  1.23        ad #endif
    938  1.23        ad 
    939  1.23        ad 	l = curlwp;
    940  1.23        ad 	if (__predict_false(l->l_cpu == NULL)) {
    941  1.23        ad 		KASSERT(l == &lwp0);
    942  1.23        ad 		return false;
    943  1.23        ad 	}
    944  1.23        ad 	do {
    945  1.23        ad 		ncsw = l->l_ncsw;
    946  1.23        ad 		__insn_barrier();
    947  1.24     skrll 		idepth = l->l_cpu->ci_intr_depth;
    948  1.23        ad 		__insn_barrier();
    949  1.23        ad 	} while (__predict_false(ncsw != l->l_ncsw));
    950  1.23        ad 
    951  1.23        ad 	return idepth > 0;
    952  1.23        ad }
    953