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.

20 minfree previewLinux 6.12C11U-Boot 2025.x→ module 2 lab
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:

  1. Privilege Boundaries (PL0, PL1, PL2): User (0x10) is the sole unprivileged PL0 mode: it cannot mask interrupts, switch CPSR modes, or touch CP15 system control. Standard operating-system and bare-metal exception work runs at PL1: Supervisor (SVC, 0x13, entered on Reset and SVC syscalls), IRQ (0x12), FIQ (0x11), Abort (ABT, 0x17, memory faults), Undefined (UND, 0x1B, illegal/disabled opcodes), System (SYS, 0x1F), plus TrustZone Monitor (MON, 0x16) in Secure state. Hyp (HYP, 0x1A) sits at Non-Secure PL2 for virtualization (ERET / ELR_hyp).
  2. What Is Banked vs What Remains Shared! Look closely at the left column of Figure 2.1a: r0..r7 are 100% physically shared across every single mode (r8..r12 are also shared across 8 modes, banked only in FIQ 0x11 so ultra-fast FIQ handlers can run using r8_fiq..r12_fiq with zero stack pushes!). Exception modes (SVC, IRQ, FIQ, ABT, UND, MON) each own a private physical r13 (SP_<mode>), r14 (LR_<mode>), and SPSR_<mode>. Meanwhile, privileged System (0x1F) deliberately shares User's r13_usr and r14_usr with zero SPSR, letting an OS kernel manipulate user-mode stacks without risking SPSR corruption. Therefore, an IRQ handler gets its own safe kernel stack (SP_irq) instantly in hardware—yet if that IRQ handler modifies r0..r3 or r12 without pushing them first, it overwrites the interrupted program's live registers!
fig_m02_l01_a.svg — Physical Cortex-A7 register banking across all nine…

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:

ast_m02_l01.svg — Always resolve active CPSR mode, physical register…step 1/4
  1. 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.
  2. Step 2. Step 2 (Register View): Remember r0..r7 are shared everywhere; only FIQ banks r8..r12 alongside SP and LR.
  3. Step 3. Step 3 (Exception State): Exception entry snapshots CPSR into SPSR_<mode> and selects banked SP/LR (absent in USR/SYS).
  4. 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_2 in [31:28]) means N = 1, Z = 0, C = 1, V = 0.
  • Low control byte 0xD3 (1101_0011_2 in [7:0]) means I = 1 (IRQ masked), F = 1 (FIQ masked), T = 0 (32-bit ARM ISA), and M[4:0] = 0x13 (Supervisor mode).

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!

fig_m02_l01_b.svg — Exact 32-bit bitfields for CPSR (top) and CP15 SCTLR…

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
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 lr

Modeling 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
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;    }
  1. Classify all nine legal Cortex-A7 M[4:0] encodings and distinguish privileged SPSR-bearing exception modes from USR/SYS.
  2. Map FIQ r8..r14 and exception r13..r14 to distinct physical storage slots while keeping r0..r7 shared across every mode.
  3. 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-l01
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-l01
strace — learner@ksa: ~/gl-e01/m02-l01
learner@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])PrivilegePrivate Banked RegistersSPSR PresentPrimary Bare-Metal / OS Role
User (0x10)PL0 (unpriv)None (shares r0..r14 with SYS)NoUnprivileged user-space application execution
System (0x1F)PL1 (priv)Shares USR r0..r14 bankNoPrivileged OS helpers accessing User SP/LR without SPSR risk
Supervisor (0x13) / IRQ (0x12)PL1 (priv)r13 (SP), r14 (LR) per modeYes (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.

quiz · 1 of 3

An IRQ (0x12) assembly handler overwrites r0 without pushing it first. Why does this corrupt the interrupted User (0x10) program upon return?

quiz · 2 of 3

Which masked update switches CPSR mode bits M[4:0] held in r0 to Supervisor (0x13) without disturbing NZCV flags or I/F/T control bits?

quiz · 3 of 3

Retrieval: during an ordinary BL subroutine call in Supervisor mode, what protects a caller's value in r4?

Key takeaways

  • Cortex-A7 provides nine modes (User at PL0; FIQ, IRQ, Supervisor, Abort, Undefined, System, and Secure Monitor at PL1; Hyp at PL2) encoded in CPSR.M[4:0].
  • r0..r7 are physically shared across every mode (FIQ privately banks r8..r14, other exception modes bank SP and LR, and System shares User's r0..r14).
  • CPSR holds live NZCV flags, A/I/F masks, T state, and M[4:0]; exception modes save interrupted CPSR into SPSR_<mode>, whereas User and System have no SPSR.
  • Privileged CP15 registers like SCTLR (c1, c0, 0) are read via MRC p15 and 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.
Progress is saved on this device.

search — fzf

0/0 ───────────────────────────────────────────

keyboard shortcuts — less

/
search
f
focus mode
[
previous
]
next
?
help
Esc
close

figure