home *** CD-ROM | disk | FTP | other *** search
/ Hot Shareware 32 / hot34.iso / ficheros / DTOOL / INTER57C.ZIP / INTERRUP.PRI < prev    next >
Text File  |  1997-01-12  |  7KB  |  136 lines

  1.             iAPX 86 Interrupt Primer
  2.             ------------------------
  3.  
  4.                  by Ralf Brown
  5.                  12/87
  6.             Updated 6/88, 4/90, 9/92, 1/97
  7.  
  8.  
  9. What is an interrupt?
  10. ---------------------
  11.    An interrupt is a hardware signal that tells the CPU to temporarily
  12.    stop what it is doing and go do something else.  Without interrupts,
  13.    the CPU would have to constantly check for external events; with
  14.    interrupts, the CPU can work on something else and still respond to
  15.    an event as soon as it occurs. 
  16.  
  17.    CPUs typically have an instruction to disable interrupts for use
  18.    when a section of code has to run without being disturbed by
  19.    external events.  Because of this, most CPUs also have a special
  20.    interrupt called a Non-Maskable Interrupt (NMI), which is responded
  21.    to even when all other interrupts are disabled.  The NMI is used to
  22.    signal calamities such as memory failure or imminent power loss.
  23.  
  24. Why so many different interrupts?
  25. ---------------------------------
  26.    The 8086 family of processors has the ability to recognize 256
  27.    different interrupts.  They also have the ability to let a program
  28.    invoke any of these interrupts with a special instruction, known as
  29.    a software interrupt (as opposed to a hardware interrupt which is
  30.    signalled from outside the processor).  Software interrupts are
  31.    treated just like hardware interrupts, except that they are never
  32.    disabled and do not result in an acknowledgement to other chips in
  33.    the computer.  The software interrupt instruction on the 8086 family
  34.    is called INT, and is given the number of the interrupt.  Thus an
  35.    INT 21h instruction invokes interrupt number 33 decimal.
  36.  
  37.    Other processors also have software interrupts, though they often
  38.    use different names, such as the Motorola 68000 family TRAP
  39.    instruction, the Intel 8080 RST (ReSTart) instruction, or many
  40.    mainframes' SVC (SuperVisor Call). 
  41.  
  42.    Since a program can invoke an interrupt by number rather than by its
  43.    address (as it has to in calling subroutines), interrupts are a
  44.    convenient way of providing services without having to recompile a
  45.    program whenever the address of the code providing the service
  46.    changes.  This also allows a user program to enhance the services
  47.    provided by directing the interrupt to itself.  These enhanced
  48.    services can then be made available to other programs.
  49.  
  50. How does an interrupt work in real-address mode?
  51. ------------------------------------------------
  52.    The 8086 reserves the lowest 1024 bytes of memory for a table (IVT,
  53.    Interrupt Vector Table) containing the addresses for each of the 256
  54.    possible interrupts.  When an interrupt occurs (hardware or
  55.    software), the processor multiplies its number by 4 and looks at the
  56.    resulting memory location to find the address of the piece of code
  57.    which handles the interrupt.  It then places the current address in
  58.    the program and the processor flags on the stack, and jumps to the
  59.    beginning of the interrupt handler.
  60.  
  61.    When the interrupt handler finishes, it invokes a special
  62.    instruction to return from the interrupt.  This instruction takes
  63.    the previously saved flags and program address off of the stack and
  64.    places them back in the appropriate registers in the CPU.
  65.  
  66.    The interrupt handler has to be careful to preserve any registers
  67.    that it uses which are not used to communicate results to the
  68.    program that invoked the interrupt.  If the interrupt can be
  69.    triggered by a hardware interrupt (only certain ones can on IBM
  70.    PC's, XT's, and AT's), then the interrupt handler has to preserve
  71.    ALL registers, since the interrupt could have happened anywhere.
  72.  
  73. How does an interrupt work in protected mode?
  74. ---------------------------------------------
  75.    The 80286 and later processors can also operate in protected mode,
  76.    in which case the interrupt handling is somewhat different.  First,
  77.    the interrupt table consists of eight-byte descriptors instead of
  78.    four-byte addresses, and need not be located at physical address
  79.    zero, nor contain the full 256 entries (the address and size of the
  80.    Interrupt Descriptor Table (IDT) may be manipulated with the LIDT
  81.    and SIDT instructions).
  82.  
  83.    Second, in protected mode, the descriptor for an interrupt number
  84.    specifies HOW control is transferred to the interrupt handler.
  85.    Three types of transfer are possible: Interrupt Gate, Trap Gate,
  86.    and Task Gate.  The first two types transfer control to a handler
  87.    running in the same process as the active program, while a Task Gate
  88.    performs a complete context switch in order to invoke a handler in
  89.    a different process from the active program.  Interrupt and Trap
  90.    gates are identical except that an Interrupt Gate will clear IF
  91.    and thus disable interrupts, while a Trap Gate leaves IF unchanged.
  92.  
  93. How does an interrupt work in virtual-86 (V86) mode?
  94. ----------------------------------------------------
  95.    The 80386 and later processors provide a virtual-8086 mode which is
  96.    a protected mode that appears to software to be the same as Real
  97.    mode.  Because it is a protected mode, however, interrupts and
  98.    various other actions that potentially affect system integrity do
  99.    not execute directly, but instead invoke a supervisor program running
  100.    in standard protected mode.  Thus, whenever a program running in
  101.    V86 mode invokes an interrupt call, the CPU switches to protected
  102.    mode and transfers control to the interrupt handler specified by
  103.    the protected-mode IDT, rather than the real-mode IVT.  The
  104.    supervisor program may handle the interrupt call in any way it
  105.    likes, but typically switches the CPU back into V86 mode and jumps
  106.    to the address specified in the real-mode IVT (a process which is
  107.    known as "reflecting" the interrupt).
  108.  
  109.  
  110. GLOSSARY
  111. --------
  112. API (Application Program[ming] Interface)
  113.    An API is the set of function calls and services that a program
  114.    makes available to other processes (applications).  Each function or
  115.    service has a set format which specifies the values to be supplied
  116.    by the caller and the values which are returned. Because of this
  117.    interface specification, the underlying organization of the function
  118.    or service can be changed without affecting the applications which
  119.    use it.  For example, the DOS INT 21h file access functions remained
  120.    unchanged between DOS 2.x and DOS 3.x, even though the internal data
  121.    structures and code organization changed significantly.
  122.  
  123. IDT (Interrupt Descriptor Table)
  124.  
  125. IVT (Interrupt Vector Table)
  126.  
  127. NMI (Non-Maskable Interrupt)
  128.    Most external (hardware) interrupts can be disabled by the CLI
  129.    (CLear Interrupt enable flag) instruction when the CPU is executing
  130.    critical code that should not be interrupted, such as switching from
  131.    one stack to another.  However, there are some situations so dire
  132.    that the CPU must act on them immediately no matter what else it is
  133.    doing, even if it has disabled interrupts.  The Non-Maskable
  134.    Interrupt serves precisely this purpose, as it cannot be disabled
  135.    (masked) by the CPU.
  136.