You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/04/13 04:20:29 UTC

[incubator-nuttx] 02/02: arch/arm/src/stm32f7/stm32_allocateheap.c: Fix MPU alignments

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 3f6bb76e01b356bc9d7852fe2b900d367e3deea9
Author: Jukka Laitinen <ju...@ssrc.tii.ae>
AuthorDate: Fri Feb 5 16:41:25 2021 +0200

    arch/arm/src/stm32f7/stm32_allocateheap.c: Fix MPU alignments
    
    Change the logic for allocating user heap for PROTECTED_BUILD:
    - Don't rely on SRAM1_END alignment
    - Make better use of MPU subregions when allocating the heap
    - Don't duplicate the calculation of user heap start in kernel heap
      allocation; use the previous calculation directly
    
    Signed-off-by: Jukka Laitinen <ju...@ssrc.tii.ae>
---
 arch/arm/src/stm32/stm32_allocateheap.c   |  2 --
 arch/arm/src/stm32f7/stm32_allocateheap.c | 45 +++++++++++++------------------
 2 files changed, 19 insertions(+), 28 deletions(-)

diff --git a/arch/arm/src/stm32/stm32_allocateheap.c b/arch/arm/src/stm32/stm32_allocateheap.c
index 9d56750..eeb0f0f 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 3757eb2..df9705b 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 */
+
+  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 = (1 << log2);
-  ubase = SRAM1_END - usize;
+  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