From 0a8ad73564420e115649c95a1c1a85aff73de4d0 Mon Sep 17 00:00:00 2001 From: Maureen Helm Date: Thu, 8 Jun 2017 22:58:17 -0500 Subject: [PATCH] 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 --- arch/arm/core/cortex_m/mpu/arm_mpu.c | 8 +++++--- arch/arm/core/cortex_m/mpu/nxp_mpu.c | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/arch/arm/core/cortex_m/mpu/arm_mpu.c b/arch/arm/core/cortex_m/mpu/arm_mpu.c index 1568fcc1d2cada..dda44660ef7421 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 @@ void arm_core_mpu_configure(u8_t type, u32_t base, u32_t size) { 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 26312b197ce0b5..54a638abc81c69 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 @@ void arm_core_mpu_configure(u8_t type, u32_t base, u32_t size) { 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;