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

Cache object: aa78912c590cba279b7337e2e9d0a16a


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