Skip to content

Commit d018d84

Browse files
committed
extmod/zephyr_kernel: Override arch_irq_lock to use PRIMASK for complete interrupt masking.
Zephyr's default arch_irq_lock() uses BASEPRI register on Cortex-M4/M7, which masks only interrupts with priority >= threshold (0x10). This allows priority 0 interrupts to fire during critical sections, potentially corrupting thread state during mutex operations, context switches, and scheduler operations. This commit overrides arch_irq_lock() and arch_irq_unlock() to use PRIMASK register, which masks ALL configurable interrupts (except NMI and HardFault). Implementation: - Created arch_irq_primask.h header with PRIMASK-based implementations - Included after Zephyr headers to override BASEPRI versions - Uses __get_PRIMASK/__disable_irq for complete masking - Matches MicroPython's disable_irq() approach in ports/stm32/irq.h Trade-offs: - Slightly longer interrupt latency for priority 0 interrupts during critical sections - Eliminates race conditions from high-priority interrupts during critical sections - More conservative approach: complete masking vs threshold-based masking Testing: 23/34 thread tests passing (same as before). Override doesn't improve pass rate but provides architectural correctness - eliminates potential corruption vector from priority 0 interrupts. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent 68ed70c commit d018d84

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

extmod/zephyr_kernel/arch/cortex_m/cortex_m_arch.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ extern int mp_printf(const mp_print_t *print, const char *fmt, ...);
4747
#include <zephyr/kernel_structs.h>
4848
#include <zephyr/arch/cpu.h>
4949

50+
// Override arch_irq_lock() to use PRIMASK for complete interrupt masking
51+
// Must be included AFTER Zephyr headers to override BASEPRI-based implementation
52+
#include "arch_irq_primask.h"
53+
5054
// Forward declarations for Zephyr functions we provide
5155
void sys_clock_announce(int32_t ticks);
5256

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2025 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_EXTMOD_ZEPHYR_KERNEL_ARCH_IRQ_PRIMASK_H
27+
#define MICROPY_INCLUDED_EXTMOD_ZEPHYR_KERNEL_ARCH_IRQ_PRIMASK_H
28+
29+
// Override Zephyr's BASEPRI-based arch_irq_lock() with PRIMASK-based version
30+
// for complete interrupt masking in critical sections.
31+
//
32+
// ISSUE: Zephyr's default arch_irq_lock() uses BASEPRI register on Cortex-M4/M7,
33+
// which masks only interrupts with priority >= threshold (0x10). This allows
34+
// priority 0 interrupts to fire during critical sections, potentially corrupting
35+
// thread state during mutex operations, context switches, and scheduler operations.
36+
//
37+
// FIX: Use PRIMASK register which masks ALL configurable interrupts (except NMI
38+
// and HardFault), providing complete interrupt masking in critical sections.
39+
//
40+
// TRADE-OFF: Slightly longer interrupt latency for priority 0 interrupts during
41+
// critical sections, but eliminates race conditions and memory corruption.
42+
43+
#undef arch_irq_lock
44+
#undef arch_irq_unlock
45+
46+
static ALWAYS_INLINE unsigned int arch_irq_lock(void) {
47+
unsigned int key = __get_PRIMASK();
48+
__disable_irq(); // Sets PRIMASK=1, disables ALL configurable interrupts
49+
return key;
50+
}
51+
52+
static ALWAYS_INLINE void arch_irq_unlock(unsigned int key) {
53+
if (key != 0U) {
54+
return; // Interrupts were already disabled, don't re-enable
55+
}
56+
__enable_irq(); // Sets PRIMASK=0, enables interrupts
57+
__ISB();
58+
}
59+
60+
#endif // MICROPY_INCLUDED_EXTMOD_ZEPHYR_KERNEL_ARCH_IRQ_PRIMASK_H

0 commit comments

Comments
 (0)