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

14.2 Speculation and the memory system

Speculation reaches the memory system in two ways. The first is the side channel of 14.1: transient loads change cache state. The second matters more in bring-up. Speculative and prefetch accesses are real transactions on the interconnect. They can hit a PA hole, a DRAM controller that is not trained yet, or a TZASC-protected region. The error responses that come back surface as SErrors or hangs, and no instruction caused them. This section covers both, along with the speculative-walk errata that make TF-A and KVM save SCTLR and TCR in unusual places.

Speculative cache fills

For any location mapped as Normal Cacheable and readable in the current regime, the architecture allows the PE to read it speculatively and allocate it into a cache at any time (6.3). Three consequences follow.

  • Side channel. A transient load leaves a resident line, and a transient dependent load leaves a line whose address encodes data. This is the covert channel used by every variant in this part.
  • Coherency hazards. A line can come back into a cache after you invalidated it, even if no instruction touched it, as long as the VA mapping stays Normal Cacheable. This is why the DMA receive recipe invalidates after the transfer (8.7), and why mismatched aliases are dangerous (6.6).
  • Reach. The PE can only speculate into what the current translation regime maps. A mapping that exists only in another regime, or a PA the tables do not map at all, is unreachable for data speculation. “Not mapped” is the only complete defence against speculative data access. Permission bits do not help on cores that have the Meltdown problem (14.6).

Device memory and the MMU-off defaults

Speculative data accesses to any Device memory type (nGnRnE through GRE) are not permitted. This is architectural, and it is why MMIO is safe as long as it is mapped Device. Two caveats apply.

  1. Instruction fetch is not covered. The PE may speculatively fetch instructions from any location that is not execute-never, Device included. Every Device mapping must therefore be UXN/PXN (or XN in the EL2 and EL3 regimes). A speculative fetch from a read-sensitive register, such as a FIFO or a clear-on-read status register, is a real read with side effects (6.2).
  2. With stage 1 disabled, data and instruction accesses get different defaults (6.5). Data accesses are Device-nGnRnE, so there is no speculative data access at all. Instruction fetches are Normal, with cacheability from SCTLR_ELx.I, and no XN exists to stop them.

How far can MMU-off instruction fetch reach?

The Arm ARM bounds instruction fetch when all associated stages of translation for the regime are disabled (see “Behavior of instruction fetches when all associated stages of translation are disabled”). As quoted in core errata notices (Cortex-A53 843819, Falkor E1041), fetches are limited to the same or the next translation granule (4KB) as an instruction that has been or will be executed. The Arm ARM text also lists a small number of further allowances tied to branch targets on the program’s actual path; read the exact list there rather than relying on this paraphrase. What it does not permit is fetching from an arbitrary PA, for example a stale predicted target left over from MMU-on execution. That rule has two sharp edges:

  • The next 4KB counts. Code that ends near the top of an SRAM, with a PA hole or an unclocked block directly above it, can generate sequential prefetches into that hole. Leave at least a 4KB pad, or place code so that the following granule is backed by real memory.
  • Errata break it. Some cores have published errata where MMU-off fetches escape the window, especially around the transition from SCTLR_ELx.M=1 to 0 or when HCR_EL2.VM is set. Two examples are Linux’s QCOM_FALKOR_ERRATUM_E1041, worked around with an ISB before clearing M, and Cortex-A53 erratum 843819. Treat the 4KB window as the architectural bound and the errata list in the core’s SDEN as the reason not to rely on it (1.3).

Once the MMU is on, the bound is the mapping. Any Normal mapping can be read speculatively, and any Normal mapping that is not XN can be fetched speculatively (9.3).

Prefetchers

Hardware data and instruction prefetchers are IMPLEMENTATION DEFINED. The architecture treats a prefetch as a speculative access, so it is permitted only where speculative reads are permitted: Normal memory that the current regime maps. In practice, prefetchers work on physical addresses and usually stop at a page boundary, because they do not have the next page’s translation. Some cores also cross the boundary using TLB information, so check the TRM. Many cores expose control bits in IMPLEMENTATION DEFINED registers (CPUECTLR_EL1 and similar) to tune or disable prefetching. Interconnects add their own system-level-cache prefetchers.

The architecture also reserves prefetch hints (PRFM, PRFUM). These never fault, and a translation fault on a PRFM is suppressed. On some cores a PRFM to a mapped but broken PA still issues a real transaction, so a stray PRFM in memcpy can reach whatever the mapping allows.

Where the stray transactions go, and how they surface

TargetTypical responseHow it shows up on the PE
Unmapped hole in the interconnect address mapDECERR or SLVERR from a default slaveAsynchronous SError, or a RAS record in the cluster or interconnect (16.7)
DRAM controller before training, or a channel not yet initialisedNo response, or an error response, depending on the controllerCore hang (outstanding transaction never completes), or SError
TZASC or TZC-protected carve-out accessed with NS=1Error response or RAZ/WI, per the TZC ACTION configuration, often with an interruptSError, or silent zeros in the cache that later look like data corruption
RME: a PA whose GPT entry does not allow the access PASGranule protection check, which applies to speculative accesses tooA failing speculative access is not made, and no fault is reported
Powered-down or unclocked IPBus hangCore or cluster hang, often with the debugger unable to halt

How a PE reports an error response to a speculative or prefetch access is IMPLEMENTATION DEFINED. It may discard the response, poison the line, log it in a RAS error record, or raise an SError interrupt. A synchronous External abort is only taken for an access that is architecturally executed. For speculative ones, the common outcome on Arm cores is an SError whose ESR_ELx gives only an IMPLEMENTATION DEFINED or uncategorised syndrome, and whose ELR_ELx points at unrelated code (3.4). The fingerprint is an SError that moves when you add or remove unrelated code, because that changes the predicted paths and the prefetch streams.

Prevention

  1. Map only what exists and is ready. Build the address map from the SoC memory map, not from “1GB blocks covering DRAM”. A hole inside a block mapping is a speculative target.
  2. Holes: no descriptor at all. Marking a hole XN stops instruction fetch but not speculative data reads of a Normal mapping. Marking it Device stops data speculation but still reaches the bus for architectural stray accesses. Invalid descriptors stop both.
  3. MMIO: Device and XN. TF-A’s xlat library applies execute-never to every MT_DEVICE region. Hand-built tables need the same.
  4. DRAM: Normal only after training. Until the DDR PHY and controller are trained, either leave DRAM unmapped or map it Device-nGnRnE for the training code’s own accesses. Switching to Normal Cacheable means a remap with break-before-make (9.2).
  5. Prefetchers: enable last. If the core or interconnect prefetchers are programmable, keep them disabled, or at their conservative reset setting, until every Normal mapping is backed by working memory. This is an IMPLEMENTATION DEFINED step. Find it in the TRM and the SoC integration guide.
  6. Secure carve-outs: not in the NS linear map. Device-tree reserved-memory with no-map keeps Linux from mapping them (6.6). A TZASC that answers NS accesses with RAZ/WI hides the bug instead of reporting it, so prefer an error response during bring-up.

Bring-up note: A multi-chiplet system often has PA windows that point at a remote die whose links are not trained yet. Map remote-die memory only after the link-up handshake (17.5). Otherwise a prefetch or a speculative read of a Normal mapping to the remote window can hang the local interconnect, and the debugger will not tell you why.

Speculative walks and the speculative-AT errata

Translation table walks may be speculative, and a speculative walk may allocate TLB entries (9.1). Architecturally, any such entry must be one that the regime’s configuration could have produced. Several cores violate this during a context switch of the EL1&0 regime done from a higher EL:

CoreErratumTF-A build flag
Cortex-A531530924ERRATA_A53_1530924
Cortex-A551530923ERRATA_A55_1530923
Cortex-A571319537ERRATA_A57_1319537
Cortex-A721319367ERRATA_A72_1319367
Cortex-A761165522ERRATA_A76_1165522

While EL3 or EL2 restores a new world’s SCTLR_EL1, TCR_EL1, TTBRn_EL1 and VTTBR_EL2 one register at a time, a speculative AT or a speculative EL1&0 walk can combine old and new values. It can then allocate a TLB entry, tagged with the new ASID and VMID, that corresponds to neither world. The fix is to make an EL1&0 stage-1 walk impossible while the registers are inconsistent:

  • Set TCR_EL1.EPD0 and EPD1, so a TTBR walk faults instead of reading memory.
  • Keep SCTLR_EL1.M=1, because with M=0 the regime would produce a flat mapping that allocates without any walk.
  • Restore SCTLR_EL1 first and TCR_EL1 last, each followed by an ISB.

TF-A implements this as apply_at_speculative_wa on every lower-EL vector entry and restore_ptw_el1_sys_regs in el3_exit, enabled by ERRATA_SPECULATIVE_AT whenever any of the flags above is set:

// lib/el3_runtime/aarch64/context.S (abridged): on entry to EL3
    mrs   x29, tcr_el1
    str   x29, [sp, #(CTX_ERRATA_SPEC_AT_OFFSET + CTX_ERRATA_SPEC_AT_TCR_EL1)]
    orr   x29, x29, #(TCR_EPD0_BIT | TCR_EPD1_BIT)
    msr   tcr_el1, x29
    isb
    mrs   x29, sctlr_el1
    orr   x29, x29, #SCTLR_M_BIT
    msr   sctlr_el1, x29
    isb
// ... on exit, after all other EL1 registers are restored:
//     isb; msr sctlr_el1, <saved>; isb; msr tcr_el1, <saved>

KVM does the same in nVHE mode under ARM64_WORKAROUND_SPECULATIVE_AT. It writes the guest’s TCR_EL1 with EPD0|EPD1 set until stage 2 is configured, and it restores the host’s SCTLR_EL1, then ISB, then TCR_EL1. It also sets the EPD bits around the guest-VMID TLB invalidation in __tlb_switch_to_guest(). The TF-A build documentation notes that if EL3 enables the workaround, EL2 software must implement it too.

TF-A’s speculation barrier points

LocationBarrierWhy
exception_return macro (every EL3 ERET)SB if ENABLE_FEAT_SB, else DSB NSH; ISBStraight-line speculation past ERET into whatever bytes follow
BHB loop on lower-EL vector entryspeculation_barrier (SB, or DSB SY; ISB) after the loopThe loop must finish overwriting history before any indirect branch (14.4)
Lower-EL vector entries on speculative-AT coresapply_at_speculative_wa (EPD and M with ISB)Prevent inconsistent EL1&0 walks during the world switch
WA1 vectors on Cortex-A57/A72SCTLR_EL3.M toggle with ISBCore-specific BTB invalidation on pre-CSV2 cores (14.4)
Compiled C, when HARDEN_SLS=1-mharden-sls=all: speculation barriers after RET and BR, BLR via thunksSLS in C code, not only in assembly

ENABLE_FEAT_SB defaults to on for platforms built for v8.5 or later, where FEAT_SB is mandatory. On earlier cores the fallback costs a full DSB on every exception return.

Validation note: To validate the “map only what exists” rule, run a directed test with an intentionally bad mapping. Map a 2MB Normal executable block that covers a known PA hole, run a tight loop with a predictable indirect branch near the end of the valid range, and count SErrors (in a RAS record or in ISR_EL1.A) over a long run. Repeat with the hole unmapped. The pass criterion is zero SErrors with the correct map and a non-zero count with the bad one. A zero count on both shows that the test cannot see the problem, not that the core is safe. Look at the interconnect’s error logs and the TZC FAIL_ADDRESS registers, or their equivalents, to confirm which PA was hit. This needs RTL with a realistic interconnect model, or silicon: FVP does not speculate.

Checkpoint

Q14.2.1 BL2 maps all of DRAM as one Normal Cacheable, executable 4GB region before the DDR controller is trained. It then calls the DDR training code, which runs from SRAM. Occasionally the system hangs during training. What is the most plausible mechanism, and the fix?

  • A. The training code’s stack is in DRAM. Move the stack to SRAM.
  • B. Speculative reads, instruction fetches or prefetches to the Normal DRAM mapping reach an untrained controller that never responds. Leave DRAM unmapped (or Device for the training accesses) until training completes, then remap as Normal with break-before-make.
  • C. The DRAM mapping lacks PXN. Setting PXN on it fixes the hang.
  • D. The training code needs a DSB SY after each DRAM register write.
Answer

B. Any Normal mapping is a legitimate target for speculative data reads and prefetches, and any Normal executable mapping is a target for speculative fetch, whether or not code touches it. An untrained controller may not answer, so the outstanding transaction hangs the core. C is the tempting wrong answer: PXN stops instruction fetch but not speculative data reads or prefetch from a Normal mapping. A would fail deterministically, not occasionally.

Read more: Arm ARM (DDI0487), speculative accesses to Normal memory; Memory types and attributes

Q14.2.2 On a core with erratum 1319367, EL3 switches from Secure to Non-secure EL1 state. Why does TF-A restore TCR_EL1 after SCTLR_EL1, with an ISB between them?

  • A. SCTLR_EL1 writes are ignored while TCR_EL1.EPDx is set.
  • B. The architecture requires TCR_EL1 to be written last in any context switch.
  • C. The ISB makes the TLB invalidation complete.
  • D. While TCR_EL1.EPD0/1=1 and SCTLR_EL1.M=1, no EL1&0 stage-1 walk can occur. Restoring TCR_EL1 last, after a context synchronization event makes every other EL1 register take effect, ensures that the first possible speculative walk sees a fully consistent new context.
Answer

D. The workaround keeps EL1&0 stage-1 walks impossible until all other EL1 registers have been restored and synchronized. Re-enabling walks by clearing the EPD bits through the TCR_EL1 write is the last step. M must stay 1 throughout, because M=0 would give a flat mapping that can allocate without a walk. B is wrong because there is no general architectural rule about write order. The order only matters because of the erratum.

Read more: TF-A docs, build options, ERRATA_SPECULATIVE_AT; Arm Cortex-A72 Software Developer Errata Notice, erratum 1319367

Q14.2.3 BL1 runs from ROM at 0x0000_0000–0x0000_FFFF with the MMU off. The address range 0x0001_0000–0x0001_FFFF is a hole that returns DECERR. BL1’s last function, which is executed, ends 64 bytes below 0x0001_0000. Which statement is correct?

  • A. The Arm ARM allows speculative instruction fetch from the next 4KB granule after sequentially fetched code, so a sequential prefetch into the hole is permitted and can produce an SError. Pad the ROM image, or put code elsewhere.
  • B. With the MMU off, instruction fetches are Device-nGnRnE, so no speculative fetch can occur.
  • C. The hole is safe because speculative instruction fetches never cross a 64KB boundary.
  • D. Only a mispredicted branch could reach the hole, and branch prediction is disabled when the MMU is off.
Answer

A. With all stages disabled, instruction fetches are Normal, and fetches may reach the same or the next 4KB granule as an instruction that has been or will be executed. The hole sits in that window. B confuses the data default (Device-nGnRnE) with the instruction default (Normal). D is wrong because branch prediction is not architecturally disabled when the MMU is off, and the sequential case alone is enough.

Read more: Arm ARM (DDI0487), Behavior of instruction fetches when all associated stages of translation are disabled