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.5 MAIR_ELx and AttrIndx

Stage 1 descriptors do not encode the memory type directly. A block or page descriptor carries a 3-bit AttrIndx in bits[4:2]. That index selects one of eight 8-bit Attr fields in MAIR_ELx (Memory Attribute Indirection Register) of the translation regime. There are three of these registers:

  • MAIR_EL1 for EL1&0;
  • MAIR_EL2 for EL2, or for EL2&0 with VHE;
  • MAIR_EL3.

Shareability is not in MAIR. It comes from the descriptor’s SH field (6.4). Stage 2 does not use indirection: its descriptors carry MemAttr[3:0] directly, and the combination rules (including FEAT_S2FWB) are in 11.3.

The Attr encoding

Each Attr field (8 bits, written Attr<n> in DDI0487) is decoded as follows.

Attr[7:4]Attr[3:0]Meaning
0000dd00Device memory. dd: 00 nGnRnE, 01 nGnRE, 10 nGRE, 11 GRE
0000dd01With FEAT_XS: Device memory with XS=0. Otherwise UNPREDICTABLE
0000dd1xUNPREDICTABLE
oooo ≠ 0000iiii ≠ 0000Normal memory. oooo = Outer, iiii = Inner, per the table below
01000000With FEAT_XS: Normal Inner/Outer Non-cacheable, XS=0
10100000With FEAT_XS: Normal Inner/Outer WT RA nWA Non-transient, XS=0
11110000With FEAT_MTE2: Tagged Normal, Inner/Outer WB RA WA Non-transient
other0000UNPREDICTABLE

The Inner and Outer nibbles of a Normal type use the same encoding:

NibbleMeaning
00RW (RW ≠ 00)Write-Through Transient
0100Non-cacheable
01RW (RW ≠ 00)Write-Back Transient
10RWWrite-Through Non-transient
11RWWrite-Back Non-transient

Here R and W are the read-allocate and write-allocate hints (1 = allocate) (6.3).

The working set of values is small:

ValueTypeTF-A indexLinux index
0xFFNormal WB RAWA Non-transient (Inner and Outer)0 (ATTR_IWBWA_OWBWA_NTR)0 (MT_NORMAL)
0xF0Tagged Normal (MTE)n/a1 (MT_NORMAL_TAGGED)
0x44Normal Non-cacheable2 (ATTR_NON_CACHEABLE)2 (MT_NORMAL_NC)
0x00Device-nGnRnE1 (ATTR_DEVICE)3 (MT_DEVICE_nGnRnE)
0x04Device-nGnREn/a4 (MT_DEVICE_nGnRE)
    // MAIR_EL3: idx0 = Normal WB, idx1 = Device-nGnRnE, idx2 = Normal NC
    mov     x0, #0xff                 // Attr0
    mov     x1, #0x440000             // Attr1 = 0x00 (bits[15:8]), Attr2 = 0x44 (bits[23:16])
    orr     x0, x0, x1
    msr     mair_el3, x0
    isb                               // before any walk that uses the new values

Tagged Normal (0xF0)

0xF0 is the only Tagged type, and it must be Inner and Outer WB RAWA Non-transient.

  • Tag checking and tag accesses take place only for Tagged memory. Instructions such as LDG and STG operate on allocation tags only there; on Untagged memory their tag accesses are RAZ/WI (13.4).
  • Without FEAT_MTE2, 0xF0 is UNPREDICTABLE. It is not “plain WB”.
  • Linux defers the switch. Linux boots with MAIR_EL1.Attr1 = 0xFF and switches it to 0xF0 only once MTE support has been detected. mte_cpu_setup() writes MAIR_EL1 and then calls local_flush_tlb_all(), because TLB entries may hold attributes derived from the old MAIR value.

That last step generalises. Changing an Attr field that live mappings use changes the attributes of every one of those mappings, and TLBs may cache the old attributes. Treat it like a change to the descriptors: change the field only when no mapping uses it, or follow the write with ISB and TLB invalidation, and deal with any aliasing it creates (6.6).

FEAT_AIE (brief)

FEAT_AIE is a 2022 extension; for its permitted-from version, see DDI0487 A2. It extends the indirection to 16 entries:

  • TCR2_ELx.AIE=1 enables an extra AttrIndx bit in the descriptor.
  • Indices 8–15 select fields in MAIR2_ELx.
  • When 128-bit descriptors are enabled (TCR2_ELx.D128=1, FEAT_D128), TCR2_ELx.AIE is RES1 (5.7).

Memory types when the MMU or the D-cache is off

When stage 1 translation of a regime is disabled (SCTLR_ELx.M=0), the PE does not consult MAIR at all. The VA is used directly as the PA, and fixed attributes apply.

AccessAttributes with SCTLR_ELx.M=0
DataDevice-nGnRnE, whatever SCTLR_ELx.C says
Instruction fetch, SCTLR_ELx.I=1Normal, Outer Shareable, Inner/Outer Write-Through RA nWA
Instruction fetch, SCTLR_ELx.I=0Normal, Outer Shareable, Inner/Outer Non-cacheable

Special cases:

  • EL1&0 under a hypervisor. For the EL1&0 regime, HCR_EL2.DC=1 overrides the data default to Normal Non-shareable, Inner/Outer Write-Back RAWA, and the PE behaves as if HCR_EL2.VM were 1, so stage 2 translation applies (11.3).
  • MMU on, D-cache off. With M=1 and C=0, MAIR is used, but every Normal data access is treated as Non-cacheable. Device mappings stay Device.

The asymmetry between data and instruction fetch is deliberate. Data stays safe for MMIO, and instruction fetch stays fast. The fetch side is still speculative. With all stages of translation disabled, DDI0487 limits speculative instruction fetches to locations near the code being executed (roughly the same or the next translation-granule-sized block; see “Behavior of instruction fetches when all associated stages of translation are disabled”). Within that window nothing is execute-never, so an unmapped hole or a read-sensitive peripheral placed directly after the boot image can be fetched. With stage 2 enabled (EL1 with its MMU off under a hypervisor), this limit does not apply, because not all stages are disabled (14.2).

The consequences of running with data as Device-nGnRnE

Early boot code (BL1, BL31’s cold-boot entry, and the PSCI warm-boot paths before the MMU is enabled) runs with all data in Device-nGnRnE memory, including its stack. Four consequences follow.

  1. Every unaligned data access faults, whatever SCTLR_ELx.A says (DFSC 0b100001). Compilers generate unaligned accesses freely on AArch64: for packed structs, for inlined memcpy, and for merged adjacent stores. TF-A therefore builds all AArch64 code with -mstrict-align (make_helpers/cflags.mk), so the compiler never emits an access it does not know to be aligned. TF-A also sets SCTLR_EL3.A=1 at entry, so alignment checking stays on after the MMU is enabled. The flag is therefore needed in both phases. It does not cover hand-written assembly or prebuilt libraries.
  2. DC ZVA faults. DC ZVA to Device memory generates an Alignment fault, so an optimised memset cannot be used before the MMU is on. TF-A has two helpers: zeromem uses plain stores and is safe with the MMU off, while zero_normalmem uses DC ZVA and requires Normal memory.
  3. Exclusives and atomics are not guaranteed. Load-Exclusive/Store-Exclusive and LSE atomics are architecturally guaranteed only on Normal shareable WB memory. On Device memory, support is IMPLEMENTATION DEFINED: a STXR might never succeed, or the access might fault with “Unsupported Exclusive or Atomic access” (DFSC 0b110101). This is why TF-A uses bakery locks, which need only ordinary loads and stores, on paths where participating PEs may have their data caches off. Spinlocks built on exclusives are used only where every participant is coherent (10.4).
  4. Everything is slow and nothing is speculative. Every load waits for a non-gathered, non-reordered round trip. On emulation, that can make the pre-MMU phase dominate boot time. That is a reason to enable the MMU early, not to “optimise” pre-MMU code.

Bring-up note: “Works on FVP, Alignment fault on silicon or emulation right after reset” is almost always consequence 1 or 2. Look for a new C file built without -mstrict-align, a vendor library linked into BL1 or BL2, or a memset that resolves to an optimised libc version. ESR shows EC 0x25 (Data Abort, same EL) with DFSC 0b100001, and FAR_ELx gives an address that is not naturally aligned, or that is the target of a DC ZVA (5.8).

Validation note: To check that every MAIR index your firmware uses produces the attributes you meant, use AT S1E3R (or S1E1R) on one address per index, then ISB, then read PAR_EL1. PAR_EL1.ATTR (bits[63:56]) returns the resulting attributes in MAIR format, and PAR_EL1.SH returns the shareability (5.9). In RTL or emulation, also check the bus attributes of one real access per index. A mismatch between PAR_EL1.ATTR and the bus means an integration problem, not a table problem.

Checkpoint

Q6.5.1 BL31 code that runs before the MMU is enabled calls a C helper that zeroes a struct with memset, and takes an Alignment fault at a 64-byte-aligned address. What is the most likely cause?

  • A. The struct is not aligned to 16 bytes
  • B. SCTLR_EL3.A is 1; clearing it would fix the fault
  • C. The memset uses DC ZVA, which faults on Device memory, and with the MMU off all data accesses are Device-nGnRnE
  • D. DC ZVA is UNDEFINED at EL3
Answer

C. With stage 1 disabled, data accesses are Device-nGnRnE, and DC ZVA to Device memory generates an Alignment fault, even at an aligned address. B is the tempting fix, but Device-memory alignment faults do not depend on SCTLR_ELx.A. The fix is a plain-store zeroing routine (TF-A’s zeromem) until the MMU is on.

Read more: Arm ARM (DDI0487), DC ZVA; “Behavior when stage 1 address translation is disabled”

Q6.5.2 With SCTLR_EL3.M=0 and SCTLR_EL3.I=1, which statement about instruction fetch is correct?

  • A. Fetches are Normal, Write-Through cacheable and Outer Shareable, so the PE may speculatively fetch beyond the executed code, within an architecturally limited window, and nothing in that window is execute-never
  • B. Fetches are Device-nGnRnE like data accesses, so no speculative fetch occurs
  • C. Fetches are Normal Write-Back, so IC IALLU is not needed after loading code
  • D. Instruction fetch is disabled until the MMU is enabled
Answer

A. With stage 1 off, instruction fetches are Normal, Outer Shareable, and WT-cacheable when I=1. Nothing marks any region execute-never. DDI0487 limits speculative fetches in this state to locations near the code being executed, but MMIO or a hole directly after the boot image is inside that window, which is how early-boot SErrors from speculative fetches arise. B is the common assumption that “MMU off means everything is Device”, which holds only for data. C is wrong because the I-cache can still hold stale lines.

Read more: Arm ARM (DDI0487), “Behavior when stage 1 address translation is disabled”; SCTLR_EL3.I

Q6.5.3 Why does TF-A build with -mstrict-align even though most of BL31 runs with the MMU and caches enabled?

  • A. Because -mstrict-align makes generated code faster on Normal memory
  • B. Because code runs with data as Device-nGnRnE before the MMU is on, and because TF-A sets SCTLR_EL3.A=1 so alignment checking also applies after the MMU is enabled
  • C. Because Normal memory forbids unaligned accesses at EL3
  • D. Because exclusives require aligned addresses, and -mstrict-align also aligns all exclusive accesses
Answer

B. Both phases fault on unaligned accesses: Device memory always, and Normal memory once SCTLR_EL3.A=1. The flag stops the compiler from introducing such accesses. C is false, because Normal memory permits unaligned accesses unless SCTLR_ELx.A is set. D misstates the flag: it affects the compiler’s choice of ordinary loads and stores, while exclusive alignment is an architectural rule that the code must meet anyway.

Read more: Arm ARM (DDI0487), “Alignment support”; TF-A make_helpers/cflags.mk, el3_common_macros.S