From 42036cdbcae270d00b5bd08e805e33bbd3b5787f Mon Sep 17 00:00:00 2001 From: Wilfried Chauveau Date: Tue, 30 Jan 2024 12:33:31 +0000 Subject: [PATCH] arch: arm: cortex_m: Only trigger context switch if thread is preemptible This is a fix for #61761 where a cooperative task is switched from at the end of an exception. A cooperative thread should only be switched from if the thread exists the ready state. Signed-off-by: Wilfried Chauveau --- arch/arm/core/cortex_m/exc_exit.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/arch/arm/core/cortex_m/exc_exit.c b/arch/arm/core/cortex_m/exc_exit.c index a919fd8bbd..450b397236 100644 --- a/arch/arm/core/cortex_m/exc_exit.c +++ b/arch/arm/core/cortex_m/exc_exit.c @@ -55,8 +55,13 @@ FUNC_ALIAS(z_arm_exc_exit, z_arm_int_exit, void); Z_GENERIC_SECTION(.text._HandlerModeExit) void z_arm_exc_exit(void) { #ifdef CONFIG_PREEMPT_ENABLED - if (_kernel.ready_q.cache != _kernel.cpus->current) { - SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk; + /* If thread is preemptible */ + if (_kernel.cpus->current->base.prio >= 0) { + /* and cached thread is not current thread */ + if (_kernel.ready_q.cache != _kernel.cpus->current) { + /* trigger a context switch */ + SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk; + } } #endif /* CONFIG_PREEMPT_ENABLED */