The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ 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  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  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  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: a19e8c5fe0e2a436aba404841bb51a1e


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


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.