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$");
   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 #endif
  126 
  127 /* Defined in i386/i386/elf_machdep.c. */
  128 extern struct sysentvec elf32_freebsd_sysvec;
  129 
  130 extern void trap(struct trapframe *frame);
  131 extern void syscall(struct trapframe *frame);
  132 
  133 static int trap_pfault(struct trapframe *, int, vm_offset_t);
  134 static void trap_fatal(struct trapframe *, vm_offset_t);
  135 void dblfault_handler(void);
  136 
  137 extern inthand_t IDTVEC(lcall_syscall);
  138 
  139 #define MAX_TRAP_MSG            30
  140 static char *trap_msg[] = {
  141         "",                                     /*  0 unused */
  142         "privileged instruction fault",         /*  1 T_PRIVINFLT */
  143         "",                                     /*  2 unused */
  144         "breakpoint instruction fault",         /*  3 T_BPTFLT */
  145         "",                                     /*  4 unused */
  146         "",                                     /*  5 unused */
  147         "arithmetic trap",                      /*  6 T_ARITHTRAP */
  148         "",                                     /*  7 unused */
  149         "",                                     /*  8 unused */
  150         "general protection fault",             /*  9 T_PROTFLT */
  151         "trace trap",                           /* 10 T_TRCTRAP */
  152         "",                                     /* 11 unused */
  153         "page fault",                           /* 12 T_PAGEFLT */
  154         "",                                     /* 13 unused */
  155         "alignment fault",                      /* 14 T_ALIGNFLT */
  156         "",                                     /* 15 unused */
  157         "",                                     /* 16 unused */
  158         "",                                     /* 17 unused */
  159         "integer divide fault",                 /* 18 T_DIVIDE */
  160         "non-maskable interrupt trap",          /* 19 T_NMI */
  161         "overflow trap",                        /* 20 T_OFLOW */
  162         "FPU bounds check fault",               /* 21 T_BOUND */
  163         "FPU device not available",             /* 22 T_DNA */
  164         "double fault",                         /* 23 T_DOUBLEFLT */
  165         "FPU operand fetch fault",              /* 24 T_FPOPFLT */
  166         "invalid TSS fault",                    /* 25 T_TSSFLT */
  167         "segment not present fault",            /* 26 T_SEGNPFLT */
  168         "stack fault",                          /* 27 T_STKFLT */
  169         "machine check trap",                   /* 28 T_MCHK */
  170         "SIMD floating-point exception",        /* 29 T_XMMFLT */
  171         "reserved (unknown) fault",             /* 30 T_RESERVED */
  172 };
  173 
  174 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
  175 extern int has_f00f_bug;
  176 #endif
  177 
  178 #ifdef KDB
  179 static int kdb_on_nmi = 1;
  180 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
  181         &kdb_on_nmi, 0, "Go to KDB on NMI");
  182 #endif
  183 static int panic_on_nmi = 1;
  184 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
  185         &panic_on_nmi, 0, "Panic on NMI");
  186 static int prot_fault_translation = 0;
  187 SYSCTL_INT(_machdep, OID_AUTO, prot_fault_translation, CTLFLAG_RW,
  188         &prot_fault_translation, 0, "Select signal to deliver on protection fault");
  189 
  190 extern char *syscallnames[];
  191 
  192 /*
  193  * Exception, fault, and trap interface to the FreeBSD kernel.
  194  * This common code is called from assembly language IDT gate entry
  195  * routines that prepare a suitable stack frame, and restore this
  196  * frame after the exception has been processed.
  197  */
  198 
  199 void
  200 trap(struct trapframe *frame)
  201 {
  202         struct thread *td = curthread;
  203         struct proc *p = td->td_proc;
  204         int i = 0, ucode = 0, code;
  205         u_int type;
  206         register_t addr = 0;
  207         vm_offset_t eva;
  208         ksiginfo_t ksi;
  209 #ifdef POWERFAIL_NMI
  210         static int lastalert = 0;
  211 #endif
  212 
  213         PCPU_INC(cnt.v_trap);
  214         type = frame->tf_trapno;
  215 
  216 #ifdef SMP
  217 #ifdef STOP_NMI
  218         /* Handler for NMI IPIs used for stopping CPUs. */
  219         if (type == T_NMI) {
  220                  if (ipi_nmi_handler() == 0)
  221                            goto out;
  222         }
  223 #endif /* STOP_NMI */
  224 #endif /* SMP */
  225 
  226 #ifdef KDB
  227         if (kdb_active) {
  228                 kdb_reenter();
  229                 goto out;
  230         }
  231 #endif
  232 
  233 #ifdef  HWPMC_HOOKS
  234         /*
  235          * CPU PMCs interrupt using an NMI so we check for that first.
  236          * If the HWPMC module is active, 'pmc_hook' will point to
  237          * the function to be called.  A return value of '1' from the
  238          * hook means that the NMI was handled by it and that we can
  239          * return immediately.
  240          */
  241         if (type == T_NMI && pmc_intr &&
  242             (*pmc_intr)(PCPU_GET(cpuid), frame))
  243             goto out;
  244 #endif
  245 
  246         if (type == T_MCHK) {
  247                 if (!mca_intr())
  248                         trap_fatal(frame, 0);
  249                 goto out;
  250         }
  251 
  252 #ifdef KDTRACE_HOOKS
  253         /*
  254          * A trap can occur while DTrace executes a probe. Before
  255          * executing the probe, DTrace blocks re-scheduling and sets
  256          * a flag in it's per-cpu flags to indicate that it doesn't
  257          * want to fault. On returning from the probe, the no-fault
  258          * flag is cleared and finally re-scheduling is enabled.
  259          *
  260          * If the DTrace kernel module has registered a trap handler,
  261          * call it and if it returns non-zero, assume that it has
  262          * handled the trap and modified the trap frame so that this
  263          * function can return normally.
  264          */
  265         if ((type == T_PROTFLT || type == T_PAGEFLT) &&
  266             dtrace_trap_func != NULL)
  267                 if ((*dtrace_trap_func)(frame, type))
  268                         goto out;
  269 #endif
  270 
  271         if ((frame->tf_eflags & PSL_I) == 0) {
  272                 /*
  273                  * Buggy application or kernel code has disabled
  274                  * interrupts and then trapped.  Enabling interrupts
  275                  * now is wrong, but it is better than running with
  276                  * interrupts disabled until they are accidentally
  277                  * enabled later.
  278                  */
  279                 if (ISPL(frame->tf_cs) == SEL_UPL || (frame->tf_eflags & PSL_VM))
  280                         printf(
  281                             "pid %ld (%s): trap %d with interrupts disabled\n",
  282                             (long)curproc->p_pid, curproc->p_comm, type);
  283                 else if (type != T_BPTFLT && type != T_TRCTRAP &&
  284                          frame->tf_eip != (int)cpu_switch_load_gs) {
  285                         /*
  286                          * XXX not quite right, since this may be for a
  287                          * multiple fault in user mode.
  288                          */
  289                         printf("kernel trap %d with interrupts disabled\n",
  290                             type);
  291                         /*
  292                          * Page faults need interrupts disabled until later,
  293                          * and we shouldn't enable interrupts while holding
  294                          * a spin lock or if servicing an NMI.
  295                          */
  296                         if (type != T_NMI && type != T_PAGEFLT &&
  297                             td->td_md.md_spinlock_count == 0)
  298                                 enable_intr();
  299                 }
  300         }
  301 
  302         eva = 0;
  303         code = frame->tf_err;
  304         if (type == T_PAGEFLT) {
  305                 /*
  306                  * For some Cyrix CPUs, %cr2 is clobbered by
  307                  * interrupts.  This problem is worked around by using
  308                  * an interrupt gate for the pagefault handler.  We
  309                  * are finally ready to read %cr2 and then must
  310                  * reenable interrupts.
  311                  *
  312                  * If we get a page fault while in a critical section, then
  313                  * it is most likely a fatal kernel page fault.  The kernel
  314                  * is already going to panic trying to get a sleep lock to
  315                  * do the VM lookup, so just consider it a fatal trap so the
  316                  * kernel can print out a useful trap message and even get
  317                  * to the debugger.
  318                  *
  319                  * If we get a page fault while holding a non-sleepable
  320                  * lock, then it is most likely a fatal kernel page fault.
  321                  * If WITNESS is enabled, then it's going to whine about
  322                  * bogus LORs with various VM locks, so just skip to the
  323                  * fatal trap handling directly.
  324                  */
  325                 eva = rcr2();
  326                 if (td->td_critnest != 0 ||
  327                     WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
  328                     "Kernel page fault") != 0)
  329                         trap_fatal(frame, eva);
  330                 else
  331                         enable_intr();
  332         }
  333 
  334         if ((ISPL(frame->tf_cs) == SEL_UPL) ||
  335             ((frame->tf_eflags & PSL_VM) && 
  336                 !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL))) {
  337                 /* user trap */
  338 
  339                 td->td_pticks = 0;
  340                 td->td_frame = frame;
  341                 addr = frame->tf_eip;
  342                 if (td->td_ucred != p->p_ucred) 
  343                         cred_update_thread(td);
  344 
  345                 switch (type) {
  346                 case T_PRIVINFLT:       /* privileged instruction fault */
  347                         i = SIGILL;
  348                         ucode = ILL_PRVOPC;
  349                         break;
  350 
  351                 case T_BPTFLT:          /* bpt instruction fault */
  352                 case T_TRCTRAP:         /* trace trap */
  353                         enable_intr();
  354                         frame->tf_eflags &= ~PSL_T;
  355                         i = SIGTRAP;
  356                         ucode = (type == T_TRCTRAP ? TRAP_TRACE : TRAP_BRKPT);
  357                         break;
  358 
  359                 case T_ARITHTRAP:       /* arithmetic trap */
  360 #ifdef DEV_NPX
  361                         ucode = npxtrap();
  362                         if (ucode == -1)
  363                                 goto userout;
  364 #else
  365                         ucode = 0;
  366 #endif
  367                         i = SIGFPE;
  368                         break;
  369 
  370                         /*
  371                          * The following two traps can happen in
  372                          * vm86 mode, and, if so, we want to handle
  373                          * them specially.
  374                          */
  375                 case T_PROTFLT:         /* general protection fault */
  376                 case T_STKFLT:          /* stack fault */
  377                         if (frame->tf_eflags & PSL_VM) {
  378                                 i = vm86_emulate((struct vm86frame *)frame);
  379                                 if (i == 0)
  380                                         goto user;
  381                                 break;
  382                         }
  383                         i = SIGBUS;
  384                         ucode = (type == T_PROTFLT) ? BUS_OBJERR : BUS_ADRERR;
  385                         break;
  386                 case T_SEGNPFLT:        /* segment not present fault */
  387                         i = SIGBUS;
  388                         ucode = BUS_ADRERR;
  389                         break;
  390                 case T_TSSFLT:          /* invalid TSS fault */
  391                         i = SIGBUS;
  392                         ucode = BUS_OBJERR;
  393                         break;
  394                 case T_DOUBLEFLT:       /* double fault */
  395                 default:
  396                         i = SIGBUS;
  397                         ucode = BUS_OBJERR;
  398                         break;
  399 
  400                 case T_PAGEFLT:         /* page fault */
  401 #ifdef KSE
  402                         if (td->td_pflags & TDP_SA)
  403                                 thread_user_enter(td);
  404 #endif
  405 
  406                         i = trap_pfault(frame, TRUE, eva);
  407 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
  408                         if (i == -2) {
  409                                 /*
  410                                  * The f00f hack workaround has triggered, so
  411                                  * treat the fault as an illegal instruction 
  412                                  * (T_PRIVINFLT) instead of a page fault.
  413                                  */
  414                                 type = frame->tf_trapno = T_PRIVINFLT;
  415 
  416                                 /* Proceed as in that case. */
  417                                 ucode = ILL_PRVOPC;
  418                                 i = SIGILL;
  419                                 break;
  420                         }
  421 #endif
  422                         if (i == -1)
  423                                 goto userout;
  424                         if (i == 0)
  425                                 goto user;
  426 
  427                         if (i == SIGSEGV)
  428                                 ucode = SEGV_MAPERR;
  429                         else {
  430                                 if (prot_fault_translation == 0) {
  431                                         /*
  432                                          * Autodetect.
  433                                          * This check also covers the images
  434                                          * without the ABI-tag ELF note.
  435                                          */
  436                                         if (curproc->p_sysent ==
  437                                             &elf32_freebsd_sysvec &&
  438                                             p->p_osrel >= 700004) {
  439                                                 i = SIGSEGV;
  440                                                 ucode = SEGV_ACCERR;
  441                                         } else {
  442                                                 i = SIGBUS;
  443                                                 ucode = BUS_PAGE_FAULT;
  444                                         }
  445                                 } else if (prot_fault_translation == 1) {
  446                                         /*
  447                                          * Always compat mode.
  448                                          */
  449                                         i = SIGBUS;
  450                                         ucode = BUS_PAGE_FAULT;
  451                                 } else {
  452                                         /*
  453                                          * Always SIGSEGV mode.
  454                                          */
  455                                         i = SIGSEGV;
  456                                         ucode = SEGV_ACCERR;
  457                                 }
  458                         }
  459                         addr = eva;
  460                         break;
  461 
  462                 case T_DIVIDE:          /* integer divide fault */
  463                         ucode = FPE_INTDIV;
  464                         i = SIGFPE;
  465                         break;
  466 
  467 #ifdef DEV_ISA
  468                 case T_NMI:
  469 #ifdef POWERFAIL_NMI
  470 #ifndef TIMER_FREQ
  471 #  define TIMER_FREQ 1193182
  472 #endif
  473                         mtx_lock(&Giant);
  474                         if (time_second - lastalert > 10) {
  475                                 log(LOG_WARNING, "NMI: power fail\n");
  476                                 sysbeep(TIMER_FREQ/880, hz);
  477                                 lastalert = time_second;
  478                         }
  479                         mtx_unlock(&Giant);
  480                         goto userout;
  481 #else /* !POWERFAIL_NMI */
  482                         /* machine/parity/power fail/"kitchen sink" faults */
  483                         /* XXX Giant */
  484                         if (isa_nmi(code) == 0) {
  485 #ifdef KDB
  486                                 /*
  487                                  * NMI can be hooked up to a pushbutton
  488                                  * for debugging.
  489                                  */
  490                                 if (kdb_on_nmi) {
  491                                         printf ("NMI ... going to debugger\n");
  492                                         kdb_trap(type, 0, frame);
  493                                 }
  494 #endif /* KDB */
  495                                 goto userout;
  496                         } else if (panic_on_nmi)
  497                                 panic("NMI indicates hardware failure");
  498                         break;
  499 #endif /* POWERFAIL_NMI */
  500 #endif /* DEV_ISA */
  501 
  502                 case T_OFLOW:           /* integer overflow fault */
  503                         ucode = FPE_INTOVF;
  504                         i = SIGFPE;
  505                         break;
  506 
  507                 case T_BOUND:           /* bounds check fault */
  508                         ucode = FPE_FLTSUB;
  509                         i = SIGFPE;
  510                         break;
  511 
  512                 case T_DNA:
  513 #ifdef DEV_NPX
  514                         /* transparent fault (due to context switch "late") */
  515                         if (npxdna())
  516                                 goto userout;
  517 #endif
  518                         printf("pid %d killed due to lack of floating point\n",
  519                                 p->p_pid);
  520                         i = SIGKILL;
  521                         ucode = 0;
  522                         break;
  523 
  524                 case T_FPOPFLT:         /* FPU operand fetch fault */
  525                         ucode = ILL_COPROC;
  526                         i = SIGILL;
  527                         break;
  528 
  529                 case T_XMMFLT:          /* SIMD floating-point exception */
  530                         ucode = 0; /* XXX */
  531                         i = SIGFPE;
  532                         break;
  533                 }
  534         } else {
  535                 /* kernel trap */
  536 
  537                 KASSERT(cold || td->td_ucred != NULL,
  538                     ("kernel trap doesn't have ucred"));
  539                 switch (type) {
  540                 case T_PAGEFLT:                 /* page fault */
  541                         (void) trap_pfault(frame, FALSE, eva);
  542                         goto out;
  543 
  544                 case T_DNA:
  545 #ifdef DEV_NPX
  546                         /*
  547                          * The kernel is apparently using npx for copying.
  548                          * XXX this should be fatal unless the kernel has
  549                          * registered such use.
  550                          */
  551                         if (npxdna())
  552                                 goto out;
  553 #endif
  554                         break;
  555 
  556                         /*
  557                          * The following two traps can happen in
  558                          * vm86 mode, and, if so, we want to handle
  559                          * them specially.
  560                          */
  561                 case T_PROTFLT:         /* general protection fault */
  562                 case T_STKFLT:          /* stack fault */
  563                         if (frame->tf_eflags & PSL_VM) {
  564                                 i = vm86_emulate((struct vm86frame *)frame);
  565                                 if (i != 0)
  566                                         /*
  567                                          * returns to original process
  568                                          */
  569                                         vm86_trap((struct vm86frame *)frame);
  570                                 goto out;
  571                         }
  572                         if (type == T_STKFLT)
  573                                 break;
  574 
  575                         /* FALL THROUGH */
  576 
  577                 case T_SEGNPFLT:        /* segment not present fault */
  578                         if (PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)
  579                                 break;
  580 
  581                         /*
  582                          * Invalid %fs's and %gs's can be created using
  583                          * procfs or PT_SETREGS or by invalidating the
  584                          * underlying LDT entry.  This causes a fault
  585                          * in kernel mode when the kernel attempts to
  586                          * switch contexts.  Lose the bad context
  587                          * (XXX) so that we can continue, and generate
  588                          * a signal.
  589                          */
  590                         if (frame->tf_eip == (int)cpu_switch_load_gs) {
  591                                 PCPU_GET(curpcb)->pcb_gs = 0;
  592 #if 0                           
  593                                 PROC_LOCK(p);
  594                                 psignal(p, SIGBUS);
  595                                 PROC_UNLOCK(p);
  596 #endif                          
  597                                 goto out;
  598                         }
  599 
  600                         if (td->td_intr_nesting_level != 0)
  601                                 break;
  602 
  603                         /*
  604                          * Invalid segment selectors and out of bounds
  605                          * %eip's and %esp's can be set up in user mode.
  606                          * This causes a fault in kernel mode when the
  607                          * kernel tries to return to user mode.  We want
  608                          * to get this fault so that we can fix the
  609                          * problem here and not have to check all the
  610                          * selectors and pointers when the user changes
  611                          * them.
  612                          */
  613                         if (frame->tf_eip == (int)doreti_iret) {
  614                                 frame->tf_eip = (int)doreti_iret_fault;
  615                                 goto out;
  616                         }
  617                         if (frame->tf_eip == (int)doreti_popl_ds) {
  618                                 frame->tf_eip = (int)doreti_popl_ds_fault;
  619                                 goto out;
  620                         }
  621                         if (frame->tf_eip == (int)doreti_popl_es) {
  622                                 frame->tf_eip = (int)doreti_popl_es_fault;
  623                                 goto out;
  624                         }
  625                         if (frame->tf_eip == (int)doreti_popl_fs) {
  626                                 frame->tf_eip = (int)doreti_popl_fs_fault;
  627                                 goto out;
  628                         }
  629                         if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
  630                                 frame->tf_eip =
  631                                     (int)PCPU_GET(curpcb)->pcb_onfault;
  632                                 goto out;
  633                         }
  634                         break;
  635 
  636                 case T_TSSFLT:
  637                         /*
  638                          * PSL_NT can be set in user mode and isn't cleared
  639                          * automatically when the kernel is entered.  This
  640                          * causes a TSS fault when the kernel attempts to
  641                          * `iret' because the TSS link is uninitialized.  We
  642                          * want to get this fault so that we can fix the
  643                          * problem here and not every time the kernel is
  644                          * entered.
  645                          */
  646                         if (frame->tf_eflags & PSL_NT) {
  647                                 frame->tf_eflags &= ~PSL_NT;
  648                                 goto out;
  649                         }
  650                         break;
  651 
  652                 case T_TRCTRAP:  /* trace trap */
  653                         if (frame->tf_eip == (int)IDTVEC(lcall_syscall)) {
  654                                 /*
  655                                  * We've just entered system mode via the
  656                                  * syscall lcall.  Continue single stepping
  657                                  * silently until the syscall handler has
  658                                  * saved the flags.
  659                                  */
  660                                 goto out;
  661                         }
  662                         if (frame->tf_eip == (int)IDTVEC(lcall_syscall) + 1) {
  663                                 /*
  664                                  * The syscall handler has now saved the
  665                                  * flags.  Stop single stepping it.
  666                                  */
  667                                 frame->tf_eflags &= ~PSL_T;
  668                                 goto out;
  669                         }
  670                         /*
  671                          * Ignore debug register trace traps due to
  672                          * accesses in the user's address space, which
  673                          * can happen under several conditions such as
  674                          * if a user sets a watchpoint on a buffer and
  675                          * then passes that buffer to a system call.
  676                          * We still want to get TRCTRAPS for addresses
  677                          * in kernel space because that is useful when
  678                          * debugging the kernel.
  679                          */
  680                         /* XXX Giant */
  681                         if (user_dbreg_trap() && 
  682                            !(PCPU_GET(curpcb)->pcb_flags & PCB_VM86CALL)) {
  683                                 /*
  684                                  * Reset breakpoint bits because the
  685                                  * processor doesn't
  686                                  */
  687                                 load_dr6(rdr6() & 0xfffffff0);
  688                                 goto out;
  689                         }
  690                         /*
  691                          * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
  692                          */
  693                 case T_BPTFLT:
  694                         /*
  695                          * If KDB is enabled, let it handle the debugger trap.
  696                          * Otherwise, debugger traps "can't happen".
  697                          */
  698 #ifdef KDB
  699                         if (kdb_trap(type, 0, frame))
  700                                 goto out;
  701 #endif
  702                         break;
  703 
  704 #ifdef DEV_ISA
  705                 case T_NMI:
  706 #ifdef POWERFAIL_NMI
  707                         mtx_lock(&Giant);
  708                         if (time_second - lastalert > 10) {
  709                                 log(LOG_WARNING, "NMI: power fail\n");
  710                                 sysbeep(TIMER_FREQ/880, hz);
  711                                 lastalert = time_second;
  712                         }
  713                         mtx_unlock(&Giant);
  714                         goto out;
  715 #else /* !POWERFAIL_NMI */
  716                         /* XXX Giant */
  717                         /* machine/parity/power fail/"kitchen sink" faults */
  718                         if (isa_nmi(code) == 0) {
  719 #ifdef KDB
  720                                 /*
  721                                  * NMI can be hooked up to a pushbutton
  722                                  * for debugging.
  723                                  */
  724                                 if (kdb_on_nmi) {
  725                                         printf ("NMI ... going to debugger\n");
  726                                         kdb_trap(type, 0, frame);
  727                                 }
  728 #endif /* KDB */
  729                                 goto out;
  730                         } else if (panic_on_nmi == 0)
  731                                 goto out;
  732                         /* FALLTHROUGH */
  733 #endif /* POWERFAIL_NMI */
  734 #endif /* DEV_ISA */
  735                 }
  736 
  737                 trap_fatal(frame, eva);
  738                 goto out;
  739         }
  740 
  741         /* Translate fault for emulators (e.g. Linux) */
  742         if (*p->p_sysent->sv_transtrap)
  743                 i = (*p->p_sysent->sv_transtrap)(i, type);
  744 
  745         ksiginfo_init_trap(&ksi);
  746         ksi.ksi_signo = i;
  747         ksi.ksi_code = ucode;
  748         ksi.ksi_addr = (void *)addr;
  749         ksi.ksi_trapno = type;
  750         trapsignal(td, &ksi);
  751 
  752 #ifdef DEBUG
  753         if (type <= MAX_TRAP_MSG) {
  754                 uprintf("fatal process exception: %s",
  755                         trap_msg[type]);
  756                 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
  757                         uprintf(", fault VA = 0x%lx", (u_long)eva);
  758                 uprintf("\n");
  759         }
  760 #endif
  761 
  762 user:
  763         userret(td, frame);
  764         mtx_assert(&Giant, MA_NOTOWNED);
  765 userout:
  766 out:
  767         return;
  768 }
  769 
  770 static int
  771 trap_pfault(frame, usermode, eva)
  772         struct trapframe *frame;
  773         int usermode;
  774         vm_offset_t eva;
  775 {
  776         vm_offset_t va;
  777         struct vmspace *vm = NULL;
  778         vm_map_t map;
  779         int rv = 0;
  780         vm_prot_t ftype;
  781         struct thread *td = curthread;
  782         struct proc *p = td->td_proc;
  783 
  784         va = trunc_page(eva);
  785         if (va >= KERNBASE) {
  786                 /*
  787                  * Don't allow user-mode faults in kernel address space.
  788                  * An exception:  if the faulting address is the invalid
  789                  * instruction entry in the IDT, then the Intel Pentium
  790                  * F00F bug workaround was triggered, and we need to
  791                  * treat it is as an illegal instruction, and not a page
  792                  * fault.
  793                  */
  794 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
  795                 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug)
  796                         return -2;
  797 #endif
  798                 if (usermode)
  799                         goto nogo;
  800 
  801                 map = kernel_map;
  802         } else {
  803                 /*
  804                  * This is a fault on non-kernel virtual memory.
  805                  * vm is initialized above to NULL. If curproc is NULL
  806                  * or curproc->p_vmspace is NULL the fault is fatal.
  807                  */
  808                 if (p != NULL)
  809                         vm = p->p_vmspace;
  810 
  811                 if (vm == NULL)
  812                         goto nogo;
  813 
  814                 map = &vm->vm_map;
  815         }
  816 
  817         /*
  818          * PGEX_I is defined only if the execute disable bit capability is
  819          * supported and enabled.
  820          */
  821         if (frame->tf_err & PGEX_W)
  822                 ftype = VM_PROT_WRITE;
  823 #ifdef PAE
  824         else if ((frame->tf_err & PGEX_I) && pg_nx != 0)
  825                 ftype = VM_PROT_EXECUTE;
  826 #endif
  827         else
  828                 ftype = VM_PROT_READ;
  829 
  830         if (map != kernel_map) {
  831                 /*
  832                  * Keep swapout from messing with us during this
  833                  *      critical time.
  834                  */
  835                 PROC_LOCK(p);
  836                 ++p->p_lock;
  837                 PROC_UNLOCK(p);
  838 
  839                 /* Fault in the user page: */
  840                 rv = vm_fault(map, va, ftype,
  841                               (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
  842                                                       : VM_FAULT_NORMAL);
  843 
  844                 PROC_LOCK(p);
  845                 --p->p_lock;
  846                 PROC_UNLOCK(p);
  847         } else {
  848                 /*
  849                  * Don't have to worry about process locking or stacks in the
  850                  * kernel.
  851                  */
  852                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
  853         }
  854         if (rv == KERN_SUCCESS)
  855                 return (0);
  856 nogo:
  857         if (!usermode) {
  858                 if (td->td_intr_nesting_level == 0 &&
  859                     PCPU_GET(curpcb)->pcb_onfault != NULL) {
  860                         frame->tf_eip = (int)PCPU_GET(curpcb)->pcb_onfault;
  861                         return (0);
  862                 }
  863                 trap_fatal(frame, eva);
  864                 return (-1);
  865         }
  866 
  867         return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
  868 }
  869 
  870 static void
  871 trap_fatal(frame, eva)
  872         struct trapframe *frame;
  873         vm_offset_t eva;
  874 {
  875         int code, ss, esp;
  876         u_int type;
  877         struct soft_segment_descriptor softseg;
  878         char *msg;
  879 
  880         code = frame->tf_err;
  881         type = frame->tf_trapno;
  882         sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
  883 
  884         if (type <= MAX_TRAP_MSG)
  885                 msg = trap_msg[type];
  886         else
  887                 msg = "UNKNOWN";
  888         printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
  889             frame->tf_eflags & PSL_VM ? "vm86" :
  890             ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
  891 #ifdef SMP
  892         /* two separate prints in case of a trap on an unmapped page */
  893         printf("cpuid = %d; ", PCPU_GET(cpuid));
  894         printf("apic id = %02x\n", PCPU_GET(apic_id));
  895 #endif
  896         if (type == T_PAGEFLT) {
  897                 printf("fault virtual address   = 0x%x\n", eva);
  898                 printf("fault code              = %s %s, %s\n",
  899                         code & PGEX_U ? "user" : "supervisor",
  900                         code & PGEX_W ? "write" : "read",
  901                         code & PGEX_P ? "protection violation" : "page not present");
  902         }
  903         printf("instruction pointer     = 0x%x:0x%x\n",
  904                frame->tf_cs & 0xffff, frame->tf_eip);
  905         if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
  906                 ss = frame->tf_ss & 0xffff;
  907                 esp = frame->tf_esp;
  908         } else {
  909                 ss = GSEL(GDATA_SEL, SEL_KPL);
  910                 esp = (int)&frame->tf_esp;
  911         }
  912         printf("stack pointer           = 0x%x:0x%x\n", ss, esp);
  913         printf("frame pointer           = 0x%x:0x%x\n", ss, frame->tf_ebp);
  914         printf("code segment            = base 0x%x, limit 0x%x, type 0x%x\n",
  915                softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
  916         printf("                        = DPL %d, pres %d, def32 %d, gran %d\n",
  917                softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
  918                softseg.ssd_gran);
  919         printf("processor eflags        = ");
  920         if (frame->tf_eflags & PSL_T)
  921                 printf("trace trap, ");
  922         if (frame->tf_eflags & PSL_I)
  923                 printf("interrupt enabled, ");
  924         if (frame->tf_eflags & PSL_NT)
  925                 printf("nested task, ");
  926         if (frame->tf_eflags & PSL_RF)
  927                 printf("resume, ");
  928         if (frame->tf_eflags & PSL_VM)
  929                 printf("vm86, ");
  930         printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
  931         printf("current process         = ");
  932         if (curproc) {
  933                 printf("%lu (%s)\n", (u_long)curproc->p_pid, curproc->p_comm);
  934         } else {
  935                 printf("Idle\n");
  936         }
  937 
  938 #ifdef KDB
  939         if (debugger_on_panic || kdb_active) {
  940                 frame->tf_err = eva;    /* smuggle fault address to ddb */
  941                 if (kdb_trap(type, 0, frame)) {
  942                         frame->tf_err = code;   /* restore error code */
  943                         return;
  944                 }
  945                 frame->tf_err = code;           /* restore error code */
  946         }
  947 #endif
  948         printf("trap number             = %d\n", type);
  949         if (type <= MAX_TRAP_MSG)
  950                 panic("%s", trap_msg[type]);
  951         else
  952                 panic("unknown/reserved trap");
  953 }
  954 
  955 /*
  956  * Double fault handler. Called when a fault occurs while writing
  957  * a frame for a trap/exception onto the stack. This usually occurs
  958  * when the stack overflows (such is the case with infinite recursion,
  959  * for example).
  960  *
  961  * XXX Note that the current PTD gets replaced by IdlePTD when the
  962  * task switch occurs. This means that the stack that was active at
  963  * the time of the double fault is not available at <kstack> unless
  964  * the machine was idle when the double fault occurred. The downside
  965  * of this is that "trace <ebp>" in ddb won't work.
  966  */
  967 void
  968 dblfault_handler()
  969 {
  970 #ifdef KDTRACE_HOOKS
  971         if (dtrace_doubletrap_func != NULL)
  972                 (*dtrace_doubletrap_func)();
  973 #endif
  974         printf("\nFatal double fault:\n");
  975         printf("eip = 0x%x\n", PCPU_GET(common_tss.tss_eip));
  976         printf("esp = 0x%x\n", PCPU_GET(common_tss.tss_esp));
  977         printf("ebp = 0x%x\n", PCPU_GET(common_tss.tss_ebp));
  978 #ifdef SMP
  979         /* two separate prints in case of a trap on an unmapped page */
  980         printf("cpuid = %d; ", PCPU_GET(cpuid));
  981         printf("apic id = %02x\n", PCPU_GET(apic_id));
  982 #endif
  983         panic("double fault");
  984 }
  985 
  986 /*
  987  *      syscall -       system call request C handler
  988  *
  989  *      A system call is essentially treated as a trap.
  990  */
  991 void
  992 syscall(struct trapframe *frame)
  993 {
  994         caddr_t params;
  995         struct sysent *callp;
  996         struct thread *td = curthread;
  997         struct proc *p = td->td_proc;
  998         register_t orig_tf_eflags;
  999         int error;
 1000         int narg;
 1001         int args[8];
 1002         u_int code;
 1003         ksiginfo_t ksi;
 1004 
 1005         PCPU_INC(cnt.v_syscall);
 1006 
 1007 #ifdef DIAGNOSTIC
 1008         if (ISPL(frame->tf_cs) != SEL_UPL) {
 1009                 panic("syscall");
 1010                 /* NOT REACHED */
 1011         }
 1012 #endif
 1013 
 1014         td->td_pticks = 0;
 1015         td->td_frame = frame;
 1016         if (td->td_ucred != p->p_ucred) 
 1017                 cred_update_thread(td);
 1018 #ifdef KSE
 1019         if (p->p_flag & P_SA)
 1020                 thread_user_enter(td);
 1021 #endif
 1022         params = (caddr_t)frame->tf_esp + sizeof(int);
 1023         code = frame->tf_eax;
 1024         orig_tf_eflags = frame->tf_eflags;
 1025 
 1026         if (p->p_sysent->sv_prepsyscall) {
 1027                 /*
 1028                  * The prep code is MP aware.
 1029                  */
 1030                 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, &params);
 1031         } else {
 1032                 /*
 1033                  * Need to check if this is a 32 bit or 64 bit syscall.
 1034                  * fuword is MP aware.
 1035                  */
 1036                 if (code == SYS_syscall) {
 1037                         /*
 1038                          * Code is first argument, followed by actual args.
 1039                          */
 1040                         code = fuword(params);
 1041                         params += sizeof(int);
 1042                 } else if (code == SYS___syscall) {
 1043                         /*
 1044                          * Like syscall, but code is a quad, so as to maintain
 1045                          * quad alignment for the rest of the arguments.
 1046                          */
 1047                         code = fuword(params);
 1048                         params += sizeof(quad_t);
 1049                 }
 1050         }
 1051 
 1052         if (p->p_sysent->sv_mask)
 1053                 code &= p->p_sysent->sv_mask;
 1054 
 1055         if (code >= p->p_sysent->sv_size)
 1056                 callp = &p->p_sysent->sv_table[0];
 1057         else
 1058                 callp = &p->p_sysent->sv_table[code];
 1059 
 1060         narg = callp->sy_narg;
 1061 
 1062         /*
 1063          * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
 1064          */
 1065         if (params != NULL && narg != 0)
 1066                 error = copyin(params, (caddr_t)args,
 1067                     (u_int)(narg * sizeof(int)));
 1068         else
 1069                 error = 0;
 1070                 
 1071 #ifdef KTRACE
 1072         if (KTRPOINT(td, KTR_SYSCALL))
 1073                 ktrsyscall(code, narg, args);
 1074 #endif
 1075 
 1076         CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
 1077             td->td_proc->p_pid, td->td_proc->p_comm, code);
 1078 
 1079         td->td_syscalls++;
 1080 
 1081         if (error == 0) {
 1082                 td->td_retval[0] = 0;
 1083                 td->td_retval[1] = frame->tf_edx;
 1084 
 1085                 STOPEVENT(p, S_SCE, narg);
 1086 
 1087                 PTRACESTOP_SC(p, td, S_PT_SCE);
 1088 
 1089 #ifdef KDTRACE_HOOKS
 1090                 /*
 1091                  * If the systrace module has registered it's probe
 1092                  * callback and if there is a probe active for the
 1093                  * syscall 'entry', process the probe.
 1094                  */
 1095                 if (systrace_probe_func != NULL && callp->sy_entry != 0)
 1096                         (*systrace_probe_func)(callp->sy_entry, code, callp,
 1097                             args);
 1098 #endif
 1099 
 1100                 AUDIT_SYSCALL_ENTER(code, td);
 1101                 error = (*callp->sy_call)(td, args);
 1102                 AUDIT_SYSCALL_EXIT(error, td);
 1103 
 1104                 /* Save the latest error return value. */
 1105                 td->td_errno = error;
 1106 
 1107 #ifdef KDTRACE_HOOKS
 1108                 /*
 1109                  * If the systrace module has registered it's probe
 1110                  * callback and if there is a probe active for the
 1111                  * syscall 'return', process the probe.
 1112                  */
 1113                 if (systrace_probe_func != NULL && callp->sy_return != 0)
 1114                         (*systrace_probe_func)(callp->sy_return, code, callp,
 1115                             args);
 1116 #endif
 1117         }
 1118 
 1119         switch (error) {
 1120         case 0:
 1121                 frame->tf_eax = td->td_retval[0];
 1122                 frame->tf_edx = td->td_retval[1];
 1123                 frame->tf_eflags &= ~PSL_C;
 1124                 break;
 1125 
 1126         case ERESTART:
 1127                 /*
 1128                  * Reconstruct pc, assuming lcall $X,y is 7 bytes,
 1129                  * int 0x80 is 2 bytes. We saved this in tf_err.
 1130                  */
 1131                 frame->tf_eip -= frame->tf_err;
 1132                 break;
 1133 
 1134         case EJUSTRETURN:
 1135                 break;
 1136 
 1137         default:
 1138                 if (p->p_sysent->sv_errsize) {
 1139                         if (error >= p->p_sysent->sv_errsize)
 1140                                 error = -1;     /* XXX */
 1141                         else
 1142                                 error = p->p_sysent->sv_errtbl[error];
 1143                 }
 1144                 frame->tf_eax = error;
 1145                 frame->tf_eflags |= PSL_C;
 1146                 break;
 1147         }
 1148 
 1149         /*
 1150          * Traced syscall.
 1151          */
 1152         if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
 1153                 frame->tf_eflags &= ~PSL_T;
 1154                 ksiginfo_init_trap(&ksi);
 1155                 ksi.ksi_signo = SIGTRAP;
 1156                 ksi.ksi_code = TRAP_TRACE;
 1157                 ksi.ksi_addr = (void *)frame->tf_eip;
 1158                 trapsignal(td, &ksi);
 1159         }
 1160 
 1161         /*
 1162          * Check for misbehavior.
 1163          */
 1164         WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
 1165             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
 1166         KASSERT(td->td_critnest == 0,
 1167             ("System call %s returning in a critical section",
 1168             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
 1169         KASSERT(td->td_locks == 0,
 1170             ("System call %s returning with %d locks held",
 1171             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
 1172             td->td_locks));
 1173 
 1174         /*
 1175          * Handle reschedule and other end-of-syscall issues
 1176          */
 1177         userret(td, frame);
 1178 
 1179         CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
 1180             td->td_proc->p_pid, td->td_proc->p_comm, code);
 1181 
 1182 #ifdef KTRACE
 1183         if (KTRPOINT(td, KTR_SYSRET))
 1184                 ktrsysret(code, error, td->td_retval[0]);
 1185 #endif
 1186 
 1187         /*
 1188          * This works because errno is findable through the
 1189          * register set.  If we ever support an emulation where this
 1190          * is not the case, this code will need to be revisited.
 1191          */
 1192         STOPEVENT(p, S_SCX, code);
 1193 
 1194         PTRACESTOP_SC(p, td, S_PT_SCX);
 1195 }
 1196 

Cache object: 6a1423934204a2b164d4d2581d263995


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