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/md_var.h>
94 #include <machine/pcb.h>
96 #include <machine/smp.h>
98 #include <machine/tss.h>
99 #include <machine/vm86.h>
102 #include <sys/syslog.h>
103 #include <machine/clock.h>
107 #include <sys/dtrace_bsd.h>
110 * This is a hook which is initialised by the dtrace module
111 * to handle traps which might occur during DTrace probe
114 dtrace_trap_func_t dtrace_trap_func;
116 dtrace_doubletrap_func_t dtrace_doubletrap_func;
119 * This is a hook which is initialised by the systrace module
120 * when it is loaded. This keeps the DTrace syscall provider
121 * implementation opaque.
123 systrace_probe_func_t systrace_probe_func;
126 extern void trap(struct trapframe *frame);
127 extern void syscall(struct trapframe *frame);
129 static int trap_pfault(struct trapframe *, int, vm_offset_t);
130 static void trap_fatal(struct trapframe *, vm_offset_t);
131 void dblfault_handler(void);
133 extern inthand_t IDTVEC(lcall_syscall);
135 #define MAX_TRAP_MSG 30
136 static char *trap_msg[] = {
138 "privileged instruction fault", /* 1 T_PRIVINFLT */
140 "breakpoint instruction fault", /* 3 T_BPTFLT */
143 "arithmetic trap", /* 6 T_ARITHTRAP */
146 "general protection fault", /* 9 T_PROTFLT */
147 "trace trap", /* 10 T_TRCTRAP */
149 "page fault", /* 12 T_PAGEFLT */
151 "alignment fault", /* 14 T_ALIGNFLT */
155 "integer divide fault", /* 18 T_DIVIDE */
156 "non-maskable interrupt trap", /* 19 T_NMI */
157 "overflow trap", /* 20 T_OFLOW */
158 "FPU bounds check fault", /* 21 T_BOUND */
159 "FPU device not available", /* 22 T_DNA */
160 "double fault", /* 23 T_DOUBLEFLT */
161 "FPU operand fetch fault", /* 24 T_FPOPFLT */
162 "invalid TSS fault", /* 25 T_TSSFLT */
163 "segment not present fault", /* 26 T_SEGNPFLT */
164 "stack fault", /* 27 T_STKFLT */
165 "machine check trap", /* 28 T_MCHK */
166 "SIMD floating-point exception", /* 29 T_XMMFLT */
167 "reserved (unknown) fault", /* 30 T_RESERVED */
170 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
171 extern int has_f00f_bug;
175 static int kdb_on_nmi = 1;
176 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
177 &kdb_on_nmi, 0, "Go to KDB on NMI");
179 static int panic_on_nmi = 1;
180 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
181 &panic_on_nmi, 0, "Panic on NMI");
182 static int prot_fault_translation = 0;
183 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
184 &prot_fault_translation, 0, "Select signal to deliver on protection fault");
186 extern char *syscallnames[];
189 * Exception, fault, and trap interface to the FreeBSD kernel.
190 * This common code is called from assembly language IDT gate entry
191 * routines that prepare a suitable stack frame, and restore this
192 * frame after the exception has been processed.
196 trap(struct trapframe *frame)
198 struct thread *td = curthread;
199 struct proc *p = td->td_proc;
200 int i = 0, ucode = 0, code;
206 static int lastalert = 0;
209 PCPU_INC(cnt.v_trap);
210 type = frame->tf_trapno;
214 /* Handler for NMI IPIs used for stopping CPUs. */
216 if (ipi_nmi_handler() == 0)
219 #endif /* STOP_NMI */
231 * CPU PMCs interrupt using an NMI so we check for that first.
232 * If the HWPMC module is active, 'pmc_hook' will point to
233 * the function to be called. A return value of '1' from the
234 * hook means that the NMI was handled by it and that we can
235 * return immediately.
237 if (type == T_NMI && pmc_intr &&
238 (*pmc_intr)(PCPU_GET(cpuid), (uintptr_t) frame->tf_eip,
239 TRAPF_USERMODE(frame)))
245 * A trap can occur while DTrace executes a probe. Before
246 * executing the probe, DTrace blocks re-scheduling and sets
247 * a flag in it's per-cpu flags to indicate that it doesn't
248 * want to fault. On returning from the the probe, the no-fault
249 * flag is cleared and finally re-scheduling is enabled.
251 * If the DTrace kernel module has registered a trap handler,
252 * call it and if it returns non-zero, assume that it has
253 * handled the trap and modified the trap frame so that this
254 * function can return normally.
256 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
257 dtrace_trap_func != NULL)
258 if ((*dtrace_trap_func)(frame, type))
262 if ((frame->tf_eflags & PSL_I) == 0) {
264 * Buggy application or kernel code has disabled
265 * interrupts and then trapped. Enabling interrupts
266 * now is wrong, but it is better than running with
267 * interrupts disabled until they are accidentally
270 if (ISPL(frame->tf_cs) == SEL_UPL || (frame->tf_eflags & PSL_VM))
272 "pid %ld (%s): trap %d with interrupts disabled\n",
273 (long)curproc->p_pid, curproc->p_comm, type);
274 else if (type != T_BPTFLT && type != T_TRCTRAP &&
275 frame->tf_eip != (int)cpu_switch_load_gs) {
277 * XXX not quite right, since this may be for a
278 * multiple fault in user mode.
280 printf("kernel trap %d with interrupts disabled\n",
283 * Page faults need interrupts disabled until later,
284 * and we shouldn't enable interrupts while holding
285 * a spin lock or if servicing an NMI.
287 if (type != T_NMI && type != T_PAGEFLT &&
288 td->td_md.md_spinlock_count == 0)
294 code = frame->tf_err;
295 if (type == T_PAGEFLT) {
297 * For some Cyrix CPUs, %cr2 is clobbered by
298 * interrupts. This problem is worked around by using
299 * an interrupt gate for the pagefault handler. We
300 * are finally ready to read %cr2 and then must
301 * reenable interrupts.
303 * If we get a page fault while in a critical section, then
304 * it is most likely a fatal kernel page fault. The kernel
305 * is already going to panic trying to get a sleep lock to
306 * do the VM lookup, so just consider it a fatal trap so the
307 * kernel can print out a useful trap message and even get
310 * If we get a page fault while holding a non-sleepable
311 * lock, then it is most likely a fatal kernel page fault.
312 * If WITNESS is enabled, then it's going to whine about
313 * bogus LORs with various VM locks, so just skip to the
314 * fatal trap handling directly.
317 if (td->td_critnest != 0 ||
318 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
319 "Kernel page fault") != 0)
320 trap_fatal(frame, eva);
325 if ((ISPL(frame->tf_cs) == SEL_UPL) ||
326 ((frame->tf_eflags & PSL_VM) &&
327 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL))) {
331 td->td_frame = frame;
332 addr = frame->tf_eip;
333 if (td->td_ucred != p->p_ucred)
334 cred_update_thread(td);
337 case T_PRIVINFLT: /* privileged instruction fault */
342 case T_BPTFLT: /* bpt instruction fault */
343 case T_TRCTRAP: /* trace trap */
345 frame->tf_eflags &= ~PSL_T;
347 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
350 case T_ARITHTRAP: /* arithmetic trap */
362 * The following two traps can happen in
363 * vm86 mode, and, if so, we want to handle
366 case T_PROTFLT: /* general protection fault */
367 case T_STKFLT: /* stack fault */
368 if (frame->tf_eflags & PSL_VM) {
369 i = vm86_emulate((struct vm86frame *)frame);
375 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
377 case T_SEGNPFLT: /* segment not present fault */
381 case T_TSSFLT: /* invalid TSS fault */
385 case T_DOUBLEFLT: /* double fault */
391 case T_PAGEFLT: /* page fault */
393 if (td->td_pflags & TDP_SA)
394 thread_user_enter(td);
397 i = trap_pfault(frame, TRUE, eva);
398 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
401 * The f00f hack workaround has triggered, so
402 * treat the fault as an illegal instruction
403 * (T_PRIVINFLT) instead of a page fault.
405 type = frame->tf_trapno = T_PRIVINFLT;
407 /* Proceed as in that case. */
421 if (prot_fault_translation == 0) {
424 * This check also covers the images
425 * without the ABI-tag ELF note.
427 if (p->p_osrel >= 700004) {
432 ucode = BUS_PAGE_FAULT;
434 } else if (prot_fault_translation == 1) {
436 * Always compat mode.
439 ucode = BUS_PAGE_FAULT;
442 * Always SIGSEGV mode.
451 case T_DIVIDE: /* integer divide fault */
460 # define TIMER_FREQ 1193182
463 if (time_second - lastalert > 10) {
464 log(LOG_WARNING, "NMI: power fail\n");
465 sysbeep(TIMER_FREQ/880, hz);
466 lastalert = time_second;
470 #else /* !POWERFAIL_NMI */
471 /* machine/parity/power fail/"kitchen sink" faults */
473 if (isa_nmi(code) == 0) {
476 * NMI can be hooked up to a pushbutton
480 printf ("NMI ... going to debugger\n");
481 kdb_trap(type, 0, frame);
485 } else if (panic_on_nmi)
486 panic("NMI indicates hardware failure");
488 #endif /* POWERFAIL_NMI */
491 case T_OFLOW: /* integer overflow fault */
496 case T_BOUND: /* bounds check fault */
503 /* transparent fault (due to context switch "late") */
507 printf("pid %d killed due to lack of floating point\n",
513 case T_FPOPFLT: /* FPU operand fetch fault */
518 case T_XMMFLT: /* SIMD floating-point exception */
526 KASSERT(cold || td->td_ucred != NULL,
527 ("kernel trap doesn't have ucred"));
529 case T_PAGEFLT: /* page fault */
530 (void) trap_pfault(frame, FALSE, eva);
536 * The kernel is apparently using npx for copying.
537 * XXX this should be fatal unless the kernel has
538 * registered such use.
546 * The following two traps can happen in
547 * vm86 mode, and, if so, we want to handle
550 case T_PROTFLT: /* general protection fault */
551 case T_STKFLT: /* stack fault */
552 if (frame->tf_eflags & PSL_VM) {
553 i = vm86_emulate((struct vm86frame *)frame);
556 * returns to original process
558 vm86_trap((struct vm86frame *)frame);
561 if (type == T_STKFLT)
566 case T_SEGNPFLT: /* segment not present fault */
567 if (PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)
571 * Invalid %fs's and %gs's can be created using
572 * procfs or PT_SETREGS or by invalidating the
573 * underlying LDT entry. This causes a fault
574 * in kernel mode when the kernel attempts to
575 * switch contexts. Lose the bad context
576 * (XXX) so that we can continue, and generate
579 if (frame->tf_eip == (int)cpu_switch_load_gs) {
580 PCPU_GET(curpcb)->pcb_gs = 0;
589 if (td->td_intr_nesting_level != 0)
593 * Invalid segment selectors and out of bounds
594 * %eip's and %esp's can be set up in user mode.
595 * This causes a fault in kernel mode when the
596 * kernel tries to return to user mode. We want
597 * to get this fault so that we can fix the
598 * problem here and not have to check all the
599 * selectors and pointers when the user changes
602 if (frame->tf_eip == (int)doreti_iret) {
603 frame->tf_eip = (int)doreti_iret_fault;
606 if (frame->tf_eip == (int)doreti_popl_ds) {
607 frame->tf_eip = (int)doreti_popl_ds_fault;
610 if (frame->tf_eip == (int)doreti_popl_es) {
611 frame->tf_eip = (int)doreti_popl_es_fault;
614 if (frame->tf_eip == (int)doreti_popl_fs) {
615 frame->tf_eip = (int)doreti_popl_fs_fault;
618 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
620 (int)PCPU_GET(curpcb)->pcb_onfault;
627 * PSL_NT can be set in user mode and isn't cleared
628 * automatically when the kernel is entered. This
629 * causes a TSS fault when the kernel attempts to
630 * `iret' because the TSS link is uninitialized. We
631 * want to get this fault so that we can fix the
632 * problem here and not every time the kernel is
635 if (frame->tf_eflags & PSL_NT) {
636 frame->tf_eflags &= ~PSL_NT;
641 case T_TRCTRAP: /* trace trap */
642 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
644 * We've just entered system mode via the
645 * syscall lcall. Continue single stepping
646 * silently until the syscall handler has
651 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
653 * The syscall handler has now saved the
654 * flags. Stop single stepping it.
656 frame->tf_eflags &= ~PSL_T;
660 * Ignore debug register trace traps due to
661 * accesses in the user's address space, which
662 * can happen under several conditions such as
663 * if a user sets a watchpoint on a buffer and
664 * then passes that buffer to a system call.
665 * We still want to get TRCTRAPS for addresses
666 * in kernel space because that is useful when
667 * debugging the kernel.
670 if (user_dbreg_trap() &&
671 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)) {
673 * Reset breakpoint bits because the
676 load_dr6(rdr6() & 0xfffffff0);
680 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
684 * If KDB is enabled, let it handle the debugger trap.
685 * Otherwise, debugger traps "can't happen".
688 if (kdb_trap(type, 0, frame))
697 if (time_second - lastalert > 10) {
698 log(LOG_WARNING, "NMI: power fail\n");
699 sysbeep(TIMER_FREQ/880, hz);
700 lastalert = time_second;
704 #else /* !POWERFAIL_NMI */
706 /* machine/parity/power fail/"kitchen sink" faults */
707 if (isa_nmi(code) == 0) {
710 * NMI can be hooked up to a pushbutton
714 printf ("NMI ... going to debugger\n");
715 kdb_trap(type, 0, frame);
719 } else if (panic_on_nmi == 0)
722 #endif /* POWERFAIL_NMI */
726 trap_fatal(frame, eva);
730 /* Translate fault for emulators (e.g. Linux) */
731 if (*p->p_sysent->sv_transtrap)
732 i = (*p->p_sysent->sv_transtrap)(i, type);
734 ksiginfo_init_trap(&ksi);
736 ksi.ksi_code = ucode;
737 ksi.ksi_addr = (void *)addr;
738 ksi.ksi_trapno = type;
739 trapsignal(td, &ksi);
742 if (type <= MAX_TRAP_MSG) {
743 uprintf("fatal process exception: %s",
745 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
746 uprintf(", fault VA = 0x%lx", (u_long)eva);
753 mtx_assert(&Giant, MA_NOTOWNED);
760 trap_pfault(frame, usermode, eva)
761 struct trapframe *frame;
766 struct vmspace *vm = NULL;
770 struct thread *td = curthread;
771 struct proc *p = td->td_proc;
773 va = trunc_page(eva);
774 if (va >= KERNBASE) {
776 * Don't allow user-mode faults in kernel address space.
777 * An exception: if the faulting address is the invalid
778 * instruction entry in the IDT, then the Intel Pentium
779 * F00F bug workaround was triggered, and we need to
780 * treat it is as an illegal instruction, and not a page
783 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
784 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
793 * This is a fault on non-kernel virtual memory.
794 * vm is initialized above to NULL. If curproc is NULL
795 * or curproc->p_vmspace is NULL the fault is fatal.
807 * PGEX_I is defined only if the execute disable bit capability is
808 * supported and enabled.
810 if (frame->tf_err & PGEX_W)
811 ftype = VM_PROT_WRITE;
813 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
814 ftype = VM_PROT_EXECUTE;
817 ftype = VM_PROT_READ;
819 if (map != kernel_map) {
821 * Keep swapout from messing with us during this
828 /* Fault in the user page: */
829 rv = vm_fault(map, va, ftype,
830 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
838 * Don't have to worry about process locking or stacks in the
841 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
843 if (rv == KERN_SUCCESS)
847 if (td->td_intr_nesting_level == 0 &&
848 PCPU_GET(curpcb)->pcb_onfault != NULL) {
849 frame->tf_eip = (int)PCPU_GET(curpcb)->pcb_onfault;
852 trap_fatal(frame, eva);
856 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
860 trap_fatal(frame, eva)
861 struct trapframe *frame;
866 struct soft_segment_descriptor softseg;
869 code = frame->tf_err;
870 type = frame->tf_trapno;
871 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
873 if (type <= MAX_TRAP_MSG)
874 msg = trap_msg[type];
877 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
878 frame->tf_eflags & PSL_VM ? "vm86" :
879 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
881 /* two separate prints in case of a trap on an unmapped page */
882 printf("cpuid = %d; ", PCPU_GET(cpuid));
883 printf("apic id = %02x\n", PCPU_GET(apic_id));
885 if (type == T_PAGEFLT) {
886 printf("fault virtual address = 0x%x\n", eva);
887 printf("fault code = %s %s, %s\n",
888 code & PGEX_U ? "user" : "supervisor",
889 code & PGEX_W ? "write" : "read",
890 code & PGEX_P ? "protection violation" : "page not present");
892 printf("instruction pointer = 0x%x:0x%x\n",
893 frame->tf_cs & 0xffff, frame->tf_eip);
894 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
895 ss = frame->tf_ss & 0xffff;
898 ss = GSEL(GDATA_SEL, SEL_KPL);
899 esp = (int)&frame->tf_esp;
901 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
902 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
903 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
904 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
905 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
906 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
908 printf("processor eflags = ");
909 if (frame->tf_eflags & PSL_T)
910 printf("trace trap, ");
911 if (frame->tf_eflags & PSL_I)
912 printf("interrupt enabled, ");
913 if (frame->tf_eflags & PSL_NT)
914 printf("nested task, ");
915 if (frame->tf_eflags & PSL_RF)
917 if (frame->tf_eflags & PSL_VM)
919 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
920 printf("current process = ");
922 printf("%lu (%s)\n", (u_long)curproc->p_pid, curproc->p_comm);
928 if (debugger_on_panic || kdb_active) {
929 frame->tf_err = eva; /* smuggle fault address to ddb */
930 if (kdb_trap(type, 0, frame)) {
931 frame->tf_err = code; /* restore error code */
934 frame->tf_err = code; /* restore error code */
937 printf("trap number = %d\n", type);
938 if (type <= MAX_TRAP_MSG)
939 panic("%s", trap_msg[type]);
941 panic("unknown/reserved trap");
945 * Double fault handler. Called when a fault occurs while writing
946 * a frame for a trap/exception onto the stack. This usually occurs
947 * when the stack overflows (such is the case with infinite recursion,
950 * XXX Note that the current PTD gets replaced by IdlePTD when the
951 * task switch occurs. This means that the stack that was active at
952 * the time of the double fault is not available at <kstack> unless
953 * the machine was idle when the double fault occurred. The downside
954 * of this is that "trace <ebp>" in ddb won't work.
960 if (dtrace_doubletrap_func != NULL)
961 (*dtrace_doubletrap_func)();
963 printf("\nFatal double fault:\n");
964 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
965 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
966 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
968 /* two separate prints in case of a trap on an unmapped page */
969 printf("cpuid = %d; ", PCPU_GET(cpuid));
970 printf("apic id = %02x\n", PCPU_GET(apic_id));
972 panic("double fault");
976 * syscall - system call request C handler
978 * A system call is essentially treated as a trap.
981 syscall(struct trapframe *frame)
984 struct sysent *callp;
985 struct thread *td = curthread;
986 struct proc *p = td->td_proc;
987 register_t orig_tf_eflags;
994 PCPU_INC(cnt.v_syscall);
997 if (ISPL(frame->tf_cs) != SEL_UPL) {
1004 td->td_frame = frame;
1005 if (td->td_ucred != p->p_ucred)
1006 cred_update_thread(td);
1008 if (p->p_flag & P_SA)
1009 thread_user_enter(td);
1011 params = (caddr_t)frame->tf_esp + sizeof(int);
1012 code = frame->tf_eax;
1013 orig_tf_eflags = frame->tf_eflags;
1015 if (p->p_sysent->sv_prepsyscall) {
1017 * The prep code is MP aware.
1019 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, ¶ms);
1022 * Need to check if this is a 32 bit or 64 bit syscall.
1023 * fuword is MP aware.
1025 if (code == SYS_syscall) {
1027 * Code is first argument, followed by actual args.
1029 code = fuword(params);
1030 params += sizeof(int);
1031 } else if (code == SYS___syscall) {
1033 * Like syscall, but code is a quad, so as to maintain
1034 * quad alignment for the rest of the arguments.
1036 code = fuword(params);
1037 params += sizeof(quad_t);
1041 if (p->p_sysent->sv_mask)
1042 code &= p->p_sysent->sv_mask;
1044 if (code >= p->p_sysent->sv_size)
1045 callp = &p->p_sysent->sv_table[0];
1047 callp = &p->p_sysent->sv_table[code];
1049 narg = callp->sy_narg;
1052 * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
1054 if (params != NULL && narg != 0)
1055 error = copyin(params, (caddr_t)args,
1056 (u_int)(narg * sizeof(int)));
1061 if (KTRPOINT(td, KTR_SYSCALL))
1062 ktrsyscall(code, narg, args);
1065 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
1066 td->td_proc->p_pid, td->td_proc->p_comm, code);
1071 td->td_retval[0] = 0;
1072 td->td_retval[1] = frame->tf_edx;
1074 STOPEVENT(p, S_SCE, narg);
1076 PTRACESTOP_SC(p, td, S_PT_SCE);
1078 #ifdef KDTRACE_HOOKS
1080 * If the systrace module has registered it's probe
1081 * callback and if there is a probe active for the
1082 * syscall 'entry', process the probe.
1084 if (systrace_probe_func != NULL && callp->sy_entry != 0)
1085 (*systrace_probe_func)(callp->sy_entry, code, callp,
1089 AUDIT_SYSCALL_ENTER(code, td);
1090 error = (*callp->sy_call)(td, args);
1091 AUDIT_SYSCALL_EXIT(error, td);
1093 /* Save the latest error return value. */
1094 td->td_errno = error;
1096 #ifdef KDTRACE_HOOKS
1098 * If the systrace module has registered it's probe
1099 * callback and if there is a probe active for the
1100 * syscall 'return', process the probe.
1102 if (systrace_probe_func != NULL && callp->sy_return != 0)
1103 (*systrace_probe_func)(callp->sy_return, code, callp,
1110 frame->tf_eax = td->td_retval[0];
1111 frame->tf_edx = td->td_retval[1];
1112 frame->tf_eflags &= ~PSL_C;
1117 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1118 * int 0x80 is 2 bytes. We saved this in tf_err.
1120 frame->tf_eip -= frame->tf_err;
1127 if (p->p_sysent->sv_errsize) {
1128 if (error >= p->p_sysent->sv_errsize)
1129 error = -1; /* XXX */
1131 error = p->p_sysent->sv_errtbl[error];
1133 frame->tf_eax = error;
1134 frame->tf_eflags |= PSL_C;
1141 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1142 frame->tf_eflags &= ~PSL_T;
1143 ksiginfo_init_trap(&ksi);
1144 ksi.ksi_signo = SIGTRAP;
1145 ksi.ksi_code = TRAP_TRACE;
1146 ksi.ksi_addr = (void *)frame->tf_eip;
1147 trapsignal(td, &ksi);
1151 * Check for misbehavior.
1153 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
1154 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
1155 KASSERT(td->td_critnest == 0,
1156 ("System call %s returning in a critical section",
1157 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
1158 KASSERT(td->td_locks == 0,
1159 ("System call %s returning with %d locks held",
1160 (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
1164 * Handle reschedule and other end-of-syscall issues
1168 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
1169 td->td_proc->p_pid, td->td_proc->p_comm, code);
1172 if (KTRPOINT(td, KTR_SYSRET))
1173 ktrsysret(code, error, td->td_retval[0]);
1177 * This works because errno is findable through the
1178 * register set. If we ever support an emulation where this
1179 * is not the case, this code will need to be revisited.
1181 STOPEVENT(p, S_SCX, code);
1183 PTRACESTOP_SC(p, td, S_PT_SCX);