10.1 Exclusives
An exclusive pair (LDXR … STXR) does not lock anything. It is an optimistic protocol: the load marks an address, the store succeeds only if nothing could have written the marked location in between, and software retries on failure. All the guarantees come from exclusive monitors, which are small state machines with two states, Open Access and Exclusive Access. Everything that is IMPLEMENTATION DEFINED about exclusives comes down to where these monitors live and what they can see.
The instructions
| Instruction | Effect | Ordering |
|---|---|---|
LDXR{B,H}, LDXR Wt/Xt | Load, and mark the address (local and, if shareable, global monitor) | none |
LDAXR… | Same, with acquire (RCsc) | acquire |
STXR Ws, Wt, [Xn] | Store if the monitors permit; Ws = 0 on success, 1 on failure | none |
STLXR… | Same, with release | release |
LDXP/STXP, LDAXP/STLXP | Pair forms, see 10.3 | as above |
CLREX | Clear the local monitor to Open Access | none |
A canonical atomic increment looks like this:
// x0 = address of a 64-bit counter
1: ldaxr x1, [x0] // load and mark, acquire
add x1, x1, #1
stlxr w2, x1, [x0] // try store, release; w2 = status
cbnz w2, 1b // lost the reservation, retry
The status register Ws must not be the same register as Xt or Xn. Those combinations are CONSTRAINED UNPREDICTABLE. A store-exclusive always clears the local monitor, whether it succeeds or fails, so a failed STXR must go back to the LDXR, not to itself.
The architecture does not guarantee that a successful STXR means “no other agent wrote the location”. It guarantees the reverse: if another observer wrote the marked granule after the LDXR, the STXR fails. Spurious failures are always permitted. That is why every exclusive sequence is a loop, and why forward progress is a separate question (below).
Local and global monitors
- The local monitor belongs to one PE. It tracks that PE’s own
LDXRand is enough for locations that are Non-shareable. It may be tag-based (an address is recorded) or just a single flag. - The global monitor tracks marked addresses on behalf of all PEs for Shareable locations. A store by any observer to a granule marked for PE n moves PE n’s global monitor to Open Access.
For Shareable locations a STXR succeeds only if both the local and the global monitor are in Exclusive Access for that address.
In a modern coherent system the “global monitor” for Normal Inner/Outer Write-Back Shareable memory is usually not a separate block. It is built from cache coherency. The PE holds the line in a Unique state between LDXR and STXR. If a snoop takes the line away (another master wants to write it), the monitor is cleared. This is non-architectural, but it is the right mental model for why exclusives on cacheable memory “just work” and scale with the coherency protocol. On CHI, the Exclusive flag on ReadShared/CleanUnique lets the home node act as a point-of-serialisation monitor when the line is not held Unique (see 8.3).
Bring-up note: on a multi-chiplet SoC, check that the chiplet-to-chiplet link (CHI C2C or a proprietary bridge) carries the Exclusive attribute and the snoops that clear monitors. If it does not,
STXRfrom cores on different dies can both succeed on the same lock. That shows up as a mutual-exclusion failure only under cross-die contention, never in single-die testing.
The exclusives reservation granule
The monitors do not track bytes. They track an Exclusives Reservation Granule (ERG), an aligned block whose size is IMPLEMENTATION DEFINED. CTR_EL0.ERG (bits [23:20]) reports log2 of the maximum granule size in words. Valid values range from 4 words (16 bytes) to 512 words (2 KB). The value 0 means “no information” (deprecated in current issues), and software must then assume 2 KB.
Consequences:
- A store by another PE anywhere in the granule may clear your reservation, even if it does not touch your bytes. Two hot lock words in one granule cause livelock-like retry storms. Pad locks to at least the ERG. Padding to the cache line size is the common practice, and is sufficient when ERG ≤ line size, which is true on mainstream cores but not guaranteed. Read
CTR_EL0to be sure. - A
STXRto a different address in the same granule as theLDXR, or of a different size, is CONSTRAINED UNPREDICTABLE in whether it succeeds. Always pair the load and store to the same address and size.
What clears a monitor
| Event | Effect |
|---|---|
STXR/STLXR by the same PE (pass or fail) | Clears the local monitor |
CLREX | Clears the local monitor |
| Store by another observer to the marked granule (Shareable) | Clears that PE’s global monitor |
| Exception return (AArch64) | Clears the local monitor |
| Taking an exception | Do not rely on the monitor state afterwards. Software must assume it may be lost |
| Eviction of the marked line, in cache-based implementations | May clear the monitor (IMPLEMENTATION DEFINED) |
| Non-exclusive store by the same PE to the marked granule | Not guaranteed to clear the local monitor |
Cache maintenance, TLBI, a translation-table change affecting the address | May clear the monitor |
Two practical rules follow from this table. First, a context switch does not need CLREX in AArch64, because the exception return already clears the local monitor. A preempted LDXR therefore cannot pair with a STXR in another thread. Second, because non-exclusive stores by the same PE are not guaranteed to clear the monitor, CLREX is still the tool when software abandons a sequence and does something else that relies on the monitor being open. An example is an emulator or a hypervisor that has emulated a store.
Security note: a reservation cannot leak across an exception boundary, because exception return clears the local monitor. The global monitor is per PE, not per world, so a Secure and a Non-secure thread on the same PE are separated by the ERET, not by the monitor. Lock words shared across worlds must still be in memory that both worlds map with the same attributes (see 6.6).
Forward progress
The architecture guarantees that an exclusive loop eventually succeeds only if software follows the usage restrictions in DDI0487, Load-Exclusive and Store-Exclusive instruction usage restrictions. In summary:
- no explicit memory accesses, cache maintenance or
TLBI,DC ZVA, or system-register writes between theLDXRand theSTXR; - no exceptions taken in between (as far as software can control this);
- the code between them is short, and the
LDXR/STXRaccess the same address and size.
Break these rules and the loop may never terminate. The classic case is a debugger single-stepping through the sequence: each step takes a debug exception, the return clears the monitor, and the STXR fails forever. Debuggers work around it by stepping over the whole sequence as a unit (GDB does this for AArch64), and Linux kprobes refuse to place probes inside one. LSE atomics (10.2) avoid the problem entirely.
Which memory types and masters
This is where the “it works on my FVP” bugs come from.
- Normal, Inner and Outer Write-Back, Inner or Outer Shareable: exclusives are architecturally supported between PEs in the same shareability domain. The coherency protocol provides the global monitor.
- Normal Non-cacheable, and Device: the global monitor must be an actual piece of hardware in the interconnect or the memory controller (an “exclusive access monitor”, like the AXI exclusive monitor in a DMC or an SRAM controller). Whether one exists for a given address range is a property of the system, so the architecture makes support IMPLEMENTATION DEFINED. If the target does not support exclusives, the behaviour is also IMPLEMENTATION DEFINED. Permitted outcomes include a Data Abort (for example DFSC
0b110101, Unsupported Exclusive or Atomic access, or an External abort), orSTXRreporting failure. Some interconnects report “OKAY” instead of “EXOKAY” and the store always fails, so a lock loop spins forever. - Device memory exclusives are a further special case. Many systems simply do not support them, and portable firmware never relies on them.
- Other masters: the global monitor only sees writes that pass through the point it monitors. A DMA master or an SCP writing through a non-coherent path to the same SRAM may not clear a PE’s reservation at all. For cacheable memory, a non-coherent master’s write is invisible to the cache-based monitor by definition. Exclusives are a mechanism for coherent PEs, not a cross-agent lock (see 10.4).
The reason the architecture cannot make Non-cacheable or Device exclusives mandatory is that the PE is not where the protection happens. For cacheable memory the PE’s own cache plus coherency enforce atomicity. For anything else, the request goes out on the bus and the target must implement the monitor. The architecture cannot require a property of an IP block it does not specify.
That is why “MMU off” matters: with SCTLR_EL3.M == 0, data accesses are Device-nGnRnE (6.5). An exclusive-based spinlock in early boot code is therefore relying on a system-level monitor that may not exist, which is why TF-A uses a different lock on its cache-off paths (10.4).
Validation note: for each memory region that firmware intends to lock in (DRAM, on-chip SRAM, a mailbox SRAM), run a directed test with the region mapped as Normal Write-Back, Normal Non-cacheable and Device. In each case, run an
LDXR/STXRincrement from two PEs and check the final count. Expected failure signatures are: a count lower than the number of increments (the monitor is missing, so both stores succeeded), a hang (the store always fails), or a synchronous Data Abort whoseESR_ELx.ISS.DFSCis0b110101or an external-abort code. On CMN-based systems, the interconnect PMU’s exclusive-access events confirm whether the transactions reached the home node with the Exclusive flag set.
Checkpoint
Q10.1.1 Two lock words, lockA at 0x8000_0000 and lockB at 0x8000_0020, are each taken with an LDAXR/STXR loop by different PEs. CTR_EL0.ERG reads 0b0100. Under heavy contention on both locks, throughput collapses even though the locks are independent. What is the best explanation?
- A.
LDAXRhas acquire semantics and serialises all loads to the same cache line - B. The ERG is 16 bytes, so the two locks are in different granules and the problem must be elsewhere
- C. The ERG is 16 words (64 bytes), so both locks share one reservation granule, and each PE’s stores clear the other’s reservation
- D. The global monitor only tracks one address per system, so every exclusive in the system competes
Answer
C. ERG is log2 of the size in words: 0b0100 gives 16 words, which is 64 bytes. Both addresses fall in the same 64-byte aligned granule, so a STXR to one lock clears the reservation for the other, causing retry storms. B misreads the field as bytes. The global monitor is per PE, not one per system, so D is wrong.
Read more: Arm ARM (DDI0487), CTR_EL0.ERG; Exclusive access instructions and Shareable memory locations
Q10.1.2 BL31 warm-boot code runs with SCTLR_EL3.M == 0 and uses an LDAXR/STXR spinlock in on-chip SRAM. On silicon, one core spins forever in the loop while no other core holds the lock. The same code passes on the FVP. Which explanation is most consistent with the architecture?
- A. With the MMU off the accesses are Device-nGnRnE, and the SRAM path has no exclusive monitor, so the store never gets an exclusive-OK response and
STXRalways reports failure - B. The missing
DSBafterSTXRkeeps the store from completing, so the status register stays 1 - C. The local monitor is disabled while the MMU is off, so
LDAXRalways marks Open Access - D.
STXRneeds a precedingCLREXon warm boot, because reset leaves the monitor in an UNKNOWN state
Answer
A. Exclusives on Device or Non-cacheable memory depend on a monitor at the target, and whether one exists is IMPLEMENTATION DEFINED (a system property). An always-failing STXR is a permitted outcome. The FVP models a monitor that the real SRAM controller may lack. B is wrong because the status register reports the monitor check, not completion. There is no architectural rule that disables the local monitor with the MMU off. A CLREX would only force failure, never fix it.
Read more: Arm ARM (DDI0487), Load-Exclusive and Store-Exclusive instruction usage restrictions; behaviour of exclusives on memory types other than Normal Write-Back
Q10.1.3 A kernel developer adds CLREX to the AArch64 context-switch path “so that thread B’s STXR cannot succeed using thread A’s reservation”. Why is this unnecessary on AArch64 for the case where the switch goes through an exception?
- A.
STXRchecks the thread ID stored inTPIDR_EL0against the reservation - B. Taking any exception always clears both the local and global monitors
- C. The global monitor is cleared by
TLBIon the ASID change - D. The exception return that resumes thread B clears the local monitor, so B’s
STXRwithout its ownLDXRfails
Answer
D. In AArch64, an exception return clears the local monitor, so a reservation cannot survive into the resumed context. B overstates the rule: software must not rely on what taking an exception does to the monitor, and the architecture does not make exception entry clear the global monitor, which is cleared by stores to the granule and the owner’s own exclusive operations (and possibly by IMPLEMENTATION DEFINED events such as eviction). Monitors know nothing about TPIDR_EL0 or ASIDs.
Read more: Arm ARM (DDI0487), Clearing the local monitor; exception return behaviour