1 /* $NetBSD: undefined.c,v 1.22 2003/11/29 22:21:29 bjh21 Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 2001 Ben Harris.
7 * Copyright (c) 1995 Mark Brinicombe.
8 * Copyright (c) 1995 Brini.
9 * All rights reserved.
10 *
11 * This code is derived from software written for Brini by Mark Brinicombe
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Brini.
24 * 4. The name of the company nor the name of the author may be used to
25 * endorse or promote products derived from this software without specific
26 * prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
32 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * RiscBSD kernel project
41 *
42 * undefined.c
43 *
44 * Fault handler
45 *
46 * Created : 06/01/95
47 */
48
49 #include "opt_ddb.h"
50
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
53
54 #include <sys/param.h>
55 #include <sys/malloc.h>
56 #include <sys/queue.h>
57 #include <sys/signal.h>
58 #include <sys/systm.h>
59 #include <sys/proc.h>
60 #include <sys/syslog.h>
61 #include <sys/vmmeter.h>
62 #include <sys/lock.h>
63 #include <sys/mutex.h>
64 #include <sys/signalvar.h>
65 #include <sys/ptrace.h>
66 #include <sys/vmmeter.h>
67 #ifdef KDB
68 #include <sys/kdb.h>
69 #endif
70
71 #include <vm/vm.h>
72 #include <vm/vm_extern.h>
73
74 #include <machine/armreg.h>
75 #include <machine/asm.h>
76 #include <machine/cpu.h>
77 #include <machine/frame.h>
78 #include <machine/undefined.h>
79 #include <machine/trap.h>
80
81 #include <machine/disassem.h>
82
83 #ifdef DDB
84 #include <ddb/db_output.h>
85 #endif
86
87 #ifdef KDB
88 #include <machine/db_machdep.h>
89 #endif
90
91 #define ARM_COPROC_INSN(insn) (((insn) & (1 << 27)) != 0)
92 #define ARM_VFP_INSN(insn) ((((insn) & 0xfe000000) == 0xf2000000) || \
93 (((insn) & 0xff100000) == 0xf4000000))
94 #define ARM_COPROC(insn) (((insn) >> 8) & 0xf)
95
96 #define THUMB_32BIT_INSN(insn) ((insn) >= 0xe800)
97 #define THUMB_COPROC_INSN(insn) (((insn) & (3 << 26)) == (3 << 26))
98 #define THUMB_COPROC_UNDEFINED(insn) (((insn) & 0x3e << 20) == 0)
99 #define THUMB_VFP_INSN(insn) (((insn) & (3 << 24)) == (3 << 24))
100 #define THUMB_COPROC(insn) (((insn) >> 8) & 0xf)
101
102 #define COPROC_VFP 10
103
104 static int gdb_trapper(u_int, u_int, struct trapframe *, int);
105
106 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
107
108 void *
109 install_coproc_handler(int coproc, undef_handler_t handler)
110 {
111 struct undefined_handler *uh;
112
113 KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc"));
114 KASSERT(handler != NULL, ("handler is NULL")); /* Used to be legal. */
115
116 /* XXX: M_TEMP??? */
117 uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK);
118 uh->uh_handler = handler;
119 install_coproc_handler_static(coproc, uh);
120 return uh;
121 }
122
123 void
124 install_coproc_handler_static(int coproc, struct undefined_handler *uh)
125 {
126
127 LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
128 }
129
130 void
131 remove_coproc_handler(void *cookie)
132 {
133 struct undefined_handler *uh = cookie;
134
135 LIST_REMOVE(uh, uh_link);
136 free(uh, M_TEMP);
137 }
138
139 static int
140 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
141 {
142 struct thread *td;
143 ksiginfo_t ksi;
144 int error;
145
146 td = (curthread == NULL) ? &thread0 : curthread;
147
148 if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
149 if (code == FAULT_USER) {
150 ksiginfo_init_trap(&ksi);
151 ksi.ksi_signo = SIGTRAP;
152 ksi.ksi_code = TRAP_BRKPT;
153 ksi.ksi_addr = (u_int32_t *)addr;
154 trapsignal(td, &ksi);
155 return 0;
156 }
157 #if 0
158 #ifdef KGDB
159 return !kgdb_trap(T_BREAKPOINT, frame);
160 #endif
161 #endif
162 }
163
164 if (code == FAULT_USER) {
165 /* TODO: No support for ptrace from Thumb-2 */
166 if ((frame->tf_spsr & PSR_T) == 0 &&
167 insn == PTRACE_BREAKPOINT) {
168 PROC_LOCK(td->td_proc);
169 _PHOLD(td->td_proc);
170 error = ptrace_clear_single_step(td);
171 _PRELE(td->td_proc);
172 PROC_UNLOCK(td->td_proc);
173 if (error == 0) {
174 ksiginfo_init_trap(&ksi);
175 ksi.ksi_signo = SIGTRAP;
176 ksi.ksi_code = TRAP_TRACE;
177 ksi.ksi_addr = (u_int32_t *)addr;
178 trapsignal(td, &ksi);
179 return (0);
180 }
181 }
182 }
183
184 return 1;
185 }
186
187 static struct undefined_handler gdb_uh;
188
189 void
190 undefined_init(void)
191 {
192 int loop;
193
194 /* Not actually necessary -- the initialiser is just NULL */
195 for (loop = 0; loop < MAX_COPROCS; ++loop)
196 LIST_INIT(&undefined_handlers[loop]);
197
198 /* Install handler for GDB breakpoints */
199 gdb_uh.uh_handler = gdb_trapper;
200 install_coproc_handler_static(0, &gdb_uh);
201 }
202
203 void
204 undefinedinstruction(struct trapframe *frame)
205 {
206 struct thread *td;
207 u_int fault_pc;
208 int fault_instruction;
209 int fault_code;
210 int coprocessor;
211 struct undefined_handler *uh;
212 #ifdef VERBOSE_ARM32
213 int s;
214 #endif
215 ksiginfo_t ksi;
216
217 /* Enable interrupts if they were enabled before the exception. */
218 if (__predict_true(frame->tf_spsr & PSR_I) == 0)
219 enable_interrupts(PSR_I);
220 if (__predict_true(frame->tf_spsr & PSR_F) == 0)
221 enable_interrupts(PSR_F);
222
223 VM_CNT_INC(v_trap);
224
225 fault_pc = frame->tf_pc;
226
227 /*
228 * Get the current thread/proc structure or thread0/proc0 if there is
229 * none.
230 */
231 td = curthread == NULL ? &thread0 : curthread;
232
233 coprocessor = 0;
234 if ((frame->tf_spsr & PSR_T) == 0) {
235 /*
236 * Make sure the program counter is correctly aligned so we
237 * don't take an alignment fault trying to read the opcode.
238 */
239 if (__predict_false((fault_pc & 3) != 0)) {
240 ksiginfo_init_trap(&ksi);
241 ksi.ksi_signo = SIGILL;
242 ksi.ksi_code = ILL_ILLADR;
243 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
244 trapsignal(td, &ksi);
245 userret(td, frame);
246 return;
247 }
248
249 /*
250 * Should use fuword() here .. but in the interests of
251 * squeezing every bit of speed we will just use ReadWord().
252 * We know the instruction can be read as was just executed
253 * so this will never fail unless the kernel is screwed up
254 * in which case it does not really matter does it ?
255 */
256
257 fault_instruction = *(u_int32_t *)fault_pc;
258
259 /* Check for coprocessor instruction */
260
261 /*
262 * According to the datasheets you only need to look at bit
263 * 27 of the instruction to tell the difference between and
264 * undefined instruction and a coprocessor instruction
265 * following an undefined instruction trap.
266 */
267
268 if (ARM_COPROC_INSN(fault_instruction))
269 coprocessor = ARM_COPROC(fault_instruction);
270 else { /* check for special instructions */
271 if (ARM_VFP_INSN(fault_instruction))
272 coprocessor = COPROC_VFP; /* vfp / simd */
273 }
274 } else {
275 #if __ARM_ARCH >= 7
276 fault_instruction = *(uint16_t *)fault_pc;
277 if (THUMB_32BIT_INSN(fault_instruction)) {
278 fault_instruction <<= 16;
279 fault_instruction |= *(uint16_t *)(fault_pc + 2);
280
281 /*
282 * Is it a Coprocessor, Advanced SIMD, or
283 * Floating-point instruction.
284 */
285 if (THUMB_COPROC_INSN(fault_instruction)) {
286 if (THUMB_COPROC_UNDEFINED(fault_instruction)) {
287 /* undefined insn */
288 } else if (THUMB_VFP_INSN(fault_instruction))
289 coprocessor = COPROC_VFP;
290 else
291 coprocessor =
292 THUMB_COPROC(fault_instruction);
293 }
294 }
295 #else
296 /*
297 * No support for Thumb-2 on this cpu
298 */
299 ksiginfo_init_trap(&ksi);
300 ksi.ksi_signo = SIGILL;
301 ksi.ksi_code = ILL_ILLADR;
302 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
303 trapsignal(td, &ksi);
304 userret(td, frame);
305 return;
306 #endif
307 }
308
309 if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
310 /*
311 * Modify the fault_code to reflect the USR/SVC state at
312 * time of fault.
313 */
314 fault_code = FAULT_USER;
315 td->td_frame = frame;
316 } else
317 fault_code = 0;
318
319 /* OK this is were we do something about the instruction. */
320 LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
321 if (uh->uh_handler(fault_pc, fault_instruction, frame,
322 fault_code) == 0)
323 break;
324
325 if (uh == NULL && (fault_code & FAULT_USER)) {
326 /* Fault has not been handled */
327 ksiginfo_init_trap(&ksi);
328 ksi.ksi_signo = SIGILL;
329 ksi.ksi_code = ILL_ILLOPC;
330 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
331 trapsignal(td, &ksi);
332 }
333
334 if ((fault_code & FAULT_USER) == 0) {
335 if (fault_instruction == KERNEL_BREAKPOINT) {
336 #ifdef KDB
337 kdb_trap(T_BREAKPOINT, 0, frame);
338 #else
339 printf("No debugger in kernel.\n");
340 #endif
341 return;
342 }
343 else
344 panic("Undefined instruction in kernel.\n");
345 }
346
347 userret(td, frame);
348 }
Cache object: a99f36ca9aa5118ff7f4f6e1f579b4b0
|