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

10.3 Wide and unaligned accesses

7.2 gives the baseline: an aligned access of up to 64 bits is single-copy atomic, and everything else is a sequence of smaller atomic accesses. This section covers the instructions that go beyond that baseline (128-bit and 64-byte accesses), the rules that let an unaligned access still be atomic, and the alignment-fault rules that decide which of these you get and which fault.

128 bits before LSE2: LDXP/STXP and CASP

LDXP/STXP (v8.0) load and store a register pair exclusively. With 64-bit registers, the pair must be 16-byte aligned. The atomicity guarantee is asymmetric, and it is the one people miss:

  • A STXP that succeeds writes the 128 bits as a single-copy atomic access.
  • An LDXP on its own is not guaranteed to read 128 bits atomically. The two halves may be read at different times. The read is only known to be atomic if a following STXP to the same address succeeds, which proves no write intervened.

A 128-bit atomic load in v8.0 is therefore a read-modify-write:

// x0 = 16-byte aligned address; returns the 128-bit value in x2:x3
1:  ldaxp   x2, x3, [x0]
    stxp    w4, x2, x3, [x0]    // write back the same value
    cbnz    w4, 1b

This writes, so it faults on read-only mappings and causes coherence traffic on every read.

CASP (v8.1, part of FEAT_LSE) compares and swaps a pair: CASP Xs, Xs+1, Xt, Xt+1, [Xn], with A/L/AL variants. Xs and Xt must be even-numbered registers. The 64-bit form needs 16-byte alignment and is a single 128-bit single-copy atomic access when it writes. It has the same “failed compare need not write” rule as CAS (10.2). Like the LDXP/STXP read idiom, it needs write permission: CASP is treated as a write for permission checks even when the compare fails, so it cannot serve as a load from read-only memory.

FEAT_LSE2 (v8.4)

FEAT_LSE2 is mandatory from v8.4 (ID_AA64MMFR2_EL1.AT, bits [35:32], = 0b0001). It changes two things.

1. 16-byte LDP/STP atomicity. An LDP, LDNP or STP of two 64-bit registers is single-copy atomic as a whole if the access is 16-byte aligned and is to Normal Inner Write-Back, Outer Write-Back memory. The exact conditions are in DDI0487, Changes to single-copy atomicity in Armv8.4. This finally gives a pure 128-bit atomic load (LDP) and store (STP). GCC’s libatomic, for example, uses this when HWCAP_USCAT is set.

2. Unaligned accesses within 16 bytes. A load or store, including an atomic, exclusive or acquire/release access, whose bytes all fall within one aligned 16-byte quantity is single-copy atomic even if it is not naturally aligned. Recent DDI0487 issues add CTR_EL0.LSE2Size (bits [43:40]), which lets an implementation advertise a larger quantity. Its minimum encoding, 0b0000, means 16 bytes, which is the only portable assumption.

It also relaxes alignment faults:

  • Exclusives and LSE atomics do not fault if they stay within the aligned 16-byte quantity. They still fault if they cross it.
  • Load-acquire/store-release (LDAR, LDAPR, LDAPUR, STLR, STLUR and relatives, the list in SCTLR_ELx.nAA) do not fault within the 16-byte quantity. For an access that crosses it, SCTLR_ELx.nAA (bit 6) decides: 0 gives an Alignment fault, and 1 performs the access without a fault but without single-copy atomicity.

The fault behaviour, as DDI0487’s AArch64.UnalignedAccessFaults() pseudocode expresses it, is:

if exclusive or atomic:   fault = !LSE2 || !AllInAlignedQuantity(addr, size, 16)
if acquire/release:       fault = !LSE2 || (SCTLR_ELx.nAA == '0' && !AllInAlignedQuantity(addr, size, 16))

Setting nAA = 1 does not make anything atomic. It only stops the fault that tells you atomicity was lost. Leave it at 0 unless you are running legacy code that you know tolerates torn acquire/release accesses.

Alignment-fault rules by memory type and SCTLR_ELx.A

AccessNormal memory, SCTLR_ELx.A = 0Normal memory, A = 1Device memory (any A)
Ordinary load/store, LDP/STP, SIMDunaligned permitted (not single-copy atomic as a whole unless LSE2 applies)Alignment fault if not aligned to the element/access sizeAlignment fault if unaligned
Exclusives, LSE atomicsfault unless aligned (LSE2: unless within 16 bytes)Alignment fault if not naturally aligned (the LSE2 relaxation does not apply)fault if unaligned, and memory-type support is IMPLEMENTATION DEFINED (10.1)
Acquire/releasefault unless aligned (LSE2: within 16 bytes, or per nAA)Alignment fault if not naturally aligned (LSE2 and nAA do not apply)fault if unaligned
DC ZVApermittedpermittedAlignment fault
LD64B/ST64B*must be 64-byte alignedsamemust be 64-byte aligned

Two rows are where firmware gets hurt. With the MMU off, data accesses are Device-nGnRnE (with the MMU on but SCTLR_ELx.C == 0, Normal memory becomes Non-cacheable instead, which does not fault on unaligned access) (6.5). Any unaligned access then faults, including a compiler-generated unaligned LDR from memcpy or a packed struct, and DC ZVA faults. This is why TF-A builds with -mstrict-align. The fault is reported as a synchronous Data Abort with DFSC 0b100001 (Alignment fault). See 5.8.

FEAT_LSE128 (v9.4)

FEAT_LSE128 (ID_AA64ISAR0_EL1.Atomic = 0b0011) adds 128-bit forms of three LSE operations. Each takes the operand in a register pair and returns the old value in the same pair:

InstructionOperation
SWPP{A,L,AL} Xt1, Xt2, [Xn]swap 128 bits
LDSETP{A,L,AL}128-bit OR
LDCLRP{A,L,AL}128-bit AND-NOT

The address must be 16-byte aligned. There is no 128-bit add: the set is bitwise and swap operations, which suits flag words and descriptors rather than arithmetic. The main architectural customer is FEAT_D128 (128-bit translation table descriptors). Software sets and clears descriptor bits atomically with LDSETP/LDCLRP and exchanges a descriptor with SWPP, against hardware access-flag and dirty-state updates (5.6, 5.7). The version rules are in DDI0487 A2; it is optional.

FEAT_LRCPC3 (v8.9)

FEAT_LRCPC3 (ID_AA64ISAR1_EL1.LRCPC = 0b0011, optional) extends the RCpc acquire/release family:

  • LDIAPP (Load-Acquire RCpc ordered Pair) and STILP (Store-Release ordered Pair), with 32- and 64-bit register forms and writeback variants. DDI0487 defines an order between the two halves. For LDIAPP, the memory effects of Xt1 are Ordered-before those of Xt2. This is what makes a “load flag, then the payload next to it” pair safe in one instruction.
  • post-index LDAPR, pre-index STLR, and SIMD&FP LDAPUR/STLUR/LDAP1/STL1.

The 128-bit use: with LSE2, a 16-byte-aligned LDIAPP Xt1, Xt2 is a 128-bit acquire load that does not write, which neither LDXP/STXP nor CASP can provide. For single-copy atomicity of the pair, DDI0487 points to Requirements for single-copy atomicity. Treat the pair as atomic only under the same alignment and memory-type conditions as LSE2 LDP.

FEAT_LS64, LS64_V, LS64_ACCDATA (v8.7)

These add 64-byte single-copy atomic loads and stores, intended for accelerator and device work queues, where a 64-byte descriptor must arrive as one transaction, never as eight. All are optional. ID_AA64ISAR1_EL1.LS64 (bits [63:60]) reports the level.

ValueFeatureAdds
0b0001FEAT_LS64LD64B, ST64B
0b0010FEAT_LS64_VST64BV Xs, Xt, [Xn]: store, with a 64-bit status returned in Xs
0b0011FEAT_LS64_ACCDATAST64BV0 Xs, Xt, [Xn]: as ST64BV, but bits [31:0] of the first doubleword come from ACCDATA_EL1[31:0]
0b0100FEAT_LS64WBLD64B/ST64B (and 32-byte SIMD&FP pair accesses) to Write-Back Cacheable, Shareable memory

The data is eight consecutive registers starting at Xt. The address must be 64-byte aligned.

Memory-type restrictions. Which locations support these instructions is IMPLEMENTATION DEFINED. In practice, without FEAT_LS64WB the targets are Device or Normal Non-cacheable memory with a target that accepts 64-byte transactions. A location that supports ST64BV0 also supports ST64BV. For an unsupported location:

  • ST64BV/ST64BV0 return 0xFFFF_FFFF_FFFF_FFFF in Xs, and the memory contents are UNKNOWN.
  • LD64B/ST64B can generate a Data Abort with DFSC 0b110101 (Unsupported Exclusive or Atomic access). The architecture also allows IMPLEMENTATION DEFINED behaviour here, so a test must accept either outcome.

Any other status value is defined by the target device. A typical meaning is “accepted” or “retry, queue full”.

Enables (all default to trapping when 0):

ControlBitTrapsTo
SCTLR_EL1.EnALS56EL0 LD64B/ST64BEL1
SCTLR_EL1.EnAS055EL0 ST64BV0EL1
SCTLR_EL1.EnASR54EL0 ST64BVEL1
HCRX_EL2.EnAS0 / EnALS / EnASR0 / 1 / 2EL0 and EL1 use (EL0 only if not already trapped to EL1)EL2
SCR_EL3.EnAS036ST64BV0 at EL0–EL2EL3
SCR_EL3.ADEn37EL1/EL2 access to ACCDATA_EL1EL3

Traps are reported with EC 0x0A, and the ISS identifies the instruction. A trap at EL2 is a common surprise. If SCR_EL3.HXEn == 0, the effective value of HCRX_EL2 is 0, so all three HCRX traps are active even though EL2 never set them. EL3 must set SCR_EL3.HXEn (and usually the HCRX enables in its EL2 hand-off) for a kernel to use LS64 (17.3).

Bring-up note: 64-byte atomic accelerator doorbells. A typical shared work queue exposes a “portal” page per queue. A submitter builds a 64-byte descriptor in x0–x7 and issues ST64BV0 x8, x0, [portal]. The device receives the whole descriptor in one write, with the low 32 bits replaced by ACCDATA_EL1. The kernel or hypervisor loads that register per process or per VM, typically with a PASID-like context ID, so user space cannot forge it. x8 returns accept or retry. The bring-up checks are:

  1. the portal is mapped Device-nGnRE (or Non-cacheable), never cacheable unless FEAT_LS64WB is present;
  2. every hop (core, CHI deferrable-write support such as WriteNoSnpDef in CHI issue E and later, C2C bridge, PCIe Deferrable Memory Write if the device is behind a root port) preserves the 64-byte write as one transaction with a response. A bridge that splits it gives torn descriptors, and one that cannot return status turns every ST64BV into 0xFFFF… or an abort;
  3. the EL3 and EL2 enables above are set;
  4. ACCDATA_EL1 is context-switched.

A torn-write test is to submit descriptors whose eight doublewords carry the same sequence number, and have the device or a bus monitor check that they all match.

Checkpoint

Q10.3.1 On a v8.0 core without LSE2, code needs to read a 16-byte aligned 128-bit value from a page mapped read-only at EL1. Another core updates it with STXP. Which approach gives a single-copy atomic read?

  • A. LDXP x2, x3, [x0] alone, because an exclusive pair load is single-copy atomic
  • B. LDP x2, x3, [x0], because the address is 16-byte aligned
  • C. CASP x2, x3, x2, x3, [x0] with equal compare and new values, because it never changes memory
  • D. None of these. The v8.0 atomic-read idiom is LDXP plus a successful STXP of the same value, which needs write permission. The data must be made writable or read with a separate protocol such as a seqlock
Answer

D. LDXP alone is not guaranteed atomic, and atomicity is only established by a successful STXP. Aligned LDP atomicity is a FEAT_LSE2 property. CASP, even one that cannot change memory, is treated as a write for permission checks and faults on a read-only page. With LSE2 (mandatory from v8.4), B becomes correct for Write-Back memory.

Read more: Arm ARM (DDI0487), Requirements for single-copy atomicity; LDXP, STXP; CASP

Q10.3.2 On a v8.4 core, SCTLR_EL1.nAA == 0. Code executes LDAR x1, [x0] with x0 = 0x1008, and then with x0 = 0x100C. What happens?

  • A. Both take Alignment faults, because acquire loads must be naturally aligned
  • B. 0x1008 is in one aligned 16-byte quantity and does not fault. 0x100C crosses into the next quantity and faults
  • C. Neither faults, because LSE2 removes alignment checks for acquire loads
  • D. Both succeed, but only 0x1008 is single-copy atomic. nAA == 0 suppresses the fault
Answer

B. With LSE2, an acquire/release access faults only if it is not wholly within an aligned 16-byte quantity and nAA == 0. Bytes 0x1008–0x100F stay inside 0x1000–0x100F, while 0x100C–0x1013 cross the boundary. C and D describe nAA == 1, which would let 0x100C complete without a fault and without atomicity.

Read more: Arm ARM (DDI0487), SCTLR_EL1.nAA; Changes to single-copy atomicity in Armv8.4

Q10.3.3 An EL1 driver’s first ST64BV to an accelerator portal traps to EL2 with EC 0x0A, although the hypervisor never writes HCRX_EL2. SCTLR_EL1.EnASR is 1. What is the most likely cause?

  • A. The portal is mapped as Normal Write-Back, and LS64 instructions trap on cacheable memory
  • B. ST64BV always traps to EL2 the first time so that the hypervisor can validate the device
  • C. SCR_EL3.HXEn is 0, so the effective value of HCRX_EL2 is 0 and HCRX_EL2.EnASR traps EL1 use to EL2
  • D. SCR_EL3.EnAS0 is 0, which traps all LS64 stores to EL3, reported at EL2
Answer

C. When EL2 is enabled and SCR_EL3.HXEn == 0, HCRX_EL2 behaves as all zeros, so every HCRX enable is effectively off, and its traps are on. A wrong memory type gives a Data Abort or a 0xFFFF… status, not an EC 0x0A trap. SCR_EL3.EnAS0 only covers ST64BV0, and it traps to EL3.

Read more: Arm ARM (DDI0487), HCRX_EL2 (EnASR), SCR_EL3 (HXEn, EnAS0), ST64BV