1 /* $NetBSD: undefined.c,v 1.22 2003/11/29 22:21:29 bjh21 Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright (c) 2001 Ben Harris. 7 * Copyright (c) 1995 Mark Brinicombe. 8 * Copyright (c) 1995 Brini. 9 * All rights reserved. 10 * 11 * This code is derived from software written for Brini by Mark Brinicombe 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by Brini. 24 * 4. The name of the company nor the name of the author may be used to 25 * endorse or promote products derived from this software without specific 26 * prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 32 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * RiscBSD kernel project 41 * 42 * undefined.c 43 * 44 * Fault handler 45 * 46 * Created : 06/01/95 47 */ 48 49 #include "opt_ddb.h" 50 51 #include <sys/cdefs.h> 52 __FBSDID("$FreeBSD$"); 53 54 #include <sys/param.h> 55 #include <sys/malloc.h> 56 #include <sys/queue.h> 57 #include <sys/signal.h> 58 #include <sys/systm.h> 59 #include <sys/proc.h> 60 #include <sys/syslog.h> 61 #include <sys/vmmeter.h> 62 #include <sys/lock.h> 63 #include <sys/mutex.h> 64 #include <sys/signalvar.h> 65 #include <sys/ptrace.h> 66 #include <sys/vmmeter.h> 67 #ifdef KDB 68 #include <sys/kdb.h> 69 #endif 70 71 #include <vm/vm.h> 72 #include <vm/vm_extern.h> 73 74 #include <machine/armreg.h> 75 #include <machine/asm.h> 76 #include <machine/cpu.h> 77 #include <machine/frame.h> 78 #include <machine/undefined.h> 79 #include <machine/trap.h> 80 81 #include <machine/disassem.h> 82 83 #ifdef DDB 84 #include <ddb/db_output.h> 85 #endif 86 87 #ifdef KDB 88 #include <machine/db_machdep.h> 89 #endif 90 91 #define ARM_COPROC_INSN(insn) (((insn) & (1 << 27)) != 0) 92 #define ARM_VFP_INSN(insn) ((((insn) & 0xfe000000) == 0xf2000000) || \ 93 (((insn) & 0xff100000) == 0xf4000000)) 94 #define ARM_COPROC(insn) (((insn) >> 8) & 0xf) 95 96 #define THUMB_32BIT_INSN(insn) ((((insn) & 0xe000) == 0xe000) && \ 97 (((insn) & 0x1800) != 0)) 98 /* 99 * Coprocessor, Advanced SIMD, and 100 * Floating-point instructions on page A6-251 101 * OP1 == 01 OR 11 102 * OP2 == 1xxxxxx 103 */ 104 #define THUMB_COPROC_INSN(insn) (((insn) & (3 << 26)) == (3 << 26)) 105 /* 106 * Advanced SIMD element or structure 107 * load/store instructions on page A7-275 108 * OP1 == 11 109 * OP2 == 001xxx0 110 */ 111 #define THUMB_VFP_INSN(insn) (((insn) & (0x1F1 << 20)) == (0x190 << 20)) 112 #define THUMB_COPROC(insn) (((insn) >> 8) & 0xf) 113 114 #define COPROC_VFP 10 115 116 static int gdb_trapper(u_int, u_int, struct trapframe *, int); 117 118 LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS]; 119 120 void * 121 install_coproc_handler(int coproc, undef_handler_t handler) 122 { 123 struct undefined_handler *uh; 124 125 KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc")); 126 KASSERT(handler != NULL, ("handler is NULL")); /* Used to be legal. */ 127 128 /* XXX: M_TEMP??? */ 129 uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK); 130 uh->uh_handler = handler; 131 install_coproc_handler_static(coproc, uh); 132 return uh; 133 } 134 135 void 136 install_coproc_handler_static(int coproc, struct undefined_handler *uh) 137 { 138 139 LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link); 140 } 141 142 void 143 remove_coproc_handler(void *cookie) 144 { 145 struct undefined_handler *uh = cookie; 146 147 LIST_REMOVE(uh, uh_link); 148 free(uh, M_TEMP); 149 } 150 151 static int 152 gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code) 153 { 154 struct thread *td; 155 ksiginfo_t ksi; 156 int error; 157 158 td = (curthread == NULL) ? &thread0 : curthread; 159 160 if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) { 161 if (code == FAULT_USER) { 162 ksiginfo_init_trap(&ksi); 163 ksi.ksi_signo = SIGTRAP; 164 ksi.ksi_code = TRAP_BRKPT; 165 ksi.ksi_addr = (u_int32_t *)addr; 166 trapsignal(td, &ksi); 167 return 0; 168 } 169 #if 0 170 #ifdef KGDB 171 return !kgdb_trap(T_BREAKPOINT, frame); 172 #endif 173 #endif 174 } 175 176 if (code == FAULT_USER) { 177 /* TODO: No support for ptrace from Thumb-2 */ 178 if ((frame->tf_spsr & PSR_T) == 0 && 179 insn == PTRACE_BREAKPOINT) { 180 PROC_LOCK(td->td_proc); 181 _PHOLD(td->td_proc); 182 error = ptrace_clear_single_step(td); 183 _PRELE(td->td_proc); 184 PROC_UNLOCK(td->td_proc); 185 if (error == 0) { 186 ksiginfo_init_trap(&ksi); 187 ksi.ksi_signo = SIGTRAP; 188 ksi.ksi_code = TRAP_TRACE; 189 ksi.ksi_addr = (u_int32_t *)addr; 190 trapsignal(td, &ksi); 191 return (0); 192 } 193 } 194 } 195 196 return 1; 197 } 198 199 static struct undefined_handler gdb_uh; 200 201 void 202 undefined_init(void) 203 { 204 int loop; 205 206 /* Not actually necessary -- the initialiser is just NULL */ 207 for (loop = 0; loop < MAX_COPROCS; ++loop) 208 LIST_INIT(&undefined_handlers[loop]); 209 210 /* Install handler for GDB breakpoints */ 211 gdb_uh.uh_handler = gdb_trapper; 212 install_coproc_handler_static(0, &gdb_uh); 213 } 214 215 void 216 undefinedinstruction(struct trapframe *frame) 217 { 218 struct thread *td; 219 u_int fault_pc; 220 int fault_instruction; 221 int fault_code; 222 int coprocessor; 223 struct undefined_handler *uh; 224 #ifdef VERBOSE_ARM32 225 int s; 226 #endif 227 ksiginfo_t ksi; 228 229 /* Enable interrupts if they were enabled before the exception. */ 230 if (__predict_true(frame->tf_spsr & PSR_I) == 0) 231 enable_interrupts(PSR_I); 232 if (__predict_true(frame->tf_spsr & PSR_F) == 0) 233 enable_interrupts(PSR_F); 234 235 VM_CNT_INC(v_trap); 236 237 fault_pc = frame->tf_pc; 238 239 /* 240 * Get the current thread/proc structure or thread0/proc0 if there is 241 * none. 242 */ 243 td = curthread == NULL ? &thread0 : curthread; 244 245 coprocessor = 0; 246 if ((frame->tf_spsr & PSR_T) == 0) { 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_init_trap(&ksi); 253 ksi.ksi_signo = SIGILL; 254 ksi.ksi_code = ILL_ILLADR; 255 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc; 256 trapsignal(td, &ksi); 257 userret(td, frame); 258 return; 259 } 260 261 /* 262 * Should use fuword() here .. but in the interests of 263 * squeezing every bit of speed we will just use ReadWord(). 264 * We know the instruction can be read as was just executed 265 * so this will never fail unless the kernel is screwed up 266 * in which case it does not really matter does it ? 267 */ 268 269 fault_instruction = *(u_int32_t *)fault_pc; 270 271 /* Check for coprocessor instruction */ 272 273 /* 274 * According to the datasheets you only need to look at bit 275 * 27 of the instruction to tell the difference between and 276 * undefined instruction and a coprocessor instruction 277 * following an undefined instruction trap. 278 */ 279 280 if (ARM_COPROC_INSN(fault_instruction)) 281 coprocessor = ARM_COPROC(fault_instruction); 282 else { /* check for special instructions */ 283 if (ARM_VFP_INSN(fault_instruction)) 284 coprocessor = COPROC_VFP; /* vfp / simd */ 285 } 286 } else { 287 #if __ARM_ARCH >= 7 288 fault_instruction = *(uint16_t *)fault_pc; 289 if (THUMB_32BIT_INSN(fault_instruction)) { 290 fault_instruction <<= 16; 291 fault_instruction |= *(uint16_t *)(fault_pc + 2); 292 293 /* Coprocessor, Advanced SIMD and Floating-point */ 294 if (THUMB_COPROC_INSN(fault_instruction)) 295 coprocessor = THUMB_COPROC(fault_instruction); 296 else { 297 /* Advanced SIMD load/store */ 298 if (THUMB_VFP_INSN(fault_instruction)) 299 coprocessor = COPROC_VFP; /* SIMD */ 300 } 301 } 302 #else 303 /* 304 * No support for Thumb-2 on this cpu 305 */ 306 ksiginfo_init_trap(&ksi); 307 ksi.ksi_signo = SIGILL; 308 ksi.ksi_code = ILL_ILLADR; 309 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc; 310 trapsignal(td, &ksi); 311 userret(td, frame); 312 return; 313 #endif 314 } 315 316 if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) { 317 /* 318 * Modify the fault_code to reflect the USR/SVC state at 319 * time of fault. 320 */ 321 fault_code = FAULT_USER; 322 td->td_frame = frame; 323 } else 324 fault_code = 0; 325 326 /* OK this is were we do something about the instruction. */ 327 LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link) 328 if (uh->uh_handler(fault_pc, fault_instruction, frame, 329 fault_code) == 0) 330 break; 331 332 if (uh == NULL && (fault_code & FAULT_USER)) { 333 /* Fault has not been handled */ 334 ksiginfo_init_trap(&ksi); 335 ksi.ksi_signo = SIGILL; 336 ksi.ksi_code = ILL_ILLOPC; 337 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc; 338 trapsignal(td, &ksi); 339 } 340 341 if ((fault_code & FAULT_USER) == 0) { 342 if (fault_instruction == KERNEL_BREAKPOINT) { 343 #ifdef KDB 344 kdb_trap(T_BREAKPOINT, 0, frame); 345 #else 346 printf("No debugger in kernel.\n"); 347 #endif 348 return; 349 } 350 else 351 panic("Undefined instruction in kernel (0x%08x).\n", 352 fault_instruction); 353 } 354 355 userret(td, frame); 356 } 357