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