Add additional functionality to the upcall support to allow us to wait for
[dragonfly.git] / sys / i386 / i386 / 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/i386/i386/Attic/trap.c,v 1.44 2003/12/07 04:20:39 dillon Exp $
40  */
41
42 /*
43  * 386 Trap and System call handling
44  */
45
46 #include "use_isa.h"
47 #include "use_npx.h"
48
49 #include "opt_cpu.h"
50 #include "opt_ddb.h"
51 #include "opt_ktrace.h"
52 #include "opt_clock.h"
53 #include "opt_trap.h"
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/proc.h>
58 #include <sys/pioctl.h>
59 #include <sys/kernel.h>
60 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
62 #include <sys/syscall.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/uio.h>
66 #include <sys/vmmeter.h>
67 #include <sys/malloc.h>
68 #ifdef KTRACE
69 #include <sys/ktrace.h>
70 #endif
71 #include <sys/upcall.h>
72 #include <sys/sysproto.h>
73 #include <sys/sysunion.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/ipl.h>
86 #include <machine/md_var.h>
87 #include <machine/pcb.h>
88 #ifdef SMP
89 #include <machine/smp.h>
90 #endif
91 #include <machine/tss.h>
92 #include <machine/globaldata.h>
93
94 #include <i386/isa/intr_machdep.h>
95
96 #ifdef POWERFAIL_NMI
97 #include <sys/syslog.h>
98 #include <machine/clock.h>
99 #endif
100
101 #include <machine/vm86.h>
102
103 #include <ddb/ddb.h>
104 #include <sys/msgport2.h>
105 #include <sys/thread2.h>
106
107 int (*pmath_emulate) (struct trapframe *);
108
109 extern void trap (struct trapframe frame);
110 extern int trapwrite (unsigned addr);
111 extern void syscall2 (struct trapframe frame);
112 extern void sendsys2 (struct trapframe frame);
113
114 static int trap_pfault (struct trapframe *, int, vm_offset_t);
115 static void trap_fatal (struct trapframe *, vm_offset_t);
116 void dblfault_handler (void);
117
118 extern inthand_t IDTVEC(syscall);
119
120 #define MAX_TRAP_MSG            28
121 static char *trap_msg[] = {
122         "",                                     /*  0 unused */
123         "privileged instruction fault",         /*  1 T_PRIVINFLT */
124         "",                                     /*  2 unused */
125         "breakpoint instruction fault",         /*  3 T_BPTFLT */
126         "",                                     /*  4 unused */
127         "",                                     /*  5 unused */
128         "arithmetic trap",                      /*  6 T_ARITHTRAP */
129         "system forced exception",              /*  7 T_ASTFLT */
130         "",                                     /*  8 unused */
131         "general protection fault",             /*  9 T_PROTFLT */
132         "trace trap",                           /* 10 T_TRCTRAP */
133         "",                                     /* 11 unused */
134         "page fault",                           /* 12 T_PAGEFLT */
135         "",                                     /* 13 unused */
136         "alignment fault",                      /* 14 T_ALIGNFLT */
137         "",                                     /* 15 unused */
138         "",                                     /* 16 unused */
139         "",                                     /* 17 unused */
140         "integer divide fault",                 /* 18 T_DIVIDE */
141         "non-maskable interrupt trap",          /* 19 T_NMI */
142         "overflow trap",                        /* 20 T_OFLOW */
143         "FPU bounds check fault",               /* 21 T_BOUND */
144         "FPU device not available",             /* 22 T_DNA */
145         "double fault",                         /* 23 T_DOUBLEFLT */
146         "FPU operand fetch fault",              /* 24 T_FPOPFLT */
147         "invalid TSS fault",                    /* 25 T_TSSFLT */
148         "segment not present fault",            /* 26 T_SEGNPFLT */
149         "stack fault",                          /* 27 T_STKFLT */
150         "machine check trap",                   /* 28 T_MCHK */
151 };
152
153 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
154 extern int has_f00f_bug;
155 #endif
156
157 #ifdef DDB
158 static int ddb_on_nmi = 1;
159 SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
160         &ddb_on_nmi, 0, "Go to DDB on NMI");
161 #endif
162 static int panic_on_nmi = 1;
163 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
164         &panic_on_nmi, 0, "Panic on NMI");
165 static int fast_release;
166 SYSCTL_INT(_machdep, OID_AUTO, fast_release, CTLFLAG_RW,
167         &fast_release, 0, "Passive Release was optimal");
168 static int slow_release;
169 SYSCTL_INT(_machdep, OID_AUTO, slow_release, CTLFLAG_RW,
170         &slow_release, 0, "Passive Release was nonoptimal");
171 static int pass_release;
172 SYSCTL_INT(_machdep, OID_AUTO, pass_release, CTLFLAG_RW,
173         &pass_release, 0, "Passive Release on switch");
174
175 MALLOC_DEFINE(M_SYSMSG, "sysmsg", "sysmsg structure");
176
177 /*
178  * USER->KERNEL transition.  Do not transition us out of userland from the
179  * point of view of the userland scheduler unless we actually have to
180  * switch.  Switching typically occurs when a process blocks in the kernel.
181  *
182  * passive_release is called from within a critical section and the BGL will
183  * still be held.  This function is NOT called for preemptions, only for
184  * switchouts.  Note that other elements of the system (uio_yield()) assume
185  * that the user cruft will be released when lwkt_switch() is called.
186  */
187 static void
188 passive_release(struct thread *td)
189 {
190         struct proc *p = td->td_proc;
191
192         td->td_release = NULL;
193
194         /*
195          * P_CP_RELEASED prevents the userland scheduler from messing with
196          * this proc.
197          */
198         if ((p->p_flag & P_CP_RELEASED) == 0) {
199                 p->p_flag |= P_CP_RELEASED;
200                 lwkt_setpri_self(TDPRI_KERN_USER);
201         }
202
203         /*
204          * Only one process will have a P_CURPROC designation for each cpu
205          * in the system.  Releasing it allows another userland process to
206          * be scheduled in case our thread blocks in the kernel.
207          */
208         if (p->p_flag & P_CURPROC) {
209                 release_curproc(p);
210                 ++pass_release;
211         }
212 }
213
214 /*
215  * userenter() passively intercepts the thread switch function to increase
216  * the thread priority from a user priority to a kernel priority, reducing
217  * syscall and trap overhead for the case where no switch occurs.
218  */
219
220 static __inline void
221 userenter(struct thread *curtd)
222 {
223         curtd->td_release = passive_release;
224 }
225
226 static __inline void
227 userexit(struct proc *p)
228 {
229         struct thread *td = p->p_thread;
230
231         /*
232          * Reacquire our P_CURPROC status and adjust the LWKT priority
233          * for our return to userland.  We can fast path the case where
234          * td_release was not called by checking particular proc flags.
235          * Otherwise we do it the slow way.
236          *
237          * Lowering our priority may make other higher priority threads
238          * runnable. lwkt_setpri_self() does not switch away, so call
239          * lwkt_maybe_switch() to deal with it.  
240          *
241          * WARNING!  Once our priority is lowered to a user level priority
242          * it is possible, once we return to user mode (or if we were to
243          * block) for a cpu-bound user process to prevent us from getting cpu
244          * again.  This is always the last step.
245          */
246         td->td_release = NULL;
247         if ((p->p_flag & (P_CP_RELEASED|P_CURPROC)) == P_CURPROC) {
248                 ++fast_release;
249         } else {
250                 ++slow_release;
251                 acquire_curproc(p);
252
253                 switch(p->p_rtprio.type) {
254                 case RTP_PRIO_IDLE:
255                         lwkt_setpri_self(TDPRI_USER_IDLE);
256                         break;
257                 case RTP_PRIO_REALTIME:
258                 case RTP_PRIO_FIFO:
259                         lwkt_setpri_self(TDPRI_USER_REAL);
260                         break;
261                 default:
262                         lwkt_setpri_self(TDPRI_USER_NORM);
263                         break;
264                 }
265         }
266         lwkt_maybe_switch();
267 }
268
269
270 static void
271 userret(struct proc *p, struct trapframe *frame, u_quad_t oticks)
272 {
273         int sig;
274
275         /*
276          * Post any pending upcalls
277          */
278         if (p->p_flag & P_UPCALLPEND) {
279                 p->p_flag &= ~P_UPCALLPEND;
280                 postupcall(p);
281         }
282
283         /*
284          * Post any pending signals
285          */
286         while ((sig = CURSIG(p)) != 0) {
287                 postsig(sig);
288         }
289
290         /*
291          * If a reschedule has been requested then we release the current
292          * process in order to shift our P_CURPROC designation to another
293          * user process.  userexit() will reacquire P_CURPROC and block
294          * there.
295          */
296         if (resched_wanted()) {
297                 p->p_thread->td_release = NULL;
298                 if ((p->p_flag & P_CP_RELEASED) == 0) {
299                         p->p_flag |= P_CP_RELEASED;
300                         lwkt_setpri_self(TDPRI_KERN_USER);
301                 }
302                 if (p->p_flag & P_CURPROC) {
303                         release_curproc(p);
304                 } else {
305                         clear_resched();
306                 }
307         }
308
309         /*
310          * Charge system time if profiling.  Note: times are in microseconds.
311          */
312         if (p->p_flag & P_PROFIL) {
313                 addupc_task(p, frame->tf_eip, 
314                     (u_int)(curthread->td_sticks - oticks));
315         }
316
317         /*
318          * Post any pending signals XXX
319          */
320         while ((sig = CURSIG(p)) != 0)
321                 postsig(sig);
322 }
323
324 #ifdef DEVICE_POLLING
325 extern u_int32_t poll_in_trap;
326 extern int ether_poll (int count);
327 #endif /* DEVICE_POLLING */
328
329 /*
330  * Exception, fault, and trap interface to the FreeBSD kernel.
331  * This common code is called from assembly language IDT gate entry
332  * routines that prepare a suitable stack frame, and restore this
333  * frame after the exception has been processed.
334  *
335  * This function is also called from doreti in an interlock to handle ASTs.
336  * For example:  hardwareint->INTROUTINE->(set ast)->doreti->trap
337  *
338  * NOTE!  We have to retrieve the fault address prior to obtaining the
339  * MP lock because get_mplock() may switch out.  YYY cr2 really ought
340  * to be retrieved by the assembly code, not here.
341  */
342 void
343 trap(frame)
344         struct trapframe frame;
345 {
346         struct thread *td = curthread;
347         struct proc *p;
348         u_quad_t sticks = 0;
349         int i = 0, ucode = 0, type, code;
350         vm_offset_t eva;
351
352         p = td->td_proc;
353 #ifdef DDB
354         if (db_active) {
355                 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
356                 get_mplock();
357                 trap_fatal(&frame, eva);
358                 goto out2;
359         }
360 #endif
361
362         eva = 0;
363         if (frame.tf_trapno == T_PAGEFLT) {
364                 /*
365                  * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
366                  * This problem is worked around by using an interrupt
367                  * gate for the pagefault handler.  We are finally ready
368                  * to read %cr2 and then must reenable interrupts.
369                  *
370                  * XXX this should be in the switch statement, but the
371                  * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
372                  * flow of control too much for this to be obviously
373                  * correct.
374                  */
375                 eva = rcr2();
376                 get_mplock();
377                 cpu_enable_intr();
378         } else {
379                 get_mplock();
380         }
381         /*
382          * MP lock is held at this point
383          */
384
385         if (!(frame.tf_eflags & PSL_I)) {
386                 /*
387                  * Buggy application or kernel code has disabled interrupts
388                  * and then trapped.  Enabling interrupts now is wrong, but
389                  * it is better than running with interrupts disabled until
390                  * they are accidentally enabled later.
391                  */
392                 type = frame.tf_trapno;
393                 if (ISPL(frame.tf_cs)==SEL_UPL || (frame.tf_eflags & PSL_VM)) {
394                         printf(
395                             "pid %ld (%s): trap %d with interrupts disabled\n",
396                             (long)curproc->p_pid, curproc->p_comm, type);
397                 } else if (type != T_BPTFLT && type != T_TRCTRAP) {
398                         /*
399                          * XXX not quite right, since this may be for a
400                          * multiple fault in user mode.
401                          */
402                         printf("kernel trap %d with interrupts disabled\n",
403                             type);
404                 }
405                 cpu_enable_intr();
406         }
407
408
409 #ifdef DEVICE_POLLING
410         if (poll_in_trap)
411                 ether_poll(poll_in_trap);
412 #endif /* DEVICE_POLLING */
413
414 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
415 restart:
416 #endif
417         type = frame.tf_trapno;
418         code = frame.tf_err;
419
420         if (in_vm86call) {
421                 if (frame.tf_eflags & PSL_VM &&
422                     (type == T_PROTFLT || type == T_STKFLT)) {
423 #ifdef SMP
424                         KKASSERT(curthread->td_mpcount > 0);
425 #endif
426                         i = vm86_emulate((struct vm86frame *)&frame);
427 #ifdef SMP
428                         KKASSERT(curthread->td_mpcount > 0);
429 #endif
430                         if (i != 0) {
431                                 /*
432                                  * returns to original process
433                                  */
434                                 vm86_trap((struct vm86frame *)&frame);
435                                 KKASSERT(0);
436                         }
437                         goto out2;
438                 }
439                 switch (type) {
440                         /*
441                          * these traps want either a process context, or
442                          * assume a normal userspace trap.
443                          */
444                 case T_PROTFLT:
445                 case T_SEGNPFLT:
446                         trap_fatal(&frame, eva);
447                         goto out2;
448                 case T_TRCTRAP:
449                         type = T_BPTFLT;        /* kernel breakpoint */
450                         /* FALL THROUGH */
451                 }
452                 goto kernel_trap;       /* normal kernel trap handling */
453         }
454
455         if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
456                 /* user trap */
457
458                 userenter(td);
459
460                 sticks = curthread->td_sticks;
461                 p->p_md.md_regs = &frame;
462
463                 switch (type) {
464                 case T_PRIVINFLT:       /* privileged instruction fault */
465                         ucode = type;
466                         i = SIGILL;
467                         break;
468
469                 case T_BPTFLT:          /* bpt instruction fault */
470                 case T_TRCTRAP:         /* trace trap */
471                         frame.tf_eflags &= ~PSL_T;
472                         i = SIGTRAP;
473                         break;
474
475                 case T_ARITHTRAP:       /* arithmetic trap */
476                         ucode = code;
477                         i = SIGFPE;
478                         break;
479
480                 case T_ASTFLT:          /* Allow process switch */
481                         mycpu->gd_cnt.v_soft++;
482                         if (mycpu->gd_reqflags & RQF_AST_OWEUPC) {
483                                 atomic_clear_int_nonlocked(&mycpu->gd_reqflags,
484                                             RQF_AST_OWEUPC);
485                                 addupc_task(p, p->p_stats->p_prof.pr_addr,
486                                             p->p_stats->p_prof.pr_ticks);
487                         }
488                         goto out;
489
490                         /*
491                          * The following two traps can happen in
492                          * vm86 mode, and, if so, we want to handle
493                          * them specially.
494                          */
495                 case T_PROTFLT:         /* general protection fault */
496                 case T_STKFLT:          /* stack fault */
497                         if (frame.tf_eflags & PSL_VM) {
498                                 i = vm86_emulate((struct vm86frame *)&frame);
499                                 if (i == 0)
500                                         goto out;
501                                 break;
502                         }
503                         /* FALL THROUGH */
504
505                 case T_SEGNPFLT:        /* segment not present fault */
506                 case T_TSSFLT:          /* invalid TSS fault */
507                 case T_DOUBLEFLT:       /* double fault */
508                 default:
509                         ucode = code + BUS_SEGM_FAULT ;
510                         i = SIGBUS;
511                         break;
512
513                 case T_PAGEFLT:         /* page fault */
514                         i = trap_pfault(&frame, TRUE, eva);
515                         if (i == -1)
516                                 goto out;
517 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
518                         if (i == -2)
519                                 goto restart;
520 #endif
521                         if (i == 0)
522                                 goto out;
523
524                         ucode = T_PAGEFLT;
525                         break;
526
527                 case T_DIVIDE:          /* integer divide fault */
528                         ucode = FPE_INTDIV;
529                         i = SIGFPE;
530                         break;
531
532 #if NISA > 0
533                 case T_NMI:
534 #ifdef POWERFAIL_NMI
535                         goto handle_powerfail;
536 #else /* !POWERFAIL_NMI */
537                         /* machine/parity/power fail/"kitchen sink" faults */
538                         if (isa_nmi(code) == 0) {
539 #ifdef DDB
540                                 /*
541                                  * NMI can be hooked up to a pushbutton
542                                  * for debugging.
543                                  */
544                                 if (ddb_on_nmi) {
545                                         printf ("NMI ... going to debugger\n");
546                                         kdb_trap (type, 0, &frame);
547                                 }
548 #endif /* DDB */
549                                 goto out2;
550                         } else if (panic_on_nmi)
551                                 panic("NMI indicates hardware failure");
552                         break;
553 #endif /* POWERFAIL_NMI */
554 #endif /* NISA > 0 */
555
556                 case T_OFLOW:           /* integer overflow fault */
557                         ucode = FPE_INTOVF;
558                         i = SIGFPE;
559                         break;
560
561                 case T_BOUND:           /* bounds check fault */
562                         ucode = FPE_FLTSUB;
563                         i = SIGFPE;
564                         break;
565
566                 case T_DNA:
567 #if NNPX > 0
568                         /* if a transparent fault (due to context switch "late") */
569                         if (npxdna())
570                                 goto out;
571 #endif
572                         if (!pmath_emulate) {
573                                 i = SIGFPE;
574                                 ucode = FPE_FPU_NP_TRAP;
575                                 break;
576                         }
577                         i = (*pmath_emulate)(&frame);
578                         if (i == 0) {
579                                 if (!(frame.tf_eflags & PSL_T))
580                                         goto out2;
581                                 frame.tf_eflags &= ~PSL_T;
582                                 i = SIGTRAP;
583                         }
584                         /* else ucode = emulator_only_knows() XXX */
585                         break;
586
587                 case T_FPOPFLT:         /* FPU operand fetch fault */
588                         ucode = T_FPOPFLT;
589                         i = SIGILL;
590                         break;
591
592                 case T_XMMFLT:          /* SIMD floating-point exception */
593                         ucode = 0; /* XXX */
594                         i = SIGFPE;
595                         break;
596                 }
597         } else {
598 kernel_trap:
599                 /* kernel trap */
600
601                 switch (type) {
602                 case T_PAGEFLT:                 /* page fault */
603                         (void) trap_pfault(&frame, FALSE, eva);
604                         goto out2;
605
606                 case T_DNA:
607 #if NNPX > 0
608                         /*
609                          * The kernel is apparently using npx for copying.
610                          * XXX this should be fatal unless the kernel has
611                          * registered such use.
612                          */
613                         if (npxdna())
614                                 goto out2;
615 #endif
616                         break;
617
618                 case T_PROTFLT:         /* general protection fault */
619                 case T_SEGNPFLT:        /* segment not present fault */
620                         /*
621                          * Invalid segment selectors and out of bounds
622                          * %eip's and %esp's can be set up in user mode.
623                          * This causes a fault in kernel mode when the
624                          * kernel tries to return to user mode.  We want
625                          * to get this fault so that we can fix the
626                          * problem here and not have to check all the
627                          * selectors and pointers when the user changes
628                          * them.
629                          */
630 #define MAYBE_DORETI_FAULT(where, whereto)                              \
631         do {                                                            \
632                 if (frame.tf_eip == (int)where) {                       \
633                         frame.tf_eip = (int)whereto;                    \
634                         goto out2;                                      \
635                 }                                                       \
636         } while (0)
637                         /*
638                          * Since we don't save %gs across an interrupt
639                          * frame this check must occur outside the intr
640                          * nesting level check.
641                          */
642                         if (frame.tf_eip == (int)cpu_switch_load_gs) {
643                                 curthread->td_pcb->pcb_gs = 0;
644                                 psignal(p, SIGBUS);
645                                 goto out2;
646                         }
647                         if (mycpu->gd_intr_nesting_level == 0) {
648                                 /*
649                                  * Invalid %fs's and %gs's can be created using
650                                  * procfs or PT_SETREGS or by invalidating the
651                                  * underlying LDT entry.  This causes a fault
652                                  * in kernel mode when the kernel attempts to
653                                  * switch contexts.  Lose the bad context
654                                  * (XXX) so that we can continue, and generate
655                                  * a signal.
656                                  */
657                                 MAYBE_DORETI_FAULT(doreti_iret,
658                                                    doreti_iret_fault);
659                                 MAYBE_DORETI_FAULT(doreti_popl_ds,
660                                                    doreti_popl_ds_fault);
661                                 MAYBE_DORETI_FAULT(doreti_popl_es,
662                                                    doreti_popl_es_fault);
663                                 MAYBE_DORETI_FAULT(doreti_popl_fs,
664                                                    doreti_popl_fs_fault);
665                                 if (curthread->td_pcb->pcb_onfault) {
666                                         frame.tf_eip = (int)curthread->td_pcb->pcb_onfault;
667                                         goto out2;
668                                 }
669                         }
670                         break;
671
672                 case T_TSSFLT:
673                         /*
674                          * PSL_NT can be set in user mode and isn't cleared
675                          * automatically when the kernel is entered.  This
676                          * causes a TSS fault when the kernel attempts to
677                          * `iret' because the TSS link is uninitialized.  We
678                          * want to get this fault so that we can fix the
679                          * problem here and not every time the kernel is
680                          * entered.
681                          */
682                         if (frame.tf_eflags & PSL_NT) {
683                                 frame.tf_eflags &= ~PSL_NT;
684                                 goto out2;
685                         }
686                         break;
687
688                 case T_TRCTRAP:  /* trace trap */
689                         if (frame.tf_eip == (int)IDTVEC(syscall)) {
690                                 /*
691                                  * We've just entered system mode via the
692                                  * syscall lcall.  Continue single stepping
693                                  * silently until the syscall handler has
694                                  * saved the flags.
695                                  */
696                                 goto out2;
697                         }
698                         if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
699                                 /*
700                                  * The syscall handler has now saved the
701                                  * flags.  Stop single stepping it.
702                                  */
703                                 frame.tf_eflags &= ~PSL_T;
704                                 goto out2;
705                         }
706                         /*
707                          * Ignore debug register trace traps due to
708                          * accesses in the user's address space, which
709                          * can happen under several conditions such as
710                          * if a user sets a watchpoint on a buffer and
711                          * then passes that buffer to a system call.
712                          * We still want to get TRCTRAPS for addresses
713                          * in kernel space because that is useful when
714                          * debugging the kernel.
715                          */
716                         if (user_dbreg_trap()) {
717                                 /*
718                                  * Reset breakpoint bits because the
719                                  * processor doesn't
720                                  */
721                                 load_dr6(rdr6() & 0xfffffff0);
722                                 goto out2;
723                         }
724                         /*
725                          * Fall through (TRCTRAP kernel mode, kernel address)
726                          */
727                 case T_BPTFLT:
728                         /*
729                          * If DDB is enabled, let it handle the debugger trap.
730                          * Otherwise, debugger traps "can't happen".
731                          */
732 #ifdef DDB
733                         if (kdb_trap (type, 0, &frame))
734                                 goto out2;
735 #endif
736                         break;
737
738 #if NISA > 0
739                 case T_NMI:
740 #ifdef POWERFAIL_NMI
741 #ifndef TIMER_FREQ
742 #  define TIMER_FREQ 1193182
743 #endif
744         handle_powerfail:
745                 {
746                   static unsigned lastalert = 0;
747
748                   if(time_second - lastalert > 10)
749                     {
750                       log(LOG_WARNING, "NMI: power fail\n");
751                       sysbeep(TIMER_FREQ/880, hz);
752                       lastalert = time_second;
753                     }
754                     /* YYY mp count */
755                   goto out2;
756                 }
757 #else /* !POWERFAIL_NMI */
758                         /* machine/parity/power fail/"kitchen sink" faults */
759                         if (isa_nmi(code) == 0) {
760 #ifdef DDB
761                                 /*
762                                  * NMI can be hooked up to a pushbutton
763                                  * for debugging.
764                                  */
765                                 if (ddb_on_nmi) {
766                                         printf ("NMI ... going to debugger\n");
767                                         kdb_trap (type, 0, &frame);
768                                 }
769 #endif /* DDB */
770                                 goto out2;
771                         } else if (panic_on_nmi == 0)
772                                 goto out2;
773                         /* FALL THROUGH */
774 #endif /* POWERFAIL_NMI */
775 #endif /* NISA > 0 */
776                 }
777
778                 trap_fatal(&frame, eva);
779                 goto out2;
780         }
781
782         /* Translate fault for emulators (e.g. Linux) */
783         if (*p->p_sysent->sv_transtrap)
784                 i = (*p->p_sysent->sv_transtrap)(i, type);
785
786         trapsignal(p, i, ucode);
787
788 #ifdef DEBUG
789         if (type <= MAX_TRAP_MSG) {
790                 uprintf("fatal process exception: %s",
791                         trap_msg[type]);
792                 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
793                         uprintf(", fault VA = 0x%lx", (u_long)eva);
794                 uprintf("\n");
795         }
796 #endif
797
798 out:
799 #ifdef SMP
800         if (ISPL(frame.tf_cs) == SEL_UPL)
801                 KASSERT(curthread->td_mpcount == 1, ("badmpcount trap from %p", (void *)frame.tf_eip));
802 #endif
803         userret(p, &frame, sticks);
804         userexit(p);
805 out2:
806 #ifdef SMP
807         KKASSERT(curthread->td_mpcount > 0);
808 #endif
809         rel_mplock();
810 }
811
812 #ifdef notyet
813 /*
814  * This version doesn't allow a page fault to user space while
815  * in the kernel. The rest of the kernel needs to be made "safe"
816  * before this can be used. I think the only things remaining
817  * to be made safe are the iBCS2 code and the process tracing/
818  * debugging code.
819  */
820 static int
821 trap_pfault(frame, usermode, eva)
822         struct trapframe *frame;
823         int usermode;
824         vm_offset_t eva;
825 {
826         vm_offset_t va;
827         struct vmspace *vm = NULL;
828         vm_map_t map = 0;
829         int rv = 0;
830         vm_prot_t ftype;
831         struct proc *p = curproc;
832
833         if (frame->tf_err & PGEX_W)
834                 ftype = VM_PROT_WRITE;
835         else
836                 ftype = VM_PROT_READ;
837
838         va = trunc_page(eva);
839         if (va < VM_MIN_KERNEL_ADDRESS) {
840                 vm_offset_t v;
841                 vm_page_t mpte;
842
843                 if (p == NULL ||
844                     (!usermode && va < VM_MAXUSER_ADDRESS &&
845                      (mycpu->gd_intr_nesting_level != 0 || 
846                       curthread->td_pcb->pcb_onfault == NULL))) {
847                         trap_fatal(frame, eva);
848                         return (-1);
849                 }
850
851                 /*
852                  * This is a fault on non-kernel virtual memory.
853                  * vm is initialized above to NULL. If curproc is NULL
854                  * or curproc->p_vmspace is NULL the fault is fatal.
855                  */
856                 vm = p->p_vmspace;
857                 if (vm == NULL)
858                         goto nogo;
859
860                 map = &vm->vm_map;
861
862                 /*
863                  * Keep swapout from messing with us during this
864                  *      critical time.
865                  */
866                 ++p->p_lock;
867
868                 /*
869                  * Grow the stack if necessary
870                  */
871                 /* grow_stack returns false only if va falls into
872                  * a growable stack region and the stack growth
873                  * fails.  It returns true if va was not within
874                  * a growable stack region, or if the stack 
875                  * growth succeeded.
876                  */
877                 if (!grow_stack (p, va)) {
878                         rv = KERN_FAILURE;
879                         --p->p_lock;
880                         goto nogo;
881                 }
882                 
883                 /* Fault in the user page: */
884                 rv = vm_fault(map, va, ftype,
885                               (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
886                                                       : VM_FAULT_NORMAL);
887
888                 --p->p_lock;
889         } else {
890                 /*
891                  * Don't allow user-mode faults in kernel address space.
892                  */
893                 if (usermode)
894                         goto nogo;
895
896                 /*
897                  * Since we know that kernel virtual address addresses
898                  * always have pte pages mapped, we just have to fault
899                  * the page.
900                  */
901                 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
902         }
903
904         if (rv == KERN_SUCCESS)
905                 return (0);
906 nogo:
907         if (!usermode) {
908                 if (mycpu->gd_intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
909                         frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
910                         return (0);
911                 }
912                 trap_fatal(frame, eva);
913                 return (-1);
914         }
915
916         /* kludge to pass faulting virtual address to sendsig */
917         frame->tf_err = eva;
918
919         return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
920 }
921 #endif
922
923 int
924 trap_pfault(frame, usermode, eva)
925         struct trapframe *frame;
926         int usermode;
927         vm_offset_t eva;
928 {
929         vm_offset_t va;
930         struct vmspace *vm = NULL;
931         vm_map_t map = 0;
932         int rv = 0;
933         vm_prot_t ftype;
934         struct proc *p = curproc;
935
936         va = trunc_page(eva);
937         if (va >= KERNBASE) {
938                 /*
939                  * Don't allow user-mode faults in kernel address space.
940                  * An exception:  if the faulting address is the invalid
941                  * instruction entry in the IDT, then the Intel Pentium
942                  * F00F bug workaround was triggered, and we need to
943                  * treat it is as an illegal instruction, and not a page
944                  * fault.
945                  */
946 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
947                 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
948                         frame->tf_trapno = T_PRIVINFLT;
949                         return -2;
950                 }
951 #endif
952                 if (usermode)
953                         goto nogo;
954
955                 map = kernel_map;
956         } else {
957                 /*
958                  * This is a fault on non-kernel virtual memory.
959                  * vm is initialized above to NULL. If curproc is NULL
960                  * or curproc->p_vmspace is NULL the fault is fatal.
961                  */
962                 if (p != NULL)
963                         vm = p->p_vmspace;
964
965                 if (vm == NULL)
966                         goto nogo;
967
968                 map = &vm->vm_map;
969         }
970
971         if (frame->tf_err & PGEX_W)
972                 ftype = VM_PROT_WRITE;
973         else
974                 ftype = VM_PROT_READ;
975
976         if (map != kernel_map) {
977                 /*
978                  * Keep swapout from messing with us during this
979                  *      critical time.
980                  */
981                 ++p->p_lock;
982
983                 /*
984                  * Grow the stack if necessary
985                  */
986                 /* grow_stack returns false only if va falls into
987                  * a growable stack region and the stack growth
988                  * fails.  It returns true if va was not within
989                  * a growable stack region, or if the stack 
990                  * growth succeeded.
991                  */
992                 if (!grow_stack (p, va)) {
993                         rv = KERN_FAILURE;
994                         --p->p_lock;
995                         goto nogo;
996                 }
997
998                 /* Fault in the user page: */
999                 rv = vm_fault(map, va, ftype,
1000                               (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
1001                                                       : VM_FAULT_NORMAL);
1002
1003                 --p->p_lock;
1004         } else {
1005                 /*
1006                  * Don't have to worry about process locking or stacks in the kernel.
1007                  */
1008                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
1009         }
1010
1011         if (rv == KERN_SUCCESS)
1012                 return (0);
1013 nogo:
1014         if (!usermode) {
1015                 if (mycpu->gd_intr_nesting_level == 0 && curthread->td_pcb->pcb_onfault) {
1016                         frame->tf_eip = (int)curthread->td_pcb->pcb_onfault;
1017                         return (0);
1018                 }
1019                 trap_fatal(frame, eva);
1020                 return (-1);
1021         }
1022
1023         /* kludge to pass faulting virtual address to sendsig */
1024         frame->tf_err = eva;
1025
1026         return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
1027 }
1028
1029 static void
1030 trap_fatal(frame, eva)
1031         struct trapframe *frame;
1032         vm_offset_t eva;
1033 {
1034         int code, type, ss, esp;
1035         struct soft_segment_descriptor softseg;
1036
1037         code = frame->tf_err;
1038         type = frame->tf_trapno;
1039         sdtossd(&gdt[mycpu->gd_cpuid * NGDT + IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
1040
1041         if (type <= MAX_TRAP_MSG)
1042                 printf("\n\nFatal trap %d: %s while in %s mode\n",
1043                         type, trap_msg[type],
1044                         frame->tf_eflags & PSL_VM ? "vm86" :
1045                         ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
1046 #ifdef SMP
1047         /* three seperate prints in case of a trap on an unmapped page */
1048         printf("mp_lock = %08x; ", mp_lock);
1049         printf("cpuid = %d; ", mycpu->gd_cpuid);
1050         printf("lapic.id = %08x\n", lapic.id);
1051 #endif
1052         if (type == T_PAGEFLT) {
1053                 printf("fault virtual address   = 0x%x\n", eva);
1054                 printf("fault code              = %s %s, %s\n",
1055                         code & PGEX_U ? "user" : "supervisor",
1056                         code & PGEX_W ? "write" : "read",
1057                         code & PGEX_P ? "protection violation" : "page not present");
1058         }
1059         printf("instruction pointer     = 0x%x:0x%x\n",
1060                frame->tf_cs & 0xffff, frame->tf_eip);
1061         if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
1062                 ss = frame->tf_ss & 0xffff;
1063                 esp = frame->tf_esp;
1064         } else {
1065                 ss = GSEL(GDATA_SEL, SEL_KPL);
1066                 esp = (int)&frame->tf_esp;
1067         }
1068         printf("stack pointer           = 0x%x:0x%x\n", ss, esp);
1069         printf("frame pointer           = 0x%x:0x%x\n", ss, frame->tf_ebp);
1070         printf("code segment            = base 0x%x, limit 0x%x, type 0x%x\n",
1071                softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
1072         printf("                        = DPL %d, pres %d, def32 %d, gran %d\n",
1073                softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
1074                softseg.ssd_gran);
1075         printf("processor eflags        = ");
1076         if (frame->tf_eflags & PSL_T)
1077                 printf("trace trap, ");
1078         if (frame->tf_eflags & PSL_I)
1079                 printf("interrupt enabled, ");
1080         if (frame->tf_eflags & PSL_NT)
1081                 printf("nested task, ");
1082         if (frame->tf_eflags & PSL_RF)
1083                 printf("resume, ");
1084         if (frame->tf_eflags & PSL_VM)
1085                 printf("vm86, ");
1086         printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
1087         printf("current process         = ");
1088         if (curproc) {
1089                 printf("%lu (%s)\n",
1090                     (u_long)curproc->p_pid, curproc->p_comm ?
1091                     curproc->p_comm : "");
1092         } else {
1093                 printf("Idle\n");
1094         }
1095         printf("current thread          = pri %d ", curthread->td_pri);
1096         if (curthread->td_pri >= TDPRI_CRIT)
1097                 printf("(CRIT)");
1098         printf("\n");
1099         printf("interrupt mask          = ");
1100         if ((curthread->td_cpl & net_imask) == net_imask)
1101                 printf("net ");
1102         if ((curthread->td_cpl & tty_imask) == tty_imask)
1103                 printf("tty ");
1104         if ((curthread->td_cpl & bio_imask) == bio_imask)
1105                 printf("bio ");
1106         if ((curthread->td_cpl & cam_imask) == cam_imask)
1107                 printf("cam ");
1108         if (curthread->td_cpl == 0)
1109                 printf("none");
1110 #ifdef SMP
1111 /**
1112  *  XXX FIXME:
1113  *      we probably SHOULD have stopped the other CPUs before now!
1114  *      another CPU COULD have been touching cpl at this moment...
1115  */
1116         printf(" <- SMP: XXX");
1117 #endif
1118         printf("\n");
1119
1120 #ifdef KDB
1121         if (kdb_trap(&psl))
1122                 return;
1123 #endif
1124 #ifdef DDB
1125         if ((debugger_on_panic || db_active) && kdb_trap(type, code, frame))
1126                 return;
1127 #endif
1128         printf("trap number             = %d\n", type);
1129         if (type <= MAX_TRAP_MSG)
1130                 panic("%s", trap_msg[type]);
1131         else
1132                 panic("unknown/reserved trap");
1133 }
1134
1135 /*
1136  * Double fault handler. Called when a fault occurs while writing
1137  * a frame for a trap/exception onto the stack. This usually occurs
1138  * when the stack overflows (such is the case with infinite recursion,
1139  * for example).
1140  *
1141  * XXX Note that the current PTD gets replaced by IdlePTD when the
1142  * task switch occurs. This means that the stack that was active at
1143  * the time of the double fault is not available at <kstack> unless
1144  * the machine was idle when the double fault occurred. The downside
1145  * of this is that "trace <ebp>" in ddb won't work.
1146  */
1147 void
1148 dblfault_handler()
1149 {
1150         struct mdglobaldata *gd = mdcpu;
1151
1152         printf("\nFatal double fault:\n");
1153         printf("eip = 0x%x\n", gd->gd_common_tss.tss_eip);
1154         printf("esp = 0x%x\n", gd->gd_common_tss.tss_esp);
1155         printf("ebp = 0x%x\n", gd->gd_common_tss.tss_ebp);
1156 #ifdef SMP
1157         /* three seperate prints in case of a trap on an unmapped page */
1158         printf("mp_lock = %08x; ", mp_lock);
1159         printf("cpuid = %d; ", mycpu->gd_cpuid);
1160         printf("lapic.id = %08x\n", lapic.id);
1161 #endif
1162         panic("double fault");
1163 }
1164
1165 /*
1166  * Compensate for 386 brain damage (missing URKR).
1167  * This is a little simpler than the pagefault handler in trap() because
1168  * it the page tables have already been faulted in and high addresses
1169  * are thrown out early for other reasons.
1170  */
1171 int trapwrite(addr)
1172         unsigned addr;
1173 {
1174         struct proc *p;
1175         vm_offset_t va;
1176         struct vmspace *vm;
1177         int rv;
1178
1179         va = trunc_page((vm_offset_t)addr);
1180         /*
1181          * XXX - MAX is END.  Changed > to >= for temp. fix.
1182          */
1183         if (va >= VM_MAXUSER_ADDRESS)
1184                 return (1);
1185
1186         p = curproc;
1187         vm = p->p_vmspace;
1188
1189         ++p->p_lock;
1190
1191         if (!grow_stack (p, va)) {
1192                 --p->p_lock;
1193                 return (1);
1194         }
1195
1196         /*
1197          * fault the data page
1198          */
1199         rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1200
1201         --p->p_lock;
1202
1203         if (rv != KERN_SUCCESS)
1204                 return 1;
1205
1206         return (0);
1207 }
1208
1209 /*
1210  *      syscall2 -      MP aware system call request C handler
1211  *
1212  *      A system call is essentially treated as a trap except that the
1213  *      MP lock is not held on entry or return.  We are responsible for
1214  *      obtaining the MP lock if necessary and for handling ASTs
1215  *      (e.g. a task switch) prior to return.
1216  *
1217  *      In general, only simple access and manipulation of curproc and
1218  *      the current stack is allowed without having to hold MP lock.
1219  */
1220 void
1221 syscall2(struct trapframe frame)
1222 {
1223         struct thread *td = curthread;
1224         struct proc *p = td->td_proc;
1225         caddr_t params;
1226         int i;
1227         struct sysent *callp;
1228         register_t orig_tf_eflags;
1229         u_quad_t sticks;
1230         int error;
1231         int narg;
1232         u_int code;
1233         union sysunion args;
1234
1235 #ifdef DIAGNOSTIC
1236         if (ISPL(frame.tf_cs) != SEL_UPL) {
1237                 get_mplock();
1238                 panic("syscall");
1239                 /* NOT REACHED */
1240         }
1241 #endif
1242
1243 #ifdef SMP
1244         KASSERT(curthread->td_mpcount == 0, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1245         get_mplock();
1246 #endif
1247         /*
1248          * access non-atomic field from critical section.  p_sticks is
1249          * updated by the clock interrupt.  Also use this opportunity
1250          * to lazy-raise our LWKT priority.
1251          */
1252         userenter(td);
1253         crit_enter_quick(td);
1254         sticks = curthread->td_sticks;
1255         crit_exit_quick(td);
1256
1257         p->p_md.md_regs = &frame;
1258         params = (caddr_t)frame.tf_esp + sizeof(int);
1259         code = frame.tf_eax;
1260         orig_tf_eflags = frame.tf_eflags;
1261
1262         if (p->p_sysent->sv_prepsyscall) {
1263                 /*
1264                  * The prep code is not MP aware.
1265                  */
1266                 (*p->p_sysent->sv_prepsyscall)(&frame, (int *)(&args.nosys.usrmsg + 1), &code, &params);
1267         } else {
1268                 /*
1269                  * Need to check if this is a 32 bit or 64 bit syscall.
1270                  * fuword is MP aware.
1271                  */
1272                 if (code == SYS_syscall) {
1273                         /*
1274                          * Code is first argument, followed by actual args.
1275                          */
1276                         code = fuword(params);
1277                         params += sizeof(int);
1278                 } else if (code == SYS___syscall) {
1279                         /*
1280                          * Like syscall, but code is a quad, so as to maintain
1281                          * quad alignment for the rest of the arguments.
1282                          */
1283                         code = fuword(params);
1284                         params += sizeof(quad_t);
1285                 }
1286         }
1287
1288         if (p->p_sysent->sv_mask)
1289                 code &= p->p_sysent->sv_mask;
1290
1291         if (code >= p->p_sysent->sv_size)
1292                 callp = &p->p_sysent->sv_table[0];
1293         else
1294                 callp = &p->p_sysent->sv_table[code];
1295
1296         narg = callp->sy_narg & SYF_ARGMASK;
1297
1298         /*
1299          * copyin is MP aware, but the tracing code is not
1300          */
1301         if (params && (i = narg * sizeof(register_t)) &&
1302             (error = copyin(params, (caddr_t)(&args.nosys.usrmsg + 1), (u_int)i))) {
1303 #ifdef KTRACE
1304                 if (KTRPOINT(td, KTR_SYSCALL))
1305                         ktrsyscall(p->p_tracep, code, narg, (void *)(&args.nosys.usrmsg + 1));
1306 #endif
1307                 goto bad;
1308         }
1309
1310 #if 0
1311         /*
1312          * Try to run the syscall without the MP lock if the syscall
1313          * is MP safe.  We have to obtain the MP lock no matter what if 
1314          * we are ktracing
1315          */
1316         if ((callp->sy_narg & SYF_MPSAFE) == 0) {
1317                 get_mplock();
1318                 have_mplock = 1;
1319         }
1320 #endif
1321
1322 #ifdef KTRACE
1323         if (KTRPOINT(td, KTR_SYSCALL)) {
1324                 ktrsyscall(p->p_tracep, code, narg, (void *)(&args.nosys.usrmsg + 1));
1325         }
1326 #endif
1327
1328         /*
1329          * For traditional syscall code edx is left untouched when 32 bit
1330          * results are returned.  Since edx is loaded from fds[1] when the 
1331          * system call returns we pre-set it here.
1332          */
1333         lwkt_initmsg(&args.lmsg, &td->td_msgport, code);
1334         args.sysmsg_copyout = NULL;
1335         args.sysmsg_fds[0] = 0;
1336         args.sysmsg_fds[1] = frame.tf_edx;
1337
1338         STOPEVENT(p, S_SCE, narg);      /* MP aware */
1339
1340         error = (*callp->sy_call)(&args);
1341
1342         /*
1343          * MP SAFE (we may or may not have the MP lock at this point)
1344          */
1345         switch (error) {
1346         case 0:
1347                 /*
1348                  * Reinitialize proc pointer `p' as it may be different
1349                  * if this is a child returning from fork syscall.
1350                  */
1351                 p = curproc;
1352                 frame.tf_eax = args.sysmsg_fds[0];
1353                 frame.tf_edx = args.sysmsg_fds[1];
1354                 frame.tf_eflags &= ~PSL_C;
1355                 break;
1356         case ERESTART:
1357                 /*
1358                  * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1359                  * int 0x80 is 2 bytes. We saved this in tf_err.
1360                  */
1361                 frame.tf_eip -= frame.tf_err;
1362                 break;
1363         case EJUSTRETURN:
1364                 break;
1365         case EASYNC:
1366                 panic("Unexpected EASYNC return value (for now)");
1367         default:
1368 bad:
1369                 if (p->p_sysent->sv_errsize) {
1370                         if (error >= p->p_sysent->sv_errsize)
1371                                 error = -1;     /* XXX */
1372                         else
1373                                 error = p->p_sysent->sv_errtbl[error];
1374                 }
1375                 frame.tf_eax = error;
1376                 frame.tf_eflags |= PSL_C;
1377                 break;
1378         }
1379
1380         /*
1381          * Traced syscall.  trapsignal() is not MP aware.
1382          */
1383         if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1384                 frame.tf_eflags &= ~PSL_T;
1385                 trapsignal(p, SIGTRAP, 0);
1386         }
1387
1388         /*
1389          * Handle reschedule and other end-of-syscall issues
1390          */
1391         userret(p, &frame, sticks);
1392
1393 #ifdef KTRACE
1394         if (KTRPOINT(td, KTR_SYSRET)) {
1395                 ktrsysret(p->p_tracep, code, error, args.sysmsg_result);
1396         }
1397 #endif
1398
1399         /*
1400          * This works because errno is findable through the
1401          * register set.  If we ever support an emulation where this
1402          * is not the case, this code will need to be revisited.
1403          */
1404         STOPEVENT(p, S_SCX, code);
1405
1406         userexit(p);
1407 #ifdef SMP
1408         /*
1409          * Release the MP lock if we had to get it
1410          */
1411         KASSERT(curthread->td_mpcount == 1, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1412         rel_mplock();
1413 #endif
1414 }
1415
1416 /*
1417  *      sendsys2 -      MP aware system message request C handler
1418  */
1419 void
1420 sendsys2(struct trapframe frame)
1421 {
1422         struct globaldata *gd;
1423         struct thread *td = curthread;
1424         struct proc *p = td->td_proc;
1425         register_t orig_tf_eflags;
1426         struct sysent *callp;
1427         union sysunion *sysun;
1428         lwkt_msg_t umsg;
1429         u_quad_t sticks;
1430         int error;
1431         int narg;
1432         u_int code = 0;
1433         int msgsize;
1434         int result;
1435
1436 #ifdef DIAGNOSTIC
1437         if (ISPL(frame.tf_cs) != SEL_UPL) {
1438                 get_mplock();
1439                 panic("syscall");
1440                 /* NOT REACHED */
1441         }
1442 #endif
1443
1444 #ifdef SMP
1445         KASSERT(curthread->td_mpcount == 0, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1446         get_mplock();
1447 #endif
1448         /*
1449          * access non-atomic field from critical section.  p_sticks is
1450          * updated by the clock interrupt.  Also use this opportunity
1451          * to lazy-raise our LWKT priority.
1452          */
1453         userenter(td);
1454         crit_enter_quick(td);
1455         sticks = curthread->td_sticks;
1456         crit_exit_quick(td);
1457
1458         p->p_md.md_regs = &frame;
1459         orig_tf_eflags = frame.tf_eflags;
1460         result = 0;
1461
1462         /*
1463          * Handle the waitport/waitmsg/checkport/checkmsg case
1464          *
1465          * YYY MOVE THIS TO INT 0x82!  We don't really need to combine it
1466          * with sendsys().
1467          */
1468         if ((msgsize = frame.tf_edx) <= 0) {
1469                 if (frame.tf_ecx) {
1470                         printf("waitmsg/checkmsg not yet supported: %08x\n",
1471                                 frame.tf_ecx);
1472                         error = ENOTSUP;
1473                         goto bad2;
1474                 }
1475                 if (frame.tf_eax) {
1476                         printf("waitport/checkport only the default port is supported at the moment\n");
1477                         error = ENOTSUP;
1478                         goto bad2;
1479                 }
1480                 switch(msgsize) {
1481                 case 0:
1482                         /*
1483                          * Wait on port for message
1484                          */
1485                         sysun = lwkt_getport(&td->td_msgport);
1486                         /* XXX block */
1487                         break;
1488                 case -1:
1489                         /*
1490                          * Test port for message
1491                          */
1492                         sysun = lwkt_getport(&td->td_msgport);
1493                         break;
1494                 default:
1495                         error = ENOSYS;
1496                         goto bad2;
1497                 }
1498                 if (sysun) {
1499                         gd = td->td_gd;
1500                         umsg = sysun->lmsg.opaque.ms_umsg;
1501                         frame.tf_eax = (register_t)umsg;
1502                         if (sysun->sysmsg_copyout)
1503                                 sysun->sysmsg_copyout(sysun);
1504                         atomic_add_int_nonlocked(&td->td_msgport.mp_refs, -1);
1505                         sysun->nosys.usrmsg.umsg.u.ms_fds[0] = sysun->lmsg.u.ms_fds[0];
1506                         sysun->nosys.usrmsg.umsg.u.ms_fds[1] = sysun->lmsg.u.ms_fds[1];
1507                         sysun->nosys.usrmsg.umsg.ms_error = sysun->lmsg.ms_error;
1508                         error = sysun->lmsg.ms_error;
1509                         result = sysun->lmsg.u.ms_fds[0]; /* for ktrace */
1510                         if (error != 0 || code != SYS_execve) {
1511                                 error = copyout(
1512                                             &sysun->nosys.usrmsg.umsg.ms_copyout_start,
1513                                             &umsg->ms_copyout_start,
1514                                             ms_copyout_size);
1515                         }
1516                         crit_enter_quick(td);
1517                         sysun->lmsg.opaque.ms_sysunnext = gd->gd_freesysun;
1518                         gd->gd_freesysun = sysun;
1519                         crit_exit_quick(td);
1520                 } else {
1521                         frame.tf_eax = 0;
1522                 }
1523                 frame.tf_edx = 0;
1524                 code = 0;
1525                 error = 0;
1526                 goto good;
1527         }
1528
1529         /*
1530          * Extract the system call message.  If msgsize is zero we are 
1531          * blocking on a message and/or message port.  If msgsize is -1 
1532          * we are testing a message for completion or a message port for
1533          * activity.
1534          *
1535          * The userland system call message size includes the size of the
1536          * userland lwkt_msg plus arguments.  We load it into the userland
1537          * portion of our sysunion structure then we initialize the kerneland
1538          * portion and go.
1539          */
1540
1541         /*
1542          * Bad message size
1543          */
1544         if (msgsize < sizeof(struct lwkt_msg) ||
1545             msgsize > sizeof(union sysunion) - sizeof(struct sysmsg)
1546         ) {
1547                 error = ENOSYS;
1548                 goto bad2;
1549         }
1550
1551         /*
1552          * Obtain a sysun from our per-cpu cache or allocate a new one.  Use
1553          * the opaque field to store the original (user) message pointer.
1554          * A critical section is necessary to interlock against interrupts
1555          * returning system messages to the thread cache.
1556          */
1557         gd = td->td_gd;
1558         crit_enter_quick(td);
1559         if ((sysun = gd->gd_freesysun) != NULL) {
1560                 gd->gd_freesysun = sysun->lmsg.opaque.ms_sysunnext;
1561                 crit_exit_quick(td);
1562         } else {
1563                 crit_exit_quick(td);
1564                 sysun = malloc(sizeof(union sysunion), M_SYSMSG, M_WAITOK);
1565         }
1566         atomic_add_int_nonlocked(&td->td_msgport.mp_refs, 1);
1567
1568         /*
1569          * Copy the user request into the kernel copy of the user request.
1570          */
1571         umsg = (void *)frame.tf_ecx;
1572         error = copyin(umsg, &sysun->nosys.usrmsg, msgsize);
1573         if (error)
1574                 goto bad1;
1575         if ((sysun->nosys.usrmsg.umsg.ms_flags & MSGF_ASYNC) &&
1576             (error = suser(td)) != 0
1577         ) {
1578                 goto bad1;
1579         }
1580
1581         /*
1582          * Initialize the kernel message from the copied-in data and
1583          * pull in appropriate flags from the userland message.
1584          */
1585         lwkt_initmsg(&sysun->lmsg, &td->td_msgport, 
1586             sysun->nosys.usrmsg.umsg.ms_cmd);
1587         sysun->sysmsg_copyout = NULL;
1588         sysun->lmsg.opaque.ms_umsg = umsg;
1589         sysun->lmsg.ms_flags |= sysun->nosys.usrmsg.umsg.ms_flags & MSGF_ASYNC;
1590
1591         /*
1592          * Extract the system call number, lookup the system call, and
1593          * set the default return value.
1594          */
1595         code = (u_int)sysun->lmsg.ms_cmd;
1596         if (code >= p->p_sysent->sv_size) {
1597                 error = ENOSYS;
1598                 goto bad1;
1599         }
1600
1601         callp = &p->p_sysent->sv_table[code];
1602
1603         narg = (msgsize - sizeof(struct lwkt_msg)) / sizeof(register_t);
1604
1605 #ifdef KTRACE
1606         if (KTRPOINT(td, KTR_SYSCALL)) {
1607                 ktrsyscall(p->p_tracep, code, narg, (void *)(&sysun->nosys.usrmsg + 1));
1608         }
1609 #endif
1610         sysun->lmsg.u.ms_fds[0] = 0;
1611         sysun->lmsg.u.ms_fds[1] = 0;
1612
1613         STOPEVENT(p, S_SCE, narg);      /* MP aware */
1614
1615         /*
1616          * Make the system call.  An error code is always returned, results
1617          * are copied back via ms_result32 or ms_result64.  YYY temporary
1618          * stage copy p_retval[] into ms_result32/64
1619          *
1620          * NOTE!  XXX if this is a child returning from a fork curproc
1621          * might be different.  YYY huh? a child returning from a fork
1622          * should never 'return' from this call, it should go right to the
1623          * fork_trampoline function.
1624          */
1625         error = (*callp->sy_call)(sysun);
1626         gd = td->td_gd; /* RELOAD, might have switched cpus */
1627
1628 bad1:
1629         /*
1630          * If a synchronous return copy p_retval to ms_result64 and return
1631          * the sysmsg to the free pool.
1632          *
1633          * YYY Don't writeback message if execve() YYY
1634          */
1635         if (error != EASYNC) {
1636                 atomic_add_int_nonlocked(&td->td_msgport.mp_refs, -1);
1637                 sysun->nosys.usrmsg.umsg.u.ms_fds[0] = sysun->lmsg.u.ms_fds[0];
1638                 sysun->nosys.usrmsg.umsg.u.ms_fds[1] = sysun->lmsg.u.ms_fds[1];
1639                 result = sysun->nosys.usrmsg.umsg.u.ms_fds[0]; /* for ktrace */
1640                 if (error != 0 || code != SYS_execve) {
1641                         int error2;
1642                         error2 = copyout(&sysun->nosys.usrmsg.umsg.ms_copyout_start,
1643                                         &umsg->ms_copyout_start,
1644                                         ms_copyout_size);
1645                         if (error == 0)
1646                                 error2 = error;
1647                 }
1648                 crit_enter_quick(td);
1649                 sysun->lmsg.opaque.ms_sysunnext = gd->gd_freesysun;
1650                 gd->gd_freesysun = sysun;
1651                 crit_exit_quick(td);
1652         }
1653 bad2:
1654         frame.tf_eax = error;
1655 good:
1656
1657         /*
1658          * Traced syscall.  trapsignal() is not MP aware.
1659          */
1660         if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1661                 frame.tf_eflags &= ~PSL_T;
1662                 trapsignal(p, SIGTRAP, 0);
1663         }
1664
1665         /*
1666          * Handle reschedule and other end-of-syscall issues
1667          */
1668         userret(p, &frame, sticks);
1669
1670 #ifdef KTRACE
1671         if (KTRPOINT(td, KTR_SYSRET)) {
1672                 ktrsysret(p->p_tracep, code, error, result);
1673         }
1674 #endif
1675
1676         /*
1677          * This works because errno is findable through the
1678          * register set.  If we ever support an emulation where this
1679          * is not the case, this code will need to be revisited.
1680          */
1681         STOPEVENT(p, S_SCX, code);
1682
1683         userexit(p);
1684 #ifdef SMP
1685         /*
1686          * Release the MP lock if we had to get it
1687          */
1688         KASSERT(curthread->td_mpcount == 1, ("badmpcount syscall from %p", (void *)frame.tf_eip));
1689         rel_mplock();
1690 #endif
1691 }
1692
1693 /*
1694  * Simplified back end of syscall(), used when returning from fork()
1695  * directly into user mode.  MP lock is held on entry and should be
1696  * released on return.  This code will return back into the fork
1697  * trampoline code which then runs doreti.
1698  */
1699 void
1700 fork_return(p, frame)
1701         struct proc *p;
1702         struct trapframe frame;
1703 {
1704         frame.tf_eax = 0;               /* Child returns zero */
1705         frame.tf_eflags &= ~PSL_C;      /* success */
1706         frame.tf_edx = 1;
1707
1708         userret(p, &frame, 0);
1709 #ifdef KTRACE
1710         if (KTRPOINT(p->p_thread, KTR_SYSRET))
1711                 ktrsysret(p->p_tracep, SYS_fork, 0, 0);
1712 #endif
1713         p->p_flag |= P_PASSIVE_ACQ;
1714         userexit(p);
1715         p->p_flag &= ~P_PASSIVE_ACQ;
1716 #ifdef SMP
1717         KKASSERT(curthread->td_mpcount == 1);
1718         rel_mplock();
1719 #endif
1720 }
1721