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>
37 #include <sys/pioctl.h>
39 #include <sys/ptrace.h>
40 #include <sys/syscall.h>
41 #include <sys/sysent.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_extern.h>
53 #include <machine/frame.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 *);
88 call_trapsignal(struct thread *td, int sig, int code, void *addr)
92 ksiginfo_init_trap(&ksi);
100 cpu_fetch_syscall_args(struct thread *td)
104 struct syscall_args *sa;
109 ap = td->td_frame->tf_x;
112 sa->code = td->td_frame->tf_x[8];
114 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
119 if (sa->code >= p->p_sysent->sv_size)
120 sa->callp = &p->p_sysent->sv_table[0];
122 sa->callp = &p->p_sysent->sv_table[sa->code];
124 sa->narg = sa->callp->sy_narg;
125 memcpy(sa->args, ap, nap * sizeof(register_t));
127 panic("ARM64TODO: Could we have more than 8 args?");
129 td->td_retval[0] = 0;
130 td->td_retval[1] = 0;
135 #include "../../kern/subr_syscall.c"
138 svc_handler(struct thread *td, struct trapframe *frame)
141 if ((frame->tf_esr & ESR_ELx_ISS_MASK) == 0) {
145 call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr);
151 data_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
152 uint64_t far, int lower, int exec)
159 int error, sig, ucode;
165 * According to the ARMv8-A rev. A.g, B2.10.5 "Load-Exclusive
166 * and Store-Exclusive instruction usage restrictions", state
167 * of the exclusive monitors after data abort exception is unknown.
181 map = &p->p_vmspace->vm_map;
183 /* The top bit tells us which range to use */
184 if (far >= VM_MAXUSER_ADDRESS) {
187 map = &p->p_vmspace->vm_map;
194 * Try to handle translation, access flag, and permission faults.
195 * Translation faults may occur as a result of the required
196 * break-before-make sequence used when promoting or demoting
197 * superpages. Such faults must not occur while holding the pmap lock,
198 * or pmap_fault() will recurse on that lock.
200 if ((lower || map == kernel_map || pcb->pcb_onfault != 0) &&
201 pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
204 KASSERT(td->td_md.md_spinlock_count == 0,
205 ("data abort with spinlock held"));
206 if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
207 WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
208 print_registers(frame);
209 printf(" far: %16lx\n", far);
210 printf(" esr: %.8lx\n", esr);
211 panic("data abort in critical section or under mutex");
214 va = trunc_page(far);
216 ftype = VM_PROT_EXECUTE;
218 ftype = (esr & ISS_DATA_WnR) == 0 ? VM_PROT_READ :
219 VM_PROT_READ | VM_PROT_WRITE;
221 /* Fault in the page. */
222 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
223 if (error != KERN_SUCCESS) {
226 if (error == KERN_PROTECTION_FAILURE)
230 call_trapsignal(td, sig, ucode, (void *)far);
232 if (td->td_intr_nesting_level == 0 &&
233 pcb->pcb_onfault != 0) {
234 frame->tf_x[0] = error;
235 frame->tf_elr = pcb->pcb_onfault;
239 printf("Fatal data abort:\n");
240 print_registers(frame);
241 printf(" far: %16lx\n", far);
242 printf(" esr: %.8lx\n", esr);
245 if (debugger_on_trap) {
246 kdb_why = KDB_WHY_TRAP;
247 handled = kdb_trap(ESR_ELx_EXCEPTION(esr), 0,
249 kdb_why = KDB_WHY_UNSET;
254 panic("vm_fault failed: %lx", frame->tf_elr);
263 print_registers(struct trapframe *frame)
267 for (reg = 0; reg < nitems(frame->tf_x); reg++) {
268 printf(" %sx%d: %16lx\n", (reg < 10) ? " " : "", reg,
271 printf(" sp: %16lx\n", frame->tf_sp);
272 printf(" lr: %16lx\n", frame->tf_lr);
273 printf(" elr: %16lx\n", frame->tf_elr);
274 printf("spsr: %8x\n", frame->tf_spsr);
278 do_el1h_sync(struct thread *td, struct trapframe *frame)
280 struct trapframe *oframe;
284 /* Read the esr register to get the exception details */
286 exception = ESR_ELx_EXCEPTION(esr);
289 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
294 "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td,
295 esr, frame->tf_elr, frame);
297 oframe = td->td_frame;
301 case EXCP_WATCHPT_EL1:
302 case EXCP_SOFTSTP_EL1:
305 td->td_frame = frame;
313 if ((td->td_pcb->pcb_fpflags & PCB_FP_KERN) != 0) {
318 print_registers(frame);
319 printf(" esr: %.8lx\n", esr);
320 panic("VFP exception in the kernel");
323 case EXCP_INSN_ABORT:
324 case EXCP_DATA_ABORT:
325 far = READ_SPECIALREG(far_el1);
327 data_abort(td, frame, esr, far, 0,
328 exception == EXCP_INSN_ABORT);
332 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
333 dtrace_invop_jump_addr != 0) {
334 dtrace_invop_jump_addr(frame);
339 kdb_trap(exception, 0,
340 (td->td_frame != NULL) ? td->td_frame : frame);
342 panic("No debugger in kernel.\n");
346 case EXCP_WATCHPT_EL1:
347 case EXCP_SOFTSTP_EL1:
349 kdb_trap(exception, 0,
350 (td->td_frame != NULL) ? td->td_frame : frame);
352 panic("No debugger in kernel.\n");
356 if (undef_insn(1, frame))
360 print_registers(frame);
361 panic("Unknown kernel exception %x esr_el1 %lx\n", exception,
365 td->td_frame = oframe;
369 do_el0_sync(struct thread *td, struct trapframe *frame)
371 pcpu_bp_harden bp_harden;
375 /* Check we have a sane environment when entering from userland */
376 KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
377 ("Invalid pcpu address from userland: %p (tpidr %lx)",
378 get_pcpu(), READ_SPECIALREG(tpidr_el1)));
381 exception = ESR_ELx_EXCEPTION(esr);
383 case EXCP_INSN_ABORT_L:
384 far = READ_SPECIALREG(far_el1);
387 * Userspace may be trying to train the branch predictor to
388 * attack the kernel. If we are on a CPU affected by this
389 * call the handler to clear the branch predictor state.
391 if (far > VM_MAXUSER_ADDRESS) {
392 bp_harden = PCPU_GET(bp_harden);
393 if (bp_harden != NULL)
398 case EXCP_DATA_ABORT_L:
399 case EXCP_DATA_ABORT:
400 far = READ_SPECIALREG(far_el1);
406 "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td, esr,
407 frame->tf_elr, frame);
415 panic("VFP exception in userland");
420 svc_handler(td, frame);
422 case EXCP_INSN_ABORT_L:
423 case EXCP_DATA_ABORT_L:
424 case EXCP_DATA_ABORT:
425 data_abort(td, frame, esr, far, 1,
426 exception == EXCP_INSN_ABORT_L);
429 if (!undef_insn(0, frame))
430 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far);
434 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp);
438 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr);
442 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr);
446 call_trapsignal(td, SIGILL, ILL_PRVOPC, (void *)frame->tf_elr);
449 case EXCP_SOFTSTP_EL0:
450 td->td_frame->tf_spsr &= ~PSR_SS;
451 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
452 WRITE_SPECIALREG(MDSCR_EL1,
453 READ_SPECIALREG(MDSCR_EL1) & ~DBG_MDSCR_SS);
454 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
455 (void *)frame->tf_elr);
459 call_trapsignal(td, SIGBUS, BUS_OBJERR, (void *)frame->tf_elr);
464 KASSERT((td->td_pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
465 ("Kernel VFP flags set while entering userspace"));
467 td->td_pcb->pcb_fpusaved == &td->td_pcb->pcb_fpustate,
468 ("Kernel VFP state in use when entering userspace"));
472 * TODO: We will need to handle these later when we support ARMv8.2 RAS.
475 do_serror(struct trapframe *frame)
479 far = READ_SPECIALREG(far_el1);
482 print_registers(frame);
483 printf(" far: %16lx\n", far);
484 printf(" esr: %.8lx\n", esr);
485 panic("Unhandled System Error");
489 unhandled_exception(struct trapframe *frame)
493 far = READ_SPECIALREG(far_el1);
496 print_registers(frame);
497 printf(" far: %16lx\n", far);
498 printf(" esr: %.8lx\n", esr);
499 panic("Unhandled exception");