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