This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 0c45a2fc48b9930bf792b2332b30c44314fc05ee Author: buxiasen <[email protected]> AuthorDate: Tue Dec 23 12:29:05 2025 +0800 arch/arm-m: merge arm_stackcheck Merge armv7-m & armv8-m stack check Kconfig and source Signed-off-by: buxiasen <[email protected]> Signed-off-by: hujun5 <[email protected]> --- arch/arm/Kconfig | 12 ++- arch/arm/src/arm_m/CMakeLists.txt | 4 + arch/arm/src/arm_m/Make.defs | 4 + arch/arm/src/{armv8-m => arm_m}/arm_stackcheck.c | 8 +- arch/arm/src/armv7-m/CMakeLists.txt | 13 +-- arch/arm/src/armv7-m/Kconfig | 1 + arch/arm/src/armv7-m/Make.defs | 4 - arch/arm/src/armv7-m/arm_stackcheck.c | 130 ----------------------- arch/arm/src/armv8-m/CMakeLists.txt | 8 -- arch/arm/src/armv8-m/Kconfig | 1 + arch/arm/src/armv8-m/Make.defs | 8 -- arch/arm/src/common/arm_internal.h | 2 +- 12 files changed, 28 insertions(+), 167 deletions(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 38653c13372..08721f9fec6 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -1869,6 +1869,8 @@ config UNWINDER_ARM endchoice # Choose ARM unwinder +endif # SCHED_BACKTRACE + config ARM_STACKCHECK bool default n @@ -1886,7 +1888,15 @@ config ARM_STACKCHECK compile. This addition to your CFLAGS should probably be added to the definition of the CFFLAGS in your board Make.defs file. -endif +config ARM_STACKCHECK_BREAKPOINT + bool + default n + depends on ARM_STACKCHECK + ---help--- + If enabled, a hard-coded breakpoint will be inserted to the stack + overflow trap. This is useful to stop the execution of the program + and diagnose the issue before the hardfault handler is called (and + context information is lost). if ARCH_ARM_M source "arch/arm/src/arm_m/Kconfig" diff --git a/arch/arm/src/arm_m/CMakeLists.txt b/arch/arm/src/arm_m/CMakeLists.txt index bf5647d07e3..5d1cc1c086c 100644 --- a/arch/arm/src/arm_m/CMakeLists.txt +++ b/arch/arm/src/arm_m/CMakeLists.txt @@ -39,6 +39,10 @@ if(CONFIG_ARCH_ARMV8M) list(APPEND SRCS arm_securefault.c) endif() +if(CONFIG_ARM_STACKCHECK) + list(APPEND SRCS arm_stackcheck.c) +endif() + if(CONFIG_ARM_SYSTICK) list(APPEND SRCS arm_systick.c) endif() diff --git a/arch/arm/src/arm_m/Make.defs b/arch/arm/src/arm_m/Make.defs index 43740c8cd2b..05124bd292b 100644 --- a/arch/arm/src/arm_m/Make.defs +++ b/arch/arm/src/arm_m/Make.defs @@ -33,6 +33,10 @@ endif CMN_CSRCS += arm_tcbinfo.c arm_busfault.c arm_hardfault.c arm_initialstate.c CMN_CSRCS += arm_systemreset.c arm_trigger_irq.c arm_svcall.c +ifeq ($(CONFIG_ARM_STACKCHECK),y) + CMN_CSRCS += arm_stackcheck.c +endif + ifneq ($(CONFIG_ARCH_ARMV6M),y) CMN_CSRCS += arm_usagefault.c arm_memfault.c endif diff --git a/arch/arm/src/armv8-m/arm_stackcheck.c b/arch/arm/src/arm_m/arm_stackcheck.c similarity index 96% rename from arch/arm/src/armv8-m/arm_stackcheck.c rename to arch/arm/src/arm_m/arm_stackcheck.c index 26d79b0eacd..22e2f4680d8 100644 --- a/arch/arm/src/armv8-m/arm_stackcheck.c +++ b/arch/arm/src/arm_m/arm_stackcheck.c @@ -1,8 +1,8 @@ /**************************************************************************** - * arch/arm/src/armv8-m/arm_stackcheck.c + * arch/arm/src/arm_m/arm_stackcheck.c * * SPDX-License-Identifier: BSD-3-Clause - * SPDX-FileCopyrightText: 2013, 2014 PX4 Development Team. All rights reserved. + * SPDX-FileCopyrightText: 2013, 2014 PX4 Development Team * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -39,7 +39,7 @@ #include <nuttx/config.h> -#ifdef CONFIG_ARMV8M_STACKCHECK +#ifdef CONFIG_ARM_STACKCHECK /* Support per function call stack checking. * This code uses R10 to check for a stack overflow within function calls. @@ -80,7 +80,7 @@ void __stack_overflow_trap(void) uint32_t regval; -#ifdef CONFIG_ARMV8M_STACKCHECK_BREAKPOINT +#ifdef CONFIG_ARM_STACKCHECK_BREAKPOINT regval = getreg32(NVIC_DHCSR); if (regval & NVIC_DHCSR_C_DEBUGEN) { diff --git a/arch/arm/src/armv7-m/CMakeLists.txt b/arch/arm/src/armv7-m/CMakeLists.txt index 730e670b105..58b1de7eae4 100644 --- a/arch/arm/src/armv7-m/CMakeLists.txt +++ b/arch/arm/src/armv7-m/CMakeLists.txt @@ -22,22 +22,13 @@ # TODO: do not use config to detect this -set(SRCS - arm_dbgmonitor.c - arm_cache.c - arm_cpuinfo.c - arm_doirq.c - arm_itm.c - arm_perf.c) +set(SRCS arm_dbgmonitor.c arm_cache.c arm_cpuinfo.c arm_doirq.c arm_itm.c + arm_perf.c) if(CONFIG_ARMV7M_ITMSYSLOG) list(APPEND SRCS arm_itm_syslog.c) endif() -if(CONFIG_ARMV7M_STACKCHECK) - list(APPEND SRCS arm_stackcheck.c) -endif() - if(CONFIG_ARCH_FPU) list(APPEND SRCS arm_fpuconfig.c arm_fpucmp.c) endif() diff --git a/arch/arm/src/armv7-m/Kconfig b/arch/arm/src/armv7-m/Kconfig index c6047c829f0..58263cf3094 100644 --- a/arch/arm/src/armv7-m/Kconfig +++ b/arch/arm/src/armv7-m/Kconfig @@ -121,6 +121,7 @@ config ARMV7M_STACKCHECK_BREAKPOINT bool "Breakpoint on stack overflow" default n depends on ARMV7M_STACKCHECK + select ARM_STACKCHECK_BREAKPOINT ---help--- If enabled, a hard-coded breakpoint will be inserted to the stack overflow trap. This is useful to stop the execution of the program diff --git a/arch/arm/src/armv7-m/Make.defs b/arch/arm/src/armv7-m/Make.defs index 51d717eda51..6eca1d9606f 100644 --- a/arch/arm/src/armv7-m/Make.defs +++ b/arch/arm/src/armv7-m/Make.defs @@ -32,10 +32,6 @@ ifeq ($(CONFIG_ARMV7M_ITMSYSLOG),y) CMN_CSRCS += arm_itm_syslog.c endif -ifeq ($(CONFIG_ARMV7M_STACKCHECK),y) - CMN_CSRCS += arm_stackcheck.c -endif - ifeq ($(CONFIG_ARCH_FPU),y) CMN_CSRCS += arm_fpuconfig.c CMN_CSRCS += arm_fpucmp.c diff --git a/arch/arm/src/armv7-m/arm_stackcheck.c b/arch/arm/src/armv7-m/arm_stackcheck.c deleted file mode 100644 index 401964a35cf..00000000000 --- a/arch/arm/src/armv7-m/arm_stackcheck.c +++ /dev/null @@ -1,130 +0,0 @@ -/**************************************************************************** - * arch/arm/src/armv7-m/arm_stackcheck.c - * - * SPDX-License-Identifier: BSD-3-Clause - * SPDX-FileCopyrightText: 2013, 2014 PX4 Development Team. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name PX4 nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#ifdef CONFIG_ARMV7M_STACKCHECK - -/* Support per function call stack checking. - * This code uses R10 to check for a stack overflow within function calls. - * This has a performance impact, but will be able to catch hard to find - * stack overflows. - */ - -#include <stdint.h> - -#include <nuttx/instrument.h> -#include "arm_internal.h" -#include "nvic.h" - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -static void stack_check_enter(void *func, void *caller, void *arg) - naked_function; -void __stack_overflow_trap(void) naked_function; - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -static struct instrument_s g_stack_check = -{ - .enter = stack_check_enter, -}; - -/**************************************************************************** - * Name: __stack_overflow_trap - ****************************************************************************/ - -void __stack_overflow_trap(void) -{ - /* if we get here, the stack has overflowed */ - - uint32_t regval; - -#ifdef CONFIG_ARMV7M_STACKCHECK_BREAKPOINT - regval = getreg32(NVIC_DHCSR); - if (regval & NVIC_DHCSR_C_DEBUGEN) - { - __asm("bkpt 1"); - } -#endif - - /* force hard fault */ - - regval = getreg32(NVIC_INTCTRL); - regval |= NVIC_INTCTRL_NMIPENDSET; - putreg32(regval, NVIC_INTCTRL); - - /* XXX no need to trap it here, the fault handler gets to it */ -} - -/**************************************************************************** - * Name: stack_check_enter - ****************************************************************************/ - -static void stack_check_enter(void *func, void *caller, void *arg) -{ - asm volatile ( - " mrs r2, ipsr \n" /* Check whether we are in interrupt mode */ - " cmp r2, #0 \n" /* since we don't switch r10 on interrupt entry, we */ - " bne 2f \n" /* can't detect overflow of the interrupt stack. */ - " \n" - " sub r2, sp, #68 \n" /* compute stack pointer as though we just stacked a full frame */ - " mrs r1, control \n" /* Test CONTROL.FPCA to see whether we also need room for the FP */ - " tst r1, #4 \n" /* context. */ - " beq 1f \n" - " sub r2, r2, #136 \n" /* subtract FP context frame size */ - "1: \n" - " cmp r2, r10 \n" /* compare stack with limit */ - " bgt 2f \n" /* stack is above limit and thus OK */ - " mov r11, r2 \n" /* fault with R11 set to calculated low */ - " b __stack_overflow_trap\n" - "2: \n" - " bx lr \n" - ); -} - -void noinstrument_function arm_stack_check_init(void) -{ - instrument_register(&g_stack_check); -} -#endif diff --git a/arch/arm/src/armv8-m/CMakeLists.txt b/arch/arm/src/armv8-m/CMakeLists.txt index ad5e1e3635f..1ca02b1ae2b 100644 --- a/arch/arm/src/armv8-m/CMakeLists.txt +++ b/arch/arm/src/armv8-m/CMakeLists.txt @@ -34,10 +34,6 @@ if(CONFIG_ARMV8M_ITMSYSLOG) list(APPEND SRCS arm_itm_syslog.c) endif() -if(CONFIG_ARMV8M_STACKCHECK) - list(APPEND SRCS arm_stackcheck.c) -endif() - if(CONFIG_ARCH_FPU) list(APPEND SRCS arm_fpuconfig.c arm_fpucmp.c) endif() @@ -50,10 +46,6 @@ if(CONFIG_ARM_MPU OR CONFIG_ARM_MPU_EARLY_RESET) list(APPEND SRCS arm_mpu.c) endif() -if(CONFIG_ARCH_TRUSTZONE_SECURE) - list(APPEND SRCS arm_gen_nonsecfault.c) -endif() - if(CONFIG_ARCH_ADDRENV) list(APPEND SRCS arm_addrenv.c) endif() diff --git a/arch/arm/src/armv8-m/Kconfig b/arch/arm/src/armv8-m/Kconfig index b272aad01d0..9dc4b4c0c2c 100644 --- a/arch/arm/src/armv8-m/Kconfig +++ b/arch/arm/src/armv8-m/Kconfig @@ -122,6 +122,7 @@ config ARMV8M_STACKCHECK_BREAKPOINT bool "Breakpoint on stack overflow" default n depends on ARMV8M_STACKCHECK + select ARM_STACKCHECK_BREAKPOINT ---help--- If enabled, a hard-coded breakpoint will be inserted to the stack overflow trap. This is useful to stop the execution of the program diff --git a/arch/arm/src/armv8-m/Make.defs b/arch/arm/src/armv8-m/Make.defs index 49c806ad91d..f90cfa7abce 100644 --- a/arch/arm/src/armv8-m/Make.defs +++ b/arch/arm/src/armv8-m/Make.defs @@ -33,10 +33,6 @@ ifeq ($(CONFIG_ARMV8M_ITMSYSLOG),y) CMN_CSRCS += arm_itm_syslog.c endif -ifeq ($(CONFIG_ARMV8M_STACKCHECK),y) - CMN_CSRCS += arm_stackcheck.c -endif - ifeq ($(CONFIG_ARCH_FPU),y) CMN_CSRCS += arm_fpuconfig.c CMN_CSRCS += arm_fpucmp.c @@ -50,10 +46,6 @@ ifneq ($(filter y,$(CONFIG_ARM_MPU) $(CONFIG_ARM_MPU_EARLY_RESET)),) CMN_CSRCS += arm_mpu.c endif -ifeq ($(CONFIG_ARCH_TRUSTZONE_SECURE),y) - CMN_CSRCS += arm_gen_nonsecfault.c -endif - ifeq ($(CONFIG_ARCH_ADDRENV),y) CMN_CSRCS += arm_addrenv.c endif diff --git a/arch/arm/src/common/arm_internal.h b/arch/arm/src/common/arm_internal.h index bf7eb0efd9e..2a9656be444 100644 --- a/arch/arm/src/common/arm_internal.h +++ b/arch/arm/src/common/arm_internal.h @@ -525,7 +525,7 @@ int arm_gen_nonsecurefault(int irq, uint32_t *regs); # define arm_gen_nonsecurefault(i, r) (0) #endif -#if defined(CONFIG_ARMV7M_STACKCHECK) || defined(CONFIG_ARMV8M_STACKCHECK) +#ifdef CONFIG_ARM_STACKCHECK void arm_stack_check_init(void) noinstrument_function; #endif
