1 /* $NetBSD: fault.c,v 1.92 2014/01/11 17:32:20 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.92 2014/01/11 17:32:20 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, ksiginfo_t *ksi) 172 { 173 174 TRAPSIGNAL(l, ksi); 175 } 176 177 static inline int 178 data_abort_fixup(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l) 179 { 180 #ifdef CPU_ABORT_FIXUP_REQUIRED 181 int error; 182 183 /* Call the CPU specific data abort fixup routine */ 184 error = cpu_dataabt_fixup(tf); 185 if (__predict_true(error != ABORT_FIXUP_FAILED)) 186 return (error); 187 188 /* 189 * Oops, couldn't fix up the instruction 190 */ 191 printf("%s: fixup for %s mode data abort failed.\n", __func__, 192 TRAP_USERMODE(tf) ? "user" : "kernel"); 193 #ifdef THUMB_CODE 194 if (tf->tf_spsr & PSR_T_bit) { 195 printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ", 196 tf->tf_pc, *((uint16 *)(tf->tf_pc & ~1)), 197 *((uint16 *)((tf->tf_pc + 2) & ~1))); 198 } 199 else 200 #endif 201 { 202 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc, 203 *((u_int *)tf->tf_pc)); 204 } 205 disassemble(tf->tf_pc); 206 207 /* Die now if this happened in kernel mode */ 208 if (!TRAP_USERMODE(tf)) 209 dab_fatal(tf, fsr, far, l, NULL); 210 211 return (error); 212 #else 213 return (ABORT_FIXUP_OK); 214 #endif /* CPU_ABORT_FIXUP_REQUIRED */ 215 } 216 217 void 218 data_abort_handler(trapframe_t *tf) 219 { 220 struct vm_map *map; 221 struct lwp * const l = curlwp; 222 struct cpu_info * const ci = curcpu(); 223 u_int far, fsr; 224 vm_prot_t ftype; 225 void *onfault; 226 vaddr_t va; 227 int error; 228 ksiginfo_t ksi; 229 230 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist); 231 232 /* Grab FAR/FSR before enabling interrupts */ 233 far = cpu_faultaddress(); 234 fsr = cpu_faultstatus(); 235 236 /* Update vmmeter statistics */ 237 ci->ci_data.cpu_ntrap++; 238 239 /* Re-enable interrupts if they were enabled previously */ 240 KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0); 241 if (__predict_true((tf->tf_spsr & IF32_bits) != IF32_bits)) 242 restore_interrupts(tf->tf_spsr & IF32_bits); 243 244 /* Get the current lwp structure */ 245 246 UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, far=0x%x, fsr=0x%x)", 247 tf->tf_pc, l, far, fsr); 248 249 /* Data abort came from user mode? */ 250 bool user = (TRAP_USERMODE(tf) != 0); 251 if (user) 252 LWP_CACHE_CREDS(l, l->l_proc); 253 254 /* Grab the current pcb */ 255 struct pcb * const pcb = lwp_getpcb(l); 256 257 curcpu()->ci_abt_evs[fsr & FAULT_TYPE_MASK].ev_count++; 258 259 /* Invoke the appropriate handler, if necessary */ 260 if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) { 261 #ifdef DIAGNOSTIC 262 printf("%s: data_aborts fsr=0x%x far=0x%x\n", 263 __func__, fsr, far); 264 #endif 265 if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far, 266 l, &ksi)) 267 goto do_trapsignal; 268 goto out; 269 } 270 271 /* 272 * At this point, we're dealing with one of the following data aborts: 273 * 274 * FAULT_TRANS_S - Translation -- Section 275 * FAULT_TRANS_P - Translation -- Page 276 * FAULT_DOMAIN_S - Domain -- Section 277 * FAULT_DOMAIN_P - Domain -- Page 278 * FAULT_PERM_S - Permission -- Section 279 * FAULT_PERM_P - Permission -- Page 280 * 281 * These are the main virtual memory-related faults signalled by 282 * the MMU. 283 */ 284 285 /* fusubailout is used by [fs]uswintr to avoid page faulting */ 286 if (__predict_false(pcb->pcb_onfault == fusubailout)) { 287 tf->tf_r0 = EFAULT; 288 tf->tf_pc = (intptr_t) pcb->pcb_onfault; 289 return; 290 } 291 292 if (user) { 293 lwp_settrapframe(l, tf); 294 } 295 296 /* 297 * Make sure the Program Counter is sane. We could fall foul of 298 * someone executing Thumb code, in which case the PC might not 299 * be word-aligned. This would cause a kernel alignment fault 300 * further down if we have to decode the current instruction. 301 */ 302 #ifdef THUMB_CODE 303 /* 304 * XXX: It would be nice to be able to support Thumb in the kernel 305 * at some point. 306 */ 307 if (__predict_false(!user && (tf->tf_pc & 3) != 0)) { 308 printf("\n%s: Misaligned Kernel-mode Program Counter\n", 309 __func__); 310 dab_fatal(tf, fsr, far, l, NULL); 311 } 312 #else 313 if (__predict_false((tf->tf_pc & 3) != 0)) { 314 if (user) { 315 /* 316 * Give the user an illegal instruction signal. 317 */ 318 /* Deliver a SIGILL to the process */ 319 KSI_INIT_TRAP(&ksi); 320 ksi.ksi_signo = SIGILL; 321 ksi.ksi_code = ILL_ILLOPC; 322 ksi.ksi_addr = (uint32_t *)(intptr_t) far; 323 ksi.ksi_trap = fsr; 324 goto do_trapsignal; 325 } 326 327 /* 328 * The kernel never executes Thumb code. 329 */ 330 printf("\n%s: Misaligned Kernel-mode Program Counter\n", 331 __func__); 332 dab_fatal(tf, fsr, far, l, NULL); 333 } 334 #endif 335 336 /* See if the CPU state needs to be fixed up */ 337 switch (data_abort_fixup(tf, fsr, far, l)) { 338 case ABORT_FIXUP_RETURN: 339 return; 340 case ABORT_FIXUP_FAILED: 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 default: 349 break; 350 } 351 352 va = trunc_page((vaddr_t)far); 353 354 /* 355 * It is only a kernel address space fault iff: 356 * 1. user == 0 and 357 * 2. pcb_onfault not set or 358 * 3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction. 359 */ 360 if (!user && (va >= VM_MIN_KERNEL_ADDRESS || 361 (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) && 362 __predict_true((pcb->pcb_onfault == NULL || 363 (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) { 364 map = kernel_map; 365 366 /* Was the fault due to the FPE/IPKDB ? */ 367 if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) { 368 KSI_INIT_TRAP(&ksi); 369 ksi.ksi_signo = SIGSEGV; 370 ksi.ksi_code = SEGV_ACCERR; 371 ksi.ksi_addr = (uint32_t *)(intptr_t) far; 372 ksi.ksi_trap = fsr; 373 374 /* 375 * Force exit via userret() 376 * This is necessary as the FPE is an extension to 377 * userland that actually runs in a priveledged mode 378 * but uses USR mode permissions for its accesses. 379 */ 380 user = true; 381 goto do_trapsignal; 382 } 383 } else { 384 map = &l->l_proc->p_vmspace->vm_map; 385 } 386 387 /* 388 * We need to know whether the page should be mapped 389 * as R or R/W. The MMU does not give us the info as 390 * to whether the fault was caused by a read or a write. 391 * 392 * However, we know that a permission fault can only be 393 * the result of a write to a read-only location, so 394 * we can deal with those quickly. 395 * 396 * Otherwise we need to disassemble the instruction 397 * responsible to determine if it was a write. 398 */ 399 if (IS_PERMISSION_FAULT(fsr)) 400 ftype = VM_PROT_WRITE; 401 else { 402 #ifdef THUMB_CODE 403 /* Fast track the ARM case. */ 404 if (__predict_false(tf->tf_spsr & PSR_T_bit)) { 405 u_int insn = fusword((void *)(tf->tf_pc & ~1)); 406 u_int insn_f8 = insn & 0xf800; 407 u_int insn_fe = insn & 0xfe00; 408 409 if (insn_f8 == 0x6000 || /* STR(1) */ 410 insn_f8 == 0x7000 || /* STRB(1) */ 411 insn_f8 == 0x8000 || /* STRH(1) */ 412 insn_f8 == 0x9000 || /* STR(3) */ 413 insn_f8 == 0xc000 || /* STM */ 414 insn_fe == 0x5000 || /* STR(2) */ 415 insn_fe == 0x5200 || /* STRH(2) */ 416 insn_fe == 0x5400) /* STRB(2) */ 417 ftype = VM_PROT_WRITE; 418 else 419 ftype = VM_PROT_READ; 420 } 421 else 422 #endif 423 { 424 u_int insn = ReadWord(tf->tf_pc); 425 426 if (((insn & 0x0c100000) == 0x04000000) || /* STR[B] */ 427 ((insn & 0x0e1000b0) == 0x000000b0) || /* STR[HD]*/ 428 ((insn & 0x0a100000) == 0x08000000) || /* STM/CDT*/ 429 ((insn & 0x0f9000f0) == 0x01800090)) /* STREX[BDH] */ 430 ftype = VM_PROT_WRITE; 431 else if ((insn & 0x0fb00ff0) == 0x01000090)/* SWP */ 432 ftype = VM_PROT_READ | VM_PROT_WRITE; 433 else 434 ftype = VM_PROT_READ; 435 } 436 } 437 438 /* 439 * See if the fault is as a result of ref/mod emulation, 440 * or domain mismatch. 441 */ 442 #ifdef DEBUG 443 last_fault_code = fsr; 444 #endif 445 if (pmap_fault_fixup(map->pmap, va, ftype, user)) { 446 UVMHIST_LOG(maphist, " <- ref/mod emul", 0, 0, 0, 0); 447 goto out; 448 } 449 450 if (__predict_false(curcpu()->ci_intr_depth > 0)) { 451 if (pcb->pcb_onfault) { 452 tf->tf_r0 = EINVAL; 453 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 454 return; 455 } 456 printf("\nNon-emulated page fault with intr_depth > 0\n"); 457 dab_fatal(tf, fsr, far, l, NULL); 458 } 459 460 onfault = pcb->pcb_onfault; 461 pcb->pcb_onfault = NULL; 462 error = uvm_fault(map, va, ftype); 463 pcb->pcb_onfault = onfault; 464 465 if (__predict_true(error == 0)) { 466 if (user) 467 uvm_grow(l->l_proc, va); /* Record any stack growth */ 468 else 469 ucas_ras_check(tf); 470 UVMHIST_LOG(maphist, " <- uvm", 0, 0, 0, 0); 471 goto out; 472 } 473 474 if (user == 0) { 475 if (pcb->pcb_onfault) { 476 tf->tf_r0 = error; 477 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 478 return; 479 } 480 481 printf("\nuvm_fault(%p, %lx, %x) -> %x\n", map, va, ftype, 482 error); 483 dab_fatal(tf, fsr, far, l, NULL); 484 } 485 486 KSI_INIT_TRAP(&ksi); 487 488 if (error == ENOMEM) { 489 printf("UVM: pid %d (%s), uid %d killed: " 490 "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm, 491 l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1); 492 ksi.ksi_signo = SIGKILL; 493 } else 494 ksi.ksi_signo = SIGSEGV; 495 496 ksi.ksi_code = (error == EACCES) ? SEGV_ACCERR : SEGV_MAPERR; 497 ksi.ksi_addr = (uint32_t *)(intptr_t) far; 498 ksi.ksi_trap = fsr; 499 UVMHIST_LOG(maphist, " <- error (%d)", error, 0, 0, 0); 500 501 do_trapsignal: 502 if (l->l_proc->p_pid == 1 || cpu_printfataltraps) { 503 printf("%d.%d(%s): trap: signo=%d code=%d addr=%p trap=%#x\n", 504 l->l_proc->p_pid, l->l_lid, l->l_proc->p_comm, 505 ksi.ksi_signo, ksi.ksi_code, ksi.ksi_addr, ksi.ksi_trap); 506 printf("r0=%08x r1=%08x r2=%08x r3=%08x\n", 507 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3); 508 printf("r4=%08x r5=%08x r6=%08x r7=%08x\n", 509 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7); 510 printf("r8=%08x r9=%08x rA=%08x rB=%08x\n", 511 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11); 512 printf("ip=%08x sp=%08x lr=%08x pc=%08x spsr=%08x\n", 513 tf->tf_r12, tf->tf_usr_sp, tf->tf_usr_lr, tf->tf_pc, 514 tf->tf_spsr); 515 } 516 call_trapsignal(l, &ksi); 517 out: 518 /* If returning to user mode, make sure to invoke userret() */ 519 if (user) 520 userret(l); 521 } 522 523 /* 524 * dab_fatal() handles the following data aborts: 525 * 526 * FAULT_WRTBUF_0 - Vector Exception 527 * FAULT_WRTBUF_1 - Terminal Exception 528 * 529 * We should never see these on a properly functioning system. 530 * 531 * This function is also called by the other handlers if they 532 * detect a fatal problem. 533 * 534 * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort. 535 */ 536 static int 537 dab_fatal(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi) 538 { 539 const char * const mode = TRAP_USERMODE(tf) ? "user" : "kernel"; 540 541 if (l != NULL) { 542 printf("Fatal %s mode data abort: '%s'\n", mode, 543 data_aborts[fsr & FAULT_TYPE_MASK].desc); 544 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr); 545 if ((fsr & FAULT_IMPRECISE) == 0) 546 printf("%08x, ", far); 547 else 548 printf("Invalid, "); 549 printf("spsr=%08x\n", tf->tf_spsr); 550 } else { 551 printf("Fatal %s mode prefetch abort at 0x%08x\n", 552 mode, tf->tf_pc); 553 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr); 554 } 555 556 printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n", 557 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3); 558 printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n", 559 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7); 560 printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n", 561 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11); 562 printf("r12=%08x, ", tf->tf_r12); 563 564 if (TRAP_USERMODE(tf)) 565 printf("usp=%08x, ulr=%08x", 566 tf->tf_usr_sp, tf->tf_usr_lr); 567 else 568 printf("ssp=%08x, slr=%08x", 569 tf->tf_svc_sp, tf->tf_svc_lr); 570 printf(", pc =%08x\n\n", tf->tf_pc); 571 572 #if defined(DDB) || defined(KGDB) 573 kdb_trap(T_FAULT, tf); 574 #endif 575 panic("Fatal abort"); 576 /*NOTREACHED*/ 577 } 578 579 /* 580 * dab_align() handles the following data aborts: 581 * 582 * FAULT_ALIGN_0 - Alignment fault 583 * FAULT_ALIGN_0 - Alignment fault 584 * 585 * These faults are fatal if they happen in kernel mode. Otherwise, we 586 * deliver a bus error to the process. 587 */ 588 static int 589 dab_align(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi) 590 { 591 /* Alignment faults are always fatal if they occur in kernel mode */ 592 if (!TRAP_USERMODE(tf)) 593 dab_fatal(tf, fsr, far, l, NULL); 594 595 /* pcb_onfault *must* be NULL at this point */ 596 KDASSERT(((struct pcb *)lwp_getpcb(l))->pcb_onfault == NULL); 597 598 /* See if the CPU state needs to be fixed up */ 599 (void) data_abort_fixup(tf, fsr, far, l); 600 601 /* Deliver a bus error signal to the process */ 602 KSI_INIT_TRAP(ksi); 603 ksi->ksi_signo = SIGBUS; 604 ksi->ksi_code = BUS_ADRALN; 605 ksi->ksi_addr = (uint32_t *)(intptr_t)far; 606 ksi->ksi_trap = fsr; 607 608 lwp_settrapframe(l, tf); 609 610 return (1); 611 } 612 613 /* 614 * dab_buserr() handles the following data aborts: 615 * 616 * FAULT_BUSERR_0 - External Abort on Linefetch -- Section 617 * FAULT_BUSERR_1 - External Abort on Linefetch -- Page 618 * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section 619 * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page 620 * FAULT_BUSTRNL1 - External abort on Translation -- Level 1 621 * FAULT_BUSTRNL2 - External abort on Translation -- Level 2 622 * 623 * If pcb_onfault is set, flag the fault and return to the handler. 624 * If the fault occurred in user mode, give the process a SIGBUS. 625 * 626 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2 627 * can be flagged as imprecise in the FSR. This causes a real headache 628 * since some of the machine state is lost. In this case, tf->tf_pc 629 * may not actually point to the offending instruction. In fact, if 630 * we've taken a double abort fault, it generally points somewhere near 631 * the top of "data_abort_entry" in exception.S. 632 * 633 * In all other cases, these data aborts are considered fatal. 634 */ 635 static int 636 dab_buserr(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, 637 ksiginfo_t *ksi) 638 { 639 struct pcb *pcb = lwp_getpcb(l); 640 641 #ifdef __XSCALE__ 642 if ((fsr & FAULT_IMPRECISE) != 0 && 643 (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) { 644 /* 645 * Oops, an imprecise, double abort fault. We've lost the 646 * r14_abt/spsr_abt values corresponding to the original 647 * abort, and the spsr saved in the trapframe indicates 648 * ABT mode. 649 */ 650 tf->tf_spsr &= ~PSR_MODE; 651 652 /* 653 * We use a simple heuristic to determine if the double abort 654 * happened as a result of a kernel or user mode access. 655 * If the current trapframe is at the top of the kernel stack, 656 * the fault _must_ have come from user mode. 657 */ 658 if (tf != ((trapframe_t *)pcb->pcb_ksp) - 1) { 659 /* 660 * Kernel mode. We're either about to die a 661 * spectacular death, or pcb_onfault will come 662 * to our rescue. Either way, the current value 663 * of tf->tf_pc is irrelevant. 664 */ 665 tf->tf_spsr |= PSR_SVC32_MODE; 666 if (pcb->pcb_onfault == NULL) 667 printf("\nKernel mode double abort!\n"); 668 } else { 669 /* 670 * User mode. We've lost the program counter at the 671 * time of the fault (not that it was accurate anyway; 672 * it's not called an imprecise fault for nothing). 673 * About all we can do is copy r14_usr to tf_pc and 674 * hope for the best. The process is about to get a 675 * SIGBUS, so it's probably history anyway. 676 */ 677 tf->tf_spsr |= PSR_USR32_MODE; 678 tf->tf_pc = tf->tf_usr_lr; 679 #ifdef THUMB_CODE 680 tf->tf_spsr &= ~PSR_T_bit; 681 if (tf->tf_usr_lr & 1) 682 tf->tf_spsr |= PSR_T_bit; 683 #endif 684 } 685 } 686 687 /* FAR is invalid for imprecise exceptions */ 688 if ((fsr & FAULT_IMPRECISE) != 0) 689 far = 0; 690 #endif /* __XSCALE__ */ 691 692 if (pcb->pcb_onfault) { 693 KDASSERT(TRAP_USERMODE(tf) == 0); 694 tf->tf_r0 = EFAULT; 695 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 696 return (0); 697 } 698 699 /* See if the CPU state needs to be fixed up */ 700 (void) data_abort_fixup(tf, fsr, far, l); 701 702 /* 703 * At this point, if the fault happened in kernel mode, we're toast 704 */ 705 if (!TRAP_USERMODE(tf)) 706 dab_fatal(tf, fsr, far, l, NULL); 707 708 /* Deliver a bus error signal to the process */ 709 KSI_INIT_TRAP(ksi); 710 ksi->ksi_signo = SIGBUS; 711 ksi->ksi_code = BUS_ADRERR; 712 ksi->ksi_addr = (uint32_t *)(intptr_t)far; 713 ksi->ksi_trap = fsr; 714 715 lwp_settrapframe(l, tf); 716 717 return (1); 718 } 719 720 static inline int 721 prefetch_abort_fixup(trapframe_t *tf) 722 { 723 #ifdef CPU_ABORT_FIXUP_REQUIRED 724 int error; 725 726 /* Call the CPU specific prefetch abort fixup routine */ 727 error = cpu_prefetchabt_fixup(tf); 728 if (__predict_true(error != ABORT_FIXUP_FAILED)) 729 return (error); 730 731 /* 732 * Oops, couldn't fix up the instruction 733 */ 734 printf("%s: fixup for %s mode prefetch abort failed.\n", __func__, 735 TRAP_USERMODE(tf) ? "user" : "kernel"); 736 #ifdef THUMB_CODE 737 if (tf->tf_spsr & PSR_T_bit) { 738 printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ", 739 tf->tf_pc, *((uint16 *)(tf->tf_pc & ~1)), 740 *((uint16 *)((tf->tf_pc + 2) & ~1))); 741 } 742 else 743 #endif 744 { 745 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc, 746 *((u_int *)tf->tf_pc)); 747 } 748 disassemble(tf->tf_pc); 749 750 /* Die now if this happened in kernel mode */ 751 if (!TRAP_USERMODE(tf)) 752 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL); 753 754 return (error); 755 #else 756 return (ABORT_FIXUP_OK); 757 #endif /* CPU_ABORT_FIXUP_REQUIRED */ 758 } 759 760 /* 761 * void prefetch_abort_handler(trapframe_t *tf) 762 * 763 * Abort handler called when instruction execution occurs at 764 * a non existent or restricted (access permissions) memory page. 765 * If the address is invalid and we were in SVC mode then panic as 766 * the kernel should never prefetch abort. 767 * If the address is invalid and the page is mapped then the user process 768 * does no have read permission so send it a signal. 769 * Otherwise fault the page in and try again. 770 */ 771 void 772 prefetch_abort_handler(trapframe_t *tf) 773 { 774 struct lwp *l; 775 struct pcb *pcb __diagused; 776 struct vm_map *map; 777 vaddr_t fault_pc, va; 778 ksiginfo_t ksi; 779 int error, user; 780 781 UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist); 782 783 /* Update vmmeter statistics */ 784 curcpu()->ci_data.cpu_ntrap++; 785 786 l = curlwp; 787 pcb = lwp_getpcb(l); 788 789 if ((user = TRAP_USERMODE(tf)) != 0) 790 LWP_CACHE_CREDS(l, l->l_proc); 791 792 /* 793 * Enable IRQ's (disabled by the abort) This always comes 794 * from user mode so we know interrupts were not disabled. 795 * But we check anyway. 796 */ 797 KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0); 798 if (__predict_true((tf->tf_spsr & I32_bit) != IF32_bits)) 799 restore_interrupts(tf->tf_spsr & IF32_bits); 800 801 /* See if the CPU state needs to be fixed up */ 802 switch (prefetch_abort_fixup(tf)) { 803 case ABORT_FIXUP_RETURN: 804 KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0); 805 return; 806 case ABORT_FIXUP_FAILED: 807 /* Deliver a SIGILL to the process */ 808 KSI_INIT_TRAP(&ksi); 809 ksi.ksi_signo = SIGILL; 810 ksi.ksi_code = ILL_ILLOPC; 811 ksi.ksi_addr = (uint32_t *)(intptr_t) tf->tf_pc; 812 lwp_settrapframe(l, tf); 813 goto do_trapsignal; 814 default: 815 break; 816 } 817 818 /* Prefetch aborts cannot happen in kernel mode */ 819 if (__predict_false(!user)) 820 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL); 821 822 /* Get fault address */ 823 fault_pc = tf->tf_pc; 824 lwp_settrapframe(l, tf); 825 UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, tf=0x%x)", fault_pc, l, tf, 826 0); 827 828 /* Ok validate the address, can only execute in USER space */ 829 if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS || 830 (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) { 831 KSI_INIT_TRAP(&ksi); 832 ksi.ksi_signo = SIGSEGV; 833 ksi.ksi_code = SEGV_ACCERR; 834 ksi.ksi_addr = (uint32_t *)(intptr_t) fault_pc; 835 ksi.ksi_trap = fault_pc; 836 goto do_trapsignal; 837 } 838 839 map = &l->l_proc->p_vmspace->vm_map; 840 va = trunc_page(fault_pc); 841 842 /* 843 * See if the pmap can handle this fault on its own... 844 */ 845 #ifdef DEBUG 846 last_fault_code = -1; 847 #endif 848 if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ|VM_PROT_EXECUTE, 1)) { 849 UVMHIST_LOG (maphist, " <- emulated", 0, 0, 0, 0); 850 goto out; 851 } 852 853 #ifdef DIAGNOSTIC 854 if (__predict_false(curcpu()->ci_intr_depth > 0)) { 855 printf("\nNon-emulated prefetch abort with intr_depth > 0\n"); 856 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL); 857 } 858 #endif 859 860 KASSERT(pcb->pcb_onfault == NULL); 861 error = uvm_fault(map, va, VM_PROT_READ); 862 863 if (__predict_true(error == 0)) { 864 UVMHIST_LOG (maphist, " <- uvm", 0, 0, 0, 0); 865 goto out; 866 } 867 KSI_INIT_TRAP(&ksi); 868 869 UVMHIST_LOG (maphist, " <- fatal (%d)", error, 0, 0, 0); 870 if (error == ENOMEM) { 871 printf("UVM: pid %d (%s), uid %d killed: " 872 "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm, 873 l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1); 874 ksi.ksi_signo = SIGKILL; 875 } else 876 ksi.ksi_signo = SIGSEGV; 877 878 ksi.ksi_code = SEGV_MAPERR; 879 ksi.ksi_addr = (uint32_t *)(intptr_t) fault_pc; 880 ksi.ksi_trap = fault_pc; 881 882 do_trapsignal: 883 call_trapsignal(l, &ksi); 884 885 out: 886 KASSERT(!TRAP_USERMODE(tf) || (tf->tf_spsr & IF32_bits) == 0); 887 userret(l); 888 } 889 890 /* 891 * Tentatively read an 8, 16, or 32-bit value from 'addr'. 892 * If the read succeeds, the value is written to 'rptr' and zero is returned. 893 * Else, return EFAULT. 894 */ 895 int 896 badaddr_read(void *addr, size_t size, void *rptr) 897 { 898 extern int badaddr_read_1(const uint8_t *, uint8_t *); 899 extern int badaddr_read_2(const uint16_t *, uint16_t *); 900 extern int badaddr_read_4(const uint32_t *, uint32_t *); 901 union { 902 uint8_t v1; 903 uint16_t v2; 904 uint32_t v4; 905 } u; 906 int rv, s; 907 908 cpu_drain_writebuf(); 909 910 s = splhigh(); 911 912 /* Read from the test address. */ 913 switch (size) { 914 case sizeof(uint8_t): 915 rv = badaddr_read_1(addr, &u.v1); 916 if (rv == 0 && rptr) 917 *(uint8_t *) rptr = u.v1; 918 break; 919 920 case sizeof(uint16_t): 921 rv = badaddr_read_2(addr, &u.v2); 922 if (rv == 0 && rptr) 923 *(uint16_t *) rptr = u.v2; 924 break; 925 926 case sizeof(uint32_t): 927 rv = badaddr_read_4(addr, &u.v4); 928 if (rv == 0 && rptr) 929 *(uint32_t *) rptr = u.v4; 930 break; 931 932 default: 933 panic("%s: invalid size (%zu)", __func__, size); 934 } 935 936 splx(s); 937 938 /* Return EFAULT if the address was invalid, else zero */ 939 return (rv); 940 } 941