Processor Modes, Register Banks, and CP15 Control
Understand from hardware first principles why Cortex-A7 implements nine processor modes and banked SP/LR/FIQ register storage, decode every control slice of CPSR, and bracket privileged CP15 SCTLR accesses with DSB and ISB barriers.
What you'll learn
- Classify all nine Cortex-A7 processor modes and their privilege boundaries.
- Trace which register bank an exception handler actually modifies.
- Decode CPSR controls and identify an MRC or MCR access to SCTLR.
Why Silicon Separates Processor Modes and Banked Register Storage
Ask a fundamental CPU design question: what happens on the exact clock edge an external Ethernet or timer interrupt asserts while an unprivileged application is executing? Suppose the CPU had only one Stack Pointer (SP) and one privilege state. If the user program had just corrupted SP (or pointed SP at kernel memory!) when the interrupt arrived, hardware pushing return state onto that untrusted SP would either trigger a double-fault crash or hand Ring 0 write access to an attacker! Conversely, if hardware copied all 16 general-purpose registers into RAM on every interrupt, entry latency would balloon by dozens of bus cycles.
ARMv7-A (AArch32 on Cortex-A7) solves both problems simultaneously using nine architectural processor modes selected by the low five bits M[4:0] of the Current Program Status Register (CPSR), paired with selective hardware register banking. Study the physical register matrix in Figure 2.1a:
- Privilege Boundaries (
PL0,PL1,PL2):User(0x10) is the sole unprivilegedPL0mode: it cannot mask interrupts, switchCPSRmodes, or touchCP15system control. Standard operating-system and bare-metal exception work runs atPL1:Supervisor(SVC,0x13, entered on Reset andSVCsyscalls),IRQ(0x12),FIQ(0x11),Abort(ABT,0x17, memory faults),Undefined(UND,0x1B, illegal/disabled opcodes),System(SYS,0x1F), plus TrustZoneMonitor(MON,0x16) in Secure state.Hyp(HYP,0x1A) sits at Non-SecurePL2for virtualization (ERET/ELR_hyp). - What Is Banked vs What Remains Shared! Look closely at the left column of Figure 2.1a:
r0..r7are100%physically shared across every single mode (r8..r12are also shared across8modes, banked only inFIQ0x11so ultra-fastFIQhandlers can run usingr8_fiq..r12_fiqwith zero stack pushes!). Exception modes (SVC,IRQ,FIQ,ABT,UND,MON) each own a private physicalr13(SP_<mode>),r14(LR_<mode>), andSPSR_<mode>. Meanwhile, privilegedSystem(0x1F) deliberately sharesUser'sr13_usrandr14_usrwith zeroSPSR, letting an OS kernel manipulate user-mode stacks without riskingSPSRcorruption. Therefore, anIRQhandler gets its own safe kernel stack (SP_irq) instantly in hardware—yet if thatIRQhandler modifiesr0..r3orr12without pushing them first, it overwrites the interrupted program's live registers!
Figure 2.1a. Physical Cortex-A7 register banking across all nine modes — private SP/LR/ protect exception entry, while shared r0..r7 (and r8..r12 outside FIQ) still require software stack saves.
mental model: Switching processor modes on Cortex-A7 is like turning a master key on a control console: the top-bench tools (r0..r7) remain shared in plain sight, while the bottom emergency drawers (SP, LR, SPSR) physically mux in a private compartment for IRQ or SVC without copying a byte.
Where it stops working: Unlike mechanical drawers, unprivileged User mode cannot turn the mode key itself, User and System modes share the exact same drawer with zero SPSR, and changing system switches requires pipeline synchronization barriers to take effect cleanly.
definition (, Saved Program Status (SPSR) & CP15 System Control (SCTLR)): In AArch32, CPSR holds arithmetic flags N, Z, C, V ([31:28]), async abort mask A ([8]), IRQ mask I ([7]), FIQ mask F ([6]), Thumb state T ([5]), and mode M[4:0] ([4:0]). Entering an exception copies CPSR into that mode's SPSR_<mode> (USR and SYS have no SPSR). Architectural caches, MMU, alignment faults, and vector bases live in Coprocessor 15 (CP15) registers—such as SCTLR (c1, c0, 0), accessed at >= PL1 via MRC (read) and MCR (write) bracketed by dsb sy and isb.
Step through the four-stage resolution of mode, register bank, SPSR, and CP15 state below:
- Step 1. Step 1 (Current State): Decode CPSR M[4:0] and preserve NZCV [31:28] plus I/F/T [7:5] via ~0x1FU masking.
- Step 2. Step 2 (Register View): Remember r0..r7 are shared everywhere; only FIQ banks r8..r12 alongside SP and LR.
- Step 3. Step 3 (Exception State): Exception entry snapshots CPSR into SPSR_<mode> and selects banked SP/LR (absent in USR/SYS).
- Step 4. Step 4 (System Control): Read SCTLR via MRC p15, modify only target bits, and serialize MCR p15 with DSB SY and ISB.
Figure 2.1b. Always resolve active CPSR mode, physical register bank, and I/F masks before touching SP or CP15.
Decoding CPSR Control Lanes and Synchronizing CP15 SCTLR Writes
Inspect Figure 2.1b to see every bit slice inside CPSR and CP15 SCTLR (p15, 0, Rt, c1, c0, 0). Take the classic bare-metal reset word 0xA00000D3:
- Upper nibble
0xA(1010_2in[31:28]) meansN = 1,Z = 0,C = 1,V = 0. - Low control byte
0xD3(1101_0011_2in[7:0]) meansI = 1(IRQmasked),F = 1(FIQmasked),T = 0(32-bit ARM ISA), andM[4:0] = 0x13(Supervisormode).
Notice what happens if startup assembly carelessly writes msr cpsr_c, #0x13 instead of #0xD3 (or masked RMW): clearing bits 7 and 6 unmasks live IRQ and FIQ interrupts before _start has initialized SP_irq or VBAR!
Figure 2.1b. Exact 32-bit bitfields for CPSR (top) and CP15 SCTLR (bottom) with barrier-ordered control updates.
Why does SCTLR use special coprocessor instructions (mrc / mcr) plus dsb sy and isb instead of a memory-mapped 0x020... address? Because SCTLR sits inside the Cortex-A7 core pipeline, controlling how the CPU itself translates virtual addresses (M, bit 0), checks pointer alignment (A, bit 1), caches data (C, bit 2) and instructions (I, bit 12), and locates exception vectors (V, bit 13). In enable_alignment_check below, mrc p15, 0, r0, c1, c0, 0 copies SCTLR into r0, orr r0, r0, #2 sets only SCTLR.A (bit 1) without disturbing M, C, I, or V, dsb sy drains pending memory accesses under the old rules, mcr writes SCTLR, and isb flushes already-fetched pipeline instructions so the very next load/store obeys strict alignment! (Note: disabling SCTLR.C or SCTLR.M requires full set/way cache cleaning first so dirty L1/L2 lines are never abandoned.)
learner@ksa:~/gl-e01/m02-l01$ bat target.S File: target.S .syntax unified .cpu cortex-a7 .arm .text .global read_sctlr read_sctlr: mrc p15, 0, r0, c1, c0, 0 bx lr .global enable_alignment_check enable_alignment_check: mrc p15, 0, r0, c1, c0, 0 orr r0, r0, #2 dsb sy mcr p15, 0, r0, c1, c0, 0 isb bx lrModeling CPSR Mode Validity and Physical Bank Aliasing in C11
In cpsr_bank_decoder.c, we turn the architectural tables into an executable verifier. First, decode_cpsr() validates M[4:0] against the exact nine Cortex-A7 encodings (rejecting undefined 5-bit patterns like 0x14), derives PL1/PL2 privilege (m != MODE_USR), and flags SPSR existence (m != MODE_USR && m != MODE_SYS). Second, bank_slot() maps (mode, reg) pairs onto physical storage indices—proving USR and SYS alias the same r13 (SP), IRQ owns a distinct r13, and FIQ owns private r8..r14 while sharing r0..r7!
learner@ksa:~/gl-e01/m02-l01$ bat -H 10 -H 11 -H 12 -H 13 -H 14 -H 15 -H 16 -H 17 -H 18 -H 19 -H 20 -H 25 -H 26 -H 27 -H 28 -H 29 -H 30 -H 31 -H 32 -H 33 -H 39 -H 40 cpsr_bank_decoder.c File: cpsr_bank_decoder.c #include <assert.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> enum { MODE_USR = 0x10U, MODE_FIQ = 0x11U, MODE_IRQ = 0x12U, MODE_SVC = 0x13U, MODE_MON = 0x16U, MODE_ABT = 0x17U, MODE_HYP = 0x1aU, MODE_UND = 0x1bU, MODE_SYS = 0x1fU }; static bool decode_cpsr(uint32_t cpsr, bool *priv, bool *has_spsr, bool *irq_m, bool *fiq_m, bool *thumb) { unsigned m = cpsr & 0x1fU; bool valid = (m == MODE_USR || m == MODE_FIQ || m == MODE_IRQ || m == MODE_SVC || m == MODE_MON || m == MODE_ABT || m == MODE_HYP || m == MODE_UND || m == MODE_SYS); if (!valid) return false; *priv = (m != MODE_USR); *has_spsr = (m != MODE_USR && m != MODE_SYS); *irq_m = ((cpsr >> 7) & 1U) != 0U; *fiq_m = ((cpsr >> 6) & 1U) != 0U; *thumb = ((cpsr >> 5) & 1U) != 0U; return true; } static unsigned bank_slot(unsigned mode, unsigned reg) { assert(reg < 15U); if (mode == MODE_FIQ && reg >= 8U) return 16U + (reg - 8U); if (reg >= 13U && mode == MODE_IRQ) return 23U + (reg - 13U); if (reg >= 13U && mode == MODE_SVC) return 25U + (reg - 13U); if (reg >= 13U && mode == MODE_ABT) return 27U + (reg - 13U); if (reg >= 13U && mode == MODE_UND) return 29U + (reg - 13U); return reg; } int main(void) { bool priv = false, spsr = false, im = false, fm = false, th = false; assert(decode_cpsr(0xa00000d3U, &priv, &spsr, &im, &fm, &th)); assert(priv && spsr && im && fm && !th); assert(decode_cpsr(0x60000030U, &priv, &spsr, &im, &fm, &th)); assert(!priv && !spsr && !im && !fm && th); assert(decode_cpsr(MODE_SYS, &priv, &spsr, &im, &fm, &th) && priv && !spsr); assert(!decode_cpsr(0x14U, &priv, &spsr, &im, &fm, &th)); assert(bank_slot(MODE_USR, 13U) == bank_slot(MODE_SYS, 13U)); assert(bank_slot(MODE_IRQ, 13U) != bank_slot(MODE_USR, 13U)); assert(bank_slot(MODE_FIQ, 8U) != bank_slot(MODE_USR, 8U)); assert(bank_slot(MODE_FIQ, 0U) == bank_slot(MODE_USR, 0U)); uint32_t old = 0xa00000c0U | MODE_USR; uint32_t next = (old & ~UINT32_C(0x1f)) | MODE_SVC; assert((next & 0x1fU) == MODE_SVC && (next & ~0x1fU) == (old & ~0x1fU)); uint32_t sctlr = 0x00c50078U, mod = sctlr | 2U; assert((mod & ~2U) == (sctlr & ~2U)); printf("CPSR=0x%08X mode=0x%02X I=%d F=%d T=%d SCTLR.A=%u\n", next, next & 0x1fU, (next >> 7) & 1U, (next >> 6) & 1U, (next >> 5) & 1U, (mod >> 1) & 1U); return 0; }- Classify all nine legal Cortex-A7 M[4:0] encodings and distinguish privileged SPSR-bearing exception modes from USR/SYS.
- Map FIQ r8..r14 and exception r13..r14 to distinct physical storage slots while keeping r0..r7 shared across every mode.
- Clear only ~0x1FU when switching CPSR mode so NZCV flags [31:28] and I/F/T controls [7:5] remain intact.
Running the Mode Decoder and Cross-Assembling CP15 Accessors
Compile and run cpsr_bank_decoder.c, then cross-assemble target.S for Cortex-A7. Remember the host/target boundary: clang proves valid ARMv7-A MRC/MCR instruction encoding in target.o, whereas executing mrc p15, 0, r0, c1, c0, 0 from User mode (0x10) on real silicon immediately traps into Undefined (0x1B) mode!
learner@ksa:~/gl-e01/m02-l01gcc -std=c11 -Wall -Wextra -Werror -pedantic cpsr_bank_decoder.c -o cpsr_bank_decoderlearner@ksa:~/gl-e01/m02-l01./cpsr_bank_decoderCPSR=0xA00000D3 mode=0x13 I=1 F=1 T=0 SCTLR.A=1learner@ksa:~/gl-e01/m02-l01clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m02-l01learner@ksa:~/gl-e01/m02-l01strace -e trace=write,exit_group \./cpsr_bank_decoderwrite(1, "CPSR=0xA00000D3 mode=0x13 I=1 F="..., 48) = 48exit_group(0) = ?+++ exited with 0 +++Hardware Banking vs AAPCS Software Preservation
Keep hardware mode banking (CPSR.M[4:0] selecting private SP/LR) strictly separate from software AAPCS32 preservation (callees pushing r4..r11). Ordinary BL function calls never change processor mode or register banks—the callee saves r4..r11 on the current stack by convention. Conversely, hardware IRQ entry switches SP to SP_irq and LR to LR_irq, yet leaves r0..r12 pointing at the interrupted program's physical registers—so your top-level IRQ stub must push r0..r3 and r12 onto SP_irq before calling any C function!
Processor Mode (M[4:0]) | Privilege | Private Banked Registers | SPSR Present | Primary Bare-Metal / OS Role |
|---|---|---|---|---|
User (0x10) | PL0 (unpriv) | None (shares r0..r14 with SYS) | No | Unprivileged user-space application execution |
System (0x1F) | PL1 (priv) | Shares USR r0..r14 bank | No | Privileged OS helpers accessing User SP/LR without SPSR risk |
Supervisor (0x13) / IRQ (0x12) | PL1 (priv) | r13 (SP), r14 (LR) per mode | Yes (SPSR_svc/irq) | Reset/kernel SVC execution and GICv2 IRQ entry (r0..r12 shared!) |
FIQ (0x11) | PL1 (priv) | r8..r12, r13 (SP_fiq), r14 (LR_fiq) | Yes (SPSR_fiq) | Low-latency fast interrupt path avoiding stack saves on r8..r12 |
Later: Lesson m02-l02 examines how SCTLR.V and CP15 VBAR compute exception vector addresses alongside ARMv7-A pre/post-indexed load/store addressing and CMP condition flags.
Common Production Pitfalls
warning: Symptom: Early _start assembly executes msr cpsr_c, #0x13 to enter Supervisor mode, and a pending timer or GPIO interrupt immediately crashes the Cortex-A7 before SP_irq or VBAR is initialized. Root Cause: Writing literal 0x13 (0001_0011_2) into CPSR[7:0] cleared I (bit 7) and F (bit 6) to 0, unmasking IRQ and FIQ prematurely. Fix: Keep mask bits 0xC0 set during startup (0xD3 = I | F | MODE_SVC, or cpsid if, #0x13) until all exception stacks and vectors are ready.
warning: Symptom: Boot code clears SCTLR.C (bit 2, data cache enable) via mcr p15, 0, r0, c1, c0, 0 without cache maintenance, and global structs modified moments earlier silently revert to old DDR3 values. Root Cause: Clearing SCTLR.C stops cache lookups without writing dirty lines sitting inside L1/L2 data caches out to DDR3, and omitting dsb sy / isb leaves memory and pipeline state unsynchronized. Fix: Clean and invalidate affected data-cache levels first, issue dsb sy, write SCTLR, and execute isb.
Practice & Retrieval
Guided Practice: Decoding CPSR 0x80000192 & Multi-Mode SP Setup
Step 1 (Bitfield Decode): Decode CPSR = 0x80000192U into flags, masks, ISA state, mode, privilege, and SPSR presence (N=1 at bit 31, A=1 at bit 8, 0x92 = 1001_0010_2 -> I=1, F=0, T=0 ARM, M=0x12 IRQ mode at PL1 with valid SPSR_irq). Step 2 (Banked SP Trace): If _start initializes sp = 0x87820000 in SVC (0x13) and switches to IRQ (0x12) without loading sp again, state what SP_svc and SP_irq hold (SP_svc == 0x87820000, SP_irq == uninitialized garbage!). Step 3 (Mask Expression): Write the C expression that clears only CPSR.I (bit 7) (cpsr & ~(UINT32_C(1) << 7)).
Independent Practice: IRQ Entry & SPSR Return Simulator (mode_transition_sim.c)
Write a standalone ISO C11 verifier mode_transition_sim.c modeling a 31-word physical Cortex-A7 register file, cpsr, and per-mode spsr array. Implement enter_irq() (SPSR_irq = CPSR, M = 0x12, I = 1, T = 0, LR_irq = pc + 4) and subs_pc_lr_4() (restoring CPSR = SPSR_irq while rejecting calls from USR or SYS). Prove r13_usr survives an IRQ round-trip while an unsaved write to r2 inside the handler clobbers User r2.
Key takeaways
Cortex-A7provides nine modes (UseratPL0;FIQ,IRQ,Supervisor,Abort,Undefined,System, and SecureMonitoratPL1;HypatPL2) encoded inCPSR.M[4:0].r0..r7are physically shared across every mode (FIQprivately banksr8..r14, other exception modes bankSPandLR, andSystemsharesUser'sr0..r14).CPSRholds liveNZCVflags,A/I/Fmasks,Tstate, andM[4:0]; exception modes save interruptedCPSRintoSPSR_<mode>, whereasUserandSystemhave noSPSR.- Privileged
CP15registers likeSCTLR(c1, c0, 0) are read viaMRC p15and updated via masked RMW +dsb sy+MCR p15+isb. - The Cortex-A7 architectural modes are User, FIQ, IRQ, Supervisor, Monitor, Abort, Hyp, Undefined, and System.
- User and System share general-purpose register storage; exception-mode banking does not preserve every general-purpose register.
- The callee saves and restores r4 under the call convention.