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

  1. Chapter 7  Multitasking
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. To provide efficient, protected multitasking, the 80386 employs several
  6. special data structures. It does not, however, use special instructions to
  7. control multitasking; instead, it interprets ordinary control-transfer
  8. instructions differently when they refer to the special data structures. The
  9. registers and data structures that support multitasking are:
  10.  
  11.   ■  Task state segment
  12.   ■  Task state segment descriptor
  13.   ■  Task register
  14.   ■  Task gate descriptor
  15.  
  16. With these structures the 80386 can rapidly switch execution from one task
  17. to another, saving the context of the original task so that the task can be
  18. restarted later. In addition to the simple task switch, the 80386 offers two
  19. other task-management features:
  20.  
  21.   1.  Interrupts and exceptions can cause task switches (if needed in the
  22.       system design). The processor not only switches automatically to the
  23.       task that handles the interrupt or exception, but it automatically
  24.       switches back to the interrupted task when the interrupt or exception
  25.       has been serviced. Interrupt tasks may interrupt lower-priority
  26.       interrupt tasks to any depth.
  27.  
  28.   2.  With each switch to another task, the 80386 can also switch to
  29.       another LDT and to another page directory. Thus each task can have a
  30.       different logical-to-linear mapping and a different linear-to-physical
  31.       mapping. This is yet another protection feature, because tasks can be
  32.       isolated and prevented from interfering with one another.
  33.  
  34.  
  35. 7.1  Task State Segment
  36.  
  37. All the information the processor needs in order to manage a task is stored
  38. in a special type of segment, a task state segment (TSS). Figure 7-1 shows
  39. the format of a TSS for executing 80386 tasks. (Another format is used for
  40. executing 80286 tasks; refer to Chapter 13.)
  41.  
  42. The fields of a TSS belong to two classes:
  43.  
  44.   1.  A dynamic set that the processor updates with each switch from the
  45.       task. This set includes the fields that store:
  46.  
  47.       ■  The general registers (EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI).
  48.  
  49.       ■  The segment registers (ES, CS, SS, DS, FS, GS).
  50.  
  51.       ■  The flags register (EFLAGS).
  52.  
  53.       ■  The instruction pointer (EIP).
  54.  
  55.       ■  The selector of the TSS of the previously executing task (updated
  56.          only when a return is expected).
  57.  
  58.   2.  A static set that the processor reads but does not change. This set
  59.       includes the fields that store:
  60.  
  61.       ■  The selector of the task's LDT.
  62.  
  63.       ■  The register (PDBR) that contains the base address of the task's
  64.          page directory (read only when paging is enabled).
  65.  
  66.       ■  Pointers to the stacks for privilege levels 0-2.
  67.  
  68.       ■  The T-bit (debug trap bit) which causes the processor to raise a
  69.          debug exception when a task switch occurs. (Refer to Chapter 12
  70.          for more information on debugging.)
  71.  
  72.       ■  The I/O map base (refer to Chapter 8 for more information on the
  73.          use of the I/O map).
  74.  
  75. Task state segments may reside anywhere in the linear space. The only case
  76. that requires caution is when the TSS spans a page boundary and the
  77. higher-addressed page is not present. In this case, the processor raises an
  78. exception if it encounters the not-present page while reading the TSS during
  79. a task switch. Such an exception can be avoided by either of two strategies:
  80.  
  81.   1.  By allocating the TSS so that it does not cross a page boundary.
  82.  
  83.   2.  By ensuring that both pages are either both present or both 
  84.       not-present at the time of a task switch. If both pages are 
  85.       not-present, then the page-fault handler must make both pages present 
  86.       before restarting the instruction that caused the task switch.
  87.  
  88.  
  89. Figure 7-1.  80386 32-Bit Task State Segment
  90.  
  91.       31              23              15              7             0
  92.      ╔═══════════════╪═══════════════╬═══════════════╪═════════════╦═╗
  93.      ║          I/O MAP BASE         ║ 0 0 0 0 0 0 0   0 0 0 0 0 0 ║T║64
  94.      ╟───────────────┼───────────────╫───────────────┼─────────────╨─╢
  95.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              LDT              ║60
  96.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  97.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              GS               ║5C
  98.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  99.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              FS               ║58
  100.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  101.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              DS               ║54
  102.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  103.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              SS               ║50
  104.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  105.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              CS               ║4C
  106.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  107.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              ES               ║48
  108.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  109.      ║                              EDI                              ║44
  110.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  111.      ║                              ESI                              ║40
  112.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  113.      ║                              EBP                              ║3C
  114.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  115.      ║                              ESP                              ║38
  116.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  117.      ║                              EBX                              ║34
  118.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  119.      ║                              EDX                              ║30
  120.      ╠═══════════════╪═══════════════╪═══════════════╪═══════════════╣
  121.      ║                              ECX                              ║2C
  122.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  123.      ║                              EAX                              ║28
  124.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  125.      ║                            EFLAGS                             ║24
  126.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  127.      ║                    INSTRUCTION POINTER (EIP)                  ║20
  128.      ╟───────────────┼───────────────┼───────────────┼───────────────╢
  129.      ║                          CR3  (PDPR)                          ║1C
  130.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  131.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              SS2              ║18
  132.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  133.      ║                             ESP2                              ║14
  134.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  135.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              SS1              ║10
  136.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  137.      ║                             ESP1                              ║0C
  138.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  139.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              SS0              ║8
  140.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  141.      ║                             ESP0                              ║4
  142.      ╟───────────────┼───────────────╫───────────────┼───────────────╢
  143.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║   BACK LINK TO PREVIOUS TSS   ║0
  144.      ╚═══════════════╪═══════════════╬═══════════════╪═══════════════╝
  145.  
  146. ────────────────────────────────────────────────────────────────────────────
  147. NOTE
  148.       0 MEANS INTEL RESERVED. DO NOT DEFINE.
  149. ────────────────────────────────────────────────────────────────────────────
  150.  
  151.  
  152. 7.2  TSS Descriptor
  153.  
  154. The task state segment, like all other segments, is defined by a
  155. descriptor. Figure 7-2 shows the format of a TSS descriptor.
  156.  
  157. The B-bit in the type field indicates whether the task is busy. A type code
  158. of 9 indicates a non-busy task; a type code of 11 indicates a busy task.
  159. Tasks are not reentrant. The B-bit allows the processor to detect an attempt
  160. to switch to a task that is already busy.
  161.  
  162. The BASE, LIMIT, and DPL fields and the G-bit and P-bit have functions
  163. similar to their counterparts in data-segment descriptors. The LIMIT field,
  164. however, must have a value equal to or greater than 103. An attempt to
  165. switch to a task whose TSS descriptor has a limit less that 103 causes an
  166. exception. A larger limit is permissible, and a larger limit is required if
  167. an I/O permission map is present. A larger limit may also be convenient for
  168. systems software if additional data is stored in the same segment as the
  169. TSS.
  170.  
  171. A procedure that has access to a TSS descriptor can cause a task switch. In
  172. most systems the DPL fields of TSS descriptors should be set to zero, so
  173. that only trusted software has the right to perform task switching.
  174.  
  175. Having access to a TSS-descriptor does not give a procedure the right to
  176. read or modify a TSS. Reading and modification can be accomplished only with
  177. another descriptor that redefines the TSS as a data segment. An attempt to
  178. load a TSS descriptor into any of the segment registers (CS, SS, DS, ES, FS,
  179. GS) causes an exception.
  180.  
  181. TSS descriptors may reside only in the GDT. An attempt to identify a TSS
  182. with a selector that has TI=1 (indicating the current LDT) results in an
  183. exception.
  184.  
  185.  
  186. Figure 7-2.  TSS Descriptor for 32-bit TSS
  187.  
  188.   31                23                15                7               0
  189.  ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═════════╪═════════════════╗
  190.  ║                 │ │ │ │A│ LIMIT   │ │     │  TYPE   │                 ║
  191.  ║   BASE 31..24   │G│0│0│V│         │P│ DPL │         │   BASE 23..16   ║ 4
  192.  ║                 │ │ │ │L│  19..16 │ │     │0│1│0│B│1│                 ║
  193.  ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─┴─┴─┴─┴─────────────────╢
  194.  ║                                   │                                   ║
  195.  ║             BASE 15..0            │             LIMIT 15..0           ║ 0
  196.  ║                                   │                                   ║
  197.  ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  198.  
  199.  
  200. 7.3  Task Register
  201.  
  202. The task register (TR) identifies the currently executing task by pointing
  203. to the TSS. Figure 7-3 shows the path by which the processor accesses the
  204. current TSS.
  205.  
  206. The task register has both a "visible" portion (i.e., can be read and
  207. changed by instructions) and an "invisible" portion (maintained by the
  208. processor to correspond to the visible portion; cannot be read by any
  209. instruction). The selector in the visible portion selects a TSS descriptor
  210. in the GDT. The processor uses the invisible portion to cache the base and
  211. limit values from the TSS descriptor. Holding the base and limit in a
  212. register makes execution of the task more efficient, because the processor
  213. does not need to repeatedly fetch these values from memory when it
  214. references the TSS of the current task.
  215.  
  216. The instructions LTR and STR are used to modify and read the visible
  217. portion of the task register. Both instructions take one operand, a 16-bit
  218. selector located in memory or in a general register.
  219.  
  220. LTR (Load task register) loads the visible portion of the task register
  221. with the selector operand, which must select a TSS descriptor in the GDT.
  222. LTR also loads the invisible portion with information from the TSS
  223. descriptor selected by the operand. LTR is a privileged instruction; it may
  224. be executed only when CPL is zero. LTR is generally used during system
  225. initialization to give an initial value to the task register; thereafter,
  226. the contents of TR are changed by task switch operations.
  227.  
  228. STR (Store task register) stores the visible portion of the task register
  229. in a general register or memory word. STR is not privileged.
  230.  
  231.  
  232. Figure 7-3.  Task Register 
  233.  
  234.                           ╔═════════════════════════╗
  235.                           ║                         ║
  236.                           ║                         ║
  237.                           ║       TASK STATE        ║
  238.                           ║        SEGMENT          ║─────────┐
  239.                           ║                         ║          │
  240.                           ║                         ║          │
  241.                           ╚═════════════════════════╝          │
  242.            16-BIT VISIBLE                                     │
  243.               REGISTER                │   HIDDEN REGISTER      │
  244.        ╔════════════════════╦═════════╧══════════╦═════════════╧══════╗
  245.     TR ║      SELECTOR      ║      (BASE)        ║       (LIMT)       ║
  246.        ╚═════════╤══════════╩════════════════════╩════════════════════╝
  247.                  │                                         
  248.                  │                    └─────────────────┐   │
  249.                  │          GLOBAL DESCRIPTOR TABLE     │   │
  250.                  │        ╒═════════════════════════╕   │   │
  251.                  │        |     TSS DESCRIPTOR      |   │   │
  252.                  │        ╔══════╦═════╦═════╦══════╗   │   │
  253.                  │        ║      ║     ║     ║      ╟───┘   │
  254.                  │        ╠══════╩═════╬═════╩══════╣       │
  255.                  └───────║            ║            ╟───────┘
  256.                           ╚════════════╩════════════╝
  257.                           |                         |
  258.                           ╘═════════════════════════╛
  259.  
  260.  
  261. 7.4  Task Gate Descriptor
  262.  
  263. A task gate descriptor provides an indirect, protected reference to a TSS.
  264. Figure 7-4 illustrates the format of a task gate.
  265.  
  266. The SELECTOR field of a task gate must refer to a TSS descriptor. The value
  267. of the RPL in this selector is not used by the processor.
  268.  
  269. The DPL field of a task gate controls the right to use the descriptor to
  270. cause a task switch. A procedure may not select a task gate descriptor
  271. unless the maximum of the selector's RPL and the CPL of the procedure is
  272. numerically less than or equal to the DPL of the descriptor. This constraint
  273. prevents untrusted procedures from causing a task switch. (Note that when a
  274. task gate is used, the DPL of the target TSS descriptor is not used for
  275. privilege checking.)
  276.  
  277. A procedure that has access to a task gate has the power to cause a task
  278. switch, just as a procedure that has access to a TSS descriptor. The 80386
  279. has task gates in addition to TSS descriptors to satisfy three needs:
  280.  
  281.   1.  The need for a task to have a single busy bit. Because the busy-bit
  282.       is stored in the TSS descriptor, each task should have only one such
  283.       descriptor. There may, however, be several task gates that select the
  284.       single TSS descriptor.
  285.  
  286.   2.  The need to provide selective access to tasks. Task gates fulfill
  287.       this need, because they can reside in LDTs and can have a DPL that is
  288.       different from the TSS descriptor's DPL. A procedure that does not
  289.       have sufficient privilege to use the TSS descriptor in the GDT (which
  290.       usually has a DPL of 0) can still switch to another task if it has
  291.       access to a task gate for that task in its LDT. With task gates,
  292.       systems software can limit the right to cause task switches to
  293.       specific tasks.
  294.  
  295.   3.  The need for an interrupt or exception to cause a task switch. Task
  296.       gates may also reside in the IDT, making it possible for interrupts
  297.       and exceptions to cause task switching. When interrupt or exception
  298.       vectors to an IDT entry that contains a task gate, the 80386 switches
  299.       to the indicated task. Thus, all tasks in the system can benefit from
  300.       the protection afforded by isolation from interrupt tasks.
  301.  
  302. Figure 7-5 illustrates how both a task gate in an LDT and a task gate in
  303. the IDT can identify the same task.
  304.  
  305.  
  306. Figure 7-4.  Task Gate Descriptor
  307.  
  308.    31                23               15                7             0
  309.   ╔═════════════════╪════════════════╪═╤═════╤═════════╪═════════════════╗
  310.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│ │     │         │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  311.   ║▒▒▒▒▒▒▒▒▒▒▒▒(NOT USED)▒▒▒▒▒▒▒▒▒▒▒▒│P│ DPL │0 0 1 0 1│▒▒▒(NOT USED)▒▒▒▒║ 4
  312.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│ │     │         │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  313.   ╟──────────────────────────────────┼─┴─────┴─────────┴─────────────────╢
  314.   ║                                  │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  315.   ║              SELECTOR            │▒▒▒▒▒▒▒▒▒▒▒▒(NOT USED)▒▒▒▒▒▒▒▒▒▒▒▒▒║ 0
  316.   ║                                  │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  317.   ╚═════════════════╪════════════════╪═════════════════╪═════════════════╝
  318.  
  319.  
  320. Figure 7-5.  Task Gate Indirectly Identifies Task
  321.  
  322.          LOCAL DESCRIPTOR TABLE              INTERRUPT DESCRIPTOR TABLE
  323.        ╒═════════════════════════╕           ╒═════════════════════════╕
  324.        |                         |           |                         |
  325.        |        TASK GATE        |           |        TASK GATE        |
  326.        ╔══════╤═════╤═════╤══════╗           ╔══════╤═════╤═════╤══════╗
  327.        ║      │     │     │      ║           ║      │     │     │      ║
  328.        ╟──────┴─────┼─────┴──────╢           ╟──────┴─────┼─────┴──────╢
  329.     ┌──╢            │            ║        ┌──╢            │            ║
  330.     │  ╚════════════╧════════════╝        │  ╚════════════╧════════════╝
  331.     │  |                         |        │  |                         |
  332.     │  |                         |        │  |                         |
  333.     │  ╘═════════════════════════╛        │  ╘═════════════════════════╛
  334.     └────────────────┐  ┌─────────────────┘
  335.                      │  │    GLOBAL DESCRIPTOR TABLE
  336.                      │  │  ╒═════════════════════════╕
  337.                      │  │  |                         |
  338.                      │  │  |     TASK DESCRIPTOR     |
  339.                      │  │  ╔══════╤═════╤═════╤══════╗
  340.                      │  │  ║      │     │     │      ║
  341.                      │  └─╟──────┴─────┼─────┴──────╢
  342.                      └────║            │            ╟──┐
  343.                            ╚════════════╧════════════╝  │
  344.                            |                         |  │
  345.                            |                         |  │
  346.                            ╘═════════════════════════╛  │
  347.                                                         │
  348.                            ╔═════════════════════════╗  │
  349.                            ║                         ║  │
  350.                            ║                         ║  │
  351.                            ║                         ║  │
  352.                            ║       TASK STATE        ║  │
  353.                            ║         SEGMENT         ║  │
  354.                            ║                         ║  │
  355.                            ║                         ║  │
  356.                            ║                         ║  │
  357.                            ╚═════════════════════════╝─┘
  358.  
  359.  
  360. 7.5  Task Switching
  361.  
  362. The 80386 switches execution to another task in any of four cases:
  363.  
  364.   1.  The current task executes a JMP or CALL that refers to a TSS
  365.       descriptor.
  366.  
  367.   2.  The current task executes a JMP or CALL that refers to a task gate.
  368.  
  369.   3.  An interrupt or exception vectors to a task gate in the IDT.
  370.  
  371.   4.  The current task executes an IRET when the NT flag is set.
  372.  
  373. JMP, CALL, IRET, interrupts, and exceptions are all ordinary mechanisms of
  374. the 80386 that can be used in circumstances that do not require a task
  375. switch. Either the type of descriptor referenced or the NT (nested task) bit
  376. in the flag word distinguishes between the standard mechanism and the
  377. variant that causes a task switch.
  378.  
  379. To cause a task switch, a JMP or CALL instruction can refer either to a TSS
  380. descriptor or to a task gate. The effect is the same in either case: the
  381. 80386 switches to the indicated task.
  382.  
  383. An exception or interrupt causes a task switch when it vectors to a task
  384. gate in the IDT. If it vectors to an interrupt or trap gate in the IDT, a
  385. task switch does not occur. Refer to Chapter 9 for more information on the
  386. interrupt mechanism.
  387.  
  388. Whether invoked as a task or as a procedure of the interrupted task, an
  389. interrupt handler always returns control to the interrupted procedure in the
  390. interrupted task. If the NT flag is set, however, the handler is an
  391. interrupt task, and the IRET switches back to the interrupted task.
  392.  
  393. A task switching operation involves these steps:
  394.  
  395.   1.  Checking that the current task is allowed to switch to the designated
  396.       task. Data-access privilege rules apply in the case of JMP or CALL
  397.       instructions. The DPL of the TSS descriptor or task gate must be less
  398.       than or equal to the maximum of CPL and the RPL of the gate selector.
  399.       Exceptions, interrupts, and IRETs are permitted to switch tasks
  400.       regardless of the DPL of the target task gate or TSS descriptor.
  401.  
  402.   2.  Checking that the TSS descriptor of the new task is marked present
  403.       and has a valid limit. Any errors up to this point occur in the
  404.       context of the outgoing task. Errors are restartable and can be
  405.       handled in a way that is transparent to applications procedures.
  406.  
  407.   3.  Saving the state of the current task. The processor finds the base
  408.       address of the current TSS cached in the task register. It copies the
  409.       registers into the current TSS (EAX, ECX, EDX, EBX, ESP, EBP, ESI,
  410.       EDI, ES, CS, SS, DS, FS, GS, and the flag register). The EIP field of
  411.       the TSS points to the instruction after the one that caused the task
  412.       switch.
  413.  
  414.   4.  Loading the task register with the selector of the incoming task's
  415.       TSS descriptor, marking the incoming task's TSS descriptor as busy,
  416.       and setting the TS (task switched) bit of the MSW. The selector is
  417.       either the operand of a control transfer instruction or is taken from
  418.       a task gate.
  419.  
  420.   5.  Loading the incoming task's state from its TSS and resuming
  421.       execution. The registers loaded are the LDT register; the flag
  422.       register; the general registers EIP, EAX, ECX, EDX, EBX, ESP, EBP,
  423.       ESI, EDI; the segment registers ES, CS, SS, DS, FS, and GS; and PDBR.
  424.       Any errors detected in this step occur in the context of the incoming
  425.       task. To an exception handler, it appears that the first instruction
  426.       of the new task has not yet executed.
  427.  
  428. Note that the state of the outgoing task is always saved when a task switch
  429. occurs. If execution of that task is resumed, it starts after the
  430. instruction that caused the task switch. The registers are restored to the
  431. values they held when the task stopped executing.
  432.  
  433. Every task switch sets the TS (task switched) bit in the MSW (machine
  434. status word). The TS flag is useful to systems software when a coprocessor
  435. (such as a numerics coprocessor) is present. The TS bit signals that the
  436. context of the coprocessor may not correspond to the current 80386 task.
  437. Chapter 11 discusses the TS bit and coprocessors in more detail.
  438.  
  439. Exception handlers that field task-switch exceptions in the incoming task
  440. (exceptions due to tests 4 thru 16 of Table 7-1) should be cautious about
  441. taking any action that might load the selector that caused the exception.
  442. Such an action will probably cause another exception, unless the exception
  443. handler first examines the selector and fixes any potential problem.
  444.  
  445. The privilege level at which execution resumes in the incoming task is
  446. neither restricted nor affected by the privilege level at which the outgoing
  447. task was executing. Because the tasks are isolated by their separate address
  448. spaces and TSSs and because privilege rules can be used to prevent improper
  449. access to a TSS, no privilege rules are needed to constrain the relation
  450. between the CPLs of the tasks. The new task begins executing at the
  451. privilege level indicated by the RPL of the CS selector value that is loaded
  452. from the TSS.
  453.  
  454.  
  455. Table 7-1. Checks Made during a Task Switch
  456.  
  457.  
  458. Test     Test Description                   Exception
  459. NP = Segment-not-present exception, GP = General protection fault, TS =
  460. Invalid TSS, SF = Stack fault   Error Code Selects
  461.  
  462.   1      Incoming TSS descriptor is         NP           Incoming TSS
  463.          present
  464.   2      Incoming TSS descriptor is         GP           Incoming TSS
  465.          marked not-busy
  466.   3      Limit of incoming TSS is           TS           Incoming TSS
  467.          greater than or equal to 103
  468.  
  469.              ── All register and selector values are loaded ──
  470.  
  471.   4      LDT selector of incoming           TS           Incoming TSS
  472.          task is valid
  473.   5      LDT of incoming task is            TS           Incoming TSS
  474.          present
  475.   6      CS selector is valid
  476. Validity tests of a selector check that the selector is in the proper
  477. table (eg., the LDT selector refers to the GDT), lies within the bounds of
  478. the table, and refers to the proper type of descriptor (e.g., the LDT
  479. selector refers to an LDT descriptor).              TS           Code segment
  480.   7      Code segment is present            NP           Code segment
  481.   8      Code segment DPL matches           TS           Code segment
  482.          CS RPL
  483.   9      Stack segment is valid
  484. Validity tests of a selector check that the selector is in the proper
  485. table (eg., the LDT selector refers to the GDT), lies within the bounds of
  486. the table, and refers to the proper type of descriptor (e.g., the LDT
  487. selector refers to an LDT descriptor).            GP           Stack segment
  488.  10      Stack segment is present           SF           Stack segment
  489.  11      Stack segment DPL = CPL            SF           Stack segment
  490.  12      Stack-selector RPL = CPL           GP           Stack segment
  491.  13      DS, ES, FS, GS selectors are       GP           Segment
  492.          valid
  493. Validity tests of a selector check that the selector is in the proper
  494. table (eg., the LDT selector refers to the GDT), lies within the bounds of
  495. the table, and refers to the proper type of descriptor (e.g., the LDT
  496. selector refers to an LDT descriptor).
  497.  
  498.  
  499.  14      DS, ES, FS, GS segments            GP           Segment
  500.          are readable
  501.  15      DS, ES, FS, GS segments            NP           Segment
  502.          are present
  503.  16      DS, ES, FS, GS segment DPL         GP           Segment
  504.          ≥ CPL (unless these are
  505.          conforming segments)
  506.  
  507.  
  508. 7.6  Task Linking
  509.  
  510. The back-link field of the TSS and the NT (nested task) bit of the flag
  511. word together allow the 80386 to automatically return to a task that CALLed
  512. another task or was interrupted by another task. When a CALL instruction, an
  513. interrupt instruction, an external interrupt, or an exception causes a
  514. switch to a new task, the 80386 automatically fills the back-link of the new
  515. TSS with the selector of the outgoing task's TSS and, at the same time,
  516. sets the NT bit in the new task's flag register. The NT flag indicates
  517. whether the back-link field is valid. The new task releases control by
  518. executing an IRET instruction. When interpreting an IRET, the 80386 examines
  519. the NT flag. If NT is set, the 80386 switches back to the task selected by
  520. the back-link field. Table 7-2 summarizes the uses of these fields.
  521.  
  522.  
  523. Table 7-2. Effect of Task Switch on BUSY, NT, and Back-Link
  524.  
  525. Affected Field      Effect of JMP      Effect of            Effect of
  526.                     Instruction        CALL Instruction     IRET Instruction
  527.  
  528. Busy bit of         Set, must be       Set, must be 0       Unchanged,
  529. incoming task       0 before           before               must be set
  530.  
  531. Busy bit of         Cleared            Unchanged            Cleared
  532. outgoing task                          (already set)
  533.  
  534. NT bit of           Cleared            Set                  Unchanged
  535. incoming task
  536.  
  537. NT bit of           Unchanged          Unchanged            Cleared
  538. outgoing task
  539.  
  540. Back-link of        Unchanged          Set to outgoing      Unchanged
  541. incoming task                          TSS selector
  542.  
  543. Back-link of        Unchanged          Unchanged            Unchanged
  544. outgoing task
  545.  
  546.  
  547. 7.6.1  Busy Bit Prevents Loops
  548.  
  549. The B-bit (busy bit) of the TSS descriptor ensures the integrity of the
  550. back-link. A chain of back-links may grow to any length as interrupt tasks
  551. interrupt other interrupt tasks or as called tasks call other tasks. The
  552. busy bit ensures that the CPU can detect any attempt to create a loop. A
  553. loop would indicate an attempt to reenter a task that is already busy;
  554. however, the TSS is not a reentrable resource.
  555.  
  556. The processor uses the busy bit as follows:
  557.  
  558.   1.  When switching to a task, the processor automatically sets the busy
  559.       bit of the new task.
  560.  
  561.   2.  When switching from a task, the processor automatically clears the
  562.       busy bit of the old task if that task is not to be placed on the
  563.       back-link chain (i.e., the instruction causing the task switch is JMP
  564.       or IRET). If the task is placed on the back-link chain, its busy bit
  565.       remains set.
  566.  
  567.   3.  When switching to a task, the processor signals an exception if the
  568.       busy bit of the new task is already set.
  569.  
  570. By these actions, the processor prevents a task from switching to itself or
  571. to any task that is on a back-link chain, thereby preventing invalid reentry
  572. into a task.
  573.  
  574. The busy bit is effective even in multiprocessor configurations, because
  575. the processor automatically asserts a bus lock when it sets or clears the
  576. busy bit. This action ensures that two processors do not invoke the same
  577. task at the same time. (Refer to Chapter 11 for more on multiprocessing.)
  578.  
  579.  
  580. 7.6.2  Modifying Task Linkages
  581.  
  582. Any modification of the linkage order of tasks should be accomplished only
  583. by software that can be trusted to correctly update the back-link and the
  584. busy-bit. Such changes may be needed to resume an interrupted task before
  585. the task that interrupted it. Trusted software that removes a task from the
  586. back-link chain must follow one of the following policies:
  587.  
  588.   1.  First change the back-link field in the TSS of the interrupting task,
  589.       then clear the busy-bit in the TSS descriptor of the task removed from
  590.       the list.
  591.  
  592.   2.  Ensure that no interrupts occur between updating the back-link chain
  593.       and the busy bit.
  594.  
  595.  
  596. 7.7  Task Address Space
  597.  
  598. The LDT selector and PDBR fields of the TSS give software systems designers
  599. flexibility in utilization of segment and page mapping features of the
  600. 80386. By appropriate choice of the segment and page mappings for each task,
  601. tasks may share address spaces, may have address spaces that are largely
  602. distinct from one another, or may have any degree of sharing between these
  603. two extremes.
  604.  
  605. The ability for tasks to have distinct address spaces is an important
  606. aspect of 80386 protection. A module in one task cannot interfere with a
  607. module in another task if the modules do not have access to the same address
  608. spaces. The flexible memory management features of the 80386 allow systems
  609. designers to assign areas of shared address space to those modules of
  610. different tasks that are designed to cooperate with each other.
  611.  
  612.  
  613. 7.7.1  Task Linear-to-Physical Space Mapping
  614.  
  615. The choices for arranging the linear-to-physical mappings of tasks fall
  616. into two general classes:
  617.  
  618.   1.  One linear-to-physical mapping shared among all tasks.
  619.  
  620.       When paging is not enabled, this is the only possibility. Without page
  621.       tables, all linear addresses map to the same physical addresses.
  622.  
  623.       When paging is enabled, this style of linear-to-physical mapping 
  624.       results from using one page directory for all tasks. The linear space
  625.       utilized may exceed the physical space available if the operating 
  626.       system also implements page-level virtual memory.
  627.  
  628.   2.  Several partially overlapping linear-to-physical mappings.
  629.  
  630.       This style is implemented by using a different page directory for each
  631.       task. Because the PDBR (page directory base register) is loaded from 
  632.       the TSS with each task switch, each task may have a different page 
  633.       directory.
  634.  
  635. In theory, the linear address spaces of different tasks may map to
  636. completely distinct physical addresses. If the entries of different page
  637. directories point to different page tables and the page tables point to
  638. different pages of physical memory, then the tasks do not share any physical
  639. addresses.
  640.  
  641. In practice, some portion of the linear address spaces of all tasks must
  642. map to the same physical addresses. The task state segments must lie in a
  643. common space so that the mapping of TSS addresses does not change while the
  644. processor is reading and updating the TSSs during a task switch. The linear
  645. space mapped by the GDT should also be mapped to a common physical space;
  646. otherwise, the purpose of the GDT is defeated. Figure 7-6 shows how the
  647. linear spaces of two tasks can overlap in the physical space by sharing
  648. page tables.
  649.  
  650.  
  651. 7.7.2  Task Logical Address Space
  652.  
  653. By itself, a common linear-to-physical space mapping does not enable
  654. sharing of data among tasks. To share data, tasks must also have a common
  655. logical-to-linear space mapping; i.e., they must also have access to
  656. descriptors that point into a shared linear address space. There are three
  657. ways to create common logical-to-physical address-space mappings:
  658.  
  659.   1.  Via the GDT. All tasks have access to the descriptors in the GDT. If
  660.       those descriptors point into a linear-address space that is mapped to
  661.       a common physical-address space for all tasks, then the tasks can
  662.       share data and instructions.
  663.  
  664.   2.  By sharing LDTs. Two or more tasks can use the same LDT if the LDT
  665.       selectors in their TSSs select the same LDT segment. Those
  666.       LDT-resident descriptors that point into a linear space that is mapped
  667.       to a common physical space permit the tasks to share physical memory.
  668.       This method of sharing is more selective than sharing by the GDT; the
  669.       sharing can be limited to specific tasks. Other tasks in the system
  670.       may have different LDTs that do not give them access to the shared
  671.       areas.
  672.  
  673.   3.  By descriptor aliases in LDTs. It is possible for certain descriptors
  674.       of different LDTs to point to the same linear address space. If that
  675.       linear address space is mapped to the same physical space by the page
  676.       mapping of the tasks involved, these descriptors permit the tasks to
  677.       share the common space. Such descriptors are commonly called
  678.       "aliases". This method of sharing is even more selective than the
  679.       prior two; other descriptors in the LDTs may point to distinct linear
  680.       addresses or to linear addresses that are not shared.
  681.  
  682.  
  683. Figure 7-6. Partially-Overlapping Linear Spaces
  684.  
  685.          TSSs                                              PAGE FRAMES
  686.                                                           ╔══════════╗
  687.       TASK A TSS    PAGE DIRECTORIES     PAGE TABLES      ║  TASK A  ║
  688.      ╔══════════╗     ╔═══════════╗     ╔═══════════╗  ┌─║   PAGE   ║
  689.      ║          ║     ║           ║     ║           ║  │  ╚══════════╝
  690.      ║          ║     ╠═══════════╣     ╠═══════════╣  │  ╔══════════╗
  691.      ║          ║     ║           ║     ║    PTE    ╟──┘  ║  TASK A  ║
  692.      ║          ║     ╠═══════════╣     ╠═══════════╣  ┌─║   PAGE   ║
  693.      ║          ║     ║           ║     ║    PTE    ╟──┘  ╚══════════╝
  694.      ╠══════════╣     ╠═══════════╣     ╠═══════════╣     ╔══════════╗
  695.      ║   PDBR   ╟────║    PDE    ╟────║    PTE    ╟──┐  ║  TASK A  ║
  696.      ╠══════════╣     ╠═══════════╣     ╚═══════════╝  └─║   PAGE   ║
  697.      ║          ║     ║    PDE    ╟──┐    SHARED PT       ╚══════════╝
  698.      ╚══════════╝     ╚═══════════╝  │  ╔═══════════╗     ╔══════════╗
  699.                                      │  ║           ║     ║  SHARED  ║
  700.                                      │  ╠═══════════╣  ┌─║   PAGE   ║
  701.                                      │  ║           ║  │  ╚══════════╝
  702.                                      │  ╠═══════════╣  │  ╔══════════╗
  703.                                      │  ║    PTE    ╟──┘  ║  SHARED  ║
  704.                                      │  ╠═══════════╣  ┌─║   PAGE   ║
  705.                                      ├─║    PTE    ╟──┘  ╚══════════╝
  706.       TASK B TSS                     │  ╚═══════════╝     ╔══════════╗
  707.      ╔══════════╗     ╔═══════════╗  │                    ║  TASK B  ║
  708.      ║          ║     ║           ║  │                ┌──║   PAGE   ║
  709.      ║          ║     ╠═══════════╣  │  ╔═══════════╗ │   ╚══════════╝
  710.      ║          ║     ║           ║  │  ║           ║ │   ╔══════════╗
  711.      ║          ║     ╠═══════════╣  │  ╠═══════════╣ │   ║  TASK B  ║
  712.      ║          ║     ║           ║  │  ║           ║ │ ┌║   PAGE   ║
  713.      ╠══════════╣     ╠═══════════╣  │  ╠═══════════╣ │ │ ╚══════════╝
  714.      ║   PDBR   ╟────║    PDE    ╟──┘  ║    PTE    ╟─┘ │  PAGE FRAMES
  715.      ╠══════════╣     ╠═══════════╣     ╠═══════════╣   │
  716.      ║          ║     ║    PDE    ╟────║    PTE    ╟───┘
  717.      ╚══════════╝     ╚═══════════╝     ╚═══════════╝
  718.          TSSs       PAGE DIRECTORIES     PAGE TABLES
  719.  
  720.  
  721.