1 /* $NetBSD: undefined.c,v 1.9 2001/10/18 21:26:21 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.9 2001/10/18 21:26:21 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 || insn == GDB5_BREAKPOINT) && 128 code == FAULT_USER) { 129 trapsignal(curproc, SIGTRAP, 0); 130 return 0; 131 } 132 return 1; 133 } 134 135 static struct undefined_handler gdb_uh; 136 137 void 138 undefined_init() 139 { 140 int loop; 141 142 /* Not actually necessary -- the initialiser is just NULL */ 143 for (loop = 0; loop < MAX_COPROCS; ++loop) 144 LIST_INIT(&undefined_handlers[loop]); 145 146 /* Install handler for GDB breakpoints */ 147 gdb_uh.uh_handler = gdb_trapper; 148 install_coproc_handler_static(0, &gdb_uh); 149 } 150 151 152 void 153 undefinedinstruction(trapframe_t *frame) 154 { 155 struct proc *p; 156 u_int fault_pc; 157 int fault_instruction; 158 int fault_code; 159 int coprocessor; 160 struct undefined_handler *uh; 161 162 /* Enable interrupts if they were enabled before the exception. */ 163 #ifdef arm26 164 if ((frame->tf_r15 & R15_IRQ_DISABLE) == 0) 165 int_on(); 166 #else 167 if (!(frame->tf_spsr & I32_bit)) 168 enable_interrupts(I32_bit); 169 #endif 170 171 #ifndef arm26 172 frame->tf_pc -= INSN_SIZE; 173 #endif 174 175 #ifdef PROG26 176 fault_pc = frame->tf_r15 & R15_PC; 177 #else 178 fault_pc = frame->tf_pc; 179 #endif 180 181 /* 182 * Should use fuword() here .. but in the interests of squeezing every 183 * bit of speed we will just use ReadWord(). We know the instruction 184 * can be read as was just executed so this will never fail unless the 185 * kernel is screwed up in which case it does not really matter does 186 * it ? 187 */ 188 189 fault_instruction = *(u_int32_t *)fault_pc; 190 191 /* Update vmmeter statistics */ 192 uvmexp.traps++; 193 194 /* Check for coprocessor instruction */ 195 196 /* 197 * According to the datasheets you only need to look at bit 27 of the 198 * instruction to tell the difference between and undefined 199 * instruction and a coprocessor instruction following an undefined 200 * instruction trap. 201 */ 202 203 if ((fault_instruction & (1 << 27)) != 0) 204 coprocessor = (fault_instruction >> 8) & 0x0f; 205 else 206 coprocessor = 0; 207 208 /* Get the current proc structure or proc0 if there is none. */ 209 210 if ((p = curproc) == 0) 211 p = &proc0; 212 213 #ifdef PROG26 214 if ((frame->tf_r15 & R15_MODE) == R15_MODE_USR) { 215 #else 216 if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) { 217 #endif 218 /* 219 * Modify the fault_code to reflect the USR/SVC state at 220 * time of fault. 221 */ 222 fault_code = FAULT_USER; 223 p->p_addr->u_pcb.pcb_tf = frame; 224 } else 225 fault_code = 0; 226 227 /* OK this is were we do something about the instruction. */ 228 LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link) 229 if (uh->uh_handler(fault_pc, fault_instruction, frame, 230 fault_code) == 0) 231 break; 232 233 if (uh == NULL) { 234 /* Fault has not been handled */ 235 236 #ifdef VERBOSE_ARM32 237 s = spltty(); 238 239 if ((fault_instruction & 0x0f000010) == 0x0e000000) { 240 printf("CDP\n"); 241 disassemble(fault_pc); 242 } else if ((fault_instruction & 0x0e000000) == 0x0c000000) { 243 printf("LDC/STC\n"); 244 disassemble(fault_pc); 245 } else if ((fault_instruction & 0x0f000010) == 0x0e000010) { 246 printf("MRC/MCR\n"); 247 disassemble(fault_pc); 248 } else if ((fault_instruction & ~INSN_COND_MASK) 249 != (KERNEL_BREAKPOINT & ~INSN_COND_MASK)) { 250 printf("Undefined instruction\n"); 251 disassemble(fault_pc); 252 } 253 254 splx(s); 255 #endif 256 257 if ((fault_code & FAULT_USER) == 0) { 258 printf("Undefined instruction in kernel\n"); 259 #ifdef DDB 260 Debugger(); 261 #endif 262 } 263 264 trapsignal(p, SIGILL, fault_instruction); 265 } 266 267 if ((fault_code & FAULT_USER) == 0) 268 return; 269 270 #ifdef FAST_FPE 271 /* Optimised exit code */ 272 { 273 int sig; 274 275 /* take pending signals */ 276 277 while ((sig = (CURSIG(p))) != 0) { 278 postsig(sig); 279 } 280 281 p->p_priority = p->p_usrpri; 282 283 /* 284 * Check for reschedule request, at the moment there is only 285 * 1 ast so this code should always be run 286 */ 287 288 if (want_resched) { 289 /* 290 * We are being preempted. 291 */ 292 preempt(NULL); 293 while ((sig = (CURSIG(p))) != 0) { 294 postsig(sig); 295 } 296 } 297 298 curcpu()->ci_schedstate.spc_curpriority = p->p_priority; 299 } 300 301 #else 302 userret(p); 303 #endif 304 } 305 306 307 void 308 resethandler(trapframe_t *frame) 309 { 310 #ifdef DDB 311 /* Extra info in case panic drops us into the debugger. */ 312 printf("Trap frame at %p\n", frame); 313 #endif 314 panic("Branch to never-never land (zero)..... we're dead\n"); 315 } 316 317 /* End of undefined.c */ 318