diff --git a/arch/arm/src/stm32/stm32_allocateheap.c b/arch/arm/src/stm32/stm32_allocateheap.c index 9d56750c60d..eeb0f0fd97c 100644 --- a/arch/arm/src/stm32/stm32_allocateheap.c +++ b/arch/arm/src/stm32/stm32_allocateheap.c @@ -677,7 +677,6 @@ void up_allocate_heap(FAR void **heap_start, size_t *heap_size) */ log2 = (int)mpu_log2regionfloor(usize); - DEBUGASSERT((SRAM1_END & ((1 << log2) - 1)) == 0); usize = (1 << log2); ubase = SRAM1_END - usize; @@ -740,7 +739,6 @@ void up_allocate_kheap(FAR void **heap_start, size_t *heap_size) */ log2 = (int)mpu_log2regionfloor(usize); - DEBUGASSERT((SRAM1_END & ((1 << log2) - 1)) == 0); usize = (1 << log2); ubase = SRAM1_END - usize; diff --git a/arch/arm/src/stm32f7/stm32_allocateheap.c b/arch/arm/src/stm32f7/stm32_allocateheap.c index 3757eb2a146..df9705b5cbc 100644 --- a/arch/arm/src/stm32f7/stm32_allocateheap.c +++ b/arch/arm/src/stm32f7/stm32_allocateheap.c @@ -273,20 +273,29 @@ void up_allocate_heap(FAR void **heap_start, size_t *heap_size) uintptr_t ubase = (uintptr_t)USERSPACE->us_bssend + CONFIG_MM_KERNEL_HEAPSIZE; size_t usize = SRAM1_END - ubase; + size_t subreg_mask; int log2; - DEBUGASSERT(ubase < (uintptr_t)SRAM1_END); - /* Adjust that size to account for MPU alignment requirements. * NOTE that there is an implicit assumption that the SRAM1_END * is aligned to the MPU requirement. */ - log2 = (int)mpu_log2regionfloor(usize); - DEBUGASSERT((SRAM1_END & ((1 << log2) - 1)) == 0); + /* align the ubase initially to a suitable mpu subregion start */ - usize = (1 << log2); - ubase = SRAM1_END - usize; + log2 = (int)mpu_log2regionceil(usize); + subreg_mask = (1 << log2) / 8 - 1; + if (ubase & subreg_mask) + { + ubase = (ubase | subreg_mask) + 1; + } + + DEBUGASSERT(ubase < (uintptr_t)SRAM1_END); + + /* reset the size to fit in SRAM and align down to subregion size */ + + usize = SRAM1_END - ubase; + usize &= ~subreg_mask; /* Return the user-space heap settings */ @@ -328,35 +337,19 @@ void up_allocate_heap(FAR void **heap_start, size_t *heap_size) #if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP) void up_allocate_kheap(FAR void **heap_start, size_t *heap_size) { - /* Get the unaligned size and position of the user-space heap. - * This heap begins after the user-space .bss section at an offset - * of CONFIG_MM_KERNEL_HEAPSIZE (subject to alignment). + /* User heap was just initialized, with proper MPU alignment, in nx_start, + * store the user heap start address */ - uintptr_t ubase = (uintptr_t)USERSPACE->us_bssend + - CONFIG_MM_KERNEL_HEAPSIZE; - size_t usize = SRAM1_END - ubase; - int log2; - + uintptr_t ubase = (uintptr_t)*heap_start; DEBUGASSERT(ubase < (uintptr_t)SRAM1_END); - /* Adjust that size to account for MPU alignment requirements. - * NOTE that there is an implicit assumption that the SRAM1_END - * is aligned to the MPU requirement. - */ - - log2 = (int)mpu_log2regionfloor(usize); - DEBUGASSERT((SRAM1_END & ((1 << log2) - 1)) == 0); - - usize = (1 << log2); - ubase = SRAM1_END - usize; - /* Return the kernel heap settings (i.e., the part of the heap region * that was not dedicated to the user heap). */ *heap_start = (FAR void *)USERSPACE->us_bssend; - *heap_size = ubase - (uintptr_t)USERSPACE->us_bssend; + *heap_size = ubase - USERSPACE->us_bssend; } #endif