1 /* $NetBSD: undefined.c,v 1.15 2002/05/02 22:47:09 rjs Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Ben Harris. 5 * Copyright (c) 1995 Mark Brinicombe. 6 * Copyright (c) 1995 Brini. 7 * All rights reserved. 8 * 9 * This code is derived from software written for Brini by Mark Brinicombe 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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. 25 * 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 36 * SUCH DAMAGE. 37 * 38 * RiscBSD kernel project 39 * 40 * undefined.c 41 * 42 * Fault handler 43 * 44 * Created : 06/01/95 45 */ 46 47 #define FAST_FPE 48 49 #include "opt_ddb.h" 50 51 #include <sys/param.h> 52 53 __KERNEL_RCSID(0, "$NetBSD: undefined.c,v 1.15 2002/05/02 22:47:09 rjs Exp $"); 54 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/user.h> 61 #include <sys/syslog.h> 62 #include <sys/vmmeter.h> 63 #ifdef FAST_FPE 64 #include <sys/acct.h> 65 #endif 66 67 #include <uvm/uvm_extern.h> 68 69 #include <machine/cpu.h> 70 #include <machine/frame.h> 71 #include <arm/undefined.h> 72 #include <machine/trap.h> 73 74 #include <arch/arm/arm/disassem.h> 75 76 #ifdef acorn26 77 #include <machine/machdep.h> 78 #endif 79 80 static int gdb_trapper(u_int, u_int, struct trapframe *, int); 81 82 #ifdef FAST_FPE 83 extern int want_resched; 84 #endif 85 86 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS]; 87 88 89 void * 90 install_coproc_handler(int coproc, undef_handler_t handler) 91 { 92 struct undefined_handler *uh; 93 94 KASSERT(coproc >= 0 && coproc < MAX_COPROCS); 95 KASSERT(handler != NULL); /* Used to be legal. */ 96 97 /* XXX: M_TEMP??? */ 98 MALLOC(uh, struct undefined_handler *, sizeof(*uh), M_TEMP, M_WAITOK); 99 uh->uh_handler = handler; 100 install_coproc_handler_static(coproc, uh); 101 return uh; 102 } 103 104 void 105 install_coproc_handler_static(int coproc, struct undefined_handler *uh) 106 { 107 108 LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link); 109 } 110 111 void 112 remove_coproc_handler(void *cookie) 113 { 114 struct undefined_handler *uh = cookie; 115 116 LIST_REMOVE(uh, uh_link); 117 FREE(uh, M_TEMP); 118 } 119 120 121 static int 122 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code) 123 { 124 125 if ((insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) && 126 code == FAULT_USER) { 127 trapsignal(curproc, SIGTRAP, 0); 128 return 0; 129 } 130 return 1; 131 } 132 133 static struct undefined_handler gdb_uh; 134 135 void 136 undefined_init() 137 { 138 int loop; 139 140 /* Not actually necessary -- the initialiser is just NULL */ 141 for (loop = 0; loop < MAX_COPROCS; ++loop) 142 LIST_INIT(&undefined_handlers[loop]); 143 144 /* Install handler for GDB breakpoints */ 145 gdb_uh.uh_handler = gdb_trapper; 146 install_coproc_handler_static(0, &gdb_uh); 147 } 148 149 150 void 151 undefinedinstruction(trapframe_t *frame) 152 { 153 struct proc *p; 154 u_int fault_pc; 155 int fault_instruction; 156 int fault_code; 157 int coprocessor; 158 struct undefined_handler *uh; 159 #ifdef VERBOSE_ARM32 160 int s; 161 #endif 162 163 /* Enable interrupts if they were enabled before the exception. */ 164 #ifdef acorn26 165 if ((frame->tf_r15 & R15_IRQ_DISABLE) == 0) 166 int_on(); 167 #else 168 if (!(frame->tf_spsr & I32_bit)) 169 enable_interrupts(I32_bit); 170 #endif 171 172 #ifndef acorn26 173 frame->tf_pc -= INSN_SIZE; 174 #endif 175 176 #ifdef __PROG26 177 fault_pc = frame->tf_r15 & R15_PC; 178 #else 179 fault_pc = frame->tf_pc; 180 #endif 181 182 /* 183 * Should use fuword() here .. but in the interests of squeezing every 184 * bit of speed we will just use ReadWord(). We know the instruction 185 * can be read as was just executed so this will never fail unless the 186 * kernel is screwed up in which case it does not really matter does 187 * it ? 188 */ 189 190 fault_instruction = *(u_int32_t *)fault_pc; 191 192 /* Update vmmeter statistics */ 193 uvmexp.traps++; 194 195 /* Check for coprocessor instruction */ 196 197 /* 198 * According to the datasheets you only need to look at bit 27 of the 199 * instruction to tell the difference between and undefined 200 * instruction and a coprocessor instruction following an undefined 201 * instruction trap. 202 */ 203 204 if ((fault_instruction & (1 << 27)) != 0) 205 coprocessor = (fault_instruction >> 8) & 0x0f; 206 else 207 coprocessor = 0; 208 209 /* Get the current proc structure or proc0 if there is none. */ 210 211 if ((p = curproc) == 0) 212 p = &proc0; 213 214 #ifdef __PROG26 215 if ((frame->tf_r15 & R15_MODE) == R15_MODE_USR) { 216 #else 217 if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) { 218 #endif 219 /* 220 * Modify the fault_code to reflect the USR/SVC state at 221 * time of fault. 222 */ 223 fault_code = FAULT_USER; 224 p->p_addr->u_pcb.pcb_tf = frame; 225 } else 226 fault_code = 0; 227 228 /* OK this is were we do something about the instruction. */ 229 LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link) 230 if (uh->uh_handler(fault_pc, fault_instruction, frame, 231 fault_code) == 0) 232 break; 233 234 if (uh == NULL) { 235 /* Fault has not been handled */ 236 237 #ifdef VERBOSE_ARM32 238 s = spltty(); 239 240 if ((fault_instruction & 0x0f000010) == 0x0e000000) { 241 printf("CDP\n"); 242 disassemble(fault_pc); 243 } else if ((fault_instruction & 0x0e000000) == 0x0c000000) { 244 printf("LDC/STC\n"); 245 disassemble(fault_pc); 246 } else if ((fault_instruction & 0x0f000010) == 0x0e000010) { 247 printf("MRC/MCR\n"); 248 disassemble(fault_pc); 249 } else if ((fault_instruction & ~INSN_COND_MASK) 250 != (KERNEL_BREAKPOINT & ~INSN_COND_MASK)) { 251 printf("Undefined instruction\n"); 252 disassemble(fault_pc); 253 } 254 255 splx(s); 256 #endif 257 258 if ((fault_code & FAULT_USER) == 0) { 259 printf("Undefined instruction in kernel\n"); 260 #ifdef DDB 261 Debugger(); 262 #endif 263 } 264 265 trapsignal(p, SIGILL, fault_instruction); 266 } 267 268 if ((fault_code & FAULT_USER) == 0) 269 return; 270 271 #ifdef FAST_FPE 272 /* Optimised exit code */ 273 { 274 int sig; 275 276 /* take pending signals */ 277 278 while ((sig = (CURSIG(p))) != 0) { 279 postsig(sig); 280 } 281 282 p->p_priority = p->p_usrpri; 283 284 /* 285 * Check for reschedule request, at the moment there is only 286 * 1 ast so this code should always be run 287 */ 288 289 if (want_resched) { 290 /* 291 * We are being preempted. 292 */ 293 preempt(NULL); 294 while ((sig = (CURSIG(p))) != 0) { 295 postsig(sig); 296 } 297 } 298 299 curcpu()->ci_schedstate.spc_curpriority = p->p_priority; 300 } 301 302 #else 303 userret(p); 304 #endif 305 } 306