2 * Copyright (c) 2017 Andrew Turner
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <sys/queue.h>
40 #include <sys/signal.h>
41 #include <sys/signalvar.h>
42 #include <sys/sysent.h>
44 #include <machine/frame.h>
45 #include <machine/undefined.h>
46 #include <machine/vmparam.h>
48 MALLOC_DEFINE(M_UNDEF, "undefhandler", "Undefined instruction handler data");
50 struct undef_handler {
51 LIST_ENTRY(undef_handler) uh_link;
52 undef_handler_t uh_handler;
56 * Create two undefined instruction handler lists, one for userspace, one for
57 * the kernel. This allows us to handle instructions that will trap
59 LIST_HEAD(, undef_handler) undef_handlers[2];
62 * Work around a bug in QEMU prior to 2.5.1 where reading unknown ID
63 * registers would raise an exception when they should return 0.
66 id_aa64mmfr2_handler(vm_offset_t va, uint32_t insn, struct trapframe *frame,
71 #define MRS_ID_AA64MMFR2_EL0_MASK (MRS_MASK | 0x000fffe0)
72 #define MRS_ID_AA64MMFR2_EL0_VALUE (MRS_VALUE | 0x00080740)
74 /* mrs xn, id_aa64mfr2_el1 */
75 if ((insn & MRS_ID_AA64MMFR2_EL0_MASK) == MRS_ID_AA64MMFR2_EL0_VALUE) {
76 reg = MRS_REGISTER(insn);
78 frame->tf_elr += INSN_SIZE;
79 if (reg < nitems(frame->tf_x)) {
81 } else if (reg == 30) {
84 /* If reg is 32 then write to xzr, i.e. do nothing */
91 #ifdef COMPAT_FREEBSD32
92 /* arm32 GDB breakpoints */
93 #define GDB_BREAKPOINT 0xe6000011
94 #define GDB5_BREAKPOINT 0xe7ffdefe
96 gdb_trapper(vm_offset_t va, uint32_t insn, struct trapframe *frame,
99 struct thread *td = curthread;
101 if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
102 if (SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
103 va < VM_MAXUSER_ADDRESS) {
106 ksiginfo_init_trap(&ksi);
107 ksi.ksi_signo = SIGTRAP;
108 ksi.ksi_code = TRAP_TRACE;
109 ksi.ksi_addr = (void *)va;
110 trapsignal(td, &ksi);
122 LIST_INIT(&undef_handlers[0]);
123 LIST_INIT(&undef_handlers[1]);
125 install_undef_handler(false, id_aa64mmfr2_handler);
126 #ifdef COMPAT_FREEBSD32
127 install_undef_handler(true, gdb_trapper);
132 install_undef_handler(bool user, undef_handler_t func)
134 struct undef_handler *uh;
136 uh = malloc(sizeof(*uh), M_UNDEF, M_WAITOK);
137 uh->uh_handler = func;
138 LIST_INSERT_HEAD(&undef_handlers[user ? 0 : 1], uh, uh_link);
144 remove_undef_handler(void *handle)
146 struct undef_handler *uh;
149 LIST_REMOVE(uh, uh_link);
150 free(handle, M_UNDEF);
154 undef_insn(u_int el, struct trapframe *frame)
156 struct undef_handler *uh;
160 KASSERT(el < 2, ("Invalid exception level %u", el));
163 ret = fueword32((uint32_t *)frame->tf_elr, &insn);
165 panic("Unable to read userspace faulting instruction");
167 insn = *(uint32_t *)frame->tf_elr;
170 LIST_FOREACH(uh, &undef_handlers[el], uh_link) {
171 ret = uh->uh_handler(frame->tf_elr, insn, frame, frame->tf_esr);