Skip to content

Commit 41840f0

Browse files
committed
Fix Cortex-R5 memory barrier for interrupt control
This adds missing memory barriers to interrupt disable/restore ops to prevent compiler reordering and ensure proper memory ordering on ARMv7-R. - tx_port.h: Add DMB/ISB barriers and "memory"/"cc" clobbers to TX_DISABLE and TX_RESTORE inline assembly macros - tx_thread_interrupt_disable.S: Add DMB/ISB after CPSID instruction - tx_thread_interrupt_restore.S: Add DMB before MSR, ISB after Implementation: - TX_DISABLE: MRS → CPSID → DMB → ISB (acquire semantics) - TX_RESTORE: DMB → MSR → ISB (release semantics) - Uses DMB (Data Memory Barrier) for optimal lockstep performance - ISB ensures pipeline synchronization for deterministic interrupt timing Rationale: - "memory" clobber prevents compiler from reordering memory operations - "cc" clobber indicates CPSR condition codes are modified - DMB ensures memory ordering without waiting for completion (vs DSB) - ISB flushes pipeline to ensure CPSID/MSR takes effect immediately - DMB before MSR in restore ensures stores are visible before interrupts re-enable (release semantics) Impact: - Fixes race conditions in queue/mutex operations under -O3 optimization - Prevents data corruption from out-of-order memory access - Lockstep-safe: identical execution on both cores in dual-core R5F Reference: ARMv7-R Architecture Manual (DDI 0406C), Sections B1.8.14, A3.8.3 Fixes: eclipse-threadx#334
1 parent c460287 commit 41840f0

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

ports/cortex_r5/gnu/inc/tx_port.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,12 @@ unsigned int _tx_thread_interrupt_restore(UINT old_posture);
287287
#define TX_INTERRUPT_SAVE_AREA UINT interrupt_save;
288288

289289
#ifdef TX_ENABLE_FIQ_SUPPORT
290-
#define TX_DISABLE asm volatile (" MRS %0,CPSR; CPSID if ": "=r" (interrupt_save) : : "memory", "cc");
290+
#define TX_DISABLE asm volatile (" MRS %0,CPSR \n CPSID if \n DMB \n ISB ": "=r" (interrupt_save) : : "memory", "cc");
291291
#else
292-
#define TX_DISABLE asm volatile (" MRS %0,CPSR; CPSID i ": "=r" (interrupt_save) : : "memory", "cc");
292+
#define TX_DISABLE asm volatile (" MRS %0,CPSR \n CPSID i \n DMB \n ISB ": "=r" (interrupt_save) : : "memory", "cc");
293293
#endif
294294

295-
#define TX_RESTORE asm volatile (" MSR CPSR_c,%0 "::"r" (interrupt_save) );
295+
#define TX_RESTORE asm volatile (" DMB \n MSR CPSR_c,%0 \n ISB "::"r" (interrupt_save) : "memory", "cc");
296296

297297
#endif
298298

ports/cortex_r5/gnu/src/tx_thread_interrupt_disable.S

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ _tx_thread_interrupt_disable:
9393
#ifdef TX_ENABLE_FIQ_SUPPORT
9494
CPSID if @ Disable IRQ and FIQ
9595
#else
96-
CPSID i @ Disable IRQ
96+
CPSID i @ Disable IRQ
9797
#endif
98+
DMB @ Memory barrier (acquire semantics)
99+
ISB @ Flush pipeline
98100

99101
#ifdef __THUMB_INTERWORK
100102
BX lr @ Return to caller

ports/cortex_r5/gnu/src/tx_thread_interrupt_restore.S

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ _tx_thread_interrupt_restore:
8787
@
8888
@ /* Apply the new interrupt posture. */
8989
@
90+
DMB @ Memory barrier (release semantics)
9091
MSR CPSR_c, r0 @ Setup new CPSR
92+
ISB @ Synchronize context
9193
#ifdef __THUMB_INTERWORK
9294
BX lr @ Return to caller
9395
#else

0 commit comments

Comments
 (0)