[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/amd64/amd64/trap.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  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: src/sys/amd64/amd64/trap.c,v 1.328 2008/09/08 09:55:51 kib Exp $");
 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), frame))
220                 goto out;
221 #endif
222 
223 #ifdef KDTRACE_HOOKS
224         /*
225          * A trap can occur while DTrace executes a probe. Before
226          * executing the probe, DTrace blocks re-scheduling and sets
227          * a flag in it's per-cpu flags to indicate that it doesn't
228          * want to fault. On returning from the the probe, the no-fault
229          * flag is cleared and finally re-scheduling is enabled.
230          *
231          * If the DTrace kernel module has registered a trap handler,
232          * call it and if it returns non-zero, assume that it has
233          * handled the trap and modified the trap frame so that this
234          * function can return normally.
235          */
236         if (dtrace_trap_func != NULL)
237                 if ((*dtrace_trap_func)(frame, type))
238                         goto out;
239 #endif
240 
241         if ((frame->tf_rflags & PSL_I) == 0) {
242                 /*
243                  * Buggy application or kernel code has disabled
244                  * interrupts and then trapped.  Enabling interrupts
245                  * now is wrong, but it is better than running with
246                  * interrupts disabled until they are accidentally
247                  * enabled later.
248                  */
249                 if (ISPL(frame->tf_cs) == SEL_UPL)
250                         printf(
251                             "pid %ld (%s): trap %d with interrupts disabled\n",
252                             (long)curproc->p_pid, curthread->td_name, type);
253                 else if (type != T_NMI && type != T_BPTFLT &&
254                     type != T_TRCTRAP) {
255                         /*
256                          * XXX not quite right, since this may be for a
257                          * multiple fault in user mode.
258                          */
259                         printf("kernel trap %d with interrupts disabled\n",
260                             type);
261                         /*
262                          * We shouldn't enable interrupts while holding a
263                          * spin lock or servicing an NMI.
264                          */
265                         if (type != T_NMI && td->td_md.md_spinlock_count == 0)
266                                 enable_intr();
267                 }
268         }
269 
270         code = frame->tf_err;
271         if (type == T_PAGEFLT) {
272                 /*
273                  * If we get a page fault while in a critical section, then
274                  * it is most likely a fatal kernel page fault.  The kernel
275                  * is already going to panic trying to get a sleep lock to
276                  * do the VM lookup, so just consider it a fatal trap so the
277                  * kernel can print out a useful trap message and even get
278                  * to the debugger.
279                  *
280                  * If we get a page fault while holding a non-sleepable
281                  * lock, then it is most likely a fatal kernel page fault.
282                  * If WITNESS is enabled, then it's going to whine about
283                  * bogus LORs with various VM locks, so just skip to the
284                  * fatal trap handling directly.
285                  */
286                 if (td->td_critnest != 0 ||
287                     WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
288                     "Kernel page fault") != 0)
289                         trap_fatal(frame, frame->tf_addr);
290         }
291 
292         if (ISPL(frame->tf_cs) == SEL_UPL) {
293                 /* user trap */
294 
295                 td->td_pticks = 0;
296                 td->td_frame = frame;
297                 addr = frame->tf_rip;
298                 if (td->td_ucred != p->p_ucred) 
299                         cred_update_thread(td);
300 
301                 switch (type) {
302                 case T_PRIVINFLT:       /* privileged instruction fault */
303                         i = SIGILL;
304                         ucode = ILL_PRVOPC;
305                         break;
306 
307                 case T_BPTFLT:          /* bpt instruction fault */
308                 case T_TRCTRAP:         /* trace trap */
309                         enable_intr();
310                         frame->tf_rflags &= ~PSL_T;
311                         i = SIGTRAP;
312                         ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
313                         break;
314 
315                 case T_ARITHTRAP:       /* arithmetic trap */
316                         ucode = fputrap();
317                         if (ucode == -1)
318                                 goto userout;
319                         i = SIGFPE;
320                         break;
321 
322                 case T_PROTFLT:         /* general protection fault */
323                         i = SIGBUS;
324                         ucode = BUS_OBJERR;
325                         break;
326                 case T_STKFLT:          /* stack fault */
327                 case T_SEGNPFLT:        /* segment not present fault */
328                         i = SIGBUS;
329                         ucode = BUS_ADRERR;
330                         break;
331                 case T_TSSFLT:          /* invalid TSS fault */
332                         i = SIGBUS;
333                         ucode = BUS_OBJERR;
334                         break;
335                 case T_DOUBLEFLT:       /* double fault */
336                 default:
337                         i = SIGBUS;
338                         ucode = BUS_OBJERR;
339                         break;
340 
341                 case T_PAGEFLT:         /* page fault */
342                         addr = frame->tf_addr;
343                         i = trap_pfault(frame, TRUE);
344                         if (i == -1)
345                                 goto userout;
346                         if (i == 0)
347                                 goto user;
348 
349                         if (i == SIGSEGV)
350                                 ucode = SEGV_MAPERR;
351                         else {
352                                 if (prot_fault_translation == 0) {
353                                         /*
354                                          * Autodetect.
355                                          * This check also covers the images
356                                          * without the ABI-tag ELF note.
357                                          */
358                                         if (p->p_osrel >= 700004) {
359                                                 i = SIGSEGV;
360                                                 ucode = SEGV_ACCERR;
361                                         } else {
362                                                 i = SIGBUS;
363                                                 ucode = BUS_PAGE_FAULT;
364                                         }
365                                 } else if (prot_fault_translation == 1) {
366                                         /*
367                                          * Always compat mode.
368                                          */
369                                         i = SIGBUS;
370                                         ucode = BUS_PAGE_FAULT;
371                                 } else {
372                                         /*
373                                          * Always SIGSEGV mode.
374                                          */
375                                         i = SIGSEGV;
376                                         ucode = SEGV_ACCERR;
377                                 }
378                         }
379                         break;
380 
381                 case T_DIVIDE:          /* integer divide fault */
382                         ucode = FPE_INTDIV;
383                         i = SIGFPE;
384                         break;
385 
386 #ifdef DEV_ISA
387                 case T_NMI:
388                         /* machine/parity/power fail/"kitchen sink" faults */
389                         /* XXX Giant */
390                         if (isa_nmi(code) == 0) {
391 #ifdef KDB
392                                 /*
393                                  * NMI can be hooked up to a pushbutton
394                                  * for debugging.
395                                  */
396                                 if (kdb_on_nmi) {
397                                         printf ("NMI ... going to debugger\n");
398                                         kdb_trap(type, 0, frame);
399                                 }
400 #endif /* KDB */
401                                 goto userout;
402                         } else if (panic_on_nmi)
403                                 panic("NMI indicates hardware failure");
404                         break;
405 #endif /* DEV_ISA */
406 
407                 case T_OFLOW:           /* integer overflow fault */
408                         ucode = FPE_INTOVF;
409                         i = SIGFPE;
410                         break;
411 
412                 case T_BOUND:           /* bounds check fault */
413                         ucode = FPE_FLTSUB;
414                         i = SIGFPE;
415                         break;
416 
417                 case T_DNA:
418                         /* transparent fault (due to context switch "late") */
419                         if (fpudna())
420                                 goto userout;
421                         printf("pid %d killed due to lack of floating point\n",
422                                 p->p_pid);
423                         i = SIGKILL;
424                         ucode = 0;
425                         break;
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                         if (fpudna()) {
454                                 printf("fpudna in kernel mode!\n");
455                                 goto out;
456                         }
457                         break;
458 
459                 case T_STKFLT:          /* stack fault */
460                         break;
461 
462                 case T_PROTFLT:         /* general protection fault */
463                 case T_SEGNPFLT:        /* segment not present fault */
464                         if (td->td_intr_nesting_level != 0)
465                                 break;
466 
467                         /*
468                          * Invalid segment selectors and out of bounds
469                          * %rip's and %rsp's can be set up in user mode.
470                          * This causes a fault in kernel mode when the
471                          * kernel tries to return to user mode.  We want
472                          * to get this fault so that we can fix the
473                          * problem here and not have to check all the
474                          * selectors and pointers when the user changes
475                          * them.
476                          */
477                         if (frame->tf_rip == (long)doreti_iret) {
478                                 frame->tf_rip = (long)doreti_iret_fault;
479                                 goto out;
480                         }
481                         if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
482                                 frame->tf_rip =
483                                     (long)PCPU_GET(curpcb)->pcb_onfault;
484                                 goto out;
485                         }
486                         break;
487 
488                 case T_TSSFLT:
489                         /*
490                          * PSL_NT can be set in user mode and isn't cleared
491                          * automatically when the kernel is entered.  This
492                          * causes a TSS fault when the kernel attempts to
493                          * `iret' because the TSS link is uninitialized.  We
494                          * want to get this fault so that we can fix the
495                          * problem here and not every time the kernel is
496                          * entered.
497                          */
498                         if (frame->tf_rflags & PSL_NT) {
499                                 frame->tf_rflags &= ~PSL_NT;
500                                 goto out;
501                         }
502                         break;
503 
504                 case T_TRCTRAP:  /* trace trap */
505                         /*
506                          * Ignore debug register trace traps due to
507                          * accesses in the user's address space, which
508                          * can happen under several conditions such as
509                          * if a user sets a watchpoint on a buffer and
510                          * then passes that buffer to a system call.
511                          * We still want to get TRCTRAPS for addresses
512                          * in kernel space because that is useful when
513                          * debugging the kernel.
514                          */
515                         if (user_dbreg_trap()) {
516                                 /*
517                                  * Reset breakpoint bits because the
518                                  * processor doesn't
519                                  */
520                                 /* XXX check upper bits here */
521                                 load_dr6(rdr6() & 0xfffffff0);
522                                 goto out;
523                         }
524                         /*
525                          * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
526                          */
527                 case T_BPTFLT:
528                         /*
529                          * If KDB is enabled, let it handle the debugger trap.
530                          * Otherwise, debugger traps "can't happen".
531                          */
532 #ifdef KDB
533                         if (kdb_trap(type, 0, frame))
534                                 goto out;
535 #endif
536                         break;
537 
538 #ifdef DEV_ISA
539                 case T_NMI:
540                         /* XXX Giant */
541                         /* machine/parity/power fail/"kitchen sink" faults */
542                         if (isa_nmi(code) == 0) {
543 #ifdef KDB
544                                 /*
545                                  * NMI can be hooked up to a pushbutton
546                                  * for debugging.
547                                  */
548                                 if (kdb_on_nmi) {
549                                         printf ("NMI ... going to debugger\n");
550                                         kdb_trap(type, 0, frame);
551                                 }
552 #endif /* KDB */
553                                 goto out;
554                         } else if (panic_on_nmi == 0)
555                                 goto out;
556                         /* FALLTHROUGH */
557 #endif /* DEV_ISA */
558                 }
559 
560                 trap_fatal(frame, 0);
561                 goto out;
562         }
563 
564         /* Translate fault for emulators (e.g. Linux) */
565         if (*p->p_sysent->sv_transtrap)
566                 i = (*p->p_sysent->sv_transtrap)(i, type);
567 
568         ksiginfo_init_trap(&ksi);
569         ksi.ksi_signo = i;
570         ksi.ksi_code = ucode;
571         ksi.ksi_trapno = type;
572         ksi.ksi_addr = (void *)addr;
573         trapsignal(td, &ksi);
574 
575 #ifdef DEBUG
576         if (type <= MAX_TRAP_MSG) {
577                 uprintf("fatal process exception: %s",
578                         trap_msg[type]);
579                 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
580                         uprintf(", fault VA = 0x%lx", frame->tf_addr);
581                 uprintf("\n");
582         }
583 #endif
584 
585 user:
586         userret(td, frame);
587         mtx_assert(&Giant, MA_NOTOWNED);
588 userout:
589 out:
590         return;
591 }
592 
593 static int
594 trap_pfault(frame, usermode)
595         struct trapframe *frame;
596         int usermode;
597 {
598         vm_offset_t va;
599         struct vmspace *vm = NULL;
600         vm_map_t map;
601         int rv = 0;
602         vm_prot_t ftype;
603         struct thread *td = curthread;
604         struct proc *p = td->td_proc;
605         vm_offset_t eva = frame->tf_addr;
606 
607         va = trunc_page(eva);
608         if (va >= VM_MIN_KERNEL_ADDRESS) {
609                 /*
610                  * Don't allow user-mode faults in kernel address space.
611                  */
612                 if (usermode)
613                         goto nogo;
614 
615                 map = kernel_map;
616         } else {
617                 /*
618                  * This is a fault on non-kernel virtual memory.
619                  * vm is initialized above to NULL. If curproc is NULL
620                  * or curproc->p_vmspace is NULL the fault is fatal.
621                  */
622                 if (p != NULL)
623                         vm = p->p_vmspace;
624 
625                 if (vm == NULL)
626                         goto nogo;
627 
628                 map = &vm->vm_map;
629         }
630 
631         /*
632          * PGEX_I is defined only if the execute disable bit capability is
633          * supported and enabled.
634          */
635         if (frame->tf_err & PGEX_W)
636                 ftype = VM_PROT_WRITE;
637         else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
638                 ftype = VM_PROT_EXECUTE;
639         else
640                 ftype = VM_PROT_READ;
641 
642         if (map != kernel_map) {
643                 /*
644                  * Keep swapout from messing with us during this
645                  *      critical time.
646                  */
647                 PROC_LOCK(p);
648                 ++p->p_lock;
649                 PROC_UNLOCK(p);
650 
651                 /* Fault in the user page: */
652                 rv = vm_fault(map, va, ftype,
653                               (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
654                                                       : VM_FAULT_NORMAL);
655 
656                 PROC_LOCK(p);
657                 --p->p_lock;
658                 PROC_UNLOCK(p);
659         } else {
660                 /*
661                  * Don't have to worry about process locking or stacks in the
662                  * kernel.
663                  */
664                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
665         }
666         if (rv == KERN_SUCCESS)
667                 return (0);
668 nogo:
669         if (!usermode) {
670                 if (td->td_intr_nesting_level == 0 &&
671                     PCPU_GET(curpcb)->pcb_onfault != NULL) {
672                         frame->tf_rip = (long)PCPU_GET(curpcb)->pcb_onfault;
673                         return (0);
674                 }
675                 trap_fatal(frame, eva);
676                 return (-1);
677         }
678 
679         return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
680 }
681 
682 static void
683 trap_fatal(frame, eva)
684         struct trapframe *frame;
685         vm_offset_t eva;
686 {
687         int code, ss;
688         u_int type;
689         long esp;
690         struct soft_segment_descriptor softseg;
691         char *msg;
692 
693         code = frame->tf_err;
694         type = frame->tf_trapno;
695         sdtossd(&gdt[NGDT * PCPU_GET(cpuid) + IDXSEL(frame->tf_cs & 0xffff)],
696             &softseg);
697 
698         if (type <= MAX_TRAP_MSG)
699                 msg = trap_msg[type];
700         else
701                 msg = "UNKNOWN";
702         printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
703             ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
704 #ifdef SMP
705         /* two separate prints in case of a trap on an unmapped page */
706         printf("cpuid = %d; ", PCPU_GET(cpuid));
707         printf("apic id = %02x\n", PCPU_GET(apic_id));
708 #endif
709         if (type == T_PAGEFLT) {
710                 printf("fault virtual address   = 0x%lx\n", eva);
711                 printf("fault code              = %s %s %s, %s\n",
712                         code & PGEX_U ? "user" : "supervisor",
713                         code & PGEX_W ? "write" : "read",
714                         code & PGEX_I ? "instruction" : "data",
715                         code & PGEX_P ? "protection violation" : "page not present");
716         }
717         printf("instruction pointer     = 0x%lx:0x%lx\n",
718                frame->tf_cs & 0xffff, frame->tf_rip);
719         if (ISPL(frame->tf_cs) == SEL_UPL) {
720                 ss = frame->tf_ss & 0xffff;
721                 esp = frame->tf_rsp;
722         } else {
723                 ss = GSEL(GDATA_SEL, SEL_KPL);
724                 esp = (long)&frame->tf_rsp;
725         }
726         printf("stack pointer           = 0x%x:0x%lx\n", ss, esp);
727         printf("frame pointer           = 0x%x:0x%lx\n", ss, frame->tf_rbp);
728         printf("code segment            = base 0x%lx, limit 0x%lx, type 0x%x\n",
729                softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
730         printf("                        = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
731                softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
732                softseg.ssd_gran);
733         printf("processor eflags        = ");
734         if (frame->tf_rflags & PSL_T)
735                 printf("trace trap, ");
736         if (frame->tf_rflags & PSL_I)
737                 printf("interrupt enabled, ");
738         if (frame->tf_rflags & PSL_NT)
739                 printf("nested task, ");
740         if (frame->tf_rflags & PSL_RF)
741                 printf("resume, ");
742         printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
743         printf("current process         = ");
744         if (curproc) {
745                 printf("%lu (%s)\n",
746                     (u_long)curproc->p_pid, curthread->td_name ?
747                     curthread->td_name : "");
748         } else {
749                 printf("Idle\n");
750         }
751 
752 #ifdef KDB
753         if (debugger_on_panic || kdb_active)
754                 if (kdb_trap(type, 0, frame))
755                         return;
756 #endif
757         printf("trap number             = %d\n", type);
758         if (type <= MAX_TRAP_MSG)
759                 panic("%s", trap_msg[type]);
760         else
761                 panic("unknown/reserved trap");
762 }
763 
764 /*
765  * Double fault handler. Called when a fault occurs while writing
766  * a frame for a trap/exception onto the stack. This usually occurs
767  * when the stack overflows (such is the case with infinite recursion,
768  * for example).
769  */
770 void
771 dblfault_handler(struct trapframe *frame)
772 {
773 #ifdef KDTRACE_HOOKS
774         if (dtrace_doubletrap_func != NULL)
775                 (*dtrace_doubletrap_func)();
776 #endif
777         printf("\nFatal double fault\n");
778         printf("rip = 0x%lx\n", frame->tf_rip);
779         printf("rsp = 0x%lx\n", frame->tf_rsp);
780         printf("rbp = 0x%lx\n", frame->tf_rbp);
781 #ifdef SMP
782         /* two separate prints in case of a trap on an unmapped page */
783         printf("cpuid = %d; ", PCPU_GET(cpuid));
784         printf("apic id = %02x\n", PCPU_GET(apic_id));
785 #endif
786         panic("double fault");
787 }
788 
789 /*
790  *      syscall -       system call request C handler
791  *
792  *      A system call is essentially treated as a trap.
793  */
794 void
795 syscall(struct trapframe *frame)
796 {
797         caddr_t params;
798         struct sysent *callp;
799         struct thread *td = curthread;
800         struct proc *p = td->td_proc;
801         register_t orig_tf_rflags;
802         int error;
803         int narg;
804         register_t args[8];
805         register_t *argp;
806         u_int code;
807         int reg, regcnt;
808         ksiginfo_t ksi;
809 
810         PCPU_INC(cnt.v_syscall);
811 
812 #ifdef DIAGNOSTIC
813         if (ISPL(frame->tf_cs) != SEL_UPL) {
814                 panic("syscall");
815                 /* NOT REACHED */
816         }
817 #endif
818 
819         reg = 0;
820         regcnt = 6;
821         td->td_pticks = 0;
822         td->td_frame = frame;
823         if (td->td_ucred != p->p_ucred) 
824                 cred_update_thread(td);
825         params = (caddr_t)frame->tf_rsp + sizeof(register_t);
826         code = frame->tf_rax;
827         orig_tf_rflags = frame->tf_rflags;
828 
829         if (p->p_sysent->sv_prepsyscall) {
830                 /*
831                  * The prep code is MP aware.
832                  */
833                 (*p->p_sysent->sv_prepsyscall)(frame, (int *)args, &code, &params);
834         } else {
835                 if (code == SYS_syscall || code == SYS___syscall) {
836                         code = frame->tf_rdi;
837                         reg++;
838                         regcnt--;
839                 }
840         }
841 
842         if (p->p_sysent->sv_mask)
843                 code &= p->p_sysent->sv_mask;
844 
845         if (code >= p->p_sysent->sv_size)
846                 callp = &p->p_sysent->sv_table[0];
847         else
848                 callp = &p->p_sysent->sv_table[code];
849 
850         narg = callp->sy_narg;
851 
852         /*
853          * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
854          */
855         KASSERT(narg <= sizeof(args) / sizeof(args[0]),
856             ("Too many syscall arguments!"));
857         error = 0;
858         argp = &frame->tf_rdi;
859         argp += reg;
860         bcopy(argp, args, sizeof(args[0]) * regcnt);
861         if (narg > regcnt) {
862                 KASSERT(params != NULL, ("copyin args with no params!"));
863                 error = copyin(params, &args[regcnt],
864                         (narg - regcnt) * sizeof(args[0]));
865         }
866         argp = &args[0];
867 
868 #ifdef KTRACE
869         if (KTRPOINT(td, KTR_SYSCALL))
870                 ktrsyscall(code, narg, argp);
871 #endif
872 
873         CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
874             td->td_proc->p_pid, td->td_name, code);
875 
876         td->td_syscalls++;
877 
878         if (error == 0) {
879                 td->td_retval[0] = 0;
880                 td->td_retval[1] = frame->tf_rdx;
881 
882                 STOPEVENT(p, S_SCE, narg);
883 
884                 PTRACESTOP_SC(p, td, S_PT_SCE);
885 
886 #ifdef KDTRACE_HOOKS
887                 /*
888                  * If the systrace module has registered it's probe
889                  * callback and if there is a probe active for the
890                  * syscall 'entry', process the probe.
891                  */
892                 if (systrace_probe_func != NULL && callp->sy_entry != 0)
893                         (*systrace_probe_func)(callp->sy_entry, code, callp,
894                             args);
895 #endif
896 
897                 AUDIT_SYSCALL_ENTER(code, td);
898                 error = (*callp->sy_call)(td, argp);
899                 AUDIT_SYSCALL_EXIT(error, td);
900 
901                 /* Save the latest error return value. */
902                 td->td_errno = error;
903 
904 #ifdef KDTRACE_HOOKS
905                 /*
906                  * If the systrace module has registered it's probe
907                  * callback and if there is a probe active for the
908                  * syscall 'return', process the probe.
909                  */
910                 if (systrace_probe_func != NULL && callp->sy_return != 0)
911                         (*systrace_probe_func)(callp->sy_return, code, callp,
912                             args);
913 #endif
914