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