The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/i386/trap.c

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

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

Cache object: effc05f266187519b051f119898c9e9f


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