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