Skip to content

Commit 5a845a0

Browse files
committed
Fix race condition and message loss in all remaining Cortex-M IAR ports (#516)
- Added compiler memory barriers to BASEPRI management functions in tx_port.h (M3, M4, M7, M55, M85). - Added architectural barriers (DSB/ISB) to scheduler return paths in tx_port.h and tx_thread_system_return.s (all architectures) to prevent fall-through before context switch. - Added Gemini attribution and updated headers to follow project mandates. - These changes address spurious thread resumption and lost messages, especially when TX_NOT_INTERRUPTABLE is enabled. Assisted-by: Gemini (Gemini 2.0 Flash)
1 parent 5b3ac32 commit 5a845a0

File tree

14 files changed

+130
-83
lines changed

14 files changed

+130
-83
lines changed

ports/cortex_m0/iar/inc/tx_port.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ __istate_t interrupt_save;
313313

314314
/* Set PendSV to invoke ThreadX scheduler. */
315315
*((volatile ULONG *) 0xE000ED04) = ((ULONG) 0x10000000);
316+
__asm__ volatile ("dsb 0xF \n isb 0xF " : : : "memory");
316317
if (__get_IPSR() == 0)
317318
{
318319
interrupt_save = __get_interrupt_state();

ports/cortex_m0/iar/src/tx_thread_system_return.s

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
;/***************************************************************************
1+
;***************************************************************************
22
; * Copyright (c) 2024 Microsoft Corporation
3+
; * Copyright (c) 2026-present Eclipse ThreadX contributors
34
; *
45
; * This program and the accompanying materials are made available under the
56
; * terms of the MIT License which is available at
67
; * https://opensource.org/licenses/MIT.
78
; *
89
; * SPDX-License-Identifier: MIT
910
; **************************************************************************/
11+
12+
; Some portions generated by Gemini (Gemini 2.0 Flash).
1013
;
1114
;
1215
;/**************************************************************************/
@@ -68,6 +71,8 @@ _tx_thread_system_return:
6871
LDR r0, =0x10000000 ; Load PENDSVSET bit
6972
LDR r1, =0xE000ED04 ; Load NVIC base
7073
STR r0, [r1] ; Set PENDSVBIT in ICSR
74+
DSB SY ; Ensure memory access is complete
75+
ISB SY ; Flush pipeline
7176
MRS r0, IPSR ; Pickup IPSR
7277
CMP r0, #0 ; Is it a thread returning?
7378
BNE _isr_context ; If ISR, skip interrupt enable

ports/cortex_m23/iar/inc/tx_port.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ __istate_t interrupt_save;
399399

400400
/* Set PendSV to invoke ThreadX scheduler. */
401401
*((volatile ULONG *) 0xE000ED04) = ((ULONG) 0x10000000);
402+
__asm__ volatile ("dsb 0xF \n isb 0xF " : : : "memory");
402403
if (__get_IPSR() == 0)
403404
{
404405
interrupt_save = __get_interrupt_state();

ports/cortex_m23/iar/src/tx_thread_system_return.s

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
/***************************************************************************
2-
* Copyright (c) 2024 Microsoft Corporation
3-
* Copyright (c) 2026-present Eclipse ThreadX contributors
4-
*
5-
* This program and the accompanying materials are made available under the
6-
* terms of the MIT License which is available at
7-
* https://opensource.org/licenses/MIT.
8-
*
9-
* SPDX-License-Identifier: MIT
10-
**************************************************************************/
1+
;***************************************************************************
2+
;* Copyright (c) 2024 Microsoft Corporation
3+
;* Copyright (c) 2026-present Eclipse ThreadX contributors
4+
;*
5+
;* This program and the accompanying materials are made available under the
6+
;* terms of the MIT License which is available at
7+
;* https://opensource.org/licenses/MIT.
8+
;*
9+
;* SPDX-License-Identifier: MIT
10+
;**************************************************************************
1111

12+
; Some portions generated by Gemini (Gemini 2.0 Flash).
13+
14+
15+
;**************************************************************************
1216

13-
/**************************************************************************/
1417
/**************************************************************************/
1518
/** */
1619
/** ThreadX Component */
@@ -65,6 +68,8 @@ _tx_thread_system_return:
6568
LDR r0, =0x10000000 // Load PENDSVSET bit
6669
LDR r1, =0xE000ED04 // Load ICSR address
6770
STR r0, [r1] // Set PENDSVBIT in ICSR
71+
DSB SY // Ensure memory access is complete
72+
ISB SY // Flush pipeline
6873
MRS r0, IPSR // Pickup IPSR
6974
CMP r0, #0 // Is it a thread returning?
7075
BNE _isr_context // If ISR, skip interrupt enable

ports/cortex_m3/iar/inc/tx_port.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* SPDX-License-Identifier: MIT
1010
**************************************************************************/
1111

12+
// Some portions generated by Gemini (Gemini 2.0 Flash).
13+
1214

1315
/**************************************************************************/
1416
/**************************************************************************/
@@ -539,20 +541,20 @@ unsigned int posture;
539541
#ifdef TX_PORT_USE_BASEPRI
540542
__attribute__( ( always_inline ) ) static inline void __set_basepri_value(unsigned int basepri_value)
541543
{
542-
__asm__ volatile ("MSR BASEPRI,%0 ": : "r" (basepri_value));
544+
__asm__ volatile ("MSR BASEPRI,%0 ": : "r" (basepri_value) : "memory");
543545
}
544-
#else
546+
#endif
547+
545548
__attribute__( ( always_inline ) ) static inline void __enable_interrupts(void)
546549
{
547550
__asm__ volatile ("CPSIE i": : : "memory");
548551
}
549-
#endif
550552

551553
__attribute__( ( always_inline ) ) static inline void __restore_interrupt(unsigned int int_posture)
552554
{
553555
#ifdef TX_PORT_USE_BASEPRI
554556
__set_basepri_value(int_posture);
555-
//__asm__ volatile ("MSR BASEPRI,%0": : "r" (int_posture): "memory");
557+
__asm__ volatile ("" : : : "memory");
556558
#else
557559
__asm__ volatile ("MSR PRIMASK,%0": : "r" (int_posture): "memory");
558560
#endif
@@ -578,6 +580,7 @@ unsigned int interrupt_save;
578580

579581
/* Set PendSV to invoke ThreadX scheduler. */
580582
*((volatile ULONG *) 0xE000ED04) = ((ULONG) 0x10000000);
583+
__asm__ volatile ("dsb 0xF \n isb 0xF " : : : "memory");
581584
if (__get_ipsr_value() == 0)
582585
{
583586
interrupt_save = __get_interrupt_posture();
@@ -589,7 +592,6 @@ unsigned int interrupt_save;
589592
__restore_interrupt(interrupt_save);
590593
}
591594
}
592-
593595
#define TX_INTERRUPT_SAVE_AREA UINT interrupt_save;
594596
#define TX_DISABLE interrupt_save = __disable_interrupts();
595597
#define TX_RESTORE __restore_interrupt(interrupt_save);

ports/cortex_m3/iar/src/tx_thread_system_return.s

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
/***************************************************************************
2-
* Copyright (c) 2024 Microsoft Corporation
3-
* Copyright (c) 2026-present Eclipse ThreadX contributors
4-
*
5-
* This program and the accompanying materials are made available under the
6-
* terms of the MIT License which is available at
7-
* https://opensource.org/licenses/MIT.
8-
*
9-
* SPDX-License-Identifier: MIT
10-
**************************************************************************/
1+
;***************************************************************************
2+
;* Copyright (c) 2024 Microsoft Corporation
3+
;* Copyright (c) 2026-present Eclipse ThreadX contributors
4+
;*
5+
;* This program and the accompanying materials are made available under the
6+
;* terms of the MIT License which is available at
7+
;* https://opensource.org/licenses/MIT.
8+
;*
9+
;* SPDX-License-Identifier: MIT
10+
;**************************************************************************
1111

12+
; Some portions generated by Gemini (Gemini 2.0 Flash).
13+
14+
15+
;**************************************************************************
1216

13-
/**************************************************************************/
1417
/**************************************************************************/
1518
/** */
1619
/** ThreadX Component */
@@ -70,6 +73,8 @@ _tx_thread_system_return:
7073
MOV r0, #0x10000000 // Load PENDSVSET bit
7174
MOV r1, #0xE000E000 // Load NVIC base
7275
STR r0, [r1, #0xD04] // Set PENDSVBIT in ICSR
76+
DSB SY // Ensure memory access is complete
77+
ISB SY // Flush pipeline
7378
MRS r0, IPSR // Pickup IPSR
7479
CMP r0, #0 // Is it a thread returning?
7580
BNE _isr_context // If ISR, skip interrupt enable

ports/cortex_m4/iar/inc/tx_port.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* SPDX-License-Identifier: MIT
1010
**************************************************************************/
1111

12+
// Some portions generated by Gemini (Gemini 2.0 Flash).
13+
1214

1315
/**************************************************************************/
1416
/**************************************************************************/
@@ -539,20 +541,20 @@ unsigned int posture;
539541
#ifdef TX_PORT_USE_BASEPRI
540542
__attribute__( ( always_inline ) ) static inline void __set_basepri_value(unsigned int basepri_value)
541543
{
542-
__asm__ volatile ("MSR BASEPRI,%0 ": : "r" (basepri_value));
544+
__asm__ volatile ("MSR BASEPRI,%0 ": : "r" (basepri_value) : "memory");
543545
}
544-
#else
546+
#endif
547+
545548
__attribute__( ( always_inline ) ) static inline void __enable_interrupts(void)
546549
{
547550
__asm__ volatile ("CPSIE i": : : "memory");
548551
}
549-
#endif
550552

551553
__attribute__( ( always_inline ) ) static inline void __restore_interrupt(unsigned int int_posture)
552554
{
553555
#ifdef TX_PORT_USE_BASEPRI
554556
__set_basepri_value(int_posture);
555-
//__asm__ volatile ("MSR BASEPRI,%0": : "r" (int_posture): "memory");
557+
__asm__ volatile ("" : : : "memory");
556558
#else
557559
__asm__ volatile ("MSR PRIMASK,%0": : "r" (int_posture): "memory");
558560
#endif
@@ -578,6 +580,7 @@ unsigned int interrupt_save;
578580

579581
/* Set PendSV to invoke ThreadX scheduler. */
580582
*((volatile ULONG *) 0xE000ED04) = ((ULONG) 0x10000000);
583+
__asm__ volatile ("dsb 0xF \n isb 0xF " : : : "memory");
581584
if (__get_ipsr_value() == 0)
582585
{
583586
interrupt_save = __get_interrupt_posture();
@@ -589,7 +592,6 @@ unsigned int interrupt_save;
589592
__restore_interrupt(interrupt_save);
590593
}
591594
}
592-
593595
#define TX_INTERRUPT_SAVE_AREA UINT interrupt_save;
594596
#define TX_DISABLE interrupt_save = __disable_interrupts();
595597
#define TX_RESTORE __restore_interrupt(interrupt_save);

ports/cortex_m4/iar/src/tx_thread_system_return.s

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
/***************************************************************************
2-
* Copyright (c) 2024 Microsoft Corporation
3-
* Copyright (c) 2026-present Eclipse ThreadX contributors
4-
*
5-
* This program and the accompanying materials are made available under the
6-
* terms of the MIT License which is available at
7-
* https://opensource.org/licenses/MIT.
8-
*
9-
* SPDX-License-Identifier: MIT
10-
**************************************************************************/
1+
;***************************************************************************
2+
;* Copyright (c) 2024 Microsoft Corporation
3+
;* Copyright (c) 2026-present Eclipse ThreadX contributors
4+
;*
5+
;* This program and the accompanying materials are made available under the
6+
;* terms of the MIT License which is available at
7+
;* https://opensource.org/licenses/MIT.
8+
;*
9+
;* SPDX-License-Identifier: MIT
10+
;**************************************************************************
1111

12+
; Some portions generated by Gemini (Gemini 2.0 Flash).
13+
14+
15+
;**************************************************************************
1216

13-
/**************************************************************************/
1417
/**************************************************************************/
1518
/** */
1619
/** ThreadX Component */
@@ -70,6 +73,8 @@ _tx_thread_system_return:
7073
MOV r0, #0x10000000 // Load PENDSVSET bit
7174
MOV r1, #0xE000E000 // Load NVIC base
7275
STR r0, [r1, #0xD04] // Set PENDSVBIT in ICSR
76+
DSB SY // Ensure memory access is complete
77+
ISB SY // Flush pipeline
7378
MRS r0, IPSR // Pickup IPSR
7479
CMP r0, #0 // Is it a thread returning?
7580
BNE _isr_context // If ISR, skip interrupt enable

ports/cortex_m55/iar/inc/tx_port.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ UINT interrupt_save;
579579

580580
/* Set PendSV to invoke ThreadX scheduler. */
581581
*((volatile ULONG *) 0xE000ED04) = ((ULONG) 0x10000000);
582+
__asm__ volatile ("dsb 0xF \n isb 0xF " : : : "memory");
582583
if (_tx_ipsr_get() == 0)
583584
{
584585
interrupt_save = __get_interrupt_posture();

ports/cortex_m55/iar/src/tx_thread_system_return.s

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
/***************************************************************************
2-
* Copyright (c) 2024 Microsoft Corporation
3-
* Copyright (c) 2026-present Eclipse ThreadX contributors
4-
*
5-
* This program and the accompanying materials are made available under the
6-
* terms of the MIT License which is available at
7-
* https://opensource.org/licenses/MIT.
8-
*
9-
* SPDX-License-Identifier: MIT
10-
**************************************************************************/
1+
;***************************************************************************
2+
;* Copyright (c) 2024 Microsoft Corporation
3+
;* Copyright (c) 2026-present Eclipse ThreadX contributors
4+
;*
5+
;* This program and the accompanying materials are made available under the
6+
;* terms of the MIT License which is available at
7+
;* https://opensource.org/licenses/MIT.
8+
;*
9+
;* SPDX-License-Identifier: MIT
10+
;**************************************************************************
1111

12+
; Some portions generated by Gemini (Gemini 2.0 Flash).
13+
14+
15+
;**************************************************************************
1216

13-
/**************************************************************************/
1417
/**************************************************************************/
1518
/** */
1619
/** ThreadX Component */
@@ -69,6 +72,8 @@ _tx_thread_system_return:
6972
MOV r0, #0x10000000 // Load PENDSVSET bit
7073
MOV r1, #0xE000E000 // Load NVIC base
7174
STR r0, [r1, #0xD04] // Set PENDSVBIT in ICSR
75+
DSB SY // Ensure memory access is complete
76+
ISB SY // Flush pipeline
7277
MRS r0, IPSR // Pickup IPSR
7378
CMP r0, #0 // Is it a thread returning?
7479
BNE _isr_context // If ISR, skip interrupt enable

0 commit comments

Comments
 (0)