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