1 /* $NetBSD: undefined.c,v 1.37 2009/03/15 22:23:16 cegger 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 #include "opt_kgdb.h" 51 52 #include <sys/param.h> 53 #ifdef KGDB 54 #include <sys/kgdb.h> 55 #endif 56 57 __KERNEL_RCSID(0, "$NetBSD: undefined.c,v 1.37 2009/03/15 22:23:16 cegger Exp $"); 58 59 #include <sys/malloc.h> 60 #include <sys/queue.h> 61 #include <sys/signal.h> 62 #include <sys/systm.h> 63 #include <sys/proc.h> 64 #include <sys/user.h> 65 #include <sys/syslog.h> 66 #include <sys/vmmeter.h> 67 #ifdef FAST_FPE 68 #include <sys/acct.h> 69 #endif 70 #include <sys/userret.h> 71 72 #include <uvm/uvm_extern.h> 73 74 #include <machine/cpu.h> 75 #include <machine/frame.h> 76 #include <arm/undefined.h> 77 #include <machine/trap.h> 78 79 #include <arch/arm/arm/disassem.h> 80 81 #ifdef DDB 82 #include <ddb/db_output.h> 83 #include <machine/db_machdep.h> 84 #endif 85 86 #ifdef acorn26 87 #include <machine/machdep.h> 88 #endif 89 90 static int gdb_trapper(u_int, u_int, struct trapframe *, int); 91 92 LIST_HEAD(, undefined_handler) undefined_handlers[NUM_UNKNOWN_HANDLERS]; 93 94 95 void * 96 install_coproc_handler(int coproc, undef_handler_t handler) 97 { 98 struct undefined_handler *uh; 99 100 KASSERT(coproc >= 0 && coproc < NUM_UNKNOWN_HANDLERS); 101 KASSERT(handler != NULL); /* Used to be legal. */ 102 103 /* XXX: M_TEMP??? */ 104 uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK); 105 uh->uh_handler = handler; 106 install_coproc_handler_static(coproc, uh); 107 return uh; 108 } 109 110 void 111 install_coproc_handler_static(int coproc, struct undefined_handler *uh) 112 { 113 114 LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link); 115 } 116 117 void 118 remove_coproc_handler(void *cookie) 119 { 120 struct undefined_handler *uh = cookie; 121 122 LIST_REMOVE(uh, uh_link); 123 free(uh, M_TEMP); 124 } 125 126 127 static int 128 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code) 129 { 130 struct lwp *l; 131 l = curlwp; 132 133 #ifdef THUMB_CODE 134 if (frame->tf_spsr & PSR_T_bit) { 135 if (insn == GDB_THUMB_BREAKPOINT) 136 goto bkpt; 137 } 138 else 139 #endif 140 { 141 if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) { 142 #ifdef THUMB_CODE 143 bkpt: 144 #endif 145 if (code == FAULT_USER) { 146 ksiginfo_t ksi; 147 148 KSI_INIT_TRAP(&ksi); 149 ksi.ksi_signo = SIGTRAP; 150 ksi.ksi_code = TRAP_BRKPT; 151 ksi.ksi_addr = (u_int32_t *)addr; 152 ksi.ksi_trap = 0; 153 trapsignal(l, &ksi); 154 return 0; 155 } 156 #ifdef KGDB 157 return !kgdb_trap(T_BREAKPOINT, frame); 158 #endif 159 } 160 } 161 return 1; 162 } 163 164 static struct undefined_handler gdb_uh; 165 #ifdef THUMB_CODE 166 static struct undefined_handler gdb_uh_thumb; 167 #endif 168 169 void 170 undefined_init(void) 171 { 172 int loop; 173 174 /* Not actually necessary -- the initialiser is just NULL */ 175 for (loop = 0; loop < NUM_UNKNOWN_HANDLERS; ++loop) 176 LIST_INIT(&undefined_handlers[loop]); 177 178 /* Install handler for GDB breakpoints */ 179 gdb_uh.uh_handler = gdb_trapper; 180 install_coproc_handler_static(CORE_UNKNOWN_HANDLER, &gdb_uh); 181 #ifdef THUMB_CODE 182 gdb_uh_thumb.uh_handler = gdb_trapper; 183 install_coproc_handler_static(THUMB_UNKNOWN_HANDLER, &gdb_uh_thumb); 184 #endif 185 } 186 187 void 188 undefinedinstruction(trapframe_t *frame) 189 { 190 struct lwp *l; 191 u_int fault_pc; 192 int fault_instruction; 193 int fault_code; 194 int coprocessor; 195 int user; 196 struct undefined_handler *uh; 197 #ifdef VERBOSE_ARM32 198 int s; 199 #endif 200 201 /* Enable interrupts if they were enabled before the exception. */ 202 #ifdef acorn26 203 if ((frame->tf_r15 & R15_IRQ_DISABLE) == 0) 204 int_on(); 205 #else 206 restore_interrupts(frame->tf_spsr & IF32_bits); 207 #endif 208 209 #ifndef acorn26 210 #ifdef THUMB_CODE 211 if (frame->tf_spsr & PSR_T_bit) 212 frame->tf_pc -= THUMB_INSN_SIZE; 213 else 214 #endif 215 { 216 frame->tf_pc -= INSN_SIZE; 217 } 218 #endif 219 220 #ifdef __PROG26 221 fault_pc = frame->tf_r15 & R15_PC; 222 #else 223 fault_pc = frame->tf_pc; 224 #endif 225 226 /* Get the current lwp/proc structure or lwp0/proc0 if there is none. */ 227 l = curlwp; 228 229 #ifdef __PROG26 230 if ((frame->tf_r15 & R15_MODE) == R15_MODE_USR) { 231 #else 232 if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) { 233 #endif 234 user = 1; 235 LWP_CACHE_CREDS(l, l->l_proc); 236 } else 237 user = 0; 238 239 240 #ifdef THUMB_CODE 241 if (frame->tf_spsr & PSR_T_bit) { 242 fault_instruction = fusword((void *)(fault_pc & ~1)); 243 } 244 else 245 #endif 246 { 247 /* 248 * Make sure the program counter is correctly aligned so we 249 * don't take an alignment fault trying to read the opcode. 250 */ 251 if (__predict_false((fault_pc & 3) != 0)) { 252 ksiginfo_t ksi; 253 /* Give the user an illegal instruction signal. */ 254 KSI_INIT_TRAP(&ksi); 255 ksi.ksi_signo = SIGILL; 256 ksi.ksi_code = ILL_ILLOPC; 257 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc; 258 trapsignal(l, &ksi); 259 userret(l); 260 return; 261 } 262 /* 263 * Should use fuword() here .. but in the interests of 264 * squeezing every bit of speed we will just use 265 * ReadWord(). We know the instruction can be read 266 * as was just executed so this will never fail unless 267 * the kernel is screwed up in which case it does 268 * not really matter does it ? 269 */ 270 271 fault_instruction = *(u_int32_t *)fault_pc; 272 } 273 274 /* Update vmmeter statistics */ 275 uvmexp.traps++; 276 277 #ifdef THUMB_CODE 278 if (frame->tf_spsr & PSR_T_bit) { 279 coprocessor = THUMB_UNKNOWN_HANDLER; 280 } 281 else 282 #endif 283 { 284 /* Check for coprocessor instruction */ 285 286 /* 287 * According to the datasheets you only need to look at 288 * bit 27 of the instruction to tell the difference 289 * between and undefined instruction and a coprocessor 290 * instruction following an undefined instruction trap. 291 * 292 * ARMv5 adds undefined instructions in the NV space, 293 * even when bit 27 is set. 294 */ 295 296 if ((fault_instruction & (1 << 27)) != 0 297 && (fault_instruction & 0xf0000000) != 0xf0000000) 298 coprocessor = (fault_instruction >> 8) & 0x0f; 299 else 300 coprocessor = CORE_UNKNOWN_HANDLER; 301 } 302 303 if (user) { 304 /* 305 * Modify the fault_code to reflect the USR/SVC state at 306 * time of fault. 307 */ 308 fault_code = FAULT_USER; 309 l->l_addr->u_pcb.pcb_tf = frame; 310 } else 311 fault_code = 0; 312 313 /* OK this is were we do something about the instruction. */ 314 LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link) 315 if (uh->uh_handler(fault_pc, fault_instruction, frame, 316 fault_code) == 0) 317 break; 318 319 if (uh == NULL) { 320 /* Fault has not been handled */ 321 ksiginfo_t ksi; 322 323 #ifdef VERBOSE_ARM32 324 s = spltty(); 325 326 if ((fault_instruction & 0x0f000010) == 0x0e000000) { 327 printf("CDP\n"); 328 disassemble(fault_pc); 329 } else if ((fault_instruction & 0x0e000000) == 0x0c000000) { 330 printf("LDC/STC\n"); 331 disassemble(fault_pc); 332 } else if ((fault_instruction & 0x0f000010) == 0x0e000010) { 333 printf("MRC/MCR\n"); 334 disassemble(fault_pc); 335 } else if ((fault_instruction & ~INSN_COND_MASK) 336 != (KERNEL_BREAKPOINT & ~INSN_COND_MASK)) { 337 printf("Undefined instruction\n"); 338 disassemble(fault_pc); 339 } 340 341 splx(s); 342 #endif 343 344 if ((fault_code & FAULT_USER) == 0) { 345 #ifdef DDB 346 db_printf("Undefined instruction in kernel\n"); 347 kdb_trap(T_FAULT, frame); 348 #else 349 panic("undefined instruction in kernel"); 350 #endif 351 } 352 KSI_INIT_TRAP(&ksi); 353 ksi.ksi_signo = SIGILL; 354 ksi.ksi_code = ILL_ILLOPC; 355 ksi.ksi_addr = (u_int32_t *)fault_pc; 356 ksi.ksi_trap = fault_instruction; 357 trapsignal(l, &ksi); 358 } 359 360 if ((fault_code & FAULT_USER) == 0) 361 return; 362 363 #ifdef FAST_FPE 364 /* Optimised exit code */ 365 { 366 /* 367 * Check for reschedule request, at the moment there is only 368 * 1 ast so this code should always be run 369 */ 370 if (curcpu()->ci_want_resched) { 371 /* 372 * We are being preempted. 373 */ 374 preempt(); 375 } 376 377 /* Invoke MI userret code */ 378 mi_userret(l); 379 } 380 #else 381 userret(l); 382 #endif 383 } 384