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>
35 #include <sys/mutex.h>
36 #include <sys/pioctl.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/pcb.h>
54 #include <machine/pcpu.h>
55 #include <machine/undefined.h>
58 #include <sys/dtrace_bsd.h>
62 #include <machine/vfp.h>
66 #include <machine/db_machdep.h>
70 #include <ddb/db_output.h>
73 extern register_t fsu_intr_fault;
75 /* Called from exception.S */
76 void do_el1h_sync(struct thread *, struct trapframe *);
77 void do_el0_sync(struct thread *, struct trapframe *);
78 void do_el0_error(struct trapframe *);
79 void do_serror(struct trapframe *);
80 void unhandled_exception(struct trapframe *);
82 static void print_registers(struct trapframe *frame);
84 int (*dtrace_invop_jump_addr)(struct trapframe *);
87 call_trapsignal(struct thread *td, int sig, int code, void *addr)
91 ksiginfo_init_trap(&ksi);
99 cpu_fetch_syscall_args(struct thread *td)
103 struct syscall_args *sa;
108 ap = td->td_frame->tf_x;
111 sa->code = td->td_frame->tf_x[8];
113 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
118 if (p->p_sysent->sv_mask)
119 sa->code &= p->p_sysent->sv_mask;
120 if (sa->code >= p->p_sysent->sv_size)
121 sa->callp = &p->p_sysent->sv_table[0];
123 sa->callp = &p->p_sysent->sv_table[sa->code];
125 sa->narg = sa->callp->sy_narg;
126 memcpy(sa->args, ap, nap * sizeof(register_t));
128 panic("ARM64TODO: Could we have more than 8 args?");
130 td->td_retval[0] = 0;
131 td->td_retval[1] = 0;
136 #include "../../kern/subr_syscall.c"
139 svc_handler(struct thread *td, struct trapframe *frame)
143 if ((frame->tf_esr & ESR_ELx_ISS_MASK) == 0) {
144 error = syscallenter(td);
145 syscallret(td, error);
147 call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr);
153 data_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
154 uint64_t far, int lower, int exec)
161 int error, sig, ucode;
167 * According to the ARMv8-A rev. A.g, B2.10.5 "Load-Exclusive
168 * and Store-Exclusive instruction usage restrictions", state
169 * of the exclusive monitors after data abort exception is unknown.
183 map = &p->p_vmspace->vm_map;
185 /* The top bit tells us which range to use */
186 if (far >= VM_MAXUSER_ADDRESS) {
189 map = &p->p_vmspace->vm_map;
196 * The call to pmap_fault can be dangerous when coming from the
197 * kernel as it may be not be able to lock the pmap to check if
198 * the address is now valid. Because of this we filter the cases
199 * when we are not going to see superpage activity.
203 * We may fault in a DMAP region due to a superpage being
204 * unmapped when the access took place.
206 if (map == kernel_map && !VIRT_IN_DMAP(far))
209 * We can also fault in the userspace handling functions,
210 * e.g. copyin. In these cases we will have set a fault
211 * handler so we can check if this is set before calling
214 if (map != kernel_map && pcb->pcb_onfault == 0)
218 if (pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
222 KASSERT(td->td_md.md_spinlock_count == 0,
223 ("data abort with spinlock held"));
224 if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
225 WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
226 print_registers(frame);
227 printf(" far: %16lx\n", far);
228 printf(" esr: %.8lx\n", esr);
229 panic("data abort in critical section or under mutex");
232 va = trunc_page(far);
233 ftype = ((esr >> 6) & 1) ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
235 ftype |= VM_PROT_EXECUTE;
237 /* Fault in the page. */
238 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
239 if (error != KERN_SUCCESS) {
242 if (error == KERN_PROTECTION_FAILURE)
246 call_trapsignal(td, sig, ucode, (void *)far);
248 if (td->td_intr_nesting_level == 0 &&
249 pcb->pcb_onfault != 0) {
250 frame->tf_x[0] = error;
251 frame->tf_elr = pcb->pcb_onfault;
255 printf("Fatal data abort:\n");
256 print_registers(frame);
257 printf(" far: %16lx\n", far);
258 printf(" esr: %.8lx\n", esr);
261 if (debugger_on_trap) {
262 kdb_why = KDB_WHY_TRAP;
263 handled = kdb_trap(ESR_ELx_EXCEPTION(esr), 0,
265 kdb_why = KDB_WHY_UNSET;
270 panic("vm_fault failed: %lx", frame->tf_elr);
279 print_registers(struct trapframe *frame)
283 for (reg = 0; reg < nitems(frame->tf_x); reg++) {
284 printf(" %sx%d: %16lx\n", (reg < 10) ? " " : "", reg,
287 printf(" sp: %16lx\n", frame->tf_sp);
288 printf(" lr: %16lx\n", frame->tf_lr);
289 printf(" elr: %16lx\n", frame->tf_elr);
290 printf("spsr: %8x\n", frame->tf_spsr);
294 do_el1h_sync(struct thread *td, struct trapframe *frame)
296 struct trapframe *oframe;
300 /* Read the esr register to get the exception details */
302 exception = ESR_ELx_EXCEPTION(esr);
305 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
310 "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td,
311 esr, frame->tf_elr, frame);
313 oframe = td->td_frame;
317 case EXCP_WATCHPT_EL1:
318 case EXCP_SOFTSTP_EL1:
321 td->td_frame = frame;
329 if ((td->td_pcb->pcb_fpflags & PCB_FP_KERN) != 0) {
334 print_registers(frame);
335 printf(" esr: %.8lx\n", esr);
336 panic("VFP exception in the kernel");
339 case EXCP_INSN_ABORT:
340 case EXCP_DATA_ABORT:
341 far = READ_SPECIALREG(far_el1);
343 data_abort(td, frame, esr, far, 0,
344 exception == EXCP_INSN_ABORT);
348 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
349 dtrace_invop_jump_addr != 0) {
350 dtrace_invop_jump_addr(frame);
355 kdb_trap(exception, 0,
356 (td->td_frame != NULL) ? td->td_frame : frame);
358 panic("No debugger in kernel.\n");
362 case EXCP_WATCHPT_EL1:
363 case EXCP_SOFTSTP_EL1:
365 kdb_trap(exception, 0,
366 (td->td_frame != NULL) ? td->td_frame : frame);
368 panic("No debugger in kernel.\n");
372 if (undef_insn(1, frame))
376 print_registers(frame);
377 panic("Unknown kernel exception %x esr_el1 %lx\n", exception,
381 td->td_frame = oframe;
385 do_el0_sync(struct thread *td, struct trapframe *frame)
387 pcpu_bp_harden bp_harden;
391 /* Check we have a sane environment when entering from userland */
392 KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
393 ("Invalid pcpu address from userland: %p (tpidr %lx)",
394 get_pcpu(), READ_SPECIALREG(tpidr_el1)));
397 exception = ESR_ELx_EXCEPTION(esr);
399 case EXCP_INSN_ABORT_L:
400 far = READ_SPECIALREG(far_el1);
403 * Userspace may be trying to train the branch predictor to
404 * attack the kernel. If we are on a CPU affected by this
405 * call the handler to clear the branch predictor state.
407 if (far > VM_MAXUSER_ADDRESS) {
408 bp_harden = PCPU_GET(bp_harden);
409 if (bp_harden != NULL)
414 case EXCP_DATA_ABORT_L:
415 case EXCP_DATA_ABORT:
416 far = READ_SPECIALREG(far_el1);
422 "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td, esr,
423 frame->tf_elr, frame);
431 panic("VFP exception in userland");
436 svc_handler(td, frame);
438 case EXCP_INSN_ABORT_L:
439 case EXCP_DATA_ABORT_L:
440 case EXCP_DATA_ABORT:
441 data_abort(td, frame, esr, far, 1,
442 exception == EXCP_INSN_ABORT_L);
445 if (!undef_insn(0, frame))
446 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far);
450 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp);
454 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr);
458 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr);
462 call_trapsignal(td, SIGILL, ILL_PRVOPC, (void *)frame->tf_elr);
465 case EXCP_SOFTSTP_EL0:
466 td->td_frame->tf_spsr &= ~PSR_SS;
467 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
468 WRITE_SPECIALREG(MDSCR_EL1,
469 READ_SPECIALREG(MDSCR_EL1) & ~DBG_MDSCR_SS);
470 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
471 (void *)frame->tf_elr);
475 call_trapsignal(td, SIGBUS, BUS_OBJERR, (void *)frame->tf_elr);
480 KASSERT((td->td_pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
481 ("Kernel VFP flags set while entering userspace"));
483 td->td_pcb->pcb_fpusaved == &td->td_pcb->pcb_fpustate,
484 ("Kernel VFP state in use when entering userspace"));
488 * TODO: We will need to handle these later when we support ARMv8.2 RAS.
491 do_serror(struct trapframe *frame)
495 far = READ_SPECIALREG(far_el1);
498 print_registers(frame);
499 printf(" far: %16lx\n", far);
500 printf(" esr: %.8lx\n", esr);
501 panic("Unhandled System Error");
505 unhandled_exception(struct trapframe *frame)
509 far = READ_SPECIALREG(far_el1);
512 print_registers(frame);
513 printf(" far: %16lx\n", far);
514 printf(" esr: %.8lx\n", esr);
515 panic("Unhandled exception");