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>
57 #include <sys/dtrace_bsd.h>
61 #include <machine/vfp.h>
65 #include <machine/db_machdep.h>
69 #include <ddb/db_output.h>
72 extern register_t fsu_intr_fault;
74 /* Called from exception.S */
75 void do_el1h_sync(struct trapframe *);
76 void do_el0_sync(struct trapframe *);
77 void do_el0_error(struct trapframe *);
78 static void print_registers(struct trapframe *frame);
80 int (*dtrace_invop_jump_addr)(struct trapframe *);
83 call_trapsignal(struct thread *td, int sig, int code, void *addr)
87 ksiginfo_init_trap(&ksi);
95 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
103 ap = td->td_frame->tf_x;
105 sa->code = td->td_frame->tf_x[8];
107 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
112 if (p->p_sysent->sv_mask)
113 sa->code &= p->p_sysent->sv_mask;
114 if (sa->code >= p->p_sysent->sv_size)
115 sa->callp = &p->p_sysent->sv_table[0];
117 sa->callp = &p->p_sysent->sv_table[sa->code];
119 sa->narg = sa->callp->sy_narg;
120 memcpy(sa->args, ap, nap * sizeof(register_t));
122 panic("ARM64TODO: Could we have more than 8 args?");
124 td->td_retval[0] = 0;
125 td->td_retval[1] = 0;
130 #include "../../kern/subr_syscall.c"
133 svc_handler(struct trapframe *frame)
135 struct syscall_args sa;
141 error = syscallenter(td, &sa);
142 syscallret(td, error, &sa);
146 data_abort(struct trapframe *frame, uint64_t esr, uint64_t far, int lower)
154 int error, sig, ucode;
157 * According to the ARMv8-A rev. A.g, B2.10.5 "Load-Exclusive
158 * and Store-Exclusive instruction usage restrictions", state
159 * of the exclusive monitors after data abort exception is unknown.
174 * Special case for fuswintr and suswintr. These can't sleep so
175 * handle them early on in the trap handler.
177 if (__predict_false(pcb->pcb_onfault == (vm_offset_t)&fsu_intr_fault)) {
178 frame->tf_elr = pcb->pcb_onfault;
182 KASSERT(td->td_md.md_spinlock_count == 0,
183 ("data abort with spinlock held"));
184 if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
185 WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
186 print_registers(frame);
187 printf(" far: %16lx\n", far);
188 printf(" esr: %.8lx\n", esr);
189 panic("data abort in critical section or under mutex");
194 map = &p->p_vmspace->vm_map;
196 /* The top bit tells us which range to use */
197 if ((far >> 63) == 1)
200 map = &p->p_vmspace->vm_map;
203 va = trunc_page(far);
204 ftype = ((esr >> 6) & 1) ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
206 /* Fault in the page. */
207 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
208 if (error != KERN_SUCCESS) {
211 if (error == KERN_PROTECTION_FAILURE)
215 call_trapsignal(td, sig, ucode, (void *)far);
217 if (td->td_intr_nesting_level == 0 &&
218 pcb->pcb_onfault != 0) {
219 frame->tf_x[0] = error;
220 frame->tf_elr = pcb->pcb_onfault;
224 printf("Fatal data abort:\n");
225 print_registers(frame);
226 printf(" far: %16lx\n", far);
227 printf(" esr: %.8lx\n", esr);
230 if (debugger_on_panic || kdb_active)
231 if (kdb_trap(ESR_ELx_EXCEPTION(esr), 0, frame))
234 panic("vm_fault failed: %lx", frame->tf_elr);
243 print_registers(struct trapframe *frame)
247 for (reg = 0; reg < 31; reg++) {
248 printf(" %sx%d: %16lx\n", (reg < 10) ? " " : "", reg,
251 printf(" sp: %16lx\n", frame->tf_sp);
252 printf(" lr: %16lx\n", frame->tf_lr);
253 printf(" elr: %16lx\n", frame->tf_elr);
254 printf("spsr: %16lx\n", frame->tf_spsr);
258 do_el1h_sync(struct trapframe *frame)
263 /* Read the esr register to get the exception details */
264 esr = READ_SPECIALREG(esr_el1);
265 exception = ESR_ELx_EXCEPTION(esr);
268 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
273 "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p",
274 curthread, esr, frame->tf_elr, frame);
279 print_registers(frame);
280 printf(" esr: %.8lx\n", esr);
281 panic("VFP exception in the kernel");
282 case EXCP_DATA_ABORT:
283 far = READ_SPECIALREG(far_el1);
285 data_abort(frame, esr, far, 0);
289 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
290 dtrace_invop_jump_addr != 0) {
291 dtrace_invop_jump_addr(frame);
296 case EXCP_WATCHPT_EL1:
297 case EXCP_SOFTSTP_EL1:
299 kdb_trap(exception, 0, frame);
301 panic("No debugger in kernel.\n");
305 print_registers(frame);
306 panic("Unknown kernel exception %x esr_el1 %lx\n", exception,
312 * The attempted execution of an instruction bit pattern that has no allocated
313 * instruction results in an exception with an unknown reason.
316 el0_excp_unknown(struct trapframe *frame)
322 far = READ_SPECIALREG(far_el1);
323 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far);
328 do_el0_sync(struct trapframe *frame)
334 /* Check we have a sane environment when entering from userland */
335 KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
336 ("Invalid pcpu address from userland: %p (tpidr %lx)",
337 get_pcpu(), READ_SPECIALREG(tpidr_el1)));
340 td->td_frame = frame;
342 esr = READ_SPECIALREG(esr_el1);
343 exception = ESR_ELx_EXCEPTION(esr);
345 case EXCP_INSN_ABORT_L:
346 case EXCP_DATA_ABORT_L:
347 case EXCP_DATA_ABORT:
348 far = READ_SPECIALREG(far_el1);
353 "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p",
354 curthread, esr, frame->tf_elr, frame);
362 panic("VFP exception in userland");
368 case EXCP_INSN_ABORT_L:
369 case EXCP_DATA_ABORT_L:
370 case EXCP_DATA_ABORT:
371 data_abort(frame, esr, far, 1);
374 el0_excp_unknown(frame);
377 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp);
381 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr);
385 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr);
388 case EXCP_SOFTSTP_EL0:
389 td->td_frame->tf_spsr &= ~PSR_SS;
390 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
391 WRITE_SPECIALREG(MDSCR_EL1,
392 READ_SPECIALREG(MDSCR_EL1) & ~DBG_MDSCR_SS);
393 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
394 (void *)frame->tf_elr);
398 print_registers(frame);
399 panic("Unknown userland exception %x esr_el1 %lx\n", exception,
405 do_el0_error(struct trapframe *frame)
408 panic("ARM64TODO: do_el0_error");