From 45f59a16abba9ba907d2f320bc446dee5b0adb23 Mon Sep 17 00:00:00 2001 From: seteiro <276597748+cms19859230182-lang@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:19:45 +0800 Subject: [libcpu][m23] select HardFault stack via EXC_RETURN --- libcpu/arm/cortex-m23/context_gcc.S | 8 +++++++- libcpu/arm/cortex-m23/context_iar.S | 8 +++++++- libcpu/arm/cortex-m23/context_rvds.S | 8 +++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/libcpu/arm/cortex-m23/context_gcc.S b/libcpu/arm/cortex-m23/context_gcc.S index abec76f934..03757a720b 100644 --- a/libcpu/arm/cortex-m23/context_gcc.S +++ b/libcpu/arm/cortex-m23/context_gcc.S @@ -198,7 +198,13 @@ rt_hw_interrupt_thread_switch: .type HardFault_Handler, %function HardFault_Handler: /* get current context */ - MRS R0, PSP /* get fault thread stack pointer */ + MRS R0, MSP /* get fault context from handler. */ + MOV R1, LR /* Cortex-M23 baseline has no Thumb-2 TST immediate */ + MOVS R2, #0x04 + TST R1, R2 /* if(!EXC_RETURN[2]) */ + BEQ _get_sp_done + MRS R0, PSP /* get fault context from thread. */ +_get_sp_done: PUSH {LR} BL rt_hw_hard_fault_exception POP {PC} diff --git a/libcpu/arm/cortex-m23/context_iar.S b/libcpu/arm/cortex-m23/context_iar.S index 7600ef3b77..fa5d568440 100644 --- a/libcpu/arm/cortex-m23/context_iar.S +++ b/libcpu/arm/cortex-m23/context_iar.S @@ -203,7 +203,13 @@ rt_hw_interrupt_thread_switch: HardFault_Handler: ; get current context - MRS r0, psp ; get fault thread stack pointer + MRS r0, msp ; get fault context from handler. + MOV r1, lr ; Cortex-M23 baseline has no Thumb-2 TST immediate + MOVS r2, #0x04 + TST r1, r2 ; if(!EXC_RETURN[2]) + BEQ _get_sp_done + MRS r0, psp ; get fault context from thread. +_get_sp_done PUSH {lr} BL rt_hw_hard_fault_exception POP {pc} diff --git a/libcpu/arm/cortex-m23/context_rvds.S b/libcpu/arm/cortex-m23/context_rvds.S index 38c3c36f31..884b16357b 100644 --- a/libcpu/arm/cortex-m23/context_rvds.S +++ b/libcpu/arm/cortex-m23/context_rvds.S @@ -209,7 +209,13 @@ HardFault_Handler PROC EXPORT HardFault_Handler ; get current context - MRS r0, psp ; get fault thread stack pointer + MRS r0, msp ; get fault context from handler. + MOV r1, lr ; Cortex-M23 baseline has no Thumb-2 TST immediate + MOVS r2, #0x04 + TST r1, r2 ; if(!EXC_RETURN[2]) + BEQ _get_sp_done + MRS r0, psp ; get fault context from thread. +_get_sp_done PUSH {lr} BL rt_hw_hard_fault_exception POP {pc} -- cgit v1.3.1