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

Cache object: 8167abb34c2cc86935f223ff72b59dc1


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