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