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

Cache object: 649774dba5ec0947bd10f925608562fb


[ 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.