diff --git a/arch/arm/src/imxrt/imxrt_allocateheap.c b/arch/arm/src/imxrt/imxrt_allocateheap.c index b4fa3e6ad7d..333804c8635 100644 --- a/arch/arm/src/imxrt/imxrt_allocateheap.c +++ b/arch/arm/src/imxrt/imxrt_allocateheap.c @@ -36,6 +36,7 @@ #include +#include "mpu.h" #include "arm_internal.h" #include "hardware/imxrt_memorymap.h" #include "imxrt_mpuinit.h" @@ -309,14 +310,30 @@ void up_allocate_heap(void **heap_start, size_t *heap_size) uintptr_t ubase = (uintptr_t)USERSPACE->us_bssend + CONFIG_MM_KERNEL_HEAPSIZE; size_t usize = PRIMARY_RAM_END - ubase; + int log2; DEBUGASSERT(ubase < (uintptr_t)PRIMARY_RAM_END); + /* Adjust that size to account for MPU alignment requirements. + * NOTE that there is an implicit assumption that the PRIMARY_RAM_END + * is aligned to the MPU requirement. + */ + + log2 = (int)mpu_log2regionfloor(usize); + DEBUGASSERT((PRIMARY_RAM_END & ((1 << log2) - 1)) == 0); + + usize = (1 << log2); + ubase = PRIMARY_RAM_END - usize; + /* Return the user-space heap settings */ board_autoled_on(LED_HEAPALLOCATE); *heap_start = (void *)ubase; *heap_size = usize; + + /* Allow user-mode access to the user heap memory */ + + imxrt_mpu_uheap((uintptr_t)ubase, usize); #else /* Return the heap settings */ @@ -348,8 +365,21 @@ void up_allocate_kheap(void **heap_start, size_t *heap_size) uintptr_t ubase = (uintptr_t)USERSPACE->us_bssend + CONFIG_MM_KERNEL_HEAPSIZE; + size_t usize = PRIMARY_RAM_END - ubase; + int log2; DEBUGASSERT(ubase < (uintptr_t)PRIMARY_RAM_END); + /* Adjust that size to account for MPU alignment requirements. + * NOTE that there is an implicit assumption that the CONFIG_RAM_END + * is aligned to the MPU requirement. + */ + + log2 = (int)mpu_log2regionfloor(usize); + DEBUGASSERT((PRIMARY_RAM_END & ((1 << log2) - 1)) == 0); + + usize = (1 << log2); + ubase = PRIMARY_RAM_END - usize; + /* Return the kernel heap settings (i.e., the part of the heap region * that was not dedicated to the user heap). */ diff --git a/arch/arm/src/imxrt/imxrt_mpuinit.c b/arch/arm/src/imxrt/imxrt_mpuinit.c index dc03caabe6e..5f8db353ca4 100644 --- a/arch/arm/src/imxrt/imxrt_mpuinit.c +++ b/arch/arm/src/imxrt/imxrt_mpuinit.c @@ -91,6 +91,10 @@ void imxrt_mpu_initialize(void) mpu_showtype(); + /* Reset MPU if enabled */ + + mpu_reset(); + #ifdef CONFIG_ARMV7M_DCACHE /* Memory barrier */