1 /* $NetBSD: undefined.c,v 1.22 2003/11/29 22:21:29 bjh21 Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (c) 2001 Ben Harris.
7 * Copyright (c) 1995 Mark Brinicombe.
8 * Copyright (c) 1995 Brini.
11 * This code is derived from software written for Brini by Mark Brinicombe
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
40 * RiscBSD kernel project
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include <sys/param.h>
56 #include <sys/malloc.h>
57 #include <sys/queue.h>
58 #include <sys/signal.h>
59 #include <sys/systm.h>
61 #include <sys/syslog.h>
62 #include <sys/vmmeter.h>
64 #include <sys/mutex.h>
65 #include <sys/signalvar.h>
66 #include <sys/ptrace.h>
67 #include <sys/vmmeter.h>
73 #include <vm/vm_extern.h>
75 #include <machine/armreg.h>
76 #include <machine/asm.h>
77 #include <machine/cpu.h>
78 #include <machine/frame.h>
79 #include <machine/undefined.h>
80 #include <machine/trap.h>
82 #include <machine/disassem.h>
85 #include <ddb/db_output.h>
89 #include <machine/db_machdep.h>
92 #define ARM_COPROC_INSN(insn) (((insn) & (1 << 27)) != 0)
93 #define ARM_VFP_INSN(insn) ((((insn) & 0xfe000000) == 0xf2000000) || \
94 (((insn) & 0xff100000) == 0xf4000000))
95 #define ARM_COPROC(insn) (((insn) >> 8) & 0xf)
97 #define THUMB_32BIT_INSN(insn) ((insn) >= 0xe800)
98 #define THUMB_COPROC_INSN(insn) (((insn) & (3 << 26)) == (3 << 26))
99 #define THUMB_COPROC_UNDEFINED(insn) (((insn) & 0x3e << 20) == 0)
100 #define THUMB_VFP_INSN(insn) (((insn) & (3 << 24)) == (3 << 24))
101 #define THUMB_COPROC(insn) (((insn) >> 8) & 0xf)
103 #define COPROC_VFP 10
105 static int gdb_trapper(u_int, u_int, struct trapframe *, int);
107 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
111 install_coproc_handler(int coproc, undef_handler_t handler)
113 struct undefined_handler *uh;
115 KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc"));
116 KASSERT(handler != NULL, ("handler is NULL")); /* Used to be legal. */
119 uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK);
120 uh->uh_handler = handler;
121 install_coproc_handler_static(coproc, uh);
126 install_coproc_handler_static(int coproc, struct undefined_handler *uh)
129 LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
133 remove_coproc_handler(void *cookie)
135 struct undefined_handler *uh = cookie;
137 LIST_REMOVE(uh, uh_link);
143 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
148 td = (curthread == NULL) ? &thread0 : curthread;
150 if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
151 if (code == FAULT_USER) {
152 ksiginfo_init_trap(&ksi);
153 ksi.ksi_signo = SIGTRAP;
154 ksi.ksi_code = TRAP_BRKPT;
155 ksi.ksi_addr = (u_int32_t *)addr;
156 trapsignal(td, &ksi);
161 return !kgdb_trap(T_BREAKPOINT, frame);
168 static struct undefined_handler gdb_uh;
175 /* Not actually necessary -- the initialiser is just NULL */
176 for (loop = 0; loop < MAX_COPROCS; ++loop)
177 LIST_INIT(&undefined_handlers[loop]);
179 /* Install handler for GDB breakpoints */
180 gdb_uh.uh_handler = gdb_trapper;
181 install_coproc_handler_static(0, &gdb_uh);
186 undefinedinstruction(struct trapframe *frame)
190 int fault_instruction;
193 struct undefined_handler *uh;
200 /* Enable interrupts if they were enabled before the exception. */
201 if (__predict_true(frame->tf_spsr & PSR_I) == 0)
202 enable_interrupts(PSR_I);
203 if (__predict_true(frame->tf_spsr & PSR_F) == 0)
204 enable_interrupts(PSR_F);
208 fault_pc = frame->tf_pc;
211 * Get the current thread/proc structure or thread0/proc0 if there is
214 td = curthread == NULL ? &thread0 : curthread;
217 if ((frame->tf_spsr & PSR_T) == 0) {
219 * Make sure the program counter is correctly aligned so we
220 * don't take an alignment fault trying to read the opcode.
222 if (__predict_false((fault_pc & 3) != 0)) {
223 ksiginfo_init_trap(&ksi);
224 ksi.ksi_signo = SIGILL;
225 ksi.ksi_code = ILL_ILLADR;
226 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
227 trapsignal(td, &ksi);
233 * Should use fuword() here .. but in the interests of
234 * squeezing every bit of speed we will just use ReadWord().
235 * We know the instruction can be read as was just executed
236 * so this will never fail unless the kernel is screwed up
237 * in which case it does not really matter does it ?
240 fault_instruction = *(u_int32_t *)fault_pc;
242 /* Check for coprocessor instruction */
245 * According to the datasheets you only need to look at bit
246 * 27 of the instruction to tell the difference between and
247 * undefined instruction and a coprocessor instruction
248 * following an undefined instruction trap.
251 if (ARM_COPROC_INSN(fault_instruction))
252 coprocessor = ARM_COPROC(fault_instruction);
253 else { /* check for special instructions */
254 if (ARM_VFP_INSN(fault_instruction))
255 coprocessor = COPROC_VFP; /* vfp / simd */
259 fault_instruction = *(uint16_t *)fault_pc;
260 if (THUMB_32BIT_INSN(fault_instruction)) {
261 fault_instruction <<= 16;
262 fault_instruction |= *(uint16_t *)(fault_pc + 2);
265 * Is it a Coprocessor, Advanced SIMD, or
266 * Floating-point instruction.
268 if (THUMB_COPROC_INSN(fault_instruction)) {
269 if (THUMB_COPROC_UNDEFINED(fault_instruction)) {
271 } else if (THUMB_VFP_INSN(fault_instruction))
272 coprocessor = COPROC_VFP;
275 THUMB_COPROC(fault_instruction);
280 * No support for Thumb-2 on this cpu
282 ksiginfo_init_trap(&ksi);
283 ksi.ksi_signo = SIGILL;
284 ksi.ksi_code = ILL_ILLADR;
285 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
286 trapsignal(td, &ksi);
292 if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
294 * Modify the fault_code to reflect the USR/SVC state at
297 fault_code = FAULT_USER;
298 td->td_frame = frame;
302 /* OK this is were we do something about the instruction. */
303 LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
304 if (uh->uh_handler(fault_pc, fault_instruction, frame,
308 if (fault_code & FAULT_USER) {
309 /* TODO: No support for ptrace from Thumb-2 */
310 if ((frame->tf_spsr & PSR_T) == 0 &&
311 fault_instruction == PTRACE_BREAKPOINT) {
312 PROC_LOCK(td->td_proc);
314 error = ptrace_clear_single_step(td);
316 PROC_UNLOCK(td->td_proc);
318 ksiginfo_init_trap(&ksi);
319 ksi.ksi_signo = SIGILL;
320 ksi.ksi_code = ILL_ILLOPC;
321 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
322 trapsignal(td, &ksi);
328 if (uh == NULL && (fault_code & FAULT_USER)) {
329 /* Fault has not been handled */
330 ksiginfo_init_trap(&ksi);
331 ksi.ksi_signo = SIGILL;
332 ksi.ksi_code = ILL_ILLOPC;
333 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
334 trapsignal(td, &ksi);
337 if ((fault_code & FAULT_USER) == 0) {
338 if (fault_instruction == KERNEL_BREAKPOINT) {
340 kdb_trap(T_BREAKPOINT, 0, frame);
342 printf("No debugger in kernel.\n");
347 panic("Undefined instruction in kernel.\n");