The idea: the hardware saves the registers, so a handler is a C function

On a small 8-bit chip the hardware saves only the return address, and the handler's own code saves the rest (see interrupt priorities and nesting for the vector table, nesting and latency in general). An Arm Cortex-M core does more in hardware. When it takes an exception (Arm's word for interrupts and faults alike), it pushes eight registers on the stack by itself, puts a magic value in LR, and starts the handler. The handler is an ordinary C function that ends with an ordinary return. The page shows one Cortex-M4 cycle by cycle, with three sources: the SysTick timer, the UART0 receiver and a button on GPIO port F. The vector numbers are those of TI's TM4C123, a common Cortex-M4 microcontroller.

Left: the Cortex-M memory map as four regions from the bottom: Code from 0x0000_0000 holding the vector table and the program, SRAM from 0x2000_0000 holding variables and the stacks with MSP starting at 0x2000_8000, Peripherals from 0x4000_0000 with UART0 at 0x4000_C000, and the Private Peripheral Bus from 0xE000_0000 with SysTick at 0xE000_E010, the NVIC at 0xE000_E100 and SCB ICSR at 0xE000_ED04. Right: the first words of the vector table: 0x00 initial SP 0x20008000, 0x04 Reset, 0x08 NMI, 0x0C HardFault, 0x38 PendSV 0x000002C1, 0x3C SysTick 0x00000201, 0x54 IRQ5 UART0 0x00000241, 0xB8 IRQ30 GPIOF 0x00000281
The map is the same on every Cortex-M chip; the vector table at address 0 holds the first stack pointer and one handler address per exception.

The memory map and the vector table

Every Cortex-M chip has the same 4 GB memory map: code from 0x0000_0000, SRAM from 0x2000_0000, peripherals from 0x4000_0000, and the Private Peripheral Bus (PPB) from 0xE000_0000. The PPB holds the core's own devices: the SysTick timer at 0xE000_E010, the NVIC (nested vectored interrupt controller) at 0xE000_E100 and the System Control Block, whose SCB->ICSR sits at 0xE000_ED04. So the same code that enables an interrupt works on any vendor's chip.

The vector table starts at address 0. Its entries are addresses, not jump instructions. Word 0 is the initial value of the main stack pointer MSP (0x2000_8000, the top of 32 KB of SRAM). Word 1 is the Reset handler. Exception number n has its vector at 4 × n: NMI is 2, HardFault 3, SVCall 11, PendSV 14, SysTick 15. Device interrupts start at 16, so IRQ5 (UART0) is exception 21 at 0x54 and IRQ30 (GPIOF) is exception 46 at 0xB8. Each vector has bit 0 set (0x0000_0201 for a handler at 0x0000_0200). It means Thumb code, the only kind a Cortex-M runs.

Entry: the 8-word stack frame

When an exception is taken, the hardware pushes a stack frame of 8 words on the current stack: R0, R1, R2, R3, R12, LR, the return address (PC) and xPSR. These are exactly the registers the Arm calling convention (AAPCS) lets a C function change. A C handler saves R4–R11 itself only if it uses them, like any C function. While the words are pushed, the core reads the vector from the table. Arm gives 12 cycles from the request to the handler's first instruction on Cortex-M3 and Cortex-M4 with zero-wait-state memory.

Demo: SysTick, entry and return: SysTick fires at cycle 4 while main counts in R0. MSP goes from 0x2000_8000 to 0x2000_7FE0. The frame holds R0 = 2 at 0x2000_7FE0, then R1 = 0x11, R2 = 0x22, R3 = 0x33, R12 = 0xCC, LR = 0x0000_0105, PC = 0x0000_0142 (main's next instruction) and xPSR = 0x0100_0000 at 0x2000_7FFC. SysTick_Handler starts at cycle 16: latency 12. In Handler mode the core always uses the MSP, and IPSR (the low bits of xPSR) holds the exception number, 15.

The main stack at the deepest point of the nesting demo. From 0x2000_7FFC down to 0x2000_7FE0 the first frame, pushed when SysTick interrupted main: xPSR 0x01000000, PC 0x00000142, LR 0x00000105, R12, R3, R2, R1, R0. Below it, from 0x2000_7FDC to 0x2000_7FC0, the second frame, pushed when UART0 interrupted SysTick_Handler: xPSR 0x0100000F, PC 0x00000202, LR 0xFFFFFFF9, R12, R3, R2, R1, R0 = 0x20000000. MSP points at 0x2000_7FC0. On the right the three EXC_RETURN values: 0xFFFFFFF1 back to Handler mode on MSP, 0xFFFFFFF9 back to Thread mode on MSP, 0xFFFFFFFD back to Thread mode on PSP
Each level of nesting adds one 8-word frame; the LR saved in the inner frame is the outer handler's EXC_RETURN.

EXC_RETURN and BX LR

On entry LR does not get a return address. It gets an EXC_RETURN value that says how to come back:

EXC_RETURNreturn tostack to unstack from
0xFFFFFFF1Handler mode (a nested exception)MSP
0xFFFFFFF9Thread modeMSP
0xFFFFFFFDThread modePSP

The handler ends with BX LR, the normal return of a C function. Code never runs at 0xFFFFFFxx, so the core knows that writing such a value to PC means "return from exception". It pops the 8 words back and goes on where it was. The page counts 12 cycles for this unstacking. In Demo: UART0 preempts SysTick (nesting) UART0 (priority 0x40) comes at cycle 18, in the middle of SysTick_Handler (0x80). A second frame goes on the MSP at 0x2000_7FC0. Its saved LR is SysTick's EXC_RETURN 0xFFFFFFF9, its xPSR is 0x0100_000F (IPSR = 15), and UART0_Handler gets LR = 0xFFFFFFF1. UART0's latency is again 12 cycles. At the deepest point the MSP holds 16 words.

Priorities: NVIC_IPR, PRIGROUP, BASEPRI and PRIMASK

Each interrupt has an 8-bit priority, lower = more urgent: NVIC_IPR0–NVIC_IPR59 for device IRQs (IRQ5 is byte 1 of NVIC_IPR1) and SCB->SHPR3 for SysTick (bits 31:24) and PendSV (bits 23:16). A chip implements only the top bits; the TM4C123 has 3 (bits 7:5), so it has 8 levels, 0x00 to 0xE0. Reset, NMI and HardFault have the fixed priorities −3, −2 and −1 and beat everything. A request sets a pending bit (NVIC_ISPR, or ICSR.PENDSTSET for SysTick); an enable bit in NVIC_ISER must be set for a device IRQ.

PRIGROUP (3 bits in SCB->AIRCR) splits the priority into a group priority (bits 7 down to PRIGROUP+1) and a subpriority (the bits below). Only a better group preempts. The subpriority only decides which of several pending requests goes first. In Demo: PRIGROUP 5, same group waits GPIOF (0x60) and UART0 (0x40) both have group 1. UART0 comes at cycle 18 while GPIOF_Handler runs, and waits: it starts at cycle 35 (latency 17) instead of nesting with latency 12 as it would under PRIGROUP 0.

registerwhat it blocksset with
PRIMASK = 1every exception with a configurable priority (NMI and HardFault still run)CPSID i / __disable_irq()
BASEPRI = ppriority p and everything less urgent; more urgent ones still get inMSR BASEPRI, R0 / __set_BASEPRI()
FAULTMASK = 1everything except NMICPSID f

BASEPRI makes a critical section that does not delay urgent interrupts. In Demo: BASEPRI = 0x60 holds SysTick back main sets BASEPRI to 0x60 and increments a shared variable. SysTick (0x80) asks at cycle 4 and stays pending. UART0 (0x40) asks at cycle 6 and gets in at once (latency 12). SysTick is taken only after MSR BASEPRI, R0 with R0 = 0 ends at cycle 44; its latency is 52 cycles.

Tail-chaining and late arrival

When a handler returns and another exception is pending that beats the code it would return to, a Cortex-M does not pop 8 words only to push the same 8 words again. It tail-chains: the frame stays, the core fetches the next vector and starts the next handler 6 cycles later. Arm documents 6 cycles for Cortex-M3 and Cortex-M4. In Demo: tail-chaining into a pending SysTick UART0 runs from cycle 16, SysTick asks at cycle 20 and waits (0x80 is less urgent). UART0_Handler's BX LR ends at cycle 25 and SysTick_Handler starts at 31, latency 11. Without tail-chaining it would be 12 + 12 = 24 cycles between the two handlers instead of 6.

Two timelines in cycles. Top, without tail-chaining: UART0 handler, then 12 cycles of unstacking, 12 cycles of stacking the same frame again, then the SysTick handler: 24 cycles between the handlers. Bottom, with tail-chaining: UART0 handler ends at cycle 25, 6 cycles of vector fetch, SysTick handler starts at cycle 31. Under it, late arrival: SysTick starts stacking at cycle 4, UART0 arrives at cycle 7 during the stacking, UART0's handler starts at cycle 16 using the same frame, then SysTick tail-chains at cycle 25
Tail-chaining skips the pop and the push between two handlers; late arrival lets a more urgent request take over a stacking already under way.

A late arrival is the other direction. In Demo: late arrival during stacking SysTick is taken at cycle 4, and UART0 asks at cycle 7 while the frame is still being pushed. The frame is the same for any handler, so the core keeps pushing and only changes the vector it fetches. UART0_Handler starts at cycle 16: latency 9, shorter than 12 because the stacking had already begun. SysTick then tail-chains after it and starts at 31. Only one frame (8 words) was ever on the stack.

PendSV: switching tasks

An RTOS runs its tasks in Thread mode on the process stack (PSP, chosen with CONTROL.SPSEL = 1) and its handlers on the MSP. The task switch itself is done in PendSV, an exception that software makes pending by writing PENDSVSET (bit 28) to SCB->ICSR. It gets the lowest priority (0xE0 here), so it runs only after every other handler is done, and a switch never happens in the middle of an interrupt.

Demo: PendSV context switch, task A to B: SysTick fires at cycle 4 while task A runs. The frame goes on task A's stack (PSP 0x2000_1000 → 0x2000_0FE0) and LR = 0xFFFFFFFD. SysTick_Handler counts the tick and sets PENDSVSET at cycle 29. Its BX LR tail-chains into PendSV_Handler at cycle 36, which:

MRS   R0, PSP           ; task A's stack, frame on top
STMDB R0!, {R4-R11}     ; save the other 8 registers under it
LDR   R1, =cur_sp
STR   R0, [R1]          ; tcbA.sp = 0x20000FC0
LDR   R2, =next_sp
LDR   R0, [R2]          ; task B's saved sp = 0x200013C0
LDMIA R0!, {R4-R11}     ; restore B's R4-R11
MSR   PSP, R0           ; PSP = 0x200013E0, B's hardware frame
BX    LR                ; 0xFFFFFFFD: unstack from PSP

The exception return pops task B's frame, so at cycle 77 the core runs task B at PC = 0x0000_0180 with PSP = 0x2000_1400. Nothing jumps to task B: the hardware simply returns into a different stack. The OS side of a switch (choosing the next task, page tables, the TLB) is on the Linux context switch. A bare-metal SysTick scheduler with no task switch is on the time-triggered scheduler.

On the real chip

With CMSIS the set-up is a few calls: NVIC_SetPriorityGrouping(5) writes PRIGROUP, NVIC_SetPriority(UART0_IRQn, 2) writes priority 2 << 5 = 0x40 into NVIC_IPR1, NVIC_EnableIRQ(UART0_IRQn) sets bit 5 of NVIC_ISER0, and SysTick_Config(ticks) starts the timer and sets its priority. The handler names (SysTick_Handler, UART0_Handler, PendSV_Handler) are the names the vector table in the startup file points to. Compare MIPS exceptions, where one handler address serves all causes and software reads the Cause register, and Linux interrupts, where the kernel saves the registers itself.

See also a FIR filter accelerator, whose done interrupt costs 32 cycles of exception entry, handler and return per call.

See also MCU startup from power-on to main(), where the reset vector leads into Reset_Handler copying .data and zeroing .bss on an STM32F103.

See also the watchdog timer, which resets the chip when the firmware stops kicking it, so the CPU starts again from the reset vector instead of returning from a handler.

What the page leaves out

No FPU: with the FPU in use (CONTROL.FPCA = 1) the frame grows to 26 words, with lazy stacking, and EXC_RETURN becomes 0xFFFFFFE1/E9/ED. Arm documents 12 cycles for entry and 6 for tail-chaining; the page's 12 cycles for unstacking and its rule that a late arrival's handler starts at least 6 cycles after the request are simplifications, and real chips add flash wait states. Multi-cycle instructions (LDM, STM, divide) can be interrupted and resumed; here requests wait for the instruction boundary. The page also leaves out the 4-byte padding (xPSR bit 9) when the SP is not 8-byte aligned, fault handling (UsageFault, BusFault and MemManage escalating to HardFault), SVCall for system calls, the vector table offset register VTOR, WFI sleep, ARMv8-M security states, and peripheral flags that must be cleared before returning.

On the mind map

Open the Operating Systems mind mapOperatingSystemsBoot and program start: from power on to main()Boot and program startProcesses and the kernel: how user code talks to the kernelProcesses and the kernelInterrupts and devices: hardware interrupts the CPUInterrupts and devicesScheduling and deadlock: who runs next, who gets whatScheduling and deadlockVirtual memory: every address is translatedVirtual memoryPhysical memory: who gets which bytes of RAMPhysical memoryFiles and storage: from open() down to the diskFiles and storageDisks and RAID: blocks down to platter or flashDisks and RAIDIsolation and security: limit what a process can doIsolation and securityInterrupt-driven I/OInterrupt-driven I/OIRQ, IDT, handler, then iretInterrupt priority nestingInterrupt priority nestingOnly a more urgent level cuts inWatchdog timerWatchdog timerKick only after real work, never from an ISRCortex-M exceptionsHardware stacks 8 words; tail-chain in 6Kinds of interruptsKinds of interruptsIRQ, timer, NMI, fault, syscall: one IDTDMADMAdevice writes RAM itself, then one IRQHW/SW co-design acceleratorHW/SW co-design acceleratorAmdahl: 2,226 → 478 cycles, capped at 12.5×Packet receive pathPacket receive pathone IRQ, then NAPI polls in a softirqOpen the Operating Systems mind mapOperating Systemshardware interrupts the CPUInterrupts and devicesInterrupt-driven I/OInterrupt-driven I/OIRQ, IDT, handler, then iretInterrupt priority nestingInterrupt priority nestingOnly a more urgent level cuts inWatchdog timerWatchdog timerKick only after real work, never from an ISRCortex-M exceptionsHardware stacks 8 words; tail-chain in 6Kinds of interruptsKinds of interruptsIRQ, timer, NMI, fault, syscall: one IDTDMADMAdevice writes RAM itself, then one IRQHW/SW co-design acceleratorHW/SW co-design acceleratorAmdahl: 2,226 → 478 cycles, capped at 12.5×Packet receive pathPacket receive pathone IRQ, then NAPI polls in a softirq
This page on the Operating Systems mind map · 78 pages in 9 branches · open it to see the whole map and test yourself in recall mode.