Skip to content

Commit de4a8af

Browse files
committed
Cortex-R5/GNU: Add CLREX in solicited context restore
This clears exclusive monitor in the solicited (voluntary yield) path to prevent silent data corruption when threads using LDREX/STREX (C11 atomics, GCC __sync builtins, lock-free code) yield via tx_thread_relinquish() or blocking calls. Without CLREX, the local exclusive monitor retains stale "exclusive" state across context switches, causing subsequent STREX to succeed without re-checking memory - breaking compare-and-swap guarantees. The interrupt preemption path is already safe because exception entry automatically clears the monitor per ARMv7-R ARM Section A3.4.4.
1 parent f541a44 commit de4a8af

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

ports/cortex_r5/gnu/src/tx_thread_schedule.S

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ _tx_skip_interrupt_vfp_restore:
177177
LDMIA sp!, {r0-r12, lr, pc}^ @ Return to point of thread interrupt
178178
@
179179
_tx_solicited_return:
180+
@
181+
@ /* Clear the exclusive monitor. This is required to prevent a thread that
182+
@ * performed LDREX before yielding (via tx_thread_relinquish or blocking)
183+
@ * from having a subsequent STREX succeed without re-checking memory.
184+
@ * Exception entry clears the monitor automatically, so only the solicited
185+
@ * (voluntary) path needs explicit CLREX.
186+
@ * Reference: ARMv7-R ARM, Section A3.4.4 (Exclusive monitors and
187+
@ * context switches).
188+
@ */
189+
@
190+
CLREX
180191
#ifdef TX_ENABLE_VFP_SUPPORT
181192
LDR r1, [r0, #144] @ Pickup the VFP enabled flag
182193
CMP r1, #0 @ Is the VFP enabled?

0 commit comments

Comments
 (0)