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