public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ard.biesheuvel@linaro.org>
To: edk2-devel@lists.01.org, liming.gao@intel.com,
	michael.d.kinney@intel.com
Cc: star.zeng@intel.com, jiewen.yao@intel.com,
	Ard Biesheuvel <ard.biesheuvel@linaro.org>
Subject: [PATCH v4 4/4] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity
Date: Fri,  3 Mar 2017 15:11:34 +0000	[thread overview]
Message-ID: <1488553894-3520-5-git-send-email-ard.biesheuvel@linaro.org> (raw)
In-Reply-To: <1488553894-3520-1-git-send-email-ard.biesheuvel@linaro.org>

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>
Reviewed-by: Star Zeng <star.zeng@intel.com>
---
 MdeModulePkg/Core/Pei/Memory/MemoryServices.c | 39 +++++++++++++++++++-
 1 file changed, 37 insertions(+), 2 deletions(-)

diff --git a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
index 4efe14313ca5..719372e06128 100644
--- a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
+++ b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c
@@ -140,6 +140,8 @@ PeiAllocatePages (
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryTop;
   EFI_PHYSICAL_ADDRESS                    *FreeMemoryBottom;
   UINTN                                   RemainingPages;
+  UINTN                                   Granularity;
+  UINTN                                   Padding;
 
   if ((MemoryType != EfiLoaderCode) &&
       (MemoryType != EfiLoaderData) &&
@@ -153,6 +155,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: aligning allocation to %d KB\n",
+      Granularity / SIZE_1KB));
+  }
+
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);
   Hob.Raw     = PrivateData->HobList.Raw;
   
@@ -176,9 +192,27 @@ 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;
+  Padding = *(FreeMemoryTop) & (Granularity - 1);
+  if ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom) < Padding) {
+    DEBUG ((DEBUG_ERROR, "AllocatePages failed: Out of space after padding.\n"));
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  *(FreeMemoryTop) -= Padding;
+  if (Padding >= EFI_PAGE_SIZE) {
+    //
+    // Create a memory allocation HOB to cover
+    // the pages that we will lose to rounding
+    //
+    BuildMemoryAllocationHob (
+      *(FreeMemoryTop),
+      Padding & ~(UINTN)EFI_PAGE_MASK,
+      EfiConventionalMemory
+      );
+  }
 
   //
   // Verify that there is sufficient memory to satisfy the allocation.
@@ -192,6 +226,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));
-- 
2.7.4



  parent reply	other threads:[~2017-03-03 15:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-03 15:11 [PATCH v4 0/4] MdePkg, MdeModulePkg: Enforce page allocation granularity Ard Biesheuvel
2017-03-03 15:11 ` [PATCH v4 1/4] MdePkg/ProcessorBind: add defines for " Ard Biesheuvel
2017-03-03 15:11 ` [PATCH v4 2/4] MdeModulePkg/DxeCore: switch to MdePkg allocation granularity macros Ard Biesheuvel
2017-03-03 15:11 ` [PATCH v4 3/4] MdeModulePkg/PiSmmCore: " Ard Biesheuvel
2017-03-03 15:11 ` Ard Biesheuvel [this message]
2017-03-03 15:31 ` [PATCH v4 0/4] MdePkg, MdeModulePkg: Enforce page allocation granularity Yao, Jiewen
2017-03-06  2:33 ` Gao, Liming
2017-03-06 15:14   ` Ard Biesheuvel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1488553894-3520-5-git-send-email-ard.biesheuvel@linaro.org \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox