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