[edk2] [PATCH v2] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity

Ard Biesheuvel posted 1 patch 7 years ago
Failed in applying to current master (apply log)
There is a newer version of this series
MdeModulePkg/Core/Pei/Memory/MemoryServices.c | 21 ++++++++++++++++++--
MdeModulePkg/Core/Pei/PeiMain.h               | 18 +++++++++++++++++
2 files changed, 37 insertions(+), 2 deletions(-)
[edk2] [PATCH v2] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity
Posted by Ard Biesheuvel 7 years ago
Architectures such as AArch64 may run the OS with 16 KB or 64 KB sized
pages, and for this reason, the UEFI spec mandates a minimal allocation
granularity of 64 KB for regions that may require different memory
attributes at OS runtime.

So make PeiCore's implementation of AllocatePages () take this into
account as well.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 MdeModulePkg/Core/Pei/Memory/MemoryServices.c | 21 ++++++++++++++++++--
 MdeModulePkg/Core/Pei/PeiMain.h               | 18 +++++++++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
index 4efe14313ca5..cc78ca2a1625 100644
--- a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
+++ b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
@@ -140,6 +140,7 @@ PeiAllocatePages (
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryTop;
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryBottom;
   UINTN                                   RemainingPages;
+  UINTN                                   Granularity;
 
   if ((MemoryType != EfiLoaderCode) &&
       (MemoryType != EfiLoaderData) &&
@@ -153,6 +154,20 @@ PeiAllocatePages (
     return EFI_INVALID_PARAMETER;
   }
 
+  Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
+
+  if  (RUNTIME_PAGE_ALLOCATION_GRANULARITY > DEFAULT_PAGE_ALLOCATION_GRANULARITY &&
+       (MemoryType == EfiACPIReclaimMemory   ||
+        MemoryType == EfiACPIMemoryNVS       ||
+        MemoryType == EfiRuntimeServicesCode ||
+        MemoryType == EfiRuntimeServicesData)) {
+
+    Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
+
+    DEBUG ((DEBUG_INFO, "AllocatePages: rounding up allocation to %d KB\n",
+      Granularity));
+  }
+
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
   Hob.Raw     = PrivateData->HobList.Raw;
   
@@ -176,9 +191,10 @@ PeiAllocatePages (
   }
 
   //
-  // Check to see if on 4k boundary, If not aligned, make the allocation aligned.
+  // Check to see if on correct boundary for the memory type.
+  // If not aligned, make the allocation aligned.
   //
-  *(FreeMemoryTop) -= *(FreeMemoryTop) & 0xFFF;
+  *(FreeMemoryTop) -= *(FreeMemoryTop) & (Granularity - 1);
 
   //
   // Verify that there is sufficient memory to satisfy the allocation.
@@ -192,6 +208,7 @@ PeiAllocatePages (
   //
   // The number of remaining pages needs to be greater than or equal to that of the request pages.
   //
+  Pages = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity));
   if (RemainingPages < Pages) {
     DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64) Pages));
     DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64) RemainingPages));
diff --git a/MdeModulePkg/Core/Pei/PeiMain.h b/MdeModulePkg/Core/Pei/PeiMain.h
index 69eea514920b..e8358d3c4e6d 100644
--- a/MdeModulePkg/Core/Pei/PeiMain.h
+++ b/MdeModulePkg/Core/Pei/PeiMain.h
@@ -55,6 +55,24 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 ///
 #define PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE   0xff
 
+#if defined (MDE_CPU_AARCH64)
+///
+/// 64-bit ARM systems allow the OS to execute with 64 KB page size,
+/// so for improved interoperability with the firmware, align the
+/// runtime regions to 64 KB as well
+///
+#define RUNTIME_PAGE_ALLOCATION_GRANULARITY       (SIZE_64KB)
+#define DEFAULT_PAGE_ALLOCATION_GRANULARITY       (EFI_PAGE_SIZE)
+
+#else
+///
+/// For generic EFI machines make the default allocations 4K aligned
+///
+#define RUNTIME_PAGE_ALLOCATION_GRANULARITY       (EFI_PAGE_SIZE)
+#define DEFAULT_PAGE_ALLOCATION_GRANULARITY       (EFI_PAGE_SIZE)
+
+#endif
+
 ///
 /// Pei Core private data structures
 ///
-- 
2.7.4

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [PATCH v2] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity
Posted by Gao, Liming 7 years ago
Ard:
  First is to make FreeMemoryTop at the expected alignment. If original FreeMemoryTop is not aligned, it will be adjusted to be align. Then, there is one memory hole that can't be used any more. I suggest to create BuildMemoryAllocationHob() with EfiConventionalMemory to describe this hole.  Now, MdePkg\Library\PeiMemoryAllocationLib AllocateAlignedPages() uses this way to describe extra memory space. 

Thanks
Liming
>-----Original Message-----
>From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Ard
>Biesheuvel
>Sent: Friday, March 03, 2017 4:38 PM
>To: edk2-devel@lists.01.org; Gao, Liming <liming.gao@intel.com>; Zeng, Star
><star.zeng@intel.com>
>Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
>Subject: [edk2] [PATCH v2] MdeModulePkg/PeiCore: honour minimal runtime
>allocation granularity
>
>Architectures such as AArch64 may run the OS with 16 KB or 64 KB sized
>pages, and for this reason, the UEFI spec mandates a minimal allocation
>granularity of 64 KB for regions that may require different memory
>attributes at OS runtime.
>
>So make PeiCore's implementation of AllocatePages () take this into
>account as well.
>
>Contributed-under: TianoCore Contribution Agreement 1.0
>Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
>---
> MdeModulePkg/Core/Pei/Memory/MemoryServices.c | 21
>++++++++++++++++++--
> MdeModulePkg/Core/Pei/PeiMain.h               | 18 +++++++++++++++++
> 2 files changed, 37 insertions(+), 2 deletions(-)
>
>diff --git a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
>b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
>index 4efe14313ca5..cc78ca2a1625 100644
>--- a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
>+++ b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
>@@ -140,6 +140,7 @@ PeiAllocatePages (
>   EFI_PHYSICAL_ADDRESS                    *FreeMemoryTop;
>   EFI_PHYSICAL_ADDRESS                    *FreeMemoryBottom;
>   UINTN                                   RemainingPages;
>+  UINTN                                   Granularity;
>
>   if ((MemoryType != EfiLoaderCode) &&
>       (MemoryType != EfiLoaderData) &&
>@@ -153,6 +154,20 @@ PeiAllocatePages (
>     return EFI_INVALID_PARAMETER;
>   }
>
>+  Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
>+
>+  if  (RUNTIME_PAGE_ALLOCATION_GRANULARITY >
>DEFAULT_PAGE_ALLOCATION_GRANULARITY &&
>+       (MemoryType == EfiACPIReclaimMemory   ||
>+        MemoryType == EfiACPIMemoryNVS       ||
>+        MemoryType == EfiRuntimeServicesCode ||
>+        MemoryType == EfiRuntimeServicesData)) {
>+
>+    Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
>+
>+    DEBUG ((DEBUG_INFO, "AllocatePages: rounding up allocation to %d
>KB\n",
>+      Granularity));
>+  }
>+
>   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
>   Hob.Raw     = PrivateData->HobList.Raw;
>
>@@ -176,9 +191,10 @@ PeiAllocatePages (
>   }
>
>   //
>-  // Check to see if on 4k boundary, If not aligned, make the allocation aligned.
>+  // Check to see if on correct boundary for the memory type.
>+  // If not aligned, make the allocation aligned.
>   //
>-  *(FreeMemoryTop) -= *(FreeMemoryTop) & 0xFFF;
>+  *(FreeMemoryTop) -= *(FreeMemoryTop) & (Granularity - 1);
>
>   //
>   // Verify that there is sufficient memory to satisfy the allocation.
>@@ -192,6 +208,7 @@ PeiAllocatePages (
>   //
>   // The number of remaining pages needs to be greater than or equal to that
>of the request pages.
>   //
>+  Pages = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity));
>   if (RemainingPages < Pages) {
>     DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is
>available.\n", (UINT64) Pages));
>     DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource to
>be allocated.\n", (UINT64) RemainingPages));
>diff --git a/MdeModulePkg/Core/Pei/PeiMain.h
>b/MdeModulePkg/Core/Pei/PeiMain.h
>index 69eea514920b..e8358d3c4e6d 100644
>--- a/MdeModulePkg/Core/Pei/PeiMain.h
>+++ b/MdeModulePkg/Core/Pei/PeiMain.h
>@@ -55,6 +55,24 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY
>KIND, EITHER EXPRESS OR IMPLIED.
> ///
> #define PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE   0xff
>
>+#if defined (MDE_CPU_AARCH64)
>+///
>+/// 64-bit ARM systems allow the OS to execute with 64 KB page size,
>+/// so for improved interoperability with the firmware, align the
>+/// runtime regions to 64 KB as well
>+///
>+#define RUNTIME_PAGE_ALLOCATION_GRANULARITY       (SIZE_64KB)
>+#define DEFAULT_PAGE_ALLOCATION_GRANULARITY       (EFI_PAGE_SIZE)
>+
>+#else
>+///
>+/// For generic EFI machines make the default allocations 4K aligned
>+///
>+#define RUNTIME_PAGE_ALLOCATION_GRANULARITY       (EFI_PAGE_SIZE)
>+#define DEFAULT_PAGE_ALLOCATION_GRANULARITY       (EFI_PAGE_SIZE)
>+
>+#endif
>+
> ///
> /// Pei Core private data structures
> ///
>--
>2.7.4
>
>_______________________________________________
>edk2-devel mailing list
>edk2-devel@lists.01.org
>https://lists.01.org/mailman/listinfo/edk2-devel
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [PATCH v2] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity
Posted by Ard Biesheuvel 7 years ago
On 3 March 2017 at 09:05, Gao, Liming <liming.gao@intel.com> wrote:
> Ard:
>   First is to make FreeMemoryTop at the expected alignment. If original FreeMemoryTop is not aligned, it will be adjusted to be align. Then, there is one memory hole that can't be used any more. I suggest to create BuildMemoryAllocationHob() with EfiConventionalMemory to describe this hole.

OK. But the original code may already result in some non-page aligned
memory to be discarded, should that be described as well? Or should
the EfiConventionalMemory HOB cover full pages only?

Now, MdePkg\Library\PeiMemoryAllocationLib AllocateAlignedPages() uses
this way to describe extra memory space.

Thanks,
Ard.
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [PATCH v2] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity
Posted by Zeng, Star 7 years ago
According to PI Spec.
"Note:MemoryBaseAddress and MemoryLength must each have 4-KiB granularity to meet the page size requirements of UEFI."

Memory allocation HOB covers full pages only.

Thanks,
Star

-----Original Message-----
From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org] 
Sent: Friday, March 3, 2017 5:12 PM
To: Gao, Liming <liming.gao@intel.com>
Cc: edk2-devel@lists.01.org; Zeng, Star <star.zeng@intel.com>
Subject: Re: [edk2] [PATCH v2] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity

On 3 March 2017 at 09:05, Gao, Liming <liming.gao@intel.com> wrote:
> Ard:
>   First is to make FreeMemoryTop at the expected alignment. If original FreeMemoryTop is not aligned, it will be adjusted to be align. Then, there is one memory hole that can't be used any more. I suggest to create BuildMemoryAllocationHob() with EfiConventionalMemory to describe this hole.

OK. But the original code may already result in some non-page aligned memory to be discarded, should that be described as well? Or should the EfiConventionalMemory HOB cover full pages only?

Now, MdePkg\Library\PeiMemoryAllocationLib AllocateAlignedPages() uses this way to describe extra memory space.

Thanks,
Ard.
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel