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>
88 #include <sys/systm.h>
90 #include <sys/kernel.h>
92 #include <sys/mutex.h>
93 #include <sys/syscall.h>
94 #include <sys/sysent.h>
95 #include <sys/signalvar.h>
99 #include <sys/ktrace.h>
101 #include <sys/ptrace.h>
102 #include <sys/pioctl.h>
106 #include <vm/vm_kern.h>
107 #include <vm/vm_map.h>
108 #include <vm/vm_extern.h>
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>
119 #include <security/audit/audit.h>
126 void swi_handler(trapframe_t *);
127 void undefinedinstruction(trapframe_t *);
129 #include <machine/disassem.h>
130 #include <machine/machdep.h>
132 extern char fusubailout[];
133 extern char *syscallnames[];
136 int last_fault_code; /* For the benefit of pmap_fault_fixup() */
139 #if defined(CPU_ARM7TDMI)
140 /* These CPUs may need data/prefetch abort fixups */
141 #define CPU_ABORT_FIXUP_REQUIRED
149 int (*func)(trapframe_t *, u_int, u_int, struct thread *, struct ksig *);
153 static int dab_fatal(trapframe_t *, u_int, u_int, struct thread *, struct ksig *);
154 static int dab_align(trapframe_t *, u_int, u_int, struct thread *, struct ksig *);
155 static int dab_buserr(trapframe_t *, u_int, u_int, struct thread *, struct ksig *);
157 static 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)"}
176 /* Determine if a fault came from user mode */
177 #define TRAP_USERMODE(tf) ((tf->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
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)
185 call_trapsignal(struct thread *td, int sig, u_long code)
189 ksiginfo_init_trap(&ksi);
191 ksi.ksi_code = (int)code;
192 trapsignal(td, &ksi);
196 data_abort_fixup(trapframe_t *tf, u_int fsr, u_int far, struct thread *td, struct ksig *ksig)
198 #ifdef CPU_ABORT_FIXUP_REQUIRED
201 /* Call the cpu specific data abort fixup routine */
202 error = cpu_dataabt_fixup(tf);
203 if (__predict_true(error != ABORT_FIXUP_FAILED))
207 * Oops, couldn't fix up the instruction
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);
215 /* Die now if this happened in kernel mode */
216 if (!TRAP_USERMODE(tf))
217 dab_fatal(tf, fsr, far, td, NULL, ksig);
221 return (ABORT_FIXUP_OK);
222 #endif /* CPU_ABORT_FIXUP_REQUIRED */
226 data_abort_handler(trapframe_t *tf)
231 u_int user, far, fsr;
240 /* Grab FAR/FSR before enabling interrupts */
241 far = cpu_faultaddress();
242 fsr = cpu_faultstatus();
244 printf("data abort: %p (from %p %p)\n", (void*)far, (void*)tf->tf_pc,
245 (void*)tf->tf_svc_lr);
248 /* Update vmmeter statistics */
256 PCPU_INC(cnt.v_trap);
257 /* Data abort came from user mode? */
258 user = TRAP_USERMODE(tf);
263 if (td->td_ucred != td->td_proc->p_ucred)
264 cred_update_thread(td);
266 if (td->td_pflags & TDP_SA)
267 thread_user_enter(td);
271 /* Grab the current pcb */
273 /* Re-enable interrupts if they were enabled previously */
274 if (td->td_md.md_spinlock_count == 0) {
275 if (__predict_true(tf->tf_spsr & I32_bit) == 0)
276 enable_interrupts(I32_bit);
277 if (__predict_true(tf->tf_spsr & F32_bit) == 0)
278 enable_interrupts(F32_bit);
282 /* Invoke the appropriate handler, if necessary */
283 if (__predict_false(data_aborts[fsr & FAULT_TYPE_MASK].func != NULL)) {
284 if ((data_aborts[fsr & FAULT_TYPE_MASK].func)(tf, fsr, far,
292 * At this point, we're dealing with one of the following data aborts:
294 * FAULT_TRANS_S - Translation -- Section
295 * FAULT_TRANS_P - Translation -- Page
296 * FAULT_DOMAIN_S - Domain -- Section
297 * FAULT_DOMAIN_P - Domain -- Page
298 * FAULT_PERM_S - Permission -- Section
299 * FAULT_PERM_P - Permission -- Page
301 * These are the main virtual memory-related faults signalled by
305 /* fusubailout is used by [fs]uswintr to avoid page faulting */
306 if (__predict_false(pcb->pcb_onfault == fusubailout)) {
308 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
313 * Make sure the Program Counter is sane. We could fall foul of
314 * someone executing Thumb code, in which case the PC might not
315 * be word-aligned. This would cause a kernel alignment fault
316 * further down if we have to decode the current instruction.
317 * XXX: It would be nice to be able to support Thumb at some point.
319 if (__predict_false((tf->tf_pc & 3) != 0)) {
322 * Give the user an illegal instruction signal.
324 /* Deliver a SIGILL to the process */
331 * The kernel never executes Thumb code.
333 printf("\ndata_abort_fault: Misaligned Kernel-mode "
334 "Program Counter\n");
335 dab_fatal(tf, fsr, far, td, &ksig);
338 /* See if the cpu state needs to be fixed up */
339 switch (data_abort_fixup(tf, fsr, far, td, &ksig)) {
340 case ABORT_FIXUP_RETURN:
342 case ABORT_FIXUP_FAILED:
343 /* Deliver a SIGILL to the process */
351 va = trunc_page((vm_offset_t)far);
354 * It is only a kernel address space fault iff:
356 * 2. pcb_onfault not set or
357 * 3. pcb_onfault set and not LDRT/LDRBT/STRT/STRBT instruction.
359 if (user == 0 && (va >= VM_MIN_KERNEL_ADDRESS ||
360 (va < VM_MIN_ADDRESS && vector_page == ARM_VECTORS_LOW)) &&
361 __predict_true((pcb->pcb_onfault == NULL ||
362 (ReadWord(tf->tf_pc) & 0x05200000) != 0x04200000))) {
365 /* Was the fault due to the FPE/IPKDB ? */
366 if (__predict_false((tf->tf_spsr & PSR_MODE)==PSR_UND32_MODE)) {
369 * Force exit via userret()
370 * This is necessary as the FPE is an extension to
371 * userland that actually runs in a priveledged mode
372 * but uses USR mode permissions for its accesses.
375 ksig.signb = SIGSEGV;
380 map = &td->td_proc->p_vmspace->vm_map;
384 * We need to know whether the page should be mapped
385 * as R or R/W. The MMU does not give us the info as
386 * to whether the fault was caused by a read or a write.
388 * However, we know that a permission fault can only be
389 * the result of a write to a read-only location, so
390 * we can deal with those quickly.
392 * Otherwise we need to disassemble the instruction
393 * responsible to determine if it was a write.
395 if (IS_PERMISSION_FAULT(fsr)) {
396 ftype = VM_PROT_WRITE;
398 u_int insn = ReadWord(tf->tf_pc);
400 if (((insn & 0x0c100000) == 0x04000000) || /* STR/STRB */
401 ((insn & 0x0e1000b0) == 0x000000b0) || /* STRH/STRD */
402 ((insn & 0x0a100000) == 0x08000000)) /* STM/CDT */
404 ftype = VM_PROT_WRITE;
407 if ((insn & 0x0fb00ff0) == 0x01000090) /* SWP */
408 ftype = VM_PROT_READ | VM_PROT_WRITE;
410 ftype = VM_PROT_READ;
414 * See if the fault is as a result of ref/mod emulation,
415 * or domain mismatch.
418 last_fault_code = fsr;
420 if (pmap_fault_fixup(vmspace_pmap(td->td_proc->p_vmspace), va, ftype,
425 onfault = pcb->pcb_onfault;
426 pcb->pcb_onfault = NULL;
427 if (map != kernel_map) {
432 error = vm_fault(map, va, ftype, (ftype & VM_PROT_WRITE) ?
433 VM_FAULT_DIRTY : VM_FAULT_NORMAL);
434 pcb->pcb_onfault = onfault;
436 if (map != kernel_map) {
441 if (__predict_true(error == 0))
444 if (pcb->pcb_onfault) {
446 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
450 printf("\nvm_fault(%p, %x, %x, 0) -> %x\n", map, va, ftype,
452 dab_fatal(tf, fsr, far, td, &ksig);
456 if (error == ENOMEM) {
457 printf("VM: pid %d (%s), uid %d killed: "
458 "out of swap\n", td->td_proc->p_pid, td->td_proc->p_comm,
459 (td->td_proc->p_ucred) ?
460 td->td_proc->p_ucred->cr_uid : -1);
461 ksig.signb = SIGKILL;
463 ksig.signb = SIGSEGV;
467 call_trapsignal(td, ksig.signb, ksig.code);
469 /* If returning to user mode, make sure to invoke userret() */
475 * dab_fatal() handles the following data aborts:
477 * FAULT_WRTBUF_0 - Vector Exception
478 * FAULT_WRTBUF_1 - Terminal Exception
480 * We should never see these on a properly functioning system.
482 * This function is also called by the other handlers if they
483 * detect a fatal problem.
485 * Note: If 'l' is NULL, we assume we're dealing with a prefetch abort.
488 dab_fatal(trapframe_t *tf, u_int fsr, u_int far, struct thread *td, struct ksig *ksig)
492 mode = TRAP_USERMODE(tf) ? "user" : "kernel";
494 disable_interrupts(I32_bit|F32_bit);
496 printf("Fatal %s mode data abort: '%s'\n", mode,
497 data_aborts[fsr & FAULT_TYPE_MASK].desc);
498 printf("trapframe: %p\nFSR=%08x, FAR=", tf, fsr);
499 if ((fsr & FAULT_IMPRECISE) == 0)
500 printf("%08x, ", far);
503 printf("spsr=%08x\n", tf->tf_spsr);
505 printf("Fatal %s mode prefetch abort at 0x%08x\n",
507 printf("trapframe: %p, spsr=%08x\n", tf, tf->tf_spsr);
510 printf("r0 =%08x, r1 =%08x, r2 =%08x, r3 =%08x\n",
511 tf->tf_r0, tf->tf_r1, tf->tf_r2, tf->tf_r3);
512 printf("r4 =%08x, r5 =%08x, r6 =%08x, r7 =%08x\n",
513 tf->tf_r4, tf->tf_r5, tf->tf_r6, tf->tf_r7);
514 printf("r8 =%08x, r9 =%08x, r10=%08x, r11=%08x\n",
515 tf->tf_r8, tf->tf_r9, tf->tf_r10, tf->tf_r11);
516 printf("r12=%08x, ", tf->tf_r12);
518 if (TRAP_USERMODE(tf))
519 printf("usp=%08x, ulr=%08x",
520 tf->tf_usr_sp, tf->tf_usr_lr);
522 printf("ssp=%08x, slr=%08x",
523 tf->tf_svc_sp, tf->tf_svc_lr);
524 printf(", pc =%08x\n\n", tf->tf_pc);
527 kdb_trap(fsr, 0, tf);
529 panic("Fatal abort");
534 * dab_align() handles the following data aborts:
536 * FAULT_ALIGN_0 - Alignment fault
537 * FAULT_ALIGN_0 - Alignment fault
539 * These faults are fatal if they happen in kernel mode. Otherwise, we
540 * deliver a bus error to the process.
543 dab_align(trapframe_t *tf, u_int fsr, u_int far, struct thread *td, struct ksig *ksig)
546 /* Alignment faults are always fatal if they occur in kernel mode */
547 if (!TRAP_USERMODE(tf)) {
548 if (!td || !td->td_pcb->pcb_onfault)
549 dab_fatal(tf, fsr, far, td, ksig);
551 tf->tf_pc = (int)td->td_pcb->pcb_onfault;
555 /* pcb_onfault *must* be NULL at this point */
557 /* See if the cpu state needs to be fixed up */
558 (void) data_abort_fixup(tf, fsr, far, td, ksig);
560 /* Deliver a bus error signal to the process */
562 ksig->signb = SIGBUS;
569 * dab_buserr() handles the following data aborts:
571 * FAULT_BUSERR_0 - External Abort on Linefetch -- Section
572 * FAULT_BUSERR_1 - External Abort on Linefetch -- Page
573 * FAULT_BUSERR_2 - External Abort on Non-linefetch -- Section
574 * FAULT_BUSERR_3 - External Abort on Non-linefetch -- Page
575 * FAULT_BUSTRNL1 - External abort on Translation -- Level 1
576 * FAULT_BUSTRNL2 - External abort on Translation -- Level 2
578 * If pcb_onfault is set, flag the fault and return to the handler.
579 * If the fault occurred in user mode, give the process a SIGBUS.
581 * Note: On XScale, FAULT_BUSERR_0, FAULT_BUSERR_1, and FAULT_BUSERR_2
582 * can be flagged as imprecise in the FSR. This causes a real headache
583 * since some of the machine state is lost. In this case, tf->tf_pc
584 * may not actually point to the offending instruction. In fact, if
585 * we've taken a double abort fault, it generally points somewhere near
586 * the top of "data_abort_entry" in exception.S.
588 * In all other cases, these data aborts are considered fatal.
591 dab_buserr(trapframe_t *tf, u_int fsr, u_int far, struct thread *td, struct ksig *ksig)
593 struct pcb *pcb = td->td_pcb;
596 if ((fsr & FAULT_IMPRECISE) != 0 &&
597 (tf->tf_spsr & PSR_MODE) == PSR_ABT32_MODE) {
599 * Oops, an imprecise, double abort fault. We've lost the
600 * r14_abt/spsr_abt values corresponding to the original
601 * abort, and the spsr saved in the trapframe indicates
604 tf->tf_spsr &= ~PSR_MODE;
607 * We use a simple heuristic to determine if the double abort
608 * happened as a result of a kernel or user mode access.
609 * If the current trapframe is at the top of the kernel stack,
610 * the fault _must_ have come from user mode.
612 if (tf != ((trapframe_t *)pcb->un_32.pcb32_sp) - 1) {
614 * Kernel mode. We're either about to die a
615 * spectacular death, or pcb_onfault will come
616 * to our rescue. Either way, the current value
617 * of tf->tf_pc is irrelevant.
619 tf->tf_spsr |= PSR_SVC32_MODE;
620 if (pcb->pcb_onfault == NULL)
621 printf("\nKernel mode double abort!\n");
624 * User mode. We've lost the program counter at the
625 * time of the fault (not that it was accurate anyway;
626 * it's not called an imprecise fault for nothing).
627 * About all we can do is copy r14_usr to tf_pc and
628 * hope for the best. The process is about to get a
629 * SIGBUS, so it's probably history anyway.
631 tf->tf_spsr |= PSR_USR32_MODE;
632 tf->tf_pc = tf->tf_usr_lr;
636 /* FAR is invalid for imprecise exceptions */
637 if ((fsr & FAULT_IMPRECISE) != 0)
639 #endif /* __XSCALE__ */
641 if (pcb->pcb_onfault) {
643 tf->tf_pc = (register_t)(intptr_t) pcb->pcb_onfault;
647 /* See if the cpu state needs to be fixed up */
648 (void) data_abort_fixup(tf, fsr, far, td, ksig);
651 * At this point, if the fault happened in kernel mode, we're toast
653 if (!TRAP_USERMODE(tf))
654 dab_fatal(tf, fsr, far, td, ksig);
656 /* Deliver a bus error signal to the process */
657 ksig->signb = SIGBUS;
665 prefetch_abort_fixup(trapframe_t *tf, struct ksig *ksig)
667 #ifdef CPU_ABORT_FIXUP_REQUIRED
670 /* Call the cpu specific prefetch abort fixup routine */
671 error = cpu_prefetchabt_fixup(tf);
672 if (__predict_true(error != ABORT_FIXUP_FAILED))
676 * Oops, couldn't fix up the instruction
679 "prefetch_abort_fixup: fixup for %s mode prefetch abort failed.\n",
680 TRAP_USERMODE(tf) ? "user" : "kernel");
681 printf("pc = 0x%08x, opcode 0x%08x, insn = ", tf->tf_pc,
682 *((u_int *)tf->tf_pc));
683 disassemble(tf->tf_pc);
685 /* Die now if this happened in kernel mode */
686 if (!TRAP_USERMODE(tf))
687 dab_fatal(tf, 0, tf->tf_pc, NULL, ksig);
691 return (ABORT_FIXUP_OK);
692 #endif /* CPU_ABORT_FIXUP_REQUIRED */
696 * void prefetch_abort_handler(trapframe_t *tf)
698 * Abort handler called when instruction execution occurs at
699 * a non existent or restricted (access permissions) memory page.
700 * If the address is invalid and we were in SVC mode then panic as
701 * the kernel should never prefetch abort.
702 * If the address is invalid and the page is mapped then the user process
703 * does no have read permission so send it a signal.
704 * Otherwise fault the page in and try again.
707 prefetch_abort_handler(trapframe_t *tf)
712 vm_offset_t fault_pc, va;
718 /* Update vmmeter statistics */
722 printf("prefetch abort handler: %p %p\n", (void*)tf->tf_pc,
723 (void*)tf->tf_usr_lr);
728 PCPU_INC(cnt.v_trap);
730 if (TRAP_USERMODE(tf)) {
732 if (td->td_ucred != td->td_proc->p_ucred)
733 cred_update_thread(td);
735 if (td->td_proc->p_flag & P_SA)
736 thread_user_enter(td);
739 fault_pc = tf->tf_pc;
740 if (td->td_md.md_spinlock_count == 0) {
741 if (__predict_true(tf->tf_spsr & I32_bit) == 0)
742 enable_interrupts(I32_bit);
743 if (__predict_true(tf->tf_spsr & F32_bit) == 0)
744 enable_interrupts(F32_bit);
749 /* See if the cpu state needs to be fixed up */
750 switch (prefetch_abort_fixup(tf, &ksig)) {
751 case ABORT_FIXUP_RETURN:
753 case ABORT_FIXUP_FAILED:
754 /* Deliver a SIGILL to the process */
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);
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;
777 map = &td->td_proc->p_vmspace->vm_map;
778 va = trunc_page(fault_pc);
781 * See if the pmap can handle this fault on its own...
784 last_fault_code = -1;
786 if (pmap_fault_fixup(map->pmap, va, VM_PROT_READ, 1))
789 if (map != kernel_map) {
795 error = vm_fault(map, va, VM_PROT_READ | VM_PROT_EXECUTE,
797 if (map != kernel_map) {
803 if (__predict_true(error == 0))
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_proc->p_comm,
809 (td->td_proc->p_ucred) ?
810 td->td_proc->p_ucred->cr_uid : -1);
811 ksig.signb = SIGKILL;
813 ksig.signb = SIGSEGV;
818 call_trapsignal(td, ksig.signb, ksig.code);
825 extern int badaddr_read_1(const uint8_t *, uint8_t *);
826 extern int badaddr_read_2(const uint16_t *, uint16_t *);
827 extern int badaddr_read_4(const uint32_t *, uint32_t *);
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.
834 badaddr_read(void *addr, size_t size, void *rptr)
843 cpu_drain_writebuf();
845 /* Read from the test address. */
847 case sizeof(uint8_t):
848 rv = badaddr_read_1(addr, &u.v1);
850 *(uint8_t *) rptr = u.v1;
853 case sizeof(uint16_t):
854 rv = badaddr_read_2(addr, &u.v2);
856 *(uint16_t *) rptr = u.v2;
859 case sizeof(uint32_t):
860 rv = badaddr_read_4(addr, &u.v4);
862 *(uint32_t *) rptr = u.v4;
866 panic("badaddr: invalid size (%lu)", (u_long) size);
869 /* Return EFAULT if the address was invalid, else zero */
875 syscall(struct thread *td, trapframe_t *frame, u_int32_t insn)
877 struct proc *p = td->td_proc;
880 register_t *ap, *args, copyargs[MAXARGS];
881 struct sysent *callp;
883 PCPU_INC(cnt.v_syscall);
885 if (td->td_ucred != td->td_proc->p_ucred)
886 cred_update_thread(td);
887 switch (insn & SWI_OS_MASK) {
888 case 0: /* XXX: we need our own one. */
892 call_trapsignal(td, SIGILL, 0);
896 code = insn & 0x000fffff;
899 if (code == SYS_syscall) {
903 } else if (code == SYS___syscall) {
904 code = ap[_QUAD_LOWWORD];
908 if (p->p_sysent->sv_mask)
909 code &= p->p_sysent->sv_mask;
910 if (code >= p->p_sysent->sv_size)
911 callp = &p->p_sysent->sv_table[0];
913 callp = &p->p_sysent->sv_table[code];
914 nargs = callp->sy_narg;
915 memcpy(copyargs, ap, nap * sizeof(register_t));
917 error = copyin((void *)frame->tf_usr_sp, copyargs + nap,
918 (nargs - nap) * sizeof(register_t));
925 if (KTRPOINT(td, KTR_SYSCALL))
926 ktrsyscall(code, nargs, args);
929 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
930 td->td_proc->p_pid, td->td_proc->p_comm, code);
932 td->td_retval[0] = 0;
933 td->td_retval[1] = 0;
934 STOPEVENT(p, S_SCE, callp->sy_narg);
935 PTRACESTOP_SC(p, td, S_PT_SCE);
936 AUDIT_SYSCALL_ENTER(code, td);
937 error = (*callp->sy_call)(td, args);
938 AUDIT_SYSCALL_EXIT(error, td);
939 KASSERT(td->td_ar == NULL,
940 ("returning from syscall with td_ar set!"));
945 if ((insn & 0x000fffff) == SYS___syscall &&
946 code != SYS_freebsd6_lseek && code != SYS_lseek) {
948 * 64-bit return, 32-bit syscall. Fixup byte order
951 frame->tf_r1 = td->td_retval[0];
953 frame->tf_r0 = td->td_retval[0];
954 frame->tf_r1 = td->td_retval[1];
957 frame->tf_r0 = td->td_retval[0];
958 frame->tf_r1 = td->td_retval[1];
961 frame->tf_spsr &= ~PSR_C_bit; /* carry bit */
966 * Reconstruct the pc to point at the swi.
968 frame->tf_pc -= INSN_SIZE;
975 frame->tf_r0 = error;
976 frame->tf_spsr |= PSR_C_bit; /* carry bit */
980 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
981 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
982 KASSERT(td->td_critnest == 0,
983 ("System call %s returning in a critical section",
984 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
985 KASSERT(td->td_locks == 0,
986 ("System call %s returning with %d locks held",
987 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
991 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
992 td->td_proc->p_pid, td->td_proc->p_comm, code);
994 STOPEVENT(p, S_SCX, code);
995 PTRACESTOP_SC(p, td, S_PT_SCX);
997 if (KTRPOINT(td, KTR_SYSRET))
998 ktrsysret(code, error, td->td_retval[0]);
1003 swi_handler(trapframe_t *frame)
1005 struct thread *td = curthread;
1008 td->td_frame = frame;
1012 if (td->td_proc->p_flag & P_SA)
1013 thread_user_enter(td);
1016 * Make sure the program counter is correctly aligned so we
1017 * don't take an alignment fault trying to read the opcode.
1019 if (__predict_false(((frame->tf_pc - INSN_SIZE) & 3) != 0)) {
1020 call_trapsignal(td, SIGILL, 0);
1024 insn = *(u_int32_t *)(frame->tf_pc - INSN_SIZE);
1026 * Enable interrupts if they were enabled before the exception.
1027 * Since all syscalls *should* come from user mode it will always
1028 * be safe to enable them, but check anyway.
1030 if (td->td_md.md_spinlock_count == 0) {
1031 if (__predict_true(frame->tf_spsr & I32_bit) == 0)
1032 enable_interrupts(I32_bit);
1033 if (__predict_true(frame->tf_spsr & F32_bit) == 0)
1034 enable_interrupts(F32_bit);
1037 syscall(td, frame, insn);