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
42 * 386 Trap and System call handling
45 #include "opt_clock.h"
49 #include "opt_ktrace.h"
53 #include <sys/param.h>
55 #include <sys/systm.h>
57 #include <sys/pioctl.h>
58 #include <sys/kernel.h>
61 #include <sys/mutex.h>
62 #include <sys/resourcevar.h>
63 #include <sys/signalvar.h>
64 #include <sys/syscall.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
68 #include <sys/vmmeter.h>
70 #include <sys/ktrace.h>
74 #include <vm/vm_param.h>
76 #include <vm/vm_kern.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_extern.h>
81 #include <machine/cpu.h>
82 #include <machine/md_var.h>
83 #include <machine/pcb.h>
85 #include <machine/smp.h>
87 #include <machine/tss.h>
89 #include <i386/isa/icu.h>
90 #include <i386/isa/intr_machdep.h>
93 #include <sys/syslog.h>
94 #include <machine/clock.h>
97 #include <machine/vm86.h>
101 #include <sys/sysctl.h>
103 int (*pmath_emulate)(struct trapframe *);
105 extern void trap(struct trapframe frame);
107 extern int trapwrite(unsigned addr);
109 extern void syscall(struct trapframe frame);
111 static int trap_pfault(struct trapframe *, int, vm_offset_t);
112 static void trap_fatal(struct trapframe *, vm_offset_t);
113 void dblfault_handler(void);
115 extern inthand_t IDTVEC(lcall_syscall);
117 #define MAX_TRAP_MSG 28
118 static char *trap_msg[] = {
120 "privileged instruction fault", /* 1 T_PRIVINFLT */
122 "breakpoint instruction fault", /* 3 T_BPTFLT */
125 "arithmetic trap", /* 6 T_ARITHTRAP */
128 "general protection fault", /* 9 T_PROTFLT */
129 "trace trap", /* 10 T_TRCTRAP */
131 "page fault", /* 12 T_PAGEFLT */
133 "alignment fault", /* 14 T_ALIGNFLT */
137 "integer divide fault", /* 18 T_DIVIDE */
138 "non-maskable interrupt trap", /* 19 T_NMI */
139 "overflow trap", /* 20 T_OFLOW */
140 "FPU bounds check fault", /* 21 T_BOUND */
141 "FPU device not available", /* 22 T_DNA */
142 "double fault", /* 23 T_DOUBLEFLT */
143 "FPU operand fetch fault", /* 24 T_FPOPFLT */
144 "invalid TSS fault", /* 25 T_TSSFLT */
145 "segment not present fault", /* 26 T_SEGNPFLT */
146 "stack fault", /* 27 T_STKFLT */
147 "machine check trap", /* 28 T_MCHK */
150 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
151 extern int has_f00f_bug;
155 static int ddb_on_nmi = 1;
156 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
157 &ddb_on_nmi, 0, "Go to DDB on NMI");
159 static int panic_on_nmi = 1;
160 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
161 &panic_on_nmi, 0, "Panic on NMI");
164 extern char *syscallnames[];
167 #ifdef DEVICE_POLLING
168 extern u_int32_t poll_in_trap;
169 extern int ether_poll(int count);
170 #endif /* DEVICE_POLLING */
173 * Exception, fault, and trap interface to the FreeBSD kernel.
174 * This common code is called from assembly language IDT gate entry
175 * routines that prepare a suitable stack frame, and restore this
176 * frame after the exception has been processed.
181 struct trapframe frame;
183 struct thread *td = curthread;
184 struct proc *p = td->td_proc;
186 int i = 0, ucode = 0, type, code;
189 static int lastalert = 0;
192 atomic_add_int(&cnt.v_trap, 1);
194 if ((frame.tf_eflags & PSL_I) == 0) {
196 * Buggy application or kernel code has disabled
197 * interrupts and then trapped. Enabling interrupts
198 * now is wrong, but it is better than running with
199 * interrupts disabled until they are accidentally
202 type = frame.tf_trapno;
203 if (ISPL(frame.tf_cs) == SEL_UPL || (frame.tf_eflags & PSL_VM))
205 "pid %ld (%s): trap %d with interrupts disabled\n",
206 (long)curproc->p_pid, curproc->p_comm, type);
207 else if (type != T_BPTFLT && type != T_TRCTRAP) {
209 * XXX not quite right, since this may be for a
210 * multiple fault in user mode.
212 printf("kernel trap %d with interrupts disabled\n",
215 * Page faults need interrupts diasabled until later,
216 * and we shouldn't enable interrupts while holding a
219 if (type != T_PAGEFLT && PCPU_GET(spinlocks) == NULL)
225 type = frame.tf_trapno;
227 if (type == T_PAGEFLT) {
229 * For some Cyrix CPUs, %cr2 is clobbered by
230 * interrupts. This problem is worked around by using
231 * an interrupt gate for the pagefault handler. We
232 * are finally ready to read %cr2 and then must
233 * reenable interrupts.
235 * If we get a page fault while holding a spin lock, then
236 * it is most likely a fatal kernel page fault. The kernel
237 * is already going to panic trying to get a sleep lock to
238 * do the VM lookup, so just consider it a fatal trap so the
239 * kernel can print out a useful trap message and even get
243 if (PCPU_GET(spinlocks) == NULL)
246 trap_fatal(&frame, eva);
249 #ifdef DEVICE_POLLING
251 ether_poll(poll_in_trap);
252 #endif /* DEVICE_POLLING */
254 if ((ISPL(frame.tf_cs) == SEL_UPL) ||
255 ((frame.tf_eflags & PSL_VM) && !in_vm86call)) {
258 sticks = td->td_kse->ke_sticks;
259 td->td_frame = &frame;
260 if (td->td_ucred != p->p_ucred)
261 cred_update_thread(td);
264 case T_PRIVINFLT: /* privileged instruction fault */
269 case T_BPTFLT: /* bpt instruction fault */
270 case T_TRCTRAP: /* trace trap */
271 frame.tf_eflags &= ~PSL_T;
275 case T_ARITHTRAP: /* arithmetic trap */
287 * The following two traps can happen in
288 * vm86 mode, and, if so, we want to handle
291 case T_PROTFLT: /* general protection fault */
292 case T_STKFLT: /* stack fault */
293 if (frame.tf_eflags & PSL_VM) {
295 i = vm86_emulate((struct vm86frame *)&frame);
303 case T_SEGNPFLT: /* segment not present fault */
304 case T_TSSFLT: /* invalid TSS fault */
305 case T_DOUBLEFLT: /* double fault */
307 ucode = code + BUS_SEGM_FAULT ;
311 case T_PAGEFLT: /* page fault */
312 i = trap_pfault(&frame, TRUE, eva);
313 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
316 * The f00f hack workaround has triggered, so
317 * treat the fault as an illegal instruction
318 * (T_PRIVINFLT) instead of a page fault.
320 type = frame.tf_trapno = T_PRIVINFLT;
322 /* Proceed as in that case. */
336 case T_DIVIDE: /* integer divide fault */
345 # define TIMER_FREQ 1193182
348 if (time_second - lastalert > 10) {
349 log(LOG_WARNING, "NMI: power fail\n");
350 sysbeep(TIMER_FREQ/880, hz);
351 lastalert = time_second;
355 #else /* !POWERFAIL_NMI */
356 /* machine/parity/power fail/"kitchen sink" faults */
358 if (isa_nmi(code) == 0) {
361 * NMI can be hooked up to a pushbutton
365 printf ("NMI ... going to debugger\n");
366 kdb_trap (type, 0, &frame);
370 } else if (panic_on_nmi)
371 panic("NMI indicates hardware failure");
373 #endif /* POWERFAIL_NMI */
376 case T_OFLOW: /* integer overflow fault */
381 case T_BOUND: /* bounds check fault */
388 /* transparent fault (due to context switch "late") */
392 if (!pmath_emulate) {
394 ucode = FPE_FPU_NP_TRAP;
398 i = (*pmath_emulate)(&frame);
401 if (!(frame.tf_eflags & PSL_T))
403 frame.tf_eflags &= ~PSL_T;
406 /* else ucode = emulator_only_knows() XXX */
409 case T_FPOPFLT: /* FPU operand fetch fault */
414 case T_XMMFLT: /* SIMD floating-point exception */
422 KASSERT(cold || td->td_ucred != NULL,
423 ("kernel trap doesn't have ucred"));
425 case T_PAGEFLT: /* page fault */
426 (void) trap_pfault(&frame, FALSE, eva);
432 * The kernel is apparently using npx for copying.
433 * XXX this should be fatal unless the kernel has
434 * registered such use.
442 * The following two traps can happen in
443 * vm86 mode, and, if so, we want to handle
446 case T_PROTFLT: /* general protection fault */
447 case T_STKFLT: /* stack fault */
448 if (frame.tf_eflags & PSL_VM) {
450 i = vm86_emulate((struct vm86frame *)&frame);
454 * returns to original process
456 vm86_trap((struct vm86frame *)&frame);
459 if (type == T_STKFLT)
464 case T_SEGNPFLT: /* segment not present fault */
468 if (td->td_intr_nesting_level != 0)
472 * Invalid %fs's and %gs's can be created using
473 * procfs or PT_SETREGS or by invalidating the
474 * underlying LDT entry. This causes a fault
475 * in kernel mode when the kernel attempts to
476 * switch contexts. Lose the bad context
477 * (XXX) so that we can continue, and generate
480 if (frame.tf_eip == (int)cpu_switch_load_gs) {
481 PCPU_GET(curpcb)->pcb_gs = 0;
489 * Invalid segment selectors and out of bounds
490 * %eip's and %esp's can be set up in user mode.
491 * This causes a fault in kernel mode when the
492 * kernel tries to return to user mode. We want
493 * to get this fault so that we can fix the
494 * problem here and not have to check all the
495 * selectors and pointers when the user changes
498 if (frame.tf_eip == (int)doreti_iret) {
499 frame.tf_eip = (int)doreti_iret_fault;
502 if (frame.tf_eip == (int)doreti_popl_ds) {
503 frame.tf_eip = (int)doreti_popl_ds_fault;
506 if (frame.tf_eip == (int)doreti_popl_es) {
507 frame.tf_eip = (int)doreti_popl_es_fault;
510 if (frame.tf_eip == (int)doreti_popl_fs) {
511 frame.tf_eip = (int)doreti_popl_fs_fault;
514 if (PCPU_GET(curpcb) != NULL &&
515 PCPU_GET(curpcb)->pcb_onfault != NULL) {
517 (int)PCPU_GET(curpcb)->pcb_onfault;
524 * PSL_NT can be set in user mode and isn't cleared
525 * automatically when the kernel is entered. This
526 * causes a TSS fault when the kernel attempts to
527 * `iret' because the TSS link is uninitialized. We
528 * want to get this fault so that we can fix the
529 * problem here and not every time the kernel is
532 if (frame.tf_eflags & PSL_NT) {
533 frame.tf_eflags &= ~PSL_NT;
538 case T_TRCTRAP: /* trace trap */
539 if (frame.tf_eip == (int)IDTVEC(lcall_syscall)) {
541 * We've just entered system mode via the
542 * syscall lcall. Continue single stepping
543 * silently until the syscall handler has
548 if (frame.tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
550 * The syscall handler has now saved the
551 * flags. Stop single stepping it.
553 frame.tf_eflags &= ~PSL_T;
557 * Ignore debug register trace traps due to
558 * accesses in the user's address space, which
559 * can happen under several conditions such as
560 * if a user sets a watchpoint on a buffer and
561 * then passes that buffer to a system call.
562 * We still want to get TRCTRAPS for addresses
563 * in kernel space because that is useful when
564 * debugging the kernel.
567 if (user_dbreg_trap() && !in_vm86call) {
569 * Reset breakpoint bits because the
572 load_dr6(rdr6() & 0xfffffff0);
576 * Fall through (TRCTRAP kernel mode, kernel address)
580 * If DDB is enabled, let it handle the debugger trap.
581 * Otherwise, debugger traps "can't happen".
585 if (kdb_trap (type, 0, &frame))
594 if (time_second - lastalert > 10) {
595 log(LOG_WARNING, "NMI: power fail\n");
596 sysbeep(TIMER_FREQ/880, hz);
597 lastalert = time_second;
601 #else /* !POWERFAIL_NMI */
603 /* machine/parity/power fail/"kitchen sink" faults */
604 if (isa_nmi(code) == 0) {
607 * NMI can be hooked up to a pushbutton
611 printf ("NMI ... going to debugger\n");
612 kdb_trap (type, 0, &frame);
616 } else if (panic_on_nmi == 0)
619 #endif /* POWERFAIL_NMI */
623 trap_fatal(&frame, eva);
627 /* Translate fault for emulators (e.g. Linux) */
628 if (*p->p_sysent->sv_transtrap)
629 i = (*p->p_sysent->sv_transtrap)(i, type);
631 trapsignal(p, i, ucode);
634 if (type <= MAX_TRAP_MSG) {
635 uprintf("fatal process exception: %s",
637 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
638 uprintf(", fault VA = 0x%lx", (u_long)eva);
644 userret(td, &frame, sticks);
645 mtx_assert(&Giant, MA_NOTOWNED);
648 cred_free_thread(td);
655 trap_pfault(frame, usermode, eva)
656 struct trapframe *frame;
661 struct vmspace *vm = NULL;
665 struct thread *td = curthread;
666 struct proc *p = td->td_proc;
668 va = trunc_page(eva);
669 if (va >= KERNBASE) {
671 * Don't allow user-mode faults in kernel address space.
672 * An exception: if the faulting address is the invalid
673 * instruction entry in the IDT, then the Intel Pentium
674 * F00F bug workaround was triggered, and we need to
675 * treat it is as an illegal instruction, and not a page
678 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
679 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
688 * This is a fault on non-kernel virtual memory.
689 * vm is initialized above to NULL. If curproc is NULL
690 * or curproc->p_vmspace is NULL the fault is fatal.
701 if (frame->tf_err & PGEX_W)
702 ftype = VM_PROT_WRITE;
704 ftype = VM_PROT_READ;
706 if (map != kernel_map) {
708 * Keep swapout from messing with us during this
715 /* Fault in the user page: */
716 rv = vm_fault(map, va, ftype,
717 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
725 * Don't have to worry about process locking or stacks in the
728 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
730 if (rv == KERN_SUCCESS)
734 if (td->td_intr_nesting_level == 0 &&
735 PCPU_GET(curpcb) != NULL &&
736 PCPU_GET(curpcb)->pcb_onfault != NULL) {
737 frame->tf_eip = (int)PCPU_GET(curpcb)->pcb_onfault;
740 trap_fatal(frame, eva);
744 /* kludge to pass faulting virtual address to sendsig */
747 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
751 trap_fatal(frame, eva)
752 struct trapframe *frame;
755 int code, type, ss, esp;
756 struct soft_segment_descriptor softseg;
758 code = frame->tf_err;
759 type = frame->tf_trapno;
760 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
762 if (type <= MAX_TRAP_MSG)
763 printf("\n\nFatal trap %d: %s while in %s mode\n",
764 type, trap_msg[type],
765 frame->tf_eflags & PSL_VM ? "vm86" :
766 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
768 /* two separate prints in case of a trap on an unmapped page */
769 printf("cpuid = %d; ", PCPU_GET(cpuid));
770 printf("lapic.id = %08x\n", lapic.id);
772 if (type == T_PAGEFLT) {
773 printf("fault virtual address = 0x%x\n", eva);
774 printf("fault code = %s %s, %s\n",
775 code & PGEX_U ? "user" : "supervisor",
776 code & PGEX_W ? "write" : "read",
777 code & PGEX_P ? "protection violation" : "page not present");
779 printf("instruction pointer = 0x%x:0x%x\n",
780 frame->tf_cs & 0xffff, frame->tf_eip);
781 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
782 ss = frame->tf_ss & 0xffff;
785 ss = GSEL(GDATA_SEL, SEL_KPL);
786 esp = (int)&frame->tf_esp;
788 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
789 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
790 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
791 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
792 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
793 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
795 printf("processor eflags = ");
796 if (frame->tf_eflags & PSL_T)
797 printf("trace trap, ");
798 if (frame->tf_eflags & PSL_I)
799 printf("interrupt enabled, ");
800 if (frame->tf_eflags & PSL_NT)
801 printf("nested task, ");
802 if (frame->tf_eflags & PSL_RF)
804 if (frame->tf_eflags & PSL_VM)
806 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
807 printf("current process = ");
810 (u_long)curproc->p_pid, curproc->p_comm ?
811 curproc->p_comm : "");
821 if ((debugger_on_panic || db_active) && kdb_trap(type, 0, frame))
824 printf("trap number = %d\n", type);
825 if (type <= MAX_TRAP_MSG)
826 panic("%s", trap_msg[type]);
828 panic("unknown/reserved trap");
832 * Double fault handler. Called when a fault occurs while writing
833 * a frame for a trap/exception onto the stack. This usually occurs
834 * when the stack overflows (such is the case with infinite recursion,
837 * XXX Note that the current PTD gets replaced by IdlePTD when the
838 * task switch occurs. This means that the stack that was active at
839 * the time of the double fault is not available at <kstack> unless
840 * the machine was idle when the double fault occurred. The downside
841 * of this is that "trace <ebp>" in ddb won't work.
846 printf("\nFatal double fault:\n");
847 printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
848 printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
849 printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
851 /* two separate prints in case of a trap on an unmapped page */
852 printf("cpuid = %d; ", PCPU_GET(cpuid));
853 printf("lapic.id = %08x\n", lapic.id);
855 panic("double fault");
860 * Compensate for 386 brain damage (missing URKR).
861 * This is a little simpler than the pagefault handler in trap() because
862 * it the page tables have already been faulted in and high addresses
863 * are thrown out early for other reasons.
874 va = trunc_page((vm_offset_t)addr);
876 * XXX - MAX is END. Changed > to >= for temp. fix.
878 if (va >= VM_MAXUSER_ADDRESS)
889 if (vm_map_growstack(p, va) != KERN_SUCCESS)
893 * fault the data page
895 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
901 if (rv != KERN_SUCCESS)
909 * syscall - system call request C handler
911 * A system call is essentially treated as a trap.
915 struct trapframe frame;
919 struct sysent *callp;
920 struct thread *td = curthread;
921 struct proc *p = td->td_proc;
922 register_t orig_tf_eflags;
930 * note: PCPU_LAZY_INC() can only be used if we can afford
931 * occassional inaccuracy in the count.
933 PCPU_LAZY_INC(cnt.v_syscall);
936 if (ISPL(frame.tf_cs) != SEL_UPL) {
937 mtx_lock(&Giant); /* try to stabilize the system XXX */
944 sticks = td->td_kse->ke_sticks;
945 td->td_frame = &frame;
946 if (td->td_ucred != p->p_ucred)
947 cred_update_thread(td);
948 params = (caddr_t)frame.tf_esp + sizeof(int);
950 orig_tf_eflags = frame.tf_eflags;
952 if (p->p_sysent->sv_prepsyscall) {
954 * The prep code is MP aware.
956 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms);
959 * Need to check if this is a 32 bit or 64 bit syscall.
960 * fuword is MP aware.
962 if (code == SYS_syscall) {
964 * Code is first argument, followed by actual args.
966 code = fuword(params);
967 params += sizeof(int);
968 } else if (code == SYS___syscall) {
970 * Like syscall, but code is a quad, so as to maintain
971 * quad alignment for the rest of the arguments.
973 code = fuword(params);
974 params += sizeof(quad_t);
978 if (p->p_sysent->sv_mask)
979 code &= p->p_sysent->sv_mask;
981 if (code >= p->p_sysent->sv_size)
982 callp = &p->p_sysent->sv_table[0];
984 callp = &p->p_sysent->sv_table[code];
986 narg = callp->sy_narg & SYF_ARGMASK;
989 * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
991 if (params && (i = narg * sizeof(int)) &&
992 (error = copyin(params, (caddr_t)args, (u_int)i))) {
994 if (KTRPOINT(p, KTR_SYSCALL))
995 ktrsyscall(p->p_tracep, code, narg, args);
1001 * Try to run the syscall without Giant if the syscall
1004 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1010 * We have to obtain Giant no matter what if
1013 if (KTRPOINT(p, KTR_SYSCALL)) {
1014 ktrsyscall(p->p_tracep, code, narg, args);
1017 td->td_retval[0] = 0;
1018 td->td_retval[1] = frame.tf_edx;
1020 STOPEVENT(p, S_SCE, narg);
1022 error = (*callp->sy_call)(td, args);
1026 frame.tf_eax = td->td_retval[0];
1027 frame.tf_edx = td->td_retval[1];
1028 frame.tf_eflags &= ~PSL_C;
1033 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1034 * int 0x80 is 2 bytes. We saved this in tf_err.
1036 frame.tf_eip -= frame.tf_err;
1044 if (p->p_sysent->sv_errsize) {
1045 if (error >= p->p_sysent->sv_errsize)
1046 error = -1; /* XXX */
1048 error = p->p_sysent->sv_errtbl[error];
1050 frame.tf_eax = error;
1051 frame.tf_eflags |= PSL_C;
1058 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1059 frame.tf_eflags &= ~PSL_T;
1060 trapsignal(p, SIGTRAP, 0);
1064 * Handle reschedule and other end-of-syscall issues
1066 userret(td, &frame, sticks);
1069 if (KTRPOINT(p, KTR_SYSRET)) {
1070 ktrsysret(p->p_tracep, code, error, td->td_retval[0]);
1075 * Release Giant if we previously set it. Do not
1076 * release based on mtx_owned() - we want to catch
1079 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1084 * This works because errno is findable through the
1085 * register set. If we ever support an emulation where this
1086 * is not the case, this code will need to be revisited.
1088 STOPEVENT(p, S_SCX, code);
1091 cred_free_thread(td);
1095 if (witness_list(td)) {
1096 panic("system call %s returning with mutex(s) held\n",
1097 syscallnames[code]);
1100 mtx_assert(&sched_lock, MA_NOTOWNED);
1101 mtx_assert(&Giant, MA_NOTOWNED);