summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorhanzhijian <[email protected]>2026-08-28 17:55:10 +0800
committerRbb666 <[email protected]>2026-09-07 10:31:49 +0800
commit2c989025432d9f8b482d3979402b96bbe0e69e51 (patch)
treec821f29c79a2e5c920d2fcb6a477d6eab30fb280 /components
parent3d3648ce2cbf2bb4367de150a8dc4c1dc49698e5 (diff)
[mprotect][kconfig] Add safe defaults for MPU region count options
NUM_MEM_REGIONS / NUM_CONFIGURABLE_REGIONS / NUM_EXCLUSIVE_REGIONS had no Kconfig defaults. Enabling RT_USING_HW_STACK_GUARD without explicitly setting them generated empty macros in rtconfig.h (e.g. '#define NUM_CONFIGURABLE_REGIONS'), which expanded into invalid C expressions like '(2 + )' and 'for (i = 0U; i < ; i++)', breaking compilation for cortex-m4/m7/m33 MPU code. Add safe defaults: NUM_MEM_REGIONS=8, NUM_CONFIGURABLE_REGIONS=0, NUM_EXCLUSIVE_REGIONS=0. Also document the MemManage_Handler strong-symbol conflict with CubeMX-generated stm32f4xx_it.c and how to resolve it. Reported-by: wdfk-prog
Diffstat (limited to 'components')
-rw-r--r--components/mprotect/Kconfig8
-rw-r--r--components/mprotect/README.md12
2 files changed, 20 insertions, 0 deletions
diff --git a/components/mprotect/Kconfig b/components/mprotect/Kconfig
index 82348cbd9b..5b6bd8e11a 100644
--- a/components/mprotect/Kconfig
+++ b/components/mprotect/Kconfig
@@ -17,12 +17,20 @@ if RT_USING_MEM_PROTECTION
config NUM_MEM_REGIONS
int "Total number of memory protection regions supported by hardware"
+ default 8
+ help
+ The number of MPU regions supported by the target MCU. The common value
+ for Cortex-M4/M7/M33 is 8, but some MCUs expose 4, 12, or 16 regions.
+ Set this to the actual value reported by the hardware, otherwise the MPU
+ initialization will reject the configuration at runtime.
config NUM_EXCLUSIVE_REGIONS
int "Total number of exclusive memory regions added using rt_mprotect_add_exclusive_region API"
+ default 0
config NUM_CONFIGURABLE_REGIONS
int "Maximum number of configurable memory regions for each thread, excluding stack guard and exclusive regions added using rt_mprotect_add_exclusive_region API"
+ default 0
endif
endmenu
diff --git a/components/mprotect/README.md b/components/mprotect/README.md
index 73c2671b10..e4d9718c91 100644
--- a/components/mprotect/README.md
+++ b/components/mprotect/README.md
@@ -83,6 +83,18 @@ rt_mem_region_t static_regions[NUM_STATIC_REGIONS] = {
- 如果开启了`RT_USING_HW_STACK_GUARD`:`NUM_STATIC_REGIONS` + `NUM_CONFIGURABLE_REGIONS` + `NUM_EXCLUSIVE_REGIONS` + 2 <= `NUM_MEM_REGIONS`
- 如果没有开启`RT_USING_HW_STACK_GUARD`:`NUM_STATIC_REGIONS` + `NUM_CONFIGURABLE_REGIONS` + `NUM_EXCLUSIVE_REGIONS` <= `NUM_MEM_REGIONS`
+`NUM_MEM_REGIONS` / `NUM_CONFIGURABLE_REGIONS` / `NUM_EXCLUSIVE_REGIONS` 在 Kconfig 中均已提供默认值(分别为 `8` / `0` / `0`),即使不显式配置也不会生成空宏导致编译失败。其中 `NUM_MEM_REGIONS` 取决于具体 MCU,请务必按目标芯片实际的 MPU region 数量修改,否则 MPU 初始化时会因配置值与硬件不符而报错。
+
+## STM32 CubeMX 集成注意事项
+如果从 STM32 CubeMX 生成的工程集成,需要注意 `stm32f4xx_it.c`(或对应型号的 `stm32xxx_it.c`)中由 CubeMX 默认生成的 `MemManage_Handler` 会与本组件冲突:
+
+- `libcpu/arm/cortex-m4/mpu.c`(以及 `cortex-m7`、`cortex-m33` 的 `mpu.c`)已经提供了 `MemManage_Handler`,用于读取 `CFSR`/`MMFAR`、定位 MPU region 并调用异常钩子;
+- CubeMX 生成的 `stm32f4xx_it.c` 同样包含一个强符号的 `MemManage_Handler`(默认为 `while(1)` 死循环),两者同时参与链接会报 `multiple definition of 'MemManage_Handler'`。
+
+解决方法(任选其一):
+1. 确保 `stm32f4xx_it.c` 不参与编译(RT-Thread 官方 STM32 BSP 默认如此);
+2. 若确需编译 `stm32f4xx_it.c`,删除其中 CubeMX 生成的 `MemManage_Handler` 函数定义。
+
## 异常检测
程序可以注册钩子函数,用来检测内存异常:
```