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

  1. Chapter 9  Exceptions and Interrupts
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. Interrupts and exceptions are special kinds of control transfer; they work
  6. somewhat like unprogrammed CALLs. They alter the normal program flow to
  7. handle external events or to report errors or exceptional conditions. The
  8. difference between interrupts and exceptions is that interrupts are used to
  9. handle asynchronous events external to the processor, but exceptions handle
  10. conditions detected by the processor itself in the course of executing
  11. instructions.
  12.  
  13. There are two sources for external interrupts and two sources for
  14. exceptions:
  15.  
  16.   1.  Interrupts
  17.  
  18.       ■  Maskable interrupts, which are signalled via the INTR pin.
  19.  
  20.       ■  Nonmaskable interrupts, which are signalled via the NMI
  21.          (Non-Maskable Interrupt) pin.
  22.  
  23.   2.  Exceptions
  24.  
  25.       ■  Processor detected. These are further classified as faults, traps,
  26.          and aborts.
  27.  
  28.       ■  Programmed. The instructions INTO, INT 3, INT n, and BOUND can
  29.          trigger exceptions. These instructions are often called "software
  30.          interrupts", but the processor handles them as exceptions.
  31.  
  32. This chapter explains the features that the 80386 offers for controlling
  33. and responding to interrupts when it is executing in protected mode.
  34.  
  35.  
  36. 9.1  Identifying Interrupts
  37.  
  38. The processor associates an identifying number with each different type of
  39. interrupt or exception.
  40.  
  41. The NMI and the exceptions recognized by the processor are assigned
  42. predetermined identifiers in the range 0 through 31. Not all of these
  43. numbers are currently used by the 80386; unassigned identifiers in this
  44. range are reserved by Intel for possible future expansion.
  45.  
  46. The identifiers of the maskable interrupts are determined by external
  47. interrupt controllers (such as Intel's 8259A Programmable Interrupt
  48. Controller) and communicated to the processor during the processor's
  49. interrupt-acknowledge sequence. The numbers assigned by an 8259A PIC can be
  50. specified by software. Any numbers in the range 32 through 255 can be used.
  51. Table 9-1 shows the assignment of interrupt and exception identifiers.
  52.  
  53. Exceptions are classified as faults, traps, or aborts depending on the way
  54. they are reported and whether restart of the instruction that caused the
  55. exception is supported.
  56.  
  57. Faults  Faults are exceptions that are reported "before" the
  58.         instruction causingthe exception. Faults are either detected before
  59.         the instruction begins to execute, or during execution of the
  60.         instruction. If detected during the instruction, the fault is
  61.         reported with the machine restored to a state that permits the
  62.         instruction to be restarted.
  63.  
  64. Traps   A trap is an exception that is reported at the instruction
  65.         boundary immediately after the instruction in which the
  66.         exception was detected.
  67.  
  68. Aborts  An abort is an exception that permits neither precise location
  69.         of the instruction causing the exception nor restart of the program
  70.         that caused the exception. Aborts are used to report severe errors,
  71.         such as hardware errors and inconsistent or illegal values in system
  72.         tables.
  73.  
  74.  
  75. Table 9-1. Interrupt and Exception ID Assignments
  76.  
  77. Identifier   Description
  78.  
  79. 0            Divide error
  80. 1            Debug exceptions
  81. 2            Nonmaskable interrupt
  82. 3            Breakpoint (one-byte INT 3 instruction)
  83. 4            Overflow (INTO instruction)
  84. 5            Bounds check (BOUND instruction)
  85. 6            Invalid opcode
  86. 7            Coprocessor not available
  87. 8            Double fault
  88. 9            (reserved)
  89. 10           Invalid TSS
  90. 11           Segment not present
  91. 12           Stack exception
  92. 13           General protection
  93. 14           Page fault
  94. 15           (reserved)
  95. 16           Coprecessor error
  96. 17-31        (reserved)
  97. 32-255       Available for external interrupts via INTR pin
  98.  
  99.  
  100. 9.2  Enabling and Disabling Interrupts
  101.  
  102. The processor services interrupts and exceptions only between the end of
  103. one instruction and the beginning of the next. When the repeat prefix is
  104. used to repeat a string instruction, interrupts and exceptions may occur
  105. between repetitions. Thus, operations on long strings do not delay interrupt
  106. response.
  107.  
  108. Certain conditions and flag settings cause the processor to inhibit certain
  109. interrupts and exceptions at instruction boundaries.
  110.  
  111.  
  112. 9.2.1  NMI Masks Further NMIs
  113.  
  114. While an NMI handler is executing, the processor ignores further interrupt
  115. signals at the NMI pin until the next IRET instruction is executed.
  116.  
  117.  
  118. 9.2.2  IF Masks INTR
  119.  
  120. The IF (interrupt-enable flag) controls the acceptance of external
  121. interrupts signalled via the INTR pin. When IF=0, INTR interrupts are
  122. inhibited; when IF=1, INTR interrupts are enabled. As with the other flag
  123. bits, the processor clears IF in response to a RESET signal. The
  124. instructions CLI and STI alter the setting of IF.
  125.  
  126. CLI (Clear Interrupt-Enable Flag) and STI (Set Interrupt-Enable Flag)
  127. explicitly alter IF (bit 9 in the flag register). These instructions may be
  128. executed only if CPL ≤ IOPL. A protection exception occurs if they are
  129. executed when CPL > IOPL.
  130.  
  131. The IF is also affected implicitly by the following operations:
  132.  
  133.   ■  The instruction PUSHF stores all flags, including IF, in the stack
  134.      where they can be examined.
  135.  
  136.   ■  Task switches and the instructions POPF and IRET load the flags
  137.      register; therefore, they can be used to modify IF.
  138.  
  139.   ■  Interrupts through interrupt gates automatically reset IF, disabling
  140.      interrupts. (Interrupt gates are explained later in this chapter.)
  141.  
  142.  
  143. 9.2.3  RF Masks Debug Faults
  144.  
  145. The RF bit in EFLAGS controls the recognition of debug faults. This permits
  146. debug faults to be raised for a given instruction at most once, no matter
  147. how many times the instruction is restarted. (Refer to Chapter 12 for more
  148. information on debugging.)
  149.  
  150.  
  151. 9.2.4  MOV or POP to SS Masks Some Interrupts and Exceptions
  152.  
  153. Software that needs to change stack segments often uses a pair of
  154. instructions; for example:
  155.  
  156.   MOV SS, AX
  157.   MOV ESP, StackTop
  158.  
  159. If an interrupt or exception is processed after SS has been changed but
  160. before ESP has received the corresponding change, the two parts of the stack
  161. pointer SS:ESP are inconsistent for the duration of the interrupt handler or
  162. exception handler.
  163.  
  164. To prevent this situation, the 80386, after both a MOV to SS and a POP to
  165. SS instruction, inhibits NMI, INTR, debug exceptions, and single-step traps
  166. at the instruction boundary following the instruction that changes SS. Some
  167. exceptions may still occur; namely, page fault and general protection fault.
  168. Always use the 80386 LSS instruction, and the problem will not occur.
  169.  
  170.  
  171. 9.3  Priority Among Simultaneous Interrupts and Exceptions
  172.  
  173. If more than one interrupt or exception is pending at an instruction
  174. boundary, the processor services one of them at a time. The priority among
  175. classes of interrupt and exception sources is shown in Table 9-2. The
  176. processor first services a pending interrupt or exception from the class
  177. that has the highest priority, transferring control to the first
  178. instruction of the interrupt handler. Lower priority exceptions are
  179. discarded; lower priority interrupts are held pending. Discarded exceptions
  180. will be rediscovered when the interrupt handler returns control to the point
  181. of interruption.
  182.  
  183.  
  184. 9.4  Interrupt Descriptor Table
  185.  
  186. The interrupt descriptor table (IDT) associates each interrupt or exception
  187. identifier with a descriptor for the instructions that service the
  188. associated event. Like the GDT and LDTs, the IDT is an array of 8-byte
  189. descriptors. Unlike the GDT and LDTs, the first entry of the IDT may contain
  190. a descriptor. To form an index into the IDT, the processor multiplies the
  191. interrupt or exception identifier by eight. Because there are only 256
  192. identifiers, the IDT need not contain more than 256 descriptors. It can
  193. contain fewer than 256 entries; entries are required only for interrupt
  194. identifiers that are actually used.
  195.  
  196. The IDT may reside anywhere in physical memory. As Figure 9-1 shows, the
  197. processor locates the IDT by means of the IDT register (IDTR). The
  198. instructions LIDT and SIDT operate on the IDTR. Both instructions have one
  199. explicit operand: the address in memory of a 6-byte area. Figure 9-2 shows
  200. the format of this area.
  201.  
  202. LIDT (Load IDT register) loads the IDT register with the linear base
  203. address and limit values contained in the memory operand.  This instruction
  204. can be executed only when the CPL is zero. It is normally used by the
  205. initialization logic of an operating system when creating an IDT.  An
  206. operating system may also use it to change from one IDT to another.
  207.  
  208. SIDT (Store IDT register) copies the base and limit value stored in IDTR
  209. to a memory location. This instruction can be executed at any privilege
  210. level.
  211.  
  212.  
  213. Table 9-2. Priority Among Simultaneous Interrupts and Exceptions
  214.  
  215. Priority   Class of Interrupt or Exception
  216.  
  217. HIGHEST    Faults except debug faults
  218.            Trap instructions INTO, INT n, INT 3
  219.            Debug traps for this instruction
  220.            Debug faults for next instruction
  221.            NMI interrupt
  222. LOWEST     INTR interrupt
  223.  
  224.  
  225. Figure 9-1.  IDT Register and Table
  226.  
  227.                                               INTERRUPT DESCRIPTOR TABLE
  228.                                               ╔══════╤═════╤═════╤══════╗
  229.                                         ┌────║      │     │     │      ║
  230.                                         │     ╟─ GATE FOR INTERRUPT #N ─╢
  231.                                         │     ║      │     │     │      ║
  232.                                         │     ╚══════╧═════╧═════╧══════╝
  233.                                         │                              
  234.                                         │                              
  235.                                         │                              
  236.                                         │     ╔══════╤═════╤═════╤══════╗
  237.                                         │     ║      │     │     │      ║
  238.                                         │     ╟─ GATE FOR INTERRUPT #2 ─╢
  239.                                         │     ║      │     │     │      ║
  240.                                         │     ╠══════╤═════╤═════╤══════╣
  241.             IDT REGISTER                │     ║      │     │     │      ║
  242.                                         │     ╟─ GATE FOR INTERRUPT #1 ─╢
  243.                     15            0     │     ║      │     │     │      ║
  244.                    ╔═══════════════╗    │     ╠══════╤═════╤═════╤══════╣
  245.                    ║   IDT LIMIT   ╟────┘     ║      │     │     │      ║
  246.   ╔════════════════╩═══════════════╣          ╟─ GATE FOR INTERRUPT #0 ─╢
  247.   ║            IDT BASE            ╟─────────║      │     │     │      ║
  248.   ╚════════════════════════════════╝          ╚══════╧═════╧═════╧══════╝
  249.    31                             0
  250.  
  251.  
  252. Figure 9-2.  Pseudo-Descriptor Format for LIDT and SIDT
  253.  
  254.   31                23                15                7               0
  255.  ╔═════════════════╪═════════════════╪═════════════════╪═════════════════╗
  256.  ║                                 BASE                                  ║2
  257.  ╚═════════════════╪═════════════════╦═════════════════╪═════════════════╣
  258.                                      ║               LIMIT               ║0
  259.                                      ╚═════════════════╪═════════════════╝
  260.  
  261.  
  262. 9.5  IDT Descriptors
  263.  
  264. The IDT may contain any of three kinds of descriptor:
  265.  
  266.    ■  Task gates
  267.    ■  Interrupt gates
  268.    ■  Trap gates
  269.  
  270. Figure 9-3 illustrates the format of task gates and 80386 interrupt gates
  271. and trap gates. (The task gate in an IDT is the same as the task gate
  272. already discussed in Chapter 7.)
  273.  
  274.  
  275. Figure 9-3.  80306 IDT Gate Descriptors
  276.  
  277.                                 80386 TASK GATE
  278.    31                23                15                7                0
  279.   ╔═════════════════╪═════════════════╪═══╤═══╤═════════╪═════════════════╗
  280.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒(NOT USED)▒▒▒▒▒▒▒▒▒▒▒▒│ P │DPL│0 0 1 0 1│▒▒▒(NOT USED)▒▒▒▒║4
  281.   ╟───────────────────────────────────┼───┴───┴─────────┴─────────────────╢
  282.   ║             SELECTOR              │▒▒▒▒▒▒▒▒▒▒▒▒▒(NOT USED)▒▒▒▒▒▒▒▒▒▒▒▒║0
  283.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  284.  
  285.                                 80386 INTERRUPT GATE
  286.    31                23                15                7                0
  287.   ╔═════════════════╪═════════════════╪═══╤═══╤═════════╪═════╪═══════════╗
  288.   ║           OFFSET 31..16           │ P │DPL│0 1 1 1 0│0 0 0│(NOT USED) ║4
  289.   ╟───────────────────────────────────┼───┴───┴─────────┴─────┴───────────╢
  290.   ║             SELECTOR              │           OFFSET 15..0            ║0
  291.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  292.  
  293.                                 80386 TRAP GATE
  294.    31                23                15                7                0
  295.   ╔═════════════════╪═════════════════╪═══╤═══╤═════════╪═════╪═══════════╗
  296.   ║          OFFSET 31..16            │ P │DPL│0 1 1 1 1│0 0 0│(NOT USED) ║4
  297.   ╟───────────────────────────────────┼───┴───┴─────────┴─────┴───────────╢
  298.   ║             SELECTOR              │           OFFSET 15..0            ║0
  299.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  300.  
  301.  
  302. 9.6  Interrupt Tasks and Interrupt Procedures
  303.  
  304. Just as a CALL instruction can call either a procedure or a task, so an
  305. interrupt or exception can "call" an interrupt handler that is either a
  306. procedure or a task. When responding to an interrupt or exception, the
  307. processor uses the interrupt or exception identifier to index a descriptor
  308. in the IDT. If the processor indexes to an interrupt gate or trap gate, it
  309. invokes the handler in a manner similar to a CALL to a call gate. If the
  310. processor finds a task gate, it causes a task switch in a manner similar to
  311. a CALL to a task gate.
  312.  
  313.  
  314. 9.6.1  Interrupt Procedures
  315.  
  316. An interrupt gate or trap gate points indirectly to a procedure which will
  317. execute in the context of the currently executing task as illustrated by
  318. Figure 9-4. The selector of the gate points to an executable-segment
  319. descriptor in either the GDT or the current LDT. The offset field of the
  320. gate points to the beginning of the interrupt or exception handling
  321. procedure.
  322.  
  323. The 80386 invokes an interrupt or exception handling procedure in much the
  324. same manner as it CALLs a procedure; the differences are explained in the
  325. following sections.
  326.  
  327.  
  328. Figure 9-4.  Interrupt Vectoring for Procedures
  329.  
  330.                   IDT                                    EXECUTABLE SEGMENT
  331.            ╔═══════════════╗                             ╔═══════════════╗
  332.            ║               ║                       OFFSET║               ║
  333.            ╠═══════════════╣  ┌─────────────────────────║ ENTRY POINT   ║
  334.            ║               ║  │      LDT OR GDT          ║               ║
  335.            ╠═══════════════╣  │   ╔═══════════════╗      ║               ║
  336.            ║               ║  │   ║               ║      ║               ║
  337. INTERRUPT  ╠═══════════════╣  │   ╠═══════════════╣      ║               ║
  338.    ID─────║ TRAP GATE OR  ╟──┘   ║               ║      ║               ║
  339.            ║INTERRUPT GATE ╟──┐   ╠═══════════════╣      ║               ║
  340.            ╠═══════════════╣  │   ║               ║      ║               ║
  341.            ║               ║  │   ╠═══════════════╣      ║               ║
  342.            ╠═══════════════╣  └──║   SEGMENT     ╟─┐    ║               ║
  343.            ║               ║      ║  DESCRIPTOR   ║ │    ║               ║
  344.            ╠═══════════════╣      ╠═══════════════╣ │    ║               ║
  345.            ║               ║      ║               ║ │    ║               ║
  346.            ╠═══════════════╣      ╠═══════════════╣ │    ║               ║
  347.            ║               ║      ║               ║ │BASE║               ║
  348.            ╚═══════════════╝      ╠═══════════════╣ └───╚═══════════════╝
  349.                                   ║               ║
  350.                                   ║               ║
  351.                                   ║               ║
  352.                                   ╚═══════════════╝
  353.  
  354.  
  355. 9.6.1.1  Stack of Interrupt Procedure
  356.  
  357. Just as with a control transfer due to a CALL instruction, a control
  358. transfer to an interrupt or exception handling procedure uses the stack to
  359. store the information needed for returning to the original procedure. As
  360. Figure 9-5 shows, an interrupt pushes the EFLAGS register onto the stack
  361. before the pointer to the interrupted instruction.
  362.  
  363. Certain types of exceptions also cause an error code to be pushed on the
  364. stack. An exception handler can use the error code to help diagnose the
  365. exception.
  366.  
  367.  
  368. 9.6.1.2  Returning from an Interrupt Procedure
  369.  
  370. An interrupt procedure also differs from a normal procedure in the method
  371. of leaving the procedure. The IRET instruction is used to exit from an
  372. interrupt procedure. IRET is similar to RET except that IRET increments EIP
  373. by an extra four bytes (because of the flags on the stack) and moves the
  374. saved flags into the EFLAGS register. The IOPL field of EFLAGS is changed
  375. only if the CPL is zero. The IF flag is changed only if CPL ≤ IOPL.
  376.  
  377.  
  378. Figure 9-5.  Stack Layout after Exception of Interrupt 
  379.  
  380.                            WITHOUT PRIVILEGE TRANSITION
  381.  
  382.       D  O      31          0                     31          0
  383.       I  F    ╠═══════╦═══════╣                 ╠═══════╦═══════╣
  384.       R       ║▒▒▒▒▒▒▒║▒▒▒▒▒▒▒║    OLD          ║▒▒▒▒▒▒▒║▒▒▒▒▒▒▒║    OLD
  385.       E  E    ╠═══════╬═══════╣   SS:ESP        ╠═══════╬═══════╣   SS:ESP
  386.       C  X    ║▒▒▒▒▒▒▒║▒▒▒▒▒▒▒║     │           ║▒▒▒▒▒▒▒║▒▒▒▒▒▒▒║     │
  387.       T  P    ╠═══════╩═══════╣────┘           ╠═══════╩═══════╣────┘
  388.       I  A    ║  OLD EFLAGS   ║                 ║  OLD EFLAGS   ║
  389.       O  N    ╠═══════╦═══════╣                 ╠═══════╦═══════╣
  390.       N  S    ║▒▒▒▒▒▒▒║OLD CS ║    NEW          ║▒▒▒▒▒▒▒║OLD CS ║
  391.          I    ╠═══════╩═══════╣   SS:ESP        ╠═══════╩═══════╣
  392.        │ O    ║    OLD EIP    ║     │           ║    OLD EIP    ║    NEW
  393.        │ N    ╠═══════════════╣────┘           ╠═══════════════╣   SS:ESP
  394.        │      ║               ║                 ║  ERROR CODE   ║     │
  395.                                              ╠═══════════════╣────┘
  396.                                               ║               ║
  397.                              
  398.               WITHOUT ERROR CODE                 WITH ERROR CODE
  399.  
  400.                              WITH PRIVILEGE TRANSITION
  401.  
  402.       D  O     31            0                     31          0
  403.       I  F    ╔═══════╦═══════╗────┐           ╔═══════╦═══════╗────┐
  404.       R       ║▒▒▒▒▒▒▒║OLD SS ║     │           ║▒▒▒▒▒▒▒║OLD SS ║     │
  405.       E  E    ╠═══════╩═══════╣   SS:ESP        ╠═══════╩═══════╣   SS:ESP
  406.       C  X    ║    OLD ESP    ║  FROM TSS       ║    OLD ESP    ║  FROM TSS
  407.       T  P    ╠═══════════════╣                 ╠═══════════════╣
  408.       I  A    ║  OLD EFLAGS   ║                 ║  OLD EFLAGS   ║
  409.       O  N    ╠═══════╦═══════╣                 ╠═══════╦═══════╣
  410.       N  S    ║▒▒▒▒▒▒▒║OLD CS ║    NEW          ║▒▒▒▒▒▒▒║OLD CS ║
  411.          I    ╠═══════╩═══════╣   SS:EIP        ╠═══════╩═══════╣
  412.        │ O    ║    OLD EIP    ║     │           ║    OLD EIP    ║    NEW
  413.        │ N    ╠═══════════════╣────┘           ╠═══════════════╣   SS:ESP
  414.        │      ║               ║                 ║  ERROR CODE   ║     │
  415.                                              ╠═══════════════╣────┘
  416.                                               ║               ║
  417.                              
  418.               WITHOUT ERROR CODE                 WITH ERROR CODE
  419.  
  420.  
  421. 9.6.1.3  Flags Usage by Interrupt Procedure
  422.  
  423. Interrupts that vector through either interrupt gates or trap gates cause
  424. TF (the trap flag) to be reset after the current value of TF is saved on the
  425. stack as part of EFLAGS. By this action the processor prevents debugging
  426. activity that uses single-stepping from affecting interrupt response. A
  427. subsequent IRET instruction restores TF to the value in the EFLAGS image on
  428. the stack.
  429.  
  430. The difference between an interrupt gate and a trap gate is in the effect
  431. on IF (the interrupt-enable flag). An interrupt that vectors through an
  432. interrupt gate resets IF, thereby preventing other interrupts from
  433. interfering with the current interrupt handler. A subsequent IRET
  434. instruction restores IF to the value in the EFLAGS image on the stack. An
  435. interrupt through a trap gate does not change IF.
  436.  
  437.  
  438. 9.6.1.4  Protection in Interrupt Procedures
  439.  
  440. The privilege rule that governs interrupt procedures is similar to that for
  441. procedure calls: the CPU does not permit an interrupt to transfer control to
  442. a procedure in a segment of lesser privilege (numerically greater privilege
  443. level) than the current privilege level. An attempt to violate this rule
  444. results in a general protection exception.
  445.  
  446. Because occurrence of interrupts is not generally predictable, this
  447. privilege rule effectively imposes restrictions on the privilege levels at
  448. which interrupt and exception handling procedures can execute. Either of the
  449. following strategies can be employed to ensure that the privilege rule is
  450. never violated.
  451.  
  452.   ■  Place the handler in a conforming segment. This strategy suits the
  453.      handlers for certain exceptions (divide error, for example). Such a
  454.      handler must use only the data available to it from the stack. If it
  455.      needed data from a data segment, the data segment would have to have
  456.      privilege level three, thereby making it unprotected.
  457.  
  458.   ■  Place the handler procedure in a privilege level zero segment.
  459.  
  460.  
  461. 9.6.2  Interrupt Tasks
  462.  
  463. A task gate in the IDT points indirectly to a task, as Figure 9-6
  464. illustrates. The selector of the gate points to a TSS descriptor in the GDT.
  465.  
  466. When an interrupt or exception vectors to a task gate in the IDT, a task
  467. switch results. Handling an interrupt with a separate task offers two
  468. advantages:
  469.  
  470.   ■  The entire context is saved automatically.
  471.  
  472.   ■  The interrupt handler can be isolated from other tasks by giving it a
  473.      separate address space, either via its LDT or via its page directory.
  474.  
  475. The actions that the processor takes to perform a task switch are discussed
  476. in Chapter 7. The interrupt task returns to the interrupted task by
  477. executing an IRET instruction.
  478.  
  479. If the task switch is caused by an exception that has an error code, the
  480. processor automatically pushes the error code onto the stack that
  481. corresponds to the privilege level of the first instruction to be executed
  482. in the interrupt task.
  483.  
  484. When interrupt tasks are used in an operating system for the 80386, there
  485. are actually two schedulers: the software scheduler (part of the operating
  486. system) and the hardware scheduler (part of the processor's interrupt
  487. mechanism). The design of the software scheduler should account for the fact
  488. that the hardware scheduler may dispatch an interrupt task whenever
  489. interrupts are enabled.
  490.  
  491.  
  492. Figure 9-6.  Interrupt Vectoring for Tasks
  493.  
  494.             IDT                       GDT
  495.      ╔════════════════╗        ╔════════════════╗
  496.      ║                ║        ║                ║              TSS
  497.      ╟────────────────╢        ╟────────────────╢       ╔════════════════╗
  498.      ║                ║        ║                ║       ║                ║
  499.      ╟────────────────╢        ╟────────────────╢       ║                ║
  500.      ║                ║        ║                ║       ║                ║
  501.      ╟────────────────╢        ╟────────────────╢       ║                ║
  502.  ┌──║   TASK GATE    ╟───┐    ║                ║       ║                ║
  503.  │   ╟────────────────╢   │    ╟────────────────╢       ║                ║
  504.  │   ║                ║   └───║ TSS DESCRIPTOR ╟───┐   ║                ║
  505.  │   ╟────────────────╢        ╟────────────────╢   │   ║                ║
  506.  │   ║                ║        ║                ║   │   ║                ║
  507.  │   ╟────────────────╢        ╟────────────────╢   └──╚════════════════╝
  508.  │   ║                ║        ║                ║
  509.  │   ╟────────────────╢        ╟────────────────╢
  510.  │   ║                ║        ║                ║
  511.  │   ╚════════════════╝        ╚════════════════╝
  512.  │
  513.  └─INTERRUPT ID
  514.  
  515.  
  516. 9.7  Error Code
  517.  
  518. With exceptions that relate to a specific segment, the processor pushes an
  519. error code onto the stack of the exception handler (whether procedure or
  520. task). The error code has the format shown in Figure 9-7. The format of the
  521. error code resembles that of a selector; however, instead of an RPL field,
  522. the error code contains two one-bit items:
  523.  
  524.   1.  The processor sets the EXT bit if an event external to the program
  525.       caused the exception.
  526.  
  527.   2.  The processor sets the I-bit (IDT-bit) if the index portion of the
  528.       error code refers to a gate descriptor in the IDT.
  529.  
  530. If the I-bit is not set, the TI bit indicates whether the error code refers
  531. to the GDT (value 0) or to the LDT (value 1). The remaining 14 bits are the
  532. upper 14 bits of the segment selector involved. In some cases the error code
  533. on the stack is null, i.e., all bits in the low-order word are zero.
  534.  
  535.  
  536. Figure 9-7.  Error Code Format
  537.  
  538.        31              15                                         2 1 0
  539.       ╔═══════════════╪════════════════╤═════════════════╪═══════╤═╤═╤═╗
  540.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                         │T│ │E║
  541.       ║▒▒▒▒▒▒▒▒▒▒▒UNDEFINED▒▒▒▒▒▒▒▒▒▒▒▒│     SELECTOR INDEX      │ │I│ ║
  542.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                         │I│ │X║
  543.       ╚═══════════════╪════════════════╧═════════════════╪═══════╧═╧═╧═╝
  544.  
  545.  
  546. 9.8  Exception Conditions
  547.  
  548. The following sections describe each of the possible exception conditions
  549. in detail. Each description classifies the exception as a fault, trap, or
  550. abort. This classification provides information needed by systems
  551. programmers for restarting the procedure in which the exception occurred:
  552.  
  553. Faults   The CS and EIP values saved when a fault is reported point to the
  554.          instruction causing the fault.
  555.  
  556. Traps    The CS and EIP values stored when the trap is reported point to the
  557.          instruction dynamically after the instruction causing the trap. If
  558.          a trap is detected during an instruction that alters program flow,
  559.          the reported values of CS and EIP reflect the alteration of program
  560.          flow. For example, if a trap is detected in a JMP instruction, the
  561.          CS and EIP values pushed onto the stack point to the target of the
  562.          JMP, not to the instruction after the JMP.
  563.  
  564. Aborts   An abort is an exception that permits neither precise location of
  565.          the instruction causing the exception nor restart of the program
  566.          that caused the exception. Aborts are used to report severe errors,
  567.          such as hardware errors and inconsistent or illegal values in
  568.          system tables.
  569.  
  570.  
  571. 9.8.1  Interrupt 0 ── Divide Error
  572.  
  573. The divide-error fault occurs during a DIV or an IDIV instruction when the
  574. divisor is zero.
  575.  
  576.  
  577. 9.8.2  Interrupt 1 ── Debug Exceptions
  578.  
  579. The processor triggers this interrupt for any of a number of conditions;
  580. whether the exception is a fault or a trap depends on the condition:
  581.  
  582.    ■  Instruction address breakpoint fault.
  583.    ■  Data address breakpoint trap.
  584.    ■  General detect fault.
  585.    ■  Single-step trap.
  586.    ■  Task-switch breakpoint trap.
  587.  
  588. The processor does not push an error code for this exception. An exception
  589. handler can examine the debug registers to determine which condition caused
  590. the exception. Refer to Chapter 12 for more detailed information about
  591. debugging and the debug registers.
  592.  
  593.  
  594. 9.8.3  Interrupt 3 ── Breakpoint
  595.  
  596. The INT 3 instruction causes this trap. The INT 3 instruction is one byte
  597. long, which makes it easy to replace an opcode in an executable segment with
  598. the breakpoint opcode. The operating system or a debugging subsystem can use
  599. a data-segment alias for an executable segment to place an INT 3 anywhere it
  600. is convenient to arrest normal execution so that some sort of special
  601. processing can be performed. Debuggers typically use breakpoints as a way of
  602. displaying registers, variables, etc., at crucial points in a task.
  603.  
  604. The saved CS:EIP value points to the byte following the breakpoint. If a
  605. debugger replaces a planted breakpoint with a valid opcode, it must subtract
  606. one from the saved EIP value before returning. Refer also to Chapter 12 for
  607. more information on debugging.
  608.  
  609.  
  610. 9.8.4  Interrupt 4 ── Overflow
  611.  
  612. This trap occurs when the processor encounters an INTO instruction and the
  613. OF (overflow) flag is set. Since signed arithmetic and unsigned arithmetic
  614. both use the same arithmetic instructions, the processor cannot determine
  615. which is intended and therefore does not cause overflow exceptions
  616. automatically. Instead it merely sets OF when the results, if interpreted as
  617. signed numbers, would be out of range. When doing arithmetic on signed
  618. operands, careful programmers and compilers either test OF directly or use
  619. the INTO instruction.
  620.  
  621.  
  622. 9.8.5  Interrupt 5 ── Bounds Check
  623.  
  624. This fault occurs when the processor, while executing a BOUND instruction,
  625. finds that the operand exceeds the specified limits. A program can use the
  626. BOUND instruction to check a signed array index against signed limits
  627. defined in a block of memory.
  628.  
  629.  
  630. 9.8.6  Interrupt 6 ── Invalid Opcode
  631.  
  632. This fault occurs when an invalid opcode is detected by the execution unit.
  633. (The exception is not detected until an attempt is made to execute the
  634. invalid opcode; i.e., prefetching an invalid opcode does not cause this
  635. exception.) No error code is pushed on the stack. The exception can be
  636. handled within the same task.
  637.  
  638. This exception also occurs when the type of operand is invalid for the
  639. given opcode. Examples include an intersegment JMP referencing a register
  640. operand, or an LES instruction with a register source operand.
  641.  
  642.  
  643. 9.8.7  Interrupt 7 ── Coprocessor Not Available
  644.  
  645. This exception occurs in either of two conditions:
  646.  
  647.   ■  The processor encounters an ESC (escape) instruction, and the EM
  648.      (emulate) bit ofCR0 (control register zero) is set.
  649.  
  650.   ■  The processor encounters either the WAIT instruction or an ESC
  651.      instruction, and both the MP (monitor coprocessor) and TS (task
  652.      switched) bits of CR0 are set.
  653.  
  654. Refer to Chapter 11 for information about the coprocessor interface.
  655.  
  656.  
  657. 9.8.8  Interrupt 8 ── Double Fault
  658.  
  659. Normally, when the processor detects an exception while trying to invoke
  660. the handler for a prior exception, the two exceptions can be handled
  661. serially. If, however, the processor cannot handle them serially, it signals
  662. the double-fault exception instead. To determine when two faults are to be
  663. signalled as a double fault, the 80386 divides the exceptions into three
  664. classes: benign exceptions, contributory exceptions, and page faults. Table
  665. 9-3 shows this classification.
  666.  
  667. Table 9-4 shows which combinations of exceptions cause a double fault and
  668. which do not.
  669.  
  670. The processor always pushes an error code onto the stack of the
  671. double-fault handler; however, the error code is always zero. The faulting
  672. instruction may not be restarted. If any other exception occurs while
  673. attempting to invoke the double-fault handler, the processor shuts down.
  674.  
  675.  
  676. Table 9-3. Double-Fault Detection Classes
  677.  
  678. Class           ID          Description
  679.  
  680.                  1          Debug exceptions
  681.                  2          NMI
  682.                  3          Breakpoint
  683. Benign           4          Overflow
  684. Exceptions       5          Bounds check
  685.                  6          Invalid opcode
  686.                  7          Coprocessor not available
  687.                 16          Coprocessor error
  688.  
  689.                  0          Divide error
  690.                  9          Coprocessor Segment Overrun
  691. Contributory    10          Invalid TSS
  692. Exceptions      11          Segment not present
  693.                 12          Stack exception
  694.                 13          General protection
  695.  
  696. Page Faults     14          Page fault
  697.  
  698.  
  699. Table 9-4. Double-Fault Definition
  700.  
  701.                                    SECOND EXCEPTION
  702.  
  703.                            Benign       Contributory    Page
  704.                            Exception    Exception       Fault
  705.  
  706.  
  707.            Benign          OK           OK              OK
  708.            Exception
  709.  
  710. FIRST      Contributory    OK           DOUBLE          OK
  711. EXCEPTION  Exception
  712.  
  713.            Page
  714.            Fault           OK           DOUBLE          DOUBLE
  715.  
  716.  
  717. 9.8.9  Interrupt 9 ── Coprocessor Segment Overrun
  718.  
  719. This exception is raised in protected mode if the 80386 detects a page or
  720. segment violation while transferring the middle portion of a coprocessor
  721. operand to the NPX. This exception is avoidable. Refer to Chapter 11 for
  722. more information about the coprocessor interface.
  723.  
  724.  
  725. 9.8.10  Interrupt 10 ── Invalid TSS
  726.  
  727. Interrupt 10 occurs if during a task switch the new TSS is invalid. A TSS
  728. is considered invalid in the cases shown in Table 9-5. An error code is
  729. pushed onto the stack to help identify the cause of the fault. The EXT bit
  730. indicates whether the exception was caused by a condition outside the
  731. control of the program; e.g., an external interrupt via a task gate
  732. triggered a switch to an invalid TSS.
  733.  
  734. This fault can occur either in the context of the original task or in the
  735. context of the new task. Until the processor has completely verified the
  736. presence of the new TSS, the exception occurs in the context of the original
  737. task. Once the existence of the new TSS is verified, the task switch is
  738. considered complete; i.e., TR is updated and, if the switch is due to a
  739. CALL or interrupt, the backlink of the new TSS is set to the old TSS. Any
  740. errors discovered by the processor after this point are handled in the
  741. context of the new task.
  742.  
  743. To insure a proper TSS to process it, the handler for exception 10 must be
  744. a task invoked via a task gate.
  745.  
  746.  
  747. Table 9-5. Conditions That Invalidate the TSS
  748.  
  749. Error Code              Condition
  750.  
  751. TSS id + EXT            The limit in the TSS descriptor is less than 103
  752. LTD id + EXT            Invalid LDT selector or LDT not present
  753. SS id + EXT             Stack segment selector is outside table limit
  754. SS id + EXT             Stack segment is not a writable segment
  755. SS id + EXT             Stack segment DPL does not match new CPL
  756. SS id + EXT             Stack segment selector RPL < >  CPL
  757. CS id + EXT             Code segment selector is outside table limit
  758. CS id + EXT             Code segment selector does not refer to code
  759.                         segment
  760. CS id + EXT             DPL of non-conforming code segment < > new CPL
  761. CS id + EXT             DPL of conforming code segment > new CPL
  762. DS/ES/FS/GS id + EXT    DS, ES, FS, or GS segment selector is outside
  763.                         table limits
  764. DS/ES/FS/GS id + EXT    DS, ES, FS, or GS is not readable segment
  765.  
  766.  
  767. 9.8.11  Interrupt 11 ── Segment Not Present
  768.  
  769. Exception 11 occurs when the processor detects that the present bit of a
  770. descriptor is zero. The processor can trigger this fault in any of these
  771. cases:
  772.  
  773.   ■  While attempting to load the CS, DS, ES, FS, or GS registers; loading
  774.      the SS register, however, causes a stack fault.
  775.  
  776.   ■  While attempting loading the LDT register with an LLDT instruction;
  777.      loading the LDT register during a task switch operation, however,
  778.      causes the "invalid TSS" exception.
  779.  
  780.   ■  While attempting to use a gate descriptor that is marked not-present.
  781.  
  782. This fault is restartable. If the exception handler makes the segment
  783. present and returns, the interrupted program will resume execution.
  784.  
  785. If a not-present exception occurs during a task switch, not all the steps
  786. of the task switch are complete. During a task switch, the processor first
  787. loads all the segment registers, then checks their contents for validity. If
  788. a not-present exception is discovered, the remaining segment registers have
  789. not been checked and therefore may not be usable for referencing memory. The
  790. not-present handler should not rely on being able to use the values found
  791. in CS, SS, DS, ES, FS, and GS without causing another exception. The
  792. exception handler should check all segment registers before trying to resume
  793. the new task; otherwise, general protection faults may result later under
  794. conditions that make diagnosis more difficult. There are three ways to
  795. handle this case:
  796.  
  797.   1.  Handle the not-present fault with a task. The task switch back to the
  798.       interrupted task will cause the processor to check the registers as it
  799.       loads them from the TSS.
  800.  
  801.   2.  PUSH and POP all segment registers. Each POP causes the processor to
  802.       check the new contents of the segment register.
  803.  
  804.   3.  Scrutinize the contents of each segment-register image in the TSS,
  805.       simulating the test that the processor makes when it loads a segment
  806.       register.
  807.  
  808. This exception pushes an error code onto the stack. The EXT bit of the
  809. error code is set if an event external to the program caused an interrupt
  810. that subsequently referenced a not-present segment. The I-bit is set if the
  811. error code refers to an IDT entry, e.g., an INT instruction referencing a
  812. not-present gate.
  813.  
  814. An operating system typically uses the "segment not present" exception to
  815. implement virtual memory at the segment level. A not-present indication in a
  816. gate descriptor, however, usually does not indicate that a segment is not
  817. present (because gates do not necessarily correspond to segments).
  818. Not-present gates may be used by an operating system to trigger exceptions
  819. of special significance to the operating system.
  820.  
  821.  
  822. 9.8.12  Interrupt 12 ── Stack Exception
  823.  
  824. A stack fault occurs in either of two general conditions:
  825.  
  826.   ■  As a result of a limit violation in any operation that refers to the
  827.      SS register. This includes stack-oriented instructions such as POP,
  828.      PUSH, ENTER, and LEAVE, as well as other memory references that
  829.      implicitly use SS (for example, MOV AX, [BP+6]). ENTER causes this
  830.      exception when the stack is too small for the indicated local-variable
  831.      space.
  832.  
  833.   ■  When attempting to load the SS register with a descriptor that is
  834.      marked not-present but is otherwise valid. This can occur in a task
  835.      switch, an interlevel CALL, an interlevel return, an LSS instruction,
  836.      or a MOV or POP instruction to SS.
  837.  
  838. When the processor detects a stack exception, it pushes an error code onto
  839. the stack of the exception handler. If the exception is due to a not-present
  840. stack segment or to overflow of the new stack during an interlevel CALL, the
  841. error code contains a selector to the segment in question (the exception
  842. handler can test the present bit in the descriptor to determine which
  843. exception occurred); otherwise the error code is zero.
  844.  
  845. An instruction that causes this fault is restartable in all cases. The
  846. return pointer pushed onto the exception handler's stack points to the
  847. instruction that needs to be restarted. This instruction is usually the one
  848. that caused the exception; however, in the case of a stack exception due to
  849. loading of a not-present stack-segment descriptor during a task switch, the
  850. indicated instruction is the first instruction of the new task.
  851.  
  852. When a stack fault occurs during a task switch, the segment registers may
  853. not be usable for referencing memory. During a task switch, the selector
  854. values are loaded before the descriptors are checked. If a stack fault is
  855. discovered, the remaining segment registers have not been checked and
  856. therefore may not be usable for referencing memory. The stack fault handler
  857. should not rely on being able to use the values found in CS, SS, DS, ES,
  858. FS, and GS without causing another exception. The exception handler should
  859. check all segment registers before trying to resume the new task; otherwise,
  860. general protection faults may result later under conditions that make
  861. diagnosis more difficult.
  862.  
  863.  
  864. 9.8.13  Interrupt 13 ── General Protection Exception
  865.  
  866. All protection violations that do not cause another exception cause a
  867. general protection exception. This includes (but is not limited to):
  868.  
  869.   1.  Exceeding segment limit when using CS, DS, ES, FS, or GS
  870.  
  871.   2.  Exceeding segment limit when referencing a descriptor table
  872.  
  873.   3.  Transferring control to a segment that is not executable
  874.  
  875.   4.  Writing into a read-only data segment or into a code segment
  876.  
  877.   5.  Reading from an execute-only segment
  878.  
  879.   6.  Loading the SS register with a read-only descriptor (unless the 
  880.       selector comes from the TSS during a task switch, in which case a TSS 
  881.       exception occurs
  882.  
  883.   7.  Loading SS, DS, ES, FS, or GS with the descriptor of a system segment
  884.  
  885.   8.  Loading DS, ES, FS, or GS with the descriptor of an executable 
  886.       segment that is not also readable
  887.  
  888.   9.  Loading SS with the descriptor of an executable segment
  889.  
  890.   10. Accessing memory via DS, ES, FS, or GS when the segment register
  891.       contains a null selector
  892.  
  893.   11. Switching to a busy task
  894.  
  895.   12. Violating privilege rules
  896.  
  897.   13. Loading CR0 with PG=1 and PE=0.
  898.  
  899.   14. Interrupt or exception via trap or interrupt gate from V86 mode to
  900.       privilege level other than zero.
  901.  
  902.   15. Exceeding the instruction length limit of 15 bytes (this can occur
  903.       only if redundant prefixes are placed before an instruction)
  904.  
  905. The general protection exception is a fault. In response to a general
  906. protection exception, the processor pushes an error code onto the exception
  907. handler's stack. If loading a descriptor causes the exception, the error
  908. code contains a selector to the descriptor; otherwise, the error code is
  909. null. The source of the selector in an error code may be any of the
  910. following:
  911.  
  912.   1.  An operand of the instruction.
  913.   2.  A selector from a gate that is the operand of the instruction.
  914.   3.  A selector from a TSS involved in a task switch.
  915.  
  916.  
  917. 9.8.14  Interrupt 14 ── Page Fault
  918.  
  919. This exception occurs when paging is enabled (PG=1) and the processor
  920. detects one of the following conditions while translating a linear address
  921. to a physical address:
  922.  
  923.   ■  The page-directory or page-table entry needed for the address
  924.      translation has zero in its present bit.
  925.  
  926.   ■  The current procedure does not have sufficient privilege to access the
  927.      indicated page.
  928.  
  929. The processor makes available to the page fault handler two items of
  930. information that aid in diagnosing the exception and recovering from it:
  931.  
  932.   ■  An error code on the stack. The error code for a page fault has a
  933.      format different from that for other exceptions (see Figure 9-8). The
  934.      error code tells the exception handler three things:
  935.  
  936.      1.  Whether the exception was due to a not present page or to an access
  937.          rights violation.
  938.  
  939.      2.  Whether the processor was executing at user or supervisor level at
  940.          the time of the exception.
  941.  
  942.      3.  Whether the memory access that caused the exception was a read or
  943.          write.
  944.  
  945.   ■  CR2 (control register two). The processor stores in CR2 the linear
  946.      address used in the access that caused the exception (see Figure 9-9).
  947.      The exception handler can use this address to locate the corresponding
  948.      page directory and page table entries. If another page fault can occur
  949.      during execution of the page fault handler, the handler should push CR2
  950.      onto the stack.
  951.  
  952.  
  953. Figure 9-8.  Page-Fault Error Code Format
  954.  
  955.  ╔═════╤═════╤════════════════════════════════════════════════════════════╗
  956.  ║Field│Value│                         Description                        ║
  957.  ╟─────┼─────┼────────────────────────────────────────────────────────────╢
  958.  ║ U/S │  0  │ The access causing the fault originated when the processor ║
  959.  ║     │     │ was executing in supervisor mode.                          ║
  960.  ║     │     │                                                            ║
  961.  ║     │  1  │ The access causing the fault originated when the processor ║
  962.  ║     │     │ was executing in user mode.                                ║
  963.  ║     │     │                                                            ║
  964.  ║ W/R │  0  │ The access causing the fault was a read.                   ║
  965.  ║     │     │                                                            ║
  966.  ║     │  1  │ The access causing the fault was a write.                  ║
  967.  ║     │     │                                                            ║
  968.  ║ P   │  0  │ The fault was caused by a not-present page.                ║
  969.  ║     │     │                                                            ║
  970.  ║     │  1  │ The fault was caused by a page-level protection violation. ║
  971.  ╚═════╧═════╧════════════════════════════════════════════════════════════╝
  972.  
  973.        31                               15             7        3 2 1 0
  974.       ╔════════════════════════════════╪═════════════════════════╤═╤═╤═╗
  975.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│U│W│ ║
  976.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒UNDEFINED▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│/│/│P║
  977.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│S│R│ ║
  978.       ╚════════════════════════════════╪═════════════════════════╧═╧═╧═╝
  979.  
  980.  
  981. 9.8.14.1  Page Fault During Task Switch
  982.  
  983. The processor may access any of four segments during a task switch:
  984.  
  985.   1.  Writes the state of the original task in the TSS of that task.
  986.  
  987.   2.  Reads the GDT to locate the TSS descriptor of the new task.
  988.  
  989.   3.  Reads the TSS of the new task to check the types of segment
  990.       descriptors from the TSS.
  991.  
  992.   4.  May read the LDT of the new task in order to verify the segment
  993.       registers stored in the new TSS.
  994.  
  995. A page fault can result from accessing any of these segments. In the latter
  996. two cases the exception occurs in the context of the new task. The
  997. instruction pointer refers to the next instruction of the new task, not to
  998. the instruction that caused the task switch. If the design of the operating
  999. system permits page faults to occur during task-switches, the page-fault
  1000. handler should be invoked via a task gate.
  1001.  
  1002.  
  1003. Figure 9-9.  CR2 Format
  1004.  
  1005.       31               23               15               7              0
  1006.      ╔════════════════╪════════════════╪════════════════╪════════════════╗
  1007.      ║                                                                   ║
  1008.      ║                      PAGE FAULT LINEAR ADDRESS                    ║
  1009.      ║                                                                   ║
  1010.      ╚════════════════╪════════════════╪════════════════╪════════════════╝
  1011.  
  1012.  
  1013. 9.8.14.2  Page Fault with Inconsistent Stack Pointer
  1014.  
  1015. Special care should be taken to ensure that a page fault does not cause the
  1016. processor to use an invalid stack pointer (SS:ESP). Software written for
  1017. earlier processors in the 8086 family often uses a pair of instructions to
  1018. change to a new stack; for example:
  1019.  
  1020. MOV SS, AX
  1021. MOV SP, StackTop
  1022.  
  1023. With the 80386, because the second instruction accesses memory, it is
  1024. possible to get a page fault after SS has been changed but before SP has
  1025. received the corresponding change. At this point, the two parts of the stack
  1026. pointer SS:SP (or, for 32-bit programs, SS:ESP) are inconsistent.
  1027.  
  1028. The processor does not use the inconsistent stack pointer if the handling
  1029. of the page fault causes a stack switch to a well defined stack (i.e., the
  1030. handler is a task or a more privileged procedure). However, if the page
  1031. fault handler is invoked by a trap or interrupt gate and the page fault
  1032. occurs at the same privilege level as the page fault handler, the processor
  1033. will attempt to use the stack indicated by the current (invalid) stack
  1034. pointer.
  1035.  
  1036. In systems that implement paging and that handle page faults within the
  1037. faulting task (with trap or interrupt gates), software that executes at the
  1038. same privilege level as the page fault handler should initialize a new stack
  1039. by using the new LSS instruction rather than an instruction pair shown
  1040. above. When the page fault handler executes at privilege level zero (the
  1041. normal case), the scope of the problem is limited to privilege-level zero
  1042. code, typically the kernel of the operating system.
  1043.  
  1044.  
  1045. 9.8.15  Interrupt 16 ── Coprocessor Error
  1046.  
  1047. The 80386 reports this exception when it detects a signal from the 80287 or
  1048. 80387 on the 80386's ERROR# input pin. The 80386 tests this pin only at the
  1049. beginning of certain ESC instructions and when it encounters a WAIT
  1050. instruction while the EM bit of the MSW is zero (no emulation). Refer to
  1051. Chapter 11 for more information on the coprocessor interface.
  1052.  
  1053.  
  1054. 9.9  Exception Summary
  1055.  
  1056.  
  1057. Table 9-6 summarizes the exceptions recognized by the 386.
  1058.  
  1059. Table 9-6. Exception Summary
  1060.  
  1061.  
  1062. Description               Interrupt   Return Address  Exception     Function That Can Generate
  1063.                           Number      Points to       Type          the Exception
  1064.                                       Faulting
  1065.                                       Instruction
  1066.  
  1067. Divide error               0          YES             FAULT         DIV, IDIV
  1068. Debug exceptions           1          
  1069. Some debug exceptions are traps and some are faults.  The exception
  1070. handler can determine which has occurred by examining DR6.  (Refer to
  1071. Chapter 12.)               
  1072. Some debug exceptions are traps and some are faults.  The exception
  1073. handler can determine which has occurred by examining DR6.  (Refer to
  1074. Chapter 12.)             Any instruction
  1075. Breakpoint                 3          NO              TRAP          One-byte INT 3
  1076. Overflow                   4          NO              TRAP          INTO
  1077. Bounds check               5          YES             FAULT         BOUND
  1078. Invalid opcode             6          YES             FAULT         Any illegal instruction
  1079. Coprocessor not available  7          YES             FAULT         ESC, WAIT
  1080. Double fault               8          YES             ABORT         Any instruction that can
  1081.                                                                     generate an exception
  1082. Coprocessor Segment
  1083. Overrun                    9          NO              ABORT         Any operand of an ESC
  1084.                                                                     instruction that wraps around
  1085.                                                                     the end of a segment.
  1086. Invalid TSS               10          YES             FAULT
  1087. An invalid-TSS fault is not restartable if it occurs during the
  1088. processing of an external interrupt.        JMP, CALL, IRET, any interrupt
  1089. Segment not present       11          YES             FAULT         Any segment-register modifier
  1090. Stack exception           12          YES             FAULT         Any memory reference thru SS
  1091. General Protection        13          YES             FAULT/ABORT
  1092. All GP faults are restartable. If the fault occurs while attempting to
  1093. vector to the handler for an external interrupt, the interrupted program is
  1094. restartable, but the interrupt may be lost.  Any memory reference or code
  1095.                                                                     fetch
  1096. Page fault                14          YES             FAULT         Any memory reference or code
  1097.                                                                     fetch
  1098. Coprocessor error         16          YES             FAULT
  1099. Coprocessor errors are reported as a fault on the first ESC or WAIT
  1100. instruction executed after the ESC instruction that caused the error.        ESC, WAIT
  1101. Two-byte SW Interrupt     0-255       NO              TRAP          INT n
  1102.  
  1103.  
  1104. 9.10  Error Code Summary
  1105.  
  1106. Table 9-7 summarizes the error information that is available with each
  1107. exception.
  1108.  
  1109.  
  1110. Table 9-7. Error-Code Summary
  1111.  
  1112. Description                       Interrupt     Error Code
  1113.                                   Number
  1114.  
  1115. Divide error                       0            No
  1116. Debug exceptions                   1            No
  1117. Breakpoint                         3            No
  1118. Overflow                           4            No
  1119. Bounds check                       5            No
  1120. Invalid opcode                     6            No
  1121. Coprocessor not available          7            No
  1122. System error                       8            Yes (always 0)
  1123. Coprocessor Segment Overrun        9            No
  1124. Invalid TSS                       10            Yes
  1125. Segment not present               11            Yes
  1126. Stack exception                   12            Yes
  1127. General protection fault          13            Yes
  1128. Page fault                        14            Yes
  1129. Coprocessor error                 16            No
  1130. Two-byte SW interrupt             0-255         No
  1131.  
  1132.  
  1133.