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

Cache object: 898b016cc77ccec6766349d0731d4b03


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