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>
35 #include <sys/kernel.h>
38 #include <sys/mutex.h>
40 #include <sys/ptrace.h>
41 #include <sys/syscall.h>
42 #include <sys/sysent.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>
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>
61 #include <sys/dtrace_bsd.h>
65 #include <machine/vfp.h>
69 #include <machine/db_machdep.h>
74 #include <ddb/db_sym.h>
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 *);
84 static void print_gp_register(const char *name, uint64_t value);
85 static void print_registers(struct trapframe *frame);
87 int (*dtrace_invop_jump_addr)(struct trapframe *);
89 typedef void (abort_handler)(struct thread *, struct trapframe *, uint64_t,
92 static abort_handler align_abort;
93 static abort_handler data_abort;
94 static abort_handler external_abort;
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,
121 call_trapsignal(struct thread *td, int sig, int code, void *addr, int trapno)
125 ksiginfo_init_trap(&ksi);
129 ksi.ksi_trapno = trapno;
130 trapsignal(td, &ksi);
134 cpu_fetch_syscall_args(struct thread *td)
137 register_t *ap, *dst_ap;
138 struct syscall_args *sa;
142 ap = td->td_frame->tf_x;
143 dst_ap = &sa->args[0];
145 sa->code = td->td_frame->tf_x[8];
147 if (__predict_false(sa->code == SYS_syscall || sa->code == SYS___syscall)) {
153 if (__predict_false(sa->code >= p->p_sysent->sv_size))
154 sa->callp = &p->p_sysent->sv_table[0];
156 sa->callp = &p->p_sysent->sv_table[sa->code];
158 KASSERT(sa->callp->sy_narg <= nitems(sa->args),
159 ("Syscall %d takes too many arguments", sa->code));
161 memcpy(dst_ap, ap, (MAXARGS - 1) * sizeof(register_t));
163 td->td_retval[0] = 0;
164 td->td_retval[1] = 0;
169 #include "../../kern/subr_syscall.c"
172 * Test for fault generated by given access instruction in
173 * bus_peek_<foo> or bus_poke_<foo> bus function.
175 extern uint32_t generic_bs_peek_1f, generic_bs_peek_2f;
176 extern uint32_t generic_bs_peek_4f, generic_bs_peek_8f;
177 extern uint32_t generic_bs_poke_1f, generic_bs_poke_2f;
178 extern uint32_t generic_bs_poke_4f, generic_bs_poke_8f;
181 test_bs_fault(void *addr)
183 return (addr == &generic_bs_peek_1f ||
184 addr == &generic_bs_peek_2f ||
185 addr == &generic_bs_peek_4f ||
186 addr == &generic_bs_peek_8f ||
187 addr == &generic_bs_poke_1f ||
188 addr == &generic_bs_poke_2f ||
189 addr == &generic_bs_poke_4f ||
190 addr == &generic_bs_poke_8f);
194 svc_handler(struct thread *td, struct trapframe *frame)
197 if ((frame->tf_esr & ESR_ELx_ISS_MASK) == 0) {
201 call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr,
202 ESR_ELx_EXCEPTION(frame->tf_esr));
208 align_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
209 uint64_t far, int lower)
212 print_registers(frame);
213 print_gp_register("far", far);
214 printf(" esr: %.8lx\n", esr);
215 panic("Misaligned access from kernel space!");
218 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr,
219 ESR_ELx_EXCEPTION(frame->tf_esr));
225 external_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
226 uint64_t far, int lower)
230 * Try to handle synchronous external aborts caused by
231 * bus_space_peek() and/or bus_space_poke() functions.
233 if (!lower && test_bs_fault((void *)frame->tf_elr)) {
234 frame->tf_elr = (uint64_t)generic_bs_fault;
238 print_registers(frame);
239 print_gp_register("far", far);
240 panic("Unhandled EL%d external data abort", lower ? 0: 1);
244 data_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
245 uint64_t far, int lower)
251 int error, sig, ucode;
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.
273 map = &p->p_vmspace->vm_map;
277 /* We received a TBI/PAC/etc. fault from the kernel */
278 if (!ADDR_IS_CANONICAL(far)) {
279 error = KERN_INVALID_ADDRESS;
283 /* The top bit tells us which range to use */
284 if (ADDR_IS_KERNEL(far)) {
287 map = &p->p_vmspace->vm_map;
294 * Try to handle translation, access flag, and permission faults.
295 * Translation faults may occur as a result of the required
296 * break-before-make sequence used when promoting or demoting
297 * superpages. Such faults must not occur while holding the pmap lock,
298 * or pmap_fault() will recurse on that lock.
300 if ((lower || map == kernel_map || pcb->pcb_onfault != 0) &&
301 pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
304 KASSERT(td->td_md.md_spinlock_count == 0,
305 ("data abort with spinlock held"));
306 if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
307 WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
308 print_registers(frame);
309 print_gp_register("far", far);
310 printf(" esr: %.8lx\n", esr);
311 panic("data abort in critical section or under mutex");
314 switch (ESR_ELx_EXCEPTION(esr)) {
315 case EXCP_INSN_ABORT:
316 case EXCP_INSN_ABORT_L:
317 ftype = VM_PROT_EXECUTE;
321 * If the exception was because of a read or cache operation
322 * pass a read fault type into the vm code. Cache operations
323 * need read permission but will set the WnR flag when the
324 * memory is unmapped.
326 if ((esr & ISS_DATA_WnR) == 0 || (esr & ISS_DATA_CM) != 0)
327 ftype = VM_PROT_READ;
329 ftype = VM_PROT_WRITE;
333 /* Fault in the page. */
334 error = vm_fault_trap(map, far, ftype, VM_FAULT_NORMAL, &sig, &ucode);
335 if (error != KERN_SUCCESS) {
338 call_trapsignal(td, sig, ucode, (void *)far,
339 ESR_ELx_EXCEPTION(esr));
341 if (td->td_intr_nesting_level == 0 &&
342 pcb->pcb_onfault != 0) {
343 frame->tf_x[0] = error;
344 frame->tf_elr = pcb->pcb_onfault;
348 printf("Fatal data abort:\n");
349 print_registers(frame);
350 print_gp_register("far", far);
351 printf(" esr: %.8lx\n", esr);
354 if (debugger_on_trap) {
355 kdb_why = KDB_WHY_TRAP;
356 handled = kdb_trap(ESR_ELx_EXCEPTION(esr), 0,
358 kdb_why = KDB_WHY_UNSET;
363 panic("vm_fault failed: %lx", frame->tf_elr);
372 print_gp_register(const char *name, uint64_t value)
376 const char *sym_name;
381 printf(" %s: %16lx", name, value);
383 /* If this looks like a kernel address try to find the symbol */
384 if (value >= VM_MIN_KERNEL_ADDRESS) {
385 sym = db_search_symbol(value, DB_STGY_ANY, &offset);
386 if (sym != C_DB_SYM_NULL) {
387 db_symbol_values(sym, &sym_name, &sym_value);
388 printf(" (%s + %lx)", sym_name, offset);
396 print_registers(struct trapframe *frame)
401 for (reg = 0; reg < nitems(frame->tf_x); reg++) {
402 snprintf(name, sizeof(name), "%sx%d", (reg < 10) ? " " : "",
404 print_gp_register(name, frame->tf_x[reg]);
406 printf(" sp: %16lx\n", frame->tf_sp);
407 print_gp_register(" lr", frame->tf_lr);
408 print_gp_register("elr", frame->tf_elr);
409 printf("spsr: %8x\n", frame->tf_spsr);
414 fpe_trap(struct thread *td, void *addr, uint32_t exception)
419 if ((exception & ISS_FP_TFV) != 0) {
420 if ((exception & ISS_FP_IOF) != 0)
422 else if ((exception & ISS_FP_DZF) != 0)
424 else if ((exception & ISS_FP_OFF) != 0)
426 else if ((exception & ISS_FP_UFF) != 0)
428 else if ((exception & ISS_FP_IXF) != 0)
431 call_trapsignal(td, SIGFPE, code, addr, exception);
436 do_el1h_sync(struct thread *td, struct trapframe *frame)
442 /* Read the esr register to get the exception details */
444 exception = ESR_ELx_EXCEPTION(esr);
447 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
452 "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td,
453 esr, frame->tf_elr, frame);
456 * Enable debug exceptions if we aren't already handling one. They will
457 * be masked again in the exception handler's epilogue.
459 if (exception != EXCP_BRK && exception != EXCP_WATCHPT_EL1 &&
460 exception != EXCP_SOFTSTP_EL1)
467 if ((td->td_pcb->pcb_fpflags & PCB_FP_KERN) != 0) {
472 print_registers(frame);
473 printf(" esr: %.8lx\n", esr);
474 panic("VFP exception in the kernel");
477 case EXCP_INSN_ABORT:
478 case EXCP_DATA_ABORT:
479 far = READ_SPECIALREG(far_el1);
480 dfsc = esr & ISS_DATA_DFSC_MASK;
481 if (dfsc < nitems(abort_handlers) &&
482 abort_handlers[dfsc] != NULL) {
483 abort_handlers[dfsc](td, frame, esr, far, 0);
485 print_registers(frame);
486 print_gp_register("far", far);
487 printf(" esr: %.8lx\n", esr);
488 panic("Unhandled EL1 %s abort: %x",
489 exception == EXCP_INSN_ABORT ? "instruction" :
495 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
496 dtrace_invop_jump_addr != 0) {
497 dtrace_invop_jump_addr(frame);
502 kdb_trap(exception, 0, frame);
504 panic("No debugger in kernel.");
507 case EXCP_WATCHPT_EL1:
508 case EXCP_SOFTSTP_EL1:
510 kdb_trap(exception, 0, frame);
512 panic("No debugger in kernel.");
516 if (undef_insn(1, frame))
518 printf("Undefined instruction: %08x\n",
519 *(uint32_t *)frame->tf_elr);
522 print_registers(frame);
523 print_gp_register("far", READ_SPECIALREG(far_el1));
524 panic("Unknown kernel exception %x esr_el1 %lx", exception,
530 do_el0_sync(struct thread *td, struct trapframe *frame)
532 pcpu_bp_harden bp_harden;
537 /* Check we have a sane environment when entering from userland */
538 KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
539 ("Invalid pcpu address from userland: %p (tpidr %lx)",
540 get_pcpu(), READ_SPECIALREG(tpidr_el1)));
543 exception = ESR_ELx_EXCEPTION(esr);
545 case EXCP_INSN_ABORT_L:
546 far = READ_SPECIALREG(far_el1);
549 * Userspace may be trying to train the branch predictor to
550 * attack the kernel. If we are on a CPU affected by this
551 * call the handler to clear the branch predictor state.
553 if (far > VM_MAXUSER_ADDRESS) {
554 bp_harden = PCPU_GET(bp_harden);
555 if (bp_harden != NULL)
560 case EXCP_DATA_ABORT_L:
561 case EXCP_DATA_ABORT:
562 case EXCP_WATCHPT_EL0:
563 far = READ_SPECIALREG(far_el1);
569 "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td, esr,
570 frame->tf_elr, frame);
577 panic("VFP exception in userland");
582 fpe_trap(td, (void *)frame->tf_elr, esr);
585 panic("VFP exception in userland");
590 svc_handler(td, frame);
592 case EXCP_INSN_ABORT_L:
593 case EXCP_DATA_ABORT_L:
594 case EXCP_DATA_ABORT:
595 dfsc = esr & ISS_DATA_DFSC_MASK;
596 if (dfsc < nitems(abort_handlers) &&
597 abort_handlers[dfsc] != NULL)
598 abort_handlers[dfsc](td, frame, esr, far, 1);
600 print_registers(frame);
601 print_gp_register("far", far);
602 printf(" esr: %.8lx\n", esr);
603 panic("Unhandled EL0 %s abort: %x",
604 exception == EXCP_INSN_ABORT_L ? "instruction" :
609 if (!undef_insn(0, frame))
610 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far,
615 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp,
620 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr,
626 #ifdef COMPAT_FREEBSD32
628 #endif /* COMPAT_FREEBSD32 */
629 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr,
633 case EXCP_WATCHPT_EL0:
634 call_trapsignal(td, SIGTRAP, TRAP_TRACE, (void *)far,
640 * The CPU can raise EXCP_MSR when userspace executes an mrs
641 * instruction to access a special register userspace doesn't
644 if (!undef_insn(0, frame))
645 call_trapsignal(td, SIGILL, ILL_PRVOPC,
646 (void *)frame->tf_elr, exception);
649 case EXCP_SOFTSTP_EL0:
650 PROC_LOCK(td->td_proc);
651 if ((td->td_dbgflags & TDB_STEP) != 0) {
652 td->td_frame->tf_spsr &= ~PSR_SS;
653 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
654 WRITE_SPECIALREG(mdscr_el1,
655 READ_SPECIALREG(mdscr_el1) & ~MDSCR_SS);
657 PROC_UNLOCK(td->td_proc);
658 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
659 (void *)frame->tf_elr, exception);
663 call_trapsignal(td, SIGBUS, BUS_OBJERR, (void *)frame->tf_elr,
669 KASSERT((td->td_pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
670 ("Kernel VFP flags set while entering userspace"));
672 td->td_pcb->pcb_fpusaved == &td->td_pcb->pcb_fpustate,
673 ("Kernel VFP state in use when entering userspace"));
677 * TODO: We will need to handle these later when we support ARMv8.2 RAS.
680 do_serror(struct trapframe *frame)
684 far = READ_SPECIALREG(far_el1);
687 print_registers(frame);
688 print_gp_register("far", far);
689 printf(" esr: %.8lx\n", esr);
690 panic("Unhandled System Error");
694 unhandled_exception(struct trapframe *frame)
698 far = READ_SPECIALREG(far_el1);
701 print_registers(frame);
702 print_gp_register("far", far);
703 printf(" esr: %.8lx\n", esr);
704 panic("Unhandled exception");