From 1b71ddea4265978de77c7bfe38dbc1fe7a13f61e Mon Sep 17 00:00:00 2001 From: Dun Tan Date: Thu, 20 Feb 2025 17:52:31 +0800 Subject: [PATCH] StandaloneMmPkg/Core: pass HOB list in MMRAM to library constructor The commit changes the code to initializes new HOB list in MMRAM before the ProcessLibraryConstructorList() and pass the MMRAM HOB list to lib constructor. Previously, the HOB list in non-MMRAM range is passed to the lib constructor. Then code in the library constructor would consume unverified HOB list in non-MMRAM buffer. With this commit, the HOB validation and memory allocation HOB migration are doned before the library constructor. Since the HOB list initialization needs to allocate memory in MMRAM, we also need to call the MmInitializeMemoryServices() before the HOB list initialization. Then the duplicated code in the StandaloneMmCore MemoryAllocationLib can also be removed. Signed-off-by: Dun Tan --- StandaloneMmPkg/Core/StandaloneMmCore.c | 15 ++-- StandaloneMmPkg/Core/StandaloneMmCore.h | 2 + StandaloneMmPkg/Core/StandaloneMmCore.inf | 1 + .../StandaloneMmCoreMemoryAllocationLib.c | 83 +------------------ .../StandaloneMmCoreMemoryAllocationLib.inf | 7 -- ...StandaloneMmCoreMemoryAllocationServices.h | 30 ------- 6 files changed, 12 insertions(+), 126 deletions(-) delete mode 100644 StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationServices.h diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.c b/StandaloneMmPkg/Core/StandaloneMmCore.c index 3870bafc9d..a23c91c358 100644 --- a/StandaloneMmPkg/Core/StandaloneMmCore.c +++ b/StandaloneMmPkg/Core/StandaloneMmCore.c @@ -1,7 +1,7 @@ /** @file MM Core Main Entry Point - Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2025, Intel Corporation. All rights reserved.
Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent @@ -797,8 +797,6 @@ StandaloneMmMain ( EFI_MMRAM_DESCRIPTOR *MmramRanges; UINTN MmramRangeCount; - ProcessLibraryConstructorList (HobStart, &gMmCoreMmst); - DEBUG ((DEBUG_INFO, "MmMain - 0x%x\n", HobStart)); DEBUG_CODE ( @@ -838,16 +836,19 @@ StandaloneMmMain ( } // - // No need to initialize memory service. - // It is done in the constructor of StandaloneMmCoreMemoryAllocationLib(), - // so that the library linked with StandaloneMmCore can use AllocatePool() in - // the constructor. + // Initialize memory service using free MMRAM // + DEBUG ((DEBUG_INFO, "MmInitializeMemoryServices\n")); + MmInitializeMemoryServices (MmramRangeCount, MmramRanges); + mMemoryAllocationMmst = &gMmCoreMmst; + // // Install HobList // gHobList = InitializeMmHobList (HobStart, MmramRanges, MmramRangeCount); + ProcessLibraryConstructorList (gHobList, &gMmCoreMmst); + // // Register notification for EFI_MM_CONFIGURATION_PROTOCOL registration and // use it to register the MM Foundation entrypoint diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.h b/StandaloneMmPkg/Core/StandaloneMmCore.h index 24574b254c..3884ed3c15 100644 --- a/StandaloneMmPkg/Core/StandaloneMmCore.h +++ b/StandaloneMmPkg/Core/StandaloneMmCore.h @@ -50,6 +50,8 @@ #include "StandaloneMmCorePrivateData.h" +extern EFI_MM_SYSTEM_TABLE *mMemoryAllocationMmst; + // // Used to build a table of MMI Handlers that the MM Core registers // diff --git a/StandaloneMmPkg/Core/StandaloneMmCore.inf b/StandaloneMmPkg/Core/StandaloneMmCore.inf index aca21a7e20..f825a98fa1 100644 --- a/StandaloneMmPkg/Core/StandaloneMmCore.inf +++ b/StandaloneMmPkg/Core/StandaloneMmCore.inf @@ -81,6 +81,7 @@ gMmCommBufferHobGuid gEfiSmmSmramMemoryGuid gEdkiiPiSmmMemoryAttributesTableGuid + gEfiMmPeiMmramMemoryReserveGuid [Pcd] gStandaloneMmPkgTokenSpaceGuid.PcdFwVolMmMaxEncapsulationDepth ##CONSUMES diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c index af2182ee52..fc72c66f99 100644 --- a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c +++ b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.c @@ -10,14 +10,11 @@ #include -#include #include #include #include -#include -#include "StandaloneMmCoreMemoryAllocationServices.h" -static EFI_MM_SYSTEM_TABLE *mMemoryAllocationMmst = NULL; +EFI_MM_SYSTEM_TABLE *mMemoryAllocationMmst = NULL; /** Allocates one or more 4KB pages of a certain memory type. @@ -827,81 +824,3 @@ FreePool ( Status = mMemoryAllocationMmst->MmFreePool (Buffer); ASSERT_EFI_ERROR (Status); } - -/** - The constructor function calls MmInitializeMemoryServices to initialize - memory in MMRAM and caches EFI_MM_SYSTEM_TABLE pointer. - - @param [in] ImageHandle The firmware allocated handle for the EFI image. - @param [in] MmSystemTable A pointer to the Management mode System Table. - - @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS. - -**/ -EFI_STATUS -EFIAPI -MemoryAllocationLibConstructor ( - IN EFI_HANDLE ImageHandle, - IN EFI_MM_SYSTEM_TABLE *MmSystemTable - ) -{ - VOID *HobStart; - EFI_MMRAM_HOB_DESCRIPTOR_BLOCK *MmramRangesHobData; - EFI_MMRAM_DESCRIPTOR *MmramRanges; - UINTN MmramRangeCount; - EFI_HOB_GUID_TYPE *MmramRangesHob; - - HobStart = GetHobList (); - DEBUG ((DEBUG_INFO, "StandaloneMmCoreMemoryAllocationLibConstructor - 0x%x\n", HobStart)); - - // - // Search for a Hob containing the MMRAM ranges - // - MmramRangesHob = GetNextGuidHob (&gEfiSmmSmramMemoryGuid, HobStart); - if (MmramRangesHob == NULL) { - MmramRangesHob = GetNextGuidHob (&gEfiMmPeiMmramMemoryReserveGuid, HobStart); - if (MmramRangesHob == NULL) { - return EFI_UNSUPPORTED; - } - } - - MmramRangesHobData = GET_GUID_HOB_DATA (MmramRangesHob); - if (MmramRangesHobData == NULL) { - return EFI_UNSUPPORTED; - } - - MmramRanges = MmramRangesHobData->Descriptor; - if (MmramRanges == NULL) { - return EFI_UNSUPPORTED; - } - - MmramRangeCount = (UINTN)MmramRangesHobData->NumberOfMmReservedRegions; - if (MmramRanges == NULL) { - return EFI_UNSUPPORTED; - } - - { - UINTN Index; - - DEBUG ((DEBUG_INFO, "MmramRangeCount - 0x%x\n", MmramRangeCount)); - for (Index = 0; Index < MmramRangeCount; Index++) { - DEBUG (( - DEBUG_INFO, - "MmramRanges[%d]: 0x%016lx - 0x%016lx\n", - Index, - MmramRanges[Index].CpuStart, - MmramRanges[Index].PhysicalSize - )); - } - } - - // - // Initialize memory service using free MMRAM - // - DEBUG ((DEBUG_INFO, "MmInitializeMemoryServices\n")); - MmInitializeMemoryServices ((UINTN)MmramRangeCount, (VOID *)(UINTN)MmramRanges); - - // Initialize MM Services Table - mMemoryAllocationMmst = MmSystemTable; - return EFI_SUCCESS; -} diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.inf b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.inf index 2848c4b0c7..64ad0410ab 100644 --- a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.inf +++ b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationLib.inf @@ -19,7 +19,6 @@ VERSION_STRING = 1.0 PI_SPECIFICATION_VERSION = 0x00010032 LIBRARY_CLASS = MemoryAllocationLib|MM_CORE_STANDALONE - CONSTRUCTOR = MemoryAllocationLibConstructor # # The following information is for reference only and not required by the build tools. @@ -29,7 +28,6 @@ [Sources] StandaloneMmCoreMemoryAllocationLib.c - StandaloneMmCoreMemoryAllocationServices.h [Packages] MdePkg/MdePkg.dec @@ -38,8 +36,3 @@ [LibraryClasses] BaseMemoryLib DebugLib - HobLib - -[Guids] - gEfiMmPeiMmramMemoryReserveGuid - gEfiSmmSmramMemoryGuid diff --git a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationServices.h b/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationServices.h deleted file mode 100644 index 1fd0478707..0000000000 --- a/StandaloneMmPkg/Library/StandaloneMmCoreMemoryAllocationLib/StandaloneMmCoreMemoryAllocationServices.h +++ /dev/null @@ -1,30 +0,0 @@ -/** @file - Contains function prototypes for Memory Services in the MM Core. - - This header file borrows the StandaloneMmCore Memory Allocation services as the primitive - for memory allocation. - - Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.
- Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.
- - SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#ifndef _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_ -#define _PI_MM_CORE_MEMORY_ALLOCATION_SERVICES_H_ - -/** - Called to initialize the memory service. - - @param MmramRangeCount Number of MMRAM Regions - @param MmramRanges Pointer to MMRAM Descriptors - -**/ -VOID -MmInitializeMemoryServices ( - IN UINTN MmramRangeCount, - IN EFI_MMRAM_DESCRIPTOR *MmramRanges - ); - -#endif