Skip to content

[arm][cortex-m4] Add hardware stack guard support - #11740

Open
Zepp-Hanzj wants to merge 3 commits into
RT-Thread:masterfrom
Zepp-Hanzj:agent/cortex-m4-hw-stack-guard
Open

[arm][cortex-m4] Add hardware stack guard support#11740
Zepp-Hanzj wants to merge 3 commits into
RT-Thread:masterfrom
Zepp-Hanzj:agent/cortex-m4-hw-stack-guard

Conversation

@Zepp-Hanzj

Copy link
Copy Markdown
Contributor

Description / 描述

为 Cortex-M4 补齐 RT_USING_HW_STACK_GUARD 硬件栈保护支持。此前该能力仅 Cortex-M7 和 Cortex-M33 可用,M4(尤其是大量 STM32F4 BSP)缺失。

Add hardware stack guard support for Cortex-M4, which was previously only available on Cortex-M7 and Cortex-M33.

Why / 为什么

栈溢出是嵌入式系统最高频、最隐蔽的故障之一。Cortex-M4 同样具备 MPU,但 RT-Thread 的 mprotect 框架尚未在 M4 上落地 rt_hw_stack_guard_init

Stack overflow is one of the most common and hardest-to-debug faults in embedded systems. Cortex-M4 has an MPU, but the mprotect framework has not yet provided rt_hw_stack_guard_init on M4.

Modified Files / 修改文件

核心移植 core porting (libcpu/arm/cortex-m4/):

  • mpu.c / mpu.h / mputype.h(新增):对齐 Cortex-M7 的 MPU 抽象层。唯一差异在默认内存类型属性——M4 无 L1 cache,cacheability 位简化为三种情况。
  • cpuport.c:新增 rt_hw_stack_guard_init()
  • context_gcc.S:补上缺失的 #include <rtconfig.h>,并在 PendSV 上下文切换路径中调用 rt_hw_mpu_table_switch()
  • SConscript:未开启内存保护时排除 mpu.c

BSP 参考实现 (bsp/stm32/stm32f407-fk407m2-zgt6/board/):

  • board.h:include rtthread.h,定义 NUM_STATIC_REGIONS#ifdef 保护)。
  • board.c:定义 static_regions[](Flash 区域只读,#ifdef 保护,不默认开启)。

Verification / 验证

  • 编译通过(STM32F407ZGT6 开启/关闭 RT_USING_HW_STACK_GUARD 两种配置均通过;frdm-k64f 无回归)。
  • 真机验证:STM32F407ZGT6 上创建小栈线程递归溢出,栈溢出被 MPU 保护区有效拦截。

@github-actions

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • Use workflow from 保持默认分支(通常为 master
    Keep the default branch (usually master) in Use workflow from
  • branch 输入框填写 PR 分支 agent/cortex-m4-hw-stack-guard
    Enter PR branch agent/cortex-m4-hw-stack-guard in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 agent/cortex-m4-hw-stack-guard 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the agent/cortex-m4-hw-stack-guard branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions github-actions Bot added BSP: STM32 BSP related with ST/STM32 BSP libcpu labels Aug 24, 2026
@github-actions

github-actions Bot commented Aug 24, 2026

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: bsp_stm32

Reviewers: @Liang1795 @hamburger-os @wdfk-prog

Changed Files (Click to expand)
  • bsp/stm32/stm32f407-fk407m2-zgt6/board/board.c
  • bsp/stm32/stm32f407-fk407m2-zgt6/board/board.h

📊 Current Review Status (Last Updated: 2026-08-24 13:51 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

Enable RT_USING_HW_STACK_GUARD on Cortex-M4, which was previously only
available on Cortex-M7 and Cortex-M33.

Core porting (libcpu/arm/cortex-m4/):
- Add mpu.c/mpu.h/mputype.h aligned with the Cortex-M7 MPU layer. The only
  difference is the default memory type attribute: Cortex-M4 has no L1 cache,
  so the cacheability bits are reduced to three cases.
- Add rt_hw_stack_guard_init() to cpuport.c.
- Add missing #include <rtconfig.h> to context_gcc.S and invoke
  rt_hw_mpu_table_switch() in the PendSV context switch path.
- Update SConscript to exclude mpu.c when memory protection is disabled.

BSP reference (bsp/stm32/stm32f407-fk407m2-zgt6/):
- board.h: include rtthread.h and define NUM_STATIC_REGIONS.
- board.c: define static_regions[] marking the Flash region read-only,
  guarded by RT_USING_MEM_PROTECTION so it is not enabled by default.
@Zepp-Hanzj
Zepp-Hanzj force-pushed the agent/cortex-m4-hw-stack-guard branch from d1ff4f6 to 07b426d Compare August 24, 2026 03:09
@github-actions

github-actions Bot commented Aug 24, 2026

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Cortex-M4 support for RT-Thread’s RT_USING_HW_STACK_GUARD (hardware stack overflow protection) by introducing an MPU backend aligned with existing Cortex-M7 / Cortex-M33 implementations, and wiring MPU table switching into the PendSV context switch path.
本次 PR 为 Cortex-M4 补齐 RT_USING_HW_STACK_GUARD(硬件栈保护)能力:新增与 Cortex-M7/M33 对齐的 MPU 后端,并在 PendSV 上下文切换路径中切换线程对应的 MPU 表。

Changes / 变更点:

  • Add Cortex-M4 MPU abstraction + implementation (mpu.c/.h, mputype.h) to support memory protection and stack guard.
    新增 Cortex-M4 的 MPU 抽象与实现(mpu.c/.h, mputype.h),用于内存保护与栈保护。
  • Add rt_hw_stack_guard_init() for Cortex-M4 and integrate MPU table switching into context_gcc.S PendSV path.
    Cortex-M4 增加 rt_hw_stack_guard_init(),并在 context_gcc.S 的 PendSV 路径中调用 rt_hw_mpu_table_switch()
  • Provide an STM32F407 BSP reference static MPU region definition (Flash RX) guarded by RT_USING_MEM_PROTECTION.
    提供 STM32F407 BSP 参考实现:在 RT_USING_MEM_PROTECTION 条件下定义静态 MPU 区域(Flash 只读可执行)。

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
libcpu/arm/cortex-m4/SConscript Excludes mpu.c when neither mem protection nor stack guard is enabled
libcpu/arm/cortex-m4/mputype.h Adds Cortex-M4 rt_mem_attr_t and dynamic region count definitions
libcpu/arm/cortex-m4/mpu.h Adds Cortex-M4 MPU permission/type macros and MPU APIs
libcpu/arm/cortex-m4/mpu.c Implements MPU init/region ops/table switch + MemManage handler for Cortex-M4
libcpu/arm/cortex-m4/cpuport.c Adds rt_hw_stack_guard_init() and adjusts formatting in several areas
libcpu/arm/cortex-m4/context_gcc.S Includes rtconfig.h and switches MPU table during PendSV context restore
bsp/stm32/stm32f407-fk407m2-zgt6/board/board.h Adds NUM_STATIC_REGIONS definition under mem protection
bsp/stm32/stm32f407-fk407m2-zgt6/board/board.c Adds static_regions[] (Flash RX) under mem protection

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread libcpu/arm/cortex-m4/cpuport.c Outdated
Comment on lines +612 to +616
__asm int __rt_ffs(int value)
{
CMP r0, #0x00
BEQ exit
CMP r0, #0x00 BEQ exit

RBIT r0, r0
CLZ r0, r0
ADDS r0, r0, #0x01
RBIT r0,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修复。Fixed.

The __rt_ffs inline assembly has been restored to the correct ARMCC syntax (same as the Cortex-M7 implementation), and wrapped with // clang-format off / // clang-format on to prevent the automatic clang-format workflow from corrupting it again.

已将 __rt_ffs 内联汇编恢复为正确的 ARMCC 语法(与 Cortex-M7 实现一致),并用 // clang-format off / // clang-format on 包裹,防止格式化工作流再次破坏。

Restore the ARMCC __rt_ffs inline assembly that was corrupted by the
automatic clang-format commit, which merged multiple instructions onto one
line and split labels across lines, making it invalid ARMCC syntax.

Wrap the block with // clang-format off/on to prevent future formatting
runs from breaking it again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BSP: STM32 BSP related with ST/STM32 BSP libcpu

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants