Merge branch 'vendor/OPENSSL' into HEAD
[dragonfly.git] / sys / platform / pc64 / x86_64 / trap.c
1 /*-
2  * Copyright (c) 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (C) 1994, David Greenman
5  * Copyright (c) 2008 The DragonFly Project.
6  * Copyright (c) 2008 Jordan Gordeev.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the University of Utah, and William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  * from: @(#)trap.c     7.4 (Berkeley) 5/13/91
40  * $FreeBSD: src/sys/i386/i386/trap.c,v 1.147.2.11 2003/02/27 19:09:59 luoqi Exp $
41  */
42
43 /*
44  * x86_64 Trap and System call handling
45  */
46
47 #include "opt_ddb.h"
48 #include "opt_ktrace.h"
49
50 #include <machine/frame.h>
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/kerneldump.h>
55 #include <sys/proc.h>
56 #include <sys/pioctl.h>
57 #include <sys/types.h>
58 #include <sys/signal2.h>
59 #include <sys/syscall.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysent.h>
62 #include <sys/systm.h>
63 #ifdef KTRACE
64 #include <sys/ktrace.h>
65 #endif
66 #include <sys/ktr.h>
67 #include <sys/sysmsg.h>
68 #include <sys/sysproto.h>
69 #include <sys/sysunion.h>
70
71 #include <vm/pmap.h>
72 #include <vm/vm.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_param.h>
76 #include <machine/cpu.h>
77 #include <machine/pcb.h>
78 #include <machine/smp.h>
79 #include <machine/thread.h>
80 #include <machine/vmparam.h>
81 #include <machine/md_var.h>
82 #include <machine_base/isa/intr_machdep.h>
83
84 #include <ddb/ddb.h>
85
86 #include <sys/thread2.h>
87 #include <sys/mplock2.h>
88
89 #ifdef SMP
90
91 #define MAKEMPSAFE(have_mplock)                 \
92         if (have_mplock == 0) {                 \
93                 get_mplock();                   \
94                 have_mplock = 1;                \
95         }
96
97 #else
98
99 #define MAKEMPSAFE(have_mplock)
100
101 #endif
102
103 extern void trap(struct trapframe *frame);
104
105 static int trap_pfault(struct trapframe *, int);
106 static void trap_fatal(struct trapframe *, vm_offset_t);
107 void dblfault_handler(struct trapframe *frame);
108
109 #define MAX_TRAP_MSG            30
110 static char *trap_msg[] = {
111         "",                                     /*  0 unused */
112         "privileged instruction fault",         /*  1 T_PRIVINFLT */
113         "",                                     /*  2 unused */
114         "breakpoint instruction fault",         /*  3 T_BPTFLT */
115         "",                                     /*  4 unused */
116         "",                                     /*  5 unused */
117         "arithmetic trap",                      /*  6 T_ARITHTRAP */
118         "system forced exception",              /*  7 T_ASTFLT */
119         "",                                     /*  8 unused */
120         "general protection fault",             /*  9 T_PROTFLT */
121         "trace trap",                           /* 10 T_TRCTRAP */
122         "",                                     /* 11 unused */
123         "page fault",                           /* 12 T_PAGEFLT */
124         "",                                     /* 13 unused */
125         "alignment fault",                      /* 14 T_ALIGNFLT */
126         "",                                     /* 15 unused */
127         "",                                     /* 16 unused */
128         "",                                     /* 17 unused */
129         "integer divide fault",                 /* 18 T_DIVIDE */
130         "non-maskable interrupt trap",          /* 19 T_NMI */
131         "overflow trap",                        /* 20 T_OFLOW */
132         "FPU bounds check fault",               /* 21 T_BOUND */
133         "FPU device not available",             /* 22 T_DNA */
134         "double fault",                         /* 23 T_DOUBLEFLT */
135         "FPU operand fetch fault",              /* 24 T_FPOPFLT */
136         "invalid TSS fault",                    /* 25 T_TSSFLT */
137         "segment not present fault",            /* 26 T_SEGNPFLT */
138         "stack fault",                          /* 27 T_STKFLT */
139         "machine check trap",                   /* 28 T_MCHK */
140         "SIMD floating-point exception",        /* 29 T_XMMFLT */
141         "reserved (unknown) fault",             /* 30 T_RESERVED */
142 };
143
144 #ifdef DDB
145 static int ddb_on_nmi = 1;
146 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
147         &ddb_on_nmi, 0, "Go to DDB on NMI");
148 static int ddb_on_seg_fault = 0;
149 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_seg_fault, CTLFLAG_RW,
150         &ddb_on_seg_fault, 0, "Go to DDB on user seg-fault");
151 #endif
152 static int panic_on_nmi = 1;
153 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
154         &panic_on_nmi, 0, "Panic on NMI");
155 static int fast_release;
156 SYSCTL_INT(_machdep, OID_AUTO, fast_release, CTLFLAG_RW,
157         &fast_release, 0, "Passive Release was optimal");
158 static int slow_release;
159 SYSCTL_INT(_machdep, OID_AUTO, slow_release, CTLFLAG_RW,
160         &slow_release, 0, "Passive Release was nonoptimal");
161
162 /*
163  * Passively intercepts the thread switch function to increase
164  * the thread priority from a user priority to a kernel priority, reducing
165  * syscall and trap overhead for the case where no switch occurs.
166  *
167  * Synchronizes td_ucred with p_ucred.  This is used by system calls,
168  * signal handling, faults, AST traps, and anything else that enters the
169  * kernel from userland and provides the kernel with a stable read-only
170  * copy of the process ucred.
171  */
172 static __inline void
173 userenter(struct thread *curtd, struct proc *curp)
174 {
175         struct ucred *ocred;
176         struct ucred *ncred;
177
178         curtd->td_release = lwkt_passive_release;
179
180         if (curtd->td_ucred != curp->p_ucred) {
181                 ncred = crhold(curp->p_ucred);
182                 ocred = curtd->td_ucred;
183                 curtd->td_ucred = ncred;
184                 if (ocred)
185                         crfree(ocred);
186         }
187 }
188
189 /*
190  * Handle signals, upcalls, profiling, and other AST's and/or tasks that
191  * must be completed before we can return to or try to return to userland.
192  *
193  * Note that td_sticks is a 64 bit quantity, but there's no point doing 64
194  * arithmatic on the delta calculation so the absolute tick values are
195  * truncated to an integer.
196  */
197 static void
198 userret(struct lwp *lp, struct trapframe *frame, int sticks)
199 {
200         struct proc *p = lp->lwp_proc;
201         int sig;
202
203         /*
204          * Charge system time if profiling.  Note: times are in microseconds.
205          * This may do a copyout and block, so do it first even though it
206          * means some system time will be charged as user time.
207          */
208         if (p->p_flag & P_PROFIL) {
209                 addupc_task(p, frame->tf_rip, 
210                         (u_int)((int)lp->lwp_thread->td_sticks - sticks));
211         }
212
213 recheck:
214         /*
215          * If the jungle wants us dead, so be it.
216          */
217         if (lp->lwp_flag & LWP_WEXIT) {
218                 get_mplock();
219                 lwp_exit(0);
220                 rel_mplock(); /* NOT REACHED */
221         }
222
223         /*
224          * Block here if we are in a stopped state.
225          */
226         if (p->p_stat == SSTOP || dump_stop_usertds) {
227                 get_mplock();
228                 tstop();
229                 rel_mplock();
230                 goto recheck;
231         }
232
233         /*
234          * Post any pending upcalls.  If running a virtual kernel be sure
235          * to restore the virtual kernel's vmspace before posting the upcall.
236          */
237         if (p->p_flag & P_UPCALLPEND) {
238                 p->p_flag &= ~P_UPCALLPEND;
239                 get_mplock();
240                 postupcall(lp);
241                 rel_mplock();
242                 goto recheck;
243         }
244
245         /*
246          * Post any pending signals.  If running a virtual kernel be sure
247          * to restore the virtual kernel's vmspace before posting the signal.
248          *
249          * WARNING!  postsig() can exit and not return.
250          */
251         if ((sig = CURSIG_TRACE(lp)) != 0) {
252                 get_mplock();
253                 postsig(sig);
254                 rel_mplock();
255                 goto recheck;
256         }
257
258         /*
259          * block here if we are swapped out, but still process signals
260          * (such as SIGKILL).  proc0 (the swapin scheduler) is already
261          * aware of our situation, we do not have to wake it up.
262          */
263         if (p->p_flag & P_SWAPPEDOUT) {
264                 get_mplock();
265                 p->p_flag |= P_SWAPWAIT;
266                 swapin_request();
267                 if (p->p_flag & P_SWAPWAIT)
268                         tsleep(p, PCATCH, "SWOUT", 0);
269                 p->p_flag &= ~P_SWAPWAIT;
270                 rel_mplock();
271                 goto recheck;
272         }
273
274         /*
275          * Make sure postsig() handled request to restore old signal mask after
276          * running signal handler.
277          */
278         KKASSERT((lp->lwp_flag & LWP_OLDMASK) == 0);
279 }
280
281 /*
282  * Cleanup from userenter and any passive release that might have occured.
283  * We must reclaim the current-process designation before we can return
284  * to usermode.  We also handle both LWKT and USER reschedule requests.
285  */
286 static __inline void
287 userexit(struct lwp *lp)
288 {
289         struct thread *td = lp->lwp_thread;
290 /*      globaldata_t gd = td->td_gd;*/
291
292         /*
293          * Handle stop requests at kernel priority.  Any requests queued
294          * after this loop will generate another AST.
295          */
296         while (lp->lwp_proc->p_stat == SSTOP) {
297                 get_mplock();
298                 tstop();
299                 rel_mplock();
300         }
301
302         /*
303          * Reduce our priority in preparation for a return to userland.  If
304          * our passive release function was still in place, our priority was
305          * never raised and does not need to be reduced.
306          */
307         lwkt_passive_recover(td);
308
309         /*
310          * Become the current user scheduled process if we aren't already,
311          * and deal with reschedule requests and other factors.
312          */
313         lp->lwp_proc->p_usched->acquire_curproc(lp);
314         /* WARNING: we may have migrated cpu's */
315         /* gd = td->td_gd; */
316 }
317
318 #if !defined(KTR_KERNENTRY)
319 #define KTR_KERNENTRY   KTR_ALL
320 #endif
321 KTR_INFO_MASTER(kernentry);
322 KTR_INFO(KTR_KERNENTRY, kernentry, trap, 0, "STR",
323          sizeof(long) + sizeof(long) + sizeof(long) + sizeof(vm_offset_t));
324 KTR_INFO(KTR_KERNENTRY, kernentry, trap_ret, 0, "STR",
325          sizeof(long) + sizeof(long));
326 KTR_INFO(KTR_KERNENTRY, kernentry, syscall, 0, "STR",
327          sizeof(long) + sizeof(long) + sizeof(long));
328 KTR_INFO(KTR_KERNENTRY, kernentry, syscall_ret, 0, "STR",
329          sizeof(long) + sizeof(long) + sizeof(long));
330 KTR_INFO(KTR_KERNENTRY, kernentry, fork_ret, 0, "STR",
331          sizeof(long) + sizeof(long));
332
333 /*
334  * Exception, fault, and trap interface to the kernel.
335  * This common code is called from assembly language IDT gate entry
336  * routines that prepare a suitable stack frame, and restore this
337  * frame after the exception has been processed.
338  *
339  * This function is also called from doreti in an interlock to handle ASTs.
340  * For example:  hardwareint->INTROUTINE->(set ast)->doreti->trap
341  *
342  * NOTE!  We have to retrieve the fault address prior to obtaining the
343  * MP lock because get_mplock() may switch out.  YYY cr2 really ought
344  * to be retrieved by the assembly code, not here.
345  *
346  * XXX gd_trap_nesting_level currently prevents lwkt_switch() from panicing
347  * if an attempt is made to switch from a fast interrupt or IPI.  This is
348  * necessary to properly take fatal kernel traps on SMP machines if 
349  * get_mplock() has to block.
350  */
351
352 void
353 trap(struct trapframe *frame)
354 {
355         struct globaldata *gd = mycpu;
356         struct thread *td = gd->gd_curthread;
357         struct lwp *lp = td->td_lwp;
358         struct proc *p;
359         int sticks = 0;
360         int i = 0, ucode = 0, type, code;
361 #ifdef SMP
362         int have_mplock = 0;
363 #endif
364 #ifdef INVARIANTS
365         int crit_count = td->td_critcount;
366         lwkt_tokref_t curstop = td->td_toks_stop;
367 #endif
368         vm_offset_t eva;
369
370         p = td->td_proc;
371
372 #ifdef DDB
373         /*
374          * We need to allow T_DNA faults when the debugger is active since
375          * some dumping paths do large bcopy() which use the floating
376          * point registers for faster copying.
377          */
378         if (db_active && frame->tf_trapno != T_DNA) {
379                 eva = (frame->tf_trapno == T_PAGEFLT ? frame->tf_addr : 0);
380                 ++gd->gd_trap_nesting_level;
381                 MAKEMPSAFE(have_mplock);
382                 trap_fatal(frame, eva);
383                 --gd->gd_trap_nesting_level;
384                 goto out2;
385         }
386 #endif
387
388         eva = 0;
389
390         if ((frame->tf_rflags & PSL_I) == 0) {
391                 /*
392                  * Buggy application or kernel code has disabled interrupts
393                  * and then trapped.  Enabling interrupts now is wrong, but
394                  * it is better than running with interrupts disabled until
395                  * they are accidentally enabled later.
396                  */
397                 type = frame->tf_trapno;
398                 if (ISPL(frame->tf_cs) == SEL_UPL) {
399                         MAKEMPSAFE(have_mplock);
400                         /* JG curproc can be NULL */
401                         kprintf(
402                             "pid %ld (%s): trap %d with interrupts disabled\n",
403                             (long)curproc->p_pid, curproc->p_comm, type);
404                 } else if (type != T_NMI && type != T_BPTFLT &&
405                     type != T_TRCTRAP) {
406                         /*
407                          * XXX not quite right, since this may be for a
408                          * multiple fault in user mode.
409                          */
410                         MAKEMPSAFE(have_mplock);
411                         kprintf("kernel trap %d with interrupts disabled\n",
412                             type);
413                 }
414                 cpu_enable_intr();
415         }
416
417         type = frame->tf_trapno;
418         code = frame->tf_err;
419
420         if (ISPL(frame->tf_cs) == SEL_UPL) {
421                 /* user trap */
422
423                 KTR_LOG(kernentry_trap, p->p_pid, lp->lwp_tid,
424                         frame->tf_trapno, eva);
425
426                 userenter(td, p);
427
428                 sticks = (int)td->td_sticks;
429                 lp->lwp_md.md_regs = frame;
430
431                 switch (type) {
432                 case T_PRIVINFLT:       /* privileged instruction fault */
433                         ucode = ILL_PRVOPC;
434                         i = SIGILL;
435                         break;
436
437                 case T_BPTFLT:          /* bpt instruction fault */
438                 case T_TRCTRAP:         /* trace trap */
439                         frame->tf_rflags &= ~PSL_T;
440                         ucode = TRAP_TRACE;
441                         i = SIGTRAP;
442                         break;
443
444                 case T_ARITHTRAP:       /* arithmetic trap */
445                         ucode = code;
446                         i = SIGFPE;
447 #if 0
448 #if JG
449                         ucode = fputrap();
450 #else
451                         ucode = code;
452 #endif
453                         i = SIGFPE;
454 #endif
455                         break;
456
457                 case T_ASTFLT:          /* Allow process switch */
458                         mycpu->gd_cnt.v_soft++;
459                         if (mycpu->gd_reqflags & RQF_AST_OWEUPC) {
460                                 atomic_clear_int_nonlocked(&mycpu->gd_reqflags,
461                                             RQF_AST_OWEUPC);
462                                 addupc_task(p, p->p_prof.pr_addr,
463                                             p->p_prof.pr_ticks);
464                         }
465                         goto out;
466
467                 case T_PROTFLT:         /* general protection fault */
468                         i = SIGBUS;
469                         ucode = BUS_OBJERR;
470                         break;
471                 case T_SEGNPFLT:        /* segment not present fault */
472                         i = SIGBUS;
473                         ucode = BUS_ADRERR;
474                         break;
475                 case T_TSSFLT:          /* invalid TSS fault */
476                 case T_DOUBLEFLT:       /* double fault */
477                         i = SIGBUS;
478                         ucode = BUS_OBJERR;
479                 default:
480 #if 0
481                         ucode = code + BUS_SEGM_FAULT ; /* XXX: ???*/
482 #endif
483                         ucode = BUS_OBJERR;
484                         i = SIGBUS;
485                         break;
486
487                 case T_PAGEFLT:         /* page fault */
488                         MAKEMPSAFE(have_mplock);
489                         i = trap_pfault(frame, TRUE);
490                         if (frame->tf_rip == 0)
491                                 kprintf("T_PAGEFLT: Warning %%rip == 0!\n");
492                         if (i == -1)
493                                 goto out;
494                         if (i == 0)
495                                 goto out;
496
497 #if 0
498                         ucode = T_PAGEFLT;
499 #endif
500                         if (i == SIGSEGV)
501                                 ucode = SEGV_MAPERR;
502                         else
503                                 ucode = BUS_ADRERR;
504                         break;
505
506                 case T_DIVIDE:          /* integer divide fault */
507                         ucode = FPE_INTDIV;
508                         i = SIGFPE;
509                         break;
510
511                 case T_NMI:
512                         MAKEMPSAFE(have_mplock);
513                         /* machine/parity/power fail/"kitchen sink" faults */
514                         if (isa_nmi(code) == 0) {
515 #ifdef DDB
516                                 /*
517                                  * NMI can be hooked up to a pushbutton
518                                  * for debugging.
519                                  */
520                                 if (ddb_on_nmi) {
521                                         kprintf ("NMI ... going to debugger\n");
522                                         kdb_trap(type, 0, frame);
523                                 }
524 #endif /* DDB */
525                                 goto out2;
526                         } else if (panic_on_nmi)
527                                 panic("NMI indicates hardware failure");
528                         break;
529
530                 case T_OFLOW:           /* integer overflow fault */
531                         ucode = FPE_INTOVF;
532                         i = SIGFPE;
533                         break;
534
535                 case T_BOUND:           /* bounds check fault */
536                         ucode = FPE_FLTSUB;
537                         i = SIGFPE;
538                         break;
539
540                 case T_DNA:
541                         /*
542                          * Virtual kernel intercept - pass the DNA exception
543                          * to the virtual kernel if it asked to handle it.
544                          * This occurs when the virtual kernel is holding
545                          * onto the FP context for a different emulated
546                          * process then the one currently running.
547                          *
548                          * We must still call npxdna() since we may have
549                          * saved FP state that the virtual kernel needs
550                          * to hand over to a different emulated process.
551                          */
552                         if (lp->lwp_vkernel && lp->lwp_vkernel->ve &&
553                             (td->td_pcb->pcb_flags & FP_VIRTFP)
554                         ) {
555                                 npxdna();
556                                 break;
557                         }
558
559                         /*
560                          * The kernel may have switched out the FP unit's
561                          * state, causing the user process to take a fault
562                          * when it tries to use the FP unit.  Restore the
563                          * state here
564                          */
565                         if (npxdna())
566                                 goto out;
567                         i = SIGFPE;
568                         ucode = FPE_FPU_NP_TRAP;
569                         break;
570
571                 case T_FPOPFLT:         /* FPU operand fetch fault */
572                         ucode = ILL_COPROC;
573                         i = SIGILL;
574                         break;
575
576                 case T_XMMFLT:          /* SIMD floating-point exception */
577                         ucode = 0; /* XXX */
578                         i = SIGFPE;
579                         break;
580                 }
581         } else {
582                 /* kernel trap */
583
584                 switch (type) {
585                 case T_PAGEFLT:                 /* page fault */
586                         MAKEMPSAFE(have_mplock);
587                         trap_pfault(frame, FALSE);
588                         goto out2;
589
590                 case T_DNA:
591                         /*
592                          * The kernel is apparently using fpu for copying.
593                          * XXX this should be fatal unless the kernel has
594                          * registered such use.
595                          */
596                         if (npxdna())
597                                 goto out2;
598                         break;
599
600                 case T_STKFLT:          /* stack fault */
601                         break;
602
603                 case T_PROTFLT:         /* general protection fault */
604                 case T_SEGNPFLT:        /* segment not present fault */
605                         /*
606                          * Invalid segment selectors and out of bounds
607                          * %rip's and %rsp's can be set up in user mode.
608                          * This causes a fault in kernel mode when the
609                          * kernel tries to return to user mode.  We want
610                          * to get this fault so that we can fix the
611                          * problem here and not have to check all the
612                          * selectors and pointers when the user changes
613                          * them.
614                          */
615                         if (mycpu->gd_intr_nesting_level == 0) {
616                                 if (td->td_pcb->pcb_onfault) {
617                                         frame->tf_rip = (register_t)
618                                                 td->td_pcb->pcb_onfault;
619                                         goto out2;
620                                 }
621                                 if (frame->tf_rip == (long)doreti_iret) {
622                                         frame->tf_rip = (long)doreti_iret_fault;
623                                         goto out2;
624                                 }
625                         }
626                         break;
627
628                 case T_TSSFLT:
629                         /*
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
636                          * entered.
637                          */
638                         if (frame->tf_rflags & PSL_NT) {
639                                 frame->tf_rflags &= ~PSL_NT;
640                                 goto out2;
641                         }
642                         break;
643
644                 case T_TRCTRAP:  /* trace trap */
645 #if 0
646                         if (frame->tf_rip == (int)IDTVEC(syscall)) {
647                                 /*
648                                  * We've just entered system mode via the
649                                  * syscall lcall.  Continue single stepping
650                                  * silently until the syscall handler has
651                                  * saved the flags.
652                                  */
653                                 goto out2;
654                         }
655                         if (frame->tf_rip == (int)IDTVEC(syscall) + 1) {
656                                 /*
657                                  * The syscall handler has now saved the
658                                  * flags.  Stop single stepping it.
659                                  */
660                                 frame->tf_rflags &= ~PSL_T;
661                                 goto out2;
662                         }
663 #endif
664
665                         /*
666                          * Ignore debug register trace traps due to
667                          * accesses in the user's address space, which
668                          * can happen under several conditions such as
669                          * if a user sets a watchpoint on a buffer and
670                          * then passes that buffer to a system call.
671                          * We still want to get TRCTRAPS for addresses
672                          * in kernel space because that is useful when
673                          * debugging the kernel.
674                          */
675 #if JG
676                         if (user_dbreg_trap()) {
677                                 /*
678                                  * Reset breakpoint bits because the
679                                  * processor doesn't
680                                  */
681                                 /* XXX check upper bits here */
682                                 load_dr6(rdr6() & 0xfffffff0);
683                                 goto out2;
684                         }
685 #endif
686                         /*
687                          * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
688                          */
689                 case T_BPTFLT:
690                         /*
691                          * If DDB is enabled, let it handle the debugger trap.
692                          * Otherwise, debugger traps "can't happen".
693                          */
694                         ucode = TRAP_BRKPT;
695 #ifdef DDB
696                         MAKEMPSAFE(have_mplock);
697                         if (kdb_trap(type, 0, frame))
698                                 goto out2;
699 #endif
700                         break;
701
702                 case T_NMI:
703                         MAKEMPSAFE(have_mplock);
704                         /* machine/parity/power fail/"kitchen sink" faults */
705 #if NISA > 0
706                         if (isa_nmi(code) == 0) {
707 #ifdef DDB
708                                 /*
709                                  * NMI can be hooked up to a pushbutton
710                                  * for debugging.
711                                  */
712                                 if (ddb_on_nmi) {
713                                         kprintf ("NMI ... going to debugger\n");
714                                         kdb_trap(type, 0, frame);
715                                 }
716 #endif /* DDB */
717                                 goto out2;
718                         } else if (panic_on_nmi == 0)
719                                 goto out2;
720                         /* FALL THROUGH */
721 #endif /* NISA > 0 */
722                 }
723                 MAKEMPSAFE(have_mplock);
724                 trap_fatal(frame, 0);
725                 goto out2;
726         }
727
728         /*
729          * Virtual kernel intercept - if the fault is directly related to a
730          * VM context managed by a virtual kernel then let the virtual kernel
731          * handle it.
732          */
733         if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
734                 vkernel_trap(lp, frame);
735                 goto out;
736         }
737
738         /*
739          * Translate fault for emulators (e.g. Linux) 
740          */
741         if (*p->p_sysent->sv_transtrap)
742                 i = (*p->p_sysent->sv_transtrap)(i, type);
743
744         MAKEMPSAFE(have_mplock);
745         trapsignal(lp, i, ucode);
746
747 #ifdef DEBUG
748         if (type <= MAX_TRAP_MSG) {
749                 uprintf("fatal process exception: %s",
750                         trap_msg[type]);
751                 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
752                         uprintf(", fault VA = 0x%lx", frame->tf_addr);
753                 uprintf("\n");
754         }
755 #endif
756
757 out:
758 #ifdef SMP
759         if (ISPL(frame->tf_cs) == SEL_UPL) {
760                 KASSERT(td->td_mpcount == have_mplock,
761                         ("badmpcount trap/end from %p", (void *)frame->tf_rip));
762         }
763 #endif
764         userret(lp, frame, sticks);
765         userexit(lp);
766 out2:   ;
767 #ifdef SMP
768         if (have_mplock)
769                 rel_mplock();
770 #endif
771         if (p != NULL && lp != NULL)
772                 KTR_LOG(kernentry_trap_ret, p->p_pid, lp->lwp_tid);
773 #ifdef INVARIANTS
774         KASSERT(crit_count == td->td_critcount,
775                 ("trap: critical section count mismatch! %d/%d",
776                 crit_count, td->td_pri));
777         KASSERT(curstop == td->td_toks_stop,
778                 ("trap: extra tokens held after trap! %ld/%ld",
779                 curstop - &td->td_toks_base,
780                 td->td_toks_stop - &td->td_toks_base));
781 #endif
782 }
783
784 static int
785 trap_pfault(struct trapframe *frame, int usermode)
786 {
787         vm_offset_t va;
788         struct vmspace *vm = NULL;
789         vm_map_t map;
790         int rv = 0;
791         int fault_flags;
792         vm_prot_t ftype;
793         thread_t td = curthread;
794         struct lwp *lp = td->td_lwp;
795         struct proc *p;
796
797         va = trunc_page(frame->tf_addr);
798         if (va >= VM_MIN_KERNEL_ADDRESS) {
799                 /*
800                  * Don't allow user-mode faults in kernel address space.
801                  */
802                 if (usermode) {
803                         fault_flags = -1;
804                         ftype = -1;
805                         goto nogo;
806                 }
807
808                 map = &kernel_map;
809         } else {
810                 /*
811                  * This is a fault on non-kernel virtual memory.
812                  * vm is initialized above to NULL. If curproc is NULL
813                  * or curproc->p_vmspace is NULL the fault is fatal.
814                  */
815                 if (lp != NULL)
816                         vm = lp->lwp_vmspace;
817
818                 if (vm == NULL) {
819                         fault_flags = -1;
820                         ftype = -1;
821                         goto nogo;
822                 }
823
824                 map = &vm->vm_map;
825         }
826
827         /*
828          * PGEX_I is defined only if the execute disable bit capability is
829          * supported and enabled.
830          */
831         if (frame->tf_err & PGEX_W)
832                 ftype = VM_PROT_WRITE;
833 #if JG
834         else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
835                 ftype = VM_PROT_EXECUTE;
836 #endif
837         else
838                 ftype = VM_PROT_READ;
839
840         if (map != &kernel_map) {
841                 /*
842                  * Keep swapout from messing with us during this
843                  *      critical time.
844                  */
845                 PHOLD(lp->lwp_proc);
846
847                 /*
848                  * Issue fault
849                  */
850                 fault_flags = 0;
851                 if (usermode)
852                         fault_flags |= VM_FAULT_BURST;
853                 if (ftype & VM_PROT_WRITE)
854                         fault_flags |= VM_FAULT_DIRTY;
855                 else
856                         fault_flags |= VM_FAULT_NORMAL;
857                 rv = vm_fault(map, va, ftype, fault_flags);
858
859                 PRELE(lp->lwp_proc);
860         } else {
861                 /*
862                  * Don't have to worry about process locking or stacks
863                  * in the kernel.
864                  */
865                 fault_flags = VM_FAULT_NORMAL;
866                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
867         }
868
869         if (rv == KERN_SUCCESS)
870                 return (0);
871 nogo:
872         if (!usermode) {
873                 if (td->td_gd->gd_intr_nesting_level == 0 &&
874                     td->td_pcb->pcb_onfault) {
875                         frame->tf_rip = (register_t)td->td_pcb->pcb_onfault;
876                         return (0);
877                 }
878                 trap_fatal(frame, frame->tf_addr);
879                 return (-1);
880         }
881
882         /*
883          * NOTE: on x86_64 we have a tf_addr field in the trapframe, no
884          * kludge is needed to pass the fault address to signal handlers.
885          */
886         p = td->td_proc;
887         if (td->td_lwp->lwp_vkernel == NULL) {
888                 kprintf("seg-fault ft=%04x ff=%04x addr=%p rip=%p "
889                         "pid=%d p_comm=%s\n",
890                         ftype, fault_flags,
891                         (void *)frame->tf_addr,
892                         (void *)frame->tf_rip,
893                         p->p_pid, p->p_comm);
894                 if (ddb_on_seg_fault)
895                         Debugger("ddb_on_seg_fault");
896         }
897         /* Debugger("seg-fault"); */
898
899         return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
900 }
901
902 static void
903 trap_fatal(struct trapframe *frame, vm_offset_t eva)
904 {
905         int code, ss;
906         u_int type;
907         long rsp;
908         struct soft_segment_descriptor softseg;
909         char *msg;
910
911         code = frame->tf_err;
912         type = frame->tf_trapno;
913         sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)], &softseg);
914
915         if (type <= MAX_TRAP_MSG)
916                 msg = trap_msg[type];
917         else
918                 msg = "UNKNOWN";
919         kprintf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
920             ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
921 #ifdef SMP
922         /* three separate prints in case of a trap on an unmapped page */
923         kprintf("mp_lock = %08x; ", mp_lock);
924         kprintf("cpuid = %d; ", mycpu->gd_cpuid);
925         kprintf("lapic->id = %08x\n", lapic->id);
926 #endif
927         if (type == T_PAGEFLT) {
928                 kprintf("fault virtual address  = 0x%lx\n", eva);
929                 kprintf("fault code             = %s %s %s, %s\n",
930                         code & PGEX_U ? "user" : "supervisor",
931                         code & PGEX_W ? "write" : "read",
932                         code & PGEX_I ? "instruction" : "data",
933                         code & PGEX_P ? "protection violation" : "page not present");
934         }
935         kprintf("instruction pointer    = 0x%lx:0x%lx\n",
936                frame->tf_cs & 0xffff, frame->tf_rip);
937         if (ISPL(frame->tf_cs) == SEL_UPL) {
938                 ss = frame->tf_ss & 0xffff;
939                 rsp = frame->tf_rsp;
940         } else {
941                 ss = GSEL(GDATA_SEL, SEL_KPL);
942                 rsp = (long)&frame->tf_rsp;
943         }
944         kprintf("stack pointer          = 0x%x:0x%lx\n", ss, rsp);
945         kprintf("frame pointer          = 0x%x:0x%lx\n", ss, frame->tf_rbp);
946         kprintf("code segment           = base 0x%lx, limit 0x%lx, type 0x%x\n",
947                softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
948         kprintf("                       = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
949                softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
950                softseg.ssd_gran);
951         kprintf("processor eflags       = ");
952         if (frame->tf_rflags & PSL_T)
953                 kprintf("trace trap, ");
954         if (frame->tf_rflags & PSL_I)
955                 kprintf("interrupt enabled, ");
956         if (frame->tf_rflags & PSL_NT)
957                 kprintf("nested task, ");
958         if (frame->tf_rflags & PSL_RF)
959                 kprintf("resume, ");
960         kprintf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
961         kprintf("current process                = ");
962         if (curproc) {
963                 kprintf("%lu\n",
964                     (u_long)curproc->p_pid);
965         } else {
966                 kprintf("Idle\n");
967         }
968         kprintf("current thread          = pri %d ", curthread->td_pri);
969         if (curthread->td_critcount)
970                 kprintf("(CRIT)");
971         kprintf("\n");
972
973 #ifdef DDB
974         if ((debugger_on_panic || db_active) && kdb_trap(type, code, frame))
975                 return;
976 #endif
977         kprintf("trap number            = %d\n", type);
978         if (type <= MAX_TRAP_MSG)
979                 panic("%s", trap_msg[type]);
980         else
981                 panic("unknown/reserved trap");
982 }
983
984 /*
985  * Double fault handler. Called when a fault occurs while writing
986  * a frame for a trap/exception onto the stack. This usually occurs
987  * when the stack overflows (such is the case with infinite recursion,
988  * for example).
989  */
990 static __inline
991 int
992 in_kstack_guard(register_t rptr)
993 {
994         thread_t td = curthread;
995
996         if ((char *)rptr >= td->td_kstack &&
997             (char *)rptr < td->td_kstack + PAGE_SIZE) {
998                 return 1;
999         }
1000         return 0;
1001 }
1002
1003 void
1004 dblfault_handler(struct trapframe *frame)
1005 {
1006         thread_t td = curthread;
1007
1008         if (in_kstack_guard(frame->tf_rsp) || in_kstack_guard(frame->tf_rbp)) {
1009                 kprintf("DOUBLE FAULT - KERNEL STACK GUARD HIT!\n");
1010                 if (in_kstack_guard(frame->tf_rsp))
1011                         frame->tf_rsp = (register_t)(td->td_kstack + PAGE_SIZE);
1012                 if (in_kstack_guard(frame->tf_rbp))
1013                         frame->tf_rbp = (register_t)(td->td_kstack + PAGE_SIZE);
1014         } else {
1015                 kprintf("DOUBLE FAULT\n");
1016         }
1017         kprintf("\nFatal double fault\n");
1018         kprintf("rip = 0x%lx\n", frame->tf_rip);
1019         kprintf("rsp = 0x%lx\n", frame->tf_rsp);
1020         kprintf("rbp = 0x%lx\n", frame->tf_rbp);
1021 #ifdef SMP
1022         /* three separate prints in case of a trap on an unmapped page */
1023         kprintf("mp_lock = %08x; ", mp_lock);
1024         kprintf("cpuid = %d; ", mycpu->gd_cpuid);
1025         kprintf("lapic->id = %08x\n", lapic->id);
1026 #endif
1027         panic("double fault");
1028 }
1029
1030 /*
1031  * syscall2 -   MP aware system call request C handler
1032  *
1033  * A system call is essentially treated as a trap except that the
1034  * MP lock is not held on entry or return.  We are responsible for
1035  * obtaining the MP lock if necessary and for handling ASTs
1036  * (e.g. a task switch) prior to return.
1037  *
1038  * MPSAFE
1039  */
1040 void
1041 syscall2(struct trapframe *frame)
1042 {
1043         struct thread *td = curthread;
1044         struct proc *p = td->td_proc;
1045         struct lwp *lp = td->td_lwp;
1046         caddr_t params;
1047         struct sysent *callp;
1048         register_t orig_tf_rflags;
1049         int sticks;
1050         int error;
1051         int narg;
1052 #ifdef INVARIANTS
1053         int crit_count = td->td_critcount;
1054 #endif
1055 #ifdef SMP
1056         int have_mplock = 0;
1057 #endif
1058         register_t *argp;
1059         u_int code;
1060         int reg, regcnt;
1061         union sysunion args;
1062         register_t *argsdst;
1063
1064         mycpu->gd_cnt.v_syscall++;
1065
1066 #ifdef DIAGNOSTIC
1067         if (ISPL(frame->tf_cs) != SEL_UPL) {
1068                 get_mplock();
1069                 panic("syscall");
1070                 /* NOT REACHED */
1071         }
1072 #endif
1073
1074         KTR_LOG(kernentry_syscall, p->p_pid, lp->lwp_tid,
1075                 frame->tf_rax);
1076
1077 #ifdef SMP
1078         KASSERT(td->td_mpcount == 0,
1079                 ("badmpcount syscall2 from %p", (void *)frame->tf_rip));
1080 #endif
1081         userenter(td, p);       /* lazy raise our priority */
1082
1083         reg = 0;
1084         regcnt = 6;
1085         /*
1086          * Misc
1087          */
1088         sticks = (int)td->td_sticks;
1089         orig_tf_rflags = frame->tf_rflags;
1090
1091         /*
1092          * Virtual kernel intercept - if a VM context managed by a virtual
1093          * kernel issues a system call the virtual kernel handles it, not us.
1094          * Restore the virtual kernel context and return from its system
1095          * call.  The current frame is copied out to the virtual kernel.
1096          */
1097         if (lp->lwp_vkernel && lp->lwp_vkernel->ve) {
1098                 vkernel_trap(lp, frame);
1099                 error = EJUSTRETURN;
1100                 goto out;
1101         }
1102
1103         /*
1104          * Get the system call parameters and account for time
1105          */
1106         lp->lwp_md.md_regs = frame;
1107         params = (caddr_t)frame->tf_rsp + sizeof(register_t);
1108         code = frame->tf_rax;
1109
1110         if (p->p_sysent->sv_prepsyscall) {
1111                 (*p->p_sysent->sv_prepsyscall)(
1112                         frame, (int *)(&args.nosys.sysmsg + 1),
1113                         &code, &params);
1114         } else {
1115                 if (code == SYS_syscall || code == SYS___syscall) {
1116                         code = frame->tf_rdi;
1117                         reg++;
1118                         regcnt--;
1119                 }
1120         }
1121
1122         if (p->p_sysent->sv_mask)
1123                 code &= p->p_sysent->sv_mask;
1124
1125         if (code >= p->p_sysent->sv_size)
1126                 callp = &p->p_sysent->sv_table[0];
1127         else
1128                 callp = &p->p_sysent->sv_table[code];
1129
1130         narg = callp->sy_narg & SYF_ARGMASK;
1131
1132         /*
1133          * On x86_64 we get up to six arguments in registers. The rest are
1134          * on the stack. The first six members of 'struct trapframe' happen
1135          * to be the registers used to pass arguments, in exactly the right
1136          * order.
1137          */
1138         argp = &frame->tf_rdi;
1139         argp += reg;
1140         argsdst = (register_t *)(&args.nosys.sysmsg + 1);
1141         /*
1142          * JG can we overflow the space pointed to by 'argsdst'
1143          * either with 'bcopy' or with 'copyin'?
1144          */
1145         bcopy(argp, argsdst, sizeof(register_t) * regcnt);
1146         /*
1147          * copyin is MP aware, but the tracing code is not
1148          */
1149         if (narg > regcnt) {
1150                 KASSERT(params != NULL, ("copyin args with no params!"));
1151                 error = copyin(params, &argsdst[regcnt],
1152                         (narg - regcnt) * sizeof(register_t));
1153                 if (error) {
1154 #ifdef KTRACE
1155                         if (KTRPOINT(td, KTR_SYSCALL)) {
1156                                 MAKEMPSAFE(have_mplock);
1157                                 
1158                                 ktrsyscall(lp, code, narg,
1159                                         (void *)(&args.nosys.sysmsg + 1));
1160                         }
1161 #endif
1162                         goto bad;
1163                 }
1164         }
1165
1166 #ifdef KTRACE
1167         if (KTRPOINT(td, KTR_SYSCALL)) {
1168                 MAKEMPSAFE(have_mplock);
1169                 ktrsyscall(lp, code, narg, (void *)(&args.nosys.sysmsg + 1));
1170         }
1171 #endif
1172
1173         /*
1174          * Default return value is 0 (will be copied to %rax).  Double-value
1175          * returns use %rax and %rdx.  %rdx is left unchanged for system
1176          * calls which return only one result.
1177          */
1178         args.sysmsg_fds[0] = 0;
1179         args.sysmsg_fds[1] = frame->tf_rdx;
1180
1181         /*
1182          * The syscall might manipulate the trap frame. If it does it
1183          * will probably return EJUSTRETURN.
1184          */
1185         args.sysmsg_frame = frame;
1186
1187         STOPEVENT(p, S_SCE, narg);      /* MP aware */
1188
1189         /*
1190          * NOTE: All system calls run MPSAFE now.  The system call itself
1191          *       is responsible for getting the MP lock.
1192          */
1193         error = (*callp->sy_call)(&args);
1194
1195 out:
1196         /*
1197          * MP SAFE (we may or may not have the MP lock at this point)
1198          */
1199         //kprintf("SYSMSG %d ", error);
1200         switch (error) {
1201         case 0:
1202                 /*
1203                  * Reinitialize proc pointer `p' as it may be different
1204                  * if this is a child returning from fork syscall.
1205                  */
1206                 p = curproc;
1207                 lp = curthread->td_lwp;
1208                 frame->tf_rax = args.sysmsg_fds[0];
1209                 frame->tf_rdx = args.sysmsg_fds[1];
1210                 frame->tf_rflags &= ~PSL_C;
1211                 break;
1212         case ERESTART:
1213                 /*
1214                  * Reconstruct pc, we know that 'syscall' is 2 bytes.
1215                  * We have to do a full context restore so that %r10
1216                  * (which was holding the value of %rcx) is restored for
1217                  * the next iteration.
1218                  */
1219                 frame->tf_rip -= frame->tf_err;
1220                 frame->tf_r10 = frame->tf_rcx;
1221                 break;
1222         case EJUSTRETURN:
1223                 break;
1224         case EASYNC:
1225                 panic("Unexpected EASYNC return value (for now)");
1226         default:
1227 bad:
1228                 if (p->p_sysent->sv_errsize) {
1229                         if (error >= p->p_sysent->sv_errsize)
1230                                 error = -1;     /* XXX */
1231                         else
1232                                 error = p->p_sysent->sv_errtbl[error];
1233                 }
1234                 frame->tf_rax = error;
1235                 frame->tf_rflags |= PSL_C;
1236                 break;
1237         }
1238
1239         /*
1240          * Traced syscall.  trapsignal() is not MP aware.
1241          */
1242         if (orig_tf_rflags & PSL_T) {
1243                 MAKEMPSAFE(have_mplock);
1244                 frame->tf_rflags &= ~PSL_T;
1245                 trapsignal(lp, SIGTRAP, TRAP_TRACE);
1246         }
1247
1248         /*
1249          * Handle reschedule and other end-of-syscall issues
1250          */
1251         userret(lp, frame, sticks);
1252
1253 #ifdef KTRACE
1254         if (KTRPOINT(td, KTR_SYSRET)) {
1255                 MAKEMPSAFE(have_mplock);
1256                 ktrsysret(lp, code, error, args.sysmsg_result);
1257         }
1258 #endif
1259
1260         /*
1261          * This works because errno is findable through the
1262          * register set.  If we ever support an emulation where this
1263          * is not the case, this code will need to be revisited.
1264          */
1265         STOPEVENT(p, S_SCX, code);
1266
1267         userexit(lp);
1268 #ifdef SMP
1269         /*
1270          * Release the MP lock if we had to get it
1271          */
1272         KASSERT(td->td_mpcount == have_mplock, 
1273                 ("badmpcount syscall2/end from %p", (void *)frame->tf_rip));
1274         if (have_mplock)
1275                 rel_mplock();
1276 #endif
1277         KTR_LOG(kernentry_syscall_ret, p->p_pid, lp->lwp_tid, error);
1278 #ifdef INVARIANTS
1279         KASSERT(crit_count == td->td_critcount,
1280                 ("syscall: critical section count mismatch! %d/%d",
1281                 crit_count, td->td_pri));
1282         KASSERT(&td->td_toks_base == td->td_toks_stop,
1283                 ("syscall: extra tokens held after trap! %ld",
1284                 td->td_toks_stop - &td->td_toks_base));
1285 #endif
1286 }
1287
1288 /*
1289  * NOTE: mplock not held at any point
1290  */
1291 void
1292 fork_return(struct lwp *lp, struct trapframe *frame)
1293 {
1294         frame->tf_rax = 0;              /* Child returns zero */
1295         frame->tf_rflags &= ~PSL_C;     /* success */
1296         frame->tf_rdx = 1;
1297
1298         generic_lwp_return(lp, frame);
1299         KTR_LOG(kernentry_fork_ret, lp->lwp_proc->p_pid, lp->lwp_tid);
1300 }
1301
1302 /*
1303  * Simplified back end of syscall(), used when returning from fork()
1304  * directly into user mode.
1305  *
1306  * This code will return back into the fork trampoline code which then
1307  * runs doreti.
1308  *
1309  * NOTE: The mplock is not held at any point.
1310  */
1311 void
1312 generic_lwp_return(struct lwp *lp, struct trapframe *frame)
1313 {
1314         struct proc *p = lp->lwp_proc;
1315
1316         /*
1317          * Newly forked processes are given a kernel priority.  We have to
1318          * adjust the priority to a normal user priority and fake entry
1319          * into the kernel (call userenter()) to install a passive release
1320          * function just in case userret() decides to stop the process.  This
1321          * can occur when ^Z races a fork.  If we do not install the passive
1322          * release function the current process designation will not be
1323          * released when the thread goes to sleep.
1324          */
1325         lwkt_setpri_self(TDPRI_USER_NORM);
1326         userenter(lp->lwp_thread, p);
1327         userret(lp, frame, 0);
1328 #ifdef KTRACE
1329         if (KTRPOINT(lp->lwp_thread, KTR_SYSRET))
1330                 ktrsysret(lp, SYS_fork, 0, 0);
1331 #endif
1332         p->p_flag |= P_PASSIVE_ACQ;
1333         userexit(lp);
1334         p->p_flag &= ~P_PASSIVE_ACQ;
1335 }
1336
1337 /*
1338  * If PGEX_FPFAULT is set then set FP_VIRTFP in the PCB to force a T_DNA
1339  * fault (which is then passed back to the virtual kernel) if an attempt is
1340  * made to use the FP unit.
1341  *
1342  * XXX this is a fairly big hack.
1343  */
1344 void
1345 set_vkernel_fp(struct trapframe *frame)
1346 {
1347         struct thread *td = curthread;
1348
1349         if (frame->tf_xflags & PGEX_FPFAULT) {
1350                 td->td_pcb->pcb_flags |= FP_VIRTFP;
1351                 if (mdcpu->gd_npxthread == td)
1352                         npxexit();
1353         } else {
1354                 td->td_pcb->pcb_flags &= ~FP_VIRTFP;
1355         }
1356 }
1357
1358 /*
1359  * Called from vkernel_trap() to fixup the vkernel's syscall
1360  * frame for vmspace_ctl() return.
1361  */
1362 void
1363 cpu_vkernel_trap(struct trapframe *frame, int error)
1364 {
1365         frame->tf_rax = error;
1366         if (error)
1367                 frame->tf_rflags |= PSL_C;
1368         else
1369                 frame->tf_rflags &= ~PSL_C;
1370 }