]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/DxeServicesLib: introduce AllocatePeiAccessiblePages routine
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Thu, 24 May 2018 08:44:01 +0000 (10:44 +0200)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Tue, 29 May 2018 08:47:18 +0000 (10:47 +0200)
Add a routine to DxeServicesLib that abstracts the allocation of memory
that should be accessible by PEI after resuming from S3. We will use it
to replace open coded implementations that limit the address to < 4 GB,
which may not be possible on non-Intel systems that have no 32-bit
addressable memory at all.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdePkg/Include/Library/DxeServicesLib.h
MdePkg/Library/DxeServicesLib/Allocate.c [new file with mode: 0644]
MdePkg/Library/DxeServicesLib/DxeServicesLib.inf
MdePkg/Library/DxeServicesLib/X64/Allocate.c [new file with mode: 0644]

index 7c1c62236d96df354b994d7e2c0f20fea49789fd..20aee68af55821507f982de312a99a7d786bb97a 100644 (file)
@@ -305,5 +305,26 @@ GetFileDevicePathFromAnyFv (
   OUT       EFI_DEVICE_PATH_PROTOCOL  **FvFileDevicePath\r
   );\r
 \r
   OUT       EFI_DEVICE_PATH_PROTOCOL  **FvFileDevicePath\r
   );\r
 \r
-#endif\r
+/**\r
+  Allocates one or more 4KB pages of a given type from a memory region that is\r
+  accessible to PEI.\r
+\r
+  Allocates the number of 4KB pages of type 'MemoryType' and returns a\r
+  pointer to the allocated buffer.  The buffer returned is aligned on a 4KB\r
+  boundary.  If Pages is 0, then NULL is returned.  If there is not enough\r
+  memory remaining to satisfy the request, then NULL is returned.\r
 \r
 \r
+  @param[in]  MemoryType            The memory type to allocate\r
+  @param[in]  Pages                 The number of 4 KB pages to allocate.\r
+\r
+  @return A pointer to the allocated buffer or NULL if allocation fails.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+AllocatePeiAccessiblePages (\r
+  IN EFI_MEMORY_TYPE  MemoryType,\r
+  IN UINTN            Pages\r
+  );\r
+\r
+#endif\r
diff --git a/MdePkg/Library/DxeServicesLib/Allocate.c b/MdePkg/Library/DxeServicesLib/Allocate.c
new file mode 100644 (file)
index 0000000..4d118f7
--- /dev/null
@@ -0,0 +1,54 @@
+/** @file\r
+  DxeServicesLib memory allocation routines\r
+\r
+  Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>\r
+\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php.\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DxeServicesLib.h>\r
+\r
+/**\r
+  Allocates one or more 4KB pages of a given type from a memory region that is\r
+  accessible to PEI.\r
+\r
+  Allocates the number of 4KB pages of type 'MemoryType' and returns a\r
+  pointer to the allocated buffer.  The buffer returned is aligned on a 4KB\r
+  boundary.  If Pages is 0, then NULL is returned.  If there is not enough\r
+  memory remaining to satisfy the request, then NULL is returned.\r
+\r
+  @param[in]  MemoryType            The memory type to allocate\r
+  @param[in]  Pages                 The number of 4 KB pages to allocate.\r
+\r
+  @return A pointer to the allocated buffer or NULL if allocation fails.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+AllocatePeiAccessiblePages (\r
+  IN EFI_MEMORY_TYPE  MemoryType,\r
+  IN UINTN            Pages\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_PHYSICAL_ADDRESS        Memory;\r
+\r
+  if (Pages == 0) {\r
+    return NULL;\r
+  }\r
+\r
+  Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+  return (VOID *)(UINTN)Memory;\r
+}\r
index bd2faf2f6f2de8a9d56ed14348f1a24744691447..50ae24f8ee22341a653ba821e1ecb8ce86a255d4 100644 (file)
   LIBRARY_CLASS                  = DxeServicesLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SAL_DRIVER DXE_SMM_DRIVER SMM_CORE UEFI_APPLICATION UEFI_DRIVER\r
 \r
 #\r
   LIBRARY_CLASS                  = DxeServicesLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SAL_DRIVER DXE_SMM_DRIVER SMM_CORE UEFI_APPLICATION UEFI_DRIVER\r
 \r
 #\r
-#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
+#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC ARM AARCH64\r
 #\r
 \r
 [Sources]\r
   DxeServicesLib.c\r
 \r
 #\r
 \r
 [Sources]\r
   DxeServicesLib.c\r
 \r
+[Sources.IA32, Sources.IPF, Sources.EBC, Sources.ARM, Sources.AARCH64]\r
+  Allocate.c\r
+\r
+[Sources.X64]\r
+  X64/Allocate.c\r
+\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
 \r
@@ -44,6 +50,9 @@
   UefiLib\r
   UefiBootServicesTableLib\r
 \r
   UefiLib\r
   UefiBootServicesTableLib\r
 \r
+[LibraryClasses.X64]\r
+  HobLib\r
+\r
 [Guids]\r
   gEfiFileInfoGuid                              ## SOMETIMES_CONSUMES ## UNDEFINED\r
 \r
 [Guids]\r
   gEfiFileInfoGuid                              ## SOMETIMES_CONSUMES ## UNDEFINED\r
 \r
diff --git a/MdePkg/Library/DxeServicesLib/X64/Allocate.c b/MdePkg/Library/DxeServicesLib/X64/Allocate.c
new file mode 100644 (file)
index 0000000..b6d34ba
--- /dev/null
@@ -0,0 +1,69 @@
+/** @file\r
+  DxeServicesLib memory allocation routines\r
+\r
+  Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>\r
+\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php.\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+#include <Library/HobLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DxeServicesLib.h>\r
+\r
+/**\r
+  Allocates one or more 4KB pages of a given type from a memory region that is\r
+  accessible to PEI.\r
+\r
+  Allocates the number of 4KB pages of type 'MemoryType' and returns a\r
+  pointer to the allocated buffer.  The buffer returned is aligned on a 4KB\r
+  boundary.  If Pages is 0, then NULL is returned.  If there is not enough\r
+  memory remaining to satisfy the request, then NULL is returned.\r
+\r
+  @param[in]  MemoryType            The memory type to allocate\r
+  @param[in]  Pages                 The number of 4 KB pages to allocate.\r
+\r
+  @return A pointer to the allocated buffer or NULL if allocation fails.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+AllocatePeiAccessiblePages (\r
+  IN EFI_MEMORY_TYPE  MemoryType,\r
+  IN UINTN            Pages\r
+  )\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_ALLOCATE_TYPE           AllocType;\r
+  EFI_PHYSICAL_ADDRESS        Memory;\r
+  EFI_HOB_HANDOFF_INFO_TABLE  *PhitHob;\r
+\r
+  if (Pages == 0) {\r
+    return NULL;\r
+  }\r
+\r
+  AllocType = AllocateAnyPages;\r
+  //\r
+  // A X64 build of DXE may be combined with a 32-bit build of PEI, and so we\r
+  // need to check the memory limit set by PEI, and allocate below 4 GB if the\r
+  // limit is set to 4 GB or lower.\r
+  //\r
+  PhitHob = (EFI_HOB_HANDOFF_INFO_TABLE *)GetHobList ();\r
+  if (PhitHob->EfiFreeMemoryTop <= MAX_UINT32) {\r
+    AllocType = AllocateMaxAddress;\r
+    Memory = MAX_UINT32;\r
+  }\r
+\r
+  Status = gBS->AllocatePages (AllocType, MemoryType, Pages, &Memory);\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+  return (VOID *)(UINTN)Memory;\r
+}\r