arm: nxp: mpu: Fix off-by-1 error in region index calculation
Both the ARM and NXP MPU drivers incorrectly calculated the region index
by assuming the region type (e.g., THREAD_STACK_GUARD_REGION) was
zero-indexed, when in reality it is one-indexed. This had the effect of
wasting one region.
Signed-off-by: Maureen Helm <maureen.helm@nxp.com>
diff --git a/arch/arm/core/cortex_m/mpu/arm_mpu.c b/arch/arm/core/cortex_m/mpu/arm_mpu.c
index 1568fcc..dda4466 100644
--- a/arch/arm/core/cortex_m/mpu/arm_mpu.c
+++ b/arch/arm/core/cortex_m/mpu/arm_mpu.c
@@ -108,10 +108,12 @@
{
SYS_LOG_DBG("Region info: 0x%x 0x%x", base, size);
/*
- * The new MPU regions are are allocated per type after the statically
- * configured regions.
+ * The new MPU regions are allocated per type after the statically
+ * configured regions. The type is one-indexed rather than
+ * zero-indexed, therefore we need to subtract by one to get the region
+ * index.
*/
- u32_t region_index = mpu_config.num_regions + type;
+ u32_t region_index = mpu_config.num_regions + type - 1;
u32_t region_attr = _get_region_attr_by_type(type, size);
/* ARM MPU supports up to 16 Regions */
diff --git a/arch/arm/core/cortex_m/mpu/nxp_mpu.c b/arch/arm/core/cortex_m/mpu/nxp_mpu.c
index 26312b1..54a638a 100644
--- a/arch/arm/core/cortex_m/mpu/nxp_mpu.c
+++ b/arch/arm/core/cortex_m/mpu/nxp_mpu.c
@@ -120,10 +120,12 @@
{
SYS_LOG_DBG("Region info: 0x%x 0x%x", base, size);
/*
- * The new MPU regions are are allocated per type after the statically
- * configured regions.
+ * The new MPU regions are allocated per type after the statically
+ * configured regions. The type is one-indexed rather than
+ * zero-indexed, therefore we need to subtract by one to get the region
+ * index.
*/
- u32_t region_index = mpu_config.num_regions + type;
+ u32_t region_index = mpu_config.num_regions + type - 1;
u32_t region_attr = _get_region_attr_by_type(type);
u32_t last_region = _get_num_regions() - 1;