2 * Copyright (C) 1994, David Greenman
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * the University of Utah, and William Jolitz.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * 386 Trap and System call handling
47 #include "opt_clock.h"
49 #include "opt_hwpmc_hooks.h"
52 #include "opt_kdtrace.h"
53 #include "opt_ktrace.h"
57 #include <sys/param.h>
59 #include <sys/systm.h>
61 #include <sys/pioctl.h>
62 #include <sys/ptrace.h>
64 #include <sys/kernel.h>
67 #include <sys/mutex.h>
68 #include <sys/resourcevar.h>
69 #include <sys/signalvar.h>
70 #include <sys/syscall.h>
71 #include <sys/sysctl.h>
72 #include <sys/sysent.h>
74 #include <sys/vmmeter.h>
76 #include <sys/ktrace.h>
79 #include <sys/pmckern.h>
81 #include <security/audit/audit.h>
84 #include <vm/vm_param.h>
86 #include <vm/vm_kern.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_extern.h>
91 #include <machine/cpu.h>
92 #include <machine/intr_machdep.h>
93 #include <machine/mca.h>
94 #include <machine/md_var.h>
95 #include <machine/pcb.h>
97 #include <machine/smp.h>
99 #include <machine/tss.h>
100 #include <machine/vm86.h>
103 #include <sys/syslog.h>
104 #include <machine/clock.h>
108 #include <sys/dtrace_bsd.h>
111 * This is a hook which is initialised by the dtrace module
112 * to handle traps which might occur during DTrace probe
115 dtrace_trap_func_t dtrace_trap_func;
117 dtrace_doubletrap_func_t dtrace_doubletrap_func;
120 * This is a hook which is initialised by the systrace module
121 * when it is loaded. This keeps the DTrace syscall provider
122 * implementation opaque.
124 systrace_probe_func_t systrace_probe_func;
127 extern void trap(struct trapframe *frame);
128 extern void syscall(struct trapframe *frame);
130 static int trap_pfault(struct trapframe *, int, vm_offset_t);
131 static void trap_fatal(struct trapframe *, vm_offset_t);
132 void dblfault_handler(void);
134 extern inthand_t IDTVEC(lcall_syscall);
136 #define MAX_TRAP_MSG 30
137 static char *trap_msg[] = {
139 "privileged instruction fault", /* 1 T_PRIVINFLT */
141 "breakpoint instruction fault", /* 3 T_BPTFLT */
144 "arithmetic trap", /* 6 T_ARITHTRAP */
147 "general protection fault", /* 9 T_PROTFLT */
148 "trace trap", /* 10 T_TRCTRAP */
150 "page fault", /* 12 T_PAGEFLT */
152 "alignment fault", /* 14 T_ALIGNFLT */
156 "integer divide fault", /* 18 T_DIVIDE */
157 "non-maskable interrupt trap", /* 19 T_NMI */
158 "overflow trap", /* 20 T_OFLOW */
159 "FPU bounds check fault", /* 21 T_BOUND */
160 "FPU device not available", /* 22 T_DNA */
161 "double fault", /* 23 T_DOUBLEFLT */
162 "FPU operand fetch fault", /* 24 T_FPOPFLT */
163 "invalid TSS fault", /* 25 T_TSSFLT */
164 "segment not present fault", /* 26 T_SEGNPFLT */
165 "stack fault", /* 27 T_STKFLT */
166 "machine check trap", /* 28 T_MCHK */
167 "SIMD floating-point exception", /* 29 T_XMMFLT */
168 "reserved (unknown) fault", /* 30 T_RESERVED */
171 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
172 extern int has_f00f_bug;
176 static int kdb_on_nmi = 1;
177 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
178 &kdb_on_nmi, 0, "Go to KDB on NMI");
180 static int panic_on_nmi = 1;
181 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
182 &panic_on_nmi, 0, "Panic on NMI");
183 static int prot_fault_translation = 0;
184 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
185 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
187 extern char *syscallnames[];
190 * Exception, fault, and trap interface to the FreeBSD kernel.
191 * This common code is called from assembly language IDT gate entry
192 * routines that prepare a suitable stack frame, and restore this
193 * frame after the exception has been processed.
197 trap(struct trapframe *frame)
199 struct thread *td = curthread;
200 struct proc *p = td->td_proc;
201 int i = 0, ucode = 0, code;
207 static int lastalert = 0;
210 PCPU_INC(cnt.v_trap);
211 type = frame->tf_trapno;
215 /* Handler for NMI IPIs used for stopping CPUs. */
217 if (ipi_nmi_handler() == 0)
220 #endif /* STOP_NMI */
232 * CPU PMCs interrupt using an NMI so we check for that first.
233 * If the HWPMC module is active, 'pmc_hook' will point to
234 * the function to be called. A return value of '1' from the
235 * hook means that the NMI was handled by it and that we can
236 * return immediately.
238 if (type == T_NMI && pmc_intr &&
239 (*pmc_intr)(PCPU_GET(cpuid), frame))
243 if (type == T_MCHK) {
245 trap_fatal(frame, 0);
251 * A trap can occur while DTrace executes a probe. Before
252 * executing the probe, DTrace blocks re-scheduling and sets
253 * a flag in it's per-cpu flags to indicate that it doesn't
254 * want to fault. On returning from the the probe, the no-fault
255 * flag is cleared and finally re-scheduling is enabled.
257 * If the DTrace kernel module has registered a trap handler,
258 * call it and if it returns non-zero, assume that it has
259 * handled the trap and modified the trap frame so that this
260 * function can return normally.
262 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
263 dtrace_trap_func != NULL)
264 if ((*dtrace_trap_func)(frame, type))
268 if ((frame->tf_eflags & PSL_I) == 0) {
270 * Buggy application or kernel code has disabled
271 * interrupts and then trapped. Enabling interrupts
272 * now is wrong, but it is better than running with
273 * interrupts disabled until they are accidentally
276 if (ISPL(frame->tf_cs) == SEL_UPL || (frame->tf_eflags & PSL_VM))
278 "pid %ld (%s): trap %d with interrupts disabled\n",
279 (long)curproc->p_pid, curthread->td_name, type);
280 else if (type != T_BPTFLT && type != T_TRCTRAP &&
281 frame->tf_eip != (int)cpu_switch_load_gs) {
283 * XXX not quite right, since this may be for a
284 * multiple fault in user mode.
286 printf("kernel trap %d with interrupts disabled\n",
289 * Page faults need interrupts disabled until later,
290 * and we shouldn't enable interrupts while holding
291 * a spin lock or if servicing an NMI.
293 if (type != T_NMI && type != T_PAGEFLT &&
294 td->td_md.md_spinlock_count == 0)
299 code = frame->tf_err;
300 if (type == T_PAGEFLT) {
302 * For some Cyrix CPUs, %cr2 is clobbered by
303 * interrupts. This problem is worked around by using
304 * an interrupt gate for the pagefault handler. We
305 * are finally ready to read %cr2 and then must
306 * reenable interrupts.
308 * If we get a page fault while in a critical section, then
309 * it is most likely a fatal kernel page fault. The kernel
310 * is already going to panic trying to get a sleep lock to
311 * do the VM lookup, so just consider it a fatal trap so the
312 * kernel can print out a useful trap message and even get
315 * If we get a page fault while holding a non-sleepable
316 * lock, then it is most likely a fatal kernel page fault.
317 * If WITNESS is enabled, then it's going to whine about
318 * bogus LORs with various VM locks, so just skip to the
319 * fatal trap handling directly.
322 if (td->td_critnest != 0 ||
323 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
324 "Kernel page fault") != 0)
325 trap_fatal(frame, eva);
330 if ((ISPL(frame->tf_cs) == SEL_UPL) ||
331 ((frame->tf_eflags & PSL_VM) &&
332 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL))) {
336 td->td_frame = frame;
337 addr = frame->tf_eip;
338 if (td->td_ucred != p->p_ucred)
339 cred_update_thread(td);
342 case T_PRIVINFLT: /* privileged instruction fault */
347 case T_BPTFLT: /* bpt instruction fault */
348 case T_TRCTRAP: /* trace trap */
350 frame->tf_eflags &= ~PSL_T;
352 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
355 case T_ARITHTRAP: /* arithmetic trap */
367 * The following two traps can happen in
368 * vm86 mode, and, if so, we want to handle
371 case T_PROTFLT: /* general protection fault */
372 case T_STKFLT: /* stack fault */
373 if (frame->tf_eflags & PSL_VM) {
374 i = vm86_emulate((struct vm86frame *)frame);
380 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
382 case T_SEGNPFLT: /* segment not present fault */
386 case T_TSSFLT: /* invalid TSS fault */
390 case T_DOUBLEFLT: /* double fault */
396 case T_PAGEFLT: /* page fault */
398 i = trap_pfault(frame, TRUE, eva);
399 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
402 * The f00f hack workaround has triggered, so
403 * treat the fault as an illegal instruction
404 * (T_PRIVINFLT) instead of a page fault.
406 type = frame->tf_trapno = T_PRIVINFLT;
408 /* Proceed as in that case. */
422 if (prot_fault_translation == 0) {
425 * This check also covers the images
426 * without the ABI-tag ELF note.
428 if (p->p_osrel >= 700004) {
433 ucode = BUS_PAGE_FAULT;
435 } else if (prot_fault_translation == 1) {
437 * Always compat mode.
440 ucode = BUS_PAGE_FAULT;
443 * Always SIGSEGV mode.
452 case T_DIVIDE: /* integer divide fault */
461 # define TIMER_FREQ 1193182
463 if (time_second - lastalert > 10) {
464 log(LOG_WARNING, "NMI: power fail\n");
466 lastalert = time_second;
469 #else /* !POWERFAIL_NMI */
470 /* machine/parity/power fail/"kitchen sink" faults */
471 if (isa_nmi(code) == 0) {
474 * NMI can be hooked up to a pushbutton
478 printf ("NMI ... going to debugger\n");
479 kdb_trap(type, 0, frame);
483 } else if (panic_on_nmi)
484 panic("NMI indicates hardware failure");
486 #endif /* POWERFAIL_NMI */
489 case T_OFLOW: /* integer overflow fault */
494 case T_BOUND: /* bounds check fault */
501 /* transparent fault (due to context switch "late") */
505 printf("pid %d killed due to lack of floating point\n",
511 case T_FPOPFLT: /* FPU operand fetch fault */
516 case T_XMMFLT: /* SIMD floating-point exception */
524 KASSERT(cold || td->td_ucred != NULL,
525 ("kernel trap doesn't have ucred"));
527 case T_PAGEFLT: /* page fault */
528 (void) trap_pfault(frame, FALSE, eva);
534 * The kernel is apparently using npx for copying.
535 * XXX this should be fatal unless the kernel has
536 * registered such use.
544 * The following two traps can happen in
545 * vm86 mode, and, if so, we want to handle
548 case T_PROTFLT: /* general protection fault */
549 case T_STKFLT: /* stack fault */
550 if (frame->tf_eflags & PSL_VM) {
551 i = vm86_emulate((struct vm86frame *)frame);
554 * returns to original process
556 vm86_trap((struct vm86frame *)frame);
559 if (type == T_STKFLT)
564 case T_SEGNPFLT: /* segment not present fault */
565 if (PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)
569 * Invalid %fs's and %gs's can be created using
570 * procfs or PT_SETREGS or by invalidating the
571 * underlying LDT entry. This causes a fault
572 * in kernel mode when the kernel attempts to
573 * switch contexts. Lose the bad context
574 * (XXX) so that we can continue, and generate
577 if (frame->tf_eip == (int)cpu_switch_load_gs) {
578 PCPU_GET(curpcb)->pcb_gs = 0;
587 if (td->td_intr_nesting_level != 0)
591 * Invalid segment selectors and out of bounds
592 * %eip's and %esp's can be set up in user mode.
593 * This causes a fault in kernel mode when the
594 * kernel tries to return to user mode. We want
595 * to get this fault so that we can fix the
596 * problem here and not have to check all the
597 * selectors and pointers when the user changes
600 if (frame->tf_eip == (int)doreti_iret) {
601 frame->tf_eip = (int)doreti_iret_fault;
604 if (frame->tf_eip == (int)doreti_popl_ds) {
605 frame->tf_eip = (int)doreti_popl_ds_fault;
608 if (frame->tf_eip == (int)doreti_popl_es) {
609 frame->tf_eip = (int)doreti_popl_es_fault;
612 if (frame->tf_eip == (int)doreti_popl_fs) {
613 frame->tf_eip = (int)doreti_popl_fs_fault;
616 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
618 (int)PCPU_GET(curpcb)->pcb_onfault;
625 * PSL_NT can be set in user mode and isn't cleared
626 * automatically when the kernel is entered. This
627 * causes a TSS fault when the kernel attempts to
628 * `iret' because the TSS link is uninitialized. We
629 * want to get this fault so that we can fix the
630 * problem here and not every time the kernel is
633 if (frame->tf_eflags & PSL_NT) {
634 frame->tf_eflags &= ~PSL_NT;
639 case T_TRCTRAP: /* trace trap */
640 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
642 * We've just entered system mode via the
643 * syscall lcall. Continue single stepping
644 * silently until the syscall handler has
649 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
651 * The syscall handler has now saved the
652 * flags. Stop single stepping it.
654 frame->tf_eflags &= ~PSL_T;
658 * Ignore debug register trace traps due to
659 * accesses in the user's address space, which
660 * can happen under several conditions such as
661 * if a user sets a watchpoint on a buffer and
662 * then passes that buffer to a system call.
663 * We still want to get TRCTRAPS for addresses
664 * in kernel space because that is useful when
665 * debugging the kernel.
667 if (user_dbreg_trap() &&
668 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)) {
670 * Reset breakpoint bits because the
673 load_dr6(rdr6() & 0xfffffff0);
677 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
681 * If KDB is enabled, let it handle the debugger trap.
682 * Otherwise, debugger traps "can't happen".
685 if (kdb_trap(type, 0, frame))
693 if (time_second - lastalert > 10) {
694 log(LOG_WARNING, "NMI: power fail\n");
696 lastalert = time_second;
699 #else /* !POWERFAIL_NMI */
700 /* machine/parity/power fail/"kitchen sink" faults */
701 if (isa_nmi(code) == 0) {
704 * NMI can be hooked up to a pushbutton
708 printf ("NMI ... going to debugger\n");
709 kdb_trap(type, 0, frame);
713 } else if (panic_on_nmi == 0)
716 #endif /* POWERFAIL_NMI */
720 trap_fatal(frame, eva);
724 /* Translate fault for emulators (e.g. Linux) */
725 if (*p->p_sysent->sv_transtrap)
726 i = (*p->p_sysent->sv_transtrap)(i, type);
728 ksiginfo_init_trap(&ksi);
730 ksi.ksi_code = ucode;
731 ksi.ksi_addr = (void *)addr;
732 ksi.ksi_trapno = type;
733 trapsignal(td, &ksi);
736 if (type <= MAX_TRAP_MSG) {
737 uprintf("fatal process exception: %s",
739 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
740 uprintf(", fault VA = 0x%lx", (u_long)eva);
747 mtx_assert(&Giant, MA_NOTOWNED);
754 trap_pfault(frame, usermode, eva)
755 struct trapframe *frame;
760 struct vmspace *vm = NULL;
764 struct thread *td = curthread;
765 struct proc *p = td->td_proc;
767 va = trunc_page(eva);
768 if (va >= KERNBASE) {
770 * Don't allow user-mode faults in kernel address space.
771 * An exception: if the faulting address is the invalid
772 * instruction entry in the IDT, then the Intel Pentium
773 * F00F bug workaround was triggered, and we need to
774 * treat it is as an illegal instruction, and not a page
777 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
778 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
787 * This is a fault on non-kernel virtual memory.
788 * vm is initialized above to NULL. If curproc is NULL
789 * or curproc->p_vmspace is NULL the fault is fatal.
801 * PGEX_I is defined only if the execute disable bit capability is
802 * supported and enabled.
804 if (frame->tf_err & PGEX_W)
805 ftype = VM_PROT_WRITE;
807 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
808 ftype = VM_PROT_EXECUTE;
811 ftype = VM_PROT_READ;
813 if (map != kernel_map) {
815 * Keep swapout from messing with us during this
822 /* Fault in the user page: */
823 rv = vm_fault(map, va, ftype,
824 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
832 * Don't have to worry about process locking or stacks in the
835 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
837 if (rv == KERN_SUCCESS)
841 if (td->td_intr_nesting_level == 0 &&
842 PCPU_GET(curpcb)->pcb_onfault != NULL) {
843 frame->tf_eip = (int)PCPU_GET(curpcb)->pcb_onfault;
846 trap_fatal(frame, eva);
850 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
854 trap_fatal(frame, eva)
855 struct trapframe *frame;
860 struct soft_segment_descriptor softseg;
863 code = frame->tf_err;
864 type = frame->tf_trapno;
865 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
867 if (type <= MAX_TRAP_MSG)
868 msg = trap_msg[type];
871 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
872 frame->tf_eflags & PSL_VM ? "vm86" :
873 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
875 /* two separate prints in case of a trap on an unmapped page */
876 printf("cpuid = %d; ", PCPU_GET(cpuid));
877 printf("apic id = %02x\n", PCPU_GET(apic_id));
879 if (type == T_PAGEFLT) {
880 printf("fault virtual address = 0x%x\n", eva);
881 printf("fault code = %s %s, %s\n",
882 code & PGEX_U ? "user" : "supervisor",
883 code & PGEX_W ? "write" : "read",
884 code & PGEX_P ? "protection violation" : "page not present");
886 printf("instruction pointer = 0x%x:0x%x\n",
887 frame->tf_cs & 0xffff, frame->tf_eip);
888 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
889 ss = frame->tf_ss & 0xffff;
892 ss = GSEL(GDATA_SEL, SEL_KPL);
893 esp = (int)&frame->tf_esp;
895 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
896 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
897 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
898 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
899 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
900 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
902 printf("processor eflags = ");
903 if (frame->tf_eflags & PSL_T)
904 printf("trace trap, ");
905 if (frame->tf_eflags & PSL_I)
906 printf("interrupt enabled, ");
907 if (frame->tf_eflags & PSL_NT)
908 printf("nested task, ");
909 if (frame->tf_eflags & PSL_RF)
911 if (frame->tf_eflags & PSL_VM)
913 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
914 printf("current process = ");
916 printf("%lu (%s)\n", (u_long)curproc->p_pid, curthread->td_name);
922 if (debugger_on_panic || kdb_active) {
923 frame->tf_err = eva; /* smuggle fault address to ddb */
924 if (kdb_trap(type, 0, frame)) {
925 frame->tf_err = code; /* restore error code */
928 frame->tf_err = code; /* restore error code */
931 printf("trap number = %d\n", type);
932 if (type <= MAX_TRAP_MSG)
933 panic("%s", trap_msg[type]);
935 panic("unknown/reserved trap");
939 * Double fault handler. Called when a fault occurs while writing
940 * a frame for a trap/exception onto the stack. This usually occurs
941 * when the stack overflows (such is the case with infinite recursion,
944 * XXX Note that the current PTD gets replaced by IdlePTD when the
945 * task switch occurs. This means that the stack that was active at
946 * the time of the double fault is not available at <kstack> unless
947 * the machine was idle when the double fault occurred. The downside
948 * of this is that "trace <ebp>" in ddb won't work.
954 if (dtrace_doubletrap_func != NULL)
955 (*dtrace_doubletrap_func)();
957 printf("\nFatal double fault:\n");
958 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
959 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
960 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
962 /* two separate prints in case of a trap on an unmapped page */
963 printf("cpuid = %d; ", PCPU_GET(cpuid));
964 printf("apic id = %02x\n", PCPU_GET(apic_id));
966 panic("double fault");
970 * syscall - system call request C handler
972 * A system call is essentially treated as a trap.
975 syscall(struct trapframe *frame)
978 struct sysent *callp;
979 struct thread *td = curthread;
980 struct proc *p = td->td_proc;
981 register_t orig_tf_eflags;
988 PCPU_INC(cnt.v_syscall);
991 if (ISPL(frame->tf_cs) != SEL_UPL) {
998 td->td_frame = frame;
999 if (td->td_ucred != p->p_ucred)
1000 cred_update_thread(td);
1001 params = (caddr_t)frame->tf_esp + sizeof(int);
1002 code = frame->tf_eax;
1003 orig_tf_eflags = frame->tf_eflags;
1005 if (p->p_sysent->sv_prepsyscall) {
1006 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, ¶ms);
1009 * Need to check if this is a 32 bit or 64 bit syscall.
1011 if (code == SYS_syscall) {
1013 * Code is first argument, followed by actual args.
1015 code = fuword(params);
1016 params += sizeof(int);
1017 } else if (code == SYS___syscall) {
1019 * Like syscall, but code is a quad, so as to maintain
1020 * quad alignment for the rest of the arguments.
1022 code = fuword(params);
1023 params += sizeof(quad_t);
1027 if (p->p_sysent->sv_mask)
1028 code &= p->p_sysent->sv_mask;
1030 if (code >= p->p_sysent->sv_size)
1031 callp = &p->p_sysent->sv_table[0];
1033 callp = &p->p_sysent->sv_table[code];
1035 narg = callp->sy_narg;
1037 if (params != NULL && narg != 0)
1038 error = copyin(params, (caddr_t)args,
1039 (u_int)(narg * sizeof(int)));
1044 if (KTRPOINT(td, KTR_SYSCALL))
1045 ktrsyscall(code, narg, args);
1048 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
1049 td->td_proc->p_pid, td->td_name, code);
1054 td->td_retval[0] = 0;
1055 td->td_retval[1] = frame->tf_edx;
1057 STOPEVENT(p, S_SCE, narg);
1059 PTRACESTOP_SC(p, td, S_PT_SCE);
1061 #ifdef KDTRACE_HOOKS
1063 * If the systrace module has registered it's probe
1064 * callback and if there is a probe active for the
1065 * syscall 'entry', process the probe.
1067 if (systrace_probe_func != NULL && callp->sy_entry != 0)
1068 (*systrace_probe_func)(callp->sy_entry, code, callp,
1072 AUDIT_SYSCALL_ENTER(code, td);
1073 error = (*callp->sy_call)(td, args);
1074 AUDIT_SYSCALL_EXIT(error, td);
1076 /* Save the latest error return value. */
1077 td->td_errno = error;
1079 #ifdef KDTRACE_HOOKS
1081 * If the systrace module has registered it's probe
1082 * callback and if there is a probe active for the
1083 * syscall 'return', process the probe.
1085 if (systrace_probe_func != NULL && callp->sy_return != 0)
1086 (*systrace_probe_func)(callp->sy_return, code, callp,
1093 frame->tf_eax = td->td_retval[0];
1094 frame->tf_edx = td->td_retval[1];
1095 frame->tf_eflags &= ~PSL_C;
1100 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1101 * int 0x80 is 2 bytes. We saved this in tf_err.
1103 frame->tf_eip -= frame->tf_err;
1110 if (p->p_sysent->sv_errsize) {
1111 if (error >= p->p_sysent->sv_errsize)
1112 error = -1; /* XXX */
1114 error = p->p_sysent->sv_errtbl[error];
1116 frame->tf_eax = error;
1117 frame->tf_eflags |= PSL_C;
1124 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1125 frame->tf_eflags &= ~PSL_T;
1126 ksiginfo_init_trap(&ksi);
1127 ksi.ksi_signo = SIGTRAP;
1128 ksi.ksi_code = TRAP_TRACE;
1129 ksi.ksi_addr = (void *)frame->tf_eip;
1130 trapsignal(td, &ksi);
1134 * Check for misbehavior.
1136 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
1137 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
1138 KASSERT(td->td_critnest == 0,
1139 ("System call %s returning in a critical section",
1140 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
1141 KASSERT(td->td_locks == 0,
1142 ("System call %s returning with %d locks held",
1143 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
1147 * Handle reschedule and other end-of-syscall issues
1151 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
1152 td->td_proc->p_pid, td->td_name, code);
1155 if (KTRPOINT(td, KTR_SYSRET))
1156 ktrsysret(code, error, td->td_retval[0]);
1160 * This works because errno is findable through the
1161 * register set. If we ever support an emulation where this
1162 * is not the case, this code will need to be revisited.
1164 STOPEVENT(p, S_SCX, code);
1166 PTRACESTOP_SC(p, td, S_PT_SCX);