Skip to content

Commit 0a8ad73

Browse files
MaureenHelmgalak
authored andcommitted
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 <[email protected]>
1 parent 07e0a78 commit 0a8ad73

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

arch/arm/core/cortex_m/mpu/arm_mpu.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ void arm_core_mpu_configure(u8_t type, u32_t base, u32_t size)
108108
{
109109
SYS_LOG_DBG("Region info: 0x%x 0x%x", base, size);
110110
/*
111-
* The new MPU regions are are allocated per type after the statically
112-
* configured regions.
111+
* The new MPU regions are allocated per type after the statically
112+
* configured regions. The type is one-indexed rather than
113+
* zero-indexed, therefore we need to subtract by one to get the region
114+
* index.
113115
*/
114-
u32_t region_index = mpu_config.num_regions + type;
116+
u32_t region_index = mpu_config.num_regions + type - 1;
115117
u32_t region_attr = _get_region_attr_by_type(type, size);
116118

117119
/* ARM MPU supports up to 16 Regions */

arch/arm/core/cortex_m/mpu/nxp_mpu.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ void arm_core_mpu_configure(u8_t type, u32_t base, u32_t size)
120120
{
121121
SYS_LOG_DBG("Region info: 0x%x 0x%x", base, size);
122122
/*
123-
* The new MPU regions are are allocated per type after the statically
124-
* configured regions.
123+
* The new MPU regions are allocated per type after the statically
124+
* configured regions. The type is one-indexed rather than
125+
* zero-indexed, therefore we need to subtract by one to get the region
126+
* index.
125127
*/
126-
u32_t region_index = mpu_config.num_regions + type;
128+
u32_t region_index = mpu_config.num_regions + type - 1;
127129
u32_t region_attr = _get_region_attr_by_type(type);
128130
u32_t last_region = _get_num_regions() - 1;
129131

0 commit comments

Comments
 (0)