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

6.6 Mismatched attributes and aliases

A physical location is accessed with mismatched attributes when two accesses to it differ in memory type, cacheability or shareability. This can happen through two VAs (an alias), through two translation regimes, or through two observers. Bring-up code creates such mismatches all the time, mostly without noticing:

  • the MMU-off to MMU-on transition, in which the same PA is accessed first as Device-nGnRnE and then as Normal WB;
  • SCTLR_ELx.C=0 phases, in which data accesses to Normal memory are Non-cacheable;
  • a non-coherent DMA master or a debugger writing memory that a PE then reads through its caches.

This section covers what DDI0487 says about mismatches, what it does not promise, the rules Linux follows, and the maintenance each bring-up transition needs. The maintenance instructions themselves are in 8.5, and power transitions are treated in depth in 8.8.

Aliases that are not mismatches

Two VAs that map the same PA with identical attributes are harmless for data. AArch64 data and unified caches behave as PIPT, so both VAs hit the same line, and coherence and ordering hold exactly as for one VA.

One caveat remains, and it does not concern attributes: the instruction cache may be VIPT, so instruction-side aliases need care (8.1).

What the architecture says about mismatches

DDI0487’s section “Mismatched memory attributes” takes a damage-limitation approach. It does not make mismatched aliases UNPREDICTABLE. Instead, it lists what software may lose. The losses fall into three groups:

  • Uniprocessor semantics. A read through alias B, after a write through alias A by the same PE, may not return the value written. For example, the write sits in a cache line that the NC read does not look up.
  • Coherency. Different observers may disagree about the value of the location. For example, one sees DRAM and another sees a dirty line.
  • Memory-type guarantees. Properties promised by one alias’s type may not hold when another alias has a weaker type. A Normal alias of a Device location permits speculative reads of the device and merged writes to it, which defeats the Device alias’s “no speculation” and nG guarantees.

DDI0487 also gives a recovery guarantee for the common case. If the mismatch is only in cacheability (and shareability) for Normal memory, software can restore coherence at defined handover points with cache maintenance by VA to the Point of Coherency, completed by DSB:

  • Before accessing through the less-cached alias (or before another agent reads memory), clean the location’s lines through the cacheable alias, so that no dirty data stays hidden in a cache.
  • Before accessing through the cacheable alias after memory was updated another way, invalidate the location’s lines, so that no stale line hides the new data.

Perform the maintenance through a mapping whose shareability covers every PE that may hold the line. Maintenance by VA acts within the location’s shareability domain (6.4).

Several things are not guaranteed:

  • Any steady state. A cacheable alias that stays mapped can have lines allocated speculatively at any moment after your invalidate (6.3). Maintenance gives guarantees at a handover point, provided nothing accesses the location through the other alias until the next handover.
  • Recovery for Device/Normal mismatches. No maintenance sequence makes a Normal alias of a read-sensitive device safe, because the damage (a speculative read with a side effect) has already happened.
  • That a Non-cacheable access does or does not look up the cache. Whether an NC access can hit a line allocated through a cacheable alias is not something to rely on in either direction.
MismatchWhat can go wrongRemedy
Normal WB vs Normal NC (same PE or different PEs)Stale or lost data in either directionClean (or clean+invalidate) to PoC before the NC/external side reads; invalidate before the WB side reads
Normal WB ISH vs WB NSHCoherency with other PEs not guaranteed. Maintenance by VA through the NSH alias may stay localDo not do this. Map all shared memory with one shareability
Normal (any) vs DeviceSpeculative or merged access to the device, which may have side effects. Device ordering guarantees lostDo not do this. Keep devices out of every Normal mapping
Device type A vs Device type B (for example nGnRnE vs nGnRE)The stronger alias’s G/R/E guarantees may not holdUse one Device type per peripheral
Tagged vs Untagged NormalTag checks and tag storage behaviour differ per aliasSee 13.6
Stage 1 vs stage 2 combined attributesGuest NC vs host WB is a mismatch at the PAFEAT_S2FWB, or host maintenance (11.3)

The rules Linux follows

Linux is a useful reference because it has to live with aliases. The kernel’s linear map covers all RAM as Normal WB Inner Shareable. Its rules:

  1. Never map RAM as Device. On arm64, ioremap_prot() refuses any PFN that is in the linear map, with a WARN_ON(pfn_is_map_memory(...)) and a NULL return. memremap(..., MEMREMAP_WB) of RAM returns the linear-map address instead of creating a second mapping.
  2. Keep memory that must not be accessed out of the linear map. Device-tree reserved-memory nodes with no-map (for example Secure or firmware carve-outs) are never mapped, so speculative accesses cannot reach them. Without no-map, a TZASC-protected region is in the linear map, and speculation can generate errors from it (14.2).
  3. Tolerate exactly one kind of mismatch: an NC alias with a cacheable linear alias, for non-coherent DMA. dma_alloc_coherent() for a non-coherent device cleans the buffer to the PoC (arch_dma_prep_coherent()), then maps it a second time as Normal NC and XN (pgprot_dmacoherent()). The linear alias stays cacheable but is never accessed. This relies on the handover rule above: any line speculatively allocated through the linear alias is clean, and software never reads through that alias. For streaming DMA, Linux avoids the second alias and uses maintenance on the cacheable mapping instead (8.7).
  4. Leave virtualised guests to FEAT_S2FWB. When FEAT_S2FWB is present, KVM uses stage 2 to force guest RAM to Normal WB, whatever the guest’s stage 1 attributes say. Otherwise it performs maintenance when it maps pages for the guest (11.3).

TF-A follows the same spirit with simpler means. Each region in the xlat tables has a single memory type. Device regions are always XN. Caches are flushed at the points where the MMU or cache state changes, as the case study below shows.

Bring-up case study

Case A: data written with SCTLR.C=0, read after enabling caches

Suppose BL2 (or a ROM) writes a parameter block or a loaded image while it runs with the MMU off (data accesses Device-nGnRnE) or with C=0 (Normal NC). The writes go towards memory. After BL31 enables its MMU and caches, it reads the same PA through a Normal WB mapping. Two things can be in the caches already:

  • A stale clean line for that PA. It may come from an earlier boot stage that ran with caches on, from a coherent PE, or from a system cache that keeps its contents across the boot stages. The read hits it and returns old data.
  • A stale dirty line. It is evicted later and overwrites the new data in memory. TF-A’s entry code names this exact hazard: “possible corruption of this memory by dirty cache lines in a system cache as a result of use by an earlier boot loader stage”.

Whether caches are invalidated by hardware at reset is IMPLEMENTATION DEFINED (9.3, 17.1). Even when they are, a system cache shared across stages is not reset with the PE.

The remedy is to invalidate the region by VA to the PoC, with caches off, after the last non-cacheable write and before the first cacheable access. TF-A’s BL2 and BL31 entry code does this for their own RW regions, before zeroing .bss:

    // Invalidate own RW image range before using it (caches still off)
    adr     x0, __RW_START__
    adr     x1, __RW_END__
    sub     x1, x1, x0
    bl      inv_dcache_range        // DC IVAC per line, then DSB SY

The reverse transition is just as common. Data written with caches on, and then read by a party with caches off, needs a clean to the PoC first. Examples are the next boot stage with its MMU off, a PE in warm boot, or a non-coherent master. TF-A’s image loader does flush_dcache_range() (clean+invalidate to PoC) on every loaded image, “so that it can be executed later by any CPU, regardless of cache and MMU state”. The power-down form of the same rule is in 8.8.

Case B: an image loaded by a non-coherent master, then executed

Suppose a DMA engine copies BL33 from flash into DRAM, or a debugger writes it through a MEM-AP. Neither agent snoops the PE caches. The PE then verifies the image and branches to it. There are four hazards:

  1. Dirty D-cache lines covering the destination, for example from its previous use as a heap or a buffer. They are evicted after the DMA and overwrite parts of the image.
  2. Stale clean D-cache lines. The PE’s verification reads (hash, signature) hit them and check old data. The data the PE checks is then not the data it executes.
  3. Stale lines in the I-cache, or in a unified L2/L3, which the instruction fetch hits.
  4. Lines allocated speculatively during the transfer through the cacheable mapping, for example by a prefetcher running ahead of the PE’s own accesses to adjacent memory.

The sequence that handles all four:

    // x0 = dest, x1 = size; caches and MMU on, dest mapped Normal WB ISH
    bl      flush_dcache_range      // 1. DC CIVAC to PoC: no dirty lines can be written back later
                                    //    (ends with DSB SY)
    // ... program DMA, start, wait for completion (poll a Device status reg) ...
    dsb     sy                      //    the completion read is complete before the maintenance
    bl      inv_dcache_range        // 2. DC IVAC to PoC: drop lines allocated during the transfer
                                    //    (ends with DSB SY)
    // ... verify image (reads now come from memory) ...
    ic      iallu                   // 3. invalidate the I-cache (IC IALLUIS if other PEs will run it)
    dsb     sy
    isb                             // 4. refetch the code that follows under the new I-cache state
    br      x0

Why both steps 1 and 2?

  • Step 1 removes dirty lines. It must happen before the DMA writes, because cleaning a dirty line afterwards would write it over the new data.
  • Step 2 removes clean lines that may have arrived during the transfer.

Using only “invalidate after” fails in two ways. A dirty line may be evicted during the transfer and overwrite the new data. And in some configurations DC IVAC is performed as a clean and invalidate (for example at EL1 with stage 2 enabled; see the DC IVAC description in DDI0487), which would write the dirty line back over the image. Using only “clean before” leaves the stale-hit hazard open. This is the general rule of 8.7.

Do not rely on CTR_EL0.IDC/DIC to skip step 3 for code written by a non-coherent agent. This is a conservative reading rather than a quoted rule. The bits are defined in terms of data-to-instruction coherence for this PE’s caches, and the architecture does not clearly extend them to memory changed behind the caches by a non-coherent agent. Treat step 3 as mandatory here (8.6).

Debuggers fall into two cases:

  • Through the PE. A debugger that loads through the PE (the PE halted in Debug state, executing stores) uses the PE’s data-side caches. It is coherent for data, but it still needs the I-cache step.
  • Through a MEM-AP. A debugger that writes through a MEM-AP is non-coherent, unless the AP is set up to issue cacheable, shareable transactions. The AXI-AP’s CSW has cache and domain fields for this, and their support is IMPLEMENTATION DEFINED.

Many debugger “download” commands perform the maintenance for you. Check that yours does before blaming the image.

Summary of transitions

TransitionMaintenance (by VA, to PoC unless stated)Hazard it removes
Caches on → caches off (same PE, or next stage starts with MMU off)Clean+invalidate the data that will be read, then DSBDirty data hidden from NC/Device reads
Caches off writes → caches on readsInvalidate after the writes and before enabling (or before the first cacheable read), then DSBStale clean or dirty lines from earlier stages or system caches
PE writes → non-coherent master readsClean, then DSB, then start the masterData not yet at the PoC
Non-coherent master writes → PE readsClean+invalidate before the transfer, invalidate after it, DSBDirty-line write-back over new data; stale hits
Any of the above, followed by executing the dataPlus IC IALLU/IALLUIS (or IC IVAU), DSB, ISBStale instructions

Security note: Case B hazard 2 is a time-of-check/time-of-use bug in secure boot. If the verifier hashes the image through cache lines that no longer match memory, it authenticates bytes that will not be executed. Always invalidate after the load and before measuring. Then measure and execute through the same mapping, with no writer outside the verifier between the two.

Validation note: Do not wait for a bad image to expose these hazards. Provoke them in a directed test.

  1. Plant dirty lines. Fill the destination through a WB mapping with a marker pattern, so it is in the cache and dirty. Skip step 1, DMA the real image, then force eviction by streaming a buffer larger than the LLC. Read memory through an NC alias, or a MEM-AP. Marker bytes in the image prove the write-back hazard.
  2. Count the write-backs. L1D_CACHE_WB (0x0015) and L2D_CACHE_WB (0x0018) count them during the eviction loop.
  3. Repeat with step 1 restored. The markers must disappear.

A debugger memory view that differs between “via core” and “via AP” for the same address shows that a line is dirty or stale in the PE’s caches.

Checkpoint

Q6.6.1 A non-coherent DMA engine writes an image into DRAM. Before the transfer, software executed no maintenance. After the transfer, it executes DC IVAC over the range, then DSB, and reads the image through a WB mapping. Occasionally a few cache lines of the image contain old buffer contents. What is the most likely mechanism?

  • A. The DSB after DC IVAC should have been an ISB
  • B. DC IVAC acts only on the PoU, so the system cache was not invalidated
  • C. The NC accesses of the DMA engine bypassed the shareability domain, so maintenance cannot help
  • D. A dirty line covering the destination was evicted (written back) during the transfer, after the DMA had written that memory; invalidating afterwards cannot undo a write-back that has already happened
Answer

D. Lines can be evicted at any time. A dirty line written back during or after the DMA writes overwrites the new data in memory, and the later invalidate only removes the cached copy. The fix is to clean (or clean+invalidate) before the transfer. B is wrong because DC IVAC operates to the PoC. A is wrong because a DSB is what completes the maintenance, while an ISB does not wait for it.

Read more: Arm ARM (DDI0487), “Mismatched memory attributes”, “Data cache maintenance instructions”

Q6.6.2 Why does TF-A’s BL31 invalidate its RW range (inv_dcache_range) at entry, while its caches are still disabled?

  • A. Because caches are always full of random data at reset, as the architecture requires
  • B. Because an earlier boot stage may have left lines, possibly dirty, in a cache that is not reset with the PE, such as a system cache; these could later hit, or be written back over BL31’s data once caches are enabled
  • C. Because DC IVAC is needed to enable the MMU
  • D. Because zeroing .bss with the MMU off would otherwise fault
Answer

B. This is the mismatch between “written with caches off” and “read with caches on”. Stale clean lines cause wrong reads, and stale dirty lines cause later corruption. The source comment cites dirty lines in a system cache left by an earlier stage. A overstates the case: reset invalidation is IMPLEMENTATION DEFINED, and the problem is lines left by earlier stages, not random contents. D confuses this with the DC ZVA and alignment rules for Device memory.

Read more: TF-A el3_common_macros.S / bl2_entrypoint.S; Arm ARM (DDI0487), “Mismatched memory attributes”

Q6.6.3 A platform maps a Secure-only DRAM carve-out as Normal WB in the Non-secure kernel’s linear map but never accesses it. The kernel occasionally takes SErrors that point at no particular instruction. What is the best explanation and fix?

  • A. The Normal mapping permits speculative and prefetch reads into the carve-out, which the TZASC rejects; remove it from the linear map (no-map)
  • B. The mapping is harmless because it is never architecturally accessed; the SErrors must come from a peripheral
  • C. Change the mapping to Device-nGnRnE, which makes it safe to leave mapped
  • D. Add DC CIVAC over the carve-out at boot to make the alias coherent
Answer

A. Any Normal mapping licenses speculative access. The TZASC (or GPC under RME) rejects the access, and the error can arrive as an asynchronous SError. Not mapping the region is the only robust fix. C removes data speculation but still permits speculative instruction fetch, unless the region is XN as well, and it still leaves a mapping to protected memory in the NS world. D misunderstands the problem: this is an access-permission issue, not a coherence one.

Read more: Arm ARM (DDI0487), “Speculative accesses” and “Mismatched memory attributes”; Linux Documentation/devicetree/bindings/reserved-memory