7.6 Task Linking 任务链
The back-link field of the TSS and the NT (nested task) bit of the flag word together allow the 80386 to automatically return to a task that CALLed another task or was interrupted by another task. When a CALL instruction, an interrupt instruction, an external interrupt, or an exception causes a switch to a new task, the 80386 automatically fills the back-link of the new TSS with the selector of the outgoing task‘s TSS and, at the same time, sets the NT bit in the new task‘s flag register.
The NT flag indicates whether the back-link field is valid. The new task releases control by executing an IRET instruction. When interpreting an IRET, the 80386 examines the NT flag. If NT is set, the 80386 switches back to the task selected by the back-link field. Table 7-2 summarizes the uses of these fields.
TSS中的后向连接字段和标志字中的NT位一起允许80386自动返回到一个任务,返回的任务是调用任务的调用者(CALLed)或被另一个任务中断的任务。当一个CALL指令、中断指令、内部中断或者异常导致切换到一个新任务时,80386使用切换出去的任务的TSS的选择子来自动填充新TSS的后向连接字段,同时,在新任务的标志寄存器中设置NT位。
NT位表明一个后向连接字段是有效的。新任务通过IRET指令来释放控制权。当解释一条IRET指令时,80386检查NT位。如果NT位被置位,80386通过后向连接位选择切换回的任务。表7-2总结了这些字段的用法。
Table 7-2. Effect of Task Switch on BUSY, NT, and Back-Link
Affected Field Effect of JMP Effect of Effect of
Instruction CALL Instruction IRET Instruction
Busy bit of Set, must be Set, must be 0 Unchanged,
incoming task 0 before before must be set
Busy bit of Cleared Unchanged Cleared
outgoing task (already set)
NT bit of Cleared Set Unchanged
incoming task
NT bit of Unchanged Unchanged Cleared
outgoing task
Back-link of Unchanged Set to outgoing Unchanged
incoming task TSS selector
Back-link of Unchanged Unchanged Unchanged
outgoing task
7.6.1 Busy Bit Prevents Loops 忙位阻止循环
The B-bit (busy bit) of the TSS descriptor ensures the integrity of the back-link. A chain of back-links may grow to any length as interrupt tasks interrupt other interrupt tasks or as called tasks call other tasks. The busy bit ensures that the CPU can detect any attempt to create a loop. A loop would indicate an attempt to reenter a task that is already busy; however, the TSS is not a reentrable resource.
TSS描述符的B位(忙位)确保后向连接的完整。当中断任务又中断其他中断任务或当调用任务再调用其他任务时,后向连接链可能增长得很大。忙位确保CPU可以检测任何创建一个循环的企图。循环表明企图重入一个已经在忙的任务;然而,TSS是不可重入资源。
The processor uses the busy bit as follows:
处理器在以下场景使用忙位:
- When switching to a task, the processor automatically sets the busy bit of the new task.
当切换到一个任务时,处理器自动设置新任务的忙位。
- When switching from a task, the processor automatically clears the busy bit of the old task if that task is not to be placed on the back-link chain (i.e., the instruction causing the task switch is JMP or IRET). If the task is placed on the back-link chain, its busy bit remains set.
当从一个任务切换时,处理器自动清除一个旧任务的忙位,如果那个任务没有被放入到后向连接链中(JMP或IRET指令引起的任务切换)。如果任务被放在后向连接链中,它的忙位将保持设置状态。
- When switching to a task, the processor signals an exception if the busy bit of the new task is already set.
当切换到一个任务时,如果新任务的忙位已经被置位,处理器会发送一个异常信号。
By these actions, the processor prevents a task from switching to itself or to any task that is on a back-link chain, thereby preventing invalid reentry into a task.
通过这些动作,处理器阻止一个任务切换到自身或者任何已经在后向连接链中的任务,因此可阻止对任务的无效重入。
The busy bit is effective even in multiprocessor configurations, because the processor automatically asserts a bus lock when it sets or clears the busy bit. This action ensures that two processors do not invoke the same task at the same time . (Refer to Chapter 11 for more on multiprocessing.)
即使在多处理器环境中忙位也是有效的,因为处理器在设置或清除一个忙位时会自动验证一个忙位锁。这个动作确保两个处理器在同一时间不会激活同一个任务。(参见第11章关于多处理器内容。)
7.6.2 Modifying Task Linkages 修改任务连
Any modification of the linkage order of tasks should be accomplished only by software that can be trusted to correctly update the back-link and the busy-bit. Such changes may be needed to resume an interrupted task before the task that interrupted it. Trusted software that removes a task from the back-link chain must follow one of the following policies:
任何对任务顺序连进行的修改都应当谨慎,只有那些被信任可以正确更新后向连接和忙位的软件才能实施。这样的修改对于在中断它的任务之前重启一个被中断的任务可能是需要。可信任的软件从后向连接移除一个任务时必须遵循以下策略:
- First change the back-link field in the TSS of the interrupting task, then clear the busy-bit in the TSS descriptor of the task removed from the list.
首先,修改正处于中断的任务的TSS的后向连接字段,然后清除从链表中被移除的任务的TSS描述符中的忙位。
- Ensure that no interrupts occur between updating the back-link chain and the busy bit.
确保在更新后向连接链和忙位时没有中断发生。