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

Cache object: 315373f2eeb15df086180b02e141bd16


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