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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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>
71 #include <ddb/db_output.h>
74 extern register_t fsu_intr_fault;
76 /* Called from exception.S */
77 void do_el1h_sync(struct thread *, struct trapframe *);
78 void do_el0_sync(struct thread *, struct trapframe *);
79 void do_el0_error(struct trapframe *);
80 void do_serror(struct trapframe *);
81 void unhandled_exception(struct trapframe *);
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,
110 call_trapsignal(struct thread *td, int sig, int code, void *addr, int trapno)
114 ksiginfo_init_trap(&ksi);
118 ksi.ksi_trapno = trapno;
119 trapsignal(td, &ksi);
123 cpu_fetch_syscall_args(struct thread *td)
127 struct syscall_args *sa;
132 ap = td->td_frame->tf_x;
135 sa->code = td->td_frame->tf_x[8];
137 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
142 if (sa->code >= p->p_sysent->sv_size)
143 sa->callp = &p->p_sysent->sv_table[0];
145 sa->callp = &p->p_sysent->sv_table[sa->code];
147 memcpy(sa->args, ap, nap * sizeof(register_t));
148 if (sa->callp->sy_narg > nap)
149 panic("ARM64TODO: Could we have more than %d args?", MAXARGS);
151 td->td_retval[0] = 0;
152 td->td_retval[1] = 0;
157 #include "../../kern/subr_syscall.c"
160 * Test for fault generated by given access instruction in
161 * bus_peek_<foo> or bus_poke_<foo> bus function.
163 extern uint32_t generic_bs_peek_1f, generic_bs_peek_2f;
164 extern uint32_t generic_bs_peek_4f, generic_bs_peek_8f;
165 extern uint32_t generic_bs_poke_1f, generic_bs_poke_2f;
166 extern uint32_t generic_bs_poke_4f, generic_bs_poke_8f;
169 test_bs_fault(void *addr)
171 return (addr == &generic_bs_peek_1f ||
172 addr == &generic_bs_peek_2f ||
173 addr == &generic_bs_peek_4f ||
174 addr == &generic_bs_peek_8f ||
175 addr == &generic_bs_poke_1f ||
176 addr == &generic_bs_poke_2f ||
177 addr == &generic_bs_poke_4f ||
178 addr == &generic_bs_poke_8f);
182 svc_handler(struct thread *td, struct trapframe *frame)
185 if ((frame->tf_esr & ESR_ELx_ISS_MASK) == 0) {
189 call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr,
190 ESR_ELx_EXCEPTION(frame->tf_esr));
196 align_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
197 uint64_t far, int lower)
200 print_registers(frame);
201 printf(" far: %16lx\n", far);
202 printf(" esr: %.8lx\n", esr);
203 panic("Misaligned access from kernel space!");
206 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr,
207 ESR_ELx_EXCEPTION(frame->tf_esr));
213 external_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
214 uint64_t far, int lower)
218 * Try to handle synchronous external aborts caused by
219 * bus_space_peek() and/or bus_space_poke() functions.
221 if (!lower && test_bs_fault((void *)frame->tf_elr)) {
222 frame->tf_elr = (uint64_t)generic_bs_fault;
226 print_registers(frame);
227 printf(" far: %16lx\n", far);
228 panic("Unhandled EL%d external data abort", lower ? 0: 1);
232 data_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
233 uint64_t far, int lower)
239 int error, sig, ucode;
245 * According to the ARMv8-A rev. A.g, B2.10.5 "Load-Exclusive
246 * and Store-Exclusive instruction usage restrictions", state
247 * of the exclusive monitors after data abort exception is unknown.
261 map = &p->p_vmspace->vm_map;
265 /* The top bit tells us which range to use */
266 if (far >= VM_MAXUSER_ADDRESS) {
269 map = &p->p_vmspace->vm_map;
276 * Try to handle translation, access flag, and permission faults.
277 * Translation faults may occur as a result of the required
278 * break-before-make sequence used when promoting or demoting
279 * superpages. Such faults must not occur while holding the pmap lock,
280 * or pmap_fault() will recurse on that lock.
282 if ((lower || map == kernel_map || pcb->pcb_onfault != 0) &&
283 pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
286 KASSERT(td->td_md.md_spinlock_count == 0,
287 ("data abort with spinlock held"));
288 if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
289 WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
290 print_registers(frame);
291 printf(" far: %16lx\n", far);
292 printf(" esr: %.8lx\n", esr);
293 panic("data abort in critical section or under mutex");
296 switch (ESR_ELx_EXCEPTION(esr)) {
297 case EXCP_INSN_ABORT:
298 case EXCP_INSN_ABORT_L:
299 ftype = VM_PROT_EXECUTE;
302 ftype = (esr & ISS_DATA_WnR) == 0 ? VM_PROT_READ :
303 VM_PROT_READ | VM_PROT_WRITE;
307 /* Fault in the page. */
308 error = vm_fault_trap(map, far, ftype, VM_FAULT_NORMAL, &sig, &ucode);
309 if (error != KERN_SUCCESS) {
311 call_trapsignal(td, sig, ucode, (void *)far,
312 ESR_ELx_EXCEPTION(esr));
314 if (td->td_intr_nesting_level == 0 &&
315 pcb->pcb_onfault != 0) {
316 frame->tf_x[0] = error;
317 frame->tf_elr = pcb->pcb_onfault;
321 printf("Fatal data abort:\n");
322 print_registers(frame);
323 printf(" far: %16lx\n", far);
324 printf(" esr: %.8lx\n", esr);
327 if (debugger_on_trap) {
328 kdb_why = KDB_WHY_TRAP;
329 handled = kdb_trap(ESR_ELx_EXCEPTION(esr), 0,
331 kdb_why = KDB_WHY_UNSET;
336 panic("vm_fault failed: %lx", frame->tf_elr);
345 print_registers(struct trapframe *frame)
349 for (reg = 0; reg < nitems(frame->tf_x); reg++) {
350 printf(" %sx%d: %16lx\n", (reg < 10) ? " " : "", reg,
353 printf(" sp: %16lx\n", frame->tf_sp);
354 printf(" lr: %16lx\n", frame->tf_lr);
355 printf(" elr: %16lx\n", frame->tf_elr);
356 printf("spsr: %8x\n", frame->tf_spsr);
360 do_el1h_sync(struct thread *td, struct trapframe *frame)
362 struct trapframe *oframe;
367 /* Read the esr register to get the exception details */
369 exception = ESR_ELx_EXCEPTION(esr);
372 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
377 "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td,
378 esr, frame->tf_elr, frame);
380 oframe = td->td_frame;
384 case EXCP_WATCHPT_EL1:
385 case EXCP_SOFTSTP_EL1:
388 td->td_frame = frame;
396 if ((td->td_pcb->pcb_fpflags & PCB_FP_KERN) != 0) {
401 print_registers(frame);
402 printf(" esr: %.8lx\n", esr);
403 panic("VFP exception in the kernel");
406 case EXCP_INSN_ABORT:
407 case EXCP_DATA_ABORT:
408 far = READ_SPECIALREG(far_el1);
409 dfsc = esr & ISS_DATA_DFSC_MASK;
410 if (dfsc < nitems(abort_handlers) &&
411 abort_handlers[dfsc] != NULL) {
412 abort_handlers[dfsc](td, frame, esr, far, 0);
414 print_registers(frame);
415 printf(" far: %16lx\n", far);
416 printf(" esr: %.8lx\n", esr);
417 panic("Unhandled EL1 %s abort: %x",
418 exception == EXCP_INSN_ABORT ? "instruction" :
424 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
425 dtrace_invop_jump_addr != 0) {
426 dtrace_invop_jump_addr(frame);
431 kdb_trap(exception, 0,
432 (td->td_frame != NULL) ? td->td_frame : frame);
434 panic("No debugger in kernel.\n");
438 case EXCP_WATCHPT_EL1:
439 case EXCP_SOFTSTP_EL1:
441 kdb_trap(exception, 0,
442 (td->td_frame != NULL) ? td->td_frame : frame);
444 panic("No debugger in kernel.\n");
448 if (undef_insn(1, frame))
452 print_registers(frame);
453 printf(" far: %16lx\n", READ_SPECIALREG(far_el1));
454 panic("Unknown kernel exception %x esr_el1 %lx\n", exception,
458 td->td_frame = oframe;
462 do_el0_sync(struct thread *td, struct trapframe *frame)
464 pcpu_bp_harden bp_harden;
469 /* Check we have a sane environment when entering from userland */
470 KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
471 ("Invalid pcpu address from userland: %p (tpidr %lx)",
472 get_pcpu(), READ_SPECIALREG(tpidr_el1)));
475 exception = ESR_ELx_EXCEPTION(esr);
477 case EXCP_INSN_ABORT_L:
478 far = READ_SPECIALREG(far_el1);
481 * Userspace may be trying to train the branch predictor to
482 * attack the kernel. If we are on a CPU affected by this
483 * call the handler to clear the branch predictor state.
485 if (far > VM_MAXUSER_ADDRESS) {
486 bp_harden = PCPU_GET(bp_harden);
487 if (bp_harden != NULL)
492 case EXCP_DATA_ABORT_L:
493 case EXCP_DATA_ABORT:
494 far = READ_SPECIALREG(far_el1);
500 "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td, esr,
501 frame->tf_elr, frame);
509 panic("VFP exception in userland");
514 svc_handler(td, frame);
516 case EXCP_INSN_ABORT_L:
517 case EXCP_DATA_ABORT_L:
518 case EXCP_DATA_ABORT:
519 dfsc = esr & ISS_DATA_DFSC_MASK;
520 if (dfsc < nitems(abort_handlers) &&
521 abort_handlers[dfsc] != NULL)
522 abort_handlers[dfsc](td, frame, esr, far, 1);
524 print_registers(frame);
525 printf(" far: %16lx\n", far);
526 printf(" esr: %.8lx\n", esr);
527 panic("Unhandled EL0 %s abort: %x",
528 exception == EXCP_INSN_ABORT_L ? "instruction" :
533 if (!undef_insn(0, frame))
534 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far,
539 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp,
544 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr,
550 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr,
555 call_trapsignal(td, SIGILL, ILL_PRVOPC, (void *)frame->tf_elr,
559 case EXCP_SOFTSTP_EL0:
560 td->td_frame->tf_spsr &= ~PSR_SS;
561 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
562 WRITE_SPECIALREG(mdscr_el1,
563 READ_SPECIALREG(mdscr_el1) & ~DBG_MDSCR_SS);
564 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
565 (void *)frame->tf_elr, exception);
569 call_trapsignal(td, SIGBUS, BUS_OBJERR, (void *)frame->tf_elr,
575 KASSERT((td->td_pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
576 ("Kernel VFP flags set while entering userspace"));
578 td->td_pcb->pcb_fpusaved == &td->td_pcb->pcb_fpustate,
579 ("Kernel VFP state in use when entering userspace"));
583 * TODO: We will need to handle these later when we support ARMv8.2 RAS.
586 do_serror(struct trapframe *frame)
590 far = READ_SPECIALREG(far_el1);
593 print_registers(frame);
594 printf(" far: %16lx\n", far);
595 printf(" esr: %.8lx\n", esr);
596 panic("Unhandled System Error");
600 unhandled_exception(struct trapframe *frame)
604 far = READ_SPECIALREG(far_el1);
607 print_registers(frame);
608 printf(" far: %16lx\n", far);
609 printf(" esr: %.8lx\n", esr);
610 panic("Unhandled exception");