2 * Copyright (c) 2014 Andrew Turner
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
39 #include <sys/mutex.h>
41 #include <sys/ptrace.h>
42 #include <sys/syscall.h>
43 #include <sys/sysent.h>
50 #include <vm/vm_kern.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_param.h>
53 #include <vm/vm_extern.h>
55 #include <machine/frame.h>
56 #include <machine/md_var.h>
57 #include <machine/pcb.h>
58 #include <machine/pcpu.h>
59 #include <machine/undefined.h>
62 #include <sys/dtrace_bsd.h>
66 #include <machine/vfp.h>
70 #include <machine/db_machdep.h>
75 #include <ddb/db_sym.h>
78 /* Called from exception.S */
79 void do_el1h_sync(struct thread *, struct trapframe *);
80 void do_el0_sync(struct thread *, struct trapframe *);
81 void do_el0_error(struct trapframe *);
82 void do_serror(struct trapframe *);
83 void unhandled_exception(struct trapframe *);
85 static void print_gp_register(const char *name, uint64_t value);
86 static void print_registers(struct trapframe *frame);
88 int (*dtrace_invop_jump_addr)(struct trapframe *);
90 typedef void (abort_handler)(struct thread *, struct trapframe *, uint64_t,
93 static abort_handler align_abort;
94 static abort_handler data_abort;
95 static abort_handler external_abort;
97 static abort_handler *abort_handlers[] = {
98 [ISS_DATA_DFSC_TF_L0] = data_abort,
99 [ISS_DATA_DFSC_TF_L1] = data_abort,
100 [ISS_DATA_DFSC_TF_L2] = data_abort,
101 [ISS_DATA_DFSC_TF_L3] = data_abort,
102 [ISS_DATA_DFSC_AFF_L1] = data_abort,
103 [ISS_DATA_DFSC_AFF_L2] = data_abort,
104 [ISS_DATA_DFSC_AFF_L3] = data_abort,
105 [ISS_DATA_DFSC_PF_L1] = data_abort,
106 [ISS_DATA_DFSC_PF_L2] = data_abort,
107 [ISS_DATA_DFSC_PF_L3] = data_abort,
108 [ISS_DATA_DFSC_ALIGN] = align_abort,
109 [ISS_DATA_DFSC_EXT] = external_abort,
110 [ISS_DATA_DFSC_EXT_L0] = external_abort,
111 [ISS_DATA_DFSC_EXT_L1] = external_abort,
112 [ISS_DATA_DFSC_EXT_L2] = external_abort,
113 [ISS_DATA_DFSC_EXT_L3] = external_abort,
114 [ISS_DATA_DFSC_ECC] = external_abort,
115 [ISS_DATA_DFSC_ECC_L0] = external_abort,
116 [ISS_DATA_DFSC_ECC_L1] = external_abort,
117 [ISS_DATA_DFSC_ECC_L2] = external_abort,
118 [ISS_DATA_DFSC_ECC_L3] = external_abort,
122 call_trapsignal(struct thread *td, int sig, int code, void *addr, int trapno)
126 ksiginfo_init_trap(&ksi);
130 ksi.ksi_trapno = trapno;
131 trapsignal(td, &ksi);
135 cpu_fetch_syscall_args(struct thread *td)
138 syscallarg_t *ap, *dst_ap;
139 struct syscall_args *sa;
143 ap = td->td_frame->tf_x;
144 dst_ap = &sa->args[0];
146 sa->code = td->td_frame->tf_x[8];
147 sa->original_code = sa->code;
149 if (__predict_false(sa->code == SYS_syscall || sa->code == SYS___syscall)) {
155 if (__predict_false(sa->code >= p->p_sysent->sv_size))
156 sa->callp = &p->p_sysent->sv_table[0];
158 sa->callp = &p->p_sysent->sv_table[sa->code];
160 KASSERT(sa->callp->sy_narg <= nitems(sa->args),
161 ("Syscall %d takes too many arguments", sa->code));
163 memcpy(dst_ap, ap, (nitems(sa->args) - 1) * sizeof(*dst_ap));
165 td->td_retval[0] = 0;
166 td->td_retval[1] = 0;
171 #include "../../kern/subr_syscall.c"
174 * Test for fault generated by given access instruction in
175 * bus_peek_<foo> or bus_poke_<foo> bus function.
177 extern uint32_t generic_bs_peek_1f, generic_bs_peek_2f;
178 extern uint32_t generic_bs_peek_4f, generic_bs_peek_8f;
179 extern uint32_t generic_bs_poke_1f, generic_bs_poke_2f;
180 extern uint32_t generic_bs_poke_4f, generic_bs_poke_8f;
183 test_bs_fault(void *addr)
185 return (addr == &generic_bs_peek_1f ||
186 addr == &generic_bs_peek_2f ||
187 addr == &generic_bs_peek_4f ||
188 addr == &generic_bs_peek_8f ||
189 addr == &generic_bs_poke_1f ||
190 addr == &generic_bs_poke_2f ||
191 addr == &generic_bs_poke_4f ||
192 addr == &generic_bs_poke_8f);
196 svc_handler(struct thread *td, struct trapframe *frame)
199 if ((frame->tf_esr & ESR_ELx_ISS_MASK) == 0) {
203 call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr,
204 ESR_ELx_EXCEPTION(frame->tf_esr));
210 align_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
211 uint64_t far, int lower)
214 print_registers(frame);
215 print_gp_register("far", far);
216 printf(" esr: 0x%.16lx\n", esr);
217 panic("Misaligned access from kernel space!");
220 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr,
221 ESR_ELx_EXCEPTION(frame->tf_esr));
227 external_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
228 uint64_t far, int lower)
232 * Try to handle synchronous external aborts caused by
233 * bus_space_peek() and/or bus_space_poke() functions.
235 if (!lower && test_bs_fault((void *)frame->tf_elr)) {
236 frame->tf_elr = (uint64_t)generic_bs_fault;
240 print_registers(frame);
241 print_gp_register("far", far);
242 panic("Unhandled EL%d external data abort", lower ? 0: 1);
246 * It is unsafe to access the stack canary value stored in "td" until
247 * kernel map translation faults are handled, see the pmap_klookup() call below.
248 * Thus, stack-smashing detection with per-thread canaries must be disabled in
251 static void NO_PERTHREAD_SSP
252 data_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
253 uint64_t far, int lower)
258 int error, sig, ucode;
264 * According to the ARMv8-A rev. A.g, B2.10.5 "Load-Exclusive
265 * and Store-Exclusive instruction usage restrictions", state
266 * of the exclusive monitors after data abort exception is unknown.
278 map = &td->td_proc->p_vmspace->vm_map;
279 } else if (!ADDR_IS_CANONICAL(far)) {
280 /* We received a TBI/PAC/etc. fault from the kernel */
281 error = KERN_INVALID_ADDRESS;
283 } else if (ADDR_IS_KERNEL(far)) {
285 * Handle a special case: the data abort was caused by accessing
286 * a thread structure while its mapping was being promoted or
287 * demoted, as a consequence of the break-before-make rule. It
288 * is not safe to enable interrupts or dereference "td" before
289 * this case is handled.
291 * In principle, if pmap_klookup() fails, there is no need to
292 * call pmap_fault() below, but avoiding that call is not worth
295 if (ESR_ELx_EXCEPTION(esr) == EXCP_DATA_ABORT) {
296 switch (esr & ISS_DATA_DFSC_MASK) {
297 case ISS_DATA_DFSC_TF_L0:
298 case ISS_DATA_DFSC_TF_L1:
299 case ISS_DATA_DFSC_TF_L2:
300 case ISS_DATA_DFSC_TF_L3:
301 if (pmap_klookup(far, NULL))
310 map = &td->td_proc->p_vmspace->vm_map;
317 * Try to handle translation, access flag, and permission faults.
318 * Translation faults may occur as a result of the required
319 * break-before-make sequence used when promoting or demoting
320 * superpages. Such faults must not occur while holding the pmap lock,
321 * or pmap_fault() will recurse on that lock.
323 if ((lower || map == kernel_map || pcb->pcb_onfault != 0) &&
324 pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
328 if (td->td_md.md_spinlock_count != 0) {
329 print_registers(frame);
330 print_gp_register("far", far);
331 printf(" esr: 0x%.16lx\n", esr);
332 panic("data abort with spinlock held (spinlock count %d != 0)",
333 td->td_md.md_spinlock_count);
336 if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
337 WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
338 print_registers(frame);
339 print_gp_register("far", far);
340 printf(" esr: 0x%.16lx\n", esr);
341 panic("data abort in critical section or under mutex");
344 switch (ESR_ELx_EXCEPTION(esr)) {
345 case EXCP_INSN_ABORT:
346 case EXCP_INSN_ABORT_L:
347 ftype = VM_PROT_EXECUTE;
351 * If the exception was because of a read or cache operation
352 * pass a read fault type into the vm code. Cache operations
353 * need read permission but will set the WnR flag when the
354 * memory is unmapped.
356 if ((esr & ISS_DATA_WnR) == 0 || (esr & ISS_DATA_CM) != 0)
357 ftype = VM_PROT_READ;
359 ftype = VM_PROT_WRITE;
363 /* Fault in the page. */
364 error = vm_fault_trap(map, far, ftype, VM_FAULT_NORMAL, &sig, &ucode);
365 if (error != KERN_SUCCESS) {
367 call_trapsignal(td, sig, ucode, (void *)far,
368 ESR_ELx_EXCEPTION(esr));
371 if (td->td_intr_nesting_level == 0 &&
372 pcb->pcb_onfault != 0) {
373 frame->tf_x[0] = error;
374 frame->tf_elr = pcb->pcb_onfault;
378 printf("Fatal data abort:\n");
379 print_registers(frame);
380 print_gp_register("far", far);
381 printf(" esr: 0x%.16lx\n", esr);
384 if (debugger_on_trap) {
385 kdb_why = KDB_WHY_TRAP;
386 handled = kdb_trap(ESR_ELx_EXCEPTION(esr), 0,
388 kdb_why = KDB_WHY_UNSET;
393 panic("vm_fault failed: 0x%lx error %d",
394 frame->tf_elr, error);
403 print_gp_register(const char *name, uint64_t value)
407 const char *sym_name;
412 printf(" %s: 0x%.16lx", name, value);
414 /* If this looks like a kernel address try to find the symbol */
415 if (value >= VM_MIN_KERNEL_ADDRESS) {
416 sym = db_search_symbol(value, DB_STGY_ANY, &offset);
417 if (sym != C_DB_SYM_NULL) {
418 db_symbol_values(sym, &sym_name, &sym_value);
419 printf(" (%s + 0x%lx)", sym_name, offset);
427 print_registers(struct trapframe *frame)
432 for (reg = 0; reg < nitems(frame->tf_x); reg++) {
433 snprintf(name, sizeof(name), "%sx%d", (reg < 10) ? " " : "",
435 print_gp_register(name, frame->tf_x[reg]);
437 printf(" sp: 0x%.16lx\n", frame->tf_sp);
438 print_gp_register(" lr", frame->tf_lr);
439 print_gp_register("elr", frame->tf_elr);
440 printf("spsr: 0x%.16lx\n", frame->tf_spsr);
445 fpe_trap(struct thread *td, void *addr, uint32_t exception)
450 if ((exception & ISS_FP_TFV) != 0) {
451 if ((exception & ISS_FP_IOF) != 0)
453 else if ((exception & ISS_FP_DZF) != 0)
455 else if ((exception & ISS_FP_OFF) != 0)
457 else if ((exception & ISS_FP_UFF) != 0)
459 else if ((exception & ISS_FP_IXF) != 0)
462 call_trapsignal(td, SIGFPE, code, addr, exception);
467 * See the comment above data_abort().
469 void NO_PERTHREAD_SSP
470 do_el1h_sync(struct thread *td, struct trapframe *frame)
476 kasan_mark(frame, sizeof(*frame), sizeof(*frame), 0);
478 /* Read the esr register to get the exception details */
480 exception = ESR_ELx_EXCEPTION(esr);
483 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
487 CTR4(KTR_TRAP, "%s: exception=%lu, elr=0x%lx, esr=0x%lx",
488 __func__, exception, frame->tf_elr, esr);
491 * Enable debug exceptions if we aren't already handling one. They will
492 * be masked again in the exception handler's epilogue.
494 if (exception != EXCP_BRK && exception != EXCP_WATCHPT_EL1 &&
495 exception != EXCP_SOFTSTP_EL1)
502 if ((td->td_pcb->pcb_fpflags & PCB_FP_KERN) != 0) {
507 print_registers(frame);
508 printf(" esr: 0x%.16lx\n", esr);
509 panic("VFP exception in the kernel");
512 case EXCP_INSN_ABORT:
513 case EXCP_DATA_ABORT:
514 dfsc = esr & ISS_DATA_DFSC_MASK;
515 if (dfsc < nitems(abort_handlers) &&
516 abort_handlers[dfsc] != NULL) {
517 abort_handlers[dfsc](td, frame, esr, far, 0);
519 print_registers(frame);
520 print_gp_register("far", far);
521 printf(" esr: 0x%.16lx\n", esr);
522 panic("Unhandled EL1 %s abort: 0x%x",
523 exception == EXCP_INSN_ABORT ? "instruction" :
529 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
530 dtrace_invop_jump_addr != 0) {
531 dtrace_invop_jump_addr(frame);
536 kdb_trap(exception, 0, frame);
538 panic("No debugger in kernel.");
541 case EXCP_WATCHPT_EL1:
542 case EXCP_SOFTSTP_EL1:
544 kdb_trap(exception, 0, frame);
546 panic("No debugger in kernel.");
550 /* We can see this if the authentication on PAC fails */
551 print_registers(frame);
552 print_gp_register("far", far);
553 panic("FPAC kernel exception");
556 if (undef_insn(1, frame))
558 print_registers(frame);
559 print_gp_register("far", far);
560 panic("Undefined instruction: %08x",
561 *(uint32_t *)frame->tf_elr);
564 print_registers(frame);
565 print_gp_register("far", far);
566 panic("Unknown kernel exception 0x%x esr_el1 0x%lx", exception,
572 do_el0_sync(struct thread *td, struct trapframe *frame)
574 pcpu_bp_harden bp_harden;
579 /* Check we have a sane environment when entering from userland */
580 KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
581 ("Invalid pcpu address from userland: %p (tpidr 0x%lx)",
582 get_pcpu(), READ_SPECIALREG(tpidr_el1)));
584 kasan_mark(frame, sizeof(*frame), sizeof(*frame), 0);
587 exception = ESR_ELx_EXCEPTION(esr);
588 if (exception == EXCP_INSN_ABORT_L && far > VM_MAXUSER_ADDRESS) {
590 * Userspace may be trying to train the branch predictor to
591 * attack the kernel. If we are on a CPU affected by this
592 * call the handler to clear the branch predictor state.
594 bp_harden = PCPU_GET(bp_harden);
595 if (bp_harden != NULL)
600 CTR4(KTR_TRAP, "%s: exception=%lu, elr=0x%lx, esr=0x%lx",
601 __func__, exception, frame->tf_elr, esr);
608 panic("VFP exception in userland");
613 fpe_trap(td, (void *)frame->tf_elr, esr);
616 panic("VFP exception in userland");
620 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_elr,
626 svc_handler(td, frame);
628 case EXCP_INSN_ABORT_L:
629 case EXCP_DATA_ABORT_L:
630 case EXCP_DATA_ABORT:
631 dfsc = esr & ISS_DATA_DFSC_MASK;
632 if (dfsc < nitems(abort_handlers) &&
633 abort_handlers[dfsc] != NULL)
634 abort_handlers[dfsc](td, frame, esr, far, 1);
636 print_registers(frame);
637 print_gp_register("far", far);
638 printf(" esr: 0x%.16lx\n", esr);
639 panic("Unhandled EL0 %s abort: 0x%x",
640 exception == EXCP_INSN_ABORT_L ? "instruction" :
645 if (!undef_insn(0, frame))
646 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far,
651 call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr,
656 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp,
661 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr,
667 #ifdef COMPAT_FREEBSD32
669 #endif /* COMPAT_FREEBSD32 */
670 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr,
674 case EXCP_WATCHPT_EL0:
675 call_trapsignal(td, SIGTRAP, TRAP_TRACE, (void *)far,
681 * The CPU can raise EXCP_MSR when userspace executes an mrs
682 * instruction to access a special register userspace doesn't
685 if (!undef_insn(0, frame))
686 call_trapsignal(td, SIGILL, ILL_PRVOPC,
687 (void *)frame->tf_elr, exception);
690 case EXCP_SOFTSTP_EL0:
691 PROC_LOCK(td->td_proc);
692 if ((td->td_dbgflags & TDB_STEP) != 0) {
693 td->td_frame->tf_spsr &= ~PSR_SS;
694 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
695 WRITE_SPECIALREG(mdscr_el1,
696 READ_SPECIALREG(mdscr_el1) & ~MDSCR_SS);
698 PROC_UNLOCK(td->td_proc);
699 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
700 (void *)frame->tf_elr, exception);
704 call_trapsignal(td, SIGBUS, BUS_OBJERR, (void *)frame->tf_elr,
710 KASSERT((td->td_pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
711 ("Kernel VFP flags set while entering userspace"));
713 td->td_pcb->pcb_fpusaved == &td->td_pcb->pcb_fpustate,
714 ("Kernel VFP state in use when entering userspace"));
718 * TODO: We will need to handle these later when we support ARMv8.2 RAS.
721 do_serror(struct trapframe *frame)
725 kasan_mark(frame, sizeof(*frame), sizeof(*frame), 0);
729 print_registers(frame);
730 print_gp_register("far", far);
731 printf(" esr: 0x%.16lx\n", esr);
732 panic("Unhandled System Error");
736 unhandled_exception(struct trapframe *frame)
740 kasan_mark(frame, sizeof(*frame), sizeof(*frame), 0);
744 print_registers(frame);
745 print_gp_register("far", far);
746 printf(" esr: 0x%.16lx\n", esr);
747 panic("Unhandled exception");