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/amd64/amd64/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/6.2/sys/amd64/amd64/trap.c 152905 2005-11-28 20:03:15Z jhb $");
   42 
   43 /*
   44  * AMD64 Trap and System call handling
   45  */
   46 
   47 #include "opt_clock.h"
   48 #include "opt_cpu.h"
   49 #include "opt_hwpmc_hooks.h"
   50 #include "opt_isa.h"
   51 #include "opt_kdb.h"
   52 #include "opt_ktrace.h"
   53 
   54 #include <sys/param.h>
   55 #include <sys/bus.h>
   56 #include <sys/systm.h>
   57 #include <sys/proc.h>
   58 #include <sys/pioctl.h>
   59 #include <sys/ptrace.h>
   60 #include <sys/kdb.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 #ifdef HWPMC_HOOKS
   76 #include <sys/pmckern.h>
   77 #endif
   78 
   79 #include <vm/vm.h>
   80 #include <vm/vm_param.h>
   81 #include <vm/pmap.h>
   82 #include <vm/vm_kern.h>
   83 #include <vm/vm_map.h>
   84 #include <vm/vm_page.h>
   85 #include <vm/vm_extern.h>
   86 
   87 #include <machine/cpu.h>
   88 #include <machine/intr_machdep.h>
   89 #include <machine/md_var.h>
   90 #include <machine/pcb.h>
   91 #ifdef SMP
   92 #include <machine/smp.h>
   93 #endif
   94 #include <machine/tss.h>
   95 
   96 extern void trap(struct trapframe frame);
   97 extern void syscall(struct trapframe frame);
   98 
   99 static int trap_pfault(struct trapframe *, int);
  100 static void trap_fatal(struct trapframe *, vm_offset_t);
  101 void dblfault_handler(void);
  102 
  103 #define MAX_TRAP_MSG            30
  104 static char *trap_msg[] = {
  105         "",                                     /*  0 unused */
  106         "privileged instruction fault",         /*  1 T_PRIVINFLT */
  107         "",                                     /*  2 unused */
  108         "breakpoint instruction fault",         /*  3 T_BPTFLT */
  109         "",                                     /*  4 unused */
  110         "",                                     /*  5 unused */
  111         "arithmetic trap",                      /*  6 T_ARITHTRAP */
  112         "",                                     /*  7 unused */
  113         "",                                     /*  8 unused */
  114         "general protection fault",             /*  9 T_PROTFLT */
  115         "trace trap",                           /* 10 T_TRCTRAP */
  116         "",                                     /* 11 unused */
  117         "page fault",                           /* 12 T_PAGEFLT */
  118         "",                                     /* 13 unused */
  119         "alignment fault",                      /* 14 T_ALIGNFLT */
  120         "",                                     /* 15 unused */
  121         "",                                     /* 16 unused */
  122         "",                                     /* 17 unused */
  123         "integer divide fault",                 /* 18 T_DIVIDE */
  124         "non-maskable interrupt trap",          /* 19 T_NMI */
  125         "overflow trap",                        /* 20 T_OFLOW */
  126         "FPU bounds check fault",               /* 21 T_BOUND */
  127         "FPU device not available",             /* 22 T_DNA */
  128         "double fault",                         /* 23 T_DOUBLEFLT */
  129         "FPU operand fetch fault",              /* 24 T_FPOPFLT */
  130         "invalid TSS fault",                    /* 25 T_TSSFLT */
  131         "segment not present fault",            /* 26 T_SEGNPFLT */
  132         "stack fault",                          /* 27 T_STKFLT */
  133         "machine check trap",                   /* 28 T_MCHK */
  134         "SIMD floating-point exception",        /* 29 T_XMMFLT */
  135         "reserved (unknown) fault",             /* 30 T_RESERVED */
  136 };
  137 
  138 #ifdef KDB
  139 static int kdb_on_nmi = 1;
  140 SYSCTL_INT(_machdep, OID_AUTO, kdb_on_nmi, CTLFLAG_RW,
  141         &kdb_on_nmi, 0, "Go to KDB on NMI");
  142 #endif
  143 static int panic_on_nmi = 1;
  144 SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
  145         &panic_on_nmi, 0, "Panic on NMI");
  146 
  147 #ifdef WITNESS
  148 extern char *syscallnames[];
  149 #endif
  150 
  151 /*
  152  * Exception, fault, and trap interface to the FreeBSD kernel.
  153  * This common code is called from assembly language IDT gate entry
  154  * routines that prepare a suitable stack frame, and restore this
  155  * frame after the exception has been processed.
  156  */
  157 
  158 void
  159 trap(frame)
  160         struct trapframe frame;
  161 {
  162         struct thread *td = curthread;
  163         struct proc *p = td->td_proc;
  164         u_int sticks = 0;
  165         int i = 0, ucode = 0, type, code;
  166 
  167         PCPU_LAZY_INC(cnt.v_trap);
  168         type = frame.tf_trapno;
  169 
  170 #ifdef KDB_STOP_NMI
  171         /* Handler for NMI IPIs used for debugging */
  172         if (type == T_NMI) {
  173                  if (ipi_nmi_handler() == 0)
  174                            goto out;
  175         }
  176 #endif /* KDB_STOP_NMI */
  177 
  178 #ifdef KDB
  179         if (kdb_active) {
  180                 kdb_reenter();
  181                 goto out;
  182         }
  183 #endif
  184 
  185 #ifdef  HWPMC_HOOKS
  186         /*
  187          * CPU PMCs interrupt using an NMI.  If the PMC module is
  188          * active, pass the 'rip' value to the PMC module's interrupt
  189          * handler.  A return value of '1' from the handler means that
  190          * the NMI was handled by it and we can return immediately.
  191          */
  192         if (type == T_NMI && pmc_intr &&
  193             (*pmc_intr)(PCPU_GET(cpuid), (uintptr_t) frame.tf_rip,
  194                 TRAPF_USERMODE(&frame)))
  195                 goto out;
  196 #endif
  197 
  198         if ((frame.tf_rflags & PSL_I) == 0) {
  199                 /*
  200                  * Buggy application or kernel code has disabled
  201                  * interrupts and then trapped.  Enabling interrupts
  202                  * now is wrong, but it is better than running with
  203                  * interrupts disabled until they are accidentally
  204                  * enabled later.
  205                  */
  206                 if (ISPL(frame.tf_cs) == SEL_UPL)
  207                         printf(
  208                             "pid %ld (%s): trap %d with interrupts disabled\n",
  209                             (long)curproc->p_pid, curproc->p_comm, type);
  210                 else if (type != T_NMI && type != T_BPTFLT &&
  211                     type != T_TRCTRAP) {
  212                         /*
  213                          * XXX not quite right, since this may be for a
  214                          * multiple fault in user mode.
  215                          */
  216                         printf("kernel trap %d with interrupts disabled\n",
  217                             type);
  218                         /*
  219                          * We shouldn't enable interrupts while in a critical
  220                          * section.
  221                          */
  222                         if (td->td_critnest == 0)
  223                                 enable_intr();
  224                 }
  225         }
  226 
  227         code = frame.tf_err;
  228         if (type == T_PAGEFLT) {
  229                 /*
  230                  * If we get a page fault while in a critical section, then
  231                  * it is most likely a fatal kernel page fault.  The kernel
  232                  * is already going to panic trying to get a sleep lock to
  233                  * do the VM lookup, so just consider it a fatal trap so the
  234                  * kernel can print out a useful trap message and even get
  235                  * to the debugger.
  236                  */
  237                 if (td->td_critnest != 0)
  238                         trap_fatal(&frame, frame.tf_addr);
  239         }
  240 
  241         if (ISPL(frame.tf_cs) == SEL_UPL) {
  242                 /* user trap */
  243 
  244                 sticks = td->td_sticks;
  245                 td->td_frame = &frame;
  246                 if (td->td_ucred != p->p_ucred) 
  247                         cred_update_thread(td);
  248 
  249                 switch (type) {
  250                 case T_PRIVINFLT:       /* privileged instruction fault */
  251                         ucode = type;
  252                         i = SIGILL;
  253                         break;
  254 
  255                 case T_BPTFLT:          /* bpt instruction fault */
  256                 case T_TRCTRAP:         /* trace trap */
  257                         enable_intr();
  258                         frame.tf_rflags &= ~PSL_T;
  259                         i = SIGTRAP;
  260                         break;
  261 
  262                 case T_ARITHTRAP:       /* arithmetic trap */
  263                         ucode = fputrap();
  264                         if (ucode == -1)
  265                                 goto userout;
  266                         i = SIGFPE;
  267                         break;
  268 
  269                 case T_PROTFLT:         /* general protection fault */
  270                 case T_STKFLT:          /* stack fault */
  271                 case T_SEGNPFLT:        /* segment not present fault */
  272                 case T_TSSFLT:          /* invalid TSS fault */
  273                 case T_DOUBLEFLT:       /* double fault */
  274                 default:
  275                         ucode = code + BUS_SEGM_FAULT ;
  276                         i = SIGBUS;
  277                         break;
  278 
  279                 case T_PAGEFLT:         /* page fault */
  280                         if (td->td_pflags & TDP_SA)
  281                                 thread_user_enter(td);
  282                         i = trap_pfault(&frame, TRUE);
  283                         if (i == -1)
  284                                 goto userout;
  285                         if (i == 0)
  286                                 goto user;
  287 
  288                         ucode = T_PAGEFLT;
  289                         break;
  290 
  291                 case T_DIVIDE:          /* integer divide fault */
  292                         ucode = FPE_INTDIV;
  293                         i = SIGFPE;
  294                         break;
  295 
  296 #ifdef DEV_ISA
  297                 case T_NMI:
  298                         /* machine/parity/power fail/"kitchen sink" faults */
  299                         /* XXX Giant */
  300                         if (isa_nmi(code) == 0) {
  301 #ifdef KDB
  302                                 /*
  303                                  * NMI can be hooked up to a pushbutton
  304                                  * for debugging.
  305                                  */
  306                                 if (kdb_on_nmi) {
  307                                         printf ("NMI ... going to debugger\n");
  308                                         kdb_trap(type, 0, &frame);
  309                                 }
  310 #endif /* KDB */
  311                                 goto userout;
  312                         } else if (panic_on_nmi)
  313                                 panic("NMI indicates hardware failure");
  314                         break;
  315 #endif /* DEV_ISA */
  316 
  317                 case T_OFLOW:           /* integer overflow fault */
  318                         ucode = FPE_INTOVF;
  319                         i = SIGFPE;
  320                         break;
  321 
  322                 case T_BOUND:           /* bounds check fault */
  323                         ucode = FPE_FLTSUB;
  324                         i = SIGFPE;
  325                         break;
  326 
  327                 case T_DNA:
  328                         /* transparent fault (due to context switch "late") */
  329                         if (fpudna())
  330                                 goto userout;
  331                         i = SIGFPE;
  332                         ucode = FPE_FPU_NP_TRAP;
  333                         break;
  334 
  335                 case T_FPOPFLT:         /* FPU operand fetch fault */
  336                         ucode = T_FPOPFLT;
  337                         i = SIGILL;
  338                         break;
  339 
  340                 case T_XMMFLT:          /* SIMD floating-point exception */
  341                         ucode = 0; /* XXX */
  342                         i = SIGFPE;
  343                         break;
  344                 }
  345         } else {
  346                 /* kernel trap */
  347 
  348                 KASSERT(cold || td->td_ucred != NULL,
  349                     ("kernel trap doesn't have ucred"));
  350                 switch (type) {
  351                 case T_PAGEFLT:                 /* page fault */
  352                         (void) trap_pfault(&frame, FALSE);
  353                         goto out;
  354 
  355                 case T_DNA:
  356                         /*
  357                          * The kernel is apparently using fpu for copying.
  358                          * XXX this should be fatal unless the kernel has
  359                          * registered such use.
  360                          */
  361                         if (fpudna()) {
  362                                 printf("fpudna in kernel mode!\n");
  363                                 goto out;
  364                         }
  365                         break;
  366 
  367                 case T_STKFLT:          /* stack fault */
  368                         break;
  369 
  370                 case T_PROTFLT:         /* general protection fault */
  371                 case T_SEGNPFLT:        /* segment not present fault */
  372                         if (td->td_intr_nesting_level != 0)
  373                                 break;
  374 
  375                         /*
  376                          * Invalid segment selectors and out of bounds
  377                          * %rip's and %rsp's can be set up in user mode.
  378                          * This causes a fault in kernel mode when the
  379                          * kernel tries to return to user mode.  We want
  380                          * to get this fault so that we can fix the
  381                          * problem here and not have to check all the
  382                          * selectors and pointers when the user changes
  383                          * them.
  384                          */
  385                         if (frame.tf_rip == (long)doreti_iret) {
  386                                 frame.tf_rip = (long)doreti_iret_fault;
  387                                 goto out;
  388                         }
  389                         if (PCPU_GET(curpcb)->pcb_onfault != NULL) {
  390                                 frame.tf_rip =
  391                                     (long)PCPU_GET(curpcb)->pcb_onfault;
  392                                 goto out;
  393                         }
  394                         break;
  395 
  396                 case T_TSSFLT:
  397                         /*
  398                          * PSL_NT can be set in user mode and isn't cleared
  399                          * automatically when the kernel is entered.  This
  400                          * causes a TSS fault when the kernel attempts to
  401                          * `iret' because the TSS link is uninitialized.  We
  402                          * want to get this fault so that we can fix the
  403                          * problem here and not every time the kernel is
  404                          * entered.
  405                          */
  406                         if (frame.tf_rflags & PSL_NT) {
  407                                 frame.tf_rflags &= ~PSL_NT;
  408                                 goto out;
  409                         }
  410                         break;
  411 
  412                 case T_TRCTRAP:  /* trace trap */
  413                         /*
  414                          * Ignore debug register trace traps due to
  415                          * accesses in the user's address space, which
  416                          * can happen under several conditions such as
  417                          * if a user sets a watchpoint on a buffer and
  418                          * then passes that buffer to a system call.
  419                          * We still want to get TRCTRAPS for addresses
  420                          * in kernel space because that is useful when
  421                          * debugging the kernel.
  422                          */
  423                         if (user_dbreg_trap()) {
  424                                 /*
  425                                  * Reset breakpoint bits because the
  426                                  * processor doesn't
  427                                  */
  428                                 /* XXX check upper bits here */
  429                                 load_dr6(rdr6() & 0xfffffff0);
  430                                 goto out;
  431                         }
  432                         /*
  433                          * FALLTHROUGH (TRCTRAP kernel mode, kernel address)
  434                          */
  435                 case T_BPTFLT:
  436                         /*
  437                          * If KDB is enabled, let it handle the debugger trap.
  438                          * Otherwise, debugger traps "can't happen".
  439                          */
  440 #ifdef KDB
  441                         /* XXX Giant */
  442                         if (kdb_trap(type, 0, &frame))
  443                                 goto out;
  444 #endif
  445                         break;
  446 
  447 #ifdef DEV_ISA
  448                 case T_NMI:
  449                         /* XXX Giant */
  450                         /* machine/parity/power fail/"kitchen sink" faults */
  451                         if (isa_nmi(code) == 0) {
  452 #ifdef KDB
  453                                 /*
  454                                  * NMI can be hooked up to a pushbutton
  455                                  * for debugging.
  456                                  */
  457                                 if (kdb_on_nmi) {
  458                                         printf ("NMI ... going to debugger\n");
  459                                         kdb_trap(type, 0, &frame);
  460                                 }
  461 #endif /* KDB */
  462                                 goto out;
  463                         } else if (panic_on_nmi == 0)
  464                                 goto out;
  465                         /* FALLTHROUGH */
  466 #endif /* DEV_ISA */
  467                 }
  468 
  469                 trap_fatal(&frame, 0);
  470                 goto out;
  471         }
  472 
  473         /* Translate fault for emulators (e.g. Linux) */
  474         if (*p->p_sysent->sv_transtrap)
  475                 i = (*p->p_sysent->sv_transtrap)(i, type);
  476 
  477         trapsignal(td, i, ucode);
  478 
  479 #ifdef DEBUG
  480         if (type <= MAX_TRAP_MSG) {
  481                 uprintf("fatal process exception: %s",
  482                         trap_msg[type]);
  483                 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
  484                         uprintf(", fault VA = 0x%lx", frame.tf_addr);
  485                 uprintf("\n");
  486         }
  487 #endif
  488 
  489 user:
  490         userret(td, &frame, sticks);
  491         mtx_assert(&Giant, MA_NOTOWNED);
  492 userout:
  493 out:
  494         return;
  495 }
  496 
  497 static int
  498 trap_pfault(frame, usermode)
  499         struct trapframe *frame;
  500         int usermode;
  501 {
  502         vm_offset_t va;
  503         struct vmspace *vm = NULL;
  504         vm_map_t map = 0;
  505         int rv = 0;
  506         vm_prot_t ftype;
  507         struct thread *td = curthread;
  508         struct proc *p = td->td_proc;
  509         vm_offset_t eva = frame->tf_addr;
  510 
  511         va = trunc_page(eva);
  512         if (va >= KERNBASE) {
  513                 /*
  514                  * Don't allow user-mode faults in kernel address space.
  515                  */
  516                 if (usermode)
  517                         goto nogo;
  518 
  519                 map = kernel_map;
  520         } else {
  521                 /*
  522                  * This is a fault on non-kernel virtual memory.
  523                  * vm is initialized above to NULL. If curproc is NULL
  524                  * or curproc->p_vmspace is NULL the fault is fatal.
  525                  */
  526                 if (p != NULL)
  527                         vm = p->p_vmspace;
  528 
  529                 if (vm == NULL)
  530                         goto nogo;
  531 
  532                 map = &vm->vm_map;
  533         }
  534 
  535         if (frame->tf_err & PGEX_W)
  536                 ftype = VM_PROT_WRITE;
  537         else
  538                 ftype = VM_PROT_READ;
  539 
  540         if (map != kernel_map) {
  541                 /*
  542                  * Keep swapout from messing with us during this
  543                  *      critical time.
  544                  */
  545                 PROC_LOCK(p);
  546                 ++p->p_lock;
  547                 PROC_UNLOCK(p);
  548 
  549                 /* Fault in the user page: */
  550                 rv = vm_fault(map, va, ftype,
  551                               (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
  552                                                       : VM_FAULT_NORMAL);
  553 
  554                 PROC_LOCK(p);
  555                 --p->p_lock;
  556                 PROC_UNLOCK(p);
  557         } else {
  558                 /*
  559                  * Don't have to worry about process locking or stacks in the
  560                  * kernel.
  561                  */
  562                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
  563         }
  564         if (rv == KERN_SUCCESS)
  565                 return (0);
  566 nogo:
  567         if (!usermode) {
  568                 if (td->td_intr_nesting_level == 0 &&
  569                     PCPU_GET(curpcb)->pcb_onfault != NULL) {
  570                         frame->tf_rip = (long)PCPU_GET(curpcb)->pcb_onfault;
  571                         return (0);
  572                 }
  573                 trap_fatal(frame, eva);
  574                 return (-1);
  575         }
  576 
  577         return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
  578 }
  579 
  580 static void
  581 trap_fatal(frame, eva)
  582         struct trapframe *frame;
  583         vm_offset_t eva;
  584 {
  585         int code, type, ss;
  586         long esp;
  587         struct soft_segment_descriptor softseg;
  588         char *msg;
  589 
  590         code = frame->tf_err;
  591         type = frame->tf_trapno;
  592         sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)], &softseg);
  593 
  594         if (type <= MAX_TRAP_MSG)
  595                 msg = trap_msg[type];
  596         else
  597                 msg = "UNKNOWN";
  598         printf("\n\nFatal trap %d: %s while in %s mode\n", type, msg,
  599             ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
  600 #ifdef SMP
  601         /* two separate prints in case of a trap on an unmapped page */
  602         printf("cpuid = %d; ", PCPU_GET(cpuid));
  603         printf("apic id = %02x\n", PCPU_GET(apic_id));
  604 #endif
  605         if (type == T_PAGEFLT) {
  606                 printf("fault virtual address   = 0x%lx\n", eva);
  607                 printf("fault code              = %s %s, %s\n",
  608                         code & PGEX_U ? "user" : "supervisor",
  609                         code & PGEX_W ? "write" : "read",
  610                         code & PGEX_P ? "protection violation" : "page not present");
  611         }
  612         printf("instruction pointer     = 0x%lx:0x%lx\n",
  613                frame->tf_cs & 0xffff, frame->tf_rip);
  614         if (ISPL(frame->tf_cs) == SEL_UPL) {
  615                 ss = frame->tf_ss & 0xffff;
  616                 esp = frame->tf_rsp;
  617         } else {
  618                 ss = GSEL(GDATA_SEL, SEL_KPL);
  619                 esp = (long)&frame->tf_rsp;
  620         }
  621         printf("stack pointer           = 0x%x:0x%lx\n", ss, esp);
  622         printf("frame pointer           = 0x%x:0x%lx\n", ss, frame->tf_rbp);
  623         printf("code segment            = base 0x%lx, limit 0x%lx, type 0x%x\n",
  624                softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
  625         printf("                        = DPL %d, pres %d, long %d, def32 %d, gran %d\n",
  626                softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_long, softseg.ssd_def32,
  627                softseg.ssd_gran);
  628         printf("processor eflags        = ");
  629         if (frame->tf_rflags & PSL_T)
  630                 printf("trace trap, ");
  631         if (frame->tf_rflags & PSL_I)
  632                 printf("interrupt enabled, ");
  633         if (frame->tf_rflags & PSL_NT)
  634                 printf("nested task, ");
  635         if (frame->tf_rflags & PSL_RF)
  636                 printf("resume, ");
  637         printf("IOPL = %ld\n", (frame->tf_rflags & PSL_IOPL) >> 12);
  638         printf("current process         = ");
  639         if (curproc) {
  640                 printf("%lu (%s)\n",
  641                     (u_long)curproc->p_pid, curproc->p_comm ?
  642                     curproc->p_comm : "");
  643         } else {
  644                 printf("Idle\n");
  645         }
  646 
  647 #ifdef KDB
  648         if (debugger_on_panic || kdb_active) {
  649                 register_t rflags;
  650                 rflags = intr_disable();
  651                 if (kdb_trap(type, 0, frame)) {
  652                         intr_restore(rflags);
  653                         return;
  654                 }
  655                 intr_restore(rflags);
  656         }
  657 #endif
  658         printf("trap number             = %d\n", type);
  659         if (type <= MAX_TRAP_MSG)
  660                 panic("%s", trap_msg[type]);
  661         else
  662                 panic("unknown/reserved trap");
  663 }
  664 
  665 /*
  666  * Double fault handler. Called when a fault occurs while writing
  667  * a frame for a trap/exception onto the stack. This usually occurs
  668  * when the stack overflows (such is the case with infinite recursion,
  669  * for example).
  670  */
  671 void
  672 dblfault_handler()
  673 {
  674         printf("\nFatal double fault\n");
  675 #ifdef SMP
  676         /* two separate prints in case of a trap on an unmapped page */
  677         printf("cpuid = %d; ", PCPU_GET(cpuid));
  678         printf("apic id = %02x\n", PCPU_GET(apic_id));
  679 #endif
  680         panic("double fault");
  681 }
  682 
  683 /*
  684  *      syscall -       system call request C handler
  685  *
  686  *      A system call is essentially treated as a trap.
  687  */
  688 void
  689 syscall(frame)
  690         struct trapframe frame;
  691 {
  692         caddr_t params;
  693         struct sysent *callp;
  694         struct thread *td = curthread;
  695         struct proc *p = td->td_proc;
  696         register_t orig_tf_rflags;
  697         u_int sticks;
  698         int error;
  699         int narg;
  700         register_t args[8];
  701         register_t *argp;
  702         u_int code;
  703         int reg, regcnt;
  704 
  705         /*
  706          * note: PCPU_LAZY_INC() can only be used if we can afford
  707          * occassional inaccuracy in the count.
  708          */
  709         PCPU_LAZY_INC(cnt.v_syscall);
  710 
  711 #ifdef DIAGNOSTIC
  712         if (ISPL(frame.tf_cs) != SEL_UPL) {
  713                 mtx_lock(&Giant);       /* try to stabilize the system XXX */
  714                 panic("syscall");
  715                 /* NOT REACHED */
  716                 mtx_unlock(&Giant);
  717         }
  718 #endif
  719 
  720         reg = 0;
  721         regcnt = 6;
  722         sticks = td->td_sticks;
  723         td->td_frame = &frame;
  724         if (td->td_ucred != p->p_ucred) 
  725                 cred_update_thread(td);
  726         if (p->p_flag & P_SA)
  727                 thread_user_enter(td);
  728         params = (caddr_t)frame.tf_rsp + sizeof(register_t);
  729         code = frame.tf_rax;
  730         orig_tf_rflags = frame.tf_rflags;
  731 
  732         if (p->p_sysent->sv_prepsyscall) {
  733                 /*
  734                  * The prep code is MP aware.
  735                  */
  736                 (*p->p_sysent->sv_prepsyscall)(&frame, (int *)args, &code, &params);
  737         } else {
  738                 if (code == SYS_syscall || code == SYS___syscall) {
  739                         code = frame.tf_rdi;
  740                         reg++;
  741                         regcnt--;
  742                 }
  743         }
  744 
  745         if (p->p_sysent->sv_mask)
  746                 code &= p->p_sysent->sv_mask;
  747 
  748         if (code >= p->p_sysent->sv_size)
  749                 callp = &p->p_sysent->sv_table[0];
  750         else
  751                 callp = &p->p_sysent->sv_table[code];
  752 
  753         narg = callp->sy_narg & SYF_ARGMASK;
  754 
  755         /*
  756          * copyin and the ktrsyscall()/ktrsysret() code is MP-aware
  757          */
  758         KASSERT(narg <= sizeof(args) / sizeof(args[0]),
  759             ("Too many syscall arguments!"));
  760         error = 0;
  761         argp = &frame.tf_rdi;
  762         argp += reg;
  763         bcopy(argp, args, sizeof(args[0]) * regcnt);
  764         if (narg > regcnt) {
  765                 KASSERT(params != NULL, ("copyin args with no params!"));
  766                 error = copyin(params, &args[regcnt],
  767                         (narg - regcnt) * sizeof(args[0]));
  768         }
  769         argp = &args[0];
  770 
  771 #ifdef KTRACE
  772         if (KTRPOINT(td, KTR_SYSCALL))
  773                 ktrsyscall(code, narg, argp);
  774 #endif
  775 
  776         CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
  777             td->td_proc->p_pid, td->td_proc->p_comm, code);
  778 
  779         if (error == 0) {
  780                 td->td_retval[0] = 0;
  781                 td->td_retval[1] = frame.tf_rdx;
  782 
  783                 STOPEVENT(p, S_SCE, narg);
  784 
  785                 PTRACESTOP_SC(p, td, S_PT_SCE);
  786 
  787                 if ((callp->sy_narg & SYF_MPSAFE) == 0) {
  788                         mtx_lock(&Giant);
  789                         error = (*callp->sy_call)(td, argp);
  790                         mtx_unlock(&Giant);
  791                 } else
  792                         error = (*callp->sy_call)(td, argp);
  793         }
  794 
  795         switch (error) {
  796         case 0:
  797                 frame.tf_rax = td->td_retval[0];
  798                 frame.tf_rdx = td->td_retval[1];
  799                 frame.tf_rflags &= ~PSL_C;
  800                 break;
  801 
  802         case ERESTART:
  803                 /*
  804                  * Reconstruct pc, we know that 'syscall' is 2 bytes.
  805                  * We have to do a full context restore so that %r10
  806                  * (which was holding the value of %rcx) is restored for
  807                  * the next iteration.
  808                  */
  809                 frame.tf_rip -= frame.tf_err;
  810                 frame.tf_r10 = frame.tf_rcx;
  811                 td->td_pcb->pcb_flags |= PCB_FULLCTX;
  812                 break;
  813 
  814         case EJUSTRETURN:
  815                 break;
  816 
  817         default:
  818                 if (p->p_sysent->sv_errsize) {
  819                         if (error >= p->p_sysent->sv_errsize)
  820                                 error = -1;     /* XXX */
  821                         else
  822                                 error = p->p_sysent->sv_errtbl[error];
  823                 }
  824                 frame.tf_rax = error;
  825                 frame.tf_rflags |= PSL_C;
  826                 break;
  827         }
  828 
  829         /*
  830          * Traced syscall.
  831          */
  832         if (orig_tf_rflags & PSL_T) {
  833                 frame.tf_rflags &= ~PSL_T;
  834                 trapsignal(td, SIGTRAP, 0);
  835         }
  836 
  837         /*
  838          * Handle reschedule and other end-of-syscall issues
  839          */
  840         userret(td, &frame, sticks);
  841 
  842         CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
  843             td->td_proc->p_pid, td->td_proc->p_comm, code);
  844 
  845 #ifdef KTRACE
  846         if (KTRPOINT(td, KTR_SYSRET))
  847                 ktrsysret(code, error, td->td_retval[0]);
  848 #endif
  849 
  850         /*
  851          * This works because errno is findable through the
  852          * register set.  If we ever support an emulation where this
  853          * is not the case, this code will need to be revisited.
  854          */
  855         STOPEVENT(p, S_SCX, code);
  856 
  857         PTRACESTOP_SC(p, td, S_PT_SCX);
  858 
  859         WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
  860             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
  861         mtx_assert(&sched_lock, MA_NOTOWNED);
  862         mtx_assert(&Giant, MA_NOTOWNED);
  863 }

Cache object: 449eba4511a8bfcca18fca3fcb749a1f


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