1 /* $NetBSD: undefined.c,v 1.51 2014/01/29 18:45:21 matt 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.51 2014/01/29 18:45:21 matt Exp $"); 58 59 #include <sys/kmem.h> 60 #include <sys/queue.h> 61 #include <sys/signal.h> 62 #include <sys/systm.h> 63 #include <sys/proc.h> 64 #include <sys/syslog.h> 65 #include <sys/vmmeter.h> 66 #include <sys/cpu.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 <arm/locore.h> 75 #include <arm/undefined.h> 76 77 #include <machine/pcb.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 LIST_HEAD(, undefined_handler) undefined_handlers[NUM_UNKNOWN_HANDLERS]; 94 95 96 void * 97 install_coproc_handler(int coproc, undef_handler_t handler) 98 { 99 struct undefined_handler *uh; 100 101 KASSERT(coproc >= 0 && coproc < NUM_UNKNOWN_HANDLERS); 102 KASSERT(handler != NULL); /* Used to be legal. */ 103 104 uh = kmem_alloc(sizeof(*uh), KM_SLEEP); 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 kmem_free(uh, sizeof(*uh)); 124 } 125 126 static struct evcnt cp15_ev = 127 EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, NULL, "cpu0", "undefined cp15 insn traps"); 128 EVCNT_ATTACH_STATIC(cp15_ev); 129 130 static int 131 cp15_trapper(u_int addr, u_int insn, struct trapframe *frame, int code) 132 { 133 struct lwp * const l = curlwp; 134 135 #if defined(THUMB_CODE) && !defined(CPU_ARMV7) 136 if (frame->tf_spsr & PSR_T_bit) 137 return 1; 138 #endif 139 if (code != FAULT_USER) 140 return 1; 141 142 /* 143 * Don't overwrite sp, pc, etc. 144 */ 145 const u_int regno = (insn >> 12) & 15; 146 if (regno > 12) 147 return 1; 148 149 /* 150 * Get a pointer to the register used in the instruction to be emulated. 151 */ 152 register_t * const regp = &frame->tf_r0 + regno; 153 154 /* 155 * Handle MRC p15, 0, <Rd>, c13, c0, 3 (Read User read-only thread id) 156 */ 157 if ((insn & 0xffff0fff) == 0xee1d0f70) { 158 *regp = (uintptr_t)l->l_private; 159 frame->tf_pc += INSN_SIZE; 160 cp15_ev.ev_count++; 161 return 0; 162 } 163 164 /* 165 * Handle {MRC,MCR} p15, 0, <Rd>, c13, c0, 2 (User read/write thread id) 166 */ 167 if ((insn & 0xffef0fff) == 0xee0d0f50) { 168 struct pcb * const pcb = lwp_getpcb(l); 169 if (insn & 0x00100000) 170 *regp = pcb->pcb_user_pid_rw; 171 else 172 pcb->pcb_user_pid_rw = *regp; 173 frame->tf_pc += INSN_SIZE; 174 cp15_ev.ev_count++; 175 return 0; 176 } 177 178 return 1; 179 } 180 181 static int 182 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code) 183 { 184 struct lwp * const l = curlwp; 185 186 #ifdef THUMB_CODE 187 if (frame->tf_spsr & PSR_T_bit) { 188 if (insn == GDB_THUMB_BREAKPOINT) 189 goto bkpt; 190 } 191 else 192 #endif 193 { 194 if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) { 195 #ifdef THUMB_CODE 196 bkpt: 197 #endif 198 if (code == FAULT_USER) { 199 ksiginfo_t ksi; 200 201 KSI_INIT_TRAP(&ksi); 202 ksi.ksi_signo = SIGTRAP; 203 ksi.ksi_code = TRAP_BRKPT; 204 ksi.ksi_addr = (uint32_t *)addr; 205 ksi.ksi_trap = 0; 206 trapsignal(l, &ksi); 207 return 0; 208 } 209 #ifdef KGDB 210 return !kgdb_trap(T_BREAKPOINT, frame); 211 #endif 212 } 213 } 214 return 1; 215 } 216 217 static struct undefined_handler cp15_uh; 218 static struct undefined_handler gdb_uh; 219 #ifdef THUMB_CODE 220 static struct undefined_handler gdb_uh_thumb; 221 #endif 222 223 void 224 undefined_init(void) 225 { 226 int loop; 227 228 /* Not actually necessary -- the initialiser is just NULL */ 229 for (loop = 0; loop < NUM_UNKNOWN_HANDLERS; ++loop) 230 LIST_INIT(&undefined_handlers[loop]); 231 232 /* Install handler for CP15 emulation */ 233 cp15_uh.uh_handler = cp15_trapper; 234 install_coproc_handler_static(SYSTEM_COPROC, &cp15_uh); 235 236 /* Install handler for GDB breakpoints */ 237 gdb_uh.uh_handler = gdb_trapper; 238 install_coproc_handler_static(CORE_UNKNOWN_HANDLER, &gdb_uh); 239 #ifdef THUMB_CODE 240 gdb_uh_thumb.uh_handler = gdb_trapper; 241 install_coproc_handler_static(THUMB_UNKNOWN_HANDLER, &gdb_uh_thumb); 242 #endif 243 } 244 245 static struct evcnt und_ev = 246 EVCNT_INITIALIZER(EVCNT_TYPE_TRAP, NULL, "cpu0", "undefined insn traps"); 247 EVCNT_ATTACH_STATIC(und_ev); 248 249 void 250 undefinedinstruction(trapframe_t *frame) 251 { 252 struct lwp *l; 253 u_int fault_pc; 254 int fault_instruction; 255 int fault_code; 256 int coprocessor; 257 int user; 258 struct undefined_handler *uh; 259 #ifdef VERBOSE_ARM32 260 int s; 261 #endif 262 263 und_ev.ev_count++; 264 265 /* Enable interrupts if they were enabled before the exception. */ 266 #ifdef acorn26 267 if ((frame->tf_r15 & R15_IRQ_DISABLE) == 0) 268 int_on(); 269 #else 270 restore_interrupts(frame->tf_spsr & IF32_bits); 271 #endif 272 273 #ifndef acorn26 274 #ifdef THUMB_CODE 275 if (frame->tf_spsr & PSR_T_bit) 276 frame->tf_pc -= THUMB_INSN_SIZE; 277 else 278 #endif 279 { 280 frame->tf_pc -= INSN_SIZE; 281 } 282 #endif 283 284 #ifdef __PROG26 285 fault_pc = frame->tf_r15 & R15_PC; 286 #else 287 fault_pc = frame->tf_pc; 288 #endif 289 290 /* Get the current lwp/proc structure or lwp0/proc0 if there is none. */ 291 l = curlwp; 292 293 #ifdef __PROG26 294 if ((frame->tf_r15 & R15_MODE) == R15_MODE_USR) { 295 #else 296 if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) { 297 #endif 298 user = 1; 299 LWP_CACHE_CREDS(l, l->l_proc); 300 } else 301 user = 0; 302 303 304 #ifdef THUMB_CODE 305 if (frame->tf_spsr & PSR_T_bit) { 306 fault_instruction = read_thumb_insn(fault_pc, user); 307 if (fault_instruction >= 0xe000) { 308 fault_instruction = (fault_instruction << 16) 309 | read_thumb_insn(fault_pc + 2, user); 310 } 311 } 312 else 313 #endif 314 { 315 /* 316 * Make sure the program counter is correctly aligned so we 317 * don't take an alignment fault trying to read the opcode. 318 */ 319 if (__predict_false((fault_pc & 3) != 0)) { 320 ksiginfo_t ksi; 321 /* Give the user an illegal instruction signal. */ 322 KSI_INIT_TRAP(&ksi); 323 ksi.ksi_signo = SIGILL; 324 ksi.ksi_code = ILL_ILLOPC; 325 ksi.ksi_addr = (uint32_t *)(intptr_t) fault_pc; 326 trapsignal(l, &ksi); 327 userret(l); 328 return; 329 } 330 /* 331 * Should use fuword() here .. but in the interests of 332 * squeezing every bit of speed we will just use 333 * ReadWord(). We know the instruction can be read 334 * as was just executed so this will never fail unless 335 * the kernel is screwed up in which case it does 336 * not really matter does it ? 337 */ 338 fault_instruction = read_insn(fault_pc, user); 339 } 340 341 /* Update vmmeter statistics */ 342 curcpu()->ci_data.cpu_ntrap++; 343 344 #ifdef THUMB_CODE 345 if ((frame->tf_spsr & PSR_T_bit) && !CPU_IS_ARMV7_P()) { 346 coprocessor = THUMB_UNKNOWN_HANDLER; 347 } 348 else 349 #endif 350 { 351 /* Check for coprocessor instruction */ 352 353 /* 354 * According to the datasheets you only need to look at 355 * bit 27 of the instruction to tell the difference 356 * between and undefined instruction and a coprocessor 357 * instruction following an undefined instruction trap. 358 * 359 * ARMv5 adds undefined instructions in the NV space, 360 * even when bit 27 is set. 361 */ 362 363 if ((fault_instruction & (1 << 27)) != 0 364 && (fault_instruction & 0xf0000000) != 0xf0000000) { 365 coprocessor = (fault_instruction >> 8) & 0x0f; 366 #ifdef THUMB_CODE 367 } else if ((frame->tf_spsr & PSR_T_bit) && !CPU_IS_ARMV7_P()) { 368 coprocessor = THUMB_UNKNOWN_HANDLER; 369 #endif 370 } else { 371 coprocessor = CORE_UNKNOWN_HANDLER; 372 } 373 } 374 375 if (user) { 376 /* 377 * Modify the fault_code to reflect the USR/SVC state at 378 * time of fault. 379 */ 380 fault_code = FAULT_USER; 381 lwp_settrapframe(l, frame); 382 } else 383 fault_code = 0; 384 385 /* OK this is were we do something about the instruction. */ 386 LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link) 387 if (uh->uh_handler(fault_pc, fault_instruction, frame, 388 fault_code) == 0) 389 break; 390 391 if (uh == NULL) { 392 /* Fault has not been handled */ 393 ksiginfo_t ksi; 394 395 #ifdef VERBOSE_ARM32 396 s = spltty(); 397 398 if ((fault_instruction & 0x0f000010) == 0x0e000000) { 399 printf("CDP\n"); 400 disassemble(fault_pc); 401 } else if ((fault_instruction & 0x0e000000) == 0x0c000000) { 402 printf("LDC/STC\n"); 403 disassemble(fault_pc); 404 } else if ((fault_instruction & 0x0f000010) == 0x0e000010) { 405 printf("MRC/MCR\n"); 406 disassemble(fault_pc); 407 } else if ((fault_instruction & ~INSN_COND_MASK) 408 != (KERNEL_BREAKPOINT & ~INSN_COND_MASK)) { 409 printf("Undefined instruction\n"); 410 disassemble(fault_pc); 411 } 412 413 splx(s); 414 #endif 415 416 if ((fault_code & FAULT_USER) == 0) { 417 #ifdef DDB 418 db_printf("Undefined instruction in kernel\n"); 419 kdb_trap(T_FAULT, frame); 420 #else 421 panic("undefined instruction in kernel"); 422 #endif 423 } 424 KSI_INIT_TRAP(&ksi); 425 ksi.ksi_signo = SIGILL; 426 ksi.ksi_code = ILL_ILLOPC; 427 ksi.ksi_addr = (uint32_t *)fault_pc; 428 ksi.ksi_trap = fault_instruction; 429 trapsignal(l, &ksi); 430 } 431 432 if ((fault_code & FAULT_USER) == 0) 433 return; 434 435 #ifdef FAST_FPE 436 /* Optimised exit code */ 437 { 438 /* 439 * Check for reschedule request, at the moment there is only 440 * 1 ast so this code should always be run 441 */ 442 if (curcpu()->ci_want_resched) { 443 /* 444 * We are being preempted. 445 */ 446 preempt(); 447 } 448 449 /* Invoke MI userret code */ 450 mi_userret(l); 451 } 452 #else 453 userret(l); 454 #endif 455 } 456