Skip to content

Commit ec2f42c

Browse files
committed
OS/ThreadX: fix TX_DISABLE/TX_RESTORE macros with volatile and memory clobber
Add volatile keyword and memory clobber to inline assembly in `TX_DISABLE` and `TX_RESTORE` macros to prevent compiler reordering and ensure proper memory barrier semantics for interrupt disable/restore operations. Signed-off-by: Huaqi Fang <578567190@qq.com>
1 parent 1ca83c1 commit ec2f42c

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

OS/ThreadX/ports/nuclei/tx_port.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ typedef unsigned short USHORT;
296296

297297
#define TX_INTERRUPT_SAVE_AREA ULONG interrupt_save;
298298
/* Atomically read mstatus into interrupt_save and clear bit 3 of mstatus. */
299-
#define TX_DISABLE {__asm__ ("csrrci %0, mstatus, 0x08" : "=r" (interrupt_save) : );};
299+
#define TX_DISABLE {__asm__ volatile ("csrrci %0, mstatus, 0x08" : "=r" (interrupt_save) : : "memory");};
300300
/* We only care about mstatus.mie (bit 3), so mask interrupt_save and write to mstatus. */
301301
#define TX_RESTORE {register ULONG __tempmask = interrupt_save & 0x08; \
302-
__asm__ ("csrrs x0, mstatus, %0 \n\t" : : "r" (__tempmask) : );};
302+
__asm__ volatile ("csrrs x0, mstatus, %0 \n\t" : : "r" (__tempmask) : "memory");};
303303

304304
#endif
305305

doc/source/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ This is release version of ``0.9.0`` of Nuclei SDK, which is still under develop
125125
- Fix RT-Thread and UCOSII interrupt masking implementation in ``cpuport.c`` and ``os_cpu_port.h`` to add memory barriers after CSR operations, ensuring pending interrupts are properly handled after enabling interrupts
126126
- Fix FreeRTOS tickless sleep mode by removing unnecessary ``__FENCE_I()`` call in ``vPortSuppressTicksAndSleep`` function to avoid i-cache misses and reduce interrupt latency. The ``__RWMB()`` memory barrier is sufficient for proper interrupt enable propagation
127127
- Upgrade FreeRTOS to v11.2.0 and update porting layer to match new API requirements (critical nesting macros and recursive lock functions now accept core ID parameter for SMP systems)
128+
- Fix ThreadX ``tx_port.h`` interrupt disable/restore macros (``TX_DISABLE``/``TX_RESTORE``) by adding ``volatile`` keyword and ``memory`` clobber to inline assembly to prevent compiler reordering and ensure proper memory barrier semantics
128129

129130
* Build System
130131

0 commit comments

Comments
 (0)