trap.c revision 1.27 1 1.27 maxv /* $NetBSD: trap.c,v 1.27 2020/04/13 05:40:25 maxv 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.27 maxv __KERNEL_RCSID(1, "$NetBSD: trap.c,v 1.27 2020/04/13 05:40:25 maxv 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.4 ryo #ifdef KDB
46 1.4 ryo #include <sys/kdb.h>
47 1.4 ryo #endif
48 1.3 nisimura #include <sys/proc.h>
49 1.3 nisimura #include <sys/systm.h>
50 1.3 nisimura #include <sys/signal.h>
51 1.3 nisimura #include <sys/signalvar.h>
52 1.3 nisimura #include <sys/siginfo.h>
53 1.1 matt
54 1.4 ryo #ifdef ARM_INTR_IMPL
55 1.4 ryo #include ARM_INTR_IMPL
56 1.4 ryo #else
57 1.4 ryo #error ARM_INTR_IMPL not defined
58 1.4 ryo #endif
59 1.4 ryo
60 1.4 ryo #ifndef ARM_IRQ_HANDLER
61 1.4 ryo #error ARM_IRQ_HANDLER not defined
62 1.4 ryo #endif
63 1.4 ryo
64 1.4 ryo #include <aarch64/userret.h>
65 1.4 ryo #include <aarch64/frame.h>
66 1.4 ryo #include <aarch64/machdep.h>
67 1.4 ryo #include <aarch64/armreg.h>
68 1.1 matt #include <aarch64/locore.h>
69 1.1 matt
70 1.4 ryo #ifdef KDB
71 1.4 ryo #include <machine/db_machdep.h>
72 1.4 ryo #endif
73 1.4 ryo #ifdef DDB
74 1.4 ryo #include <ddb/db_output.h>
75 1.4 ryo #include <machine/db_machdep.h>
76 1.4 ryo #endif
77 1.22 jmcneill #ifdef KDTRACE_HOOKS
78 1.22 jmcneill #include <sys/dtrace_bsd.h>
79 1.22 jmcneill #endif
80 1.4 ryo
81 1.8 ryo #ifdef DDB
82 1.8 ryo int sigill_debug = 0;
83 1.8 ryo #endif
84 1.4 ryo
85 1.22 jmcneill #ifdef KDTRACE_HOOKS
86 1.22 jmcneill dtrace_doubletrap_func_t dtrace_doubletrap_func = NULL;
87 1.22 jmcneill dtrace_trap_func_t dtrace_trap_func = NULL;
88 1.22 jmcneill int (*dtrace_invop_jump_addr)(struct trapframe *);
89 1.22 jmcneill #endif
90 1.22 jmcneill
91 1.4 ryo const char * const trap_names[] = {
92 1.4 ryo [ESR_EC_UNKNOWN] = "Unknown Reason (Illegal Instruction)",
93 1.4 ryo [ESR_EC_SERROR] = "SError Interrupt",
94 1.4 ryo [ESR_EC_WFX] = "WFI or WFE instruction execution",
95 1.4 ryo [ESR_EC_ILL_STATE] = "Illegal Execution State",
96 1.4 ryo
97 1.25 maxv [ESR_EC_BTE_A64] = "Branch Target Exception",
98 1.25 maxv
99 1.4 ryo [ESR_EC_SYS_REG] = "MSR/MRS/SYS instruction",
100 1.4 ryo [ESR_EC_SVC_A64] = "SVC Instruction Execution",
101 1.4 ryo [ESR_EC_HVC_A64] = "HVC Instruction Execution",
102 1.4 ryo [ESR_EC_SMC_A64] = "SMC Instruction Execution",
103 1.4 ryo
104 1.4 ryo [ESR_EC_INSN_ABT_EL0] = "Instruction Abort (EL0)",
105 1.4 ryo [ESR_EC_INSN_ABT_EL1] = "Instruction Abort (EL1)",
106 1.4 ryo [ESR_EC_DATA_ABT_EL0] = "Data Abort (EL0)",
107 1.4 ryo [ESR_EC_DATA_ABT_EL1] = "Data Abort (EL1)",
108 1.4 ryo
109 1.4 ryo [ESR_EC_PC_ALIGNMENT] = "Misaligned PC",
110 1.4 ryo [ESR_EC_SP_ALIGNMENT] = "Misaligned SP",
111 1.4 ryo
112 1.4 ryo [ESR_EC_FP_ACCESS] = "Access to SIMD/FP Registers",
113 1.4 ryo [ESR_EC_FP_TRAP_A64] = "FP Exception",
114 1.4 ryo
115 1.4 ryo [ESR_EC_BRKPNT_EL0] = "Breakpoint Exception (EL0)",
116 1.4 ryo [ESR_EC_BRKPNT_EL1] = "Breakpoint Exception (EL1)",
117 1.4 ryo [ESR_EC_SW_STEP_EL0] = "Software Step (EL0)",
118 1.4 ryo [ESR_EC_SW_STEP_EL1] = "Software Step (EL1)",
119 1.4 ryo [ESR_EC_WTCHPNT_EL0] = "Watchpoint (EL0)",
120 1.4 ryo [ESR_EC_WTCHPNT_EL1] = "Watchpoint (EL1)",
121 1.4 ryo [ESR_EC_BKPT_INSN_A64] = "BKPT Instruction Execution",
122 1.4 ryo
123 1.4 ryo [ESR_EC_CP15_RT] = "A32: MCR/MRC access to CP15",
124 1.4 ryo [ESR_EC_CP15_RRT] = "A32: MCRR/MRRC access to CP15",
125 1.4 ryo [ESR_EC_CP14_RT] = "A32: MCR/MRC access to CP14",
126 1.4 ryo [ESR_EC_CP14_DT] = "A32: LDC/STC access to CP14",
127 1.4 ryo [ESR_EC_CP14_RRT] = "A32: MRRC access to CP14",
128 1.4 ryo [ESR_EC_SVC_A32] = "A32: SVC Instruction Execution",
129 1.4 ryo [ESR_EC_HVC_A32] = "A32: HVC Instruction Execution",
130 1.4 ryo [ESR_EC_SMC_A32] = "A32: SMC Instruction Execution",
131 1.4 ryo [ESR_EC_FPID] = "A32: MCR/MRC access to CP10",
132 1.4 ryo [ESR_EC_FP_TRAP_A32] = "A32: FP Exception",
133 1.4 ryo [ESR_EC_BKPT_INSN_A32] = "A32: BKPT Instruction Execution",
134 1.4 ryo [ESR_EC_VECTOR_CATCH] = "A32: Vector Catch Exception"
135 1.4 ryo };
136 1.4 ryo
137 1.6 christos const char *
138 1.4 ryo eclass_trapname(uint32_t eclass)
139 1.3 nisimura {
140 1.4 ryo static char trapnamebuf[sizeof("Unknown trap 0x????????")];
141 1.4 ryo
142 1.4 ryo if (eclass >= __arraycount(trap_names) || trap_names[eclass] == NULL) {
143 1.4 ryo snprintf(trapnamebuf, sizeof(trapnamebuf),
144 1.6 christos "Unknown trap %#02x", eclass);
145 1.4 ryo return trapnamebuf;
146 1.4 ryo }
147 1.4 ryo return trap_names[eclass];
148 1.3 nisimura }
149 1.3 nisimura
150 1.1 matt void
151 1.4 ryo userret(struct lwp *l)
152 1.1 matt {
153 1.1 matt mi_userret(l);
154 1.1 matt }
155 1.2 nisimura
156 1.3 nisimura void
157 1.4 ryo trap_doast(struct trapframe *tf)
158 1.3 nisimura {
159 1.3 nisimura struct lwp * const l = curlwp;
160 1.4 ryo
161 1.4 ryo /*
162 1.4 ryo * allow to have a chance of context switch just prior to user
163 1.4 ryo * exception return.
164 1.4 ryo */
165 1.4 ryo #ifdef __HAVE_PREEMPTION
166 1.4 ryo kpreempt_disable();
167 1.4 ryo #endif
168 1.4 ryo struct cpu_info * const ci = curcpu();
169 1.4 ryo
170 1.4 ryo ci->ci_data.cpu_ntrap++;
171 1.4 ryo
172 1.4 ryo KDASSERT(ci->ci_cpl == IPL_NONE);
173 1.4 ryo #ifdef __HAVE_PREEMPTION
174 1.4 ryo kpreempt_enable();
175 1.4 ryo #endif
176 1.4 ryo
177 1.4 ryo if (l->l_pflag & LP_OWEUPC) {
178 1.4 ryo l->l_pflag &= ~LP_OWEUPC;
179 1.4 ryo ADDUPROF(l);
180 1.3 nisimura }
181 1.4 ryo
182 1.4 ryo userret(l);
183 1.4 ryo }
184 1.4 ryo
185 1.4 ryo void
186 1.4 ryo trap_el1h_sync(struct trapframe *tf)
187 1.4 ryo {
188 1.4 ryo const uint32_t esr = tf->tf_esr;
189 1.4 ryo const uint32_t eclass = __SHIFTOUT(esr, ESR_EC); /* exception class */
190 1.4 ryo
191 1.4 ryo /* re-enable traps and interrupts */
192 1.4 ryo if (!(tf->tf_spsr & SPSR_I))
193 1.4 ryo daif_enable(DAIF_D|DAIF_A|DAIF_I|DAIF_F);
194 1.4 ryo else
195 1.4 ryo daif_enable(DAIF_D|DAIF_A);
196 1.4 ryo
197 1.22 jmcneill #ifdef KDTRACE_HOOKS
198 1.22 jmcneill if (dtrace_trap_func != NULL && (*dtrace_trap_func)(tf, eclass))
199 1.22 jmcneill return;
200 1.22 jmcneill #endif
201 1.22 jmcneill
202 1.4 ryo switch (eclass) {
203 1.4 ryo case ESR_EC_INSN_ABT_EL1:
204 1.4 ryo case ESR_EC_DATA_ABT_EL1:
205 1.6 christos data_abort_handler(tf, eclass);
206 1.4 ryo break;
207 1.4 ryo
208 1.22 jmcneill case ESR_EC_BKPT_INSN_A64:
209 1.22 jmcneill #ifdef KDTRACE_HOOKS
210 1.22 jmcneill if (__SHIFTOUT(esr, ESR_ISS) == 0x40d &&
211 1.22 jmcneill dtrace_invop_jump_addr != 0) {
212 1.22 jmcneill (*dtrace_invop_jump_addr)(tf);
213 1.22 jmcneill break;
214 1.22 jmcneill }
215 1.22 jmcneill /* FALLTHROUGH */
216 1.22 jmcneill #endif
217 1.4 ryo case ESR_EC_BRKPNT_EL1:
218 1.4 ryo case ESR_EC_SW_STEP_EL1:
219 1.4 ryo case ESR_EC_WTCHPNT_EL1:
220 1.4 ryo #ifdef DDB
221 1.4 ryo if (eclass == ESR_EC_BRKPNT_EL1)
222 1.4 ryo kdb_trap(DB_TRAP_BREAKPOINT, tf);
223 1.4 ryo else if (eclass == ESR_EC_BKPT_INSN_A64)
224 1.4 ryo kdb_trap(DB_TRAP_BKPT_INSN, tf);
225 1.4 ryo else if (eclass == ESR_EC_WTCHPNT_EL1)
226 1.4 ryo kdb_trap(DB_TRAP_WATCHPOINT, tf);
227 1.4 ryo else if (eclass == ESR_EC_SW_STEP_EL1)
228 1.4 ryo kdb_trap(DB_TRAP_SW_STEP, tf);
229 1.4 ryo else
230 1.4 ryo kdb_trap(DB_TRAP_UNKNOWN, tf);
231 1.4 ryo #else
232 1.4 ryo panic("No debugger in kernel");
233 1.4 ryo #endif
234 1.4 ryo break;
235 1.4 ryo
236 1.4 ryo case ESR_EC_FP_ACCESS:
237 1.4 ryo case ESR_EC_FP_TRAP_A64:
238 1.4 ryo case ESR_EC_PC_ALIGNMENT:
239 1.4 ryo case ESR_EC_SP_ALIGNMENT:
240 1.4 ryo case ESR_EC_ILL_STATE:
241 1.27 maxv case ESR_EC_BTE_A64:
242 1.4 ryo default:
243 1.13 ryo panic("Trap: fatal %s: pc=%016" PRIx64 " sp=%016" PRIx64
244 1.13 ryo " esr=%08x", eclass_trapname(eclass), tf->tf_pc, tf->tf_sp,
245 1.6 christos esr);
246 1.4 ryo break;
247 1.3 nisimura }
248 1.3 nisimura }
249 1.3 nisimura
250 1.3 nisimura void
251 1.4 ryo trap_el0_sync(struct trapframe *tf)
252 1.3 nisimura {
253 1.4 ryo struct lwp * const l = curlwp;
254 1.4 ryo const uint32_t esr = tf->tf_esr;
255 1.4 ryo const uint32_t eclass = __SHIFTOUT(esr, ESR_EC); /* exception class */
256 1.4 ryo
257 1.14 ryo /* disable trace */
258 1.14 ryo reg_mdscr_el1_write(reg_mdscr_el1_read() & ~MDSCR_SS);
259 1.4 ryo /* enable traps and interrupts */
260 1.4 ryo daif_enable(DAIF_D|DAIF_A|DAIF_I|DAIF_F);
261 1.4 ryo
262 1.4 ryo switch (eclass) {
263 1.4 ryo case ESR_EC_INSN_ABT_EL0:
264 1.4 ryo case ESR_EC_DATA_ABT_EL0:
265 1.6 christos data_abort_handler(tf, eclass);
266 1.4 ryo userret(l);
267 1.4 ryo break;
268 1.4 ryo
269 1.4 ryo case ESR_EC_SVC_A64:
270 1.4 ryo (*l->l_proc->p_md.md_syscall)(tf);
271 1.4 ryo break;
272 1.4 ryo case ESR_EC_FP_ACCESS:
273 1.4 ryo fpu_load(l);
274 1.4 ryo userret(l);
275 1.4 ryo break;
276 1.4 ryo case ESR_EC_FP_TRAP_A64:
277 1.4 ryo do_trapsignal(l, SIGFPE, FPE_FLTUND, NULL, esr); /* XXX */
278 1.4 ryo userret(l);
279 1.4 ryo break;
280 1.4 ryo
281 1.4 ryo case ESR_EC_PC_ALIGNMENT:
282 1.5 christos do_trapsignal(l, SIGBUS, BUS_ADRALN, (void *)tf->tf_pc, esr);
283 1.4 ryo userret(l);
284 1.4 ryo break;
285 1.4 ryo case ESR_EC_SP_ALIGNMENT:
286 1.5 christos do_trapsignal(l, SIGBUS, BUS_ADRALN, (void *)tf->tf_sp, esr);
287 1.4 ryo userret(l);
288 1.4 ryo break;
289 1.4 ryo
290 1.4 ryo case ESR_EC_BKPT_INSN_A64:
291 1.4 ryo case ESR_EC_BRKPNT_EL0:
292 1.4 ryo case ESR_EC_WTCHPNT_EL0:
293 1.5 christos do_trapsignal(l, SIGTRAP, TRAP_BRKPT, (void *)tf->tf_pc, esr);
294 1.4 ryo userret(l);
295 1.4 ryo break;
296 1.14 ryo case ESR_EC_SW_STEP_EL0:
297 1.14 ryo /* disable trace, and send trace trap */
298 1.14 ryo tf->tf_spsr &= ~SPSR_SS;
299 1.14 ryo do_trapsignal(l, SIGTRAP, TRAP_TRACE, (void *)tf->tf_pc, esr);
300 1.14 ryo userret(l);
301 1.14 ryo break;
302 1.4 ryo
303 1.4 ryo default:
304 1.4 ryo case ESR_EC_UNKNOWN:
305 1.8 ryo #ifdef DDB
306 1.8 ryo if (sigill_debug) {
307 1.8 ryo /* show illegal instruction */
308 1.11 ryo printf("TRAP: pid %d (%s), uid %d: %s:"
309 1.11 ryo " esr=0x%lx: pc=0x%lx: %s\n",
310 1.8 ryo curlwp->l_proc->p_pid, curlwp->l_proc->p_comm,
311 1.8 ryo l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1,
312 1.11 ryo eclass_trapname(eclass), tf->tf_esr, tf->tf_pc,
313 1.11 ryo strdisasm(tf->tf_pc));
314 1.8 ryo }
315 1.8 ryo #endif
316 1.4 ryo /* illegal or not implemented instruction */
317 1.5 christos do_trapsignal(l, SIGILL, ILL_ILLTRP, (void *)tf->tf_pc, esr);
318 1.4 ryo userret(l);
319 1.4 ryo break;
320 1.4 ryo }
321 1.3 nisimura }
322 1.3 nisimura
323 1.4 ryo void
324 1.4 ryo interrupt(struct trapframe *tf)
325 1.4 ryo {
326 1.4 ryo struct cpu_info * const ci = curcpu();
327 1.2 nisimura
328 1.12 ryo #ifdef STACKCHECKS
329 1.12 ryo struct lwp *l = curlwp;
330 1.12 ryo void *sp = (void *)reg_sp_read();
331 1.12 ryo if (l->l_addr >= sp) {
332 1.12 ryo panic("lwp/interrupt stack overflow detected."
333 1.12 ryo " lwp=%p, sp=%p, l_addr=%p", l, sp, l->l_addr);
334 1.12 ryo }
335 1.12 ryo #endif
336 1.12 ryo
337 1.14 ryo /* disable trace */
338 1.14 ryo reg_mdscr_el1_write(reg_mdscr_el1_read() & ~MDSCR_SS);
339 1.14 ryo
340 1.4 ryo /* enable traps */
341 1.4 ryo daif_enable(DAIF_D|DAIF_A);
342 1.2 nisimura
343 1.4 ryo ci->ci_intr_depth++;
344 1.4 ryo ARM_IRQ_HANDLER(tf);
345 1.4 ryo ci->ci_intr_depth--;
346 1.2 nisimura
347 1.4 ryo cpu_dosoftints();
348 1.4 ryo }
349 1.2 nisimura
350 1.21 rin #ifdef COMPAT_NETBSD32
351 1.21 rin
352 1.21 rin /*
353 1.21 rin * 32-bit length Thumb instruction. See ARMv7 DDI0406A A6.3.
354 1.21 rin */
355 1.21 rin #define THUMB_32BIT(hi) (((hi) & 0xe000) == 0xe000 && ((hi) & 0x1800))
356 1.21 rin
357 1.21 rin static int
358 1.21 rin fetch_arm_insn(struct trapframe *tf, uint32_t *insn)
359 1.21 rin {
360 1.21 rin
361 1.21 rin /* THUMB? */
362 1.21 rin if (tf->tf_spsr & SPSR_A32_T) {
363 1.21 rin uint16_t *pc = (uint16_t *)(tf->tf_pc & ~1UL); /* XXX */
364 1.21 rin uint16_t hi, lo;
365 1.21 rin
366 1.26 rin if (ufetch_16(pc, &hi))
367 1.26 rin return -1;
368 1.26 rin
369 1.21 rin if (!THUMB_32BIT(hi)) {
370 1.21 rin /* 16-bit Thumb instruction */
371 1.21 rin *insn = hi;
372 1.21 rin return 2;
373 1.21 rin }
374 1.21 rin
375 1.26 rin /* 32-bit Thumb instruction */
376 1.26 rin if (ufetch_16(pc + 1, &lo))
377 1.26 rin return -1;
378 1.21 rin
379 1.21 rin *insn = ((uint32_t)hi << 16) | lo;
380 1.21 rin return 4;
381 1.21 rin }
382 1.21 rin
383 1.26 rin if (ufetch_32((uint32_t *)tf->tf_pc, insn))
384 1.26 rin return -1;
385 1.26 rin
386 1.21 rin return 4;
387 1.21 rin }
388 1.21 rin
389 1.26 rin enum emul_arm_result {
390 1.26 rin EMUL_ARM_SUCCESS = 0,
391 1.26 rin EMUL_ARM_UNKNOWN,
392 1.26 rin EMUL_ARM_FAULT,
393 1.26 rin };
394 1.26 rin
395 1.26 rin static enum emul_arm_result
396 1.21 rin emul_arm_insn(struct trapframe *tf)
397 1.21 rin {
398 1.21 rin uint32_t insn;
399 1.21 rin int insn_size;
400 1.21 rin
401 1.21 rin insn_size = fetch_arm_insn(tf, &insn);
402 1.21 rin
403 1.21 rin switch (insn_size) {
404 1.21 rin case 2:
405 1.21 rin /* T32-16bit instruction */
406 1.21 rin
407 1.21 rin /* XXX: some T32 IT instruction deprecated should be emulated */
408 1.21 rin break;
409 1.21 rin case 4:
410 1.21 rin /* T32-32bit instruction, or A32 instruction */
411 1.21 rin
412 1.21 rin /*
413 1.21 rin * Emulate ARMv6 instructions with cache operations
414 1.21 rin * register (c7), that can be used in user mode.
415 1.21 rin */
416 1.21 rin switch (insn & 0x0fff0fff) {
417 1.21 rin case 0x0e070f95:
418 1.21 rin /*
419 1.21 rin * mcr p15, 0, <Rd>, c7, c5, 4
420 1.21 rin * (flush prefetch buffer)
421 1.21 rin */
422 1.21 rin __asm __volatile("isb sy" ::: "memory");
423 1.21 rin goto emulated;
424 1.21 rin case 0x0e070f9a:
425 1.21 rin /*
426 1.21 rin * mcr p15, 0, <Rd>, c7, c10, 4
427 1.21 rin * (data synchronization barrier)
428 1.21 rin */
429 1.21 rin __asm __volatile("dsb sy" ::: "memory");
430 1.21 rin goto emulated;
431 1.21 rin case 0x0e070fba:
432 1.21 rin /*
433 1.21 rin * mcr p15, 0, <Rd>, c7, c10, 5
434 1.21 rin * (data memory barrier)
435 1.21 rin */
436 1.21 rin __asm __volatile("dmb sy" ::: "memory");
437 1.21 rin goto emulated;
438 1.21 rin default:
439 1.21 rin break;
440 1.21 rin }
441 1.21 rin break;
442 1.26 rin default:
443 1.26 rin return EMUL_ARM_FAULT;
444 1.21 rin }
445 1.21 rin
446 1.21 rin /* unknown, or unsupported instruction */
447 1.26 rin return EMUL_ARM_UNKNOWN;
448 1.21 rin
449 1.21 rin emulated:
450 1.21 rin tf->tf_pc += insn_size;
451 1.26 rin return EMUL_ARM_SUCCESS;
452 1.21 rin }
453 1.21 rin #endif /* COMPAT_NETBSD32 */
454 1.21 rin
455 1.2 nisimura void
456 1.4 ryo trap_el0_32sync(struct trapframe *tf)
457 1.2 nisimura {
458 1.4 ryo struct lwp * const l = curlwp;
459 1.4 ryo const uint32_t esr = tf->tf_esr;
460 1.4 ryo const uint32_t eclass = __SHIFTOUT(esr, ESR_EC); /* exception class */
461 1.4 ryo
462 1.14 ryo /* disable trace */
463 1.14 ryo reg_mdscr_el1_write(reg_mdscr_el1_read() & ~MDSCR_SS);
464 1.4 ryo /* enable traps and interrupts */
465 1.4 ryo daif_enable(DAIF_D|DAIF_A|DAIF_I|DAIF_F);
466 1.4 ryo
467 1.4 ryo switch (eclass) {
468 1.11 ryo #ifdef COMPAT_NETBSD32
469 1.11 ryo case ESR_EC_INSN_ABT_EL0:
470 1.11 ryo case ESR_EC_DATA_ABT_EL0:
471 1.11 ryo data_abort_handler(tf, eclass);
472 1.11 ryo userret(l);
473 1.11 ryo break;
474 1.11 ryo
475 1.11 ryo case ESR_EC_SVC_A32:
476 1.11 ryo (*l->l_proc->p_md.md_syscall)(tf);
477 1.11 ryo break;
478 1.19 skrll
479 1.4 ryo case ESR_EC_FP_ACCESS:
480 1.4 ryo fpu_load(l);
481 1.4 ryo userret(l);
482 1.4 ryo break;
483 1.19 skrll
484 1.11 ryo case ESR_EC_FP_TRAP_A32:
485 1.11 ryo do_trapsignal(l, SIGFPE, FPE_FLTUND, NULL, esr); /* XXX */
486 1.4 ryo userret(l);
487 1.18 jmcneill break;
488 1.4 ryo
489 1.4 ryo case ESR_EC_PC_ALIGNMENT:
490 1.5 christos do_trapsignal(l, SIGBUS, BUS_ADRALN, (void *)tf->tf_pc, esr);
491 1.4 ryo userret(l);
492 1.4 ryo break;
493 1.19 skrll
494 1.4 ryo case ESR_EC_SP_ALIGNMENT:
495 1.11 ryo do_trapsignal(l, SIGBUS, BUS_ADRALN,
496 1.11 ryo (void *)tf->tf_reg[13], esr); /* sp is r13 on AArch32 */
497 1.4 ryo userret(l);
498 1.4 ryo break;
499 1.4 ryo
500 1.11 ryo case ESR_EC_BKPT_INSN_A32:
501 1.11 ryo do_trapsignal(l, SIGTRAP, TRAP_BRKPT, (void *)tf->tf_pc, esr);
502 1.11 ryo userret(l);
503 1.4 ryo break;
504 1.11 ryo
505 1.21 rin case ESR_EC_UNKNOWN:
506 1.26 rin switch (emul_arm_insn(tf)) {
507 1.26 rin case EMUL_ARM_SUCCESS:
508 1.26 rin break;
509 1.26 rin case EMUL_ARM_UNKNOWN:
510 1.21 rin goto unknown;
511 1.26 rin case EMUL_ARM_FAULT:
512 1.26 rin do_trapsignal(l, SIGSEGV, SEGV_MAPERR,
513 1.26 rin (void *)tf->tf_pc, esr);
514 1.26 rin break;
515 1.26 rin }
516 1.21 rin userret(l);
517 1.21 rin break;
518 1.21 rin
519 1.4 ryo case ESR_EC_CP15_RT:
520 1.4 ryo case ESR_EC_CP15_RRT:
521 1.4 ryo case ESR_EC_CP14_RT:
522 1.4 ryo case ESR_EC_CP14_DT:
523 1.4 ryo case ESR_EC_CP14_RRT:
524 1.21 rin unknown:
525 1.4 ryo #endif /* COMPAT_NETBSD32 */
526 1.4 ryo default:
527 1.11 ryo #ifdef DDB
528 1.11 ryo if (sigill_debug) {
529 1.11 ryo /* show illegal instruction */
530 1.11 ryo printf("TRAP: pid %d (%s), uid %d: %s:"
531 1.11 ryo " esr=0x%lx: pc=0x%lx: %s\n",
532 1.11 ryo curlwp->l_proc->p_pid, curlwp->l_proc->p_comm,
533 1.11 ryo l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1,
534 1.11 ryo eclass_trapname(eclass), tf->tf_esr, tf->tf_pc,
535 1.11 ryo strdisasm_aarch32(tf->tf_pc));
536 1.11 ryo }
537 1.11 ryo #endif
538 1.11 ryo /* illegal or not implemented instruction */
539 1.5 christos do_trapsignal(l, SIGILL, ILL_ILLTRP, (void *)tf->tf_pc, esr);
540 1.4 ryo userret(l);
541 1.4 ryo break;
542 1.4 ryo }
543 1.4 ryo }
544 1.4 ryo
545 1.4 ryo #define bad_trap_panic(trapfunc) \
546 1.4 ryo void \
547 1.4 ryo trapfunc(struct trapframe *tf) \
548 1.4 ryo { \
549 1.4 ryo panic("%s", __func__); \
550 1.4 ryo }
551 1.4 ryo bad_trap_panic(trap_el1t_sync)
552 1.4 ryo bad_trap_panic(trap_el1t_irq)
553 1.4 ryo bad_trap_panic(trap_el1t_fiq)
554 1.4 ryo bad_trap_panic(trap_el1t_error)
555 1.4 ryo bad_trap_panic(trap_el1h_fiq)
556 1.4 ryo bad_trap_panic(trap_el1h_error)
557 1.4 ryo bad_trap_panic(trap_el0_fiq)
558 1.4 ryo bad_trap_panic(trap_el0_error)
559 1.4 ryo bad_trap_panic(trap_el0_32fiq)
560 1.4 ryo bad_trap_panic(trap_el0_32error)
561 1.2 nisimura
562 1.4 ryo void
563 1.4 ryo cpu_jump_onfault(struct trapframe *tf, const struct faultbuf *fb, int val)
564 1.4 ryo {
565 1.2 nisimura tf->tf_reg[19] = fb->fb_reg[FB_X19];
566 1.2 nisimura tf->tf_reg[20] = fb->fb_reg[FB_X20];
567 1.2 nisimura tf->tf_reg[21] = fb->fb_reg[FB_X21];
568 1.2 nisimura tf->tf_reg[22] = fb->fb_reg[FB_X22];
569 1.2 nisimura tf->tf_reg[23] = fb->fb_reg[FB_X23];
570 1.2 nisimura tf->tf_reg[24] = fb->fb_reg[FB_X24];
571 1.2 nisimura tf->tf_reg[25] = fb->fb_reg[FB_X25];
572 1.2 nisimura tf->tf_reg[26] = fb->fb_reg[FB_X26];
573 1.2 nisimura tf->tf_reg[27] = fb->fb_reg[FB_X27];
574 1.2 nisimura tf->tf_reg[28] = fb->fb_reg[FB_X28];
575 1.2 nisimura tf->tf_reg[29] = fb->fb_reg[FB_X29];
576 1.2 nisimura tf->tf_sp = fb->fb_reg[FB_SP];
577 1.4 ryo tf->tf_pc = fb->fb_reg[FB_LR];
578 1.4 ryo tf->tf_reg[0] = val;
579 1.2 nisimura }
580 1.2 nisimura
581 1.6 christos #ifdef TRAP_SIGDEBUG
582 1.6 christos static void
583 1.6 christos frame_dump(const struct trapframe *tf)
584 1.6 christos {
585 1.6 christos const struct reg *r = &tf->tf_regs;
586 1.6 christos
587 1.6 christos printf("trapframe %p\n", tf);
588 1.6 christos for (size_t i = 0; i < __arraycount(r->r_reg); i++) {
589 1.7 christos printf(" r%.2zu %#018" PRIx64 "%c", i, r->r_reg[i],
590 1.6 christos " \n"[i && (i & 1) == 0]);
591 1.6 christos }
592 1.6 christos
593 1.6 christos printf("\n");
594 1.6 christos printf(" sp %#018" PRIx64 " pc %#018" PRIx64 "\n",
595 1.6 christos r->r_sp, r->r_pc);
596 1.6 christos printf(" spsr %#018" PRIx64 " tpidr %#018" PRIx64 "\n",
597 1.6 christos r->r_spsr, r->r_tpidr);
598 1.6 christos printf(" esr %#018" PRIx64 " far %#018" PRIx64 "\n",
599 1.6 christos tf->tf_esr, tf->tf_far);
600 1.6 christos
601 1.6 christos printf("\n");
602 1.6 christos hexdump(printf, "Stack dump", tf, 256);
603 1.6 christos }
604 1.6 christos
605 1.6 christos static void
606 1.6 christos sigdebug(const struct trapframe *tf, const ksiginfo_t *ksi)
607 1.6 christos {
608 1.6 christos struct lwp *l = curlwp;
609 1.6 christos struct proc *p = l->l_proc;
610 1.6 christos const uint32_t eclass = __SHIFTOUT(ksi->ksi_trap, ESR_EC);
611 1.6 christos
612 1.6 christos printf("pid %d.%d (%s): signal %d (trap %#x) "
613 1.6 christos "@pc %#" PRIx64 ", addr %p, error=%s\n",
614 1.6 christos p->p_pid, l->l_lid, p->p_comm, ksi->ksi_signo, ksi->ksi_trap,
615 1.6 christos tf->tf_regs.r_pc, ksi->ksi_addr, eclass_trapname(eclass));
616 1.6 christos frame_dump(tf);
617 1.6 christos }
618 1.6 christos #endif
619 1.6 christos
620 1.6 christos void do_trapsignal1(
621 1.6 christos #ifdef TRAP_SIGDEBUG
622 1.6 christos const char *func,
623 1.6 christos size_t line,
624 1.6 christos struct trapframe *tf,
625 1.6 christos #endif
626 1.6 christos struct lwp *l, int signo, int code, void *addr, int trap)
627 1.6 christos {
628 1.6 christos ksiginfo_t ksi;
629 1.6 christos
630 1.6 christos KSI_INIT_TRAP(&ksi);
631 1.6 christos ksi.ksi_signo = signo;
632 1.6 christos ksi.ksi_code = code;
633 1.6 christos ksi.ksi_addr = addr;
634 1.6 christos ksi.ksi_trap = trap;
635 1.6 christos #ifdef TRAP_SIGDEBUG
636 1.6 christos printf("%s, %zu: ", func, line);
637 1.6 christos sigdebug(tf, &ksi);
638 1.6 christos #endif
639 1.6 christos (*l->l_proc->p_emul->e_trapsignal)(l, &ksi);
640 1.6 christos }
641 1.23 ad
642 1.23 ad bool
643 1.23 ad cpu_intr_p(void)
644 1.23 ad {
645 1.23 ad uint64_t ncsw;
646 1.23 ad int idepth;
647 1.23 ad lwp_t *l;
648 1.23 ad
649 1.23 ad #ifdef __HAVE_PIC_FAST_SOFTINTS
650 1.23 ad /* XXX Copied from cpu.h. Looks incomplete - needs fixing. */
651 1.23 ad if (ci->ci_cpl < IPL_VM)
652 1.23 ad return false;
653 1.23 ad #endif
654 1.23 ad
655 1.23 ad l = curlwp;
656 1.23 ad if (__predict_false(l->l_cpu == NULL)) {
657 1.23 ad KASSERT(l == &lwp0);
658 1.23 ad return false;
659 1.23 ad }
660 1.23 ad do {
661 1.23 ad ncsw = l->l_ncsw;
662 1.23 ad __insn_barrier();
663 1.24 skrll idepth = l->l_cpu->ci_intr_depth;
664 1.23 ad __insn_barrier();
665 1.23 ad } while (__predict_false(ncsw != l->l_ncsw));
666 1.23 ad
667 1.23 ad return idepth > 0;
668 1.23 ad }
669