1 /* $NetBSD: undefined.c,v 1.34 2008/05/21 14:12:06 ad 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.34 2008/05/21 14:12:06 ad 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 MALLOC(uh, struct undefined_handler *, 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() 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 if (!(frame->tf_spsr & I32_bit)) 207 enable_interrupts(I32_bit); 208 #endif 209 210 #ifndef acorn26 211 #ifdef THUMB_CODE 212 if (frame->tf_spsr & PSR_T_bit) 213 frame->tf_pc -= THUMB_INSN_SIZE; 214 else 215 #endif 216 { 217 frame->tf_pc -= INSN_SIZE; 218 } 219 #endif 220 221 #ifdef __PROG26 222 fault_pc = frame->tf_r15 & R15_PC; 223 #else 224 fault_pc = frame->tf_pc; 225 #endif 226 227 /* Get the current lwp/proc structure or lwp0/proc0 if there is none. */ 228 l = curlwp; 229 230 #ifdef __PROG26 231 if ((frame->tf_r15 & R15_MODE) == R15_MODE_USR) { 232 #else 233 if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) { 234 #endif 235 user = 1; 236 LWP_CACHE_CREDS(l, l->l_proc); 237 } else 238 user = 0; 239 240 241 #ifdef THUMB_CODE 242 if (frame->tf_spsr & PSR_T_bit) { 243 fault_instruction = fusword((void *)(fault_pc & ~1)); 244 } 245 else 246 #endif 247 { 248 /* 249 * Make sure the program counter is correctly aligned so we 250 * don't take an alignment fault trying to read the opcode. 251 */ 252 if (__predict_false((fault_pc & 3) != 0)) { 253 ksiginfo_t ksi; 254 /* Give the user an illegal instruction signal. */ 255 KSI_INIT_TRAP(&ksi); 256 ksi.ksi_signo = SIGILL; 257 ksi.ksi_code = ILL_ILLOPC; 258 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc; 259 trapsignal(l, &ksi); 260 userret(l); 261 return; 262 } 263 /* 264 * Should use fuword() here .. but in the interests of 265 * squeezing every bit of speed we will just use 266 * ReadWord(). We know the instruction can be read 267 * as was just executed so this will never fail unless 268 * the kernel is screwed up in which case it does 269 * not really matter does it ? 270 */ 271 272 fault_instruction = *(u_int32_t *)fault_pc; 273 } 274 275 /* Update vmmeter statistics */ 276 uvmexp.traps++; 277 278 #ifdef THUMB_CODE 279 if (frame->tf_spsr & PSR_T_bit) { 280 coprocessor = THUMB_UNKNOWN_HANDLER; 281 } 282 else 283 #endif 284 { 285 /* Check for coprocessor instruction */ 286 287 /* 288 * According to the datasheets you only need to look at 289 * bit 27 of the instruction to tell the difference 290 * between and undefined instruction and a coprocessor 291 * instruction following an undefined instruction trap. 292 * 293 * ARMv5 adds undefined instructions in the NV space, 294 * even when bit 27 is set. 295 */ 296 297 if ((fault_instruction & (1 << 27)) != 0 298 && (fault_instruction & 0xf0000000) != 0xf0000000) 299 coprocessor = (fault_instruction >> 8) & 0x0f; 300 else 301 coprocessor = CORE_UNKNOWN_HANDLER; 302 } 303 304 if (user) { 305 /* 306 * Modify the fault_code to reflect the USR/SVC state at 307 * time of fault. 308 */ 309 fault_code = FAULT_USER; 310 l->l_addr->u_pcb.pcb_tf = frame; 311 } else 312 fault_code = 0; 313 314 /* OK this is were we do something about the instruction. */ 315 LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link) 316 if (uh->uh_handler(fault_pc, fault_instruction, frame, 317 fault_code) == 0) 318 break; 319 320 if (uh == NULL) { 321 /* Fault has not been handled */ 322 ksiginfo_t ksi; 323 324 #ifdef VERBOSE_ARM32 325 s = spltty(); 326 327 if ((fault_instruction & 0x0f000010) == 0x0e000000) { 328 printf("CDP\n"); 329 disassemble(fault_pc); 330 } else if ((fault_instruction & 0x0e000000) == 0x0c000000) { 331 printf("LDC/STC\n"); 332 disassemble(fault_pc); 333 } else if ((fault_instruction & 0x0f000010) == 0x0e000010) { 334 printf("MRC/MCR\n"); 335 disassemble(fault_pc); 336 } else if ((fault_instruction & ~INSN_COND_MASK) 337 != (KERNEL_BREAKPOINT & ~INSN_COND_MASK)) { 338 printf("Undefined instruction\n"); 339 disassemble(fault_pc); 340 } 341 342 splx(s); 343 #endif 344 345 if ((fault_code & FAULT_USER) == 0) { 346 #ifdef DDB 347 db_printf("Undefined instruction in kernel\n"); 348 kdb_trap(T_FAULT, frame); 349 #else 350 panic("undefined instruction in kernel"); 351 #endif 352 } 353 KSI_INIT_TRAP(&ksi); 354 ksi.ksi_signo = SIGILL; 355 ksi.ksi_code = ILL_ILLOPC; 356 ksi.ksi_addr = (u_int32_t *)fault_pc; 357 ksi.ksi_trap = fault_instruction; 358 trapsignal(l, &ksi); 359 } 360 361 if ((fault_code & FAULT_USER) == 0) 362 return; 363 364 #ifdef FAST_FPE 365 /* Optimised exit code */ 366 { 367 /* 368 * Check for reschedule request, at the moment there is only 369 * 1 ast so this code should always be run 370 */ 371 if (curcpu()->ci_want_resched) { 372 /* 373 * We are being preempted. 374 */ 375 preempt(); 376 } 377 378 /* Invoke MI userret code */ 379 mi_userret(l); 380 } 381 #else 382 userret(l); 383 #endif 384 } 385