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

Cache object: 802728754f5f4dfc3816b8d1bac24755


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