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