Information Technology Reference
In-Depth Information
5. Invoke the interrupt handler. Finally, the hardware changes the pro-
gram counter to the address of the interrupt handler procedure, specified
via a special register in the processor that is accessible only to the kernel.
This register contains a pointer to an array of exception handler addresses
in memory. The type of interrupt is mapped to an index in this array, and
the program counter is set to the value at this index.
Notice that changing the program counter to start of the interrupt handler
means that the next thing that happens is that the interrupt handler software
begins to run.
The first thing the handler needs to do is save the rest of the interrupted
process's state|it needs to save the other registers before it changes them! So,
the handler pushes the rest of the registers, including the current stack pointer,
onto the stack. The x86 pushad instruction, which pushes the contents of all
general purpose integer registers onto the stack, is convenient here.
As Figure 2.11 indicates, at this point the kernel's exception stack holds (1)
the stack pointer, execution flags, and program counter saved by the hardware,
(2) an error code or dummy value, and (3) a copy of all of the general registers
(including the stack pointer but not the instruction pointer or eflags register)
as they were after the hardware pushed the stack pointer, execution flags, and
program counter onto the exception stack.
Once the handler has saved the interrupted thread's state to the stack, it is
free to use the registers as it pleases, and it can also push additional items onto
the stack. So, the handler can now do whatever work it needs to do.
When the handler completes, it can resume the interrupted process. To do
this, the handler pops all of the registers saved by the software from the stack,
thereby restoring all registers except the execution flags, program counter, and
stack pointer (which was changed by the hardware when the hardware pushed
the execution flags and program counter onto the stack.) For the x86 instruction
set, the popad instruction is commonly used. The handler also pops the error
value off the stack
Finally, the handler executes the x86 iret instruction to pop the program
counter, execution ags, and stack pointer from the kernel's exception stack
stack into their respective registers.
Thus, the interrupted thread's state has been restored to exactly what it
was before the interrupt, and it continues execution, as if nothing happened.
A small but important detail occurs when the operating system returns from
an exception, where the instruction is emulated by the kernel, e.g., for emulating
floating point hardware. If we return back to the instruction that caused the
exception, another exception will instantly recur! To prevent an infinite loop,
the exception handler modifies the program counter stored at the base on the
stack to point to the instruction immediately after the one causing the mode
 
Search WWH ::




Custom Search