1 /* $NetBSD: undefined.c,v 1.22 2003/11/29 22:21:29 bjh21 Exp $ */
4 * Copyright (c) 2001 Ben Harris.
5 * Copyright (c) 1995 Mark Brinicombe.
6 * Copyright (c) 1995 Brini.
9 * This code is derived from software written for Brini by Mark Brinicombe
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Brini.
22 * 4. The name of the company nor the name of the author may be used to
23 * endorse or promote products derived from this software without specific
24 * prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * RiscBSD kernel project
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/malloc.h>
55 #include <sys/queue.h>
56 #include <sys/signal.h>
57 #include <sys/systm.h>
59 #include <sys/syslog.h>
60 #include <sys/vmmeter.h>
61 #include <sys/types.h>
63 #include <sys/mutex.h>
64 #include <sys/signalvar.h>
65 #include <sys/ptrace.h>
74 #include <vm/vm_extern.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>
86 #include <machine/db_machdep.h>
90 #include <machine/machdep.h>
93 static int gdb_trapper(u_int, u_int, struct trapframe *, int);
95 extern int want_resched;
98 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
102 install_coproc_handler(int coproc, undef_handler_t handler)
104 struct undefined_handler *uh;
106 KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc"));
107 KASSERT(handler != NULL, ("handler is NULL")); /* Used to be legal. */
110 MALLOC(uh, struct undefined_handler *, sizeof(*uh), M_TEMP, M_WAITOK);
111 uh->uh_handler = handler;
112 install_coproc_handler_static(coproc, uh);
117 install_coproc_handler_static(int coproc, struct undefined_handler *uh)
120 LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
124 remove_coproc_handler(void *cookie)
126 struct undefined_handler *uh = cookie;
128 LIST_REMOVE(uh, uh_link);
134 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
139 td = (curthread == NULL) ? &thread0 : curthread;
141 if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
142 if (code == FAULT_USER) {
143 ksiginfo_init_trap(&ksi);
144 ksi.ksi_signo = SIGTRAP;
145 ksi.ksi_code = TRAP_BRKPT;
146 ksi.ksi_addr = (u_int32_t *)addr;
147 trapsignal(td, &ksi);
152 return !kgdb_trap(T_BREAKPOINT, frame);
159 static struct undefined_handler gdb_uh;
166 /* Not actually necessary -- the initialiser is just NULL */
167 for (loop = 0; loop < MAX_COPROCS; ++loop)
168 LIST_INIT(&undefined_handlers[loop]);
170 /* Install handler for GDB breakpoints */
171 gdb_uh.uh_handler = gdb_trapper;
172 install_coproc_handler_static(0, &gdb_uh);
177 undefinedinstruction(trapframe_t *frame)
181 int fault_instruction;
184 struct undefined_handler *uh;
190 /* Enable interrupts if they were enabled before the exception. */
191 if (!(frame->tf_spsr & I32_bit))
192 enable_interrupts(I32_bit);
194 frame->tf_pc -= INSN_SIZE;
195 PCPU_LAZY_INC(cnt.v_trap);
197 fault_pc = frame->tf_pc;
200 * Get the current thread/proc structure or thread0/proc0 if there is
203 td = curthread == NULL ? &thread0 : curthread;
206 * Make sure the program counter is correctly aligned so we
207 * don't take an alignment fault trying to read the opcode.
209 if (__predict_false((fault_pc & 3) != 0)) {
210 ksiginfo_init_trap(&ksi);
211 ksi.ksi_signo = SIGILL;
212 ksi.ksi_code = ILL_ILLADR;
213 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
214 trapsignal(td, &ksi);
220 * Should use fuword() here .. but in the interests of squeezing every
221 * bit of speed we will just use ReadWord(). We know the instruction
222 * can be read as was just executed so this will never fail unless the
223 * kernel is screwed up in which case it does not really matter does
227 fault_instruction = *(u_int32_t *)fault_pc;
229 /* Update vmmeter statistics */
233 /* Check for coprocessor instruction */
236 * According to the datasheets you only need to look at bit 27 of the
237 * instruction to tell the difference between and undefined
238 * instruction and a coprocessor instruction following an undefined
242 if ((fault_instruction & (1 << 27)) != 0)
243 coprocessor = (fault_instruction >> 8) & 0x0f;
247 if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
249 * Modify the fault_code to reflect the USR/SVC state at
252 fault_code = FAULT_USER;
253 td->td_frame = frame;
257 /* OK this is were we do something about the instruction. */
258 LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
259 if (uh->uh_handler(fault_pc, fault_instruction, frame,
263 if (fault_code & FAULT_USER && fault_instruction == PTRACE_BREAKPOINT) {
264 PROC_LOCK(td->td_proc);
266 ptrace_clear_single_step(td);
268 PROC_UNLOCK(td->td_proc);
272 if (uh == NULL && (fault_code & FAULT_USER)) {
273 /* Fault has not been handled */
274 ksiginfo_init_trap(&ksi);
275 ksi.ksi_signo = SIGILL;
276 ksi.ksi_code = ILL_ILLOPC;
277 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
278 trapsignal(td, &ksi);
281 if ((fault_code & FAULT_USER) == 0) {
282 if (fault_instruction == KERNEL_BREAKPOINT) {
284 kdb_trap(T_BREAKPOINT, 0, frame);
286 printf("No debugger in kernel.\n");
290 panic("Undefined instruction in kernel.\n");
294 /* Optimised exit code */
298 * Check for reschedule request, at the moment there is only
299 * 1 ast so this code should always be run
304 * We are being preempted.
309 /* Invoke MI userret code */
313 l->l_priority = l->l_usrpri;
315 curcpu()->ci_schedstate.spc_curpriority = l->l_priority;