]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/i386/i386/trap.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 /*
44  * 386 Trap and System call handling
45  */
46
47 #include "opt_clock.h"
48 #include "opt_cpu.h"
49 #include "opt_hwpmc_hooks.h"
50 #include "opt_isa.h"
51 #include "opt_kdb.h"
52 #include "opt_kdtrace.h"
53 #include "opt_ktrace.h"
54 #include "opt_npx.h"
55 #include "opt_trap.h"
56
57 #include <sys/param.h>
58 #include <sys/bus.h>
59 #include <sys/systm.h>
60 #include <sys/proc.h>
61 #include <sys/pioctl.h>
62 #include <sys/ptrace.h>
63 #include <sys/kdb.h>
64 #include <sys/kernel.h>
65 #include <sys/ktr.h>
66 #include <sys/lock.h>
67 #include <sys/mutex.h>
68 #include <sys/resourcevar.h>
69 #include <sys/signalvar.h>
70 #include <sys/syscall.h>
71 #include <sys/sysctl.h>
72 #include <sys/sysent.h>
73 #include <sys/uio.h>
74 #include <sys/vmmeter.h>
75 #ifdef KTRACE
76 #include <sys/ktrace.h>
77 #endif
78 #ifdef HWPMC_HOOKS
79 #include <sys/pmckern.h>
80 #endif
81 #include <security/audit/audit.h>
82
83 #include <vm/vm.h>
84 #include <vm/vm_param.h>
85 #include <vm/pmap.h>
86 #include <vm/vm_kern.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_page.h>
89 #include <vm/vm_extern.h>
90
91 #include <machine/cpu.h>
92 #include <machine/intr_machdep.h>
93 #include <machine/md_var.h>
94 #include <machine/pcb.h>
95 #ifdef SMP
96 #include <machine/smp.h>
97 #endif
98 #include <machine/tss.h>
99 #include <machine/vm86.h>
100
101 #ifdef POWERFAIL_NMI
102 #include <sys/syslog.h>
103 #include <machine/clock.h>
104 #endif
105
106 #ifdef KDTRACE_HOOKS
107 #include <sys/dtrace_bsd.h>
108
109 /*
110  * This is a hook which is initialised by the dtrace module
111  * to handle traps which might occur during DTrace probe
112  * execution.
113  */
114 dtrace_trap_func_t      dtrace_trap_func;
115
116 dtrace_doubletrap_func_t        dtrace_doubletrap_func;
117
118 /*
119  * This is a hook which is initialised by the systrace module
120  * when it is loaded. This keeps the DTrace syscall provider
121  * implementation opaque. 
122  */
123 systrace_probe_func_t   systrace_probe_func;
124 #endif
125
126 extern void trap(struct trapframe *frame);
127 extern void syscall(struct trapframe *frame);
128
129 static int trap_pfault(struct trapframe *, int, vm_offset_t);
130 static void trap_fatal(struct trapframe *, vm_offset_t);
131 void dblfault_handler(void);
132
133 extern inthand_t IDTVEC(lcall_syscall);
134
135 #define MAX_TRAP_MSG            30
136 static char *trap_msg[] = {
137         "",                                     /*  0 unused */
138         "privileged instruction fault",         /*  1 T_PRIVINFLT */
139         "",                                     /*  2 unused */
140         "breakpoint instruction fault",         /*  3 T_BPTFLT */
141         "",                                     /*  4 unused */
142         "",                                     /*  5 unused */
143         "arithmetic trap",                      /*  6 T_ARITHTRAP */
144         "",                                     /*  7 unused */
145         "",                                     /*  8 unused */
146         "general protection fault",             /*  9 T_PROTFLT */
147         "trace trap",                           /* 10 T_TRCTRAP */
148         "",                                     /* 11 unused */
149         "page fault",                           /* 12 T_PAGEFLT */
150         "",                                     /* 13 unused */
151         "alignment fault",                      /* 14 T_ALIGNFLT */
152         "",                                     /* 15 unused */
153         "",                                     /* 16 unused */
154         "",                                     /* 17 unused */
155         "integer divide fault",                 /* 18 T_DIVIDE */
156         "non-maskable interrupt trap",          /* 19 T_NMI */
157         "overflow trap",                        /* 20 T_OFLOW */
158         "FPU bounds check fault",               /* 21 T_BOUND */
159         "FPU device not available",             /* 22 T_DNA */
160         "double fault",                         /* 23 T_DOUBLEFLT */
161         "FPU operand fetch fault",              /* 24 T_FPOPFLT */
162         "invalid TSS fault",                    /* 25 T_TSSFLT */
163         "segment not present fault",            /* 26 T_SEGNPFLT */
164         "stack fault",                          /* 27 T_STKFLT */
165         "machine check trap",                   /* 28 T_MCHK */
166         "SIMD floating-point exception",        /* 29 T_XMMFLT */
167         "reserved (unknown) fault",             /* 30 T_RESERVED */
168 };
169
170 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
171 extern int has_f00f_bug;
172 #endif
173
174 #ifdef KDB
175 static int kdb_on_nmi = 1;
176 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
177         &kdb_on_nmi, 0, "Go to KDB on NMI");
178 #endif
179 static int panic_on_nmi = 1;
180 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
181         &panic_on_nmi, 0, "Panic on NMI");
182 static int prot_fault_translation = 0;
183 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
184         &prot_fault_translation, 0, "Select signal to deliver on protection fault");
185
186 extern char *syscallnames[];
187
188 /*
189  * Exception, fault, and trap interface to the FreeBSD kernel.
190  * This common code is called from assembly language IDT gate entry
191  * routines that prepare a suitable stack frame, and restore this
192  * frame after the exception has been processed.
193  */
194
195 void
196 trap(struct trapframe *frame)
197 {
198         struct thread *td = curthread;
199         struct proc *p = td->td_proc;
200         int i = 0, ucode = 0, code;
201         u_int type;
202         register_t addr = 0;
203         vm_offset_t eva;
204         ksiginfo_t ksi;
205 #ifdef POWERFAIL_NMI
206         static int lastalert = 0;
207 #endif
208
209         PCPU_INC(cnt.v_trap);
210         type = frame->tf_trapno;
211
212 #ifdef SMP
213 #ifdef STOP_NMI
214         /* Handler for NMI IPIs used for stopping CPUs. */
215         if (type == T_NMI) {
216                  if (ipi_nmi_handler() == 0)
217                            goto out;
218         }
219 #endif /* STOP_NMI */
220 #endif /* SMP */
221
222 #ifdef KDB
223         if (kdb_active) {
224                 kdb_reenter();
225                 goto out;
226         }
227 #endif
228
229 #ifdef  HWPMC_HOOKS
230         /*
231          * CPU PMCs interrupt using an NMI so we check for that first.
232          * If the HWPMC module is active, 'pmc_hook' will point to
233          * the function to be called.  A return value of '1' from the
234          * hook means that the NMI was handled by it and that we can
235          * return immediately.
236          */
237         if (type == T_NMI && pmc_intr &&
238             (*pmc_intr)(PCPU_GET(cpuid), (uintptr_t) frame->tf_eip,
239                 TRAPF_USERMODE(frame)))
240             goto out;
241 #endif
242
243 #ifdef KDTRACE_HOOKS
244         /*
245          * A trap can occur while DTrace executes a probe. Before
246          * executing the probe, DTrace blocks re-scheduling and sets
247          * a flag in it's per-cpu flags to indicate that it doesn't
248          * want to fault. On returning from the the probe, the no-fault
249          * flag is cleared and finally re-scheduling is enabled.
250          *
251          * If the DTrace kernel module has registered a trap handler,
252          * call it and if it returns non-zero, assume that it has
253          * handled the trap and modified the trap frame so that this
254          * function can return normally.
255          */
256         if ((type == T_PROTFLT || type == T_PAGEFLT) &&
257             dtrace_trap_func != NULL)
258                 if ((*dtrace_trap_func)(frame, type))
259                         goto out;
260 #endif
261
262         if ((frame->tf_eflags & PSL_I) == 0) {
263                 /*
264                  * Buggy application or kernel code has disabled
265                  * interrupts and then trapped.  Enabling interrupts
266                  * now is wrong, but it is better than running with
267                  * interrupts disabled until they are accidentally
268                  * enabled later.
269                  */
270                 if (ISPL(frame->tf_cs) == SEL_UPL || (frame->tf_eflags & PSL_VM))
271                         printf(
272                             "pid %ld (%s): trap %d with interrupts disabled\n",
273                             (long)curproc->p_pid, curproc->p_comm, type);
274                 else if (type != T_BPTFLT && type != T_TRCTRAP &&
275                          frame->tf_eip != (int)cpu_switch_load_gs) {
276                         /*
277                          * XXX not quite right, since this may be for a
278                          * multiple fault in user mode.
279                          */
280                         printf("kernel trap %d with interrupts disabled\n",
281                             type);
282                         /*
283                          * Page faults need interrupts disabled until later,
284                          * and we shouldn't enable interrupts while holding
285                          * a spin lock or if servicing an NMI.
286                          */
287                         if (type != T_NMI && type != T_PAGEFLT &&
288                             td->td_md.md_spinlock_count == 0)
289                                 enable_intr();
290                 }
291         }
292
293         eva = 0;
294         code = frame->tf_err;
295         if (type == T_PAGEFLT) {
296                 /*
297                  * For some Cyrix CPUs, %cr2 is clobbered by
298                  * interrupts.  This problem is worked around by using
299                  * an interrupt gate for the pagefault handler.  We
300                  * are finally ready to read %cr2 and then must
301                  * reenable interrupts.
302                  *
303                  * If we get a page fault while in a critical section, then
304                  * it is most likely a fatal kernel page fault.  The kernel
305                  * is already going to panic trying to get a sleep lock to
306                  * do the VM lookup, so just consider it a fatal trap so the
307                  * kernel can print out a useful trap message and even get
308                  * to the debugger.
309                  *
310                  * If we get a page fault while holding a non-sleepable
311                  * lock, then it is most likely a fatal kernel page fault.
312                  * If WITNESS is enabled, then it's going to whine about
313                  * bogus LORs with various VM locks, so just skip to the
314                  * fatal trap handling directly.
315                  */
316                 eva = rcr2();
317                 if (td->td_critnest != 0 ||
318                     WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
319                     "Kernel page fault") != 0)
320                         trap_fatal(frame, eva);
321                 else
322                         enable_intr();
323         }
324
325         if ((ISPL(frame->tf_cs) == SEL_UPL) ||
326             ((frame->tf_eflags & PSL_VM) && 
327                 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL))) {
328                 /* user trap */
329
330                 td->td_pticks = 0;
331                 td->td_frame = frame;
332                 addr = frame->tf_eip;
333                 if (td->td_ucred != p->p_ucred) 
334                         cred_update_thread(td);
335
336                 switch (type) {
337                 case T_PRIVINFLT:       /* privileged instruction fault */
338                         i = SIGILL;
339                         ucode = ILL_PRVOPC;
340                         break;
341
342                 case T_BPTFLT:          /* bpt instruction fault */
343                 case T_TRCTRAP:         /* trace trap */
344                         enable_intr();
345                         frame->tf_eflags &= ~PSL_T;
346                         i = SIGTRAP;
347                         ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
348                         break;
349
350                 case T_ARITHTRAP:       /* arithmetic trap */
351 #ifdef DEV_NPX
352                         ucode = npxtrap();
353                         if (ucode == -1)
354                                 goto userout;
355 #else
356                         ucode = 0;
357 #endif
358                         i = SIGFPE;
359                         break;
360
361                         /*
362                          * The following two traps can happen in
363                          * vm86 mode, and, if so, we want to handle
364                          * them specially.
365                          */
366                 case T_PROTFLT:         /* general protection fault */
367                 case T_STKFLT:          /* stack fault */
368                         if (frame->tf_eflags & PSL_VM) {
369                                 i = vm86_emulate((struct vm86frame *)frame);
370                                 if (i == 0)
371                                         goto user;
372                                 break;
373                         }
374                         i = SIGBUS;
375                         ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
376                         break;
377                 case T_SEGNPFLT:        /* segment not present fault */
378                         i = SIGBUS;
379                         ucode = BUS_ADRERR;
380                         break;
381                 case T_TSSFLT:          /* invalid TSS fault */
382                         i = SIGBUS;
383                         ucode = BUS_OBJERR;
384                         break;
385                 case T_DOUBLEFLT:       /* double fault */
386                 default:
387                         i = SIGBUS;
388                         ucode = BUS_OBJERR;
389                         break;
390
391                 case T_PAGEFLT:         /* page fault */
392 #ifdef KSE
393                         if (td->td_pflags & TDP_SA)
394                                 thread_user_enter(td);
395 #endif
396
397                         i = trap_pfault(frame, TRUE, eva);
398 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
399                         if (i == -2) {
400                                 /*
401                                  * The f00f hack workaround has triggered, so
402                                  * treat the fault as an illegal instruction 
403                                  * (T_PRIVINFLT) instead of a page fault.
404                                  */
405                                 type = frame->tf_trapno = T_PRIVINFLT;
406
407                                 /* Proceed as in that case. */
408                                 ucode = ILL_PRVOPC;
409                                 i = SIGILL;
410                                 break;
411                         }
412 #endif
413                         if (i == -1)
414                                 goto userout;
415                         if (i == 0)
416                                 goto user;
417
418                         if (i == SIGSEGV)
419                                 ucode = SEGV_MAPERR;
420                         else {
421                                 if (prot_fault_translation == 0) {
422                                         /*
423                                          * Autodetect.
424                                          * This check also covers the images
425                                          * without the ABI-tag ELF note.
426                                          */
427                                         if (p->p_osrel >= 700004) {
428                                                 i = SIGSEGV;
429                                                 ucode = SEGV_ACCERR;
430                                         } else {
431                                                 i = SIGBUS;
432                                                 ucode = BUS_PAGE_FAULT;
433                                         }
434                                 } else if (prot_fault_translation == 1) {
435                                         /*
436                                          * Always compat mode.
437                                          */
438                                         i = SIGBUS;
439                                         ucode = BUS_PAGE_FAULT;
440                                 } else {
441                                         /*
442                                          * Always SIGSEGV mode.
443                                          */
444                                         i = SIGSEGV;
445                                         ucode = SEGV_ACCERR;
446                                 }
447                         }
448                         addr = eva;
449                         break;
450
451                 case T_DIVIDE:          /* integer divide fault */
452                         ucode = FPE_INTDIV;
453                         i = SIGFPE;
454                         break;
455
456 #ifdef DEV_ISA
457                 case T_NMI:
458 #ifdef POWERFAIL_NMI
459 #ifndef TIMER_FREQ
460 #  define TIMER_FREQ 1193182
461 #endif
462                         mtx_lock(&Giant);
463                         if (time_second - lastalert > 10) {
464                                 log(LOG_WARNING, "NMI: power fail\n");
465                                 sysbeep(TIMER_FREQ/880, hz);
466                                 lastalert = time_second;
467                         }
468                         mtx_unlock(&Giant);
469                         goto userout;
470 #else /* !POWERFAIL_NMI */
471                         /* machine/parity/power fail/"kitchen sink" faults */
472                         /* XXX Giant */
473                         if (isa_nmi(code) == 0) {
474 #ifdef KDB
475                                 /*
476                                  * NMI can be hooked up to a pushbutton
477                                  * for debugging.
478                                  */
479                                 if (kdb_on_nmi) {
480                                         printf ("NMI ... going to debugger\n");
481                                         kdb_trap(type, 0, frame);
482                                 }
483 #endif /* KDB */
484                                 goto userout;
485                         } else if (panic_on_nmi)
486                                 panic("NMI indicates hardware failure");
487                         break;
488 #endif /* POWERFAIL_NMI */
489 #endif /* DEV_ISA */
490
491                 case T_OFLOW:           /* integer overflow fault */
492                         ucode = FPE_INTOVF;
493                         i = SIGFPE;
494                         break;
495
496                 case T_BOUND:           /* bounds check fault */
497                         ucode = FPE_FLTSUB;
498                         i = SIGFPE;
499                         break;
500
501                 case T_DNA:
502 #ifdef DEV_NPX
503                         /* transparent fault (due to context switch "late") */
504                         if (npxdna())
505                                 goto userout;
506 #endif
507                         printf("pid %d killed due to lack of floating point\n",
508                                 p->p_pid);
509                         i = SIGKILL;
510                         ucode = 0;
511                         break;
512
513                 case T_FPOPFLT:         /* FPU operand fetch fault */
514                         ucode = ILL_COPROC;
515                         i = SIGILL;
516                         break;
517
518                 case T_XMMFLT:          /* SIMD floating-point exception */
519                         ucode = 0; /* XXX */
520                         i = SIGFPE;
521                         break;
522                 }
523         } else {
524                 /* kernel trap */
525
526                 KASSERT(cold || td->td_ucred != NULL,
527                     ("kernel trap doesn't have ucred"));
528                 switch (type) {
529                 case T_PAGEFLT:                 /* page fault */
530                         (void) trap_pfault(frame, FALSE, eva);
531                         goto out;
532
533                 case T_DNA:
534 #ifdef DEV_NPX
535                         /*
536                          * The kernel is apparently using npx for copying.
537                          * XXX this should be fatal unless the kernel has
538                          * registered such use.
539                          */
540                         if (npxdna())
541                                 goto out;
542 #endif
543                         break;
544
545                         /*
546                          * The following two traps can happen in
547                          * vm86 mode, and, if so, we want to handle
548                          * them specially.
549                          */
550                 case T_PROTFLT:         /* general protection fault */
551                 case T_STKFLT:          /* stack fault */
552                         if (frame->tf_eflags & PSL_VM) {
553                                 i = vm86_emulate((struct vm86frame *)frame);
554                                 if (i != 0)
555                                         /*
556                                          * returns to original process
557                                          */
558                                         vm86_trap((struct vm86frame *)frame);
559                                 goto out;
560                         }
561                         if (type == T_STKFLT)
562                                 break;
563
564                         /* FALL THROUGH */
565
566                 case T_SEGNPFLT:        /* segment not present fault */
567                         if (PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)
568                                 break;
569
570                         /*
571                          * Invalid %fs's and %gs's can be created using
572                          * procfs or PT_SETREGS or by invalidating the
573                          * underlying LDT entry.  This causes a fault
574                          * in kernel mode when the kernel attempts to
575                          * switch contexts.  Lose the bad context
576                          * (XXX) so that we can continue, and generate
577                          * a signal.
578                          */
579                         if (frame->tf_eip == (int)cpu_switch_load_gs) {
580                                 PCPU_GET(curpcb)->pcb_gs = 0;
581 #if 0                           
582                                 PROC_LOCK(p);
583                                 psignal(p, SIGBUS);
584                                 PROC_UNLOCK(p);
585 #endif                          
586                                 goto out;
587                         }
588
589                         if (td->td_intr_nesting_level != 0)
590                                 break;
591
592                         /*
593                          * Invalid segment selectors and out of bounds
594                          * %eip's and %esp's can be set up in user mode.
595                          * This causes a fault in kernel mode when the
596                          * kernel tries to return to user mode.  We want
597                          * to get this fault so that we can fix the
598                          * problem here and not have to check all the
599                          * selectors and pointers when the user changes
600                          * them.
601                          */
602                         if (frame->tf_eip == (int)doreti_iret) {
603                                 frame->tf_eip = (int)doreti_iret_fault;
604                                 goto out;
605                         }
606                         if (frame->tf_eip == (int)doreti_popl_ds) {
607                                 frame->tf_eip = (int)doreti_popl_ds_fault;
608                                 goto out;
609                         }
610                         if (frame->tf_eip == (int)doreti_popl_es) {
611                                 frame->tf_eip = (int)doreti_popl_es_fault;
612                                 goto out;
613                         }
614                         if (frame->tf_eip == (int)doreti_popl_fs) {
615                                 frame->tf_eip = (int)doreti_popl_fs_fault;
616                                 goto out;
617                         }
618                         if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
619                                 frame->tf_eip =
620                                     (int)PCPU_GET(curpcb)->pcb_onfault;
621                                 goto out;
622                         }
623                         break;
624
625                 case T_TSSFLT:
626                         /*
627                          * PSL_NT can be set in user mode and isn't cleared
628                          * automatically when the kernel is entered.  This
629                          * causes a TSS fault when the kernel attempts to
630                          * `iret' because the TSS link is uninitialized.  We
631                          * want to get this fault so that we can fix the
632                          * problem here and not every time the kernel is
633                          * entered.
634                          */
635                         if (frame->tf_eflags & PSL_NT) {
636                                 frame->tf_eflags &= ~PSL_NT;
637                                 goto out;
638                         }
639                         break;
640
641                 case T_TRCTRAP:  /* trace trap */
642                         if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
643                                 /*
644                                  * We've just entered system mode via the
645                                  * syscall lcall.  Continue single stepping
646                                  * silently until the syscall handler has
647                                  * saved the flags.
648                                  */
649                                 goto out;
650                         }
651                         if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
652                                 /*
653                                  * The syscall handler has now saved the
654                                  * flags.  Stop single stepping it.
655                                  */
656                                 frame->tf_eflags &= ~PSL_T;
657                                 goto out;
658                         }
659                         /*
660                          * Ignore debug register trace traps due to
661                          * accesses in the user's address space, which
662                          * can happen under several conditions such as
663                          * if a user sets a watchpoint on a buffer and
664                          * then passes that buffer to a system call.
665                          * We still want to get TRCTRAPS for addresses
666                          * in kernel space because that is useful when
667                          * debugging the kernel.
668                          */
669                         /* XXX Giant */
670                         if (user_dbreg_trap() && 
671                            !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)) {
672                                 /*
673                                  * Reset breakpoint bits because the
674                                  * processor doesn't
675                                  */
676                                 load_dr6(rdr6() & 0xfffffff0);
677                                 goto out;
678                         }
679                         /*
680                          * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
681                          */
682                 case T_BPTFLT:
683                         /*
684                          * If KDB is enabled, let it handle the debugger trap.
685                          * Otherwise, debugger traps "can't happen".
686                          */
687 #ifdef KDB
688                         if (kdb_trap(type, 0, frame))
689                                 goto out;
690 #endif
691                         break;
692
693 #ifdef DEV_ISA
694                 case T_NMI:
695 #ifdef POWERFAIL_NMI
696                         mtx_lock(&Giant);
697                         if (time_second - lastalert > 10) {
698                                 log(LOG_WARNING, "NMI: power fail\n");
699                                 sysbeep(TIMER_FREQ/880, hz);
700                                 lastalert = time_second;
701                         }
702                         mtx_unlock(&Giant);
703                         goto out;
704 #else /* !POWERFAIL_NMI */
705                         /* XXX Giant */
706                         /* machine/parity/power fail/"kitchen sink" faults */
707                         if (isa_nmi(code) == 0) {
708 #ifdef KDB
709                                 /*
710                                  * NMI can be hooked up to a pushbutton
711                                  * for debugging.
712                                  */
713                                 if (kdb_on_nmi) {
714                                         printf ("NMI ... going to debugger\n");
715                                         kdb_trap(type, 0, frame);
716                                 }
717 #endif /* KDB */
718                                 goto out;
719                         } else if (panic_on_nmi == 0)
720                                 goto out;
721                         /* FALLTHROUGH */
722 #endif /* POWERFAIL_NMI */
723 #endif /* DEV_ISA */
724                 }
725
726                 trap_fatal(frame, eva);
727                 goto out;
728         }
729
730         /* Translate fault for emulators (e.g. Linux) */
731         if (*p->p_sysent->sv_transtrap)
732                 i = (*p->p_sysent->sv_transtrap)(i, type);
733
734         ksiginfo_init_trap(&ksi);
735         ksi.ksi_signo = i;
736         ksi.ksi_code = ucode;
737         ksi.ksi_addr = (void *)addr;
738         ksi.ksi_trapno = type;
739         trapsignal(td, &ksi);
740
741 #ifdef DEBUG
742         if (type <= MAX_TRAP_MSG) {
743                 uprintf("fatal process exception: %s",
744                         trap_msg[type]);
745                 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
746                         uprintf(", fault VA = 0x%lx", (u_long)eva);
747                 uprintf("\n");
748         }
749 #endif
750
751 user:
752         userret(td, frame);
753         mtx_assert(&Giant, MA_NOTOWNED);
754 userout:
755 out:
756         return;
757 }
758
759 static int
760 trap_pfault(frame, usermode, eva)
761         struct trapframe *frame;
762         int usermode;
763         vm_offset_t eva;
764 {
765         vm_offset_t va;
766         struct vmspace *vm = NULL;
767         vm_map_t map;
768         int rv = 0;
769         vm_prot_t ftype;
770         struct thread *td = curthread;
771         struct proc *p = td->td_proc;
772
773         va = trunc_page(eva);
774         if (va >= KERNBASE) {
775                 /*
776                  * Don't allow user-mode faults in kernel address space.
777                  * An exception:  if the faulting address is the invalid
778                  * instruction entry in the IDT, then the Intel Pentium
779                  * F00F bug workaround was triggered, and we need to
780                  * treat it is as an illegal instruction, and not a page
781                  * fault.
782                  */
783 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
784                 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
785                         return -2;
786 #endif
787                 if (usermode)
788                         goto nogo;
789
790                 map = kernel_map;
791         } else {
792                 /*
793                  * This is a fault on non-kernel virtual memory.
794                  * vm is initialized above to NULL. If curproc is NULL
795                  * or curproc->p_vmspace is NULL the fault is fatal.
796                  */
797                 if (p != NULL)
798                         vm = p->p_vmspace;
799
800                 if (vm == NULL)
801                         goto nogo;
802
803                 map = &vm->vm_map;
804         }
805
806         /*
807          * PGEX_I is defined only if the execute disable bit capability is
808          * supported and enabled.
809          */
810         if (frame->tf_err & PGEX_W)
811                 ftype = VM_PROT_WRITE;
812 #ifdef PAE
813         else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
814                 ftype = VM_PROT_EXECUTE;
815 #endif
816         else
817                 ftype = VM_PROT_READ;
818
819         if (map != kernel_map) {
820                 /*
821                  * Keep swapout from messing with us during this
822                  *      critical time.
823                  */
824                 PROC_LOCK(p);
825                 ++p->p_lock;
826                 PROC_UNLOCK(p);
827
828                 /* Fault in the user page: */
829                 rv = vm_fault(map, va, ftype,
830                               (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
831                                                       : VM_FAULT_NORMAL);
832
833                 PROC_LOCK(p);
834                 --p->p_lock;
835                 PROC_UNLOCK(p);
836         } else {
837                 /*
838                  * Don't have to worry about process locking or stacks in the
839                  * kernel.
840                  */
841                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
842         }
843         if (rv == KERN_SUCCESS)
844                 return (0);
845 nogo:
846         if (!usermode) {
847                 if (td->td_intr_nesting_level == 0 &&
848                     PCPU_GET(curpcb)->pcb_onfault != NULL) {
849                         frame->tf_eip = (int)PCPU_GET(curpcb)->pcb_onfault;
850                         return (0);
851                 }
852                 trap_fatal(frame, eva);
853                 return (-1);
854         }
855
856         return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
857 }
858
859 static void
860 trap_fatal(frame, eva)
861         struct trapframe *frame;
862         vm_offset_t eva;
863 {
864         int code, ss, esp;
865         u_int type;
866         struct soft_segment_descriptor softseg;
867         char *msg;
868
869         code = frame->tf_err;
870         type = frame->tf_trapno;
871         sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
872
873         if (type <= MAX_TRAP_MSG)
874                 msg = trap_msg[type];
875         else
876                 msg = "UNKNOWN";
877         printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
878             frame->tf_eflags & PSL_VM ? "vm86" :
879             ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
880 #ifdef SMP
881         /* two separate prints in case of a trap on an unmapped page */
882         printf("cpuid = %d; ", PCPU_GET(cpuid));
883         printf("apic id = %02x\n", PCPU_GET(apic_id));
884 #endif
885         if (type == T_PAGEFLT) {
886                 printf("fault virtual address   = 0x%x\n", eva);
887                 printf("fault code              = %s %s, %s\n",
888                         code & PGEX_U ? "user" : "supervisor",
889                         code & PGEX_W ? "write" : "read",
890                         code & PGEX_P ? "protection violation" : "page not present");
891         }
892         printf("instruction pointer     = 0x%x:0x%x\n",
893                frame->tf_cs & 0xffff, frame->tf_eip);
894         if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
895                 ss = frame->tf_ss & 0xffff;
896                 esp = frame->tf_esp;
897         } else {
898                 ss = GSEL(GDATA_SEL, SEL_KPL);
899                 esp = (int)&frame->tf_esp;
900         }
901         printf("stack pointer           = 0x%x:0x%x\n", ss, esp);
902         printf("frame pointer           = 0x%x:0x%x\n", ss, frame->tf_ebp);
903         printf("code segment            = base 0x%x, limit 0x%x, type 0x%x\n",
904                softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
905         printf("                        = DPL %d, pres %d, def32 %d, gran %d\n",
906                softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
907                softseg.ssd_gran);
908         printf("processor eflags        = ");
909         if (frame->tf_eflags & PSL_T)
910                 printf("trace trap, ");
911         if (frame->tf_eflags & PSL_I)
912                 printf("interrupt enabled, ");
913         if (frame->tf_eflags & PSL_NT)
914                 printf("nested task, ");
915         if (frame->tf_eflags & PSL_RF)
916                 printf("resume, ");
917         if (frame->tf_eflags & PSL_VM)
918                 printf("vm86, ");
919         printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
920         printf("current process         = ");
921         if (curproc) {
922                 printf("%lu (%s)\n", (u_long)curproc->p_pid, curproc->p_comm);
923         } else {
924                 printf("Idle\n");
925         }
926
927 #ifdef KDB
928         if (debugger_on_panic || kdb_active) {
929                 frame->tf_err = eva;    /* smuggle fault address to ddb */
930                 if (kdb_trap(type, 0, frame)) {
931                         frame->tf_err = code;   /* restore error code */
932                         return;
933                 }
934                 frame->tf_err = code;           /* restore error code */
935         }
936 #endif
937         printf("trap number             = %d\n", type);
938         if (type <= MAX_TRAP_MSG)
939                 panic("%s", trap_msg[type]);
940         else
941                 panic("unknown/reserved trap");
942 }
943
944 /*
945  * Double fault handler. Called when a fault occurs while writing
946  * a frame for a trap/exception onto the stack. This usually occurs
947  * when the stack overflows (such is the case with infinite recursion,
948  * for example).
949  *
950  * XXX Note that the current PTD gets replaced by IdlePTD when the
951  * task switch occurs. This means that the stack that was active at
952  * the time of the double fault is not available at <kstack> unless
953  * the machine was idle when the double fault occurred. The downside
954  * of this is that "trace <ebp>" in ddb won't work.
955  */
956 void
957 dblfault_handler()
958 {
959 #ifdef KDTRACE_HOOKS
960         if (dtrace_doubletrap_func != NULL)
961                 (*dtrace_doubletrap_func)();
962 #endif
963         printf("\nFatal double fault:\n");
964         printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
965         printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
966         printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
967 #ifdef SMP
968         /* two separate prints in case of a trap on an unmapped page */
969         printf("cpuid = %d; ", PCPU_GET(cpuid));
970         printf("apic id = %02x\n", PCPU_GET(apic_id));
971 #endif
972         panic("double fault");
973 }
974
975 /*
976  *      syscall -       system call request C handler
977  *
978  *      A system call is essentially treated as a trap.
979  */
980 void
981 syscall(struct trapframe *frame)
982 {
983         caddr_t params;
984         struct sysent *callp;
985         struct thread *td = curthread;
986         struct proc *p = td->td_proc;
987         register_t orig_tf_eflags;
988         int error;
989         int narg;
990         int args[8];
991         u_int code;
992         ksiginfo_t ksi;
993
994         PCPU_INC(cnt.v_syscall);
995
996 #ifdef DIAGNOSTIC
997         if (ISPL(frame->tf_cs) != SEL_UPL) {
998                 panic("syscall");
999                 /* NOT REACHED */
1000         }
1001 #endif
1002
1003         td->td_pticks = 0;
1004         td->td_frame = frame;
1005         if (td->td_ucred != p->p_ucred) 
1006                 cred_update_thread(td);
1007 #ifdef KSE
1008         if (p->p_flag & P_SA)
1009                 thread_user_enter(td);
1010 #endif
1011         params = (caddr_t)frame->tf_esp + sizeof(int);
1012         code = frame->tf_eax;
1013         orig_tf_eflags = frame->tf_eflags;
1014
1015         if (p->p_sysent->sv_prepsyscall) {
1016                 /*
1017                  * The prep code is MP aware.
1018                  */
1019                 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, &params);
1020         } else {
1021                 /*
1022                  * Need to check if this is a 32 bit or 64 bit syscall.
1023                  * fuword is MP aware.
1024                  */
1025                 if (code == SYS_syscall) {
1026                         /*
1027                          * Code is first argument, followed by actual args.
1028                          */
1029                         code = fuword(params);
1030                         params += sizeof(int);
1031                 } else if (code == SYS___syscall) {
1032                         /*
1033                          * Like syscall, but code is a quad, so as to maintain
1034                          * quad alignment for the rest of the arguments.
1035                          */
1036                         code = fuword(params);
1037                         params += sizeof(quad_t);
1038                 }
1039         }
1040
1041         if (p->p_sysent->sv_mask)
1042                 code &= p->p_sysent->sv_mask;
1043
1044         if (code >= p->p_sysent->sv_size)
1045                 callp = &p->p_sysent->sv_table[0];
1046         else
1047                 callp = &p->p_sysent->sv_table[code];
1048
1049         narg = callp->sy_narg;
1050
1051         /*
1052          * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
1053          */
1054         if (params != NULL && narg != 0)
1055                 error = copyin(params, (caddr_t)args,
1056                     (u_int)(narg * sizeof(int)));
1057         else
1058                 error = 0;
1059                 
1060 #ifdef KTRACE
1061         if (KTRPOINT(td, KTR_SYSCALL))
1062                 ktrsyscall(code, narg, args);
1063 #endif
1064
1065         CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
1066             td->td_proc->p_pid, td->td_proc->p_comm, code);
1067
1068         td->td_syscalls++;
1069
1070         if (error == 0) {
1071                 td->td_retval[0] = 0;
1072                 td->td_retval[1] = frame->tf_edx;
1073
1074                 STOPEVENT(p, S_SCE, narg);
1075
1076                 PTRACESTOP_SC(p, td, S_PT_SCE);
1077
1078 #ifdef KDTRACE_HOOKS
1079                 /*
1080                  * If the systrace module has registered it's probe
1081                  * callback and if there is a probe active for the
1082                  * syscall 'entry', process the probe.
1083                  */
1084                 if (systrace_probe_func != NULL && callp->sy_entry != 0)
1085                         (*systrace_probe_func)(callp->sy_entry, code, callp,
1086                             args);
1087 #endif
1088
1089                 AUDIT_SYSCALL_ENTER(code, td);
1090                 error = (*callp->sy_call)(td, args);
1091                 AUDIT_SYSCALL_EXIT(error, td);
1092
1093                 /* Save the latest error return value. */
1094                 td->td_errno = error;
1095
1096 #ifdef KDTRACE_HOOKS
1097                 /*
1098                  * If the systrace module has registered it's probe
1099                  * callback and if there is a probe active for the
1100                  * syscall 'return', process the probe.
1101                  */
1102                 if (systrace_probe_func != NULL && callp->sy_return != 0)
1103                         (*systrace_probe_func)(callp->sy_return, code, callp,
1104                             args);
1105 #endif
1106         }
1107
1108         switch (error) {
1109         case 0:
1110                 frame->tf_eax = td->td_retval[0];
1111                 frame->tf_edx = td->td_retval[1];
1112                 frame->tf_eflags &= ~PSL_C;
1113                 break;
1114
1115         case ERESTART:
1116                 /*
1117                  * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1118                  * int 0x80 is 2 bytes. We saved this in tf_err.
1119                  */
1120                 frame->tf_eip -= frame->tf_err;
1121                 break;
1122
1123         case EJUSTRETURN:
1124                 break;
1125
1126         default:
1127                 if (p->p_sysent->sv_errsize) {
1128                         if (error >= p->p_sysent->sv_errsize)
1129                                 error = -1;     /* XXX */
1130                         else
1131                                 error = p->p_sysent->sv_errtbl[error];
1132                 }
1133                 frame->tf_eax = error;
1134                 frame->tf_eflags |= PSL_C;
1135                 break;
1136         }
1137
1138         /*
1139          * Traced syscall.
1140          */
1141         if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
1142                 frame->tf_eflags &= ~PSL_T;
1143                 ksiginfo_init_trap(&ksi);
1144                 ksi.ksi_signo = SIGTRAP;
1145                 ksi.ksi_code = TRAP_TRACE;
1146                 ksi.ksi_addr = (void *)frame->tf_eip;
1147                 trapsignal(td, &ksi);
1148         }
1149
1150         /*
1151          * Check for misbehavior.
1152          */
1153         WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
1154             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
1155         KASSERT(td->td_critnest == 0,
1156             ("System call %s returning in a critical section",
1157             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
1158         KASSERT(td->td_locks == 0,
1159             ("System call %s returning with %d locks held",
1160             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
1161             td->td_locks));
1162
1163         /*
1164          * Handle reschedule and other end-of-syscall issues
1165          */
1166         userret(td, frame);
1167
1168         CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
1169             td->td_proc->p_pid, td->td_proc->p_comm, code);
1170
1171 #ifdef KTRACE
1172         if (KTRPOINT(td, KTR_SYSRET))
1173                 ktrsysret(code, error, td->td_retval[0]);
1174 #endif
1175
1176         /*
1177          * This works because errno is findable through the
1178          * register set.  If we ever support an emulation where this
1179          * is not the case, this code will need to be revisited.
1180          */
1181         STOPEVENT(p, S_SCX, code);
1182
1183         PTRACESTOP_SC(p, td, S_PT_SCX);
1184 }
1185