1 /* $NetBSD: fault.c,v 1.45 2003/11/20 14:44:36 scw Exp $ */
4 * Copyright 2004 Olivier Houchard
5 * Copyright 2003 Wasabi Systems, Inc.
8 * Written by Steve C. Woodford for Wasabi Systems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
39 * Copyright (c) 1994-1997 Mark Brinicombe.
40 * Copyright (c) 1994 Brini.
41 * All rights reserved.
43 * This code is derived from software written for Brini by Mark Brinicombe
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
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.
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
72 * RiscBSD kernel project
82 #include "opt_ktrace.h"
84 #include <sys/cdefs.h>
85 __FBSDID("$FreeBSD$");
87 #include <sys/param.h>
89 #include <sys/systm.h>
91 #include <sys/kernel.h>
93 #include <sys/mutex.h>
94 #include <sys/syscall.h>
95 #include <sys/sysent.h>
96 #include <sys/signalvar.h>
100 #include <sys/ktrace.h>
102 #include <sys/ptrace.h>
103 #include <sys/pioctl.h>
107 #include <vm/vm_kern.h>
108 #include <vm/vm_map.h>
109 #include <vm/vm_extern.h>
111 #include <machine/armreg.h>
112 #include <machine/cpuconf.h>
113 #include <machine/vmparam.h>
114 #include <machine/frame.h>
115 #include <machine/cpu.h>
116 #include <machine/intr.h>
117 #include <machine/pcb.h>
118 #include <machine/proc.h>
119 #include <machine/swi.h>
121 #include <security/audit/audit.h>
128 void swi_handler(struct trapframe *);
129 void undefinedinstruction(struct trapframe *);
131 #include <machine/disassem.h>
132 #include <machine/machdep.h>
134 extern char fusubailout[];
137 int last_fault_code; /* For the benefit of pmap_fault_fixup() */
140 #if defined(CPU_ARM7TDMI)
141 /* These CPUs may need data/prefetch abort fixups */
142 #define CPU_ABORT_FIXUP_REQUIRED
150 int (*func)(struct trapframe *, u_int, u_int, struct thread *,
155 static int dab_fatal(struct trapframe *, u_int, u_int, struct thread *,
157 static int dab_align(struct trapframe *, u_int, u_int, struct thread *,
159 static int dab_buserr(struct trapframe *, u_int, u_int, struct thread *,
162 static const struct data_abort data_aborts[] = {
163 {dab_fatal, "Vector Exception"},
164 {dab_align, "Alignment Fault 1"},
165 {dab_fatal, "Terminal Exception"},
166 {dab_align, "Alignment Fault 3"},
167 {dab_buserr, "External Linefetch Abort (S)"},
168 {NULL, "Translation Fault (S)"},
169 #if (ARM_MMU_V6 + ARM_MMU_V7) != 0
170 {NULL, "Translation Flag Fault"},
172 {dab_buserr, "External Linefetch Abort (P)"},
174 {NULL, "Translation Fault (P)"},
175 {dab_buserr, "External Non-Linefetch Abort (S)"},
176 {NULL, "Domain Fault (S)"},
177 {dab_buserr, "External Non-Linefetch Abort (P)"},
178 {NULL, "Domain Fault (P)"},
179 {dab_buserr, "External Translation Abort (L1)"},
180 {NULL, "Permission Fault (S)"},
181 {dab_buserr, "External Translation Abort (L2)"},
182 {NULL, "Permission Fault (P)"}
185 /* Determine if a fault came from user mode */
186 #define TRAP_USERMODE(tf) ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
188 /* Determine if 'x' is a permission fault */
189 #define IS_PERMISSION_FAULT(x) \
190 (((1 << ((x) & FAULT_TYPE_MASK)) & \
191 ((1 << FAULT_PERM_P) | (1 << FAULT_PERM_S))) != 0)
194 call_trapsignal(struct thread *td, int sig, u_long code)
198 ksiginfo_init_trap(&ksi);
200 ksi.ksi_code = (int)code;
201 trapsignal(td, &ksi);
205 data_abort_fixup(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
208 #ifdef CPU_ABORT_FIXUP_REQUIRED
211 /* Call the cpu specific data abort fixup routine */
212 error = cpu_dataabt_fixup(tf);
213 if (__predict_true(error != ABORT_FIXUP_FAILED))
217 * Oops, couldn't fix up the instruction
219 printf("data_abort_fixup: fixup for %s mode data abort failed.\n",
220 TRAP_USERMODE(tf) ? "user" : "kernel");
221 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
222 *((u_int *)tf->tf_pc));
223 disassemble(tf->tf_pc);
225 /* Die now if this happened in kernel mode */
226 if (!TRAP_USERMODE(tf))
227 dab_fatal(tf, fsr, far, td, NULL, ksig);
231 return (ABORT_FIXUP_OK);
232 #endif /* CPU_ABORT_FIXUP_REQUIRED */
236 data_abort_handler(struct trapframe *tf)
241 u_int user, far, fsr;
250 /* Grab FAR/FSR before enabling interrupts */
251 far = cpu_faultaddress();
252 fsr = cpu_faultstatus();
254 printf("data abort: fault address=%p (from pc=%p lr=%p)\n",
255 (void*)far, (void*)tf->tf_pc, (void*)tf->tf_svc_lr);
258 /* Update vmmeter statistics */
266 PCPU_INC(cnt.v_trap);
267 /* Data abort came from user mode? */
268 user = TRAP_USERMODE(tf);
273 if (td->td_ucred != td->td_proc->p_ucred)
274 cred_update_thread(td);
277 /* Grab the current pcb */
279 /* Re-enable interrupts if they were enabled previously */
280 if (td->td_md.md_spinlock_count == 0) {
281 if (__predict_true(tf->tf_spsr & I32_bit) == 0)
282 enable_interrupts(I32_bit);
283 if (__predict_true(tf->tf_spsr & F32_bit) == 0)
284 enable_interrupts(F32_bit);
288 /* Invoke the appropriate handler, if necessary */
289 if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) {
290 if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far,
298 * At this point, we're dealing with one of the following data aborts:
300 * FAULT_TRANS_S - Translation -- Section
301 * FAULT_TRANS_P - Translation -- Page
302 * FAULT_DOMAIN_S - Domain -- Section
303 * FAULT_DOMAIN_P - Domain -- Page
304 * FAULT_PERM_S - Permission -- Section
305 * FAULT_PERM_P - Permission -- Page
307 * These are the main virtual memory-related faults signalled by
311 /* fusubailout is used by [fs]uswintr to avoid page faulting */
312 if (__predict_false(pcb->pcb_onfault == fusubailout)) {
314 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
319 * Make sure the Program Counter is sane. We could fall foul of
320 * someone executing Thumb code, in which case the PC might not
321 * be word-aligned. This would cause a kernel alignment fault
322 * further down if we have to decode the current instruction.
323 * XXX: It would be nice to be able to support Thumb at some point.
325 if (__predict_false((tf->tf_pc & 3) != 0)) {
328 * Give the user an illegal instruction signal.
330 /* Deliver a SIGILL to the process */
337 * The kernel never executes Thumb code.
339 printf("\ndata_abort_fault: Misaligned Kernel-mode "
340 "Program Counter\n");
341 dab_fatal(tf, fsr, far, td, &ksig);
344 /* See if the cpu state needs to be fixed up */
345 switch (data_abort_fixup(tf, fsr, far, td, &ksig)) {
346 case ABORT_FIXUP_RETURN:
348 case ABORT_FIXUP_FAILED:
349 /* Deliver a SIGILL to the process */
357 va = trunc_page((vm_offset_t)far);
360 * It is only a kernel address space fault iff:
362 * 2. pcb_onfault not set or
363 * 3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction.
365 if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
366 (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) &&
367 __predict_true((pcb->pcb_onfault == NULL ||
368 (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) {
371 /* Was the fault due to the FPE/IPKDB ? */
372 if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
375 * Force exit via userret()
376 * This is necessary as the FPE is an extension to
377 * userland that actually runs in a priveledged mode
378 * but uses USR mode permissions for its accesses.
381 ksig.signb = SIGSEGV;
386 map = &td->td_proc->p_vmspace->vm_map;
390 * We need to know whether the page should be mapped as R or R/W. On
391 * armv6 and later the fault status register indicates whether the
392 * access was a read or write. Prior to armv6, we know that a
393 * permission fault can only be the result of a write to a read-only
394 * location, so we can deal with those quickly. Otherwise we need to
395 * disassemble the faulting instruction to determine if it was a write.
397 #if ARM_ARCH_6 || ARM_ARCH_7A
398 ftype = (fsr & FAULT_WNR) ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
400 if (IS_PERMISSION_FAULT(fsr))
401 ftype = VM_PROT_WRITE;
403 u_int insn = ReadWord(tf->tf_pc);
405 if (((insn & 0x0c100000) == 0x04000000) || /* STR/STRB */
406 ((insn & 0x0e1000b0) == 0x000000b0) || /* STRH/STRD */
407 ((insn & 0x0a100000) == 0x08000000)) { /* STM/CDT */
408 ftype = VM_PROT_WRITE;
410 if ((insn & 0x0fb00ff0) == 0x01000090) /* SWP */
411 ftype = VM_PROT_READ | VM_PROT_WRITE;
413 ftype = VM_PROT_READ;
419 * See if the fault is as a result of ref/mod emulation,
420 * or domain mismatch.
423 last_fault_code = fsr;
425 if (pmap_fault_fixup(vmspace_pmap(td->td_proc->p_vmspace), va, ftype,
430 onfault = pcb->pcb_onfault;
431 pcb->pcb_onfault = NULL;
432 if (map != kernel_map) {
437 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
438 pcb->pcb_onfault = onfault;
440 if (map != kernel_map) {
445 if (__predict_true(error == 0))
448 if (pcb->pcb_onfault) {
450 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
454 printf("\nvm_fault(%p, %x, %x, 0) -> %x\n", map, va, ftype,
456 dab_fatal(tf, fsr, far, td, &ksig);
460 if (error == ENOMEM) {
461 printf("VM: pid %d (%s), uid %d killed: "
462 "out of swap\n", td->td_proc->p_pid, td->td_name,
463 (td->td_proc->p_ucred) ?
464 td->td_proc->p_ucred->cr_uid : -1);
465 ksig.signb = SIGKILL;
467 ksig.signb = SIGSEGV;
471 call_trapsignal(td, ksig.signb, ksig.code);
473 /* If returning to user mode, make sure to invoke userret() */
479 * dab_fatal() handles the following data aborts:
481 * FAULT_WRTBUF_0 - Vector Exception
482 * FAULT_WRTBUF_1 - Terminal Exception
484 * We should never see these on a properly functioning system.
486 * This function is also called by the other handlers if they
487 * detect a fatal problem.
489 * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
492 dab_fatal(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
497 mode = TRAP_USERMODE(tf) ? "user" : "kernel";
499 disable_interrupts(I32_bit|F32_bit);
501 printf("Fatal %s mode data abort: '%s'\n", mode,
502 data_aborts[fsr & FAULT_TYPE_MASK].desc);
503 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr);
504 if ((fsr & FAULT_IMPRECISE) == 0)
505 printf("%08x, ", far);
508 printf("spsr=%08x\n", tf->tf_spsr);
510 printf("Fatal %s mode prefetch abort at 0x%08x\n",
512 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr);
515 printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n",
516 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3);
517 printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n",
518 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
519 printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n",
520 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
521 printf("r12=%08x, ", tf->tf_r12);
523 if (TRAP_USERMODE(tf))
524 printf("usp=%08x, ulr=%08x",
525 tf->tf_usr_sp, tf->tf_usr_lr);
527 printf("ssp=%08x, slr=%08x",
528 tf->tf_svc_sp, tf->tf_svc_lr);
529 printf(", pc =%08x\n\n", tf->tf_pc);
532 if (debugger_on_panic || kdb_active)
533 if (kdb_trap(fsr, 0, tf))
536 panic("Fatal abort");
541 * dab_align() handles the following data aborts:
543 * FAULT_ALIGN_0 - Alignment fault
544 * FAULT_ALIGN_1 - Alignment fault
546 * These faults are fatal if they happen in kernel mode. Otherwise, we
547 * deliver a bus error to the process.
550 dab_align(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
554 /* Alignment faults are always fatal if they occur in kernel mode */
555 if (!TRAP_USERMODE(tf)) {
556 if (!td || !td->td_pcb->pcb_onfault)
557 dab_fatal(tf, fsr, far, td, ksig);
559 tf->tf_pc = (int)td->td_pcb->pcb_onfault;
563 /* pcb_onfault *must* be NULL at this point */
565 /* See if the cpu state needs to be fixed up */
566 (void) data_abort_fixup(tf, fsr, far, td, ksig);
568 /* Deliver a bus error signal to the process */
570 ksig->signb = SIGBUS;
577 * dab_buserr() handles the following data aborts:
579 * FAULT_BUSERR_0 - External Abort on Linefetch -- Section
580 * FAULT_BUSERR_1 - External Abort on Linefetch -- Page
581 * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
582 * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
583 * FAULT_BUSTRNL1 - External abort on Translation -- Level 1
584 * FAULT_BUSTRNL2 - External abort on Translation -- Level 2
586 * If pcb_onfault is set, flag the fault and return to the handler.
587 * If the fault occurred in user mode, give the process a SIGBUS.
589 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
590 * can be flagged as imprecise in the FSR. This causes a real headache
591 * since some of the machine state is lost. In this case, tf->tf_pc
592 * may not actually point to the offending instruction. In fact, if
593 * we've taken a double abort fault, it generally points somewhere near
594 * the top of "data_abort_entry" in exception.S.
596 * In all other cases, these data aborts are considered fatal.
599 dab_buserr(struct trapframe *tf, u_int fsr, u_int far, struct thread *td,
602 struct pcb *pcb = td->td_pcb;
605 if ((fsr & FAULT_IMPRECISE) != 0 &&
606 (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
608 * Oops, an imprecise, double abort fault. We've lost the
609 * r14_abt/spsr_abt values corresponding to the original
610 * abort, and the spsr saved in the trapframe indicates
613 tf->tf_spsr &= ~PSR_MODE;
616 * We use a simple heuristic to determine if the double abort
617 * happened as a result of a kernel or user mode access.
618 * If the current trapframe is at the top of the kernel stack,
619 * the fault _must_ have come from user mode.
621 if (tf != ((struct trapframe *)pcb->un_32.pcb32_sp) - 1) {
623 * Kernel mode. We're either about to die a
624 * spectacular death, or pcb_onfault will come
625 * to our rescue. Either way, the current value
626 * of tf->tf_pc is irrelevant.
628 tf->tf_spsr |= PSR_SVC32_MODE;
629 if (pcb->pcb_onfault == NULL)
630 printf("\nKernel mode double abort!\n");
633 * User mode. We've lost the program counter at the
634 * time of the fault (not that it was accurate anyway;
635 * it's not called an imprecise fault for nothing).
636 * About all we can do is copy r14_usr to tf_pc and
637 * hope for the best. The process is about to get a
638 * SIGBUS, so it's probably history anyway.
640 tf->tf_spsr |= PSR_USR32_MODE;
641 tf->tf_pc = tf->tf_usr_lr;
645 /* FAR is invalid for imprecise exceptions */
646 if ((fsr & FAULT_IMPRECISE) != 0)
648 #endif /* __XSCALE__ */
650 if (pcb->pcb_onfault) {
652 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
656 /* See if the cpu state needs to be fixed up */
657 (void) data_abort_fixup(tf, fsr, far, td, ksig);
660 * At this point, if the fault happened in kernel mode, we're toast
662 if (!TRAP_USERMODE(tf))
663 dab_fatal(tf, fsr, far, td, ksig);
665 /* Deliver a bus error signal to the process */
666 ksig->signb = SIGBUS;
674 prefetch_abort_fixup(struct trapframe *tf, struct ksig *ksig)
676 #ifdef CPU_ABORT_FIXUP_REQUIRED
679 /* Call the cpu specific prefetch abort fixup routine */
680 error = cpu_prefetchabt_fixup(tf);
681 if (__predict_true(error != ABORT_FIXUP_FAILED))
685 * Oops, couldn't fix up the instruction
688 "prefetch_abort_fixup: fixup for %s mode prefetch abort failed.\n",
689 TRAP_USERMODE(tf) ? "user" : "kernel");
690 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
691 *((u_int *)tf->tf_pc));
692 disassemble(tf->tf_pc);
694 /* Die now if this happened in kernel mode */
695 if (!TRAP_USERMODE(tf))
696 dab_fatal(tf, 0, tf->tf_pc, NULL, ksig);
700 return (ABORT_FIXUP_OK);
701 #endif /* CPU_ABORT_FIXUP_REQUIRED */
705 * void prefetch_abort_handler(struct trapframe *tf)
707 * Abort handler called when instruction execution occurs at
708 * a non existent or restricted (access permissions) memory page.
709 * If the address is invalid and we were in SVC mode then panic as
710 * the kernel should never prefetch abort.
711 * If the address is invalid and the page is mapped then the user process
712 * does no have read permission so send it a signal.
713 * Otherwise fault the page in and try again.
716 prefetch_abort_handler(struct trapframe *tf)
721 vm_offset_t fault_pc, va;
727 /* Update vmmeter statistics */
731 printf("prefetch abort handler: %p %p\n", (void*)tf->tf_pc,
732 (void*)tf->tf_usr_lr);
737 PCPU_INC(cnt.v_trap);
739 if (TRAP_USERMODE(tf)) {
741 if (td->td_ucred != td->td_proc->p_ucred)
742 cred_update_thread(td);
744 fault_pc = tf->tf_pc;
745 if (td->td_md.md_spinlock_count == 0) {
746 if (__predict_true(tf->tf_spsr & I32_bit) == 0)
747 enable_interrupts(I32_bit);
748 if (__predict_true(tf->tf_spsr & F32_bit) == 0)
749 enable_interrupts(F32_bit);
752 /* See if the cpu state needs to be fixed up */
753 switch (prefetch_abort_fixup(tf, &ksig)) {
754 case ABORT_FIXUP_RETURN:
756 case ABORT_FIXUP_FAILED:
757 /* Deliver a SIGILL to the process */
766 /* Prefetch aborts cannot happen in kernel mode */
767 if (__predict_false(!TRAP_USERMODE(tf)))
768 dab_fatal(tf, 0, tf->tf_pc, NULL, &ksig);
772 /* Ok validate the address, can only execute in USER space */
773 if (__predict_false(fault_pc >= VM_MAXUSER_ADDRESS ||
774 (fault_pc < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW))) {
775 ksig.signb = SIGSEGV;
780 map = &td->td_proc->p_vmspace->vm_map;
781 va = trunc_page(fault_pc);
784 * See if the pmap can handle this fault on its own...
787 last_fault_code = -1;
789 if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1))
792 if (map != kernel_map) {
798 error = vm_fault(map, va, VM_PROT_READ | VM_PROT_EXECUTE,
800 if (map != kernel_map) {
806 if (__predict_true(error == 0))
809 if (error == ENOMEM) {
810 printf("VM: pid %d (%s), uid %d killed: "
811 "out of swap\n", td->td_proc->p_pid, td->td_name,
812 (td->td_proc->p_ucred) ?
813 td->td_proc->p_ucred->cr_uid : -1);
814 ksig.signb = SIGKILL;
816 ksig.signb = SIGSEGV;
821 call_trapsignal(td, ksig.signb, ksig.code);
828 extern int badaddr_read_1(const uint8_t *, uint8_t *);
829 extern int badaddr_read_2(const uint16_t *, uint16_t *);
830 extern int badaddr_read_4(const uint32_t *, uint32_t *);
832 * Tentatively read an 8, 16, or 32-bit value from 'addr'.
833 * If the read succeeds, the value is written to 'rptr' and zero is returned.
834 * Else, return EFAULT.
837 badaddr_read(void *addr, size_t size, void *rptr)
846 cpu_drain_writebuf();
848 /* Read from the test address. */
850 case sizeof(uint8_t):
851 rv = badaddr_read_1(addr, &u.v1);
853 *(uint8_t *) rptr = u.v1;
856 case sizeof(uint16_t):
857 rv = badaddr_read_2(addr, &u.v2);
859 *(uint16_t *) rptr = u.v2;
862 case sizeof(uint32_t):
863 rv = badaddr_read_4(addr, &u.v4);
865 *(uint32_t *) rptr = u.v4;
869 panic("badaddr: invalid size (%lu)", (u_long) size);
872 /* Return EFAULT if the address was invalid, else zero */
877 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
884 sa->code = td->td_frame->tf_r7;
886 sa->code = sa->insn & 0x000fffff;
888 ap = &td->td_frame->tf_r0;
889 if (sa->code == SYS_syscall) {
892 } else if (sa->code == SYS___syscall) {
893 sa->code = ap[_QUAD_LOWWORD];
898 if (p->p_sysent->sv_mask)
899 sa->code &= p->p_sysent->sv_mask;
900 if (sa->code >= p->p_sysent->sv_size)
901 sa->callp = &p->p_sysent->sv_table[0];
903 sa->callp = &p->p_sysent->sv_table[sa->code];
904 sa->narg = sa->callp->sy_narg;
906 memcpy(sa->args, ap, sa->nap * sizeof(register_t));
907 if (sa->narg > sa->nap) {
908 error = copyin((void *)td->td_frame->tf_usr_sp, sa->args +
909 sa->nap, (sa->narg - sa->nap) * sizeof(register_t));
912 td->td_retval[0] = 0;
913 td->td_retval[1] = 0;
918 #include "../../kern/subr_syscall.c"
921 syscall(struct thread *td, struct trapframe *frame)
923 struct syscall_args sa;
927 sa.insn = *(uint32_t *)(frame->tf_pc - INSN_SIZE);
928 switch (sa.insn & SWI_OS_MASK) {
929 case 0: /* XXX: we need our own one. */
932 call_trapsignal(td, SIGILL, 0);
939 error = syscallenter(td, &sa);
940 KASSERT(error != 0 || td->td_ar == NULL,
941 ("returning from syscall with td_ar set!"));
942 syscallret(td, error, &sa);
946 swi_handler(struct trapframe *frame)
948 struct thread *td = curthread;
950 td->td_frame = frame;
954 * Make sure the program counter is correctly aligned so we
955 * don't take an alignment fault trying to read the opcode.
957 if (__predict_false(((frame->tf_pc - INSN_SIZE) & 3) != 0)) {
958 call_trapsignal(td, SIGILL, 0);
963 * Enable interrupts if they were enabled before the exception.
964 * Since all syscalls *should* come from user mode it will always
965 * be safe to enable them, but check anyway.
967 if (td->td_md.md_spinlock_count == 0) {
968 if (__predict_true(frame->tf_spsr & I32_bit) == 0)
969 enable_interrupts(I32_bit);
970 if (__predict_true(frame->tf_spsr & F32_bit) == 0)
971 enable_interrupts(F32_bit);