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

Cache object: 3bfe0643d6b46e080304ac065bbad9ec


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