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