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