xref: /freebsd/sys/arm64/arm64/undefined.c (revision 0957b409)
1 /*-
2  * Copyright (c) 2017 Andrew Turner
3  * All rights reserved.
4  *
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.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/proc.h>
39 #include <sys/queue.h>
40 #include <sys/signal.h>
41 #include <sys/signalvar.h>
42 #include <sys/sysent.h>
43 
44 #include <machine/frame.h>
45 #include <machine/undefined.h>
46 #include <machine/vmparam.h>
47 
48 MALLOC_DEFINE(M_UNDEF, "undefhandler", "Undefined instruction handler data");
49 
50 struct undef_handler {
51 	LIST_ENTRY(undef_handler) uh_link;
52 	undef_handler_t		uh_handler;
53 };
54 
55 /*
56  * Create two undefined instruction handler lists, one for userspace, one for
57  * the kernel. This allows us to handle instructions that will trap
58  */
59 LIST_HEAD(, undef_handler) undef_handlers[2];
60 
61 /*
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.
64  */
65 static int
66 id_aa64mmfr2_handler(vm_offset_t va, uint32_t insn, struct trapframe *frame,
67     uint32_t esr)
68 {
69 	int reg;
70 
71 #define	 MRS_ID_AA64MMFR2_EL0_MASK	(MRS_MASK | 0x000fffe0)
72 #define	 MRS_ID_AA64MMFR2_EL0_VALUE	(MRS_VALUE | 0x00080740)
73 
74 	/* mrs xn, id_aa64mfr2_el1 */
75 	if ((insn & MRS_ID_AA64MMFR2_EL0_MASK) == MRS_ID_AA64MMFR2_EL0_VALUE) {
76 		reg = MRS_REGISTER(insn);
77 
78 		frame->tf_elr += INSN_SIZE;
79 		if (reg < nitems(frame->tf_x)) {
80 			frame->tf_x[reg] = 0;
81 		} else if (reg == 30) {
82 			frame->tf_lr = 0;
83 		}
84 		/* If reg is 32 then write to xzr, i.e. do nothing */
85 
86 		return (1);
87 	}
88 	return (0);
89 }
90 
91 #ifdef COMPAT_FREEBSD32
92 /* arm32 GDB breakpoints */
93 #define GDB_BREAKPOINT	0xe6000011
94 #define GDB5_BREAKPOINT	0xe7ffdefe
95 static int
96 gdb_trapper(vm_offset_t va, uint32_t insn, struct trapframe *frame,
97 		uint32_t esr)
98 {
99 	struct thread *td = curthread;
100 
101 	if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
102 		if (SV_PROC_FLAG(td->td_proc, SV_ILP32) &&
103 		    va < VM_MAXUSER_ADDRESS) {
104 			ksiginfo_t ksi;
105 
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);
111 			return 1;
112 		}
113 	}
114 	return 0;
115 }
116 #endif
117 
118 void
119 undef_init(void)
120 {
121 
122 	LIST_INIT(&undef_handlers[0]);
123 	LIST_INIT(&undef_handlers[1]);
124 
125 	install_undef_handler(false, id_aa64mmfr2_handler);
126 #ifdef COMPAT_FREEBSD32
127 	install_undef_handler(true, gdb_trapper);
128 #endif
129 }
130 
131 void *
132 install_undef_handler(bool user, undef_handler_t func)
133 {
134 	struct undef_handler *uh;
135 
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);
139 
140 	return (uh);
141 }
142 
143 void
144 remove_undef_handler(void *handle)
145 {
146 	struct undef_handler *uh;
147 
148 	uh = handle;
149 	LIST_REMOVE(uh, uh_link);
150 	free(handle, M_UNDEF);
151 }
152 
153 int
154 undef_insn(u_int el, struct trapframe *frame)
155 {
156 	struct undef_handler *uh;
157 	uint32_t insn;
158 	int ret;
159 
160 	KASSERT(el < 2, ("Invalid exception level %u", el));
161 
162 	if (el == 0) {
163 		ret = fueword32((uint32_t *)frame->tf_elr, &insn);
164 		if (ret != 0)
165 			panic("Unable to read userspace faulting instruction");
166 	} else {
167 		insn = *(uint32_t *)frame->tf_elr;
168 	}
169 
170 	LIST_FOREACH(uh, &undef_handlers[el], uh_link) {
171 		ret = uh->uh_handler(frame->tf_elr, insn, frame, frame->tf_esr);
172 		if (ret)
173 			return (1);
174 	}
175 
176 	return (0);
177 }
178