diff --git a/arch/xtensa/core/xtensa-asm2.c b/arch/xtensa/core/xtensa-asm2.c index 3e31dcaa939f59b..a38493d106d922b 100644 --- a/arch/xtensa/core/xtensa-asm2.c +++ b/arch/xtensa/core/xtensa-asm2.c @@ -255,6 +255,16 @@ void *xtensa_excint1_c(int *interrupted_stack) reason = bsa[BSA_A2_OFF/4]; /* Skip ILL to RETW */ bsa[BSA_PC_OFF/4] += 3; + /* This would ensure nested count is reset to one when an exception + * is triggered from ISR context. When nested count <=1 the handler would + * switch the thread context, otherwise would return the interrupted stack. + * This would cause xtensa_arch_except to return when it is not expected to. + * Resetting the cpu nested count prevents xtensa_arch_except from returning + * in ISR context. + */ + if ((arch_curr_cpu()->nested > 1) && (reason == K_ERR_STACK_CHK_FAIL)) { + arch_curr_cpu()->nested = 1U; + } pc = (void *)bsa[BSA_PC_OFF/4]; } diff --git a/include/zephyr/arch/xtensa/arch.h b/include/zephyr/arch/xtensa/arch.h index 5b5a90c029f5fe6..18b2a76f61ebeeb 100644 --- a/include/zephyr/arch/xtensa/arch.h +++ b/include/zephyr/arch/xtensa/arch.h @@ -48,6 +48,7 @@ extern void xtensa_arch_except(int reason_p); #define ARCH_EXCEPT(reason_p) do { \ xtensa_arch_except(reason_p); \ + CODE_UNREACHABLE; \ } while (false) /* internal routine documented in C file, needed by IRQ_CONNECT() macro */