Intel architecture ia-32 User Manual

Page of 636
Vol. 3A 5-53
INTERRUPT AND EXCEPTION HANDLING
Saved Instruction Pointer
The saved contents of CS and EIP registers generally point to the instruction that generated the
exception. If the page-fault exception occurred during a task switch, the CS and EIP registers
may point to the first instruction of the new task (as described in the following “Program State
Change” section).
Program State Change
A program-state change does not normally accompany a page-fault exception, because the
instruction that causes the exception to be generated is not executed. After the page-fault excep-
tion handler has corrected the violation (for example, loaded the missing page into memory),
execution of the program or task can be resumed.
When a page-fault exception is generated during a task switch, the program-state may change,
as follows. During a task switch, a page-fault exception can occur during any of following
operations:
While writing the state of the original task into the TSS of that task.
While reading the GDT to locate the TSS descriptor of the new task.
While reading the TSS of the new task.
While reading segment descriptors associated with segment selectors from the new task.
While reading the LDT of the new task to verify the segment registers stored in the new
TSS.
In the last two cases the exception occurs in the context of the new task. The instruction pointer
refers to the first instruction of the new task, not to the instruction which caused the task switch
(or the last instruction to be executed, in the case of an interrupt). If the design of the operating
system permits page faults to occur during task-switches, the page-fault handler should be called
through a task gate.
If a page fault occurs during a task switch, the processor will load all the state information from
the new TSS (without performing any additional limit, present, or type checks) before it gener-
ates the exception. The page-fault handler should thus not rely on being able to use the segment
selectors found in the CS, SS, DS, ES, FS, and GS registers without causing another exception.
(See the Program State Change description for “Interrupt 10—Invalid TSS Exception (#TS)” in
this chapter for additional information on how to handle this situation.) 
Additional Exception-Handling Information
Special care should be taken to ensure that an exception that occurs during an explicit stack
switch does not cause the processor to use an invalid stack pointer (SS:ESP). Software written
for 16-bit IA-32 processors often use a pair of instructions to change to a new stack, for example:
MOV SS, AX
MOV SP, StackTop