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/i386/i386/trap.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (C) 1994, David Greenman
    3  * Copyright (c) 1990, 1993
    4  *      The Regents of the University of California.  All rights reserved.
    5  *
    6  * This code is derived from software contributed to Berkeley by
    7  * the University of Utah, and William Jolitz.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  * 3. All advertising materials mentioning features or use of this software
   18  *    must display the following acknowledgement:
   19  *      This product includes software developed by the University of
   20  *      California, Berkeley and its contributors.
   21  * 4. Neither the name of the University nor the names of its contributors
   22  *    may be used to endorse or promote products derived from this software
   23  *    without specific prior written permission.
   24  *
   25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   35  * SUCH DAMAGE.
   36  *
   37  *      from: @(#)trap.c        7.4 (Berkeley) 5/13/91
   38  */
   39 
   40 #include <sys/cdefs.h>
   41 __FBSDID("$FreeBSD: releng/8.4/sys/i386/i386/trap.c 235517 2012-05-16 20:05:31Z jhb $");
   42 
   43 /*
   44  * 386 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 #include "opt_npx.h"
   55 #include "opt_trap.h"
   56 
   57 #include <sys/param.h>
   58 #include <sys/bus.h>
   59 #include <sys/systm.h>
   60 #include <sys/proc.h>
   61 #include <sys/pioctl.h>
   62 #include <sys/ptrace.h>
   63 #include <sys/kdb.h>
   64 #include <sys/kernel.h>
   65 #include <sys/ktr.h>
   66 #include <sys/lock.h>
   67 #include <sys/mutex.h>
   68 #include <sys/resourcevar.h>
   69 #include <sys/signalvar.h>
   70 #include <sys/syscall.h>
   71 #include <sys/sysctl.h>
   72 #include <sys/sysent.h>
   73 #include <sys/uio.h>
   74 #include <sys/vmmeter.h>
   75 #ifdef KTRACE
   76 #include <sys/ktrace.h>
   77 #endif
   78 #ifdef HWPMC_HOOKS
   79 #include <sys/pmckern.h>
   80 #endif
   81 #include <security/audit/audit.h>
   82 
   83 #include <vm/vm.h>
   84 #include <vm/vm_param.h>
   85 #include <vm/pmap.h>
   86 #include <vm/vm_kern.h>
   87 #include <vm/vm_map.h>
   88 #include <vm/vm_page.h>
   89 #include <vm/vm_extern.h>
   90 
   91 #include <machine/cpu.h>
   92 #include <machine/intr_machdep.h>
   93 #include <machine/mca.h>
   94 #include <machine/md_var.h>
   95 #include <machine/pcb.h>
   96 #ifdef SMP
   97 #include <machine/smp.h>
   98 #endif
   99 #include <machine/tss.h>
  100 #include <machine/vm86.h>
  101 
  102 #ifdef POWERFAIL_NMI
  103 #include <sys/syslog.h>
  104 #include <machine/clock.h>
  105 #endif
  106 
  107 #ifdef KDTRACE_HOOKS
  108 #include <sys/dtrace_bsd.h>
  109 
  110 /*
  111  * This is a hook which is initialised by the dtrace module
  112  * to handle traps which might occur during DTrace probe
  113  * execution.
  114  */
  115 dtrace_trap_func_t      dtrace_trap_func;
  116 
  117 dtrace_doubletrap_func_t        dtrace_doubletrap_func;
  118 
  119 /*
  120  * This is a hook which is initialised by the systrace module
  121  * when it is loaded. This keeps the DTrace syscall provider
  122  * implementation opaque. 
  123  */
  124 systrace_probe_func_t   systrace_probe_func;
  125 
  126 /*
  127  * These hooks are necessary for the pid, usdt and fasttrap providers.
  128  */
  129 dtrace_fasttrap_probe_ptr_t     dtrace_fasttrap_probe_ptr;
  130 dtrace_pid_probe_ptr_t          dtrace_pid_probe_ptr;
  131 dtrace_return_probe_ptr_t       dtrace_return_probe_ptr;
  132 #endif
  133 
  134 extern void trap(struct trapframe *frame);
  135 extern void syscall(struct trapframe *frame);
  136 
  137 static int trap_pfault(struct trapframe *, int, vm_offset_t);
  138 static void trap_fatal(struct trapframe *, vm_offset_t);
  139 void dblfault_handler(void);
  140 
  141 extern inthand_t IDTVEC(lcall_syscall);
  142 
  143 #define MAX_TRAP_MSG            33
  144 static char *trap_msg[] = {
  145         "",                                     /*  0 unused */
  146         "privileged instruction fault",         /*  1 T_PRIVINFLT */
  147         "",                                     /*  2 unused */
  148         "breakpoint instruction fault",         /*  3 T_BPTFLT */
  149         "",                                     /*  4 unused */
  150         "",                                     /*  5 unused */
  151         "arithmetic trap",                      /*  6 T_ARITHTRAP */
  152         "",                                     /*  7 unused */
  153         "",                                     /*  8 unused */
  154         "general protection fault",             /*  9 T_PROTFLT */
  155         "trace trap",                           /* 10 T_TRCTRAP */
  156         "",                                     /* 11 unused */
  157         "page fault",                           /* 12 T_PAGEFLT */
  158         "",                                     /* 13 unused */
  159         "alignment fault",                      /* 14 T_ALIGNFLT */
  160         "",                                     /* 15 unused */
  161         "",                                     /* 16 unused */
  162         "",                                     /* 17 unused */
  163         "integer divide fault",                 /* 18 T_DIVIDE */
  164         "non-maskable interrupt trap",          /* 19 T_NMI */
  165         "overflow trap",                        /* 20 T_OFLOW */
  166         "FPU bounds check fault",               /* 21 T_BOUND */
  167         "FPU device not available",             /* 22 T_DNA */
  168         "double fault",                         /* 23 T_DOUBLEFLT */
  169         "FPU operand fetch fault",              /* 24 T_FPOPFLT */
  170         "invalid TSS fault",                    /* 25 T_TSSFLT */
  171         "segment not present fault",            /* 26 T_SEGNPFLT */
  172         "stack fault",                          /* 27 T_STKFLT */
  173         "machine check trap",                   /* 28 T_MCHK */
  174         "SIMD floating-point exception",        /* 29 T_XMMFLT */
  175         "reserved (unknown) fault",             /* 30 T_RESERVED */
  176         "",                                     /* 31 unused (reserved) */
  177         "DTrace pid return trap",               /* 32 T_DTRACE_RET */
  178         "DTrace fasttrap probe trap",           /* 33 T_DTRACE_PROBE */
  179 
  180 };
  181 
  182 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
  183 extern int has_f00f_bug;
  184 #endif
  185 
  186 #ifdef KDB
  187 static int kdb_on_nmi = 1;
  188 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
  189         &kdb_on_nmi, 0, "Go to KDB on NMI");
  190 TUNABLE_INT("machdep.kdb_on_nmi", &kdb_on_nmi);
  191 #endif
  192 static int panic_on_nmi = 1;
  193 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
  194         &panic_on_nmi, 0, "Panic on NMI");
  195 TUNABLE_INT("machdep.panic_on_nmi", &panic_on_nmi);
  196 static int prot_fault_translation = 0;
  197 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
  198         &prot_fault_translation, 0, "Select signal to deliver on protection fault");
  199 
  200 /*
  201  * Exception, fault, and trap interface to the FreeBSD kernel.
  202  * This common code is called from assembly language IDT gate entry
  203  * routines that prepare a suitable stack frame, and restore this
  204  * frame after the exception has been processed.
  205  */
  206 
  207 void
  208 trap(struct trapframe *frame)
  209 {
  210         struct thread *td = curthread;
  211         struct proc *p = td->td_proc;
  212         int i = 0, ucode = 0, code;
  213         u_int type;
  214         register_t addr = 0;
  215         vm_offset_t eva;
  216         ksiginfo_t ksi;
  217 #ifdef POWERFAIL_NMI
  218         static int lastalert = 0;
  219 #endif
  220 
  221         PCPU_INC(cnt.v_trap);
  222         type = frame->tf_trapno;
  223 
  224 #ifdef SMP
  225         /* Handler for NMI IPIs used for stopping CPUs. */
  226         if (type == T_NMI) {
  227                  if (ipi_nmi_handler() == 0)
  228                            goto out;
  229         }
  230 #endif /* SMP */
  231 
  232 #ifdef KDB
  233         if (kdb_active) {
  234                 kdb_reenter();
  235                 goto out;
  236         }
  237 #endif
  238 
  239         if (type == T_RESERVED) {
  240                 trap_fatal(frame, 0);
  241                 goto out;
  242         }
  243 
  244 #ifdef  HWPMC_HOOKS
  245         /*
  246          * CPU PMCs interrupt using an NMI so we check for that first.
  247          * If the HWPMC module is active, 'pmc_hook' will point to
  248          * the function to be called.  A return value of '1' from the
  249          * hook means that the NMI was handled by it and that we can
  250          * return immediately.
  251          */
  252         if (type == T_NMI && pmc_intr &&
  253             (*pmc_intr)(PCPU_GET(cpuid), frame))
  254             goto out;
  255 #endif
  256 
  257         if (type == T_MCHK) {
  258                 mca_intr();
  259                 goto out;
  260         }
  261 
  262 #ifdef KDTRACE_HOOKS
  263         /*
  264          * A trap can occur while DTrace executes a probe. Before
  265          * executing the probe, DTrace blocks re-scheduling and sets
  266          * a flag in it's per-cpu flags to indicate that it doesn't
  267          * want to fault. On returning from the probe, the no-fault
  268          * flag is cleared and finally re-scheduling is enabled.
  269          *
  270          * If the DTrace kernel module has registered a trap handler,
  271          * call it and if it returns non-zero, assume that it has
  272          * handled the trap and modified the trap frame so that this
  273          * function can return normally.
  274          */
  275         if (type == T_DTRACE_PROBE || type == T_DTRACE_RET ||
  276             type == T_BPTFLT) {
  277                 struct reg regs;
  278 
  279                 fill_frame_regs(frame, &regs);
  280                 if (type == T_DTRACE_PROBE &&
  281                     dtrace_fasttrap_probe_ptr != NULL &&
  282                     dtrace_fasttrap_probe_ptr(&regs) == 0)
  283                         goto out;
  284                 if (type == T_BPTFLT &&
  285                     dtrace_pid_probe_ptr != NULL &&
  286                     dtrace_pid_probe_ptr(&regs) == 0)
  287                         goto out;
  288                 if (type == T_DTRACE_RET &&
  289                     dtrace_return_probe_ptr != NULL &&
  290                     dtrace_return_probe_ptr(&regs) == 0)
  291                         goto out;
  292         }
  293         if ((type == T_PROTFLT || type == T_PAGEFLT) &&
  294             dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
  295                 goto out;
  296 #endif
  297 
  298         if ((frame->tf_eflags & PSL_I) == 0) {
  299                 /*
  300                  * Buggy application or kernel code has disabled
  301                  * interrupts and then trapped.  Enabling interrupts
  302                  * now is wrong, but it is better than running with
  303                  * interrupts disabled until they are accidentally
  304                  * enabled later.
  305                  */
  306                 if (ISPL(frame->tf_cs) == SEL_UPL || (frame->tf_eflags & PSL_VM))
  307                         uprintf(
  308                             "pid %ld (%s): trap %d with interrupts disabled\n",
  309                             (long)curproc->p_pid, curthread->td_name, type);
  310                 else if (type != T_BPTFLT && type != T_TRCTRAP &&
  311                          frame->tf_eip != (int)cpu_switch_load_gs) {
  312                         /*
  313                          * XXX not quite right, since this may be for a
  314                          * multiple fault in user mode.
  315                          */
  316                         printf("kernel trap %d with interrupts disabled\n",
  317                             type);
  318                         /*
  319                          * Page faults need interrupts disabled until later,
  320                          * and we shouldn't enable interrupts while holding
  321                          * a spin lock or if servicing an NMI.
  322                          */
  323                         if (type != T_NMI && type != T_PAGEFLT &&
  324                             td->td_md.md_spinlock_count == 0)
  325                                 enable_intr();
  326                 }
  327         }
  328         eva = 0;
  329         code = frame->tf_err;
  330         if (type == T_PAGEFLT) {
  331                 /*
  332                  * For some Cyrix CPUs, %cr2 is clobbered by
  333                  * interrupts.  This problem is worked around by using
  334                  * an interrupt gate for the pagefault handler.  We
  335                  * are finally ready to read %cr2 and conditionally
  336                  * reenable interrupts.  If we hold a spin lock, then
  337                  * we must not reenable interrupts.  This might be a
  338                  * spurious page fault.
  339                  */
  340                 eva = rcr2();
  341                 if (td->td_md.md_spinlock_count == 0)
  342                         enable_intr();
  343         }
  344 
  345         if ((ISPL(frame->tf_cs) == SEL_UPL) ||
  346             ((frame->tf_eflags & PSL_VM) && 
  347                 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL))) {
  348                 /* user trap */
  349 
  350                 td->td_pticks = 0;
  351                 td->td_frame = frame;
  352                 addr = frame->tf_eip;
  353                 if (td->td_ucred != p->p_ucred) 
  354                         cred_update_thread(td);
  355 
  356                 switch (type) {
  357                 case T_PRIVINFLT:       /* privileged instruction fault */
  358                         i = SIGILL;
  359                         ucode = ILL_PRVOPC;
  360                         break;
  361 
  362                 case T_BPTFLT:          /* bpt instruction fault */
  363                 case T_TRCTRAP:         /* trace trap */
  364                         enable_intr();
  365                         frame->tf_eflags &= ~PSL_T;
  366                         i = SIGTRAP;
  367                         ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
  368                         break;
  369 
  370                 case T_ARITHTRAP:       /* arithmetic trap */
  371 #ifdef DEV_NPX
  372                         ucode = npxtrap();
  373                         if (ucode == -1)
  374                                 goto userout;
  375 #else
  376                         ucode = 0;
  377 #endif
  378                         i = SIGFPE;
  379                         break;
  380 
  381                         /*
  382                          * The following two traps can happen in
  383                          * vm86 mode, and, if so, we want to handle
  384                          * them specially.
  385                          */
  386                 case T_PROTFLT:         /* general protection fault */
  387                 case T_STKFLT:          /* stack fault */
  388                         if (frame->tf_eflags & PSL_VM) {
  389                                 i = vm86_emulate((struct vm86frame *)frame);
  390                                 if (i == 0)
  391                                         goto user;
  392                                 break;
  393                         }
  394                         i = SIGBUS;
  395                         ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
  396                         break;
  397                 case T_SEGNPFLT:        /* segment not present fault */
  398                         i = SIGBUS;
  399                         ucode = BUS_ADRERR;
  400                         break;
  401                 case T_TSSFLT:          /* invalid TSS fault */
  402                         i = SIGBUS;
  403                         ucode = BUS_OBJERR;
  404                         break;
  405                 case T_DOUBLEFLT:       /* double fault */
  406                 default:
  407                         i = SIGBUS;
  408                         ucode = BUS_OBJERR;
  409                         break;
  410 
  411                 case T_PAGEFLT:         /* page fault */
  412 
  413                         i = trap_pfault(frame, TRUE, eva);
  414 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
  415                         if (i == -2) {
  416                                 /*
  417                                  * The f00f hack workaround has triggered, so
  418                                  * treat the fault as an illegal instruction 
  419                                  * (T_PRIVINFLT) instead of a page fault.
  420                                  */
  421                                 type = frame->tf_trapno = T_PRIVINFLT;
  422 
  423                                 /* Proceed as in that case. */
  424                                 ucode = ILL_PRVOPC;
  425                                 i = SIGILL;
  426                                 break;
  427                         }
  428 #endif
  429                         if (i == -1)
  430                                 goto userout;
  431                         if (i == 0)
  432                                 goto user;
  433 
  434                         if (i == SIGSEGV)
  435                                 ucode = SEGV_MAPERR;
  436                         else {
  437                                 if (prot_fault_translation == 0) {
  438                                         /*
  439                                          * Autodetect.
  440                                          * This check also covers the images
  441                                          * without the ABI-tag ELF note.
  442                                          */
  443                                         if (SV_CURPROC_ABI() == SV_ABI_FREEBSD
  444                                             && p->p_osrel >= P_OSREL_SIGSEGV) {
  445                                                 i = SIGSEGV;
  446                                                 ucode = SEGV_ACCERR;
  447                                         } else {
  448                                                 i = SIGBUS;
  449                                                 ucode = BUS_PAGE_FAULT;
  450                                         }
  451                                 } else if (prot_fault_translation == 1) {
  452                                         /*
  453                                          * Always compat mode.
  454                                          */
  455                                         i = SIGBUS;
  456                                         ucode = BUS_PAGE_FAULT;
  457                                 } else {
  458                                         /*
  459                                          * Always SIGSEGV mode.
  460                                          */
  461                                         i = SIGSEGV;
  462                                         ucode = SEGV_ACCERR;
  463                                 }
  464                         }
  465                         addr = eva;
  466                         break;
  467 
  468                 case T_DIVIDE:          /* integer divide fault */
  469                         ucode = FPE_INTDIV;
  470                         i = SIGFPE;
  471                         break;
  472 
  473 #ifdef DEV_ISA
  474                 case T_NMI:
  475 #ifdef POWERFAIL_NMI
  476 #ifndef TIMER_FREQ
  477 #  define TIMER_FREQ 1193182
  478 #endif
  479                         if (time_second - lastalert > 10) {
  480                                 log(LOG_WARNING, "NMI: power fail\n");
  481                                 sysbeep(880, hz);
  482                                 lastalert = time_second;
  483                         }
  484                         goto userout;
  485 #else /* !POWERFAIL_NMI */
  486                         /* machine/parity/power fail/"kitchen sink" faults */
  487                         if (isa_nmi(code) == 0) {
  488 #ifdef KDB
  489                                 /*
  490                                  * NMI can be hooked up to a pushbutton
  491                                  * for debugging.
  492                                  */
  493                                 if (kdb_on_nmi) {
  494                                         printf ("NMI ... going to debugger\n");
  495                                         kdb_trap(type, 0, frame);
  496                                 }
  497 #endif /* KDB */
  498                                 goto userout;
  499                         } else if (panic_on_nmi)
  500                                 panic("NMI indicates hardware failure");
  501                         break;
  502 #endif /* POWERFAIL_NMI */
  503 #endif /* DEV_ISA */
  504 
  505                 case T_OFLOW:           /* integer overflow fault */
  506                         ucode = FPE_INTOVF;
  507                         i = SIGFPE;
  508                         break;
  509 
  510                 case T_BOUND:           /* bounds check fault */
  511                         ucode = FPE_FLTSUB;
  512                         i = SIGFPE;
  513                         break;
  514 
  515                 case T_DNA:
  516 #ifdef DEV_NPX
  517                         KASSERT(PCB_USER_FPU(td->td_pcb),
  518                             ("kernel FPU ctx has leaked"));
  519                         /* transparent fault (due to context switch "late") */
  520                         if (npxdna())
  521                                 goto userout;
  522 #endif
  523                         uprintf("pid %d killed due to lack of floating point\n",
  524                                 p->p_pid);
  525                         i = SIGKILL;
  526                         ucode = 0;
  527                         break;
  528 
  529                 case T_FPOPFLT:         /* FPU operand fetch fault */
  530                         ucode = ILL_COPROC;
  531                         i = SIGILL;
  532                         break;
  533 
  534                 case T_XMMFLT:          /* SIMD floating-point exception */
  535                         ucode = 0; /* XXX */
  536                         i = SIGFPE;
  537                         break;
  538                 }
  539         } else {
  540                 /* kernel trap */
  541 
  542                 KASSERT(cold || td->td_ucred != NULL,
  543                     ("kernel trap doesn't have ucred"));
  544                 switch (type) {
  545                 case T_PAGEFLT:                 /* page fault */
  546                         (void) trap_pfault(frame, FALSE, eva);
  547                         goto out;
  548 
  549                 case T_DNA:
  550 #ifdef DEV_NPX
  551                         KASSERT(!PCB_USER_FPU(td->td_pcb),
  552                             ("Unregistered use of FPU in kernel"));
  553                         if (npxdna())
  554                                 goto out;
  555 #endif
  556                         break;
  557 
  558                 case T_ARITHTRAP:       /* arithmetic trap */
  559                 case T_XMMFLT:          /* SIMD floating-point exception */
  560                 case T_FPOPFLT:         /* FPU operand fetch fault */
  561                         /*
  562                          * XXXKIB for now disable any FPU traps in kernel
  563                          * handler registration seems to be overkill
  564                          */
  565                         trap_fatal(frame, 0);
  566                         goto out;
  567 
  568                         /*
  569                          * The following two traps can happen in
  570                          * vm86 mode, and, if so, we want to handle
  571                          * them specially.
  572                          */
  573                 case T_PROTFLT:         /* general protection fault */
  574                 case T_STKFLT:          /* stack fault */
  575                         if (frame->tf_eflags & PSL_VM) {
  576                                 i = vm86_emulate((struct vm86frame *)frame);
  577                                 if (i != 0)
  578                                         /*
  579                                          * returns to original process
  580                                          */
  581                                         vm86_trap((struct vm86frame *)frame);
  582                                 goto out;
  583                         }
  584                         if (type == T_STKFLT)
  585                                 break;
  586 
  587                         /* FALL THROUGH */
  588 
  589                 case T_SEGNPFLT:        /* segment not present fault */
  590                         if (PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)
  591                                 break;
  592 
  593                         /*
  594                          * Invalid %fs's and %gs's can be created using
  595                          * procfs or PT_SETREGS or by invalidating the
  596                          * underlying LDT entry.  This causes a fault
  597                          * in kernel mode when the kernel attempts to
  598                          * switch contexts.  Lose the bad context
  599                          * (XXX) so that we can continue, and generate
  600                          * a signal.
  601                          */
  602                         if (frame->tf_eip == (int)cpu_switch_load_gs) {
  603                                 PCPU_GET(curpcb)->pcb_gs = 0;
  604 #if 0                           
  605                                 PROC_LOCK(p);
  606                                 psignal(p, SIGBUS);
  607                                 PROC_UNLOCK(p);
  608 #endif                          
  609                                 goto out;
  610                         }
  611 
  612                         if (td->td_intr_nesting_level != 0)
  613                                 break;
  614 
  615                         /*
  616                          * Invalid segment selectors and out of bounds
  617                          * %eip's and %esp's can be set up in user mode.
  618                          * This causes a fault in kernel mode when the
  619                          * kernel tries to return to user mode.  We want
  620                          * to get this fault so that we can fix the
  621                          * problem here and not have to check all the
  622                          * selectors and pointers when the user changes
  623                          * them.
  624                          */
  625                         if (frame->tf_eip == (int)doreti_iret) {
  626                                 frame->tf_eip = (int)doreti_iret_fault;
  627                                 goto out;
  628                         }
  629                         if (frame->tf_eip == (int)doreti_popl_ds) {
  630                                 frame->tf_eip = (int)doreti_popl_ds_fault;
  631                                 goto out;
  632                         }
  633                         if (frame->tf_eip == (int)doreti_popl_es) {
  634                                 frame->tf_eip = (int)doreti_popl_es_fault;
  635                                 goto out;
  636                         }
  637                         if (frame->tf_eip == (int)doreti_popl_fs) {
  638                                 frame->tf_eip = (int)doreti_popl_fs_fault;
  639                                 goto out;
  640                         }
  641                         if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
  642                                 frame->tf_eip =
  643                                     (int)PCPU_GET(curpcb)->pcb_onfault;
  644                                 goto out;
  645                         }
  646                         break;
  647 
  648                 case T_TSSFLT:
  649                         /*
  650                          * PSL_NT can be set in user mode and isn't cleared
  651                          * automatically when the kernel is entered.  This
  652                          * causes a TSS fault when the kernel attempts to
  653                          * `iret' because the TSS link is uninitialized.  We
  654                          * want to get this fault so that we can fix the
  655                          * problem here and not every time the kernel is
  656                          * entered.
  657                          */
  658                         if (frame->tf_eflags & PSL_NT) {
  659                                 frame->tf_eflags &= ~PSL_NT;
  660                                 goto out;
  661                         }
  662                         break;
  663 
  664                 case T_TRCTRAP:  /* trace trap */
  665                         if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
  666                                 /*
  667                                  * We've just entered system mode via the
  668                                  * syscall lcall.  Continue single stepping
  669                                  * silently until the syscall handler has
  670                                  * saved the flags.
  671                                  */
  672                                 goto out;
  673                         }
  674                         if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
  675                                 /*
  676                                  * The syscall handler has now saved the
  677                                  * flags.  Stop single stepping it.
  678                                  */
  679                                 frame->tf_eflags &= ~PSL_T;
  680                                 goto out;
  681                         }
  682                         /*
  683                          * Ignore debug register trace traps due to
  684                          * accesses in the user's address space, which
  685                          * can happen under several conditions such as
  686                          * if a user sets a watchpoint on a buffer and
  687                          * then passes that buffer to a system call.
  688                          * We still want to get TRCTRAPS for addresses
  689                          * in kernel space because that is useful when
  690                          * debugging the kernel.
  691                          */
  692                         if (user_dbreg_trap() && 
  693                            !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)) {
  694                                 /*
  695                                  * Reset breakpoint bits because the
  696                                  * processor doesn't
  697                                  */
  698                                 load_dr6(rdr6() & 0xfffffff0);
  699                                 goto out;
  700                         }
  701                         /*
  702                          * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
  703                          */
  704                 case T_BPTFLT:
  705                         /*
  706                          * If KDB is enabled, let it handle the debugger trap.
  707                          * Otherwise, debugger traps "can't happen".
  708                          */
  709 #ifdef KDB
  710                         if (kdb_trap(type, 0, frame))
  711                                 goto out;
  712 #endif
  713                         break;
  714 
  715 #ifdef DEV_ISA
  716                 case T_NMI:
  717 #ifdef POWERFAIL_NMI
  718                         if (time_second - lastalert > 10) {
  719                                 log(LOG_WARNING, "NMI: power fail\n");
  720                                 sysbeep(880, hz);
  721                                 lastalert = time_second;
  722                         }
  723                         goto out;
  724 #else /* !POWERFAIL_NMI */
  725                         /* machine/parity/power fail/"kitchen sink" faults */
  726                         if (isa_nmi(code) == 0) {
  727 #ifdef KDB
  728                                 /*
  729                                  * NMI can be hooked up to a pushbutton
  730                                  * for debugging.
  731                                  */
  732                                 if (kdb_on_nmi) {
  733                                         printf ("NMI ... going to debugger\n");
  734                                         kdb_trap(type, 0, frame);
  735                                 }
  736 #endif /* KDB */
  737                                 goto out;
  738                         } else if (panic_on_nmi == 0)
  739                                 goto out;
  740                         /* FALLTHROUGH */
  741 #endif /* POWERFAIL_NMI */
  742 #endif /* DEV_ISA */
  743                 }
  744 
  745                 trap_fatal(frame, eva);
  746                 goto out;
  747         }
  748 
  749         /* Translate fault for emulators (e.g. Linux) */
  750         if (*p->p_sysent->sv_transtrap)
  751                 i = (*p->p_sysent->sv_transtrap)(i, type);
  752 
  753         ksiginfo_init_trap(&ksi);
  754         ksi.ksi_signo = i;
  755         ksi.ksi_code = ucode;
  756         ksi.ksi_addr = (void *)addr;
  757         ksi.ksi_trapno = type;
  758         trapsignal(td, &ksi);
  759 
  760 #ifdef DEBUG
  761         if (type <= MAX_TRAP_MSG) {
  762                 uprintf("fatal process exception: %s",
  763                         trap_msg[type]);
  764                 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
  765                         uprintf(", fault VA = 0x%lx", (u_long)eva);
  766                 uprintf("\n");
  767         }
  768 #endif
  769 
  770 user:
  771         userret(td, frame);
  772         mtx_assert(&Giant, MA_NOTOWNED);
  773         KASSERT(PCB_USER_FPU(td->td_pcb),
  774             ("Return from trap with kernel FPU ctx leaked"));
  775 userout:
  776 out:
  777         return;
  778 }
  779 
  780 static int
  781 trap_pfault(frame, usermode, eva)
  782         struct trapframe *frame;
  783         int usermode;
  784         vm_offset_t eva;
  785 {
  786         vm_offset_t va;
  787         struct vmspace *vm = NULL;
  788         vm_map_t map;
  789         int rv = 0;
  790         vm_prot_t ftype;
  791         struct thread *td = curthread;
  792         struct proc *p = td->td_proc;
  793 
  794         if (__predict_false((td->td_pflags & TDP_NOFAULTING) != 0)) {
  795                 /*
  796                  * Due to both processor errata and lazy TLB invalidation when
  797                  * access restrictions are removed from virtual pages, memory
  798                  * accesses that are allowed by the physical mapping layer may
  799                  * nonetheless cause one spurious page fault per virtual page. 
  800                  * When the thread is executing a "no faulting" section that
  801                  * is bracketed by vm_fault_{disable,enable}_pagefaults(),
  802                  * every page fault is treated as a spurious page fault,
  803                  * unless it accesses the same virtual address as the most
  804                  * recent page fault within the same "no faulting" section.
  805                  */
  806                 if (td->td_md.md_spurflt_addr != eva ||
  807                     (td->td_pflags & TDP_RESETSPUR) != 0) {
  808                         /*
  809                          * Do nothing to the TLB.  A stale TLB entry is
  810                          * flushed automatically by a page fault.
  811                          */
  812                         td->td_md.md_spurflt_addr = eva;
  813                         td->td_pflags &= ~TDP_RESETSPUR;
  814                         return (0);
  815                 }
  816         } else {
  817                 /*
  818                  * If we get a page fault while in a critical section, then
  819                  * it is most likely a fatal kernel page fault.  The kernel
  820                  * is already going to panic trying to get a sleep lock to
  821                  * do the VM lookup, so just consider it a fatal trap so the
  822                  * kernel can print out a useful trap message and even get
  823                  * to the debugger.
  824                  *
  825                  * If we get a page fault while holding a non-sleepable
  826                  * lock, then it is most likely a fatal kernel page fault.
  827                  * If WITNESS is enabled, then it's going to whine about
  828                  * bogus LORs with various VM locks, so just skip to the
  829                  * fatal trap handling directly.
  830                  */
  831                 if (td->td_critnest != 0 ||
  832                     WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
  833                     "Kernel page fault") != 0) {
  834                         trap_fatal(frame, eva);
  835                         return (-1);
  836                 }
  837         }
  838         va = trunc_page(eva);
  839         if (va >= KERNBASE) {
  840                 /*
  841                  * Don't allow user-mode faults in kernel address space.
  842                  * An exception:  if the faulting address is the invalid
  843                  * instruction entry in the IDT, then the Intel Pentium
  844                  * F00F bug workaround was triggered, and we need to
  845                  * treat it is as an illegal instruction, and not a page
  846                  * fault.
  847                  */
  848 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
  849                 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
  850                         return -2;
  851 #endif
  852                 if (usermode)
  853                         goto nogo;
  854 
  855                 map = kernel_map;
  856         } else {
  857                 /*
  858                  * This is a fault on non-kernel virtual memory.
  859                  * vm is initialized above to NULL. If curproc is NULL
  860                  * or curproc->p_vmspace is NULL the fault is fatal.
  861                  */
  862                 if (p != NULL)
  863                         vm = p->p_vmspace;
  864 
  865                 if (vm == NULL)
  866                         goto nogo;
  867 
  868                 map = &vm->vm_map;
  869                 if (!usermode && (td->td_intr_nesting_level != 0 ||
  870                     PCPU_GET(curpcb)->pcb_onfault == NULL)) {
  871                         trap_fatal(frame, eva);
  872                         return (-1);
  873                 }
  874         }
  875 
  876         /*
  877          * PGEX_I is defined only if the execute disable bit capability is
  878          * supported and enabled.
  879          */
  880         if (frame->tf_err & PGEX_W)
  881                 ftype = VM_PROT_WRITE;
  882 #ifdef PAE
  883         else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
  884                 ftype = VM_PROT_EXECUTE;
  885 #endif
  886         else
  887                 ftype = VM_PROT_READ;
  888 
  889         if (map != kernel_map) {
  890                 /*
  891                  * Keep swapout from messing with us during this
  892                  *      critical time.
  893                  */
  894                 PROC_LOCK(p);
  895                 ++p->p_lock;
  896                 PROC_UNLOCK(p);
  897 
  898                 /* Fault in the user page: */
  899                 rv = vm_fault(map, va, ftype,
  900                               (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
  901                                                       : VM_FAULT_NORMAL);
  902 
  903                 PROC_LOCK(p);
  904                 --p->p_lock;
  905                 PROC_UNLOCK(p);
  906         } else {
  907                 /*
  908                  * Don't have to worry about process locking or stacks in the
  909                  * kernel.
  910                  */
  911                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
  912         }
  913         if (rv == KERN_SUCCESS)
  914                 return (0);
  915 nogo:
  916         if (!usermode) {
  917                 if (td->td_intr_nesting_level == 0 &&
  918                     PCPU_GET(curpcb)->pcb_onfault != NULL) {
  919                         frame->tf_eip = (int)PCPU_GET(curpcb)->pcb_onfault;
  920                         return (0);
  921                 }
  922                 trap_fatal(frame, eva);
  923                 return (-1);
  924         }
  925 
  926         return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
  927 }
  928 
  929 static void
  930 trap_fatal(frame, eva)
  931         struct trapframe *frame;
  932         vm_offset_t eva;
  933 {
  934         int code, ss, esp;
  935         u_int type;
  936         struct soft_segment_descriptor softseg;
  937         char *msg;
  938 
  939         code = frame->tf_err;
  940         type = frame->tf_trapno;
  941         sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
  942 
  943         if (type <= MAX_TRAP_MSG)
  944                 msg = trap_msg[type];
  945         else
  946                 msg = "UNKNOWN";
  947         printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
  948             frame->tf_eflags & PSL_VM ? "vm86" :
  949             ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
  950 #ifdef SMP
  951         /* two separate prints in case of a trap on an unmapped page */
  952         printf("cpuid = %d; ", PCPU_GET(cpuid));
  953         printf("apic id = %02x\n", PCPU_GET(apic_id));
  954 #endif
  955         if (type == T_PAGEFLT) {
  956                 printf("fault virtual address   = 0x%x\n", eva);
  957                 printf("fault code              = %s %s, %s\n",
  958                         code & PGEX_U ? "user" : "supervisor",
  959                         code & PGEX_W ? "write" : "read",
  960                         code & PGEX_P ? "protection violation" : "page not present");
  961         }
  962         printf("instruction pointer     = 0x%x:0x%x\n",
  963                frame->tf_cs & 0xffff, frame->tf_eip);
  964         if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
  965                 ss = frame->tf_ss & 0xffff;
  966                 esp = frame->tf_esp;
  967         } else {
  968                 ss = GSEL(GDATA_SEL, SEL_KPL);
  969                 esp = (int)&frame->tf_esp;
  970         }
  971         printf("stack pointer           = 0x%x:0x%x\n", ss, esp);
  972         printf("frame pointer           = 0x%x:0x%x\n", ss, frame->tf_ebp);
  973         printf("code segment            = base 0x%x, limit 0x%x, type 0x%x\n",
  974                softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
  975         printf("                        = DPL %d, pres %d, def32 %d, gran %d\n",
  976                softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
  977                softseg.ssd_gran);
  978         printf("processor eflags        = ");
  979         if (frame->tf_eflags & PSL_T)
  980                 printf("trace trap, ");
  981         if (frame->tf_eflags & PSL_I)
  982                 printf("interrupt enabled, ");
  983         if (frame->tf_eflags & PSL_NT)
  984                 printf("nested task, ");
  985         if (frame->tf_eflags & PSL_RF)
  986                 printf("resume, ");
  987         if (frame->tf_eflags & PSL_VM)
  988                 printf("vm86, ");
  989         printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
  990         printf("current process         = ");
  991         if (curproc) {
  992                 printf("%lu (%s)\n", (u_long)curproc->p_pid, curthread->td_name);
  993         } else {
  994                 printf("Idle\n");
  995         }
  996 
  997 #ifdef KDB
  998         if (debugger_on_panic || kdb_active) {
  999                 frame->tf_err = eva;    /* smuggle fault address to ddb */
 1000                 if (kdb_trap(type, 0, frame)) {
 1001                         frame->tf_err = code;   /* restore error code */
 1002                         return;
 1003                 }
 1004                 frame->tf_err = code;           /* restore error code */
 1005         }
 1006 #endif
 1007         printf("trap number             = %d\n", type);
 1008         if (type <= MAX_TRAP_MSG)
 1009                 panic("%s", trap_msg[type]);
 1010         else
 1011                 panic("unknown/reserved trap");
 1012 }
 1013 
 1014 /*
 1015  * Double fault handler. Called when a fault occurs while writing
 1016  * a frame for a trap/exception onto the stack. This usually occurs
 1017  * when the stack overflows (such is the case with infinite recursion,
 1018  * for example).
 1019  *
 1020  * XXX Note that the current PTD gets replaced by IdlePTD when the
 1021  * task switch occurs. This means that the stack that was active at
 1022  * the time of the double fault is not available at <kstack> unless
 1023  * the machine was idle when the double fault occurred. The downside
 1024  * of this is that "trace <ebp>" in ddb won't work.
 1025  */
 1026 void
 1027 dblfault_handler()
 1028 {
 1029 #ifdef KDTRACE_HOOKS
 1030         if (dtrace_doubletrap_func != NULL)
 1031                 (*dtrace_doubletrap_func)();
 1032 #endif
 1033         printf("\nFatal double fault:\n");
 1034         printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
 1035         printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
 1036         printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
 1037 #ifdef SMP
 1038         /* two separate prints in case of a trap on an unmapped page */
 1039         printf("cpuid = %d; ", PCPU_GET(cpuid));
 1040         printf("apic id = %02x\n", PCPU_GET(apic_id));
 1041 #endif
 1042         panic("double fault");
 1043 }
 1044 
 1045 int
 1046 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
 1047 {
 1048         struct proc *p;
 1049         struct trapframe *frame;
 1050         caddr_t params;
 1051         int error;
 1052 
 1053         p = td->td_proc;
 1054         frame = td->td_frame;
 1055 
 1056         params = (caddr_t)frame->tf_esp + sizeof(int);
 1057         sa->code = frame->tf_eax;
 1058 
 1059         /*
 1060          * Need to check if this is a 32 bit or 64 bit syscall.
 1061          */
 1062         if (sa->code == SYS_syscall) {
 1063                 /*
 1064                  * Code is first argument, followed by actual args.
 1065                  */
 1066                 sa->code = fuword(params);
 1067                 params += sizeof(int);
 1068         } else if (sa->code == SYS___syscall) {
 1069                 /*
 1070                  * Like syscall, but code is a quad, so as to maintain
 1071                  * quad alignment for the rest of the arguments.
 1072                  */
 1073                 sa->code = fuword(params);
 1074                 params += sizeof(quad_t);
 1075         }
 1076 
 1077         if (p->p_sysent->sv_mask)
 1078                 sa->code &= p->p_sysent->sv_mask;
 1079         if (sa->code >= p->p_sysent->sv_size)
 1080                 sa->callp = &p->p_sysent->sv_table[0];
 1081         else
 1082                 sa->callp = &p->p_sysent->sv_table[sa->code];
 1083         sa->narg = sa->callp->sy_narg;
 1084 
 1085         if (params != NULL && sa->narg != 0)
 1086                 error = copyin(params, (caddr_t)sa->args,
 1087                     (u_int)(sa->narg * sizeof(int)));
 1088         else
 1089                 error = 0;
 1090 
 1091         if (error == 0) {
 1092                 td->td_retval[0] = 0;
 1093                 td->td_retval[1] = frame->tf_edx;
 1094         }
 1095                 
 1096         return (error);
 1097 }
 1098 
 1099 #include "../../kern/subr_syscall.c"
 1100 
 1101 /*
 1102  *      syscall -       system call request C handler
 1103  *
 1104  *      A system call is essentially treated as a trap.
 1105  */
 1106 void
 1107 syscall(struct trapframe *frame)
 1108 {
 1109         struct thread *td;
 1110         struct syscall_args sa;
 1111         register_t orig_tf_eflags;
 1112         int error;
 1113         ksiginfo_t ksi;
 1114 
 1115 #ifdef DIAGNOSTIC
 1116         if (ISPL(frame->tf_cs) != SEL_UPL) {
 1117                 panic("syscall");
 1118                 /* NOT REACHED */
 1119         }
 1120 #endif
 1121         orig_tf_eflags = frame->tf_eflags;
 1122 
 1123         td = curthread;
 1124         td->td_frame = frame;
 1125 
 1126         error = syscallenter(td, &sa);
 1127 
 1128         /*
 1129          * Traced syscall.
 1130          */
 1131         if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
 1132                 frame->tf_eflags &= ~PSL_T;
 1133                 ksiginfo_init_trap(&ksi);
 1134                 ksi.ksi_signo = SIGTRAP;
 1135                 ksi.ksi_code = TRAP_TRACE;
 1136                 ksi.ksi_addr = (void *)frame->tf_eip;
 1137                 trapsignal(td, &ksi);
 1138         }
 1139 
 1140         KASSERT(PCB_USER_FPU(td->td_pcb),
 1141             ("System call %s returning with kernel FPU ctx leaked",
 1142              syscallname(td->td_proc, sa.code)));
 1143         KASSERT(td->td_pcb->pcb_save == &td->td_pcb->pcb_user_save,
 1144             ("System call %s returning with mangled pcb_save",
 1145              syscallname(td->td_proc, sa.code)));
 1146 
 1147         syscallret(td, error, &sa);
 1148 }

Cache object: 6ca32c28df82cbaf951e64229c71f11e


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