## 11.3  INTERRUPT PRIORITIES AND NESTING

---------- > While an interrupt handler is in progress, if the interrupt
flag is cleared, can the handler be interrupted by another hardware
interrupt?  Also, if an interrupt handler takes a long time to run, can
it be interrupted by itself (for example, a keyboard interrupt handler)?

No hardware interrupt will be accepted if the interrupt flag is clear, as
it is on entry to an interrupt handler.  But, it is normal for most
interrupt handlers to enable interrupts via an STI instruction fairly
early on, so that higher priority interrupts will be able to interrupt
them. Hardware interrupts are prioritised by the 8259 interrupt
controller(s). Lower IRQ numbers are higher priority (unless software has
reprogrammed the interrupt controller modes).  IRQ8-15 (not present on
original PC and XT) fit in between IRQ1 and IRQ3.  In other words, the
priority order is:

   IRQ0    INT 8     Timer tick interrupt (HIGHEST PRIORITY)
   IRQ1    INT 9     Keyboard scancode interrupt
   IRQ2    INT 0Ah   Uncommitted (see IRQ9) (ONLY PRESENT ON ORIGINAL PC
AND XT)   IRQ8    INT 70h   RTC interrupt
   IRQ9    INT 71h   Redirected IRQ2, uncommitted (COM ports, vertical
retrace)
   IRQ10   INT 72h   Unallocated
   IRQ11   INT 73h   Unallocated
   IRQ12   INT 74h   Bus mouse hardware interrupt
   IRQ13   INT 75h   Math coprocessor
   IRQ14   INT 76h   Hard disk (AT and later)
   IRQ15   INT 77h   Unallocated
   IRQ3    INT 0Bh   COM2, usually, or uncommitted
   IRQ4    INT 0Ch   COM1, usually
   IRQ5    INT 0Dh   Uncommitted (COM ports, sound cards, XT hard disk)
   IRQ6    INT 0Eh   Floppy disk hardware interrupt
   IRQ7    INT 0Fh   Parallel port, sound cards (LOWEST PRIORITY)

If an interrupt handler is in progress, say on IRQ4 for example, and the
handler enables interrupts using STI, then a higher priority interrupt,
such as the keyboard scancode interrupt or timer tick interrupt, if
signalled, _will_ interrupt the interrupt handler in progress.  Once that
higher priority interrupt has been processed, the lower priority interrupt
handler will be resumed.  But, a lower or equal priority interrupt will
_not_ interrupt the handler in progress, until that handler has sent an
EOI (end of interrupt) command to the interrupt controller(s) (first
interrupt controller for IRQ0-7, both interrupt controllers for IRQ8-15).  
The EOI command is value 20h, and is sent to I/O port 20h for the first
interrupt controller, and port 0A0h for the second interrupt controller.  

The EOI command tells the interrupt controller that the interrupt that was
signalled by the interrupt controller (which provides the interrupt vector
to tell the processor where the interrupt handler begins) is now finished
with, and it resets the interrupt controller's logic. The interrupt
controller will then signal any other interrupts that were pending.  For
example if an IRQ7 came along while IRQ4 was being processed, the
interrupt controller would ignore it until the IRQ4 handler issued an EOI,
then the interrupt controller would re-evaluate its pending interrupts and
issue the highest priority pending interrupt, which would be IRQ7. 

To avoid the lower priority interrupt 'nesting' on top of the higher
priority interrupt and causing stack growth, the EOI command is normally
issued right at the end of the interrupt handler, and is issued with
interrupts locked out, so that the interrupt handler will return to the
  main code    before the lower priority interrupt is accepted.

If you specifically want a particular interrupt priority to be able to
interrupt itself, this is normally possible - just send the EOI at an
early stage in the interrupt handler, and make sure interrupts are
enabled.  The interrupt controller thinks the interrupt handler has
finished, so it will signal the interrupt again if the interrupt is
triggered again during processing of the same priority interrupt.  Of
course this also lets lower priority interrupts through as well.


If a hardware interrupt request disappears while the interrupt controller
is waiting for the processor to acknowledge its interrupt request (INTR),
the interrupt controller is in the embarrassing position of having
interrupted the processor but not having a valid interrupt request to
issue.  In this case, the interrupt controller issues an interrupt level
7.  If the fleeting interrupt was on the primary interrupt controller
(IRQ0, IRQ1, or IRQ3-7), this will cause IRQ7 (int 0Fh) to be executed.  
If the fleeting interrupt was on the secondary interrupt controller
(IRQ8-15), this will cause IRQ15 (int 77h) to be executed.  Any program
handling IRQ7 and/or IRQ15 should be prepared for this possibility.


