1 /* $NetBSD: fault.c,v 1.64 2007/02/18 07:25:35 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.64 2007/02/18 07:25:35 matt Exp $"); 85 86 #include <sys/param.h> 87 #include <sys/systm.h> 88 #include <sys/proc.h> 89 #include <sys/user.h> 90 #include <sys/kernel.h> 91 #include <sys/kauth.h> 92 93 #include <uvm/uvm_extern.h> 94 #include <uvm/uvm_stat.h> 95 #ifdef UVMHIST 96 #include <uvm/uvm.h> 97 #endif 98 99 #include <arm/cpuconf.h> 100 101 #include <machine/frame.h> 102 #include <arm/arm32/katelib.h> 103 #include <machine/cpu.h> 104 #include <machine/intr.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 a fault came from user mode */ 159 #define TRAP_USERMODE(tf) ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE) 160 161 /* Determine if 'x' is a permission fault */ 162 #define IS_PERMISSION_FAULT(x) \ 163 (((1 << ((x) & FAULT_TYPE_MASK)) & \ 164 ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0) 165 166 #if 0 167 /* maybe one day we'll do emulations */ 168 #define TRAPSIGNAL(l,k) (*(l)->l_proc->p_emul->e_trapsignal)((l), (k)) 169 #else 170 #define TRAPSIGNAL(l,k) trapsignal((l), (k)) 171 #endif 172 173 static inline void 174 call_trapsignal(struct lwp *l, ksiginfo_t *ksi) 175 { 176 177 KERNEL_LOCK(1, l); 178 TRAPSIGNAL(l, ksi); 179 KERNEL_UNLOCK_LAST(l); 180 } 181 182 static inline int 183 data_abort_fixup(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l) 184 { 185 #ifdef CPU_ABORT_FIXUP_REQUIRED 186 int error; 187 188 /* Call the CPU specific data abort fixup routine */ 189 error = cpu_dataabt_fixup(tf); 190 if (__predict_true(error != ABORT_FIXUP_FAILED)) 191 return (error); 192 193 /* 194 * Oops, couldn't fix up the instruction 195 */ 196 printf("data_abort_fixup: fixup for %s mode data abort failed.\n", 197 TRAP_USERMODE(tf) ? "user" : "kernel"); 198 #ifdef THUMB_CODE 199 if (tf->tf_spsr & PSR_T_bit) { 200 printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ", 201 tf->tf_pc, *((u_int16 *)(tf->tf_pc & ~1), 202 *((u_int16 *)((tf->tf_pc + 2) & ~1)); 203 } 204 else 205 #endif 206 { 207 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc, 208 *((u_int *)tf->tf_pc)); 209 } 210 disassemble(tf->tf_pc); 211 212 /* Die now if this happened in kernel mode */ 213 if (!TRAP_USERMODE(tf)) 214 dab_fatal(tf, fsr, far, l, NULL); 215 216 return (error); 217 #else 218 return (ABORT_FIXUP_OK); 219 #endif /* CPU_ABORT_FIXUP_REQUIRED */ 220 } 221 222 void 223 data_abort_handler(trapframe_t *tf) 224 { 225 struct vm_map *map; 226 struct pcb *pcb; 227 struct lwp *l; 228 u_int user, far, fsr; 229 vm_prot_t ftype; 230 void *onfault; 231 vaddr_t va; 232 int error; 233 ksiginfo_t ksi; 234 235 UVMHIST_FUNC("data_abort_handler"); 236 237 /* Grab FAR/FSR before enabling interrupts */ 238 far = cpu_faultaddress(); 239 fsr = cpu_faultstatus(); 240 241 UVMHIST_CALLED(maphist); 242 /* Update vmmeter statistics */ 243 uvmexp.traps++; 244 245 /* Re-enable interrupts if they were enabled previously */ 246 if (__predict_true((tf->tf_spsr & I32_bit) == 0)) 247 enable_interrupts(I32_bit); 248 249 /* Get the current lwp structure or lwp0 if there is none */ 250 l = (curlwp != NULL) ? curlwp : &lwp0; 251 252 UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, far=0x%x, fsr=0x%x)", 253 tf->tf_pc, l, far, fsr); 254 255 /* Data abort came from user mode? */ 256 if ((user = TRAP_USERMODE(tf)) != 0) 257 LWP_CACHE_CREDS(l, l->l_proc); 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 384 /* 385 * We need to know whether the page should be mapped 386 * as R or R/W. The MMU does not give us the info as 387 * to whether the fault was caused by a read or a write. 388 * 389 * However, we know that a permission fault can only be 390 * the result of a write to a read-only location, so 391 * we can deal with those quickly. 392 * 393 * Otherwise we need to disassemble the instruction 394 * responsible to determine if it was a write. 395 */ 396 if (IS_PERMISSION_FAULT(fsr)) 397 ftype = VM_PROT_WRITE; 398 else { 399 #ifdef THUMB_CODE 400 /* Fast track the ARM case. */ 401 if (__predict_false(tf->tf_spsr & PSR_T_bit)) { 402 u_int insn = fusword((void *)(tf->tf_pc & ~1)); 403 u_int insn_f8 = insn & 0xf800; 404 u_int insn_fe = insn & 0xfe00; 405 406 if (insn_f8 == 0x6000 || /* STR(1) */ 407 insn_f8 == 0x7000 || /* STRB(1) */ 408 insn_f8 == 0x8000 || /* STRH(1) */ 409 insn_f8 == 0x9000 || /* STR(3) */ 410 insn_f8 == 0xc000 || /* STM */ 411 insn_fe == 0x5000 || /* STR(2) */ 412 insn_fe == 0x5200 || /* STRH(2) */ 413 insn_fe == 0x5400) /* STRB(2) */ 414 ftype = VM_PROT_WRITE; 415 else 416 ftype = VM_PROT_READ; 417 } 418 else 419 #endif 420 { 421 u_int insn = ReadWord(tf->tf_pc); 422 423 if (((insn & 0x0c100000) == 0x04000000) || /* STR[B] */ 424 ((insn & 0x0e1000b0) == 0x000000b0) || /* STR[HD]*/ 425 ((insn & 0x0a100000) == 0x08000000)) /* STM/CDT*/ 426 ftype = VM_PROT_WRITE; 427 else if ((insn & 0x0fb00ff0) == 0x01000090)/* SWP */ 428 ftype = VM_PROT_READ | VM_PROT_WRITE; 429 else 430 ftype = VM_PROT_READ; 431 } 432 } 433 434 /* 435 * See if the fault is as a result of ref/mod emulation, 436 * or domain mismatch. 437 */ 438 #ifdef DEBUG 439 last_fault_code = fsr; 440 #endif 441 if (pmap_fault_fixup(map->pmap, va, ftype, user)) { 442 UVMHIST_LOG(maphist, " <- ref/mod emul", 0, 0, 0, 0); 443 goto out; 444 } 445 446 if (__predict_false(current_intr_depth > 0)) { 447 if (pcb->pcb_onfault) { 448 tf->tf_r0 = EINVAL; 449 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 450 return; 451 } 452 printf("\nNon-emulated page fault with intr_depth > 0\n"); 453 dab_fatal(tf, fsr, far, l, NULL); 454 } 455 456 onfault = pcb->pcb_onfault; 457 pcb->pcb_onfault = NULL; 458 error = uvm_fault(map, va, ftype); 459 pcb->pcb_onfault = onfault; 460 461 if (__predict_true(error == 0)) { 462 if (user) 463 uvm_grow(l->l_proc, va); /* Record any stack growth */ 464 UVMHIST_LOG(maphist, " <- uvm", 0, 0, 0, 0); 465 goto out; 466 } 467 468 if (user == 0) { 469 if (pcb->pcb_onfault) { 470 tf->tf_r0 = error; 471 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 472 return; 473 } 474 475 printf("\nuvm_fault(%p, %lx, %x) -> %x\n", map, va, ftype, 476 error); 477 dab_fatal(tf, fsr, far, l, NULL); 478 } 479 480 KSI_INIT_TRAP(&ksi); 481 482 if (error == ENOMEM) { 483 printf("UVM: pid %d (%s), uid %d killed: " 484 "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm, 485 l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1); 486 ksi.ksi_signo = SIGKILL; 487 } else 488 ksi.ksi_signo = SIGSEGV; 489 490 ksi.ksi_code = (error == EACCES) ? SEGV_ACCERR : SEGV_MAPERR; 491 ksi.ksi_addr = (u_int32_t *)(intptr_t) far; 492 ksi.ksi_trap = fsr; 493 UVMHIST_LOG(maphist, " <- erorr (%d)", error, 0, 0, 0); 494 495 do_trapsignal: 496 call_trapsignal(l, &ksi); 497 out: 498 /* If returning to user mode, make sure to invoke userret() */ 499 if (user) 500 userret(l); 501 } 502 503 /* 504 * dab_fatal() handles the following data aborts: 505 * 506 * FAULT_WRTBUF_0 - Vector Exception 507 * FAULT_WRTBUF_1 - Terminal Exception 508 * 509 * We should never see these on a properly functioning system. 510 * 511 * This function is also called by the other handlers if they 512 * detect a fatal problem. 513 * 514 * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort. 515 */ 516 static int 517 dab_fatal(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi) 518 { 519 const char *mode; 520 521 mode = TRAP_USERMODE(tf) ? "user" : "kernel"; 522 523 if (l != NULL) { 524 printf("Fatal %s mode data abort: '%s'\n", mode, 525 data_aborts[fsr & FAULT_TYPE_MASK].desc); 526 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr); 527 if ((fsr & FAULT_IMPRECISE) == 0) 528 printf("%08x, ", far); 529 else 530 printf("Invalid, "); 531 printf("spsr=%08x\n", tf->tf_spsr); 532 } else { 533 printf("Fatal %s mode prefetch abort at 0x%08x\n", 534 mode, tf->tf_pc); 535 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr); 536 } 537 538 printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n", 539 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3); 540 printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n", 541 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7); 542 printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n", 543 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11); 544 printf("r12=%08x, ", tf->tf_r12); 545 546 if (TRAP_USERMODE(tf)) 547 printf("usp=%08x, ulr=%08x", 548 tf->tf_usr_sp, tf->tf_usr_lr); 549 else 550 printf("ssp=%08x, slr=%08x", 551 tf->tf_svc_sp, tf->tf_svc_lr); 552 printf(", pc =%08x\n\n", tf->tf_pc); 553 554 #if defined(DDB) || defined(KGDB) 555 kdb_trap(T_FAULT, tf); 556 #endif 557 panic("Fatal abort"); 558 /*NOTREACHED*/ 559 } 560 561 /* 562 * dab_align() handles the following data aborts: 563 * 564 * FAULT_ALIGN_0 - Alignment fault 565 * FAULT_ALIGN_0 - Alignment fault 566 * 567 * These faults are fatal if they happen in kernel mode. Otherwise, we 568 * deliver a bus error to the process. 569 */ 570 static int 571 dab_align(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, ksiginfo_t *ksi) 572 { 573 574 /* Alignment faults are always fatal if they occur in kernel mode */ 575 if (!TRAP_USERMODE(tf)) 576 dab_fatal(tf, fsr, far, l, NULL); 577 578 /* pcb_onfault *must* be NULL at this point */ 579 KDASSERT(l->l_addr->u_pcb.pcb_onfault == NULL); 580 581 /* See if the CPU state needs to be fixed up */ 582 (void) data_abort_fixup(tf, fsr, far, l); 583 584 /* Deliver a bus error signal to the process */ 585 KSI_INIT_TRAP(ksi); 586 ksi->ksi_signo = SIGBUS; 587 ksi->ksi_code = BUS_ADRALN; 588 ksi->ksi_addr = (u_int32_t *)(intptr_t)far; 589 ksi->ksi_trap = fsr; 590 591 l->l_addr->u_pcb.pcb_tf = tf; 592 593 return (1); 594 } 595 596 /* 597 * dab_buserr() handles the following data aborts: 598 * 599 * FAULT_BUSERR_0 - External Abort on Linefetch -- Section 600 * FAULT_BUSERR_1 - External Abort on Linefetch -- Page 601 * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section 602 * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page 603 * FAULT_BUSTRNL1 - External abort on Translation -- Level 1 604 * FAULT_BUSTRNL2 - External abort on Translation -- Level 2 605 * 606 * If pcb_onfault is set, flag the fault and return to the handler. 607 * If the fault occurred in user mode, give the process a SIGBUS. 608 * 609 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2 610 * can be flagged as imprecise in the FSR. This causes a real headache 611 * since some of the machine state is lost. In this case, tf->tf_pc 612 * may not actually point to the offending instruction. In fact, if 613 * we've taken a double abort fault, it generally points somewhere near 614 * the top of "data_abort_entry" in exception.S. 615 * 616 * In all other cases, these data aborts are considered fatal. 617 */ 618 static int 619 dab_buserr(trapframe_t *tf, u_int fsr, u_int far, struct lwp *l, 620 ksiginfo_t *ksi) 621 { 622 struct pcb *pcb = &l->l_addr->u_pcb; 623 624 #ifdef __XSCALE__ 625 if ((fsr & FAULT_IMPRECISE) != 0 && 626 (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) { 627 /* 628 * Oops, an imprecise, double abort fault. We've lost the 629 * r14_abt/spsr_abt values corresponding to the original 630 * abort, and the spsr saved in the trapframe indicates 631 * ABT mode. 632 */ 633 tf->tf_spsr &= ~PSR_MODE; 634 635 /* 636 * We use a simple heuristic to determine if the double abort 637 * happened as a result of a kernel or user mode access. 638 * If the current trapframe is at the top of the kernel stack, 639 * the fault _must_ have come from user mode. 640 */ 641 if (tf != ((trapframe_t *)pcb->pcb_un.un_32.pcb32_sp) - 1) { 642 /* 643 * Kernel mode. We're either about to die a 644 * spectacular death, or pcb_onfault will come 645 * to our rescue. Either way, the current value 646 * of tf->tf_pc is irrelevant. 647 */ 648 tf->tf_spsr |= PSR_SVC32_MODE; 649 if (pcb->pcb_onfault == NULL) 650 printf("\nKernel mode double abort!\n"); 651 } else { 652 /* 653 * User mode. We've lost the program counter at the 654 * time of the fault (not that it was accurate anyway; 655 * it's not called an imprecise fault for nothing). 656 * About all we can do is copy r14_usr to tf_pc and 657 * hope for the best. The process is about to get a 658 * SIGBUS, so it's probably history anyway. 659 */ 660 tf->tf_spsr |= PSR_USR32_MODE; 661 tf->tf_pc = tf->tf_usr_lr; 662 #ifdef THUMB_CODE 663 tf->tf_spsr &= ~PSR_T_bit; 664 if (tf->tf_usr_lr & 1) 665 tf->tf_spsr |= PSR_T_bit; 666 #endif 667 } 668 } 669 670 /* FAR is invalid for imprecise exceptions */ 671 if ((fsr & FAULT_IMPRECISE) != 0) 672 far = 0; 673 #endif /* __XSCALE__ */ 674 675 if (pcb->pcb_onfault) { 676 KDASSERT(TRAP_USERMODE(tf) == 0); 677 tf->tf_r0 = EFAULT; 678 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 679 return (0); 680 } 681 682 /* See if the CPU state needs to be fixed up */ 683 (void) data_abort_fixup(tf, fsr, far, l); 684 685 /* 686 * At this point, if the fault happened in kernel mode, we're toast 687 */ 688 if (!TRAP_USERMODE(tf)) 689 dab_fatal(tf, fsr, far, l, NULL); 690 691 /* Deliver a bus error signal to the process */ 692 KSI_INIT_TRAP(ksi); 693 ksi->ksi_signo = SIGBUS; 694 ksi->ksi_code = BUS_ADRERR; 695 ksi->ksi_addr = (u_int32_t *)(intptr_t)far; 696 ksi->ksi_trap = fsr; 697 698 l->l_addr->u_pcb.pcb_tf = tf; 699 700 return (1); 701 } 702 703 static inline int 704 prefetch_abort_fixup(trapframe_t *tf) 705 { 706 #ifdef CPU_ABORT_FIXUP_REQUIRED 707 int error; 708 709 /* Call the CPU specific prefetch abort fixup routine */ 710 error = cpu_prefetchabt_fixup(tf); 711 if (__predict_true(error != ABORT_FIXUP_FAILED)) 712 return (error); 713 714 /* 715 * Oops, couldn't fix up the instruction 716 */ 717 printf( 718 "prefetch_abort_fixup: fixup for %s mode prefetch abort failed.\n", 719 TRAP_USERMODE(tf) ? "user" : "kernel"); 720 #ifdef THUMB_CODE 721 if (tf->tf_spsr & PSR_T_bit) { 722 printf("pc = 0x%08x, opcode 0x%04x, 0x%04x, insn = ", 723 tf->tf_pc, *((u_int16 *)(tf->tf_pc & ~1), 724 *((u_int16 *)((tf->tf_pc + 2) & ~1)); 725 } 726 else 727 #endif 728 { 729 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc, 730 *((u_int *)tf->tf_pc)); 731 } 732 disassemble(tf->tf_pc); 733 734 /* Die now if this happened in kernel mode */ 735 if (!TRAP_USERMODE(tf)) 736 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL); 737 738 return (error); 739 #else 740 return (ABORT_FIXUP_OK); 741 #endif /* CPU_ABORT_FIXUP_REQUIRED */ 742 } 743 744 /* 745 * void prefetch_abort_handler(trapframe_t *tf) 746 * 747 * Abort handler called when instruction execution occurs at 748 * a non existent or restricted (access permissions) memory page. 749 * If the address is invalid and we were in SVC mode then panic as 750 * the kernel should never prefetch abort. 751 * If the address is invalid and the page is mapped then the user process 752 * does no have read permission so send it a signal. 753 * Otherwise fault the page in and try again. 754 */ 755 void 756 prefetch_abort_handler(trapframe_t *tf) 757 { 758 struct lwp *l; 759 struct vm_map *map; 760 vaddr_t fault_pc, va; 761 ksiginfo_t ksi; 762 int error, user; 763 764 UVMHIST_FUNC("prefetch_abort_handler"); UVMHIST_CALLED(maphist); 765 766 /* Update vmmeter statistics */ 767 uvmexp.traps++; 768 769 l = curlwp; 770 771 if ((user = TRAP_USERMODE(tf)) != 0) 772 LWP_CACHE_CREDS(l, l->l_proc); 773 774 /* 775 * Enable IRQ's (disabled by the abort) This always comes 776 * from user mode so we know interrupts were not disabled. 777 * But we check anyway. 778 */ 779 if (__predict_true((tf->tf_spsr & I32_bit) == 0)) 780 enable_interrupts(I32_bit); 781 782 /* See if the CPU state needs to be fixed up */ 783 switch (prefetch_abort_fixup(tf)) { 784 case ABORT_FIXUP_RETURN: 785 return; 786 case ABORT_FIXUP_FAILED: 787 /* Deliver a SIGILL to the process */ 788 KSI_INIT_TRAP(&ksi); 789 ksi.ksi_signo = SIGILL; 790 ksi.ksi_code = ILL_ILLOPC; 791 ksi.ksi_addr = (u_int32_t *)(intptr_t) tf->tf_pc; 792 l->l_addr->u_pcb.pcb_tf = tf; 793 goto do_trapsignal; 794 default: 795 break; 796 } 797 798 /* Prefetch aborts cannot happen in kernel mode */ 799 if (__predict_false(!user)) 800 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL); 801 802 /* Get fault address */ 803 fault_pc = tf->tf_pc; 804 l = curlwp; 805 l->l_addr->u_pcb.pcb_tf = tf; 806 UVMHIST_LOG(maphist, " (pc=0x%x, l=0x%x, tf=0x%x)", fault_pc, l, tf, 807 0); 808 809 /* Ok validate the address, can only execute in USER space */ 810 if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS || 811 (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) { 812 KSI_INIT_TRAP(&ksi); 813 ksi.ksi_signo = SIGSEGV; 814 ksi.ksi_code = SEGV_ACCERR; 815 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc; 816 ksi.ksi_trap = fault_pc; 817 goto do_trapsignal; 818 } 819 820 map = &l->l_proc->p_vmspace->vm_map; 821 va = trunc_page(fault_pc); 822 823 /* 824 * See if the pmap can handle this fault on its own... 825 */ 826 #ifdef DEBUG 827 last_fault_code = -1; 828 #endif 829 if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1)) { 830 UVMHIST_LOG (maphist, " <- emulated", 0, 0, 0, 0); 831 goto out; 832 } 833 834 #ifdef DIAGNOSTIC 835 if (__predict_false(current_intr_depth > 0)) { 836 printf("\nNon-emulated prefetch abort with intr_depth > 0\n"); 837 dab_fatal(tf, 0, tf->tf_pc, NULL, NULL); 838 } 839 #endif 840 error = uvm_fault(map, va, VM_PROT_READ); 841 842 if (__predict_true(error == 0)) { 843 UVMHIST_LOG (maphist, " <- uvm", 0, 0, 0, 0); 844 goto out; 845 } 846 KSI_INIT_TRAP(&ksi); 847 848 UVMHIST_LOG (maphist, " <- fatal (%d)", error, 0, 0, 0); 849 if (error == ENOMEM) { 850 printf("UVM: pid %d (%s), uid %d killed: " 851 "out of swap\n", l->l_proc->p_pid, l->l_proc->p_comm, 852 l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1); 853 ksi.ksi_signo = SIGKILL; 854 } else 855 ksi.ksi_signo = SIGSEGV; 856 857 ksi.ksi_code = SEGV_MAPERR; 858 ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc; 859 ksi.ksi_trap = fault_pc; 860 861 do_trapsignal: 862 call_trapsignal(l, &ksi); 863 864 out: 865 userret(l); 866 } 867 868 /* 869 * Tentatively read an 8, 16, or 32-bit value from 'addr'. 870 * If the read succeeds, the value is written to 'rptr' and zero is returned. 871 * Else, return EFAULT. 872 */ 873 int 874 badaddr_read(void *addr, size_t size, void *rptr) 875 { 876 extern int badaddr_read_1(const uint8_t *, uint8_t *); 877 extern int badaddr_read_2(const uint16_t *, uint16_t *); 878 extern int badaddr_read_4(const uint32_t *, uint32_t *); 879 union { 880 uint8_t v1; 881 uint16_t v2; 882 uint32_t v4; 883 } u; 884 struct pcb *curpcb_save; 885 int rv, s; 886 887 cpu_drain_writebuf(); 888 889 /* 890 * We might be called at interrupt time, so arrange to steal 891 * lwp0's PCB temporarily, if required, so that pcb_onfault 892 * handling works correctly. 893 */ 894 s = splhigh(); 895 if ((curpcb_save = curpcb) == NULL) 896 curpcb = &lwp0.l_addr->u_pcb; 897 898 /* Read from the test address. */ 899 switch (size) { 900 case sizeof(uint8_t): 901 rv = badaddr_read_1(addr, &u.v1); 902 if (rv == 0 && rptr) 903 *(uint8_t *) rptr = u.v1; 904 break; 905 906 case sizeof(uint16_t): 907 rv = badaddr_read_2(addr, &u.v2); 908 if (rv == 0 && rptr) 909 *(uint16_t *) rptr = u.v2; 910 break; 911 912 case sizeof(uint32_t): 913 rv = badaddr_read_4(addr, &u.v4); 914 if (rv == 0 && rptr) 915 *(uint32_t *) rptr = u.v4; 916 break; 917 918 default: 919 curpcb = curpcb_save; 920 panic("badaddr: invalid size (%lu)", (u_long) size); 921 } 922 923 /* Restore curpcb */ 924 curpcb = curpcb_save; 925 splx(s); 926 927 /* Return EFAULT if the address was invalid, else zero */ 928 return (rv); 929 } 930