1/* $NetBSD: fault.c,v 1.45 2003/11/20 14:44:36 scw Exp $ */ 2 3/*- 4 * Copyright 2004 Olivier Houchard 5 * Copyright 2003 Wasabi Systems, Inc. 6 * All rights reserved. 7 * 8 * Written by Steve C. Woodford for Wasabi Systems, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed for the NetBSD Project by 21 * Wasabi Systems, Inc. 22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 23 * or promote products derived from this software without specific prior 24 * written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38/*- 39 * Copyright (c) 1994-1997 Mark Brinicombe. 40 * Copyright (c) 1994 Brini. 41 * All rights reserved. 42 * 43 * This code is derived from software written for Brini by Mark Brinicombe 44 * 45 * Redistribution and use in source and binary forms, with or without 46 * modification, are permitted provided that the following conditions 47 * are met: 48 * 1. Redistributions of source code must retain the above copyright 49 * notice, this list of conditions and the following disclaimer. 50 * 2. Redistributions in binary form must reproduce the above copyright 51 * notice, this list of conditions and the following disclaimer in the 52 * documentation and/or other materials provided with the distribution. 53 * 3. All advertising materials mentioning features or use of this software 54 * must display the following acknowledgement: 55 * This product includes software developed by Brini. 56 * 4. The name of the company nor the name of the author may be used to 57 * endorse or promote products derived from this software without specific 58 * prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED 61 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 62 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 63 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 64 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 65 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * RiscBSD kernel project 73 * 74 * fault.c 75 * 76 * Fault handlers 77 * 78 * Created : 28/11/94 79 */ 80 81#include <sys/cdefs.h> 82__FBSDID("$FreeBSD: stable/11/sys/arm/arm/trap-v4.c 344905 2019-03-08 00:20:37Z jhb $"); 83 84#include <sys/param.h> 85#include <sys/systm.h> 86#include <sys/proc.h> 87#include <sys/lock.h> 88#include <sys/mutex.h> 89#include <sys/signalvar.h> 90#include <sys/vmmeter.h> 91 92#include <vm/vm.h> 93#include <vm/pmap.h> 94#include <vm/vm_kern.h> 95#include <vm/vm_map.h> 96#include <vm/vm_extern.h> 97 98#include <machine/cpu.h> 99#include <machine/frame.h> 100#include <machine/machdep.h> 101#include <machine/pcb.h> 102#include <machine/vmparam.h> 103 104#ifdef KDB 105#include <sys/kdb.h> 106#endif 107 108#ifdef KDTRACE_HOOKS 109#include <sys/dtrace_bsd.h> 110#endif 111 112#define ReadWord(a) (*((volatile unsigned int *)(a))) 113 114#ifdef DEBUG 115int last_fault_code; /* For the benefit of pmap_fault_fixup() */ 116#endif 117 118struct ksig { 119 int signb; 120 u_long code; 121}; 122struct data_abort { 123 int (*func)(struct trapframe *, u_int, u_int, struct thread *, 124 struct ksig *); 125 const char *desc; 126}; 127 128static int dab_fatal(struct trapframe *, u_int, u_int, struct thread *, 129 struct ksig *); 130static int dab_align(struct trapframe *, u_int, u_int, struct thread *, 131 struct ksig *); 132static int dab_buserr(struct trapframe *, u_int, u_int, struct thread *, 133 struct ksig *); 134static void prefetch_abort_handler(struct trapframe *); 135 136static const struct data_abort data_aborts[] = { 137 {dab_fatal, "Vector Exception"}, 138 {dab_align, "Alignment Fault 1"}, 139 {dab_fatal, "Terminal Exception"}, 140 {dab_align, "Alignment Fault 3"}, 141 {dab_buserr, "External Linefetch Abort (S)"}, 142 {NULL, "Translation Fault (S)"}, 143 {dab_buserr, "External Linefetch Abort (P)"}, 144 {NULL, "Translation Fault (P)"}, 145 {dab_buserr, "External Non-Linefetch Abort (S)"}, 146 {NULL, "Domain Fault (S)"}, 147 {dab_buserr, "External Non-Linefetch Abort (P)"}, 148 {NULL, "Domain Fault (P)"}, 149 {dab_buserr, "External Translation Abort (L1)"}, 150 {NULL, "Permission Fault (S)"}, 151 {dab_buserr, "External Translation Abort (L2)"}, 152 {NULL, "Permission Fault (P)"} 153}; 154 155/* Determine if a fault came from user mode */ 156#define TRAP_USERMODE(tf) ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE) 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 163static __inline void 164call_trapsignal(struct thread *td, int sig, u_long code) 165{ 166 ksiginfo_t ksi; 167 168 ksiginfo_init_trap(&ksi); 169 ksi.ksi_signo = sig; 170 ksi.ksi_code = (int)code; 171 trapsignal(td, &ksi); 172} 173 174void 175abort_handler(struct trapframe *tf, int type) 176{ 177 struct vm_map *map; 178 struct pcb *pcb; 179 struct thread *td; 180 u_int user, far, fsr; 181 vm_prot_t ftype; 182 void *onfault; 183 vm_offset_t va; 184 int error = 0; 185 struct ksig ksig; 186 struct proc *p; 187 188 if (type == 1) 189 return (prefetch_abort_handler(tf)); 190 191 /* Grab FAR/FSR before enabling interrupts */ 192 far = cpu_faultaddress(); 193 fsr = cpu_faultstatus(); 194#if 0 195 printf("data abort: fault address=%p (from pc=%p lr=%p)\n", 196 (void*)far, (void*)tf->tf_pc, (void*)tf->tf_svc_lr); 197#endif 198 199 /* Update vmmeter statistics */ 200#if 0 201 vmexp.traps++; 202#endif 203 204 td = curthread; 205 p = td->td_proc; 206 207 PCPU_INC(cnt.v_trap); 208 /* Data abort came from user mode? */ 209 user = TRAP_USERMODE(tf); 210 211 if (user) { 212 td->td_pticks = 0; 213 td->td_frame = tf; 214 if (td->td_cowgen != td->td_proc->p_cowgen) 215 thread_cow_update(td); 216 217 } 218 /* Grab the current pcb */ 219 pcb = td->td_pcb; 220 /* Re-enable interrupts if they were enabled previously */ 221 if (td->td_md.md_spinlock_count == 0) { 222 if (__predict_true(tf->tf_spsr & PSR_I) == 0) 223 enable_interrupts(PSR_I); 224 if (__predict_true(tf->tf_spsr & PSR_F) == 0) 225 enable_interrupts(PSR_F); 226 } 227 228 229 /* Invoke the appropriate handler, if necessary */ 230 if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) { 231 if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far, 232 td, &ksig)) { 233 goto do_trapsignal; 234 } 235 goto out; 236 } 237 238 /* 239 * At this point, we're dealing with one of the following data aborts: 240 * 241 * FAULT_TRANS_S - Translation -- Section 242 * FAULT_TRANS_P - Translation -- Page 243 * FAULT_DOMAIN_S - Domain -- Section 244 * FAULT_DOMAIN_P - Domain -- Page 245 * FAULT_PERM_S - Permission -- Section 246 * FAULT_PERM_P - Permission -- Page 247 * 248 * These are the main virtual memory-related faults signalled by 249 * the MMU. 250 */ 251 252 /* 253 * Make sure the Program Counter is sane. We could fall foul of 254 * someone executing Thumb code, in which case the PC might not 255 * be word-aligned. This would cause a kernel alignment fault 256 * further down if we have to decode the current instruction. 257 * XXX: It would be nice to be able to support Thumb at some point. 258 */ 259 if (__predict_false((tf->tf_pc & 3) != 0)) { 260 if (user) { 261 /* 262 * Give the user an illegal instruction signal. 263 */ 264 /* Deliver a SIGILL to the process */ 265 ksig.signb = SIGILL; 266 ksig.code = 0; 267 goto do_trapsignal; 268 } 269 270 /* 271 * The kernel never executes Thumb code. 272 */ 273 printf("\ndata_abort_fault: Misaligned Kernel-mode " 274 "Program Counter\n"); 275 dab_fatal(tf, fsr, far, td, &ksig); 276 } 277 278 va = trunc_page((vm_offset_t)far); 279 280 /* 281 * It is only a kernel address space fault iff: 282 * 1. user == 0 and 283 * 2. pcb_onfault not set or 284 * 3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction. 285 */ 286 if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS || 287 (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) && 288 __predict_true((pcb->pcb_onfault == NULL || 289 (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) { 290 map = kernel_map; 291 292 /* Was the fault due to the FPE/IPKDB ? */ 293 if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) { 294 295 /* 296 * Force exit via userret() 297 * This is necessary as the FPE is an extension to 298 * userland that actually runs in a priveledged mode 299 * but uses USR mode permissions for its accesses. 300 */ 301 user = 1; 302 ksig.signb = SIGSEGV; 303 ksig.code = 0; 304 goto do_trapsignal; 305 } 306 } else { 307 map = &td->td_proc->p_vmspace->vm_map; 308 } 309 310 /* 311 * We need to know whether the page should be mapped as R or R/W. 312 * On armv4, the fault status register does not indicate whether 313 * the access was a read or write. We know that a permission fault 314 * can only be the result of a write to a read-only location, so we 315 * can deal with those quickly. Otherwise we need to disassemble 316 * the faulting instruction to determine if it was a write. 317 */ 318 if (IS_PERMISSION_FAULT(fsr)) 319 ftype = VM_PROT_WRITE; 320 else { 321 u_int insn = ReadWord(tf->tf_pc); 322 323 if (((insn & 0x0c100000) == 0x04000000) || /* STR/STRB */ 324 ((insn & 0x0e1000b0) == 0x000000b0) || /* STRH/STRD */ 325 ((insn & 0x0a100000) == 0x08000000)) { /* STM/CDT */ 326 ftype = VM_PROT_WRITE; 327 } else { 328 if ((insn & 0x0fb00ff0) == 0x01000090) /* SWP */ 329 ftype = VM_PROT_READ | VM_PROT_WRITE; 330 else 331 ftype = VM_PROT_READ; 332 } 333 } 334 335 /* 336 * See if the fault is as a result of ref/mod emulation, 337 * or domain mismatch. 338 */ 339#ifdef DEBUG 340 last_fault_code = fsr; 341#endif 342 if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, 343 NULL, "Kernel page fault") != 0) 344 goto fatal_pagefault; 345 346 if (pmap_fault_fixup(vmspace_pmap(td->td_proc->p_vmspace), va, ftype, 347 user)) { 348 goto out; 349 } 350 351 onfault = pcb->pcb_onfault; 352 pcb->pcb_onfault = NULL; 353 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL); 354 pcb->pcb_onfault = onfault; 355 if (__predict_true(error == 0)) 356 goto out; 357fatal_pagefault: 358 if (user == 0) { 359 if (pcb->pcb_onfault) { 360 tf->tf_r0 = error; 361 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 362 return; 363 } 364 365 printf("\nvm_fault(%p, %x, %x, 0) -> %x\n", map, va, ftype, 366 error); 367 dab_fatal(tf, fsr, far, td, &ksig); 368 } 369 370 371 if (error == ENOMEM) { 372 printf("VM: pid %d (%s), uid %d killed: " 373 "out of swap\n", td->td_proc->p_pid, td->td_name, 374 (td->td_proc->p_ucred) ? 375 td->td_proc->p_ucred->cr_uid : -1); 376 ksig.signb = SIGKILL; 377 } else { 378 ksig.signb = SIGSEGV; 379 } 380 ksig.code = 0; 381do_trapsignal: 382 call_trapsignal(td, ksig.signb, ksig.code); 383out: 384 /* If returning to user mode, make sure to invoke userret() */ 385 if (user) 386 userret(td, tf); 387} 388 389/* 390 * dab_fatal() handles the following data aborts: 391 * 392 * FAULT_WRTBUF_0 - Vector Exception 393 * FAULT_WRTBUF_1 - Terminal Exception 394 * 395 * We should never see these on a properly functioning system. 396 * 397 * This function is also called by the other handlers if they 398 * detect a fatal problem. 399 * 400 * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort. 401 */ 402static int 403dab_fatal(struct trapframe *tf, u_int fsr, u_int far, struct thread *td, 404 struct ksig *ksig) 405{ 406 const char *mode; 407#ifdef KDB 408 bool handled; 409#endif 410 411#ifdef KDB 412 if (kdb_active) { 413 kdb_reenter(); 414 return (0); 415 } 416#endif 417#ifdef KDTRACE_HOOKS 418 if (!TRAP_USERMODE(tf)) { 419 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(tf, far & FAULT_TYPE_MASK)) 420 return (0); 421 } 422#endif 423 424 mode = TRAP_USERMODE(tf) ? "user" : "kernel"; 425 426 disable_interrupts(PSR_I|PSR_F); 427 if (td != NULL) { 428 printf("Fatal %s mode data abort: '%s'\n", mode, 429 data_aborts[fsr & FAULT_TYPE_MASK].desc); 430 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr); 431 if ((fsr & FAULT_IMPRECISE) == 0) 432 printf("%08x, ", far); 433 else 434 printf("Invalid, "); 435 printf("spsr=%08x\n", tf->tf_spsr); 436 } else { 437 printf("Fatal %s mode prefetch abort at 0x%08x\n", 438 mode, tf->tf_pc); 439 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr); 440 } 441 442 printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n", 443 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3); 444 printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n", 445 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7); 446 printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n", 447 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11); 448 printf("r12=%08x, ", tf->tf_r12); 449 450 if (TRAP_USERMODE(tf)) 451 printf("usp=%08x, ulr=%08x", 452 tf->tf_usr_sp, tf->tf_usr_lr); 453 else 454 printf("ssp=%08x, slr=%08x", 455 tf->tf_svc_sp, tf->tf_svc_lr); 456 printf(", pc =%08x\n\n", tf->tf_pc); 457 458#ifdef KDB 459 if (debugger_on_trap) { 460 kdb_why = KDB_WHY_TRAP; 461 handled = kdb_trap(fsr, 0, tf); 462 kdb_why = KDB_WHY_UNSET; 463 if (handled) 464 return (0); 465 } 466#endif 467 panic("Fatal abort"); 468 /*NOTREACHED*/ 469} 470 471/* 472 * dab_align() handles the following data aborts: 473 * 474 * FAULT_ALIGN_0 - Alignment fault 475 * FAULT_ALIGN_1 - Alignment fault 476 * 477 * These faults are fatal if they happen in kernel mode. Otherwise, we 478 * deliver a bus error to the process. 479 */ 480static int 481dab_align(struct trapframe *tf, u_int fsr, u_int far, struct thread *td, 482 struct ksig *ksig) 483{ 484 485 /* Alignment faults are always fatal if they occur in kernel mode */ 486 if (!TRAP_USERMODE(tf)) { 487 if (!td || !td->td_pcb->pcb_onfault) 488 dab_fatal(tf, fsr, far, td, ksig); 489 tf->tf_r0 = EFAULT; 490 tf->tf_pc = (int)td->td_pcb->pcb_onfault; 491 return (0); 492 } 493 494 /* pcb_onfault *must* be NULL at this point */ 495 496 /* Deliver a bus error signal to the process */ 497 ksig->code = 0; 498 ksig->signb = SIGBUS; 499 td->td_frame = tf; 500 501 return (1); 502} 503 504/* 505 * dab_buserr() handles the following data aborts: 506 * 507 * FAULT_BUSERR_0 - External Abort on Linefetch -- Section 508 * FAULT_BUSERR_1 - External Abort on Linefetch -- Page 509 * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section 510 * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page 511 * FAULT_BUSTRNL1 - External abort on Translation -- Level 1 512 * FAULT_BUSTRNL2 - External abort on Translation -- Level 2 513 * 514 * If pcb_onfault is set, flag the fault and return to the handler. 515 * If the fault occurred in user mode, give the process a SIGBUS. 516 * 517 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2 518 * can be flagged as imprecise in the FSR. This causes a real headache 519 * since some of the machine state is lost. In this case, tf->tf_pc 520 * may not actually point to the offending instruction. In fact, if 521 * we've taken a double abort fault, it generally points somewhere near 522 * the top of "data_abort_entry" in exception.S. 523 * 524 * In all other cases, these data aborts are considered fatal. 525 */ 526static int 527dab_buserr(struct trapframe *tf, u_int fsr, u_int far, struct thread *td, 528 struct ksig *ksig) 529{ 530 struct pcb *pcb = td->td_pcb; 531 532#ifdef __XSCALE__ 533 if ((fsr & FAULT_IMPRECISE) != 0 && 534 (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) { 535 /* 536 * Oops, an imprecise, double abort fault. We've lost the 537 * r14_abt/spsr_abt values corresponding to the original 538 * abort, and the spsr saved in the trapframe indicates 539 * ABT mode. 540 */ 541 tf->tf_spsr &= ~PSR_MODE; 542 543 /* 544 * We use a simple heuristic to determine if the double abort 545 * happened as a result of a kernel or user mode access. 546 * If the current trapframe is at the top of the kernel stack, 547 * the fault _must_ have come from user mode. 548 */ 549 if (tf != ((struct trapframe *)pcb->pcb_regs.sf_sp) - 1) { 550 /* 551 * Kernel mode. We're either about to die a 552 * spectacular death, or pcb_onfault will come 553 * to our rescue. Either way, the current value 554 * of tf->tf_pc is irrelevant. 555 */ 556 tf->tf_spsr |= PSR_SVC32_MODE; 557 if (pcb->pcb_onfault == NULL) 558 printf("\nKernel mode double abort!\n"); 559 } else { 560 /* 561 * User mode. We've lost the program counter at the 562 * time of the fault (not that it was accurate anyway; 563 * it's not called an imprecise fault for nothing). 564 * About all we can do is copy r14_usr to tf_pc and 565 * hope for the best. The process is about to get a 566 * SIGBUS, so it's probably history anyway. 567 */ 568 tf->tf_spsr |= PSR_USR32_MODE; 569 tf->tf_pc = tf->tf_usr_lr; 570 } 571 } 572 573 /* FAR is invalid for imprecise exceptions */ 574 if ((fsr & FAULT_IMPRECISE) != 0) 575 far = 0; 576#endif /* __XSCALE__ */ 577 578 if (pcb->pcb_onfault) { 579 tf->tf_r0 = EFAULT; 580 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 581 return (0); 582 } 583 584 /* 585 * At this point, if the fault happened in kernel mode, we're toast 586 */ 587 if (!TRAP_USERMODE(tf)) 588 dab_fatal(tf, fsr, far, td, ksig); 589 590 /* Deliver a bus error signal to the process */ 591 ksig->signb = SIGBUS; 592 ksig->code = 0; 593 td->td_frame = tf; 594 595 return (1); 596} 597 598/* 599 * void prefetch_abort_handler(struct trapframe *tf) 600 * 601 * Abort handler called when instruction execution occurs at 602 * a non existent or restricted (access permissions) memory page. 603 * If the address is invalid and we were in SVC mode then panic as 604 * the kernel should never prefetch abort. 605 * If the address is invalid and the page is mapped then the user process 606 * does no have read permission so send it a signal. 607 * Otherwise fault the page in and try again. 608 */ 609static void 610prefetch_abort_handler(struct trapframe *tf) 611{ 612 struct thread *td; 613 struct proc * p; 614 struct vm_map *map; 615 vm_offset_t fault_pc, va; 616 int error = 0; 617 struct ksig ksig; 618 619 620#if 0 621 /* Update vmmeter statistics */ 622 uvmexp.traps++; 623#endif 624#if 0 625 printf("prefetch abort handler: %p %p\n", (void*)tf->tf_pc, 626 (void*)tf->tf_usr_lr); 627#endif 628 629 td = curthread; 630 p = td->td_proc; 631 PCPU_INC(cnt.v_trap); 632 633 if (TRAP_USERMODE(tf)) { 634 td->td_frame = tf; 635 if (td->td_cowgen != td->td_proc->p_cowgen) 636 thread_cow_update(td); 637 } 638 fault_pc = tf->tf_pc; 639 if (td->td_md.md_spinlock_count == 0) { 640 if (__predict_true(tf->tf_spsr & PSR_I) == 0) 641 enable_interrupts(PSR_I); 642 if (__predict_true(tf->tf_spsr & PSR_F) == 0) 643 enable_interrupts(PSR_F); 644 } 645 646 /* Prefetch aborts cannot happen in kernel mode */ 647 if (__predict_false(!TRAP_USERMODE(tf))) 648 dab_fatal(tf, 0, tf->tf_pc, NULL, &ksig); 649 td->td_pticks = 0; 650 651 652 /* Ok validate the address, can only execute in USER space */ 653 if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS || 654 (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) { 655 ksig.signb = SIGSEGV; 656 ksig.code = 0; 657 goto do_trapsignal; 658 } 659 660 map = &td->td_proc->p_vmspace->vm_map; 661 va = trunc_page(fault_pc); 662 663 /* 664 * See if the pmap can handle this fault on its own... 665 */ 666#ifdef DEBUG 667 last_fault_code = -1; 668#endif 669 if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1)) 670 goto out; 671 672 error = vm_fault(map, va, VM_PROT_READ | VM_PROT_EXECUTE, 673 VM_FAULT_NORMAL); 674 if (__predict_true(error == 0)) 675 goto out; 676 677 if (error == ENOMEM) { 678 printf("VM: pid %d (%s), uid %d killed: " 679 "out of swap\n", td->td_proc->p_pid, td->td_name, 680 (td->td_proc->p_ucred) ? 681 td->td_proc->p_ucred->cr_uid : -1); 682 ksig.signb = SIGKILL; 683 } else { 684 ksig.signb = SIGSEGV; 685 } 686 ksig.code = 0; 687 688do_trapsignal: 689 call_trapsignal(td, ksig.signb, ksig.code); 690 691out: 692 userret(td, tf); 693 694} 695 696extern int badaddr_read_1(const uint8_t *, uint8_t *); 697extern int badaddr_read_2(const uint16_t *, uint16_t *); 698extern int badaddr_read_4(const uint32_t *, uint32_t *); 699/* 700 * Tentatively read an 8, 16, or 32-bit value from 'addr'. 701 * If the read succeeds, the value is written to 'rptr' and zero is returned. 702 * Else, return EFAULT. 703 */ 704int 705badaddr_read(void *addr, size_t size, void *rptr) 706{ 707 union { 708 uint8_t v1; 709 uint16_t v2; 710 uint32_t v4; 711 } u; 712 int rv; 713 714 cpu_drain_writebuf(); 715 716 /* Read from the test address. */ 717 switch (size) { 718 case sizeof(uint8_t): 719 rv = badaddr_read_1(addr, &u.v1); 720 if (rv == 0 && rptr) 721 *(uint8_t *) rptr = u.v1; 722 break; 723 724 case sizeof(uint16_t): 725 rv = badaddr_read_2(addr, &u.v2); 726 if (rv == 0 && rptr) 727 *(uint16_t *) rptr = u.v2; 728 break; 729 730 case sizeof(uint32_t): 731 rv = badaddr_read_4(addr, &u.v4); 732 if (rv == 0 && rptr) 733 *(uint32_t *) rptr = u.v4; 734 break; 735 736 default: 737 panic("badaddr: invalid size (%lu)", (u_long) size); 738 } 739 740 /* Return EFAULT if the address was invalid, else zero */ 741 return (rv); 742} 743