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
47 #include "opt_ktrace.h"
48 #include "opt_clock.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/pioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/resourcevar.h>
57 #include <sys/signalvar.h>
58 #include <sys/syscall.h>
59 #include <sys/sysent.h>
61 #include <sys/vmmeter.h>
63 #include <sys/ktrace.h>
67 #include <vm/vm_param.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_extern.h>
75 #include <machine/cpu.h>
76 #include <machine/ipl.h>
77 #include <machine/md_var.h>
78 #include <machine/pcb.h>
80 #include <machine/smp.h>
82 #include <machine/tss.h>
84 #include <i386/isa/intr_machdep.h>
87 #include <sys/syslog.h>
88 #include <machine/clock.h>
91 #include <machine/vm86.h>
94 extern int in_Debugger, debugger_on_panic;
100 int (*pmath_emulate) __P((struct trapframe *));
102 extern void trap __P((struct trapframe frame));
103 extern int trapwrite __P((unsigned addr));
104 extern void syscall __P((struct trapframe frame));
106 static int trap_pfault __P((struct trapframe *, int, vm_offset_t));
107 static void trap_fatal __P((struct trapframe *, vm_offset_t));
108 void dblfault_handler __P((void));
110 extern inthand_t IDTVEC(syscall);
112 #define MAX_TRAP_MSG 28
113 static char *trap_msg[] = {
115 "privileged instruction fault", /* 1 T_PRIVINFLT */
117 "breakpoint instruction fault", /* 3 T_BPTFLT */
120 "arithmetic trap", /* 6 T_ARITHTRAP */
121 "system forced exception", /* 7 T_ASTFLT */
123 "general protection fault", /* 9 T_PROTFLT */
124 "trace trap", /* 10 T_TRCTRAP */
126 "page fault", /* 12 T_PAGEFLT */
128 "alignment fault", /* 14 T_ALIGNFLT */
132 "integer divide fault", /* 18 T_DIVIDE */
133 "non-maskable interrupt trap", /* 19 T_NMI */
134 "overflow trap", /* 20 T_OFLOW */
135 "FPU bounds check fault", /* 21 T_BOUND */
136 "FPU device not available", /* 22 T_DNA */
137 "double fault", /* 23 T_DOUBLEFLT */
138 "FPU operand fetch fault", /* 24 T_FPOPFLT */
139 "invalid TSS fault", /* 25 T_TSSFLT */
140 "segment not present fault", /* 26 T_SEGNPFLT */
141 "stack fault", /* 27 T_STKFLT */
142 "machine check trap", /* 28 T_MCHK */
145 static __inline void userret __P((struct proc *p, struct trapframe *frame,
148 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
149 extern int has_f00f_bug;
153 userret(p, frame, oticks)
155 struct trapframe *frame;
160 while ((sig = CURSIG(p)) != 0)
165 (p->p_priority <= p->p_usrpri) &&
166 (p->p_rtprio.type == RTP_PRIO_NORMAL)) {
169 newpriority = PUSER + p->p_estcpu / 4 + 2 * p->p_nice;
170 newpriority = min(newpriority, MAXPRI);
171 p->p_usrpri = newpriority;
175 p->p_priority = p->p_usrpri;
178 * Since we are curproc, clock will normally just change
179 * our priority without moving us from one queue to another
180 * (since the running process is not on a queue.)
181 * If that happened after we setrunqueue ourselves but before we
182 * mi_switch()'ed, we might not be on the queue indicated by
187 p->p_stats->p_ru.ru_nivcsw++;
190 while ((sig = CURSIG(p)) != 0)
194 * Charge system time if profiling.
196 if (p->p_flag & P_PROFIL)
197 addupc_task(p, frame->tf_eip,
198 (u_int)(p->p_sticks - oticks) * psratio);
200 curpriority = p->p_priority;
204 * Exception, fault, and trap interface to the FreeBSD kernel.
205 * This common code is called from assembly language IDT gate entry
206 * routines that prepare a suitable stack frame, and restore this
207 * frame after the exception has been processed.
212 struct trapframe frame;
214 struct proc *p = curproc;
216 int i = 0, ucode = 0, type, code;
219 if (!(frame.tf_eflags & PSL_I)) {
221 * Buggy application or kernel code has disabled interrupts
222 * and then trapped. Enabling interrupts now is wrong, but
223 * it is better than running with interrupts disabled until
224 * they are accidentally enabled later.
226 type = frame.tf_trapno;
227 if (ISPL(frame.tf_cs) == SEL_UPL || (frame.tf_eflags & PSL_VM))
229 "pid %ld (%s): trap %d with interrupts disabled\n",
230 (long)curproc->p_pid, curproc->p_comm, type);
231 else if (type != T_BPTFLT && type != T_TRCTRAP)
233 * XXX not quite right, since this may be for a
234 * multiple fault in user mode.
236 printf("kernel trap %d with interrupts disabled\n",
242 if (frame.tf_trapno == T_PAGEFLT) {
244 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
245 * This problem is worked around by using an interrupt
246 * gate for the pagefault handler. We are finally ready
247 * to read %cr2 and then must reenable interrupts.
249 * XXX this should be in the switch statement, but the
250 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
251 * flow of control too much for this to be obviously
258 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
261 type = frame.tf_trapno;
265 if (frame.tf_eflags & PSL_VM &&
266 (type == T_PROTFLT || type == T_STKFLT)) {
267 i = vm86_emulate((struct vm86frame *)&frame);
270 * returns to original process
272 vm86_trap((struct vm86frame *)&frame);
277 * these traps want either a process context, or
278 * assume a normal userspace trap.
282 trap_fatal(&frame, eva);
285 type = T_BPTFLT; /* kernel breakpoint */
288 goto kernel_trap; /* normal kernel trap handling */
291 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
294 sticks = p->p_sticks;
295 p->p_md.md_regs = &frame;
298 case T_PRIVINFLT: /* privileged instruction fault */
303 case T_BPTFLT: /* bpt instruction fault */
304 case T_TRCTRAP: /* trace trap */
305 frame.tf_eflags &= ~PSL_T;
309 case T_ARITHTRAP: /* arithmetic trap */
314 case T_ASTFLT: /* Allow process switch */
317 if (p->p_flag & P_OWEUPC) {
318 p->p_flag &= ~P_OWEUPC;
319 addupc_task(p, p->p_stats->p_prof.pr_addr,
320 p->p_stats->p_prof.pr_ticks);
325 * The following two traps can happen in
326 * vm86 mode, and, if so, we want to handle
329 case T_PROTFLT: /* general protection fault */
330 case T_STKFLT: /* stack fault */
331 if (frame.tf_eflags & PSL_VM) {
332 i = vm86_emulate((struct vm86frame *)&frame);
339 case T_SEGNPFLT: /* segment not present fault */
340 case T_TSSFLT: /* invalid TSS fault */
341 case T_DOUBLEFLT: /* double fault */
343 ucode = code + BUS_SEGM_FAULT ;
347 case T_PAGEFLT: /* page fault */
348 i = trap_pfault(&frame, TRUE, eva);
351 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
361 case T_DIVIDE: /* integer divide fault */
369 goto handle_powerfail;
370 #else /* !POWERFAIL_NMI */
372 /* NMI can be hooked up to a pushbutton for debugging */
373 printf ("NMI ... going to debugger\n");
374 if (kdb_trap (type, 0, &frame))
377 /* machine/parity/power fail/"kitchen sink" faults */
378 if (isa_nmi(code) == 0) return;
379 panic("NMI indicates hardware failure");
380 #endif /* POWERFAIL_NMI */
381 #endif /* NISA > 0 */
383 case T_OFLOW: /* integer overflow fault */
388 case T_BOUND: /* bounds check fault */
395 /* if a transparent fault (due to context switch "late") */
399 if (!pmath_emulate) {
401 ucode = FPE_FPU_NP_TRAP;
404 i = (*pmath_emulate)(&frame);
406 if (!(frame.tf_eflags & PSL_T))
408 frame.tf_eflags &= ~PSL_T;
411 /* else ucode = emulator_only_knows() XXX */
414 case T_FPOPFLT: /* FPU operand fetch fault */
424 case T_PAGEFLT: /* page fault */
425 (void) trap_pfault(&frame, FALSE, eva);
431 * The kernel is apparently using npx for copying.
432 * XXX this should be fatal unless the kernel has
433 * registered such use.
440 case T_PROTFLT: /* general protection fault */
441 case T_SEGNPFLT: /* segment not present fault */
443 * Invalid segment selectors and out of bounds
444 * %eip's and %esp's can be set up in user mode.
445 * This causes a fault in kernel mode when the
446 * kernel tries to return to user mode. We want
447 * to get this fault so that we can fix the
448 * problem here and not have to check all the
449 * selectors and pointers when the user changes
452 #define MAYBE_DORETI_FAULT(where, whereto) \
454 if (frame.tf_eip == (int)where) { \
455 frame.tf_eip = (int)whereto; \
460 if (intr_nesting_level == 0) {
462 * Invalid %fs's and %gs's can be created using
463 * procfs or PT_SETREGS or by invalidating the
464 * underlying LDT entry. This causes a fault
465 * in kernel mode when the kernel attempts to
466 * switch contexts. Lose the bad context
467 * (XXX) so that we can continue, and generate
470 if (frame.tf_eip == (int)cpu_switch_load_gs) {
475 MAYBE_DORETI_FAULT(doreti_iret,
477 MAYBE_DORETI_FAULT(doreti_popl_ds,
478 doreti_popl_ds_fault);
479 MAYBE_DORETI_FAULT(doreti_popl_es,
480 doreti_popl_es_fault);
481 MAYBE_DORETI_FAULT(doreti_popl_fs,
482 doreti_popl_fs_fault);
483 if (curpcb && curpcb->pcb_onfault) {
484 frame.tf_eip = (int)curpcb->pcb_onfault;
492 * PSL_NT can be set in user mode and isn't cleared
493 * automatically when the kernel is entered. This
494 * causes a TSS fault when the kernel attempts to
495 * `iret' because the TSS link is uninitialized. We
496 * want to get this fault so that we can fix the
497 * problem here and not every time the kernel is
500 if (frame.tf_eflags & PSL_NT) {
501 frame.tf_eflags &= ~PSL_NT;
506 case T_TRCTRAP: /* trace trap */
507 if (frame.tf_eip == (int)IDTVEC(syscall)) {
509 * We've just entered system mode via the
510 * syscall lcall. Continue single stepping
511 * silently until the syscall handler has
516 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
518 * The syscall handler has now saved the
519 * flags. Stop single stepping it.
521 frame.tf_eflags &= ~PSL_T;
529 * If DDB is enabled, let it handle the debugger trap.
530 * Otherwise, debugger traps "can't happen".
533 if (kdb_trap (type, 0, &frame))
542 # define TIMER_FREQ 1193182
546 static unsigned lastalert = 0;
548 if(time_second - lastalert > 10)
550 log(LOG_WARNING, "NMI: power fail\n");
551 sysbeep(TIMER_FREQ/880, hz);
552 lastalert = time_second;
556 #else /* !POWERFAIL_NMI */
558 /* NMI can be hooked up to a pushbutton for debugging */
559 printf ("NMI ... going to debugger\n");
560 if (kdb_trap (type, 0, &frame))
563 /* machine/parity/power fail/"kitchen sink" faults */
564 if (isa_nmi(code) == 0) return;
566 #endif /* POWERFAIL_NMI */
567 #endif /* NISA > 0 */
570 trap_fatal(&frame, eva);
574 /* Translate fault for emulators (e.g. Linux) */
575 if (*p->p_sysent->sv_transtrap)
576 i = (*p->p_sysent->sv_transtrap)(i, type);
578 trapsignal(p, i, ucode);
581 if (type <= MAX_TRAP_MSG) {
582 uprintf("fatal process exception: %s",
584 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
585 uprintf(", fault VA = 0x%lx", (u_long)eva);
591 userret(p, &frame, sticks);
596 * This version doesn't allow a page fault to user space while
597 * in the kernel. The rest of the kernel needs to be made "safe"
598 * before this can be used. I think the only things remaining
599 * to be made safe are the iBCS2 code and the process tracing/
603 trap_pfault(frame, usermode, eva)
604 struct trapframe *frame;
609 struct vmspace *vm = NULL;
613 struct proc *p = curproc;
615 if (frame->tf_err & PGEX_W)
616 ftype = VM_PROT_READ | VM_PROT_WRITE;
618 ftype = VM_PROT_READ;
620 va = trunc_page(eva);
621 if (va < VM_MIN_KERNEL_ADDRESS) {
626 (!usermode && va < VM_MAXUSER_ADDRESS &&
627 (intr_nesting_level != 0 || curpcb == NULL ||
628 curpcb->pcb_onfault == NULL))) {
629 trap_fatal(frame, eva);
634 * This is a fault on non-kernel virtual memory.
635 * vm is initialized above to NULL. If curproc is NULL
636 * or curproc->p_vmspace is NULL the fault is fatal.
645 * Keep swapout from messing with us during this
651 * Grow the stack if necessary
653 /* grow_stack returns false only if va falls into
654 * a growable stack region and the stack growth
655 * fails. It returns true if va was not within
656 * a growable stack region, or if the stack
659 if (!grow_stack (p, va)) {
665 /* Fault in the user page: */
666 rv = vm_fault(map, va, ftype,
667 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
673 * Don't allow user-mode faults in kernel address space.
679 * Since we know that kernel virtual address addresses
680 * always have pte pages mapped, we just have to fault
683 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
686 if (rv == KERN_SUCCESS)
690 if (intr_nesting_level == 0 && curpcb && curpcb->pcb_onfault) {
691 frame->tf_eip = (int)curpcb->pcb_onfault;
694 trap_fatal(frame, eva);
698 /* kludge to pass faulting virtual address to sendsig */
701 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
706 trap_pfault(frame, usermode, eva)
707 struct trapframe *frame;
712 struct vmspace *vm = NULL;
716 struct proc *p = curproc;
718 va = trunc_page(eva);
719 if (va >= KERNBASE) {
721 * Don't allow user-mode faults in kernel address space.
722 * An exception: if the faulting address is the invalid
723 * instruction entry in the IDT, then the Intel Pentium
724 * F00F bug workaround was triggered, and we need to
725 * treat it is as an illegal instruction, and not a page
728 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
729 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
730 frame->tf_trapno = T_PRIVINFLT;
740 * This is a fault on non-kernel virtual memory.
741 * vm is initialized above to NULL. If curproc is NULL
742 * or curproc->p_vmspace is NULL the fault is fatal.
753 if (frame->tf_err & PGEX_W)
754 ftype = VM_PROT_READ | VM_PROT_WRITE;
756 ftype = VM_PROT_READ;
758 if (map != kernel_map) {
760 * Keep swapout from messing with us during this
766 * Grow the stack if necessary
768 /* grow_stack returns false only if va falls into
769 * a growable stack region and the stack growth
770 * fails. It returns true if va was not within
771 * a growable stack region, or if the stack
774 if (!grow_stack (p, va)) {
780 /* Fault in the user page: */
781 rv = vm_fault(map, va, ftype,
782 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
788 * Don't have to worry about process locking or stacks in the kernel.
790 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
793 if (rv == KERN_SUCCESS)
797 if (intr_nesting_level == 0 && curpcb && curpcb->pcb_onfault) {
798 frame->tf_eip = (int)curpcb->pcb_onfault;
801 trap_fatal(frame, eva);
805 /* kludge to pass faulting virtual address to sendsig */
808 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
812 trap_fatal(frame, eva)
813 struct trapframe *frame;
816 int code, type, ss, esp;
817 struct soft_segment_descriptor softseg;
819 code = frame->tf_err;
820 type = frame->tf_trapno;
821 sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
823 if (type <= MAX_TRAP_MSG)
824 printf("\n\nFatal trap %d: %s while in %s mode\n",
825 type, trap_msg[type],
826 frame->tf_eflags & PSL_VM ? "vm86" :
827 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
829 /* three seperate prints in case of a trap on an unmapped page */
830 printf("mp_lock = %08x; ", mp_lock);
831 printf("cpuid = %d; ", cpuid);
832 printf("lapic.id = %08x\n", lapic.id);
834 if (type == T_PAGEFLT) {
835 printf("fault virtual address = 0x%x\n", eva);
836 printf("fault code = %s %s, %s\n",
837 code & PGEX_U ? "user" : "supervisor",
838 code & PGEX_W ? "write" : "read",
839 code & PGEX_P ? "protection violation" : "page not present");
841 printf("instruction pointer = 0x%x:0x%x\n",
842 frame->tf_cs & 0xffff, frame->tf_eip);
843 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
844 ss = frame->tf_ss & 0xffff;
847 ss = GSEL(GDATA_SEL, SEL_KPL);
848 esp = (int)&frame->tf_esp;
850 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
851 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
852 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
853 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
854 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
855 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
857 printf("processor eflags = ");
858 if (frame->tf_eflags & PSL_T)
859 printf("trace trap, ");
860 if (frame->tf_eflags & PSL_I)
861 printf("interrupt enabled, ");
862 if (frame->tf_eflags & PSL_NT)
863 printf("nested task, ");
864 if (frame->tf_eflags & PSL_RF)
866 if (frame->tf_eflags & PSL_VM)
868 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
869 printf("current process = ");
872 (u_long)curproc->p_pid, curproc->p_comm ?
873 curproc->p_comm : "");
877 printf("interrupt mask = ");
878 if ((cpl & net_imask) == net_imask)
880 if ((cpl & tty_imask) == tty_imask)
882 if ((cpl & bio_imask) == bio_imask)
884 if ((cpl & cam_imask) == cam_imask)
891 * we probably SHOULD have stopped the other CPUs before now!
892 * another CPU COULD have been touching cpl at this moment...
894 printf(" <- SMP: XXX");
903 if ((debugger_on_panic || in_Debugger) && kdb_trap(type, 0, frame))
906 printf("trap number = %d\n", type);
907 if (type <= MAX_TRAP_MSG)
908 panic(trap_msg[type]);
910 panic("unknown/reserved trap");
914 * Double fault handler. Called when a fault occurs while writing
915 * a frame for a trap/exception onto the stack. This usually occurs
916 * when the stack overflows (such is the case with infinite recursion,
919 * XXX Note that the current PTD gets replaced by IdlePTD when the
920 * task switch occurs. This means that the stack that was active at
921 * the time of the double fault is not available at <kstack> unless
922 * the machine was idle when the double fault occurred. The downside
923 * of this is that "trace <ebp>" in ddb won't work.
928 printf("\nFatal double fault:\n");
929 printf("eip = 0x%x\n", common_tss.tss_eip);
930 printf("esp = 0x%x\n", common_tss.tss_esp);
931 printf("ebp = 0x%x\n", common_tss.tss_ebp);
933 /* three seperate prints in case of a trap on an unmapped page */
934 printf("mp_lock = %08x; ", mp_lock);
935 printf("cpuid = %d; ", cpuid);
936 printf("lapic.id = %08x\n", lapic.id);
938 panic("double fault");
942 * Compensate for 386 brain damage (missing URKR).
943 * This is a little simpler than the pagefault handler in trap() because
944 * it the page tables have already been faulted in and high addresses
945 * are thrown out early for other reasons.
955 va = trunc_page((vm_offset_t)addr);
957 * XXX - MAX is END. Changed > to >= for temp. fix.
959 if (va >= VM_MAXUSER_ADDRESS)
967 if (!grow_stack (p, va)) {
973 * fault the data page
975 rv = vm_fault(&vm->vm_map, va, VM_PROT_READ|VM_PROT_WRITE, VM_FAULT_DIRTY);
979 if (rv != KERN_SUCCESS)
986 * System call request from POSIX system call gate interface to kernel.
987 * Like trap(), argument is call by reference.
991 struct trapframe frame;
995 struct sysent *callp;
996 struct proc *p = curproc;
1003 if (ISPL(frame.tf_cs) != SEL_UPL)
1006 sticks = p->p_sticks;
1007 p->p_md.md_regs = &frame;
1008 params = (caddr_t)frame.tf_esp + sizeof(int);
1009 code = frame.tf_eax;
1010 if (p->p_sysent->sv_prepsyscall) {
1011 (*p->p_sysent->sv_prepsyscall)(&frame, args, &code, ¶ms);
1014 * Need to check if this is a 32 bit or 64 bit syscall.
1016 if (code == SYS_syscall) {
1018 * Code is first argument, followed by actual args.
1020 code = fuword(params);
1021 params += sizeof(int);
1022 } else if (code == SYS___syscall) {
1024 * Like syscall, but code is a quad, so as to maintain
1025 * quad alignment for the rest of the arguments.
1027 code = fuword(params);
1028 params += sizeof(quad_t);
1032 if (p->p_sysent->sv_mask)
1033 code &= p->p_sysent->sv_mask;
1035 if (code >= p->p_sysent->sv_size)
1036 callp = &p->p_sysent->sv_table[0];
1038 callp = &p->p_sysent->sv_table[code];
1040 if (params && (i = callp->sy_narg * sizeof(int)) &&
1041 (error = copyin(params, (caddr_t)args, (u_int)i))) {
1043 if (KTRPOINT(p, KTR_SYSCALL))
1044 ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
1049 if (KTRPOINT(p, KTR_SYSCALL))
1050 ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
1053 p->p_retval[1] = frame.tf_edx;
1055 STOPEVENT(p, S_SCE, callp->sy_narg);
1057 error = (*callp->sy_call)(p, args);
1063 * Reinitialize proc pointer `p' as it may be different
1064 * if this is a child returning from fork syscall.
1067 frame.tf_eax = p->p_retval[0];
1068 frame.tf_edx = p->p_retval[1];
1069 frame.tf_eflags &= ~PSL_C;
1074 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1075 * int 0x80 is 2 bytes. We saved this in tf_err.
1077 frame.tf_eip -= frame.tf_err;
1085 if (p->p_sysent->sv_errsize) {
1086 if (error >= p->p_sysent->sv_errsize)
1087 error = -1; /* XXX */
1089 error = p->p_sysent->sv_errtbl[error];
1091 frame.tf_eax = error;
1092 frame.tf_eflags |= PSL_C;
1096 if ((frame.tf_eflags & PSL_T) && !(frame.tf_eflags & PSL_VM)) {
1097 /* Traced syscall. */
1098 frame.tf_eflags &= ~PSL_T;
1099 trapsignal(p, SIGTRAP, 0);
1102 userret(p, &frame, sticks);
1105 if (KTRPOINT(p, KTR_SYSRET))
1106 ktrsysret(p->p_tracep, code, error, p->p_retval[0]);
1110 * This works because errno is findable through the
1111 * register set. If we ever support an emulation where this
1112 * is not the case, this code will need to be revisited.
1114 STOPEVENT(p, S_SCX, code);
1119 * Simplified back end of syscall(), used when returning from fork()
1120 * directly into user mode.
1123 fork_return(p, frame)
1125 struct trapframe frame;
1127 frame.tf_eax = 0; /* Child returns zero */
1128 frame.tf_eflags &= ~PSL_C; /* success */
1131 userret(p, &frame, 0);
1133 if (KTRPOINT(p, KTR_SYSRET))
1134 ktrsysret(p->p_tracep, SYS_fork, 0, 0);