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

Cache object: e0830183e2ff11b7b814a910c42747d2


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