trap-v4.c revision 331017
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 331017 2018-03-15 19:08:33Z kevans $"); 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 408#ifdef KDTRACE_HOOKS 409 if (!TRAP_USERMODE(tf)) { 410 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(tf, far & FAULT_TYPE_MASK)) 411 return (0); 412 } 413#endif 414 415 mode = TRAP_USERMODE(tf) ? "user" : "kernel"; 416 417 disable_interrupts(PSR_I|PSR_F); 418 if (td != NULL) { 419 printf("Fatal %s mode data abort: '%s'\n", mode, 420 data_aborts[fsr & FAULT_TYPE_MASK].desc); 421 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr); 422 if ((fsr & FAULT_IMPRECISE) == 0) 423 printf("%08x, ", far); 424 else 425 printf("Invalid, "); 426 printf("spsr=%08x\n", tf->tf_spsr); 427 } else { 428 printf("Fatal %s mode prefetch abort at 0x%08x\n", 429 mode, tf->tf_pc); 430 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr); 431 } 432 433 printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n", 434 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3); 435 printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n", 436 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7); 437 printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n", 438 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11); 439 printf("r12=%08x, ", tf->tf_r12); 440 441 if (TRAP_USERMODE(tf)) 442 printf("usp=%08x, ulr=%08x", 443 tf->tf_usr_sp, tf->tf_usr_lr); 444 else 445 printf("ssp=%08x, slr=%08x", 446 tf->tf_svc_sp, tf->tf_svc_lr); 447 printf(", pc =%08x\n\n", tf->tf_pc); 448 449#ifdef KDB 450 if (debugger_on_panic || kdb_active) 451 if (kdb_trap(fsr, 0, tf)) 452 return (0); 453#endif 454 panic("Fatal abort"); 455 /*NOTREACHED*/ 456} 457 458/* 459 * dab_align() handles the following data aborts: 460 * 461 * FAULT_ALIGN_0 - Alignment fault 462 * FAULT_ALIGN_1 - Alignment fault 463 * 464 * These faults are fatal if they happen in kernel mode. Otherwise, we 465 * deliver a bus error to the process. 466 */ 467static int 468dab_align(struct trapframe *tf, u_int fsr, u_int far, struct thread *td, 469 struct ksig *ksig) 470{ 471 472 /* Alignment faults are always fatal if they occur in kernel mode */ 473 if (!TRAP_USERMODE(tf)) { 474 if (!td || !td->td_pcb->pcb_onfault) 475 dab_fatal(tf, fsr, far, td, ksig); 476 tf->tf_r0 = EFAULT; 477 tf->tf_pc = (int)td->td_pcb->pcb_onfault; 478 return (0); 479 } 480 481 /* pcb_onfault *must* be NULL at this point */ 482 483 /* Deliver a bus error signal to the process */ 484 ksig->code = 0; 485 ksig->signb = SIGBUS; 486 td->td_frame = tf; 487 488 return (1); 489} 490 491/* 492 * dab_buserr() handles the following data aborts: 493 * 494 * FAULT_BUSERR_0 - External Abort on Linefetch -- Section 495 * FAULT_BUSERR_1 - External Abort on Linefetch -- Page 496 * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section 497 * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page 498 * FAULT_BUSTRNL1 - External abort on Translation -- Level 1 499 * FAULT_BUSTRNL2 - External abort on Translation -- Level 2 500 * 501 * If pcb_onfault is set, flag the fault and return to the handler. 502 * If the fault occurred in user mode, give the process a SIGBUS. 503 * 504 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2 505 * can be flagged as imprecise in the FSR. This causes a real headache 506 * since some of the machine state is lost. In this case, tf->tf_pc 507 * may not actually point to the offending instruction. In fact, if 508 * we've taken a double abort fault, it generally points somewhere near 509 * the top of "data_abort_entry" in exception.S. 510 * 511 * In all other cases, these data aborts are considered fatal. 512 */ 513static int 514dab_buserr(struct trapframe *tf, u_int fsr, u_int far, struct thread *td, 515 struct ksig *ksig) 516{ 517 struct pcb *pcb = td->td_pcb; 518 519#ifdef __XSCALE__ 520 if ((fsr & FAULT_IMPRECISE) != 0 && 521 (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) { 522 /* 523 * Oops, an imprecise, double abort fault. We've lost the 524 * r14_abt/spsr_abt values corresponding to the original 525 * abort, and the spsr saved in the trapframe indicates 526 * ABT mode. 527 */ 528 tf->tf_spsr &= ~PSR_MODE; 529 530 /* 531 * We use a simple heuristic to determine if the double abort 532 * happened as a result of a kernel or user mode access. 533 * If the current trapframe is at the top of the kernel stack, 534 * the fault _must_ have come from user mode. 535 */ 536 if (tf != ((struct trapframe *)pcb->pcb_regs.sf_sp) - 1) { 537 /* 538 * Kernel mode. We're either about to die a 539 * spectacular death, or pcb_onfault will come 540 * to our rescue. Either way, the current value 541 * of tf->tf_pc is irrelevant. 542 */ 543 tf->tf_spsr |= PSR_SVC32_MODE; 544 if (pcb->pcb_onfault == NULL) 545 printf("\nKernel mode double abort!\n"); 546 } else { 547 /* 548 * User mode. We've lost the program counter at the 549 * time of the fault (not that it was accurate anyway; 550 * it's not called an imprecise fault for nothing). 551 * About all we can do is copy r14_usr to tf_pc and 552 * hope for the best. The process is about to get a 553 * SIGBUS, so it's probably history anyway. 554 */ 555 tf->tf_spsr |= PSR_USR32_MODE; 556 tf->tf_pc = tf->tf_usr_lr; 557 } 558 } 559 560 /* FAR is invalid for imprecise exceptions */ 561 if ((fsr & FAULT_IMPRECISE) != 0) 562 far = 0; 563#endif /* __XSCALE__ */ 564 565 if (pcb->pcb_onfault) { 566 tf->tf_r0 = EFAULT; 567 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault; 568 return (0); 569 } 570 571 /* 572 * At this point, if the fault happened in kernel mode, we're toast 573 */ 574 if (!TRAP_USERMODE(tf)) 575 dab_fatal(tf, fsr, far, td, ksig); 576 577 /* Deliver a bus error signal to the process */ 578 ksig->signb = SIGBUS; 579 ksig->code = 0; 580 td->td_frame = tf; 581 582 return (1); 583} 584 585/* 586 * void prefetch_abort_handler(struct trapframe *tf) 587 * 588 * Abort handler called when instruction execution occurs at 589 * a non existent or restricted (access permissions) memory page. 590 * If the address is invalid and we were in SVC mode then panic as 591 * the kernel should never prefetch abort. 592 * If the address is invalid and the page is mapped then the user process 593 * does no have read permission so send it a signal. 594 * Otherwise fault the page in and try again. 595 */ 596static void 597prefetch_abort_handler(struct trapframe *tf) 598{ 599 struct thread *td; 600 struct proc * p; 601 struct vm_map *map; 602 vm_offset_t fault_pc, va; 603 int error = 0; 604 struct ksig ksig; 605 606 607#if 0 608 /* Update vmmeter statistics */ 609 uvmexp.traps++; 610#endif 611#if 0 612 printf("prefetch abort handler: %p %p\n", (void*)tf->tf_pc, 613 (void*)tf->tf_usr_lr); 614#endif 615 616 td = curthread; 617 p = td->td_proc; 618 PCPU_INC(cnt.v_trap); 619 620 if (TRAP_USERMODE(tf)) { 621 td->td_frame = tf; 622 if (td->td_cowgen != td->td_proc->p_cowgen) 623 thread_cow_update(td); 624 } 625 fault_pc = tf->tf_pc; 626 if (td->td_md.md_spinlock_count == 0) { 627 if (__predict_true(tf->tf_spsr & PSR_I) == 0) 628 enable_interrupts(PSR_I); 629 if (__predict_true(tf->tf_spsr & PSR_F) == 0) 630 enable_interrupts(PSR_F); 631 } 632 633 /* Prefetch aborts cannot happen in kernel mode */ 634 if (__predict_false(!TRAP_USERMODE(tf))) 635 dab_fatal(tf, 0, tf->tf_pc, NULL, &ksig); 636 td->td_pticks = 0; 637 638 639 /* Ok validate the address, can only execute in USER space */ 640 if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS || 641 (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) { 642 ksig.signb = SIGSEGV; 643 ksig.code = 0; 644 goto do_trapsignal; 645 } 646 647 map = &td->td_proc->p_vmspace->vm_map; 648 va = trunc_page(fault_pc); 649 650 /* 651 * See if the pmap can handle this fault on its own... 652 */ 653#ifdef DEBUG 654 last_fault_code = -1; 655#endif 656 if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1)) 657 goto out; 658 659 error = vm_fault(map, va, VM_PROT_READ | VM_PROT_EXECUTE, 660 VM_FAULT_NORMAL); 661 if (__predict_true(error == 0)) 662 goto out; 663 664 if (error == ENOMEM) { 665 printf("VM: pid %d (%s), uid %d killed: " 666 "out of swap\n", td->td_proc->p_pid, td->td_name, 667 (td->td_proc->p_ucred) ? 668 td->td_proc->p_ucred->cr_uid : -1); 669 ksig.signb = SIGKILL; 670 } else { 671 ksig.signb = SIGSEGV; 672 } 673 ksig.code = 0; 674 675do_trapsignal: 676 call_trapsignal(td, ksig.signb, ksig.code); 677 678out: 679 userret(td, tf); 680 681} 682 683extern int badaddr_read_1(const uint8_t *, uint8_t *); 684extern int badaddr_read_2(const uint16_t *, uint16_t *); 685extern int badaddr_read_4(const uint32_t *, uint32_t *); 686/* 687 * Tentatively read an 8, 16, or 32-bit value from 'addr'. 688 * If the read succeeds, the value is written to 'rptr' and zero is returned. 689 * Else, return EFAULT. 690 */ 691int 692badaddr_read(void *addr, size_t size, void *rptr) 693{ 694 union { 695 uint8_t v1; 696 uint16_t v2; 697 uint32_t v4; 698 } u; 699 int rv; 700 701 cpu_drain_writebuf(); 702 703 /* Read from the test address. */ 704 switch (size) { 705 case sizeof(uint8_t): 706 rv = badaddr_read_1(addr, &u.v1); 707 if (rv == 0 && rptr) 708 *(uint8_t *) rptr = u.v1; 709 break; 710 711 case sizeof(uint16_t): 712 rv = badaddr_read_2(addr, &u.v2); 713 if (rv == 0 && rptr) 714 *(uint16_t *) rptr = u.v2; 715 break; 716 717 case sizeof(uint32_t): 718 rv = badaddr_read_4(addr, &u.v4); 719 if (rv == 0 && rptr) 720 *(uint32_t *) rptr = u.v4; 721 break; 722 723 default: 724 panic("badaddr: invalid size (%lu)", (u_long) size); 725 } 726 727 /* Return EFAULT if the address was invalid, else zero */ 728 return (rv); 729} 730