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

Execution Handling

Steps

  1. Current thread of execution is suspended
  2. An exception handler (a subroutine) is invoked to service the exception
  3. 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:
  • Handler:
    1. Preserve callee-save registers (R4-R11)
    2. Service the condition and clear IRQ
    3. Restore R4-R11
    4. Return from handler (BX LR)
  • On handler return:
    1. Restore prior operating mode and priority
    2. 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.