Skip to content

Commit 6e2fbea

Browse files
mshawcroftgalak
authored andcommitted
kernel: Fix ARM irq_lock() ordering bug.
The inline asm definition of irq_lock() on the ARM architecture marks the ASM as volatile which prevents the compiler from removing the isntruction but does provide any information to the compiler to prevent the inline ASM instruction being re-ordered relative to other instructions. The instruction used in irq_lock() do not touch memory, however in order to acheive their intended purpose they must be ordered relative to other memory access instruction. This is acheived by adding the "memory" clobber. Instances of the compiler inappropriately re-ordering irq_lock() calls relative to other instructions without this patch can be observed in the code generated for k_sleep() on NRF51 target boards. Signed-off-by: Marcus Shawcroft <[email protected]> Change-Id: I9d42d54cd9a50e8150c10ce6715af7ca2f5cfe51 (cherry picked from commit 15bc537)
1 parent 9adaf59 commit 6e2fbea

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

include/arch/arm/cortex_m/asm_inline_gcc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,17 @@ static ALWAYS_INLINE unsigned int _arch_irq_lock(void)
131131
#if defined(CONFIG_CPU_CORTEX_M0_M0PLUS)
132132
__asm__ volatile("mrs %0, PRIMASK;\n\t"
133133
"cpsid i;\n\t"
134-
: "=r" (key));
134+
: "=r" (key)
135+
:
136+
: "memory");
135137
#else /* CONFIG_CPU_CORTEX_M3_M4 */
136138
__asm__ volatile(
137139
"movs.n %%r1, %1;\n\t"
138140
"mrs %0, BASEPRI;\n\t"
139141
"msr BASEPRI, %%r1;\n\t"
140142
: "=r"(key)
141143
: "i"(_EXC_IRQ_DEFAULT_PRIO)
142-
: "r1");
144+
: "r1", "memory");
143145
#endif
144146

145147
return key;

0 commit comments

Comments
 (0)