MdePkg/DxeServicesLib: introduce AllocatePeiAccessiblePages routine

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>
This commit is contained in:
Ard Biesheuvel 2018-05-24 10:44:01 +02:00
parent 97399f2d6f
commit a40e0b7aa9
4 changed files with 155 additions and 2 deletions

View File

@ -305,5 +305,26 @@ GetFileDevicePathFromAnyFv (
OUT EFI_DEVICE_PATH_PROTOCOL **FvFileDevicePath
);
#endif
/**
Allocates one or more 4KB pages of a given type from a memory region that is
accessible to PEI.
Allocates the number of 4KB pages of type 'MemoryType' and returns a
pointer to the allocated buffer. The buffer returned is aligned on a 4KB
boundary. If Pages is 0, then NULL is returned. If there is not enough
memory remaining to satisfy the request, then NULL is returned.
@param[in] MemoryType The memory type to allocate
@param[in] Pages The number of 4 KB pages to allocate.
@return A pointer to the allocated buffer or NULL if allocation fails.
**/
VOID *
EFIAPI
AllocatePeiAccessiblePages (
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN Pages
);
#endif

View File

@ -0,0 +1,54 @@
/** @file
DxeServicesLib memory allocation routines
Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available
under the terms and conditions of the BSD License which accompanies this
distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <PiDxe.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DxeServicesLib.h>
/**
Allocates one or more 4KB pages of a given type from a memory region that is
accessible to PEI.
Allocates the number of 4KB pages of type 'MemoryType' and returns a
pointer to the allocated buffer. The buffer returned is aligned on a 4KB
boundary. If Pages is 0, then NULL is returned. If there is not enough
memory remaining to satisfy the request, then NULL is returned.
@param[in] MemoryType The memory type to allocate
@param[in] Pages The number of 4 KB pages to allocate.
@return A pointer to the allocated buffer or NULL if allocation fails.
**/
VOID *
EFIAPI
AllocatePeiAccessiblePages (
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN Pages
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Memory;
if (Pages == 0) {
return NULL;
}
Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
if (EFI_ERROR (Status)) {
return NULL;
}
return (VOID *)(UINTN)Memory;
}

View File

@ -27,12 +27,18 @@
LIBRARY_CLASS = DxeServicesLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SAL_DRIVER DXE_SMM_DRIVER SMM_CORE UEFI_APPLICATION UEFI_DRIVER
#
# VALID_ARCHITECTURES = IA32 X64 IPF EBC
# VALID_ARCHITECTURES = IA32 X64 IPF EBC ARM AARCH64
#
[Sources]
DxeServicesLib.c
[Sources.IA32, Sources.IPF, Sources.EBC, Sources.ARM, Sources.AARCH64]
Allocate.c
[Sources.X64]
X64/Allocate.c
[Packages]
MdePkg/MdePkg.dec
@ -44,6 +50,9 @@
UefiLib
UefiBootServicesTableLib
[LibraryClasses.X64]
HobLib
[Guids]
gEfiFileInfoGuid ## SOMETIMES_CONSUMES ## UNDEFINED

View File

@ -0,0 +1,69 @@
/** @file
DxeServicesLib memory allocation routines
Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available
under the terms and conditions of the BSD License which accompanies this
distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <PiDxe.h>
#include <Library/HobLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DxeServicesLib.h>
/**
Allocates one or more 4KB pages of a given type from a memory region that is
accessible to PEI.
Allocates the number of 4KB pages of type 'MemoryType' and returns a
pointer to the allocated buffer. The buffer returned is aligned on a 4KB
boundary. If Pages is 0, then NULL is returned. If there is not enough
memory remaining to satisfy the request, then NULL is returned.
@param[in] MemoryType The memory type to allocate
@param[in] Pages The number of 4 KB pages to allocate.
@return A pointer to the allocated buffer or NULL if allocation fails.
**/
VOID *
EFIAPI
AllocatePeiAccessiblePages (
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN Pages
)
{
EFI_STATUS Status;
EFI_ALLOCATE_TYPE AllocType;
EFI_PHYSICAL_ADDRESS Memory;
EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;
if (Pages == 0) {
return NULL;
}
AllocType = AllocateAnyPages;
//
// A X64 build of DXE may be combined with a 32-bit build of PEI, and so we
// need to check the memory limit set by PEI, and allocate below 4 GB if the
// limit is set to 4 GB or lower.
//
PhitHob = (EFI_HOB_HANDOFF_INFO_TABLE *)GetHobList ();
if (PhitHob->EfiFreeMemoryTop <= MAX_UINT32) {
AllocType = AllocateMaxAddress;
Memory = MAX_UINT32;
}
Status = gBS->AllocatePages (AllocType, MemoryType, Pages, &Memory);
if (EFI_ERROR (Status)) {
return NULL;
}
return (VOID *)(UINTN)Memory;
}