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

  1. Chapter 14  80386 Real-Address Mode
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. The real-address mode of the 80386 executes object code designed for
  6. execution on 8086, 8088, 80186, or 80188 processors, or for execution in the
  7. real-address mode of an 80286:
  8.  
  9. In effect, the architecture of the 80386 in this mode is almost identical
  10. to that of the 8086, 8088, 80186, and 80188. To a programmer, an 80386 in
  11. real-address mode appears as a high-speed 8086 with extensions to the
  12. instruction set and registers. The principal features of this architecture
  13. are defined in Chapters 2 and 3.
  14.  
  15. This chapter discusses certain additional topics that complete the system
  16. programmer's view of the 80386 in real-address mode:
  17.  
  18.   ■  Address formation.
  19.   ■  Extensions to registers and instructions.
  20.   ■  Interrupt and exception handling.
  21.   ■  Entering and leaving real-address mode.
  22.   ■  Real-address-mode exceptions.
  23.   ■  Differences from 8086.
  24.   ■  Differences from 80286 real-address mode.
  25.  
  26.  
  27. 14.1  Physical Address Formation
  28.  
  29. The 80386 provides a one Mbyte + 64 Kbyte memory space for an 8086 program.
  30. Segment relocation is performed as in the 8086: the 16-bit value in a
  31. segment selector is shifted left by four bits to form the base address of a
  32. segment. The effective address is extended with four high order zeros and
  33. added to the base to form a linear address as Figure 14-1 illustrates. (The
  34. linear address is equivalent to the physical address, because paging is not
  35. used in real-address mode.) Unlike the 8086, the resulting linear address
  36. may have up to 21 significant bits. There is a possibility of a carry when
  37. the base address is added to the effective address. On the 8086, the carried
  38. bit is truncated, whereas on the 80386 the carried bit is stored in bit
  39. position 20 of the linear address.
  40.  
  41. Unlike the 8086 and 80286, 32-bit effective addresses can be generated (via
  42. the address-size prefix); however, the value of a 32-bit address may not
  43. exceed 65535 without causing an exception. For full compatibility with 80286
  44. real-address mode, pseudo-protection faults (interrupt 12 or 13 with no
  45. error code) occur if an effective address is generated outside the range 0
  46. through 65535.
  47.  
  48.  
  49. Figure 14-1.  Real-Address Mode Address Formation
  50.  
  51.                       19                                3       0
  52.                      ╔═════════════════════════════════╪═════════╗
  53.          BASE        ║     16-BIT SEGMENT SELECTOR     │ 0 0 0 0 ║
  54.                      ╚═════════════════════════════════╪═════════╝
  55.  
  56.          +
  57.                       19        15                              0
  58.                      ╔═════════╪═════════════════════════════════╗
  59.          OFFSET      ║ 0 0 0 0 │    16-BIT EFFECTIVE ADDRESS     ║
  60.                      ╚═════════╪═════════════════════════════════╝
  61.  
  62.          =
  63.                     20                                          0
  64.          LINEAR    ╔═════════════════════════════════════════════╗
  65.          ADDRESS   ║ X X X X X X X X X X X X X X X X X X X X X X ║
  66.                    ╚═════════════════════════════════════════════╝
  67.  
  68.  
  69. 14.2  Registers and Instructions
  70.  
  71. The register set available in real-address mode includes all the registers
  72. defined for the 8086 plus the new registers introduced by the 80386: FS, GS,
  73. debug registers, control registers, and test registers. New instructions
  74. that explicitly operate on the segment registers FS and GS are available,
  75. and the new segment-override prefixes can be used to cause instructions to
  76. utilize FS and GS for address calculations. Instructions can utilize 32-bit
  77. operands through the use of the operand size prefix.
  78.  
  79. The instruction codes that cause undefined opcode traps (interrupt 6)
  80. include instructions of the protected mode that manipulate or interrogate
  81. 80386 selectors and descriptors; namely, VERR, VERW, LAR, LSL, LTR, STR,
  82. LLDT, and SLDT. Programs executing in real-address mode are able to take
  83. advantage of the new applications-oriented instructions added to the
  84. architecture by the introduction of the 80186/80188, 80286 and 80386:
  85.  
  86. ■ New instructions introduced by 80186/80188 and 80286.
  87.  
  88.    ── PUSH immediate data
  89.    ── Push all and pop all (PUSHA and POPA)
  90.    ── Multiply immediate data
  91.    ── Shift and rotate by immediate count
  92.    ── String I/O
  93.    ── ENTER and LEAVE
  94.    ── BOUND
  95.  
  96. ■ New instructions introduced by 80386.
  97.  
  98.    ── LSS, LFS, LGS instructions
  99.    ── Long-displacement conditional jumps
  100.    ── Single-bit instructions
  101.    ── Bit scan
  102.    ── Double-shift instructions
  103.    ── Byte set on condition
  104.    ── Move with sign/zero extension
  105.    ── Generalized multiply
  106.    ── MOV to and from control registers
  107.    ── MOV to and from test registers
  108.    ── MOV to and from debug registers
  109.  
  110.  
  111. 14.3  Interrupt and Exception Handling
  112.  
  113. Interrupts and exceptions in 80386 real-address mode work as much as they
  114. do on an 8086. Interrupts and exceptions vector to interrupt procedures via
  115. an interrupt table. The processor multiplies the interrupt or exception
  116. identifier by four to obtain an index into the interrupt table. The entries
  117. of the interrupt table are far pointers to the entry points of interrupt or
  118. exception handler procedures. When an interrupt occurs, the processor
  119. pushes the current values of CS:IP onto the stack, disables interrupts,
  120. clears TF (the single-step flag), then transfers control to the location
  121. specified in the interrupt table. An IRET instruction at the end of the
  122. handler procedure reverses these steps before returning control to the
  123. interrupted procedure.
  124.  
  125. The primary difference in the interrupt handling of the 80386 compared to
  126. the 8086 is that the location and size of the interrupt table depend on the
  127. contents of the IDTR (IDT register). Ordinarily, this fact is not apparent
  128. to programmers, because, after RESET, the IDTR contains a base address of 0
  129. and a limit of 3FFH, which is compatible with the 8086. However, the LIDT
  130. instruction can be used in real-address mode to change the base and limit
  131. values in the IDTR. Refer to Chapter 9 for details on the IDTR, and the
  132. LIDT and SIDT instructions. If an interrupt occurs and the corresponding
  133. entry of the interrupt table is beyond the limit stored in the IDTR, the
  134. processor raises exception 8.
  135.  
  136.  
  137. 14.4  Entering and Leaving Real-Address Mode
  138.  
  139. Real-address mode is in effect after a signal on the RESET pin. Even if the
  140. system is going to be used in protected mode, the start-up program will
  141. execute in real-address mode temporarily while initializing for protected
  142. mode.
  143.  
  144.  
  145. 14.4.1  Switching to Protected Mode
  146.  
  147. The only way to leave real-address mode is to switch to protected mode. The
  148. processor enters protected mode when a MOV to CR0 instruction sets the PE
  149. (protection enable) bit in CR0. (For compatibility with the 80286, the LMSW
  150. instruction may also be used to set the PE bit.)
  151.  
  152. Refer to Chapter 10 "Initialization" for other aspects of switching to
  153. protected mode.
  154.  
  155.  
  156. 14.5  Switching Back to Real-Address Mode
  157.  
  158. The processor reenters real-address mode if software clears the PE bit in
  159. CR0 with a MOV to CR0 instruction. A procedure that attempts to do this,
  160. however, should proceed as follows:
  161.  
  162.   1.  If paging is enabled, perform the following sequence:
  163.  
  164.       ■  Transfer control to linear addresses that have an identity mapping;
  165.          i.e., linear addresses equal physical addresses.
  166.  
  167.       ■  Clear the PG bit in CR0.
  168.  
  169.       ■  Move zeros to CR3 to clear out the paging cache.
  170.  
  171.   2.  Transfer control to a segment that has a limit of 64K (FFFFH). This
  172.       loads the CS register with the limit it needs to have in real mode.
  173.  
  174.   3.  Load segment registers SS, DS, ES, FS, and GS with a selector that
  175.       points to a descriptor containing the following values, which are
  176.       appropriate to real mode:
  177.  
  178.       ■  Limit = 64K   (FFFFH)
  179.       ■  Byte granular (G = 0)
  180.       ■  Expand up     (E = 0)
  181.       ■  Writable      (W = 1)
  182.       ■  Present       (P = 1)
  183.       ■  Base = any value
  184.  
  185.   4.  Disable interrupts. A CLI instruction disables INTR interrupts. NMIs
  186.       can be disabled with external circuitry.
  187.  
  188.   5.  Clear the PE bit.
  189.  
  190.   6.  Jump to the real mode code to be executed using a far JMP. This
  191.       action flushes the instruction queue and puts appropriate values in
  192.       the access rights of the CS register.
  193.  
  194.   7.  Use the LIDT instruction to load the base and limit of the real-mode
  195.       interrupt vector table.
  196.  
  197.   8.  Enable interrupts.
  198.  
  199.   9.  Load the segment registers as needed by the real-mode code.
  200.  
  201.  
  202. 14.6  Real-Address Mode Exceptions
  203.  
  204. The 80386 reports some exceptions differently when executing in
  205. real-address mode than when executing in protected mode. Table 14-1 details
  206. the real-address-mode exceptions.
  207.  
  208.  
  209. 14.7  Differences From 8086
  210.  
  211. In general, the 80386 in real-address mode will correctly execute ROM-based
  212. software designed for the 8086, 8088, 80186, and 80188. Following is a list
  213. of the minor differences between 8086 execution on the 80386 and on an 8086.
  214.  
  215.   1.  Instruction clock counts.
  216.  
  217.       The 80386 takes fewer clocks for most instructions than the 8086/8088.
  218.       The areas most likely to be affected are:
  219.  
  220.       ■  Delays required by I/O devices between I/O operations.
  221.  
  222.       ■  Assumed delays with 8086/8088 operating in parallel with an 8087.
  223.  
  224.   2.  Divide Exceptions Point to the DIV instruction.
  225.  
  226.       Divide exceptions on the 80386 always leave the saved CS:IP value
  227.       pointing to the instruction that failed. On the 8086/8088, the CS:IP
  228.       value points to the next instruction.
  229.  
  230.   3.  Undefined 8086/8088 opcodes.
  231.  
  232.       Opcodes that were not defined for the 8086/8088 will cause exception
  233.       6 or will execute one of the new instructions defined for the 80386.
  234.  
  235.   4.  Value written by PUSH SP.
  236.  
  237.       The 80386 pushes a different value on the stack for PUSH SP than the
  238.       8086/8088. The 80386 pushes the value of SP before SP is incremented
  239.       as part of the push operation; the 8086/8088 pushes the value of SP
  240.       after it is incremented. If the value pushed is important, replace
  241.       PUSH SP instructions with the following three instructions:
  242.  
  243.       PUSH  BP
  244.       MOV   BP, SP
  245.       XCHG  BP, [BP]
  246.  
  247.       This code functions as the 8086/8088 PUSH SP instruction on the 80386.
  248.  
  249.   5.  Shift or rotate by more than 31 bits.
  250.  
  251.       The 80386 masks all shift and rotate counts to the low-order five
  252.       bits. This MOD 32 operation limits the count to a maximum of 31 bits,
  253.       thereby limiting the time that interrupt response is delayed while
  254.       the instruction is executing.
  255.  
  256.   6.  Redundant prefixes.
  257.  
  258.       The 80386 sets a limit of 15 bytes on instruction length. The only
  259.       way to violate this limit is by putting redundant prefixes before an
  260.       instruction. Exception 13 occurs if the limit on instruction length
  261.       is violated. The 8086/8088 has no instruction length limit.
  262.  
  263.   7.  Operand crossing offset 0 or 65,535.
  264.  
  265.       On the 8086, an attempt to access a memory operand that crosses
  266.       offset 65,535 (e.g., MOV a word to offset 65,535) or offset 0 (e.g.,
  267.       PUSH a word when SP = 1) causes the offset to wrap around modulo
  268.       65,536. The 80386 raises an exception in these cases──exception 13 if
  269.       the segment is a data segment (i.e., if CS, DS, ES, FS, or GS is being
  270.       used to address the segment), exception 12 if the segment is a stack
  271.       segment (i.e., if SS is being used).
  272.  
  273.   8.  Sequential execution across offset 65,535.
  274.  
  275.       On the 8086, if sequential execution of instructions proceeds past
  276.       offset 65,535, the processor fetches the next instruction byte from
  277.       offset 0 of the same segment. On the 80386, the processor raises
  278.       exception 13 in such a case.
  279.  
  280.   9.  LOCK is restricted to certain instructions.
  281.  
  282.       The LOCK prefix and its corresponding output signal should only be
  283.       used to prevent other bus masters from interrupting a data movement
  284.       operation. The 80386 always asserts the LOCK signal during an XCHG
  285.       instruction with memory (even if the LOCK prefix is not used). LOCK
  286.       may only be used with the following 80386 instructions when they
  287.       update memory: BTS, BTR, BTC, XCHG, ADD, ADC, SUB, SBB, INC, DEC,
  288.       AND, OR, XOR, NOT, and NEG. An undefined-opcode exception
  289.       (interrupt 6) results from using LOCK before any other instruction.
  290.  
  291.  10.  Single-stepping external interrupt handlers.
  292.  
  293.       The priority of the 80386 single-step exception is different from that
  294.       of the 8086/8088. The change prevents an external interrupt handler
  295.       from being single-stepped if the interrupt occurs while a program is
  296.       being single-stepped. The 80386 single-step exception has higher
  297.       priority that any external interrupt. The 80386 will still single-step
  298.       through an interrupt handler invoked by the INT instructions or by an
  299.       exception.
  300.  
  301.  11.  IDIV exceptions for quotients of 80H or 8000H.
  302.  
  303.       The 80386 can generate the largest negative number as a quotient for
  304.       the IDIV instruction. The 8086/8088 causes exception zero instead.
  305.  
  306.  12.  Flags in stack.
  307.  
  308.       The setting of the flags stored by PUSHF, by interrupts, and by
  309.       exceptions is different from that stored by the 8086 in bit positions
  310.       12 through 15. On the 8086 these bits are stored as ones, but in
  311.       80386 real-address mode bit 15 is always zero, and bits 14 through 12
  312.       reflect the last value loaded into them.
  313.  
  314.  13.  NMI interrupting NMI handlers.
  315.  
  316.       After an NMI is recognized on the 80386, the NMI interrupt is masked
  317.       until an IRET instruction is executed.
  318.  
  319.  14.  Coprocessor errors vector to interrupt 16.
  320.  
  321.       Any 80386 system with a coprocessor must use interrupt vector 16 for
  322.       the coprocessor error exception. If an 8086/8088 system uses another
  323.       vector for the 8087 interrupt, both vectors should point to the
  324.       coprocessor-error exception handler.
  325.  
  326.  15.  Numeric exception handlers should allow prefixes.
  327.  
  328.       On the 80386, the value of CS:IP saved for coprocessor exceptions
  329.       points at any prefixes before an ESC instruction. On 8086/8088
  330.       systems, the saved CS:IP points to the ESC instruction.
  331.  
  332.  16.  Coprocessor does not use interrupt controller.
  333.  
  334.       The coprocessor error signal to the 80386 does not pass through an
  335.       interrupt controller (an 8087 INT signal does). Some instructions in
  336.       a coprocessor error handler may need to be deleted if they deal with
  337.       the interrupt controller.
  338.  
  339.  17.  Six new interrupt vectors.
  340.  
  341.       The 80386 adds six exceptions that arise only if the 8086 program has
  342.       a hidden bug. It is recommended that exception handlers be added that
  343.       treat these exceptions as invalid operations. This additional
  344.       software does not significantly affect the existing 8086 software
  345.       because the interrupts do not normally occur. These interrupt
  346.       identifiers should not already have been used by the 8086 software,
  347.       because they are in the range reserved by Intel. Table 14-2 describes
  348.       the new 80386 exceptions.
  349.  
  350.  18.  One megabyte wraparound.
  351.  
  352.       The 80386 does not wrap addresses at 1 megabyte in real-address mode.
  353.       On members of the 8086 family, it possible to specify addresses
  354.       greater than one megabyte.  For example, with a selector value 0FFFFH
  355.       and an offset of 0FFFFH, the effective address would be 10FFEFH (1
  356.       Mbyte + 65519).  The 8086, which can form adresses only up to 20 bits
  357.       long, truncates the high-order bit, thereby "wrapping" this address
  358.       to 0FFEFH.  However, the 80386, which can form addresses up to 32
  359.       bits long does not truncate such an address.
  360.  
  361.  
  362. Table 14-1. 80386 Real-Address Mode Exceptions
  363.  
  364.  
  365. Description                      Interrupt  Function that Can                   Return Address
  366.                                  Number     Generate the Exception              Points to Faulting
  367.                                                                                 Instruction
  368. Divide error                     0          DIV, IDIV                           YES
  369. Debug exceptions                 1          All                                 
  370. Some debug exceptions point to the faulting instruction, others to the
  371. next instruction. The exception handler can determine which has occurred by
  372. examining DR6.
  373.  
  374.  
  375.  
  376.  
  377.  
  378. Breakpoint                       3          INT                                 NO
  379. Overflow                         4          INTO                                NO
  380. Bounds check                     5          BOUND                               YES
  381. Invalid opcode                   6          Any undefined opcode or LOCK        YES
  382.                                             used with wrong instruction
  383. Coprocessor not available        7          ESC or WAIT                         YES
  384. Interrupt table limit too small  8          INT vector is not within IDTR       YES
  385.                                             limit
  386. Reserved                         9-12
  387. Stack fault                      12         Memory operand crosses offset       YES
  388.                                             0 or 0FFFFH
  389. Pseudo-protection exception      13         Memory operand crosses offset       YES
  390.                                             0FFFFH or attempt to execute
  391.                                             past offset 0FFFFH or
  392.                                             instruction longer than 15
  393.                                             bytes
  394. Reserved                         14,15
  395. Coprocessor error                16         ESC or WAIT                         YES
  396. Coprocessor errors are reported on the first ESC or WAIT instruction
  397. after the ESC instruction that caused the error.
  398.  
  399.  
  400.  
  401.  
  402.  
  403. Two-byte SW interrupt            0-255      INT n                               NO
  404.  
  405.  
  406. Table 14-2. New 80386 Exceptions
  407.  
  408. Interrupt   Function
  409. Identifier
  410.  
  411.     5       A BOUND instruction was executed with a register value outside
  412.             the limit values.
  413.  
  414.     6       An undefined opcode was encountered or LOCK was used improperly
  415.             before an instruction to which it does not apply.
  416.  
  417.     7       The EM bit in the MSW is set when an ESC instruction was
  418.             encountered. This exception also occurs on a WAIT instruction
  419.             if TS is set.
  420.  
  421.     8       An exception or interrupt has vectored to an interrupt table
  422.             entry beyond the interrupt table limit in IDTR. This can occur
  423.             only if the LIDT instruction has changed the limit from the
  424.             default value of 3FFH, which is enough for all 256 interrupt
  425.             IDs.
  426.  
  427.    12       Operand crosses extremes of stack segment, e.g., MOV operation
  428.             at offset 0FFFFH or push with SP=1 during PUSH, CALL, or INT.
  429.  
  430.    13       Operand crosses extremes of a segment other than a stack
  431.             segment; or sequential instruction execution attempts to
  432.             proceed beyond offset 0FFFFH; or an instruction is longer than
  433.             15 bytes (including prefixes).
  434.  
  435.  
  436. 14.8  Differences From 80286 Real-Address Mode
  437.  
  438. The few differences that exist between 80386 real-address mode and 80286
  439. real-address mode are not likely to affect any existing 80286 programs
  440. except possibly the system initialization procedures.
  441.  
  442.  
  443. 14.8.1  Bus Lock
  444.  
  445. The 80286 processor implements the bus lock function differently than the
  446. 80386. Programs that use forms of memory locking specific to the 80286 may
  447. not execute properly if transported to a specific application of the 80386.
  448.  
  449. The LOCK prefix and its corresponding output signal should only be used to
  450. prevent other bus masters from interrupting a data movement operation.  LOCK
  451. may only be used with the following 80386 instructions when they modify
  452. memory.  An undefined-opcode exception results from using LOCK before any
  453. other instruction.
  454.  
  455.   ■  Bit test and change:  BTS, BTR, BTC.
  456.   ■  Exchange: XCHG.
  457.   ■  One-operand arithmetic and logical: INC, DEC, NOT, and NEG.
  458.   ■  Two-operand arithmetic and logical: ADD, ADC, SUB, SBB, AND, OR, XOR.
  459.  
  460. A locked instruction is guaranteed to lock only the area of memory defined
  461. by the destination operand, but may lock a larger memory area.  For example,
  462. typical 8086 and 80286 configurations lock the entire physical memory space.
  463. With the 80386, the defined area of memory is guranteed to be locked against
  464. access by a processor executing a locked instruction on exactly the same
  465. memory area, i.e., an operand with identical starting address and identical
  466. length.
  467.  
  468.  
  469. 14.8.2  Location of First Instruction
  470.  
  471. The starting location is 0FFFFFFF0H (sixteen bytes from end of 32-bit
  472. address space) on the 80386 rather than 0FFFFF0H (sixteen bytes from end of
  473. 24-bit address space) as on the 80286.  Many 80286 ROM initialization
  474. programs will work correctly in this new environment.  Others can be made to
  475. work correctly with external hardware that redefines the signals on
  476. A{31-20}.
  477.  
  478.  
  479. 14.8.3  Initial Values of General Registers
  480.  
  481. On the 80386, certain general registers may contain different values after
  482. RESET than on the 80286. This should not cause compatibility problems,
  483. because the content of 8086 registers after RESET is undefined.  If
  484. self-test is requested during the reset sequence and errors are detected in
  485. the 80386 unit, EAX will contain a nonzero value. EDX contains the component
  486. and revision identifier. Refer to Chapter 10 for more information.
  487.  
  488.  
  489. 14.8.4  MSW Initialization
  490.  
  491. The 80286 initializes the MSW register to FFF0H, but the 80386 initializes
  492. this register to 0000H. This difference should have no effect, because the
  493. bits that are different are undefined on the 80286.  Programs that read the
  494. value of the MSW will behave differently on the 80386 only if they depend on
  495. the setting of the undefined, high-order bits.
  496.  
  497.  
  498.