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;
214 /* Handler for NMI IPIs used for stopping CPUs. */
216 if (ipi_nmi_handler() == 0)
228 if (type == T_RESERVED) {
229 trap_fatal(frame, 0);
235 * CPU PMCs interrupt using an NMI so we check for that first.
236 * If the HWPMC module is active, 'pmc_hook' will point to
237 * the function to be called. A return value of '1' from the
238 * hook means that the NMI was handled by it and that we can
239 * return immediately.
241 if (type == T_NMI && pmc_intr &&
242 (*pmc_intr)(PCPU_GET(cpuid), frame))
246 if (type == T_MCHK) {
248 trap_fatal(frame, 0);
254 * A trap can occur while DTrace executes a probe. Before
255 * executing the probe, DTrace blocks re-scheduling and sets
256 * a flag in it's per-cpu flags to indicate that it doesn't
257 * want to fault. On returning from the the probe, the no-fault
258 * flag is cleared and finally re-scheduling is enabled.
260 * If the DTrace kernel module has registered a trap handler,
261 * call it and if it returns non-zero, assume that it has
262 * handled the trap and modified the trap frame so that this
263 * function can return normally.
265 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
266 dtrace_trap_func != NULL)
267 if ((*dtrace_trap_func)(frame, type))
271 if ((frame->tf_eflags & PSL_I) == 0) {
273 * Buggy application or kernel code has disabled
274 * interrupts and then trapped. Enabling interrupts
275 * now is wrong, but it is better than running with
276 * interrupts disabled until they are accidentally
279 if (ISPL(frame->tf_cs) == SEL_UPL || (frame->tf_eflags & PSL_VM))
281 "pid %ld (%s): trap %d with interrupts disabled\n",
282 (long)curproc->p_pid, curthread->td_name, type);
283 else if (type != T_BPTFLT && type != T_TRCTRAP &&
284 frame->tf_eip != (int)cpu_switch_load_gs) {
286 * XXX not quite right, since this may be for a
287 * multiple fault in user mode.
289 printf("kernel trap %d with interrupts disabled\n",
292 * Page faults need interrupts disabled until later,
293 * and we shouldn't enable interrupts while holding
294 * a spin lock or if servicing an NMI.
296 if (type != T_NMI && type != T_PAGEFLT &&
297 td->td_md.md_spinlock_count == 0)
302 code = frame->tf_err;
303 if (type == T_PAGEFLT) {
305 * For some Cyrix CPUs, %cr2 is clobbered by
306 * interrupts. This problem is worked around by using
307 * an interrupt gate for the pagefault handler. We
308 * are finally ready to read %cr2 and then must
309 * reenable interrupts.
311 * If we get a page fault while in a critical section, then
312 * it is most likely a fatal kernel page fault. The kernel
313 * is already going to panic trying to get a sleep lock to
314 * do the VM lookup, so just consider it a fatal trap so the
315 * kernel can print out a useful trap message and even get
318 * If we get a page fault while holding a non-sleepable
319 * lock, then it is most likely a fatal kernel page fault.
320 * If WITNESS is enabled, then it's going to whine about
321 * bogus LORs with various VM locks, so just skip to the
322 * fatal trap handling directly.
325 if (td->td_critnest != 0 ||
326 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
327 "Kernel page fault") != 0)
328 trap_fatal(frame, eva);
333 if ((ISPL(frame->tf_cs) == SEL_UPL) ||
334 ((frame->tf_eflags & PSL_VM) &&
335 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL))) {
339 td->td_frame = frame;
340 addr = frame->tf_eip;
341 if (td->td_ucred != p->p_ucred)
342 cred_update_thread(td);
345 case T_PRIVINFLT: /* privileged instruction fault */
350 case T_BPTFLT: /* bpt instruction fault */
351 case T_TRCTRAP: /* trace trap */
353 frame->tf_eflags &= ~PSL_T;
355 ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
358 case T_ARITHTRAP: /* arithmetic trap */
370 * The following two traps can happen in
371 * vm86 mode, and, if so, we want to handle
374 case T_PROTFLT: /* general protection fault */
375 case T_STKFLT: /* stack fault */
376 if (frame->tf_eflags & PSL_VM) {
377 i = vm86_emulate((struct vm86frame *)frame);
383 ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
385 case T_SEGNPFLT: /* segment not present fault */
389 case T_TSSFLT: /* invalid TSS fault */
393 case T_DOUBLEFLT: /* double fault */
399 case T_PAGEFLT: /* page fault */
401 i = trap_pfault(frame, TRUE, eva);
402 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
405 * The f00f hack workaround has triggered, so
406 * treat the fault as an illegal instruction
407 * (T_PRIVINFLT) instead of a page fault.
409 type = frame->tf_trapno = T_PRIVINFLT;
411 /* Proceed as in that case. */
425 if (prot_fault_translation == 0) {
428 * This check also covers the images
429 * without the ABI-tag ELF note.
431 if (SV_CURPROC_ABI() ==
433 p->p_osrel >= 700004) {
438 ucode = BUS_PAGE_FAULT;
440 } else if (prot_fault_translation == 1) {
442 * Always compat mode.
445 ucode = BUS_PAGE_FAULT;
448 * Always SIGSEGV mode.
457 case T_DIVIDE: /* integer divide fault */
466 # define TIMER_FREQ 1193182
468 if (time_second - lastalert > 10) {
469 log(LOG_WARNING, "NMI: power fail\n");
471 lastalert = time_second;
474 #else /* !POWERFAIL_NMI */
475 /* machine/parity/power fail/"kitchen sink" faults */
476 if (isa_nmi(code) == 0) {
479 * NMI can be hooked up to a pushbutton
483 printf ("NMI ... going to debugger\n");
484 kdb_trap(type, 0, frame);
488 } else if (panic_on_nmi)
489 panic("NMI indicates hardware failure");
491 #endif /* POWERFAIL_NMI */
494 case T_OFLOW: /* integer overflow fault */
499 case T_BOUND: /* bounds check fault */
506 /* transparent fault (due to context switch "late") */
510 uprintf("pid %d killed due to lack of floating point\n",
516 case T_FPOPFLT: /* FPU operand fetch fault */
521 case T_XMMFLT: /* SIMD floating-point exception */
529 KASSERT(cold || td->td_ucred != NULL,
530 ("kernel trap doesn't have ucred"));
532 case T_PAGEFLT: /* page fault */
533 (void) trap_pfault(frame, FALSE, eva);
539 * The kernel is apparently using npx for copying.
540 * XXX this should be fatal unless the kernel has
541 * registered such use.
549 * The following two traps can happen in
550 * vm86 mode, and, if so, we want to handle
553 case T_PROTFLT: /* general protection fault */
554 case T_STKFLT: /* stack fault */
555 if (frame->tf_eflags & PSL_VM) {
556 i = vm86_emulate((struct vm86frame *)frame);
559 * returns to original process
561 vm86_trap((struct vm86frame *)frame);
564 if (type == T_STKFLT)
569 case T_SEGNPFLT: /* segment not present fault */
570 if (PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)
574 * Invalid %fs's and %gs's can be created using
575 * procfs or PT_SETREGS or by invalidating the
576 * underlying LDT entry. This causes a fault
577 * in kernel mode when the kernel attempts to
578 * switch contexts. Lose the bad context
579 * (XXX) so that we can continue, and generate
582 if (frame->tf_eip == (int)cpu_switch_load_gs) {
583 PCPU_GET(curpcb)->pcb_gs = 0;
592 if (td->td_intr_nesting_level != 0)
596 * Invalid segment selectors and out of bounds
597 * %eip's and %esp's can be set up in user mode.
598 * This causes a fault in kernel mode when the
599 * kernel tries to return to user mode. We want
600 * to get this fault so that we can fix the
601 * problem here and not have to check all the
602 * selectors and pointers when the user changes
605 if (frame->tf_eip == (int)doreti_iret) {
606 frame->tf_eip = (int)doreti_iret_fault;
609 if (frame->tf_eip == (int)doreti_popl_ds) {
610 frame->tf_eip = (int)doreti_popl_ds_fault;
613 if (frame->tf_eip == (int)doreti_popl_es) {
614 frame->tf_eip = (int)doreti_popl_es_fault;
617 if (frame->tf_eip == (int)doreti_popl_fs) {
618 frame->tf_eip = (int)doreti_popl_fs_fault;
621 if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
623 (int)PCPU_GET(curpcb)->pcb_onfault;
630 * PSL_NT can be set in user mode and isn't cleared
631 * automatically when the kernel is entered. This
632 * causes a TSS fault when the kernel attempts to
633 * `iret' because the TSS link is uninitialized. We
634 * want to get this fault so that we can fix the
635 * problem here and not every time the kernel is
638 if (frame->tf_eflags & PSL_NT) {
639 frame->tf_eflags &= ~PSL_NT;
644 case T_TRCTRAP: /* trace trap */
645 if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
647 * We've just entered system mode via the
648 * syscall lcall. Continue single stepping
649 * silently until the syscall handler has
654 if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
656 * The syscall handler has now saved the
657 * flags. Stop single stepping it.
659 frame->tf_eflags &= ~PSL_T;
663 * Ignore debug register trace traps due to
664 * accesses in the user's address space, which
665 * can happen under several conditions such as
666 * if a user sets a watchpoint on a buffer and
667 * then passes that buffer to a system call.
668 * We still want to get TRCTRAPS for addresses
669 * in kernel space because that is useful when
670 * debugging the kernel.
672 if (user_dbreg_trap() &&
673 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)) {
675 * Reset breakpoint bits because the
678 load_dr6(rdr6() & 0xfffffff0);
682 * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
686 * If KDB is enabled, let it handle the debugger trap.
687 * Otherwise, debugger traps "can't happen".
690 if (kdb_trap(type, 0, frame))
698 if (time_second - lastalert > 10) {
699 log(LOG_WARNING, "NMI: power fail\n");
701 lastalert = time_second;
704 #else /* !POWERFAIL_NMI */
705 /* machine/parity/power fail/"kitchen sink" faults */
706 if (isa_nmi(code) == 0) {
709 * NMI can be hooked up to a pushbutton
713 printf ("NMI ... going to debugger\n");
714 kdb_trap(type, 0, frame);
718 } else if (panic_on_nmi == 0)
721 #endif /* POWERFAIL_NMI */
725 trap_fatal(frame, eva);
729 /* Translate fault for emulators (e.g. Linux) */
730 if (*p->p_sysent->sv_transtrap)
731 i = (*p->p_sysent->sv_transtrap)(i, type);
733 ksiginfo_init_trap(&ksi);
735 ksi.ksi_code = ucode;
736 ksi.ksi_addr = (void *)addr;
737 ksi.ksi_trapno = type;
738 trapsignal(td, &ksi);
741 if (type <= MAX_TRAP_MSG) {
742 uprintf("fatal process exception: %s",
744 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
745 uprintf(", fault VA = 0x%lx", (u_long)eva);
752 mtx_assert(&Giant, MA_NOTOWNED);
759 trap_pfault(frame, usermode, eva)
760 struct trapframe *frame;
765 struct vmspace *vm = NULL;
769 struct thread *td = curthread;
770 struct proc *p = td->td_proc;
772 va = trunc_page(eva);
773 if (va >= KERNBASE) {
775 * Don't allow user-mode faults in kernel address space.
776 * An exception: if the faulting address is the invalid
777 * instruction entry in the IDT, then the Intel Pentium
778 * F00F bug workaround was triggered, and we need to
779 * treat it is as an illegal instruction, and not a page
782 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
783 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
792 * This is a fault on non-kernel virtual memory.
793 * vm is initialized above to NULL. If curproc is NULL
794 * or curproc->p_vmspace is NULL the fault is fatal.
806 * PGEX_I is defined only if the execute disable bit capability is
807 * supported and enabled.
809 if (frame->tf_err & PGEX_W)
810 ftype = VM_PROT_WRITE;
812 else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
813 ftype = VM_PROT_EXECUTE;
816 ftype = VM_PROT_READ;
818 if (map != kernel_map) {
820 * Keep swapout from messing with us during this
827 /* Fault in the user page: */
828 rv = vm_fault(map, va, ftype,
829 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
837 * Don't have to worry about process locking or stacks in the
840 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
842 if (rv == KERN_SUCCESS)
846 if (td->td_intr_nesting_level == 0 &&
847 PCPU_GET(curpcb)->pcb_onfault != NULL) {
848 frame->tf_eip = (int)PCPU_GET(curpcb)->pcb_onfault;
851 trap_fatal(frame, eva);
855 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
859 trap_fatal(frame, eva)
860 struct trapframe *frame;
865 struct soft_segment_descriptor softseg;
868 code = frame->tf_err;
869 type = frame->tf_trapno;
870 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
872 if (type <= MAX_TRAP_MSG)
873 msg = trap_msg[type];
876 printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
877 frame->tf_eflags & PSL_VM ? "vm86" :
878 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
880 /* two separate prints in case of a trap on an unmapped page */
881 printf("cpuid = %d; ", PCPU_GET(cpuid));
882 printf("apic id = %02x\n", PCPU_GET(apic_id));
884 if (type == T_PAGEFLT) {
885 printf("fault virtual address = 0x%x\n", eva);
886 printf("fault code = %s %s, %s\n",
887 code & PGEX_U ? "user" : "supervisor",
888 code & PGEX_W ? "write" : "read",
889 code & PGEX_P ? "protection violation" : "page not present");
891 printf("instruction pointer = 0x%x:0x%x\n",
892 frame->tf_cs & 0xffff, frame->tf_eip);
893 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
894 ss = frame->tf_ss & 0xffff;
897 ss = GSEL(GDATA_SEL, SEL_KPL);
898 esp = (int)&frame->tf_esp;
900 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
901 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
902 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
903 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
904 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
905 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
907 printf("processor eflags = ");
908 if (frame->tf_eflags & PSL_T)
909 printf("trace trap, ");
910 if (frame->tf_eflags & PSL_I)
911 printf("interrupt enabled, ");
912 if (frame->tf_eflags & PSL_NT)
913 printf("nested task, ");
914 if (frame->tf_eflags & PSL_RF)
916 if (frame->tf_eflags & PSL_VM)
918 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
919 printf("current process = ");
921 printf("%lu (%s)\n", (u_long)curproc->p_pid, curthread->td_name);
927 if (debugger_on_panic || kdb_active) {
928 frame->tf_err = eva; /* smuggle fault address to ddb */
929 if (kdb_trap(type, 0, frame)) {
930 frame->tf_err = code; /* restore error code */
933 frame->tf_err = code; /* restore error code */
936 printf("trap number = %d\n", type);
937 if (type <= MAX_TRAP_MSG)
938 panic("%s", trap_msg[type]);
940 panic("unknown/reserved trap");
944 * Double fault handler. Called when a fault occurs while writing
945 * a frame for a trap/exception onto the stack. This usually occurs
946 * when the stack overflows (such is the case with infinite recursion,
949 * XXX Note that the current PTD gets replaced by IdlePTD when the
950 * task switch occurs. This means that the stack that was active at
951 * the time of the double fault is not available at <kstack> unless
952 * the machine was idle when the double fault occurred. The downside
953 * of this is that "trace <ebp>" in ddb won't work.
959 if (dtrace_doubletrap_func != NULL)
960 (*dtrace_doubletrap_func)();
962 printf("\nFatal double fault:\n");
963 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
964 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
965 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
967 /* two separate prints in case of a trap on an unmapped page */
968 printf("cpuid = %d; ", PCPU_GET(cpuid));
969 printf("apic id = %02x\n", PCPU_GET(apic_id));
971 panic("double fault");
974 struct syscall_args {
976 struct sysent *callp;
983 fetch_syscall_args(struct thread *td, struct syscall_args *sa)
986 struct trapframe *frame;
991 frame = td->td_frame;
993 params = (caddr_t)frame->tf_esp + sizeof(int);
994 sa->code = frame->tf_eax;
996 if (p->p_sysent->sv_prepsyscall) {
997 (*p->p_sysent->sv_prepsyscall)(frame, sa->args, &sa->code,
1001 * Need to check if this is a 32 bit or 64 bit syscall.
1003 if (sa->code == SYS_syscall) {
1005 * Code is first argument, followed by actual args.
1007 sa->code = fuword(params);
1008 params += sizeof(int);
1009 } else if (sa->code == SYS___syscall) {
1011 * Like syscall, but code is a quad, so as to maintain
1012 * quad alignment for the rest of the arguments.
1014 sa->code = fuword(params);
1015 params += sizeof(quad_t);
1019 if (p->p_sysent->sv_mask)
1020 sa->code &= p->p_sysent->sv_mask;
1021 if (sa->code >= p->p_sysent->sv_size)
1022 sa->callp = &p->p_sysent->sv_table[0];
1024 sa->callp = &p->p_sysent->sv_table[sa->code];
1025 sa->narg = sa->callp->sy_narg;
1027 if (params != NULL && sa->narg != 0)
1028 error = copyin(params, (caddr_t)sa->args,
1029 (u_int)(sa->narg * sizeof(int)));
1034 if (KTRPOINT(td, KTR_SYSCALL))
1035 ktrsyscall(sa->code, sa->narg, sa->args);
1041 * syscall - system call request C handler
1043 * A system call is essentially treated as a trap.
1046 syscall(struct trapframe *frame)
1050 struct syscall_args sa;
1051 register_t orig_tf_eflags;
1055 PCPU_INC(cnt.v_syscall);
1061 if (ISPL(frame->tf_cs) != SEL_UPL) {
1068 td->td_frame = frame;
1069 if (td->td_ucred != p->p_ucred)
1070 cred_update_thread(td);
1071 orig_tf_eflags = frame->tf_eflags;
1072 if (p->p_flag & P_TRACED) {
1074 td->td_dbgflags &= ~TDB_USERWR;
1077 error = fetch_syscall_args(td, &sa);
1079 CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
1080 td->td_proc->p_pid, td->td_name, sa.code);
1083 td->td_retval[0] = 0;
1084 td->td_retval[1] = frame->tf_edx;
1086 STOPEVENT(p, S_SCE, sa.narg);
1087 PTRACESTOP_SC(p, td, S_PT_SCE);
1088 if (td->td_dbgflags & TDB_USERWR) {
1090 * Reread syscall number and arguments if
1091 * debugger modified registers or memory.
1093 error = fetch_syscall_args(td, &sa);
1096 td->td_retval[1] = frame->tf_edx;
1099 #ifdef KDTRACE_HOOKS
1101 * If the systrace module has registered it's probe
1102 * callback and if there is a probe active for the
1103 * syscall 'entry', process the probe.
1105 if (systrace_probe_func != NULL && sa.callp->sy_entry != 0)
1106 (*systrace_probe_func)(sa.callp->sy_entry, sa.code,
1110 AUDIT_SYSCALL_ENTER(sa.code, td);
1111 error = (*sa.callp->sy_call)(td, sa.args);
1112 AUDIT_SYSCALL_EXIT(error, td);
1114 /* Save the latest error return value. */
1115 td->td_errno = error;
1117 #ifdef KDTRACE_HOOKS
1119 * If the systrace module has registered it's probe
1120 * callback and if there is a probe active for the
1121 * syscall 'return', process the probe.
1123 if (systrace_probe_func != NULL && sa.callp->sy_return != 0)
1124 (*systrace_probe_func)(sa.callp->sy_return, sa.code,
1129 cpu_set_syscall_retval(td, error);
1134 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1135 frame->tf_eflags &= ~PSL_T;
1136 ksiginfo_init_trap(&ksi);
1137 ksi.ksi_signo = SIGTRAP;
1138 ksi.ksi_code = TRAP_TRACE;
1139 ksi.ksi_addr = (void *)frame->tf_eip;
1140 trapsignal(td, &ksi);
1144 * Check for misbehavior.
1146 WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
1147 (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
1148 syscallnames[sa.code] : "???");
1149 KASSERT(td->td_critnest == 0,
1150 ("System call %s returning in a critical section",
1151 (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
1152 syscallnames[sa.code] : "???"));
1153 KASSERT(td->td_locks == 0,
1154 ("System call %s returning with %d locks held",
1155 (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
1156 syscallnames[sa.code] : "???", td->td_locks));
1159 * Handle reschedule and other end-of-syscall issues
1163 CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
1164 td->td_proc->p_pid, td->td_name, sa.code);
1167 if (KTRPOINT(td, KTR_SYSRET))
1168 ktrsysret(sa.code, error, td->td_retval[0]);
1172 * This works because errno is findable through the
1173 * register set. If we ever support an emulation where this
1174 * is not the case, this code will need to be revisited.
1176 STOPEVENT(p, S_SCX, sa.code);
1178 PTRACESTOP_SC(p, td, S_PT_SCX);