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/exception.s

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) 1989, 1990 William F. Jolitz.
    3  * Copyright (c) 1990 The Regents of the University of California.
    4  * Copyright (c) 2007 The FreeBSD Foundation
    5  * All rights reserved.
    6  *
    7  * Portions of this software were developed by A. Joseph Koshy under
    8  * sponsorship from the FreeBSD Foundation and Google, Inc.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  * $FreeBSD$
   35  */
   36 
   37 #include "opt_apic.h"
   38 #include "opt_atpic.h"
   39 #include "opt_hwpmc_hooks.h"
   40 #include "opt_kdtrace.h"
   41 #include "opt_npx.h"
   42 
   43 #include <machine/asmacros.h>
   44 #include <machine/psl.h>
   45 #include <machine/trap.h>
   46 
   47 #include "assym.s"
   48 
   49 #define SEL_RPL_MASK    0x0003
   50 #define GSEL_KPL        0x0020  /* GSEL(GCODE_SEL, SEL_KPL) */
   51 
   52 #ifdef KDTRACE_HOOKS
   53         .bss
   54         .globl  dtrace_invop_jump_addr
   55         .align  4
   56         .type   dtrace_invop_jump_addr, @object
   57         .size   dtrace_invop_jump_addr, 4
   58 dtrace_invop_jump_addr:
   59         .zero   4
   60         .globl  dtrace_invop_calltrap_addr
   61         .align  4
   62         .type   dtrace_invop_calltrap_addr, @object
   63         .size   dtrace_invop_calltrap_addr, 4
   64 dtrace_invop_calltrap_addr:
   65         .zero   8
   66 #endif
   67         .text
   68 #ifdef HWPMC_HOOKS
   69         ENTRY(start_exceptions)
   70 #endif
   71 /*****************************************************************************/
   72 /* Trap handling                                                             */
   73 /*****************************************************************************/
   74 /*
   75  * Trap and fault vector routines.
   76  *
   77  * Most traps are 'trap gates', SDT_SYS386TGT.  A trap gate pushes state on
   78  * the stack that mostly looks like an interrupt, but does not disable
   79  * interrupts.  A few of the traps we are use are interrupt gates,
   80  * SDT_SYS386IGT, which are nearly the same thing except interrupts are
   81  * disabled on entry.
   82  *
   83  * The cpu will push a certain amount of state onto the kernel stack for
   84  * the current process.  The amount of state depends on the type of trap
   85  * and whether the trap crossed rings or not.  See i386/include/frame.h.
   86  * At the very least the current EFLAGS (status register, which includes
   87  * the interrupt disable state prior to the trap), the code segment register,
   88  * and the return instruction pointer are pushed by the cpu.  The cpu
   89  * will also push an 'error' code for certain traps.  We push a dummy
   90  * error code for those traps where the cpu doesn't in order to maintain
   91  * a consistent frame.  We also push a contrived 'trap number'.
   92  *
   93  * The cpu does not push the general registers, we must do that, and we
   94  * must restore them prior to calling 'iret'.  The cpu adjusts the %cs and
   95  * %ss segment registers, but does not mess with %ds, %es, or %fs.  Thus we
   96  * must load them with appropriate values for supervisor mode operation.
   97  */
   98 
   99 MCOUNT_LABEL(user)
  100 MCOUNT_LABEL(btrap)
  101 
  102 #define TRAP(a)         pushl $(a) ; jmp alltraps
  103 
  104 IDTVEC(div)
  105         pushl $0; TRAP(T_DIVIDE)
  106 IDTVEC(dbg)
  107         pushl $0; TRAP(T_TRCTRAP)
  108 IDTVEC(nmi)
  109         pushl $0; TRAP(T_NMI)
  110 IDTVEC(bpt)
  111         pushl $0; TRAP(T_BPTFLT)
  112 IDTVEC(dtrace_ret)
  113         pushl $0; TRAP(T_DTRACE_RET)
  114 IDTVEC(ofl)
  115         pushl $0; TRAP(T_OFLOW)
  116 IDTVEC(bnd)
  117         pushl $0; TRAP(T_BOUND)
  118 #ifndef KDTRACE_HOOKS
  119 IDTVEC(ill)
  120         pushl $0; TRAP(T_PRIVINFLT)
  121 #endif
  122 IDTVEC(dna)
  123         pushl $0; TRAP(T_DNA)
  124 IDTVEC(fpusegm)
  125         pushl $0; TRAP(T_FPOPFLT)
  126 IDTVEC(tss)
  127         TRAP(T_TSSFLT)
  128 IDTVEC(missing)
  129         TRAP(T_SEGNPFLT)
  130 IDTVEC(stk)
  131         TRAP(T_STKFLT)
  132 IDTVEC(prot)
  133         TRAP(T_PROTFLT)
  134 IDTVEC(page)
  135         TRAP(T_PAGEFLT)
  136 IDTVEC(mchk)
  137         pushl $0; TRAP(T_MCHK)
  138 IDTVEC(rsvd)
  139         pushl $0; TRAP(T_RESERVED)
  140 IDTVEC(fpu)
  141         pushl $0; TRAP(T_ARITHTRAP)
  142 IDTVEC(align)
  143         TRAP(T_ALIGNFLT)
  144 IDTVEC(xmm)
  145         pushl $0; TRAP(T_XMMFLT)
  146 
  147         /*
  148          * All traps except ones for syscalls jump to alltraps.  If
  149          * interrupts were enabled when the trap occurred, then interrupts
  150          * are enabled now if the trap was through a trap gate, else
  151          * disabled if the trap was through an interrupt gate.  Note that
  152          * int0x80_syscall is a trap gate.   Interrupt gates are used by
  153          * page faults, non-maskable interrupts, debug and breakpoint
  154          * exceptions.
  155          */
  156         SUPERALIGN_TEXT
  157         .globl  alltraps
  158         .type   alltraps,@function
  159 alltraps:
  160         pushal
  161         pushl   $0
  162         movw    %ds,(%esp)
  163         pushl   $0
  164         movw    %es,(%esp)
  165         pushl   $0
  166         movw    %fs,(%esp)
  167 alltraps_with_regs_pushed:
  168         SET_KERNEL_SREGS
  169         cld
  170         FAKE_MCOUNT(TF_EIP(%esp))
  171 calltrap:
  172         pushl   %esp
  173         call    trap
  174         add     $4, %esp
  175 
  176         /*
  177          * Return via doreti to handle ASTs.
  178          */
  179         MEXITCOUNT
  180         jmp     doreti
  181 
  182 /*
  183  * Privileged instruction fault.
  184  */
  185 #ifdef KDTRACE_HOOKS
  186         SUPERALIGN_TEXT
  187 IDTVEC(ill)
  188         /* Check if there is no DTrace hook registered. */
  189         cmpl    $0,dtrace_invop_jump_addr
  190         je      norm_ill
  191 
  192         /* Check if this is a user fault. */
  193         cmpl    $GSEL_KPL, 4(%esp)      /* Check the code segment. */
  194 
  195         /* If so, just handle it as a normal trap. */
  196         jne     norm_ill
  197 
  198         /*
  199          * This is a kernel instruction fault that might have been caused
  200          * by a DTrace provider.
  201          */
  202         pushal                          /* Push all registers onto the stack. */
  203 
  204         /*
  205          * Set our jump address for the jump back in the event that
  206          * the exception wasn't caused by DTrace at all.
  207          */
  208         movl    $norm_ill, dtrace_invop_calltrap_addr
  209 
  210         /* Jump to the code hooked in by DTrace. */
  211         jmpl    *dtrace_invop_jump_addr
  212 
  213         /*
  214          * Process the instruction fault in the normal way.
  215          */
  216 norm_ill:
  217         pushl $0
  218         TRAP(T_PRIVINFLT)
  219 #endif
  220 
  221 /*
  222  * Call gate entry for syscalls (lcall 7,0).
  223  * This is used by FreeBSD 1.x a.out executables and "old" NetBSD executables.
  224  *
  225  * The intersegment call has been set up to specify one dummy parameter.
  226  * This leaves a place to put eflags so that the call frame can be
  227  * converted to a trap frame. Note that the eflags is (semi-)bogusly
  228  * pushed into (what will be) tf_err and then copied later into the
  229  * final spot. It has to be done this way because esp can't be just
  230  * temporarily altered for the pushfl - an interrupt might come in
  231  * and clobber the saved cs/eip.
  232  */
  233         SUPERALIGN_TEXT
  234 IDTVEC(lcall_syscall)
  235         pushfl                          /* save eflags */
  236         popl    8(%esp)                 /* shuffle into tf_eflags */
  237         pushl   $7                      /* sizeof "lcall 7,0" */
  238         pushl   $0                      /* tf_trapno */
  239         pushal
  240         pushl   $0
  241         movw    %ds,(%esp)
  242         pushl   $0
  243         movw    %es,(%esp)
  244         pushl   $0
  245         movw    %fs,(%esp)
  246         SET_KERNEL_SREGS
  247         cld
  248         FAKE_MCOUNT(TF_EIP(%esp))
  249         pushl   %esp
  250         call    syscall
  251         add     $4, %esp
  252         MEXITCOUNT
  253         jmp     doreti
  254 
  255 /*
  256  * Trap gate entry for syscalls (int 0x80).
  257  * This is used by FreeBSD ELF executables, "new" NetBSD executables, and all
  258  * Linux executables.
  259  *
  260  * Even though the name says 'int0x80', this is actually a trap gate, not an
  261  * interrupt gate.  Thus interrupts are enabled on entry just as they are for
  262  * a normal syscall.
  263  */
  264         SUPERALIGN_TEXT
  265 IDTVEC(int0x80_syscall)
  266         pushl   $2                      /* sizeof "int 0x80" */
  267         pushl   $0                      /* tf_trapno */
  268         pushal
  269         pushl   $0
  270         movw    %ds,(%esp)
  271         pushl   $0
  272         movw    %es,(%esp)
  273         pushl   $0
  274         movw    %fs,(%esp)
  275         SET_KERNEL_SREGS
  276         cld
  277         FAKE_MCOUNT(TF_EIP(%esp))
  278         pushl   %esp
  279         call    syscall
  280         add     $4, %esp
  281         MEXITCOUNT
  282         jmp     doreti
  283 
  284 ENTRY(fork_trampoline)
  285         pushl   %esp                    /* trapframe pointer */
  286         pushl   %ebx                    /* arg1 */
  287         pushl   %esi                    /* function */
  288         call    fork_exit
  289         addl    $12,%esp
  290         /* cut from syscall */
  291 
  292         /*
  293          * Return via doreti to handle ASTs.
  294          */
  295         MEXITCOUNT
  296         jmp     doreti
  297 
  298 
  299 /*
  300  * To efficiently implement classification of trap and interrupt handlers
  301  * for profiling, there must be only trap handlers between the labels btrap
  302  * and bintr, and only interrupt handlers between the labels bintr and
  303  * eintr.  This is implemented (partly) by including files that contain
  304  * some of the handlers.  Before including the files, set up a normal asm
  305  * environment so that the included files doen't need to know that they are
  306  * included.
  307  */
  308 
  309         .data
  310         .p2align 4
  311         .text
  312         SUPERALIGN_TEXT
  313 MCOUNT_LABEL(bintr)
  314 
  315 #ifdef DEV_ATPIC
  316 #include <i386/i386/atpic_vector.s>
  317 #endif
  318 
  319 #if defined(DEV_APIC) && defined(DEV_ATPIC)
  320         .data
  321         .p2align 4
  322         .text
  323         SUPERALIGN_TEXT
  324 #endif
  325 
  326 #ifdef DEV_APIC
  327 #include <i386/i386/apic_vector.s>
  328 #endif
  329 
  330         .data
  331         .p2align 4
  332         .text
  333         SUPERALIGN_TEXT
  334 #include <i386/i386/vm86bios.s>
  335 
  336         .text
  337 MCOUNT_LABEL(eintr)
  338 
  339 /*
  340  * void doreti(struct trapframe)
  341  *
  342  * Handle return from interrupts, traps and syscalls.
  343  */
  344         .text
  345         SUPERALIGN_TEXT
  346         .type   doreti,@function
  347 doreti:
  348         FAKE_MCOUNT($bintr)             /* init "from" bintr -> doreti */
  349 doreti_next:
  350         /*
  351          * Check if ASTs can be handled now.  ASTs cannot be safely
  352          * processed when returning from an NMI.
  353          */
  354         cmpb    $T_NMI,TF_TRAPNO(%esp)
  355 #ifdef HWPMC_HOOKS
  356         je      doreti_nmi
  357 #else
  358         je      doreti_exit
  359 #endif
  360         /*
  361          * PSL_VM must be checked first since segment registers only
  362          * have an RPL in non-VM86 mode.
  363          * ASTs can not be handled now if we are in a vm86 call.
  364          */
  365         testl   $PSL_VM,TF_EFLAGS(%esp)
  366         jz      doreti_notvm86
  367         movl    PCPU(CURPCB),%ecx
  368         testl   $PCB_VM86CALL,PCB_FLAGS(%ecx)
  369         jz      doreti_ast
  370         jmp     doreti_exit
  371 
  372 doreti_notvm86:
  373         testb   $SEL_RPL_MASK,TF_CS(%esp) /* are we returning to user mode? */
  374         jz      doreti_exit             /* can't handle ASTs now if not */
  375 
  376 doreti_ast:
  377         /*
  378          * Check for ASTs atomically with returning.  Disabling CPU
  379          * interrupts provides sufficient locking even in the SMP case,
  380          * since we will be informed of any new ASTs by an IPI.
  381          */
  382         cli
  383         movl    PCPU(CURTHREAD),%eax
  384         testl   $TDF_ASTPENDING | TDF_NEEDRESCHED,TD_FLAGS(%eax)
  385         je      doreti_exit
  386         sti
  387         pushl   %esp                    /* pass a pointer to the trapframe */
  388         call    ast
  389         add     $4,%esp
  390         jmp     doreti_ast
  391 
  392         /*
  393          * doreti_exit: pop registers, iret.
  394          *
  395          *      The segment register pop is a special case, since it may
  396          *      fault if (for example) a sigreturn specifies bad segment
  397          *      registers.  The fault is handled in trap.c.
  398          */
  399 doreti_exit:
  400         MEXITCOUNT
  401 
  402         .globl  doreti_popl_fs
  403 doreti_popl_fs:
  404         popl    %fs
  405         .globl  doreti_popl_es
  406 doreti_popl_es:
  407         popl    %es
  408         .globl  doreti_popl_ds
  409 doreti_popl_ds:
  410         popl    %ds
  411         popal
  412         addl    $8,%esp
  413         .globl  doreti_iret
  414 doreti_iret:
  415         iret
  416 
  417         /*
  418          * doreti_iret_fault and friends.  Alternative return code for
  419          * the case where we get a fault in the doreti_exit code
  420          * above.  trap() (i386/i386/trap.c) catches this specific
  421          * case, sends the process a signal and continues in the
  422          * corresponding place in the code below.
  423          */
  424         ALIGN_TEXT
  425         .globl  doreti_iret_fault
  426 doreti_iret_fault:
  427         subl    $8,%esp
  428         pushal
  429         pushl   $0
  430         movw    %ds,(%esp)
  431         .globl  doreti_popl_ds_fault
  432 doreti_popl_ds_fault:
  433         pushl   $0
  434         movw    %es,(%esp)
  435         .globl  doreti_popl_es_fault
  436 doreti_popl_es_fault:
  437         pushl   $0
  438         movw    %fs,(%esp)
  439         .globl  doreti_popl_fs_fault
  440 doreti_popl_fs_fault:
  441         sti
  442         movl    $0,TF_ERR(%esp) /* XXX should be the error code */
  443         movl    $T_PROTFLT,TF_TRAPNO(%esp)
  444         jmp     alltraps_with_regs_pushed
  445 #ifdef HWPMC_HOOKS
  446 doreti_nmi:
  447         /*
  448          * Since we are returning from an NMI, check if the current trap
  449          * was from user mode and if so whether the current thread
  450          * needs a user call chain capture.
  451          */
  452         testb   $SEL_RPL_MASK,TF_CS(%esp)
  453         jz      doreti_exit
  454         movl    PCPU(CURTHREAD),%eax    /* curthread present? */
  455         orl     %eax,%eax
  456         jz      doreti_exit
  457         testl   $TDP_CALLCHAIN,TD_PFLAGS(%eax) /* flagged for capture? */
  458         jz      doreti_exit
  459         /*
  460          * Take the processor out of NMI mode by executing a fake "iret".
  461          */
  462         pushfl
  463         pushl   %cs
  464         pushl   $outofnmi
  465         iret
  466 outofnmi:
  467         /*
  468          * Call the callchain capture hook after turning interrupts back on.
  469          */
  470         movl    pmc_hook,%ecx
  471         orl     %ecx,%ecx
  472         jz      doreti_exit
  473         pushl   %esp                    /* frame pointer */
  474         pushl   $PMC_FN_USER_CALLCHAIN  /* command */
  475         movl    PCPU(CURTHREAD),%eax
  476         pushl   %eax                    /* curthread */
  477         sti
  478         call    *%ecx
  479         addl    $12,%esp
  480         jmp     doreti_ast
  481         ENTRY(end_exceptions)
  482 #endif

Cache object: 8b85f7a891408b1cce5f92a46737dc09


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