home *** CD-ROM | disk | FTP | other *** search
/ CD-X 1 / cdx_01.iso / demodisc / tyrant / docs / 386code / 15.386 < prev    next >
Encoding:
Text File  |  1994-01-20  |  31.6 KB  |  694 lines

  1. Chapter 15  Virtual 8086 Mode
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. The 80386 supports execution of one or more 8086, 8088, 80186, or 80188
  6. programs in an 80386 protected-mode environment. An 8086 program runs in
  7. this environment as part of a V86 (virtual 8086) task. V86 tasks take
  8. advantage of the hardware support of multitasking offered by the protected
  9. mode. Not only can there be multiple V86 tasks, each one executing an 8086
  10. program, but V86 tasks can be multiprogrammed with other 80386 tasks.
  11.  
  12. The purpose of a V86 task is to form a "virtual machine" with which to
  13. execute an 8086 program. A complete virtual machine consists not only of
  14. 80386 hardware but also of systems software. Thus, the emulation of an 8086
  15. is the result of cooperation between hardware and software:
  16.  
  17.   ■  The hardware provides a virtual set of registers (via the TSS), a
  18.      virtual memory space (the first megabyte of the linear address space of
  19.      the task), and directly executes all instructions that deal with these
  20.      registers and with this address space.
  21.  
  22.   ■  The software controls the external interfaces of the virtual machine
  23.      (I/O, interrupts, and exceptions) in a manner consistent with the
  24.      larger environment in which it executes. In the case of I/O, software
  25.      can choose either to emulate I/O instructions or to let the hardware
  26.      execute them directly without software intervention.
  27.  
  28. Software that helps implement virtual 8086 machines is called a V86
  29. monitor.
  30.  
  31.  
  32. 15.1  Executing 8086 Code
  33.  
  34. The processor executes in V86 mode when the VM (virtual machine) bit in the
  35. EFLAGS register is set. The processor tests this flag under two general
  36. conditions:
  37.  
  38.   1.  When loading segment registers to know whether to use 8086-style
  39.       address formation.
  40.  
  41.   2.  When decoding instructions to determine which instructions are
  42.       sensitive to IOPL.
  43.  
  44. Except for these two modifications to its normal operations, the 80386 in
  45. V86 mode operated much as in protected mode.
  46.  
  47.  
  48. 15.1.1  Registers and Instructions
  49.  
  50. The register set available in V86 mode includes all the registers defined
  51. for the 8086 plus the new registers introduced by the 80386: FS, GS, debug
  52. registers, control registers, and test registers. New instructions that
  53. explicitly operate on the segment registers FS and GS are available, and the
  54. new segment-override prefixes can be used to cause instructions to utilize
  55. FS and GS for address calculations. Instructions can utilize 32-bit
  56. operands through the use of the operand size prefix.
  57.  
  58. 8086 programs running as V86 tasks are able to take advantage of the new
  59. applications-oriented instructions added to the architecture by the
  60. introduction of the 80186/80188, 80286 and 80386:
  61.  
  62.   ■  New instructions introduced by 80186/80188 and 80286.
  63.      ── PUSH immediate data
  64.      ── Push all and pop all (PUSHA and POPA)
  65.      ── Multiply immediate data
  66.      ── Shift and rotate by immediate count
  67.      ── String I/O
  68.      ── ENTER and LEAVE
  69.      ── BOUND
  70.  
  71.   ■  New instructions introduced by 80386.
  72.      ── LSS, LFS, LGS instructions
  73.      ── Long-displacement conditional jumps
  74.      ── Single-bit instructions
  75.      ── Bit scan
  76.      ── Double-shift instructions
  77.      ── Byte set on condition
  78.      ── Move with sign/zero extension
  79.      ── Generalized multiply
  80.  
  81.  
  82. 15.1.2  Linear Address Formation
  83.  
  84. In V86 mode, the 80386 processor does not interpret 8086 selectors by
  85. referring to descriptors; instead, it forms linear addresses as an 8086
  86. would. It shifts the selector left by four bits to form a 20-bit base
  87. address. The effective address is extended with four high-order zeros and
  88. added to the base address to create a linear address as Figure 15-1
  89. illustrates.
  90.  
  91. Because of the possibility of a carry, the resulting linear address may
  92. contain up to 21 significant bits. An 8086 program may generate linear
  93. addresses anywhere in the range 0 to 10FFEFH (one megabyte plus
  94. approximately 64 Kbytes) of the task's linear address space.
  95.  
  96. V86 tasks generate 32-bit linear addresses. While an 8086 program can only
  97. utilize the low-order 21 bits of a linear address, the linear address can be
  98. mapped via page tables to any 32-bit physical address.
  99.  
  100. Unlike the 8086 and 80286, 32-bit effective addresses can be generated (via
  101. the address-size prefix); however, the value of a 32-bit address may not
  102. exceed 65,535 without causing an exception. For full compatibility with
  103. 80286 real-address mode, pseudo-protection faults (interrupt 12 or 13 with
  104. no error code) occur if an address is generated outside the range 0 through
  105. 65,535.
  106.  
  107.  
  108. Figure 15-1.  V86 Mode Address Formation
  109.  
  110.                       19                                3       0
  111.                      ╔═════════════════════════════════╪═════════╗
  112.          BASE        ║     16-BIT SEGMENT SELECTOR     │ 0 0 0 0 ║
  113.                      ╚═════════════════════════════════╪═════════╝
  114.  
  115.          +
  116.                       19        15                              0
  117.                      ╔═════════╪═════════════════════════════════╗
  118.          OFFSET      ║ 0 0 0 0 │    16-BIT EFFECTIVE ADDRESS     ║
  119.                      ╚═════════╪═════════════════════════════════╝
  120.  
  121.          =
  122.                     20                                          0
  123.          LINEAR    ╔═════════════════════════════════════════════╗
  124.          ADDRESS   ║ X X X X X X X X X X X X X X X X X X X X X X ║
  125.                    ╚═════════════════════════════════════════════╝
  126.  
  127.  
  128. 15.2  Structure of a V86 Task
  129.  
  130. A V86 task consists partly of the 8086 program to be executed and partly of
  131. 80386 "native mode" code that serves as the virtual-machine monitor. The
  132. task must be represented by an 80386 TSS (not an 80286 TSS). The processor
  133. enters V86 mode to execute the 8086 program and returns to protected mode to
  134. execute the monitor or other 80386 tasks.
  135.  
  136. To run successfully in V86 mode, an existing 8086 program needs the
  137. following:
  138.  
  139.   ■  A V86 monitor.
  140.   ■  Operating-system services.
  141.  
  142. The V86 monitor is 80386 protected-mode code that executes at
  143. privilege-level zero. The monitor consists primarily of initialization and
  144. exception-handling procedures. As for any other 80386 program,
  145. executable-segment descriptors for the monitor must exist in the GDT or in
  146. the task's LDT. The linear addresses above 10FFEFH are available for the
  147. V86 monitor, the operating system, and other systems software. The monitor
  148. may also need data-segment descriptors so that it can examine the interrupt
  149. vector table or other parts of the 8086 program in the first megabyte of the
  150. address space.
  151.  
  152. In general, there are two options for implementing the 8086 operating
  153. system:
  154.  
  155.   1.  The 8086 operating system may run as part of the 8086 code. This
  156.       approach is desirable for any of the following reasons:
  157.  
  158.       ■  The 8086 applications code modifies the operating system.
  159.  
  160.       ■  There is not sufficient development time to reimplement the 8086
  161.          operating system as 80386 code.
  162.  
  163.   2.  The 8086 operating system may be implemented or emulated in the V86
  164.       monitor. This approach is desirable for any of the following reasons:
  165.  
  166.       ■  Operating system functions can be more easily coordinated among
  167.          several V86 tasks.
  168.  
  169.       ■  The functions of the 8086 operating system can be easily emulated
  170.          by calls to the 80386 operating system.
  171.  
  172. Note that, regardless of the approach chosen for implementing the 8086
  173. operating system, different V86 tasks may use different 8086 operating
  174. systems.
  175.  
  176.  
  177. 15.2.1  Using Paging for V86 Tasks
  178.  
  179. Paging is not necessary for a single V86 task, but paging is useful or
  180. necessary for any of the following reasons:
  181.  
  182.   ■  To create multiple V86 tasks. Each task must map the lower megabyte of
  183.      linear addresses to different physical locations.
  184.  
  185.   ■  To emulate the megabyte wrap. On members of the 8086 family, it is
  186.      possible to specify addresses larger than one megabyte. For example,
  187.      with a selector value of 0FFFFH and an offset of 0FFFFH, the effective
  188.      address would be 10FFEFH (one megabyte + 65519). The 8086, which can
  189.      form addresses only up to 20 bits long, truncates the high-order bit,
  190.      thereby "wrapping" this address to 0FFEFH. The 80386, however, which
  191.      can form addresses up to 32 bits long does not truncate such an
  192.      address. If any 8086 programs depend on this addressing anomaly, the
  193.      same effect can be achieved in a V86 task by mapping linear addresses
  194.      between 100000H and 110000H and linear addresses between 0 and 10000H
  195.      to the same physical addresses.
  196.  
  197.   ■  To create a virtual address space larger than the physical address
  198.      space.
  199.  
  200.   ■  To share 8086 OS code or ROM code that is common to several 8086
  201.      programs that are executing simultaneously.
  202.  
  203.   ■  To redirect or trap references to memory-mapped I/O devices.
  204.  
  205.  
  206. 15.2.2  Protection within a V86 Task
  207.  
  208. Because it does not refer to descriptors while executing 8086 programs, the
  209. processor also does not utilize the protection mechanisms offered by
  210. descriptors. To protect the systems software that runs in a V86 task from
  211. the 8086 program, software designers may follow either of these approaches:
  212.  
  213.   ■  Reserve the first megabyte (plus 64 kilobytes) of each task's linear
  214.      address space for the 8086 program. An 8086 task cannot generate
  215.      addresses outside this range.
  216.  
  217.   ■  Use the U/S bit of page-table entries to protect the virtual-machine
  218.      monitor and other systems software in each virtual 8086 task's space.
  219.      When the processor is in V86 mode, CPL is 3. Therefore, an 8086 program
  220.      has only user privileges. If the pages of the virtual-machine monitor
  221.      have supervisor privilege, they cannot be accessed by the 8086 program.
  222.  
  223.  
  224. 15.3  Entering and Leaving V86 Mode
  225.  
  226. Figure 15-2 summarizes the ways that the processor can enter and leave an
  227. 8086 program. The processor can enter V86 by either of two means:
  228.  
  229.   1.  A task switch to an 80386 task loads the image of EFLAGS from the new
  230.       TSS. The TSS of the new task must be an 80386 TSS, not an 80286 TSS,
  231.       because the 80286 TSS does not store the high-order word of EFLAGS,
  232.       which contains the VM flag. A value of one in the VM bit of the new
  233.       EFLAGS indicates that the new task is executing 8086 instructions;
  234.       therefore, while loading the segment registers from the TSS, the
  235.       processor forms base addresses as the 8086 would.
  236.  
  237.   2.  An IRET from a procedure of an 80386 task loads the image of EFLAGS
  238.       from the stack. A value of one in VM in this case indicates that the
  239.       procedure to which control is being returned is an 8086 procedure. The
  240.       CPL at the time the IRET is executed must be zero, else the processor
  241.       does not change VM.
  242.  
  243. The processor leaves V86 mode when an interrupt or exception occurs. There
  244. are two cases:
  245.  
  246.   1.  The interrupt or exception causes a task switch. A task switch from a
  247.       V86 task to any other task loads EFLAGS from the TSS of the new task.
  248.       If the new TSS is an 80386 TSS and the VM bit in the EFLAGS image is
  249.       zero or if the new TSS is an 80286 TSS, then the processor clears the
  250.       VM bit of EFLAGS, loads the segment registers from the new TSS using
  251.       80386-style address formation, and begins executing the instructions
  252.       of the new task according to 80386 protected-mode semantics.
  253.  
  254.   2.  The interrupt or exception vectors to a privilege-level zero
  255.       procedure. The processor stores the current setting of EFLAGS on the
  256.       stack, then clears the VM bit. The interrupt or exception handler,
  257.       therefore, executes as "native" 80386 protected-mode code. If an
  258.       interrupt or exception vectors to a conforming segment or to a
  259.       privilege level other than three, the processor causes a
  260.       general-protection exception; the error code is the selector of the
  261.       executable segment to which transfer was attempted.
  262.  
  263. Systems software does not manipulate the VM flag directly, but rather
  264. manipulates the image of the EFLAGS register that is stored on the stack or
  265. in the TSS. The V86 monitor sets the VM flag in the EFLAGS image on the
  266. stack or in the TSS when first creating a V86 task. Exception and interrupt
  267. handlers can examine the VM flag on the stack. If the interrupted procedure
  268. was executing in V86 mode, the handler may need to invoke the V86 monitor.
  269.  
  270.  
  271. Figure 15-2.  Entering and Leaving the 8086 Program
  272.  
  273.                             MODE TRANSITION DIAGRAM
  274.  
  275.                                  ╔═══════════╗
  276.                   TASK SWITCH    ║  INITIAL  ║
  277.                 ┌────────────────╢   ENTRY   ║
  278.                 │   OR IRET      ╚═══════════╝
  279.                 │
  280.                 
  281.         ╔══════════════╗    INTERRUPT, EXCEPTION      ╔═════════════╗
  282.         ║ 8086 PROGRAM ╟─────────────────────────────║ V86 MONITOR ║
  283.         ║  (V86 MODE)  ║─────────────────────────────╢ (PROTECTED  ║
  284.         ╚═══════╤══════╝            IRET              ║    MODE)    ║
  285.                │                                     ╚═════╤═══════╝
  286.               │ │                                           │  
  287.               │ │                                           │  │
  288.               │ │                                           │  │
  289.               │ │TASK SWITCH ╔═══════════════════╗ TASK SWITCH │
  290.               │ └───────────║ OTHER 80386 TASKS ║─────────┘  │
  291.               └──────────────╢ (PROTECTED MODE)  ╟─────────────┘
  292.                  TASK SWITCH ╚═══════════════════╝ TASK SWITCH
  293.  
  294.  
  295. 15.3.1  Transitions Through Task Switches
  296.  
  297. A task switch to or from a V86 task may be due to any of three causes:
  298.  
  299.   1.  An interrupt that vectors to a task gate.
  300.   2.  An action of the scheduler of the 80386 operating system.
  301.   3.  An IRET when the NT flag is set.
  302.  
  303. In any of these cases, the processor changes the VM bit in EFLAGS according
  304. to the image of EFLAGS in the new TSS. If the new TSS is an 80286 TSS, the
  305. high-order word of EFLAGS is not in the TSS; the processor clears VM in this
  306. case. The processor updates VM prior to loading the segment registers from
  307. the images in the new TSS. The new setting of VM determines whether the
  308. processor interprets the new segment-register images as 8086 selectors or
  309. 80386/80286 selectors.
  310.  
  311.  
  312. 15.3.2  Transitions Through Trap Gates and Interrupt Gates
  313.  
  314. The processor leaves V86 mode as the result of an exception or interrupt
  315. that vectors via a trap or interrupt gate to a privilege-level zero
  316. procedure. The exception or interrupt handler returns to the 8086 code by
  317. executing an IRET.
  318.  
  319. Because it was designed for execution by an 8086 processor, an 8086 program
  320. in a V86 task will have an 8086-style interrupt table starting at linear
  321. address zero. However, the 80386 does not use this table directly. For all
  322. exceptions and interrupts that occur in V86 mode, the processor vectors
  323. through the IDT. The IDT entry for an interrupt or exception that occurs in
  324. a V86 task must contain either:
  325.  
  326.   ■  A task gate.
  327.  
  328.   ■  An 80386 trap gate (type 14) or an 80386 interrupt gate (type 15),
  329.      which must point to a nonconforming, privilege-level zero, code
  330.      segment.
  331.  
  332. Interrupts and exceptions that have 80386 trap or interrupt gates in the
  333. IDT vector to the appropriate handler procedure at privilege-level zero. The
  334. contents of all the 8086 segment registers are stored on the PL 0 stack.
  335. Figure 15-3 shows the format of the PL 0 stack after an exception or
  336. interrupt that occurs while a V86 task is executing an 8086 program.
  337.  
  338. After the processor stores all the 8086 segment registers on the PL 0
  339. stack, it loads all the segment registers with zeros before starting to
  340. execute the handler procedure. This permits the interrupt handler to safely
  341. save and restore the DS, ES, FS, and GS registers as 80386 selectors.
  342. Interrupt handlers that may be invoked in the context of either a regular
  343. task or a V86 task, can use the same prolog and epilog code for register
  344. saving regardless of the kind of task. Restoring zeros to these registers
  345. before execution of the IRET does not cause a trap in the interrupt handler.
  346. Interrupt procedures that expect values in the segment registers or that
  347. return values via segment registers have to use the register images stored
  348. on the PL 0 stack. Interrupt handlers that need to know whether the
  349. interrupt occurred in V86 mode can examine the VM bit in the stored EFLAGS
  350. image.
  351.  
  352. An interrupt handler passes control to the V86 monitor if the VM bit is set
  353. in the EFLAGS image stored on the stack and the interrupt or exception is
  354. one that the monitor needs to handle. The V86 monitor may either:
  355.  
  356.   ■  Handle the interrupt completely within the V86 monitor.
  357.   ■  Invoke the 8086 program's interrupt handler.
  358.  
  359. Reflecting an interrupt or exception back to the 8086 code involves the
  360. following steps:
  361.  
  362.   1.  Refer to the 8086 interrupt vector to locate the appropriate handler
  363.       procedure.
  364.  
  365.   2.  Store the state of the 8086 program on the privilege-level three
  366.       stack.
  367.  
  368.   3.  Change the return link on the privilege-level zero stack to point to
  369.       the privilege-level three handler procedure.
  370.  
  371.   4.  Execute an IRET so as to pass control to the handler.
  372.  
  373.   5.  When the IRET by the privilege-level three handler again traps to the
  374.       V86 monitor, restore the return link on the privilege-level zero stack
  375.       to point to the originally interrupted, privilege-level three
  376.       procedure.
  377.  
  378.   6.  Execute an IRET so as to pass control back to the interrupted
  379.       procedure.
  380.  
  381.  
  382. Figure 15-3. PL 0 Stack after Interrupt in V86 Task
  383.  
  384.  
  385.                 WITHOUT ERROR CODE            WITH ERROR CODE
  386.                  31            0               31            0
  387.                 ╔══════╦═══════╗────┐        ╔══════╦═══════╗────┐
  388.                 ║▒▒▒▒▒▒║OLD GS ║     │        ║▒▒▒▒▒▒║OLD GS ║     │
  389.                 ╠══════╬═══════╣   SS:ESP     ╠══════╬═══════╣   SS:ESP
  390.       D  O      ║▒▒▒▒▒▒║OLD FS ║  FROM TSS    ║▒▒▒▒▒▒║OLD FS ║  FROM TSS
  391.       I  F      ╠══════╬═══════╣              ╠══════╬═══════╣
  392.       R         ║▒▒▒▒▒▒║OLD DS ║              ║▒▒▒▒▒▒║OLD DS ║
  393.       E  E      ╠══════╬═══════╣              ╠══════╬═══════╣
  394.       C  X      ║▒▒▒▒▒▒║OLD ES ║              ║▒▒▒▒▒▒║OLD ES ║
  395.       T  P      ╠══════╬═══════╣              ╠══════╬═══════╣
  396.       I  A      ║▒▒▒▒▒▒║OLD SS ║              ║▒▒▒▒▒▒║OLD SS ║
  397.       O  N      ╠══════╩═══════╣              ╠══════╩═══════╣
  398.       N  S      ║    OLD ESP   ║              ║    OLD ESP   ║
  399.          I      ╠══════════════╣              ╠══════════════╣
  400.        │ O      ║  OLD EFLAGS  ║              ║  OLD EFLAGS  ║
  401.        │ N      ╠══════╦═══════╣              ╠══════╦═══════╣
  402.        │        ║▒▒▒▒▒▒║OLD CS ║   NEW        ║▒▒▒▒▒▒║OLD CS ║
  403.                ╠══════╩═══════╣  SS:EIP      ╠══════╩═══════╣
  404.                 ║    OLD EIP   ║    │         ║    OLD EIP   ║   NEW
  405.                 ╠══════════════╣───┘         ╠══════════════╣  SS:EIP
  406.                 ║              ║              ║  ERROR CODE  ║    │
  407.                                             ╠══════════════╣───┘
  408.                                             ║              ║
  409.                                                           
  410.  
  411.  
  412. 15.4  Additional Sensitive Instructions
  413.  
  414. When the 80386 is executing in V86 mode, the instructions PUSHF, POPF,
  415. INT n, and IRET are sensitive to IOPL. The instructions IN, INS, OUT, and
  416. OUTS, which are ordinarily sensitive in protected mode, are not sensitive
  417. in V86 mode. Following is a complete list of instructions that are sensitive
  418. in V86 mode:
  419.  
  420.    CLI     ── Clear Interrupt-Enable Flag
  421.    STI     ── Set Interrupt-Enable Flag
  422.    LOCK    ── Assert Bus-Lock Signal
  423.    PUSHF   ── Push Flags
  424.    POPF    ── Pop Flags
  425.    INT n   ── Software Interrupt
  426.    RET     ── Interrupt Return
  427.  
  428. CPL is always three in V86 mode; therefore, if IOPL < 3, these instructions
  429. will trigger a general-protection exceptions. These instructions are made
  430. sensitive so that their functions can be simulated by the V86 monitor.
  431.  
  432.  
  433. 15.4.1  Emulating 8086 Operating System Calls
  434.  
  435. INT n is sensitive so that the V86 monitor can intercept calls to the
  436. 8086 OS. Many 8086 operating systems are called by pushing parameters onto
  437. the stack, then executing an INT n instruction. If IOPL < 3, INT n
  438. instructions will be intercepted by the V86 monitor. The V86 monitor can
  439. then emulate the function of the 8086 operating system or reflect the
  440. interrupt back to the 8086 operating system in V86 mode.
  441.  
  442.  
  443. 15.4.2  Virtualizing the Interrupt-Enable Flag
  444.  
  445. When the processor is executing 8086 code in a V86 task, the instructions
  446. PUSHF, POPF, and IRET are sensitive to IOPL so that the V86 monitor can
  447. control changes to the interrupt-enable flag (IF). Other instructions that
  448. affect IF (STI and CLI) are IOPL sensitive both in 8086 code and in
  449. 80386/80386 code.
  450.  
  451. Many 8086 programs that were designed to execute on single-task systems set
  452. and clear IF to control interrupts. However, when these same programs are
  453. executed in a multitasking environment, such control of IF can be
  454. disruptive. If IOPL is less than three, all instructions that change or
  455. interrogate IF will trap to the V86 monitor. The V86 monitor can then
  456. control IF in a manner that both suits the needs of the larger environment
  457. and is transparent to the 8086 program.
  458.  
  459.  
  460. 15.5  Virtual I/O
  461.  
  462. Many 8086 programs that were designed to execute on single-task systems use
  463. I/O devices directly. However, when these same programs are executed in a
  464. multitasking environment, such use of devices can be disruptive. The 80386
  465. provides sufficient flexibility to control I/O in a manner that both suits
  466. the needs of the new environment and is transparent to the 8086 program.
  467. Designers may take any of several possible approaches to controlling I/O:
  468.  
  469.   ■  Implement or emulate the 8086 operating system as an 80386 program and
  470.      require the 8086 application to do I/O via software interrupts to the
  471.      operating system, trapping all attempts to do I/O directly.
  472.  
  473.   ■  Let the 8086 program take complete control of all I/O.
  474.  
  475.   ■  Selectively trap and emulate references that a task makes to specific
  476.      I/O ports.
  477.  
  478.   ■  Trap or redirect references to memory-mapped I/O addresses.
  479.  
  480. The method of controlling I/O depends upon whether I/O ports are I/O mapped
  481. or memory mapped.
  482.  
  483.  
  484. 15.5.1  I/O-Mapped I/O
  485.  
  486. I/O-mapped I/O in V86 mode differs from protected mode only in that the
  487. protection mechanism does not consult IOPL when executing the I/O
  488. instructions IN, INS, OUT, OUTS. Only the I/O permission bit map controls
  489. the right for V86 tasks to execute these I/O instructions.
  490.  
  491. The I/O permission map traps I/O instructions selectively depending on the
  492. I/O addresses to which they refer. The I/O permission bit map of each V86
  493. task determines which I/O addresses are trapped for that task. Because each
  494. task may have a different I/O permission bit map, the addresses trapped for
  495. one task may be different from those trapped for others. Refer to Chapter 8
  496. for more information about the I/O permission map.
  497.  
  498.  
  499. 15.5.2  Memory-Mapped I/O
  500.  
  501. In hardware designs that utilize memory-mapped I/O, the paging facilities
  502. of the 80386 can be used to trap or redirect I/O operations. Each task that
  503. executes memory-mapped I/O must have a page (or pages) for the memory-mapped
  504. address space. The V86 monitor may control memory-mapped I/O by any of
  505. these means:
  506.  
  507.   ■  Assign the memory-mapped page to appropriate physical addresses.
  508.      Different tasks may have different physical addresses, thereby
  509.      preventing the tasks from interfering with each other.
  510.  
  511.   ■  Cause a trap to the monitor by forcing a page fault on the
  512.      memory-mapped page. Read-only pages trap writes. Not-present pages trap
  513.      both reads and writes.
  514.  
  515. Intervention for every I/O might be excessive for some kinds of I/O
  516. devices. A page fault can still be used in this case to cause intervention
  517. on the first I/O operation. The monitor can then at least make sure that the
  518. task has exclusive access to the device. Then the monitor can change the
  519. page status to present and read/write, allowing subsequent I/O to proceed at
  520. full speed.
  521.  
  522.  
  523. 15.5.3  Special I/O Buffers
  524.  
  525. Buffers of intelligent controllers (for example, a bit-mapped graphics
  526. buffer) can also be virtualized via page mapping. The linear space for the
  527. buffer can be mapped to a different physical space for each virtual 8086
  528. task. The V86 monitor can then assume responsibility for spooling the data
  529. or assigning the virtual buffer to the real buffer at appropriate times.
  530.  
  531.  
  532. 15.6  Differences From 8086
  533.  
  534. In general, V86 mode will correctly execute software designed for the 8086,
  535. 8088, 80186, and 80188. Following is a list of the minor differences between
  536. 8086 execution on the 80386 and on an 8086.
  537.  
  538.   1.  Instruction clock counts.
  539.  
  540.       The 80386 takes fewer clocks for most instructions than the 
  541.       8086/8088. The areas most likely to be affected are:
  542.  
  543.       ■  Delays required by I/O devices between I/O operations.
  544.  
  545.       ■  Assumed delays with 8086/8088 operating in parallel with an 8087.
  546.  
  547.   2.  Divide exceptions point to the DIV instruction.
  548.  
  549.       Divide exceptions on the 80386 always leave the saved CS:IP value
  550.       pointing to the instruction that failed. On the 8086/8088, the CS:IP
  551.       value points to the next instruction.
  552.  
  553.   3.  Undefined 8086/8088 opcodes.
  554.  
  555.       Opcodes that were not defined for the 8086/8088 will cause exception
  556.       6 or will execute one of the new instructions defined for the 80386.
  557.  
  558.   4.  Value written by PUSH SP.
  559.  
  560.       The 80386 pushes a different value on the stack for PUSH SP than the
  561.       8086/8088. The 80386 pushes the value of SP before SP is incremented
  562.       as part of the push operation; the 8086/8088 pushes the value of SP
  563.       after it is incremented. If the value pushed is important, replace
  564.       PUSH SP instructions with the following three instructions:
  565.  
  566.       PUSH  BP
  567.       MOV   BP, SP
  568.       XCHG  BP, [BP]
  569.  
  570.       This code functions as the 8086/8088 PUSH SP instruction on the 
  571.       80386.
  572.  
  573.   5.  Shift or rotate by more than 31 bits.
  574.  
  575.       The 80386 masks all shift and rotate counts to the low-order five
  576.       bits. This MOD 32 operation limits the count to a maximum of 31 bits,
  577.       thereby limiting the time that interrupt response is delayed while
  578.       the instruction is executing.
  579.  
  580.   6.  Redundant prefixes.
  581.  
  582.       The 80386 sets a limit of 15 bytes on instruction length. The only
  583.       way to violate this limit is by putting redundant prefixes before an
  584.       instruction. Exception 13 occurs if the limit on instruction length
  585.       is violated. The 8086/8088 has no instruction length limit.
  586.  
  587.   7.  Operand crossing offset 0 or 65,535.
  588.  
  589.       On the 8086, an attempt to access a memory operand that crosses
  590.       offset 65,535 (e.g., MOV a word to offset 65,535) or offset 0 (e.g.,
  591.       PUSH a word when SP = 1) causes the offset to wrap around modulo
  592.       65,536. The 80386 raises an exception in these cases──exception 13 if
  593.       the segment is a data segment (i.e., if CS, DS, ES, FS, or GS is
  594.       being used to address the segment), exception 12 if the segment is a
  595.       stack segment (i.e., if SS is being used).
  596.  
  597.   8.  Sequential execution across offset 65,535.
  598.  
  599.       On the 8086, if sequential execution of instructions proceeds past
  600.       offset 65,535, the processor fetches the next instruction byte from
  601.       offset 0 of the same segment. On the 80386, the processor raises
  602.       exception 13 in such a case.
  603.  
  604.   9.  LOCK is restricted to certain instructions.
  605.  
  606.       The LOCK prefix and its corresponding output signal should only be
  607.       used to prevent other bus masters from interrupting a data movement
  608.       operation. The 80386 always asserts the LOCK signal during an XCHG
  609.       instruction with memory (even if the LOCK prefix is not used). LOCK
  610.       may only be used with the following 80386 instructions when they
  611.       update memory: BTS, BTR, BTC, XCHG, ADD, ADC, SUB, SBB, INC, DEC,
  612.       AND, OR, XOR, NOT, and NEG. An undefined-opcode exception (interrupt
  613.       6) results from using LOCK before any other instruction.
  614.  
  615.  10.  Single-stepping external interrupt handlers.
  616.  
  617.       The priority of the 80386 single-step exception is different from
  618.       that of the 8086/8088. The change prevents an external interrupt
  619.       handler from being single-stepped if the interrupt occurs while a
  620.       program is being single-stepped. The 80386 single-step exception has
  621.       higher priority that any external interrupt. The 80386 will still
  622.       single-step through an interrupt handler invoked by the INT
  623.       instructions or by an exception.
  624.  
  625.   11.  IDIV exceptions for quotients of 80H or 8000H.
  626.  
  627.       The 80386 can generate the largest negative number as a quotient for
  628.       the IDIV instruction. The 8086/8088 causes exception zero instead.
  629.  
  630.  12.  Flags in stack.
  631.  
  632.       The setting of the flags stored by PUSHF, by interrupts, and by
  633.       exceptions is different from that stored by the 8086 in bit positions
  634.       12 through 15. On the 8086 these bits are stored as ones, but in V86
  635.       mode bit 15 is always zero, and bits 14 through 12 reflect the last
  636.       value loaded into them.
  637.  
  638.  13.  NMI interrupting NMI handlers.
  639.  
  640.       After an NMI is recognized on the 80386, the NMI interrupt is masked
  641.       until an IRET instruction is executed.
  642.  
  643.  14.  Coprocessor errors vector to interrupt 16.
  644.  
  645.       Any 80386 system with a coprocessor must use interrupt vector 16 for
  646.       the coprocessor error exception. If an 8086/8088 system uses another
  647.       vector for the 8087 interrupt, both vectors should point to the
  648.       coprocessor-error exception handler.
  649.  
  650.  15.  Numeric exception handlers should allow prefixes.
  651.  
  652.       On the 80386, the value of CS:IP saved for coprocessor exceptions
  653.       points at any prefixes before an ESC instruction. On 8086/8088
  654.       systems, the saved CS:IP points to the ESC instruction itself.
  655.  
  656.  16.  Coprocessor does not use interrupt controller.
  657.  
  658.       The coprocessor error signal to the 80386 does not pass through an
  659.       interrupt controller (an 8087 INT signal does). Some instructions in
  660.       a coprocessor error handler may need to be deleted if they deal with
  661.       the interrupt controller.
  662.  
  663.  
  664. 15.7  Differences From 80286 Real-Address Mode
  665.  
  666. The 80286 processor implements the bus lock function differently than the
  667. 80386. This fact may or may not be apparent to 8086 programs, depending on
  668. how the V86 monitor handles the LOCK prefix. LOCKed instructions are
  669. sensitive to IOPL; therefore, software designers can choose to emulate its
  670. function. If, however, 8086 programs are allowed to execute LOCK directly,
  671. programs that use forms of memory locking specific to the 8086 may not
  672. execute properly when transported to a specific application of the 80386.
  673.  
  674. The LOCK prefix and its corresponding output signal should only be used to
  675. prevent other bus masters from interrupting a data movement operation. LOCK
  676. may only be used with the following 80386 instructions when they modify
  677. memory. An undefined-opcode exception results from using LOCK before any
  678. other instruction.
  679.  
  680.   ■  Bit test and change: BTS, BTR, BTC.
  681.   ■  Exchange: XCHG.
  682.   ■  One-operand arithmetic and logical: INC, DEC, NOT, and NEG.
  683.   ■  Two-operand arithmetic and logical: ADD, ADC, SUB, SBB, AND, OR, XOR.
  684.  
  685. A locked instruction is guaranteed to lock only the area of memory defined
  686. by the destination operand, but may lock a larger memory area. For example,
  687. typical 8086 and 80286 configurations lock the entire physical memory space.
  688. With the 80386, the defined area of memory is guaranteed to be locked
  689. against access by a processor executing a locked instruction on exactly the
  690. same memory area, i.e., an operand with identical starting address and
  691. identical length.
  692.  
  693.  
  694.