3.5 Exclusives: LDREX/STREX, CLREX and the monitors
Armv8-M provides LDREX/STREX in byte, halfword and word forms, in Baseline as well as Mainline. (v6-M had no exclusives.) There is no doubleword form. CLREX clears the local monitor. Armv8-M also adds load-acquire/store-release, LDA/STL, and the exclusive forms LDAEX/STLEX. There is no LSE (no CAS or LDADD) as there is in v8.1-A.
The monitor model is the same as on A-profile:
- The local monitor is per PE. Exclusive accesses to Non-shareable memory are checked only against the local monitor.
- The global monitor belongs to the memory system. Exclusives to Shareable memory are checked against both monitors. Many single-core MCUs have no global monitor. In that case exclusives to Shareable regions may not behave as intended (depending on the core and bus, STREX can always fail), which is why lock variables are often placed in Non-shareable memory.
STREX writes 0 on success and 1 on failure. The architecture also allows it to fail without a conflicting access, so the code must loop.
Exceptions: In Armv8-M, exception entry and exception return both clear the local monitor, as if a CLREX had run. Suppose a thread is preempted between its LDREX and STREX. When it resumes, its STREX fails and it retries. An ISR that does its own LDREX/STREX also cannot inherit the thread’s reservation. This means an RTOS context switch does not need an explicit CLREX for correctness. CLREX is still useful for abandoning a reservation in thread code.
Exception entry only clears the local monitor. It does not reset any global monitor state that other bus masters see. Exclusives are also not a substitute for barriers. If the lock protects data, you need a DMB after acquiring and before releasing, or LDAEX to acquire and STL to release. LDREX/STREX by themselves give no ordering.
Coming from A-profile: On AArch64, an exception return also clears the local monitor, and Linux arm64 relies on this instead of a CLREX on context switch. In v8-M, both entry and return clear it as part of the exception model. The bigger difference is the missing LSE atomics: every RMW on v8-M is an exclusive loop.
Checkpoint
Q3.5.1 A thread executes LDREX, is preempted by an IRQ whose handler does nothing to the monitor, and then resumes and executes STREX to the same address. What happens?
- A. The STREX always succeeds, because no other agent wrote the location
- B. It depends on whether the ISR touched the same address
- C. The STREX faults
- D. The STREX fails (returns 1), because exception entry and return cleared the local monitor
Answer
D. Armv8-M clears the local monitor on both exception entry and exception return, so the reservation is lost and the retry loop runs again. The tempting idea that it succeeds because nobody wrote the location ignores that the architecture clears the monitor anyway. This is also why no CLREX is needed in the context switch path.
Read more: Armv8-M ARM (DDI0553), exception entry/return pseudocode (ClearExclusiveLocal) and the synchronization primitives chapter
Q3.5.2 A spinlock uses an LDREX/STREX loop to take the lock and a plain STR of 0 to release it. Protected data is updated in the critical section. What is missing for correctness on Armv8-M Mainline?
- A. A DMB after a successful acquire and a DMB before the releasing STR (or LDAEX to acquire and STL to release)
- B. A CLREX after every STREX
- C. An ISB after the STREX
- D. Nothing: exclusives imply full ordering
Answer
A. LDREX/STREX give atomicity but no ordering, so data accesses could be reordered across the lock operations. A DMB, or acquire/release semantics, prevents that. CLREX only abandons a reservation and does nothing for ordering.
Read more: Armv8-M ARM (DDI0553), synchronization and semaphores; load-acquire/store-release