Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

9.3 Turning the MMU and caches on

Enabling the MMU is one write to SCTLR_ELx, but that write switches how the next instruction is fetched and how every data access is typed, and it starts a walker that reads memory written while the caches were off. Most “dead after SCTLR.M=1” failures (18.3) come down to a short list of rules in this section.

What reset gives you

  • SCTLR_ELx.{M,C,I}. These reset to 0 at the highest EL on a cold reset, and many other fields are UNKNOWN. Write the whole register early, not just the bits you care about (17.1, 17.2).
  • Caches. Whether reset invalidates the caches is IMPLEMENTATION DEFINED. Arm’s Armv8-A cores invalidate their caches in hardware at reset, so firmware for them does no set/way invalidation at cold boot (some Armv7 cores did need it). Read the TRM of your core, and of any system cache, rather than assuming. If software invalidation is needed, it is a set/way loop over the local levels only, before any cacheable access (8.8).
  • TLBs. Hardware invalidation at reset is also implementation-specific. Even when it happens, a later boot stage inherits TLB entries from an earlier stage that ran with the MMU on, or from before a warm reset. Always issue a local TLBI ALLE3 (or ALLE2/VMALLE1) before enabling. It is cheap.
  • Memory types with the MMU off. Data accesses are Device-nGnRnE, and instruction fetches are Normal (cacheable if SCTLR.I=1). This is why early C code faults on unaligned accesses and DC ZVA (6.5).

Register write order, and the two ISBs

    // Tables already built and visible; x0=MAIR, x1=TCR, x2=TTBR0
    tlbi    alle3
    msr     mair_el3, x0
    msr     tcr_el3, x1
    msr     ttbr0_el3, x2
    dsb     ish                 // table stores and the TLBI complete
    isb                         // MAIR/TCR/TTBR visible to the MMU
    mrs     x3, sctlr_el3
    orr     x3, x3, #(1 << 0)   // M
    orr     x3, x3, #(1 << 2)   // C
    orr     x3, x3, #(1 << 12)  // I
    msr     sctlr_el3, x3
    isb                         // later instructions fetched and executed translated

MAIR, TCR and TTBR are not ordered among themselves. The MMU reads them indirectly, and an indirect read is guaranteed to see a direct write only after a context synchronization event (7.8). Because one ISB makes all three visible together, the order in which they are written does not matter. What matters is that the ISB comes between them and the SCTLR write.

ISB before SCTLR.M. The write to SCTLR is a direct write to a different register, and it is not ordered with the earlier TCR/TTBR writes as the MMU sees them. Without the ISB, the PE may start translating with the old (reset, UNKNOWN) TCR or TTBR values. The DSB ISH before it makes sure the walker can see the translation tables and that the TLBI has completed, because a TLBI is complete only after a DSB.

ISB after SCTLR.M. Instructions after the MSR may already have been fetched, and possibly executed speculatively, under the old context. After the ISB every instruction is fetched through the new translation and the new memory types. Without it, a few instructions run in an undefined mixture of contexts. That is harmless only by luck.

Compare TF-A’s enable_mmu_direct_el3 (in lib/xlat_tables_v2/aarch64/enable_mmu.S), the worked example for this section. It asserts that SCTLR.M is still 0, issues TLBI ALLE3, loads MAIR, TCR and TTBR0 from mmu_cfg_params, and then runs dsb ish; isb. Next it sets SCTLR.{WXN,C,M}, clearing C again if the caller passed DISABLE_DCACHE, and finishes with isb. The I-cache is enabled separately, earlier, in el3_entrypoint_common. Because the TLBI is local, any DSB domain would complete it. ISH is there for the table stores, which matters if another PE built or will share the tables.

Identity-mapping the trampoline

The instruction after the second ISB is fetched at PC+4 as a VA. So the code that enables the MMU must be mapped at VA == PA, or at least the page that contains it must be. At EL3, TF-A simply maps all of its images flat, so the issue never arises. If you want the image to run at a different VA:

  1. Map the enable routine’s page both at its PA (the identity, or “idmap”) and at the target VA.
  2. Enable the MMU in the idmap page, then branch absolutely to the target VA (ldr x0, =target; br x0).
  3. Optionally remove the idmap afterwards, with the 9.1 sequence.

Linux does exactly this with .idmap.text, using TTBR0 for the idmap and TTBR1 for the kernel. At EL3 and in non-VHE EL2 there is only TTBR0, so both mappings must live in one table. Keep the stack and the exception vectors (VBAR_ELx) mapped across the switch as well. Recursive exceptions at VBAR immediately after enabling (18.3) usually mean the vector table is not mapped, or is mapped XN.

The classic failures

SymptomCauseCheck
Level 0 (or starting-level) Translation fault on the first fetch after M=1Wrong T0SZ/TG0, so the walk starts at a different level from the one the tables were built for, or TTBR0 points to the wrong pageRemember that the TG0 and TG1 encodings differ (TG0: 00 = 4K, 01 = 64K, 10 = 16K; TG1: 01 = 16K, 10 = 4K, 11 = 64K), and recompute the starting level (5.3)
Address size faultTCR.PS/IPS smaller than the table or output addressesID_AA64MMFR0_EL1.PARange vs PS
Garbage walks or random faults with the caches on, correct on FVPTables written with the MMU off (Non-cacheable stores) while stale lines for the same addresses, left by an earlier stage or a previous run, sit in the cache. The walk, which is cacheable per TCR.IRGN0/ORGN0, hits the stale lineInvalidate the table region to the PoC (DC IVAC) before building it, so that no stale dirty line is evicted over the new entries, and again after, to drop any line allocated (speculatively, say) in between. Alternatively the previous stage cleans and invalidates the region before handing over. Linux’s head.S invalidates the page tables it built with the MMU off
Data written before enabling the cache “disappears” afterwardsThe same stale-line effect, for data (6.6)Invalidate (not clean) the region by VA to the PoC (DC IVAC) before enabling the cache. A clean would write a stale dirty line back over the new data
Works with a debugger single-stepping, fails at speedA missing ISB, before or after the SCTLR writeRead the sequence above against the disassembly. A compiler-generated MSR in C needs explicit isb()
Permission fault on the first fetchSCTLR.WXN=1 with writable code pages, or code mapped XNBefore enabling, decode the leaf descriptors by hand: with SCTLR.M=0 an AT instruction reports the flat MMU-off mapping and does not walk your tables. After enabling (from a mapped page), AT S1E3R and PAR_EL1 show the translation, though AT does not check execute permission (5.9)

The stale-line case is the one FVP hides, because cache state is not modelled by default (18.4). Turn on cache modelling, or test on RTL or silicon, before you trust a boot path that writes tables with the MMU off.

Bring-up note: Once the MMU is on, the PE may speculatively fetch and prefetch from any address mapped as Normal and executable, and may speculatively read or prefetch anything mapped as Normal. These accesses are not limited to what the code actually touches. A large flat block (say 1 GB of “DRAM”) that also covers a PA hole, untrained DDR, a Secure-only carve-out or an unclocked memory can produce asynchronous SErrors or bus hangs with no instruction to blame. Map only what exists and is ready. Map MMIO as Device, which data accesses never read speculatively, and XN, so that instruction fetch cannot touch it either. Leave holes unmapped, and remap DDR as Normal only after training. The MMU-off case, where instruction fetch is Normal everywhere, and the speculation details are covered in 14.2.

Validation note: A minimal MMU-on directed test: build tables with the MMU off, fill the table region’s cache lines with a poison pattern beforehand (cacheable alias, no clean), then enable. A correct boot path survives, and one that omits the invalidate faults or walks garbage. Check the outcome with PAR_EL1 from AT S1E3R on a few VAs, and with DTLB_WALK (0x34) counts, which show whether the walks happen at all. Run it on silicon or with FVP cache modelling on.

Turning it off again

Disabling is the reverse transition, and it reintroduces the stale-line problem in the other direction: the next stage, running with caches off, must find its data at the PoC, and no new dirty lines may be created after the last clean. The usual order is to clear M and C together, ISB, and then clean and invalidate to the PoC the data the next stage relies on (a clean done before clearing C is undone by any later cacheable write, including stack pushes). The code doing it must be identity-mapped. Power-down ordering and set/way usage are covered once, in 8.8.

Checkpoint

Q9.3.1 An engineer removes the isb between msr ttbr0_el3, x2 and the msr sctlr_el3 that sets M, reasoning that “MSRs to system registers execute in order”. Why is that reasoning wrong?

  • A. MSR instructions can be reordered by the assembler unless separated by an ISB
  • B. The ISB is needed to complete the earlier TLBI ALLE3
  • C. The MMU reads TCR and TTBR indirectly, and those reads are guaranteed to see the new values only after a context synchronization event, so translation could start with the old values
  • D. The ISB makes the translation table stores visible to the walker
Answer

C. Program order of direct writes says nothing about when the walker, an indirect reader, sees them. An ISB is the context synchronization event that guarantees it. Option B is tempting, but TLBI completion and table-store visibility (D) come from the DSB, not the ISB. The sequence needs both.

Read more: Arm ARM (DDI0487), Synchronization requirements for AArch64 System registers; context synchronization event

Q9.3.2 BL2 builds its translation tables with the MMU and D-cache off, then enables both. It works on FVP but walks garbage on silicon after a warm reset. Which fix addresses the root cause?

  • A. Add an extra ISB after setting SCTLR.M
  • B. Invalidate the table region to the PoC by VA before and after building the tables, so that cacheable walks cannot hit stale lines left from before the reset
  • C. Mark the walks Inner Shareable in TCR.SH0
  • D. Replace TLBI ALLE3 with TLBI ALLE3IS
Answer

B. The stores went to memory (Non-cacheable with the MMU off), but older cache lines for the same addresses survived the warm reset, and the cacheable walk hits them. FVP does not model cache contents by default, which is why it passed. The invalidate before building stops a stale dirty line from being evicted over the new entries. The TLB is not the problem, so D does nothing, and an ISB orders context, not data.

Read more: Arm ARM (DDI0487), Behavior of caches at reset; translation table walk cacheability and TCR_ELx.IRGN0/ORGN0

Q9.3.3 Firmware maps 0x8000_0000–0xFFFF_FFFF as one 2 GB range of Normal Write-Back, executable memory. Only the first 1 GB is DRAM, and the rest is a PA hole. Shortly after the MMU is turned on, an SError is taken and no faulting instruction can be found. What is the most likely cause?

  • A. Speculative instruction fetch or prefetch into the mapped hole, which returns a bus error reported asynchronously
  • B. The ISB after SCTLR.M was missing
  • C. A TLB conflict abort between two block entries
  • D. A missing DSB before the SCTLR write
Answer

A. Normal executable mappings permit speculative accesses that the program never makes. A hole that returns an error response produces an SError with no precise instruction. Map only real memory, and use Device/XN for MMIO. A TLB conflict abort is synchronous and has its own fault status, and missing barriers give synchronous translation symptoms, not an SError.

Read more: Arm ARM (DDI0487), Normal memory and speculative accesses; see also 14.2