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