17-20 Vol. 3
8086 EMULATION
Interrupt and exception handlers can examine the VM flag on the stack to determine
if the interrupted procedure was running in virtual-8086 mode. If so, the interrupt or
exception can be handled in one of three ways:
• The protected-mode interrupt or exception handler that was called can handle
the interrupt or exception.
• The protected-mode interrupt or exception handler can call the virtual-8086
monitor to handle the interrupt or exception.
• The virtual-8086 monitor (if called) can in turn pass control back to the 8086
program’s interrupt and exception handler.
If the interrupt or exception is handled with a protected-mode handler, the handler
can return to the interrupted program in virtual-8086 mode by executing an IRET
instruction. This instruction loads the EFLAGS and segment registers from the
images saved in the privilege level 0 stack (see
Figure 17-4). A set VM flag in the
EFLAGS image causes the processor to switch back to virtual-8086 mode. The CPL at
the time the IRET instruction is executed must be 0, otherwise the processor does
not change the state of the VM flag.
The virtual-8086 monitor runs at privilege level 0, like the protected-mode interrupt
and exception handlers. It is commonly closely tied to the protected-mode general-
protection exception (#GP, vector 13) handler. If the protected-mode interrupt or
exception handler calls the virtual-8086 monitor to handle the interrupt or exception,
the return from the virtual-8086 monitor to the interrupted virtual-8086 mode
program requires two return instructions: a RET instruction to return to the
protected-mode handler and an IRET instruction to return to the interrupted
program.
The virtual-8086 monitor has the option of directing the interrupt and exception back
to an interrupt or exception handler that is part of the interrupted 8086 program, as
described in
Section 17.3.1.2, “Handling an Interrupt or Exception With an 8086
Program Interrupt or Exception Handler”.
17.3.1.2 Handling an Interrupt or Exception With an 8086 Program
Interrupt or Exception Handler
Because it was designed to run on an 8086 processor, an 8086 program running in a
virtual-8086-mode task contains an 8086-style interrupt vector table, which starts at
linear address 0. If the virtual-8086 monitor correctly directs an interrupt or excep
-
tion vector back to the virtual-8086-mode task it came from, the handlers in the
8086 program can handle the interrupt or exception. The virtual-8086 monitor must
carry out the following steps to send an interrupt or exception back to the 8086
program:
1. Use the 8086 interrupt vector to locate the appropriate handler procedure in the
8086 program interrupt table.