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 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
36 #include <sys/mutex.h>
38 #include <sys/ptrace.h>
39 #include <sys/syscall.h>
40 #include <sys/sysent.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_map.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_extern.h>
52 #include <machine/frame.h>
53 #include <machine/md_var.h>
54 #include <machine/pcb.h>
55 #include <machine/pcpu.h>
56 #include <machine/undefined.h>
59 #include <sys/dtrace_bsd.h>
63 #include <machine/vfp.h>
67 #include <machine/db_machdep.h>
72 #include <ddb/db_sym.h>
75 /* Called from exception.S */
76 void do_el1h_sync(struct thread *, struct trapframe *);
77 void do_el0_sync(struct thread *, struct trapframe *, uint64_t far);
78 void do_el0_error(struct trapframe *);
79 void do_serror(struct trapframe *);
80 void unhandled_exception(struct trapframe *);
82 static void print_gp_register(const char *name, uint64_t value);
83 static void print_registers(struct trapframe *frame);
85 int (*dtrace_invop_jump_addr)(struct trapframe *);
87 typedef void (abort_handler)(struct thread *, struct trapframe *, uint64_t,
90 static abort_handler align_abort;
91 static abort_handler data_abort;
92 static abort_handler external_abort;
94 static abort_handler *abort_handlers[] = {
95 [ISS_DATA_DFSC_TF_L0] = data_abort,
96 [ISS_DATA_DFSC_TF_L1] = data_abort,
97 [ISS_DATA_DFSC_TF_L2] = data_abort,
98 [ISS_DATA_DFSC_TF_L3] = data_abort,
99 [ISS_DATA_DFSC_AFF_L1] = data_abort,
100 [ISS_DATA_DFSC_AFF_L2] = data_abort,
101 [ISS_DATA_DFSC_AFF_L3] = data_abort,
102 [ISS_DATA_DFSC_PF_L1] = data_abort,
103 [ISS_DATA_DFSC_PF_L2] = data_abort,
104 [ISS_DATA_DFSC_PF_L3] = data_abort,
105 [ISS_DATA_DFSC_ALIGN] = align_abort,
106 [ISS_DATA_DFSC_EXT] = external_abort,
107 [ISS_DATA_DFSC_EXT_L0] = external_abort,
108 [ISS_DATA_DFSC_EXT_L1] = external_abort,
109 [ISS_DATA_DFSC_EXT_L2] = external_abort,
110 [ISS_DATA_DFSC_EXT_L3] = external_abort,
111 [ISS_DATA_DFSC_ECC] = external_abort,
112 [ISS_DATA_DFSC_ECC_L0] = external_abort,
113 [ISS_DATA_DFSC_ECC_L1] = external_abort,
114 [ISS_DATA_DFSC_ECC_L2] = external_abort,
115 [ISS_DATA_DFSC_ECC_L3] = external_abort,
119 call_trapsignal(struct thread *td, int sig, int code, void *addr, int trapno)
123 ksiginfo_init_trap(&ksi);
127 ksi.ksi_trapno = trapno;
128 trapsignal(td, &ksi);
132 cpu_fetch_syscall_args(struct thread *td)
135 register_t *ap, *dst_ap;
136 struct syscall_args *sa;
140 ap = td->td_frame->tf_x;
141 dst_ap = &sa->args[0];
143 sa->code = td->td_frame->tf_x[8];
145 if (__predict_false(sa->code == SYS_syscall || sa->code == SYS___syscall)) {
151 if (__predict_false(sa->code >= p->p_sysent->sv_size))
152 sa->callp = &nosys_sysent;
154 sa->callp = &p->p_sysent->sv_table[sa->code];
156 KASSERT(sa->callp->sy_narg <= nitems(sa->args),
157 ("Syscall %d takes too many arguments", sa->code));
159 memcpy(dst_ap, ap, (MAXARGS - 1) * sizeof(register_t));
161 td->td_retval[0] = 0;
162 td->td_retval[1] = 0;
167 #include "../../kern/subr_syscall.c"
170 * Test for fault generated by given access instruction in
171 * bus_peek_<foo> or bus_poke_<foo> bus function.
173 extern uint32_t generic_bs_peek_1f, generic_bs_peek_2f;
174 extern uint32_t generic_bs_peek_4f, generic_bs_peek_8f;
175 extern uint32_t generic_bs_poke_1f, generic_bs_poke_2f;
176 extern uint32_t generic_bs_poke_4f, generic_bs_poke_8f;
179 test_bs_fault(void *addr)
181 return (addr == &generic_bs_peek_1f ||
182 addr == &generic_bs_peek_2f ||
183 addr == &generic_bs_peek_4f ||
184 addr == &generic_bs_peek_8f ||
185 addr == &generic_bs_poke_1f ||
186 addr == &generic_bs_poke_2f ||
187 addr == &generic_bs_poke_4f ||
188 addr == &generic_bs_poke_8f);
192 svc_handler(struct thread *td, struct trapframe *frame)
195 if ((frame->tf_esr & ESR_ELx_ISS_MASK) == 0) {
199 call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr,
200 ESR_ELx_EXCEPTION(frame->tf_esr));
206 align_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
207 uint64_t far, int lower)
210 print_registers(frame);
211 print_gp_register("far", far);
212 printf(" esr: %.8lx\n", esr);
213 panic("Misaligned access from kernel space!");
216 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr,
217 ESR_ELx_EXCEPTION(frame->tf_esr));
223 external_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
224 uint64_t far, int lower)
228 * Try to handle synchronous external aborts caused by
229 * bus_space_peek() and/or bus_space_poke() functions.
231 if (!lower && test_bs_fault((void *)frame->tf_elr)) {
232 frame->tf_elr = (uint64_t)generic_bs_fault;
236 print_registers(frame);
237 print_gp_register("far", far);
238 panic("Unhandled EL%d external data abort", lower ? 0: 1);
242 data_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
243 uint64_t far, int lower)
249 int error, sig, ucode;
255 * According to the ARMv8-A rev. A.g, B2.10.5 "Load-Exclusive
256 * and Store-Exclusive instruction usage restrictions", state
257 * of the exclusive monitors after data abort exception is unknown.
271 map = &p->p_vmspace->vm_map;
275 /* We received a TBI/PAC/etc. fault from the kernel */
276 if (!ADDR_IS_CANONICAL(far)) {
277 error = KERN_INVALID_ADDRESS;
281 /* The top bit tells us which range to use */
282 if (ADDR_IS_KERNEL(far)) {
285 map = &p->p_vmspace->vm_map;
292 * Try to handle translation, access flag, and permission faults.
293 * Translation faults may occur as a result of the required
294 * break-before-make sequence used when promoting or demoting
295 * superpages. Such faults must not occur while holding the pmap lock,
296 * or pmap_fault() will recurse on that lock.
298 if ((lower || map == kernel_map || pcb->pcb_onfault != 0) &&
299 pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
303 if (td->td_md.md_spinlock_count != 0) {
304 print_registers(frame);
305 print_gp_register("far", far);
306 printf(" esr: %.16lx\n", esr);
307 panic("data abort with spinlock held (spinlock count %d != 0)",
308 td->td_md.md_spinlock_count);
311 if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
312 WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
313 print_registers(frame);
314 print_gp_register("far", far);
315 printf(" esr: %.8lx\n", esr);
316 panic("data abort in critical section or under mutex");
319 switch (ESR_ELx_EXCEPTION(esr)) {
320 case EXCP_INSN_ABORT:
321 case EXCP_INSN_ABORT_L:
322 ftype = VM_PROT_EXECUTE;
326 * If the exception was because of a read or cache operation
327 * pass a read fault type into the vm code. Cache operations
328 * need read permission but will set the WnR flag when the
329 * memory is unmapped.
331 if ((esr & ISS_DATA_WnR) == 0 || (esr & ISS_DATA_CM) != 0)
332 ftype = VM_PROT_READ;
334 ftype = VM_PROT_WRITE;
338 /* Fault in the page. */
339 error = vm_fault_trap(map, far, ftype, VM_FAULT_NORMAL, &sig, &ucode);
340 if (error != KERN_SUCCESS) {
343 call_trapsignal(td, sig, ucode, (void *)far,
344 ESR_ELx_EXCEPTION(esr));
346 if (td->td_intr_nesting_level == 0 &&
347 pcb->pcb_onfault != 0) {
348 frame->tf_x[0] = error;
349 frame->tf_elr = pcb->pcb_onfault;
353 printf("Fatal data abort:\n");
354 print_registers(frame);
355 print_gp_register("far", far);
356 printf(" esr: %.8lx\n", esr);
359 if (debugger_on_trap) {
360 kdb_why = KDB_WHY_TRAP;
361 handled = kdb_trap(ESR_ELx_EXCEPTION(esr), 0,
363 kdb_why = KDB_WHY_UNSET;
368 panic("vm_fault failed: %lx", frame->tf_elr);
377 print_gp_register(const char *name, uint64_t value)
381 const char *sym_name;
386 printf(" %s: %16lx", name, value);
388 /* If this looks like a kernel address try to find the symbol */
389 if (value >= VM_MIN_KERNEL_ADDRESS) {
390 sym = db_search_symbol(value, DB_STGY_ANY, &offset);
391 if (sym != C_DB_SYM_NULL) {
392 db_symbol_values(sym, &sym_name, &sym_value);
393 printf(" (%s + %lx)", sym_name, offset);
401 print_registers(struct trapframe *frame)
406 for (reg = 0; reg < nitems(frame->tf_x); reg++) {
407 snprintf(name, sizeof(name), "%sx%d", (reg < 10) ? " " : "",
409 print_gp_register(name, frame->tf_x[reg]);
411 printf(" sp: %16lx\n", frame->tf_sp);
412 print_gp_register(" lr", frame->tf_lr);
413 print_gp_register("elr", frame->tf_elr);
414 printf("spsr: %8x\n", frame->tf_spsr);
419 fpe_trap(struct thread *td, void *addr, uint32_t exception)
424 if ((exception & ISS_FP_TFV) != 0) {
425 if ((exception & ISS_FP_IOF) != 0)
427 else if ((exception & ISS_FP_DZF) != 0)
429 else if ((exception & ISS_FP_OFF) != 0)
431 else if ((exception & ISS_FP_UFF) != 0)
433 else if ((exception & ISS_FP_IXF) != 0)
436 call_trapsignal(td, SIGFPE, code, addr, exception);
441 do_el1h_sync(struct thread *td, struct trapframe *frame)
447 /* Read the esr register to get the exception details */
449 exception = ESR_ELx_EXCEPTION(esr);
452 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
457 "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td,
458 esr, frame->tf_elr, frame);
461 * Enable debug exceptions if we aren't already handling one. They will
462 * be masked again in the exception handler's epilogue.
464 if (exception != EXCP_BRK && exception != EXCP_WATCHPT_EL1 &&
465 exception != EXCP_SOFTSTP_EL1)
472 if ((td->td_pcb->pcb_fpflags & PCB_FP_KERN) != 0) {
477 print_registers(frame);
478 printf(" esr: %.8lx\n", esr);
479 panic("VFP exception in the kernel");
482 case EXCP_INSN_ABORT:
483 case EXCP_DATA_ABORT:
484 far = READ_SPECIALREG(far_el1);
485 dfsc = esr & ISS_DATA_DFSC_MASK;
486 if (dfsc < nitems(abort_handlers) &&
487 abort_handlers[dfsc] != NULL) {
488 abort_handlers[dfsc](td, frame, esr, far, 0);
490 print_registers(frame);
491 print_gp_register("far", far);
492 printf(" esr: %.8lx\n", esr);
493 panic("Unhandled EL1 %s abort: %x",
494 exception == EXCP_INSN_ABORT ? "instruction" :
500 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
501 dtrace_invop_jump_addr != 0) {
502 dtrace_invop_jump_addr(frame);
507 kdb_trap(exception, 0, frame);
509 panic("No debugger in kernel.");
512 case EXCP_WATCHPT_EL1:
513 case EXCP_SOFTSTP_EL1:
515 kdb_trap(exception, 0, frame);
517 panic("No debugger in kernel.");
521 if (undef_insn(1, frame))
523 far = READ_SPECIALREG(far_el1);
524 print_registers(frame);
525 print_gp_register("far", far);
526 panic("Undefined instruction: %08x",
527 *(uint32_t *)frame->tf_elr);
530 print_registers(frame);
531 print_gp_register("far", READ_SPECIALREG(far_el1));
532 panic("Unknown kernel exception %x esr_el1 %lx", exception,
538 do_el0_sync(struct thread *td, struct trapframe *frame, uint64_t far)
540 pcpu_bp_harden bp_harden;
545 /* Check we have a sane environment when entering from userland */
546 KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
547 ("Invalid pcpu address from userland: %p (tpidr %lx)",
548 get_pcpu(), READ_SPECIALREG(tpidr_el1)));
551 exception = ESR_ELx_EXCEPTION(esr);
552 if (exception == EXCP_INSN_ABORT_L && far > VM_MAXUSER_ADDRESS) {
554 * Userspace may be trying to train the branch predictor to
555 * attack the kernel. If we are on a CPU affected by this
556 * call the handler to clear the branch predictor state.
558 bp_harden = PCPU_GET(bp_harden);
559 if (bp_harden != NULL)
565 "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td, esr,
566 frame->tf_elr, frame);
573 panic("VFP exception in userland");
578 fpe_trap(td, (void *)frame->tf_elr, esr);
581 panic("VFP exception in userland");
585 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_elr,
591 svc_handler(td, frame);
593 case EXCP_INSN_ABORT_L:
594 case EXCP_DATA_ABORT_L:
595 case EXCP_DATA_ABORT:
596 dfsc = esr & ISS_DATA_DFSC_MASK;
597 if (dfsc < nitems(abort_handlers) &&
598 abort_handlers[dfsc] != NULL)
599 abort_handlers[dfsc](td, frame, esr, far, 1);
601 print_registers(frame);
602 print_gp_register("far", far);
603 printf(" esr: %.8lx\n", esr);
604 panic("Unhandled EL0 %s abort: %x",
605 exception == EXCP_INSN_ABORT_L ? "instruction" :
610 if (!undef_insn(0, frame))
611 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far,
616 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp,
621 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr,
627 #ifdef COMPAT_FREEBSD32
629 #endif /* COMPAT_FREEBSD32 */
630 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr,
634 case EXCP_WATCHPT_EL0:
635 call_trapsignal(td, SIGTRAP, TRAP_TRACE, (void *)far,
641 * The CPU can raise EXCP_MSR when userspace executes an mrs
642 * instruction to access a special register userspace doesn't
645 if (!undef_insn(0, frame))
646 call_trapsignal(td, SIGILL, ILL_PRVOPC,
647 (void *)frame->tf_elr, exception);
650 case EXCP_SOFTSTP_EL0:
651 PROC_LOCK(td->td_proc);
652 if ((td->td_dbgflags & TDB_STEP) != 0) {
653 td->td_frame->tf_spsr &= ~PSR_SS;
654 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
655 WRITE_SPECIALREG(mdscr_el1,
656 READ_SPECIALREG(mdscr_el1) & ~MDSCR_SS);
658 PROC_UNLOCK(td->td_proc);
659 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
660 (void *)frame->tf_elr, exception);
664 call_trapsignal(td, SIGBUS, BUS_OBJERR, (void *)frame->tf_elr,
670 KASSERT((td->td_pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
671 ("Kernel VFP flags set while entering userspace"));
673 td->td_pcb->pcb_fpusaved == &td->td_pcb->pcb_fpustate,
674 ("Kernel VFP state in use when entering userspace"));
678 * TODO: We will need to handle these later when we support ARMv8.2 RAS.
681 do_serror(struct trapframe *frame)
685 far = READ_SPECIALREG(far_el1);
688 print_registers(frame);
689 print_gp_register("far", far);
690 printf(" esr: %.8lx\n", esr);
691 panic("Unhandled System Error");
695 unhandled_exception(struct trapframe *frame)
699 far = READ_SPECIALREG(far_el1);
702 print_registers(frame);
703 print_gp_register("far", far);
704 printf(" esr: %.8lx\n", esr);
705 panic("Unhandled exception");