Exceptions
Exceptions
Definition
An exception is a condition requiring handling which causes a change in control flow
Note that this is a bit different from an exception in software. These exceptions happen outside of the program, and the current program is paused to run the exception handler.
Exception Types
List
- Internal (from processor)
- Fault: a potential error in executing an instruction, (e.g division by 0, unaligned memory access, page fault)
- Trap: a machine instruction that requests an OS service
- External
- Interrupt request (IRQ): a peripheral device requires service (e.g ADC sample ready, output error, timer counted down to 0)
Vector Table
Definition
A vector table is an array of handler addresses stored in memory
Figure
- The vector table is used by the interrupt controller (IC) to select the handler to run
- The IC is integral to the processor
- Each exception source is assigned a number (known as a vector for historical reasons) and a priority
Execution Handling
Steps
- Current thread of execution is suspended
- An exception handler (a subroutine) is invoked to service the exception
- The suspended thread is resumed
How does the computer know which handler to run? Vector table! The IC is responsible for handling exceptions (such as IRQ0) and selecting which handler to run.
Implementation
- On exception raise:
- If exception priority > current operating priority
- Preserve caller-save registers (registers 0, 1, 2, 3, 12, link register (14), program counter (15), and PSR flags) to save the current execution context
- Set operating mode to privileged
- Set operating priority to exception priority
- Branch to the handler
PC <- vector[exception #]
- If exception priority > current operating priority
- Handler:
- Preserve callee-save registers (R4-R11)
- Service the condition and clear IRQ
- Restore R4-R11
- Return from handler (
BX LR
)
- On handler return:
- Restore prior operating mode and priority
- Restore execution context from the stack (resume the suspended thread)
The handler is done by software while the "on exception raise" and "on handler return" is done by hardware.