1 /* $NetBSD: fault.c,v 1.101 2014/08/13 21:41:32 matt Exp $ */ 2 3 /* 4 * Copyright 2003 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Steve C. Woodford for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 /* 38 * Copyright (c) 1994-1997 Mark Brinicombe. 39 * Copyright (c) 1994 Brini. 40 * All rights reserved. 41 * 42 * This code is derived from software written for Brini by Mark Brinicombe 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the above copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * This product includes software developed by Brini. 55 * 4. The name of the company nor the name of the author may be used to 56 * endorse or promote products derived from this software without specific 57 * prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED 60 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 61 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 62 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 63 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 64 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 65 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * RiscBSD kernel project 72 * 73 * fault.c 74 * 75 * Fault handlers 76 * 77 * Created : 28/11/94 78 */ 79 80 #include "opt_ddb.h" 81 #include "opt_kgdb.h" 82 83 #include <sys/types.h> 84 __KERNEL_RCSID(0, "$NetBSD: fault.c,v 1.101 2014/08/13 21:41:32 matt Exp $"); 85 86 #include <sys/param.h> 87 #include <sys/systm.h> 88 #include <sys/proc.h> 89 #include <sys/kernel.h> 90 #include <sys/kauth.h> 91 #include <sys/cpu.h> 92 #include <sys/intr.h> 93 94 #include <uvm/uvm_extern.h> 95 #include <uvm/uvm_stat.h> 96 #ifdef UVMHIST 97 #include <uvm/uvm.h> 98 #endif 99 100 #include <arm/locore.h> 101 102 #include <arm/arm32/katelib.h> 103 104 #include <machine/pcb.h> 105 #if defined(DDB) || defined(KGDB) 106 #include <machine/db_machdep.h> 107 #ifdef KGDB 108 #include <sys/kgdb.h> 109 #endif 110 #if !defined(DDB) 111 #define kdb_trap kgdb_trap 112 #endif 113 #endif 114 115 #include <arch/arm/arm/disassem.h> 116 #include <arm/arm32/machdep.h> 117 118 extern char fusubailout[]; 119 120 #ifdef DEBUG 121 int last_fault_code; /* For the benefit of pmap_fault_fixup() */ 122 #endif 123 124 #if defined(CPU_ARM3) || defined(CPU_ARM6) || \ 125 defined(CPU_ARM7) || defined(CPU_ARM7TDMI) 126 /* These CPUs may need data/prefetch abort fixups */ 127 #define CPU_ABORT_FIXUP_REQUIRED 128 #endif 129 130 struct data_abort { 131 int (*func)(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *); 132 const char *desc; 133 }; 134 135 static int dab_fatal(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *); 136 static int dab_align(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *); 137 static int dab_buserr(trapframe_t *, u_int, u_int, struct lwp *, ksiginfo_t *); 138 139 static const struct data_abort data_aborts[] = { 140 {dab_fatal, "Vector Exception"}, 141 {dab_align, "Alignment Fault 1"}, 142 {dab_fatal, "Terminal Exception"}, 143 {dab_align, "Alignment Fault 3"}, 144 {dab_buserr, "External Linefetch Abort (S)"}, 145 {NULL, "Translation Fault (S)"}, 146 {dab_buserr, "External Linefetch Abort (P)"}, 147 {NULL, "Translation Fault (P)"}, 148 {dab_buserr, "External Non-Linefetch Abort (S)"}, 149 {NULL, "Domain Fault (S)"}, 150 {dab_buserr, "External Non-Linefetch Abort (P)"}, 151 {NULL, "Domain Fault (P)"}, 152 {dab_buserr, "External Translation Abort (L1)"}, 153 {NULL, "Permission Fault (S)"}, 154 {dab_buserr, "External Translation Abort (L2)"}, 155 {NULL, "Permission Fault (P)"} 156 }; 157 158 /* Determine if 'x' is a permission fault */ 159 #define IS_PERMISSION_FAULT(x) \ 160 (((1 << ((x) & FAULT_TYPE_MASK)) & \ 161 ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0) 162 163 #if 0 164 /* maybe one day we'll do emulations */ 165 #define TRAPSIGNAL(l,k) (*(l)->l_proc->p_emul->e_trapsignal)((l), (k)) 166 #else 167 #define TRAPSIGNAL(l,k) trapsignal((l), (k)) 168 #endif 169 170 static inline void 171 call_trapsignal(struct lwp *l, const struct trapframe *tf, ksiginfo_t *ksi) 172 { 173 if (l->l_proc->p_pid == 1 || cpu_printfataltraps) { 174 printf("%d.%d(%s): trap: signo=%d code=%d addr=%p trap=%#x\n", 175 l->l_proc->p_pid, l->l_lid, l->l_proc->p_comm, 176 ksi->ksi_signo, ksi->ksi_code, ksi->ksi_addr, 177 ksi->ksi_trap); 178 printf("r0=%08x r1=%08x r2=%08x r3=%08x\n", 179 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3); 180 printf("r4=%08x r5=%08x r6=%08x r7=%08x\n", 181 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7); 182 printf("r8=%08x r9=%08x rA=%08x rB=%08x\n", 183 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11); 184 printf("ip=%08x sp=%08x lr=%08x pc=%08x spsr=%08x\n", 185 tf->tf_r12, tf->tf_usr_sp, tf->tf_usr_lr, tf->tf_pc, 186 tf->tf_spsr); 187 } 188 189 TRAPSIGNAL(l, ksi); 190 } 191 192 static inline int 193 data_abort_fixup(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l) 194 { 195 #ifdef CPU_ABORT_FIXUP_REQUIRED 196 int error; 197 198 /* Call the CPU specific data abort fixup routine */ 199 error = cpu_dataabt_fixup(tf); 200 if (__predict_true(error != ABORT_FIXUP_FAILED)) 201 return (error); 202 203 /* 204 * Oops, couldn't fix up the instruction 205 */ 206 printf("%s: fixup for %s mode data abort failed.\n", __func__, 207 TRAP_USERMODE(tf) ? "user" : "kernel"); 208 #ifdef THUMB_CODE 209 if (tf->tf_spsr & PSR_T_bit) { 210 printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ", 211 tf->tf_pc, *((uint16 *)(tf->tf_pc & ~1)), 212 *((uint16 *)((tf->tf_pc + 2) & ~1))); 213 } 214 else 215 #endif 216 { 217 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc, 218 *((u_int *)tf->tf_pc)); 219 } 220 disassemble(tf->tf_pc); 221 222 /* Die now if this happened in kernel mode */ 223 if (!TRAP_USERMODE(tf)) 224 dab_fatal(tf, fsr, far, l, NULL); 225 226 return (error); 227 #else 228 return (ABORT_FIXUP_OK); 229 #endif /* CPU_ABORT_FIXUP_REQUIRED */ 230 } 231 232 void 233 data_abort_handler(trapframe_t *tf) 234 { 235 struct vm_map *map; 236 struct lwp * const l = curlwp; 237 struct cpu_info * const ci = curcpu(); 238 u_int far, fsr; 239 vm_prot_t ftype; 240 void *onfault; 241 vaddr_t va; 242 int error; 243 ksiginfo_t ksi; 244 245 UVMHIST_FUNC(__func__); 246 UVMHIST_CALLED(maphist); 247 248 /* Grab FAR/FSR before enabling interrupts */ 249 far = cpu_faultaddress(); 250 fsr = cpu_faultstatus(); 251 252 /* Update vmmeter statistics */ 253 ci->ci_data.cpu_ntrap++; 254 255 /* Re-enable interrupts if they were enabled previously */ 256 KASSERT(!TRAP_USERMODE(tf) || VALID_R15_PSR(tf->tf_pc, tf->tf_spsr)); 257 #ifdef __NO_FIQ 258 if (__predict_true((tf->tf_spsr & I32_bit) != I32_bit)) 259 restore_interrupts(tf->tf_spsr & IF32_bits); 260 #else 261 if (__predict_true((tf->tf_spsr & IF32_bits) != IF32_bits)) 262 restore_interrupts(tf->tf_spsr & IF32_bits); 263 #endif 264 265 /* Get the current lwp structure */ 266 267 UVMHIST_LOG(maphist, " (l=%#x, far=%#x, fsr=%#x", 268 l, far, fsr, 0); 269 UVMHIST_LOG(maphist, " tf=%#x, pc=%#x)", 270 tf, tf->tf_pc, 0, 0); 271 272 /* Data abort came from user mode? */ 273 bool user = (TRAP_USERMODE(tf) != 0); 274 if (user) 275 LWP_CACHE_CREDS(l, l->l_proc); 276 277 /* Grab the current pcb */ 278 struct pcb * const pcb = lwp_getpcb(l); 279 280 curcpu()->ci_abt_evs[fsr & FAULT_TYPE_MASK].ev_count++; 281 282 /* Invoke the appropriate handler, if necessary */ 283 if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) { 284 #ifdef DIAGNOSTIC 285 printf("%s: data_aborts fsr=0x%x far=0x%x\n", 286 __func__, fsr, far); 287 #endif 288 if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far, 289 l, &ksi)) 290 goto do_trapsignal; 291 goto out; 292 } 293 294 /* 295 * At this point, we're dealing with one of the following data aborts: 296 * 297 * FAULT_TRANS_S - Translation -- Section 298 * FAULT_TRANS_P - Translation -- Page 299 * FAULT_DOMAIN_S - Domain -- Section 300 * FAULT_DOMAIN_P - Domain -- Page 301 * FAULT_PERM_S - Permission -- Section 302 * FAULT_PERM_P - Permission -- Page 303 * 304 * These are the main virtual memory-related faults signalled by 305 * the MMU. 306 */ 307 308 /* fusubailout is used by [fs]uswintr to avoid page faulting */ 309 if (__predict_false(pcb->pcb_onfault == fusubailout)) { 310 tf->tf_r0 = EFAULT; 311 tf->tf_pc = (intptr_t) pcb->pcb_onfault; 312 return; 313 } 314 315 if (user) { 316 lwp_settrapframe(l, tf); 317 } 318 319 /* 320 * Make sure the Program Counter is sane. We could fall foul of 321 * someone executing Thumb code, in which case the PC might not 322 * be word-aligned. This would cause a kernel alignment fault 323 * further down if we have to decode the current instruction. 324 */ 325 #ifdef THUMB_CODE 326 /* 327 * XXX: It would be nice to be able to support Thumb in the kernel 328 * at some point. 329 */ 330 if (__predict_false(!user && (tf->tf_pc & 3) != 0)) { 331 printf("\n%s: Misaligned Kernel-mode Program Counter\n", 332 __func__); 333 dab_fatal(tf, fsr, far, l, NULL); 334 } 335 #else 336 if (__predict_false((tf->tf_pc & 3) != 0)) { 337 if (user) { 338 /* 339 * Give the user an illegal instruction signal. 340 */ 341 /* Deliver a SIGILL to the process */ 342 KSI_INIT_TRAP(&ksi); 343 ksi.ksi_signo = SIGILL; 344 ksi.ksi_code = ILL_ILLOPC; 345 ksi.ksi_addr = (uint32_t *)(intptr_t) far; 346 ksi.ksi_trap = fsr; 347 goto do_trapsignal; 348 } 349 350 /* 351 * The kernel never executes Thumb code. 352 */ 353 printf("\n%s: Misaligned Kernel-mode Program Counter\n", 354 __func__); 355 dab_fatal(tf, fsr, far, l, NULL); 356 } 357 #endif 358 359 /* See if the CPU state needs to be fixed up */ 360 switch (data_abort_fixup(tf, fsr, far, l)) { 361 case ABORT_FIXUP_RETURN: 362 return; 363 case ABORT_FIXUP_FAILED: 364 /* Deliver a SIGILL to the process */ 365 KSI_INIT_TRAP(&ksi); 366 ksi.ksi_signo = SIGILL; 367 ksi.ksi_code = ILL_ILLOPC; 368 ksi.ksi_addr = (uint32_t *)(intptr_t) far; 369 ksi.ksi_trap = fsr; 370 goto do_trapsignal; 371 default: 372 break; 373 } 374 375 va = trunc_page((vaddr_t)far); 376 377 /* 378 * It is only a kernel address space fault iff: 379 * 1. user == 0 and 380 * 2. pcb_onfault not set or 381 * 3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction. 382 */ 383 if (!user && (va >= VM_MIN_KERNEL_ADDRESS || 384 (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) && 385 __predict_true((pcb->pcb_onfault == NULL || 386 (read_insn(tf->tf_pc, false) & 0x05200000) != 0x04200000))) { 387 map = kernel_map; 388 389 /* Was the fault due to the FPE/IPKDB ? */ 390 if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) { 391 KSI_INIT_TRAP(&ksi); 392 ksi.ksi_signo = SIGSEGV; 393 ksi.ksi_code = SEGV_ACCERR; 394 ksi.ksi_addr = (uint32_t *)(intptr_t) far; 395 ksi.ksi_trap = fsr; 396 397 /* 398 * Force exit via userret() 399 * This is necessary as the FPE is an extension to 400 * userland that actually runs in a priveledged mode 401 * but uses USR mode permissions for its accesses. 402 */ 403 user = true; 404 goto do_trapsignal; 405 } 406 } else { 407 map = &l->l_proc->p_vmspace->vm_map; 408 } 409 410 /* 411 * We need to know whether the page should be mapped as R or R/W. 412 * Before ARMv6, the MMU did not give us the info as to whether the 413 * fault was caused by a read or a write. 414 * 415 * However, we know that a permission fault can only be the result of 416 * a write to a read-only location, so we can deal with those quickly. 417 * 418 * Otherwise we need to disassemble the instruction responsible to 419 * determine if it was a write. 420 */ 421 if (CPU_IS_ARMV6_P() || CPU_IS_ARMV7_P()) { 422 ftype = (fsr & FAULT_WRITE) ? VM_PROT_WRITE : VM_PROT_READ; 423 } else if (IS_PERMISSION_FAULT(fsr)) { 424 ftype = VM_PROT_WRITE; 425 } else { 426 #ifdef THUMB_CODE 427 /* Fast track the ARM case. */ 428 if (__predict_false(tf->tf_spsr & PSR_T_bit)) { 429 u_int insn = read_thumb_insn(tf->tf_pc, user); 430 u_int insn_f8 = insn & 0xf800; 431 u_int insn_fe = insn & 0xfe00; 432 433 if (insn_f8 == 0x6000 || /* STR(1) */ 434 insn_f8 == 0x7000 || /* STRB(1) */ 435 insn_f8 == 0x8000 || /* STRH(1) */ 436 insn_f8 == 0x9000 || /* STR(3) */ 437 insn_f8 == 0xc000 || /* STM */ 438 insn_fe == 0x5000 || /* STR(2) */ 439 insn_fe == 0x5200 || /* STRH(2) */ 440 insn_fe == 0x5400) /* STRB(2) */ 441 ftype = VM_PROT_WRITE; 442 else 443 ftype = VM_PROT_READ; 444 } 445 else 446 #endif 447 { 448 u_int insn = read_insn(tf->tf_pc, user); 449 450 if (((insn & 0x0c100000) == 0x04000000) || /* STR[B] */ 451 ((insn & 0x0e1000b0) == 0x000000b0) || /* STR[HD]*/ 452 ((insn & 0x0a100000) == 0x08000000) || /* STM/CDT*/ 453 ((insn & 0x0f9000f0) == 0x01800090)) /* STREX[BDH] */ 454 ftype = VM_PROT_WRITE; 455 else if ((insn & 0x0fb00ff0) == 0x01000090)/* SWP */ 456 ftype = VM_PROT_READ | VM_PROT_WRITE; 457 else 458 ftype = VM_PROT_READ; 459 } 460 } 461 462 /* 463 * See if the fault is as a result of ref/mod emulation, 464 * or domain mismatch. 465 */ 466 #ifdef DEBUG 467 last_fault_code = fsr; 468 #endif 469 if (pmap_fault_fixup(map->pmap, va, ftype, user)) { 470 UVMHIST_LOG(maphist, " <- ref/mod emul", 0, 0, 0, 0); 471 goto out; 472 } 473 474 if (__predict_false(curcpu()->ci_intr_depth > 0)) { 475 if (pcb->pcb_onfault) { 476 tf->tf_r0 = EINVAL; 477 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 478 return; 479 } 480 printf("\nNon-emulated page fault with intr_depth > 0\n"); 481 dab_fatal(tf, fsr, far, l, NULL); 482 } 483 484 onfault = pcb->pcb_onfault; 485 pcb->pcb_onfault = NULL; 486 error = uvm_fault(map, va, ftype); 487 pcb->pcb_onfault = onfault; 488 489 if (__predict_true(error == 0)) { 490 if (user) 491 uvm_grow(l->l_proc, va); /* Record any stack growth */ 492 else 493 ucas_ras_check(tf); 494 UVMHIST_LOG(maphist, " <- uvm", 0, 0, 0, 0); 495 goto out; 496 } 497 498 if (user == 0) { 499 if (pcb->pcb_onfault) { 500 tf->tf_r0 = error; 501 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 502 return; 503 } 504 505 printf("\nuvm_fault(%p, %lx, %x) -> %x\n", map, va, ftype, 506 error); 507 dab_fatal(tf, fsr, far, l, NULL); 508 } 509 510 KSI_INIT_TRAP(&ksi); 511 512 if (error == ENOMEM) { 513 printf("UVM: pid %d (%s), uid %d killed: " 514 "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm, 515 l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1); 516 ksi.ksi_signo = SIGKILL; 517 } else 518 ksi.ksi_signo = SIGSEGV; 519 520 ksi.ksi_code = (error == EACCES) ? SEGV_ACCERR : SEGV_MAPERR; 521 ksi.ksi_addr = (uint32_t *)(intptr_t) far; 522 ksi.ksi_trap = fsr; 523 UVMHIST_LOG(maphist, " <- error (%d)", error, 0, 0, 0); 524 525 do_trapsignal: 526 call_trapsignal(l, tf, &ksi); 527 out: 528 /* If returning to user mode, make sure to invoke userret() */ 529 if (user) 530 userret(l); 531 } 532 533 /* 534 * dab_fatal() handles the following data aborts: 535 * 536 * FAULT_WRTBUF_0 - Vector Exception 537 * FAULT_WRTBUF_1 - Terminal Exception 538 * 539 * We should never see these on a properly functioning system. 540 * 541 * This function is also called by the other handlers if they 542 * detect a fatal problem. 543 * 544 * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort. 545 */ 546 static int 547 dab_fatal(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi) 548 { 549 const char * const mode = TRAP_USERMODE(tf) ? "user" : "kernel"; 550 551 if (l != NULL) { 552 printf("Fatal %s mode data abort: '%s'\n", mode, 553 data_aborts[fsr & FAULT_TYPE_MASK].desc); 554 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr); 555 if ((fsr & FAULT_IMPRECISE) == 0) 556 printf("%08x, ", far); 557 else 558 printf("Invalid, "); 559 printf("spsr=%08x\n", tf->tf_spsr); 560 } else { 561 printf("Fatal %s mode prefetch abort at 0x%08x\n", 562 mode, tf->tf_pc); 563 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr); 564 } 565 566 printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n", 567 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3); 568 printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n", 569 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7); 570 printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n", 571 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11); 572 printf("r12=%08x, ", tf->tf_r12); 573 574 if (TRAP_USERMODE(tf)) 575 printf("usp=%08x, ulr=%08x", 576 tf->tf_usr_sp, tf->tf_usr_lr); 577 else 578 printf("ssp=%08x, slr=%08x", 579 tf->tf_svc_sp, tf->tf_svc_lr); 580 printf(", pc =%08x\n\n", tf->tf_pc); 581 582 #if defined(DDB) || defined(KGDB) 583 kdb_trap(T_FAULT, tf); 584 #endif 585 panic("Fatal abort"); 586 /*NOTREACHED*/ 587 } 588 589 /* 590 * dab_align() handles the following data aborts: 591 * 592 * FAULT_ALIGN_0 - Alignment fault 593 * FAULT_ALIGN_0 - Alignment fault 594 * 595 * These faults are fatal if they happen in kernel mode. Otherwise, we 596 * deliver a bus error to the process. 597 */ 598 static int 599 dab_align(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi) 600 { 601 /* Alignment faults are always fatal if they occur in kernel mode */ 602 if (!TRAP_USERMODE(tf)) 603 dab_fatal(tf, fsr, far, l, NULL); 604 605 /* pcb_onfault *must* be NULL at this point */ 606 KDASSERT(((struct pcb *)lwp_getpcb(l))->pcb_onfault == NULL); 607 608 /* See if the CPU state needs to be fixed up */ 609 (void) data_abort_fixup(tf, fsr, far, l); 610 611 /* Deliver a bus error signal to the process */ 612 KSI_INIT_TRAP(ksi); 613 ksi->ksi_signo = SIGBUS; 614 ksi->ksi_code = BUS_ADRALN; 615 ksi->ksi_addr = (uint32_t *)(intptr_t)far; 616 ksi->ksi_trap = fsr; 617 618 lwp_settrapframe(l, tf); 619 620 return (1); 621 } 622 623 /* 624 * dab_buserr() handles the following data aborts: 625 * 626 * FAULT_BUSERR_0 - External Abort on Linefetch -- Section 627 * FAULT_BUSERR_1 - External Abort on Linefetch -- Page 628 * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section 629 * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page 630 * FAULT_BUSTRNL1 - External abort on Translation -- Level 1 631 * FAULT_BUSTRNL2 - External abort on Translation -- Level 2 632 * 633 * If pcb_onfault is set, flag the fault and return to the handler. 634 * If the fault occurred in user mode, give the process a SIGBUS. 635 * 636 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2 637 * can be flagged as imprecise in the FSR. This causes a real headache 638 * since some of the machine state is lost. In this case, tf->tf_pc 639 * may not actually point to the offending instruction. In fact, if 640 * we've taken a double abort fault, it generally points somewhere near 641 * the top of "data_abort_entry" in exception.S. 642 * 643 * In all other cases, these data aborts are considered fatal. 644 */ 645 static int 646 dab_buserr(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, 647 ksiginfo_t *ksi) 648 { 649 struct pcb *pcb = lwp_getpcb(l); 650 651 #ifdef __XSCALE__ 652 if ((fsr & FAULT_IMPRECISE) != 0 && 653 (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) { 654 /* 655 * Oops, an imprecise, double abort fault. We've lost the 656 * r14_abt/spsr_abt values corresponding to the original 657 * abort, and the spsr saved in the trapframe indicates 658 * ABT mode. 659 */ 660 tf->tf_spsr &= ~PSR_MODE; 661 662 /* 663 * We use a simple heuristic to determine if the double abort 664 * happened as a result of a kernel or user mode access. 665 * If the current trapframe is at the top of the kernel stack, 666 * the fault _must_ have come from user mode. 667 */ 668 if (tf != ((trapframe_t *)pcb->pcb_ksp) - 1) { 669 /* 670 * Kernel mode. We're either about to die a 671 * spectacular death, or pcb_onfault will come 672 * to our rescue. Either way, the current value 673 * of tf->tf_pc is irrelevant. 674 */ 675 tf->tf_spsr |= PSR_SVC32_MODE; 676 if (pcb->pcb_onfault == NULL) 677 printf("\nKernel mode double abort!\n"); 678 } else { 679 /* 680 * User mode. We've lost the program counter at the 681 * time of the fault (not that it was accurate anyway; 682 * it's not called an imprecise fault for nothing). 683 * About all we can do is copy r14_usr to tf_pc and 684 * hope for the best. The process is about to get a 685 * SIGBUS, so it's probably history anyway. 686 */ 687 tf->tf_spsr |= PSR_USR32_MODE; 688 tf->tf_pc = tf->tf_usr_lr; 689 #ifdef THUMB_CODE 690 tf->tf_spsr &= ~PSR_T_bit; 691 if (tf->tf_usr_lr & 1) 692 tf->tf_spsr |= PSR_T_bit; 693 #endif 694 } 695 } 696 697 /* FAR is invalid for imprecise exceptions */ 698 if ((fsr & FAULT_IMPRECISE) != 0) 699 far = 0; 700 #endif /* __XSCALE__ */ 701 702 if (pcb->pcb_onfault) { 703 KDASSERT(TRAP_USERMODE(tf) == 0); 704 tf->tf_r0 = EFAULT; 705 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 706 return (0); 707 } 708 709 /* See if the CPU state needs to be fixed up */ 710 (void) data_abort_fixup(tf, fsr, far, l); 711 712 /* 713 * At this point, if the fault happened in kernel mode, we're toast 714 */ 715 if (!TRAP_USERMODE(tf)) 716 dab_fatal(tf, fsr, far, l, NULL); 717 718 /* Deliver a bus error signal to the process */ 719 KSI_INIT_TRAP(ksi); 720 ksi->ksi_signo = SIGBUS; 721 ksi->ksi_code = BUS_ADRERR; 722 ksi->ksi_addr = (uint32_t *)(intptr_t)far; 723 ksi->ksi_trap = fsr; 724 725 lwp_settrapframe(l, tf); 726 727 return (1); 728 } 729 730 static inline int 731 prefetch_abort_fixup(trapframe_t *tf) 732 { 733 #ifdef CPU_ABORT_FIXUP_REQUIRED 734 int error; 735 736 /* Call the CPU specific prefetch abort fixup routine */ 737 error = cpu_prefetchabt_fixup(tf); 738 if (__predict_true(error != ABORT_FIXUP_FAILED)) 739 return (error); 740 741 /* 742 * Oops, couldn't fix up the instruction 743 */ 744 printf("%s: fixup for %s mode prefetch abort failed.\n", __func__, 745 TRAP_USERMODE(tf) ? "user" : "kernel"); 746 #ifdef THUMB_CODE 747 if (tf->tf_spsr & PSR_T_bit) { 748 printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ", 749 tf->tf_pc, *((uint16 *)(tf->tf_pc & ~1)), 750 *((uint16 *)((tf->tf_pc + 2) & ~1))); 751 } 752 else 753 #endif 754 { 755 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc, 756 *((u_int *)tf->tf_pc)); 757 } 758 disassemble(tf->tf_pc); 759 760 /* Die now if this happened in kernel mode */ 761 if (!TRAP_USERMODE(tf)) 762 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL); 763 764 return (error); 765 #else 766 return (ABORT_FIXUP_OK); 767 #endif /* CPU_ABORT_FIXUP_REQUIRED */ 768 } 769 770 /* 771 * void prefetch_abort_handler(trapframe_t *tf) 772 * 773 * Abort handler called when instruction execution occurs at 774 * a non existent or restricted (access permissions) memory page. 775 * If the address is invalid and we were in SVC mode then panic as 776 * the kernel should never prefetch abort. 777 * If the address is invalid and the page is mapped then the user process 778 * does no have read permission so send it a signal. 779 * Otherwise fault the page in and try again. 780 */ 781 void 782 prefetch_abort_handler(trapframe_t *tf) 783 { 784 struct lwp *l; 785 struct pcb *pcb __diagused; 786 struct vm_map *map; 787 vaddr_t fault_pc, va; 788 ksiginfo_t ksi; 789 int error, user; 790 791 UVMHIST_FUNC(__func__); 792 UVMHIST_CALLED(maphist); 793 794 /* Update vmmeter statistics */ 795 curcpu()->ci_data.cpu_ntrap++; 796 797 l = curlwp; 798 pcb = lwp_getpcb(l); 799 800 if ((user = TRAP_USERMODE(tf)) != 0) 801 LWP_CACHE_CREDS(l, l->l_proc); 802 803 /* 804 * Enable IRQ's (disabled by the abort) This always comes 805 * from user mode so we know interrupts were not disabled. 806 * But we check anyway. 807 */ 808 KASSERT(!TRAP_USERMODE(tf) || VALID_R15_PSR(tf->tf_pc, tf->tf_spsr)); 809 #ifdef __NO_FIQ 810 if (__predict_true((tf->tf_spsr & I32_bit) != I32_bit)) 811 restore_interrupts(tf->tf_spsr & IF32_bits); 812 #else 813 if (__predict_true((tf->tf_spsr & IF32_bits) != IF32_bits)) 814 restore_interrupts(tf->tf_spsr & IF32_bits); 815 #endif 816 817 /* See if the CPU state needs to be fixed up */ 818 switch (prefetch_abort_fixup(tf)) { 819 case ABORT_FIXUP_RETURN: 820 KASSERT(!TRAP_USERMODE(tf) || VALID_R15_PSR(tf->tf_pc, tf->tf_spsr)); 821 return; 822 case ABORT_FIXUP_FAILED: 823 /* Deliver a SIGILL to the process */ 824 KSI_INIT_TRAP(&ksi); 825 ksi.ksi_signo = SIGILL; 826 ksi.ksi_code = ILL_ILLOPC; 827 ksi.ksi_addr = (uint32_t *)(intptr_t) tf->tf_pc; 828 lwp_settrapframe(l, tf); 829 goto do_trapsignal; 830 default: 831 break; 832 } 833 834 /* Prefetch aborts cannot happen in kernel mode */ 835 if (__predict_false(!user)) 836 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL); 837 838 /* Get fault address */ 839 fault_pc = tf->tf_pc; 840 lwp_settrapframe(l, tf); 841 UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, tf=0x%x)", 842 fault_pc, l, tf, 0); 843 844 /* Ok validate the address, can only execute in USER space */ 845 if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS || 846 (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) { 847 KSI_INIT_TRAP(&ksi); 848 ksi.ksi_signo = SIGSEGV; 849 ksi.ksi_code = SEGV_ACCERR; 850 ksi.ksi_addr = (uint32_t *)(intptr_t) fault_pc; 851 ksi.ksi_trap = fault_pc; 852 goto do_trapsignal; 853 } 854 855 map = &l->l_proc->p_vmspace->vm_map; 856 va = trunc_page(fault_pc); 857 858 /* 859 * See if the pmap can handle this fault on its own... 860 */ 861 #ifdef DEBUG 862 last_fault_code = -1; 863 #endif 864 if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ|VM_PROT_EXECUTE, 1)) { 865 UVMHIST_LOG (maphist, " <- emulated", 0, 0, 0, 0); 866 goto out; 867 } 868 869 #ifdef DIAGNOSTIC 870 if (__predict_false(curcpu()->ci_intr_depth > 0)) { 871 printf("\nNon-emulated prefetch abort with intr_depth > 0\n"); 872 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL); 873 } 874 #endif 875 876 KASSERT(pcb->pcb_onfault == NULL); 877 error = uvm_fault(map, va, VM_PROT_READ|VM_PROT_EXECUTE); 878 879 if (__predict_true(error == 0)) { 880 UVMHIST_LOG (maphist, " <- uvm", 0, 0, 0, 0); 881 goto out; 882 } 883 KSI_INIT_TRAP(&ksi); 884 885 UVMHIST_LOG (maphist, " <- fatal (%d)", error, 0, 0, 0); 886 887 if (error == ENOMEM) { 888 printf("UVM: pid %d (%s), uid %d killed: " 889 "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm, 890 l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1); 891 ksi.ksi_signo = SIGKILL; 892 } else 893 ksi.ksi_signo = SIGSEGV; 894 895 ksi.ksi_code = SEGV_MAPERR; 896 ksi.ksi_addr = (uint32_t *)(intptr_t) fault_pc; 897 ksi.ksi_trap = fault_pc; 898 899 do_trapsignal: 900 call_trapsignal(l, tf, &ksi); 901 902 out: 903 KASSERT(!TRAP_USERMODE(tf) || VALID_R15_PSR(tf->tf_pc, tf->tf_spsr)); 904 userret(l); 905 } 906 907 /* 908 * Tentatively read an 8, 16, or 32-bit value from 'addr'. 909 * If the read succeeds, the value is written to 'rptr' and zero is returned. 910 * Else, return EFAULT. 911 */ 912 int 913 badaddr_read(void *addr, size_t size, void *rptr) 914 { 915 extern int badaddr_read_1(const uint8_t *, uint8_t *); 916 extern int badaddr_read_2(const uint16_t *, uint16_t *); 917 extern int badaddr_read_4(const uint32_t *, uint32_t *); 918 union { 919 uint8_t v1; 920 uint16_t v2; 921 uint32_t v4; 922 } u; 923 int rv, s; 924 925 cpu_drain_writebuf(); 926 927 s = splhigh(); 928 929 /* Read from the test address. */ 930 switch (size) { 931 case sizeof(uint8_t): 932 rv = badaddr_read_1(addr, &u.v1); 933 if (rv == 0 && rptr) 934 *(uint8_t *) rptr = u.v1; 935 break; 936 937 case sizeof(uint16_t): 938 rv = badaddr_read_2(addr, &u.v2); 939 if (rv == 0 && rptr) 940 *(uint16_t *) rptr = u.v2; 941 break; 942 943 case sizeof(uint32_t): 944 rv = badaddr_read_4(addr, &u.v4); 945 if (rv == 0 && rptr) 946 *(uint32_t *) rptr = u.v4; 947 break; 948 949 default: 950 panic("%s: invalid size (%zu)", __func__, size); 951 } 952 953 splx(s); 954 955 /* Return EFAULT if the address was invalid, else zero */ 956 return (rv); 957 } 958