Course: Computer Architectures
Source lesson: ARM Exceptions and Interrupts
Modes and privilege
Cortex-M has Thread mode for normal code and Handler mode for exception handlers. Handler mode is always privileged. Thread mode can be privileged or user level.
CONTROL[0] selects privilege (0 privileged, 1 user). CONTROL[1] selects the active Thread-mode stack (0 MSP, 1 PSP). Handler mode uses MSP.
Automatic entry sequence
When an exception occurs, hardware:
- Stacks
r0,r1,r2,r3,r12,LR,PC, andPSRon the currently active stack. - Looks up the handler in the Interrupt Vector Table.
- Switches to
MSPand updatesIPSRwith the exception number. - Loads the handler address into
PCand anEXC_RETURNcode intoLR. - Runs the handler.
The new stack frame is:
Offset from new SP | Value |
|---|---|
0 | r0 |
4 | r1 |
8 | r2 |
12 | r3 |
16 | r12 |
20 | LR |
24 | PC |
28 | PSR |
The saved PC is at offset #24 because six words come before it.
Vector table
The vector table maps exception numbers to handler addresses. It begins with the initial stack pointer, Reset, NMI, Hard Fault, memory, bus, and usage faults, followed by SVC, PendSV, SysTick, and external interrupts.
EXC_RETURN
During a handler, LR is not an ordinary subroutine return address. Its encoded value tells the processor how to restore context:
| Value | Return target |
|---|---|
0xFFFFFFF1 | Handler mode |
0xFFFFFFF9 | Thread mode using MSP |
0xFFFFFFFD | Thread mode using PSP |
The handler normally executes:
BX LRThe processor recognizes the special value, un-stacks the saved frame, clears the exception’s active state, and resumes the selected context.
Common confusion
Exception entry is not the same as BL: hardware saves a complete context and changes mode. LR contains EXC_RETURN, not simply the address after a call. The saved PC belongs to the interrupted code and is read from the stack frame.
Retrieval questions
- Which stack is used while a handler runs? MSP.
- Where is the stacked
PC? At[SP,#24]from the newSP. - Why does
BX LRreturn from an exception? BecauseLRcontains anEXC_RETURNcode.