xref: /freebsd/sys/arm64/arm64/trap.c (revision 2c10be9e)
1 /*-
2  * Copyright (c) 2014 Andrew Turner
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include "opt_ddb.h"
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/ktr.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/ptrace.h>
41 #include <sys/syscall.h>
42 #include <sys/sysent.h>
43 #ifdef KDB
44 #include <sys/kdb.h>
45 #endif
46 
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_param.h>
52 #include <vm/vm_extern.h>
53 
54 #include <machine/frame.h>
55 #include <machine/md_var.h>
56 #include <machine/pcb.h>
57 #include <machine/pcpu.h>
58 #include <machine/undefined.h>
59 
60 #ifdef KDTRACE_HOOKS
61 #include <sys/dtrace_bsd.h>
62 #endif
63 
64 #ifdef VFP
65 #include <machine/vfp.h>
66 #endif
67 
68 #ifdef KDB
69 #include <machine/db_machdep.h>
70 #endif
71 
72 #ifdef DDB
73 #include <ddb/ddb.h>
74 #include <ddb/db_sym.h>
75 #endif
76 
77 /* Called from exception.S */
78 void do_el1h_sync(struct thread *, struct trapframe *);
79 void do_el0_sync(struct thread *, struct trapframe *);
80 void do_el0_error(struct trapframe *);
81 void do_serror(struct trapframe *);
82 void unhandled_exception(struct trapframe *);
83 
84 static void print_gp_register(const char *name, uint64_t value);
85 static void print_registers(struct trapframe *frame);
86 
87 int (*dtrace_invop_jump_addr)(struct trapframe *);
88 
89 typedef void (abort_handler)(struct thread *, struct trapframe *, uint64_t,
90     uint64_t, int);
91 
92 static abort_handler align_abort;
93 static abort_handler data_abort;
94 static abort_handler external_abort;
95 
96 static abort_handler *abort_handlers[] = {
97 	[ISS_DATA_DFSC_TF_L0] = data_abort,
98 	[ISS_DATA_DFSC_TF_L1] = data_abort,
99 	[ISS_DATA_DFSC_TF_L2] = data_abort,
100 	[ISS_DATA_DFSC_TF_L3] = data_abort,
101 	[ISS_DATA_DFSC_AFF_L1] = data_abort,
102 	[ISS_DATA_DFSC_AFF_L2] = data_abort,
103 	[ISS_DATA_DFSC_AFF_L3] = data_abort,
104 	[ISS_DATA_DFSC_PF_L1] = data_abort,
105 	[ISS_DATA_DFSC_PF_L2] = data_abort,
106 	[ISS_DATA_DFSC_PF_L3] = data_abort,
107 	[ISS_DATA_DFSC_ALIGN] = align_abort,
108 	[ISS_DATA_DFSC_EXT] =  external_abort,
109 	[ISS_DATA_DFSC_EXT_L0] =  external_abort,
110 	[ISS_DATA_DFSC_EXT_L1] =  external_abort,
111 	[ISS_DATA_DFSC_EXT_L2] =  external_abort,
112 	[ISS_DATA_DFSC_EXT_L3] =  external_abort,
113 	[ISS_DATA_DFSC_ECC] =  external_abort,
114 	[ISS_DATA_DFSC_ECC_L0] =  external_abort,
115 	[ISS_DATA_DFSC_ECC_L1] =  external_abort,
116 	[ISS_DATA_DFSC_ECC_L2] =  external_abort,
117 	[ISS_DATA_DFSC_ECC_L3] =  external_abort,
118 };
119 
120 static __inline void
121 call_trapsignal(struct thread *td, int sig, int code, void *addr, int trapno)
122 {
123 	ksiginfo_t ksi;
124 
125 	ksiginfo_init_trap(&ksi);
126 	ksi.ksi_signo = sig;
127 	ksi.ksi_code = code;
128 	ksi.ksi_addr = addr;
129 	ksi.ksi_trapno = trapno;
130 	trapsignal(td, &ksi);
131 }
132 
133 int
134 cpu_fetch_syscall_args(struct thread *td)
135 {
136 	struct proc *p;
137 	syscallarg_t *ap, *dst_ap;
138 	struct syscall_args *sa;
139 
140 	p = td->td_proc;
141 	sa = &td->td_sa;
142 	ap = td->td_frame->tf_x;
143 	dst_ap = &sa->args[0];
144 
145 	sa->code = td->td_frame->tf_x[8];
146 	sa->original_code = sa->code;
147 
148 	if (__predict_false(sa->code == SYS_syscall || sa->code == SYS___syscall)) {
149 		sa->code = *ap++;
150 	} else {
151 		*dst_ap++ = *ap++;
152 	}
153 
154 	if (__predict_false(sa->code >= p->p_sysent->sv_size))
155 		sa->callp = &p->p_sysent->sv_table[0];
156 	else
157 		sa->callp = &p->p_sysent->sv_table[sa->code];
158 
159 	KASSERT(sa->callp->sy_narg <= nitems(sa->args),
160 	    ("Syscall %d takes too many arguments", sa->code));
161 
162 	memcpy(dst_ap, ap, (nitems(sa->args) - 1) * sizeof(*dst_ap));
163 
164 	td->td_retval[0] = 0;
165 	td->td_retval[1] = 0;
166 
167 	return (0);
168 }
169 
170 #include "../../kern/subr_syscall.c"
171 
172 /*
173  * Test for fault generated by given access instruction in
174  * bus_peek_<foo> or bus_poke_<foo> bus function.
175  */
176 extern uint32_t generic_bs_peek_1f, generic_bs_peek_2f;
177 extern uint32_t generic_bs_peek_4f, generic_bs_peek_8f;
178 extern uint32_t generic_bs_poke_1f, generic_bs_poke_2f;
179 extern uint32_t generic_bs_poke_4f, generic_bs_poke_8f;
180 
181 static bool
182 test_bs_fault(void *addr)
183 {
184 	return (addr == &generic_bs_peek_1f ||
185 	    addr == &generic_bs_peek_2f ||
186 	    addr == &generic_bs_peek_4f ||
187 	    addr == &generic_bs_peek_8f ||
188 	    addr == &generic_bs_poke_1f ||
189 	    addr == &generic_bs_poke_2f ||
190 	    addr == &generic_bs_poke_4f ||
191 	    addr == &generic_bs_poke_8f);
192 }
193 
194 static void
195 svc_handler(struct thread *td, struct trapframe *frame)
196 {
197 
198 	if ((frame->tf_esr & ESR_ELx_ISS_MASK) == 0) {
199 		syscallenter(td);
200 		syscallret(td);
201 	} else {
202 		call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr,
203 		    ESR_ELx_EXCEPTION(frame->tf_esr));
204 		userret(td, frame);
205 	}
206 }
207 
208 static void
209 align_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
210     uint64_t far, int lower)
211 {
212 	if (!lower) {
213 		print_registers(frame);
214 		print_gp_register("far", far);
215 		printf(" esr:         %.8lx\n", esr);
216 		panic("Misaligned access from kernel space!");
217 	}
218 
219 	call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr,
220 	    ESR_ELx_EXCEPTION(frame->tf_esr));
221 	userret(td, frame);
222 }
223 
224 
225 static void
226 external_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
227     uint64_t far, int lower)
228 {
229 
230 	/*
231 	 * Try to handle synchronous external aborts caused by
232 	 * bus_space_peek() and/or bus_space_poke() functions.
233 	 */
234 	if (!lower && test_bs_fault((void *)frame->tf_elr)) {
235 		frame->tf_elr = (uint64_t)generic_bs_fault;
236 		return;
237 	}
238 
239 	print_registers(frame);
240 	print_gp_register("far", far);
241 	panic("Unhandled EL%d external data abort", lower ? 0: 1);
242 }
243 
244 static void
245 data_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
246     uint64_t far, int lower)
247 {
248 	struct vm_map *map;
249 	struct pcb *pcb;
250 	vm_prot_t ftype;
251 	int error, sig, ucode;
252 #ifdef KDB
253 	bool handled;
254 #endif
255 
256 	/*
257 	 * According to the ARMv8-A rev. A.g, B2.10.5 "Load-Exclusive
258 	 * and Store-Exclusive instruction usage restrictions", state
259 	 * of the exclusive monitors after data abort exception is unknown.
260 	 */
261 	clrex();
262 
263 #ifdef KDB
264 	if (kdb_active) {
265 		kdb_reenter();
266 		return;
267 	}
268 #endif
269 
270 	if (lower) {
271 		map = &td->td_proc->p_vmspace->vm_map;
272 	} else if (!ADDR_IS_CANONICAL(far)) {
273 		/* We received a TBI/PAC/etc. fault from the kernel */
274 		error = KERN_INVALID_ADDRESS;
275 		goto bad_far;
276 	} else if (ADDR_IS_KERNEL(far)) {
277 		/*
278 		 * Handle a special case: the data abort was caused by accessing
279 		 * a thread structure while its mapping was being promoted or
280 		 * demoted, as a consequence of the break-before-make rule.  It
281 		 * is not safe to enable interrupts or dereference "td" before
282 		 * this case is handled.
283 		 *
284 		 * In principle, if pmap_klookup() fails, there is no need to
285 		 * call pmap_fault() below, but avoiding that call is not worth
286 		 * the effort.
287 		 */
288 		if (ESR_ELx_EXCEPTION(esr) == EXCP_DATA_ABORT) {
289 			switch (esr & ISS_DATA_DFSC_MASK) {
290 			case ISS_DATA_DFSC_TF_L0:
291 			case ISS_DATA_DFSC_TF_L1:
292 			case ISS_DATA_DFSC_TF_L2:
293 			case ISS_DATA_DFSC_TF_L3:
294 				if (pmap_klookup(far, NULL))
295 					return;
296 				break;
297 			}
298 		}
299 		intr_enable();
300 		map = kernel_map;
301 	} else {
302 		intr_enable();
303 		map = &td->td_proc->p_vmspace->vm_map;
304 		if (map == NULL)
305 			map = kernel_map;
306 	}
307 	pcb = td->td_pcb;
308 
309 	/*
310 	 * Try to handle translation, access flag, and permission faults.
311 	 * Translation faults may occur as a result of the required
312 	 * break-before-make sequence used when promoting or demoting
313 	 * superpages.  Such faults must not occur while holding the pmap lock,
314 	 * or pmap_fault() will recurse on that lock.
315 	 */
316 	if ((lower || map == kernel_map || pcb->pcb_onfault != 0) &&
317 	    pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
318 		return;
319 
320 	KASSERT(td->td_md.md_spinlock_count == 0,
321 	    ("data abort with spinlock held"));
322 	if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
323 	    WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
324 		print_registers(frame);
325 		print_gp_register("far", far);
326 		printf(" esr:         %.8lx\n", esr);
327 		panic("data abort in critical section or under mutex");
328 	}
329 
330 	switch (ESR_ELx_EXCEPTION(esr)) {
331 	case EXCP_INSN_ABORT:
332 	case EXCP_INSN_ABORT_L:
333 		ftype = VM_PROT_EXECUTE;
334 		break;
335 	default:
336 		/*
337 		 * If the exception was because of a read or cache operation
338 		 * pass a read fault type into the vm code. Cache operations
339 		 * need read permission but will set the WnR flag when the
340 		 * memory is unmapped.
341 		 */
342 		if ((esr & ISS_DATA_WnR) == 0 || (esr & ISS_DATA_CM) != 0)
343 			ftype = VM_PROT_READ;
344 		else
345 			ftype = VM_PROT_WRITE;
346 		break;
347 	}
348 
349 	/* Fault in the page. */
350 	error = vm_fault_trap(map, far, ftype, VM_FAULT_NORMAL, &sig, &ucode);
351 	if (error != KERN_SUCCESS) {
352 		if (lower) {
353 			call_trapsignal(td, sig, ucode, (void *)far,
354 			    ESR_ELx_EXCEPTION(esr));
355 		} else {
356 bad_far:
357 			if (td->td_intr_nesting_level == 0 &&
358 			    pcb->pcb_onfault != 0) {
359 				frame->tf_x[0] = error;
360 				frame->tf_elr = pcb->pcb_onfault;
361 				return;
362 			}
363 
364 			printf("Fatal data abort:\n");
365 			print_registers(frame);
366 			print_gp_register("far", far);
367 			printf(" esr:         %.8lx\n", esr);
368 
369 #ifdef KDB
370 			if (debugger_on_trap) {
371 				kdb_why = KDB_WHY_TRAP;
372 				handled = kdb_trap(ESR_ELx_EXCEPTION(esr), 0,
373 				    frame);
374 				kdb_why = KDB_WHY_UNSET;
375 				if (handled)
376 					return;
377 			}
378 #endif
379 			panic("vm_fault failed: %lx error %d",
380 			    frame->tf_elr, error);
381 		}
382 	}
383 
384 	if (lower)
385 		userret(td, frame);
386 }
387 
388 static void
389 print_gp_register(const char *name, uint64_t value)
390 {
391 #if defined(DDB)
392 	c_db_sym_t sym;
393 	const char *sym_name;
394 	db_expr_t sym_value;
395 	db_expr_t offset;
396 #endif
397 
398 	printf(" %s: %16lx", name, value);
399 #if defined(DDB)
400 	/* If this looks like a kernel address try to find the symbol */
401 	if (value >= VM_MIN_KERNEL_ADDRESS) {
402 		sym = db_search_symbol(value, DB_STGY_ANY, &offset);
403 		if (sym != C_DB_SYM_NULL) {
404 			db_symbol_values(sym, &sym_name, &sym_value);
405 			printf(" (%s + %lx)", sym_name, offset);
406 		}
407 	}
408 #endif
409 	printf("\n");
410 }
411 
412 static void
413 print_registers(struct trapframe *frame)
414 {
415 	char name[4];
416 	u_int reg;
417 
418 	for (reg = 0; reg < nitems(frame->tf_x); reg++) {
419 		snprintf(name, sizeof(name), "%sx%d", (reg < 10) ? " " : "",
420 		    reg);
421 		print_gp_register(name, frame->tf_x[reg]);
422 	}
423 	printf("  sp: %16lx\n", frame->tf_sp);
424 	print_gp_register(" lr", frame->tf_lr);
425 	print_gp_register("elr", frame->tf_elr);
426 	printf("spsr:         %8x\n", frame->tf_spsr);
427 }
428 
429 #ifdef VFP
430 static void
431 fpe_trap(struct thread *td, void *addr, uint32_t exception)
432 {
433 	int code;
434 
435 	code = FPE_FLTIDO;
436 	if ((exception & ISS_FP_TFV) != 0) {
437 		if ((exception & ISS_FP_IOF) != 0)
438 			code = FPE_FLTINV;
439 		else if ((exception & ISS_FP_DZF) != 0)
440 			code = FPE_FLTDIV;
441 		else if ((exception & ISS_FP_OFF) != 0)
442 			code = FPE_FLTOVF;
443 		else if ((exception & ISS_FP_UFF) != 0)
444 			code = FPE_FLTUND;
445 		else if ((exception & ISS_FP_IXF) != 0)
446 			code = FPE_FLTRES;
447 	}
448 	call_trapsignal(td, SIGFPE, code, addr, exception);
449 }
450 #endif
451 
452 void
453 do_el1h_sync(struct thread *td, struct trapframe *frame)
454 {
455 	uint32_t exception;
456 	uint64_t esr, far;
457 	int dfsc;
458 
459 	/* Read the esr register to get the exception details */
460 	esr = frame->tf_esr;
461 	exception = ESR_ELx_EXCEPTION(esr);
462 
463 #ifdef KDTRACE_HOOKS
464 	if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
465 		return;
466 #endif
467 
468 	CTR4(KTR_TRAP,
469 	    "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td,
470 	    esr, frame->tf_elr, frame);
471 
472 	/*
473 	 * Enable debug exceptions if we aren't already handling one. They will
474 	 * be masked again in the exception handler's epilogue.
475 	 */
476 	if (exception != EXCP_BRK && exception != EXCP_WATCHPT_EL1 &&
477 	    exception != EXCP_SOFTSTP_EL1)
478 		dbg_enable();
479 
480 	switch (exception) {
481 	case EXCP_FP_SIMD:
482 	case EXCP_TRAP_FP:
483 #ifdef VFP
484 		if ((td->td_pcb->pcb_fpflags & PCB_FP_KERN) != 0) {
485 			vfp_restore_state();
486 		} else
487 #endif
488 		{
489 			print_registers(frame);
490 			printf(" esr:         %.8lx\n", esr);
491 			panic("VFP exception in the kernel");
492 		}
493 		break;
494 	case EXCP_INSN_ABORT:
495 	case EXCP_DATA_ABORT:
496 		far = READ_SPECIALREG(far_el1);
497 		dfsc = esr & ISS_DATA_DFSC_MASK;
498 		if (dfsc < nitems(abort_handlers) &&
499 		    abort_handlers[dfsc] != NULL) {
500 			abort_handlers[dfsc](td, frame, esr, far, 0);
501 		} else {
502 			print_registers(frame);
503 			print_gp_register("far", far);
504 			printf(" esr:         %.8lx\n", esr);
505 			panic("Unhandled EL1 %s abort: %x",
506 			    exception == EXCP_INSN_ABORT ? "instruction" :
507 			    "data", dfsc);
508 		}
509 		break;
510 	case EXCP_BRK:
511 #ifdef KDTRACE_HOOKS
512 		if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
513 		    dtrace_invop_jump_addr != 0) {
514 			dtrace_invop_jump_addr(frame);
515 			break;
516 		}
517 #endif
518 #ifdef KDB
519 		kdb_trap(exception, 0, frame);
520 #else
521 		panic("No debugger in kernel.");
522 #endif
523 		break;
524 	case EXCP_WATCHPT_EL1:
525 	case EXCP_SOFTSTP_EL1:
526 #ifdef KDB
527 		kdb_trap(exception, 0, frame);
528 #else
529 		panic("No debugger in kernel.");
530 #endif
531 		break;
532 	case EXCP_FPAC:
533 		/* We can see this if the authentication on PAC fails */
534 		print_registers(frame);
535 		printf(" far: %16lx\n", READ_SPECIALREG(far_el1));
536 		panic("FPAC kernel exception");
537 		break;
538 	case EXCP_UNKNOWN:
539 		if (undef_insn(1, frame))
540 			break;
541 		printf("Undefined instruction: %08x\n",
542 		    *(uint32_t *)frame->tf_elr);
543 		/* FALLTHROUGH */
544 	default:
545 		print_registers(frame);
546 		print_gp_register("far", READ_SPECIALREG(far_el1));
547 		panic("Unknown kernel exception %x esr_el1 %lx", exception,
548 		    esr);
549 	}
550 }
551 
552 void
553 do_el0_sync(struct thread *td, struct trapframe *frame)
554 {
555 	pcpu_bp_harden bp_harden;
556 	uint32_t exception;
557 	uint64_t esr, far;
558 	int dfsc;
559 
560 	/* Check we have a sane environment when entering from userland */
561 	KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
562 	    ("Invalid pcpu address from userland: %p (tpidr %lx)",
563 	     get_pcpu(), READ_SPECIALREG(tpidr_el1)));
564 
565 	esr = frame->tf_esr;
566 	exception = ESR_ELx_EXCEPTION(esr);
567 	switch (exception) {
568 	case EXCP_INSN_ABORT_L:
569 		far = READ_SPECIALREG(far_el1);
570 
571 		/*
572 		 * Userspace may be trying to train the branch predictor to
573 		 * attack the kernel. If we are on a CPU affected by this
574 		 * call the handler to clear the branch predictor state.
575 		 */
576 		if (far > VM_MAXUSER_ADDRESS) {
577 			bp_harden = PCPU_GET(bp_harden);
578 			if (bp_harden != NULL)
579 				bp_harden();
580 		}
581 		break;
582 	case EXCP_UNKNOWN:
583 	case EXCP_DATA_ABORT_L:
584 	case EXCP_DATA_ABORT:
585 	case EXCP_WATCHPT_EL0:
586 		far = READ_SPECIALREG(far_el1);
587 		break;
588 	}
589 	intr_enable();
590 
591 	CTR4(KTR_TRAP,
592 	    "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td, esr,
593 	    frame->tf_elr, frame);
594 
595 	switch (exception) {
596 	case EXCP_FP_SIMD:
597 #ifdef VFP
598 		vfp_restore_state();
599 #else
600 		panic("VFP exception in userland");
601 #endif
602 		break;
603 	case EXCP_TRAP_FP:
604 #ifdef VFP
605 		fpe_trap(td, (void *)frame->tf_elr, esr);
606 		userret(td, frame);
607 #else
608 		panic("VFP exception in userland");
609 #endif
610 		break;
611 	case EXCP_SVE:
612 		call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_elr,
613 		    exception);
614 		userret(td, frame);
615 		break;
616 	case EXCP_SVC32:
617 	case EXCP_SVC64:
618 		svc_handler(td, frame);
619 		break;
620 	case EXCP_INSN_ABORT_L:
621 	case EXCP_DATA_ABORT_L:
622 	case EXCP_DATA_ABORT:
623 		dfsc = esr & ISS_DATA_DFSC_MASK;
624 		if (dfsc < nitems(abort_handlers) &&
625 		    abort_handlers[dfsc] != NULL)
626 			abort_handlers[dfsc](td, frame, esr, far, 1);
627 		else {
628 			print_registers(frame);
629 			print_gp_register("far", far);
630 			printf(" esr:         %.8lx\n", esr);
631 			panic("Unhandled EL0 %s abort: %x",
632 			    exception == EXCP_INSN_ABORT_L ? "instruction" :
633 			    "data", dfsc);
634 		}
635 		break;
636 	case EXCP_UNKNOWN:
637 		if (!undef_insn(0, frame))
638 			call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far,
639 			    exception);
640 		userret(td, frame);
641 		break;
642 	case EXCP_FPAC:
643 		call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr,
644 		    exception);
645 		userret(td, frame);
646 		break;
647 	case EXCP_SP_ALIGN:
648 		call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp,
649 		    exception);
650 		userret(td, frame);
651 		break;
652 	case EXCP_PC_ALIGN:
653 		call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr,
654 		    exception);
655 		userret(td, frame);
656 		break;
657 	case EXCP_BRKPT_EL0:
658 	case EXCP_BRK:
659 #ifdef COMPAT_FREEBSD32
660 	case EXCP_BRKPT_32:
661 #endif /* COMPAT_FREEBSD32 */
662 		call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr,
663 		    exception);
664 		userret(td, frame);
665 		break;
666 	case EXCP_WATCHPT_EL0:
667 		call_trapsignal(td, SIGTRAP, TRAP_TRACE, (void *)far,
668 		    exception);
669 		userret(td, frame);
670 		break;
671 	case EXCP_MSR:
672 		/*
673 		 * The CPU can raise EXCP_MSR when userspace executes an mrs
674 		 * instruction to access a special register userspace doesn't
675 		 * have access to.
676 		 */
677 		if (!undef_insn(0, frame))
678 			call_trapsignal(td, SIGILL, ILL_PRVOPC,
679 			    (void *)frame->tf_elr, exception);
680 		userret(td, frame);
681 		break;
682 	case EXCP_SOFTSTP_EL0:
683 		PROC_LOCK(td->td_proc);
684 		if ((td->td_dbgflags & TDB_STEP) != 0) {
685 			td->td_frame->tf_spsr &= ~PSR_SS;
686 			td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
687 			WRITE_SPECIALREG(mdscr_el1,
688 			    READ_SPECIALREG(mdscr_el1) & ~MDSCR_SS);
689 		}
690 		PROC_UNLOCK(td->td_proc);
691 		call_trapsignal(td, SIGTRAP, TRAP_TRACE,
692 		    (void *)frame->tf_elr, exception);
693 		userret(td, frame);
694 		break;
695 	default:
696 		call_trapsignal(td, SIGBUS, BUS_OBJERR, (void *)frame->tf_elr,
697 		    exception);
698 		userret(td, frame);
699 		break;
700 	}
701 
702 	KASSERT((td->td_pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
703 	    ("Kernel VFP flags set while entering userspace"));
704 	KASSERT(
705 	    td->td_pcb->pcb_fpusaved == &td->td_pcb->pcb_fpustate,
706 	    ("Kernel VFP state in use when entering userspace"));
707 }
708 
709 /*
710  * TODO: We will need to handle these later when we support ARMv8.2 RAS.
711  */
712 void
713 do_serror(struct trapframe *frame)
714 {
715 	uint64_t esr, far;
716 
717 	far = READ_SPECIALREG(far_el1);
718 	esr = frame->tf_esr;
719 
720 	print_registers(frame);
721 	print_gp_register("far", far);
722 	printf(" esr:         %.8lx\n", esr);
723 	panic("Unhandled System Error");
724 }
725 
726 void
727 unhandled_exception(struct trapframe *frame)
728 {
729 	uint64_t esr, far;
730 
731 	far = READ_SPECIALREG(far_el1);
732 	esr = frame->tf_esr;
733 
734 	print_registers(frame);
735 	print_gp_register("far", far);
736 	printf(" esr:         %.8lx\n", esr);
737 	panic("Unhandled exception");
738 }
739