From 378aff173c3e57374a4df2765b9b971236bb03bd Mon Sep 17 00:00:00 2001 From: Zhang Hongbin Date: Tue, 2 Jul 2024 18:16:16 +0800 Subject: [PATCH] StandaloneMmPkg/MmIpl: Create MM profile data HOBs Create memory allocation and resource HOB for MM profile data Signed-off-by: Hongbin1 Zhang Cc: Jiewen Yao Cc: Ray Ni Cc: Star Zeng Cc: Jiaxin Wu Cc: Wei6 Xu Cc: Sami Mujawar Cc: Ard Biesheuvel Cc: Supreeth Venkatesh --- .../StandaloneMmIplPei/MmFoundationHob.c | 150 ++++++++++++++++++ .../StandaloneMmIplPei/StandaloneMmIplPei.c | 21 ++- .../StandaloneMmIplPei/StandaloneMmIplPei.h | 14 ++ .../StandaloneMmIplPei/StandaloneMmIplPei.inf | 3 + 4 files changed, 182 insertions(+), 6 deletions(-) diff --git a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/MmFoundationHob.c b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/MmFoundationHob.c index dbb13b06a9..f3f4363451 100644 --- a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/MmFoundationHob.c +++ b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/MmFoundationHob.c @@ -11,6 +11,9 @@ #include #include #include +#include +#include +#include /** Add a new HOB to the HOB List. @@ -44,6 +47,45 @@ MmIplCreateHob ( return Hob; } +/** + Builds a HOB that describes a chunk of system memory. + + This function builds a HOB that describes a chunk of system memory. + If there is no additional space for HOB creation, then ASSERT(). + + @param[in] Hob The pointer of new HOB buffer. + @param[in] ResourceType The type of resource described by this HOB. + @param[in] ResourceAttribute The resource attributes of the memory described by this HOB. + @param[in] PhysicalStart The 64 bit physical address of memory described by this HOB. + @param[in] NumberOfBytes The length of the memory described by this HOB in bytes. + @param[in] Owner The pointer of GUID. + +**/ +VOID +MmIplBuildMemoryResourceHob ( + IN EFI_HOB_RESOURCE_DESCRIPTOR *Hob, + IN EFI_RESOURCE_TYPE ResourceType, + IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute, + IN EFI_PHYSICAL_ADDRESS PhysicalStart, + IN UINT64 NumberOfBytes, + IN EFI_GUID *Owner + ) +{ + ASSERT (Hob != NULL); + MmIplCreateHob (Hob, EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR)); + + Hob->ResourceType = EFI_RESOURCE_SYSTEM_MEMORY; + Hob->ResourceAttribute = ResourceAttribute; + Hob->PhysicalStart = PhysicalStart; + Hob->ResourceLength = NumberOfBytes; + + if (Owner != NULL) { + CopyGuid (&Hob->Owner, Owner); + } else { + ZeroMem (&Hob->Owner, sizeof (EFI_GUID)); + } +} + /** Builds a Firmware Volume HOB. @@ -260,6 +302,105 @@ MmIplBuildMmCoreModuleHob ( *HobBufferSize = HobLength; } +/** + Build memory allocation HOB in PEI HOB list for MM profile data. + + This function is to allocate memory for MM profile data. + + @return NULL if MM profile data memory allocation HOB build fail. + @return Pointer of MM profile data memory allocation HOB if build successfully. + +**/ +EFI_HOB_MEMORY_ALLOCATION * +BuildMmProfileDataHobInPeiHobList ( + VOID + ) +{ + EFI_PEI_HOB_POINTERS Hob; + UINTN TotalSize; + VOID *Alloc; + + TotalSize = PcdGet32 (PcdCpuSmmProfileSize); + Alloc = AllocateReservedPages (EFI_SIZE_TO_PAGES (TotalSize)); + if (Alloc == NULL) { + return NULL; + } + + ZeroMem (Alloc, TotalSize); + + Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION); + while (Hob.Raw != NULL) { + if (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == (EFI_PHYSICAL_ADDRESS)(UINTN)Alloc) { + // + // Find the HOB just created and change the Name to gMmProfileDataHobGuid in PEI HOB list + // + CopyGuid (&Hob.MemoryAllocation->AllocDescriptor.Name, &gMmProfileDataHobGuid); + return Hob.MemoryAllocation; + } + + Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, GET_NEXT_HOB (Hob)); + } + + return NULL; +} + +/** + Build memory allocation and resource HOB for MM profile data + + This function builds HOBs for MM profile data, one is memory + allocation HOB, another is resource HOB. + + @param[in] HobBuffer The pointer of new HOB buffer. + @param[in, out] HobBufferSize The total size of the same GUID HOBs when as input. + The size will be 0 for output when build HOB fail. + +**/ +VOID +MmIplBuildMmProfileHobs ( + IN UINT8 *HobBuffer, + IN OUT UINTN *HobBufferSize + ) +{ + EFI_PEI_HOB_POINTERS Hob; + UINTN HobLength; + + Hob.MemoryAllocation = NULL; + HobLength = ALIGN_VALUE (sizeof (EFI_HOB_MEMORY_ALLOCATION), 8) + ALIGN_VALUE (sizeof (EFI_HOB_RESOURCE_DESCRIPTOR), 8); + + if (*HobBufferSize >= HobLength) { + Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION); + while (Hob.Raw != NULL) { + if (CompareGuid (&Hob.MemoryAllocation->AllocDescriptor.Name, &gMmProfileDataHobGuid)) { + break; + } + + Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, GET_NEXT_HOB (Hob)); + } + + ASSERT (Hob.MemoryAllocation != NULL); + + // + // Build memory allocation HOB + // + ASSERT (Hob.MemoryAllocation->Header.HobLength == ALIGN_VALUE (sizeof (EFI_HOB_MEMORY_ALLOCATION), 8)); + CopyMem (HobBuffer, Hob.Raw, Hob.MemoryAllocation->Header.HobLength); + + // + // Build resource HOB + // + MmIplBuildMemoryResourceHob ( + (EFI_HOB_RESOURCE_DESCRIPTOR *)(HobBuffer + Hob.MemoryAllocation->Header.HobLength), + EFI_RESOURCE_SYSTEM_MEMORY, + 0, + Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress, + Hob.MemoryAllocation->AllocDescriptor.MemoryLength, + &gMmProfileDataHobGuid + ); + } + + *HobBufferSize = HobLength; +} + /** Get remaining size for building HOBs. @@ -406,6 +547,15 @@ CreateMmFoundationHobList ( MmIplCopyGuidHob (FoundationHobList + UsedSize, &HobLength, &gEfiAcpiVariableGuid, FALSE); UsedSize += HobLength; + if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + // + // Build memory allocation and resource HOB for MM profile data + // + HobLength = GetRemainingHobSize (*FoundationHobSize, UsedSize); + MmIplBuildMmProfileHobs (FoundationHobList + UsedSize, &HobLength); + UsedSize += HobLength; + } + if (*FoundationHobSize < UsedSize) { Status = RETURN_BUFFER_TOO_SMALL; } else { diff --git a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c index 89dd05c1b7..b6525b20fc 100644 --- a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c +++ b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.c @@ -238,12 +238,13 @@ CreatMmHobList ( IN EFI_MMRAM_HOB_DESCRIPTOR_BLOCK *Block ) { - EFI_STATUS Status; - VOID *HobList; - VOID *PlatformHobList; - UINTN PlatformHobSize; - UINTN BufferSize; - UINTN FoundationHobSize; + EFI_STATUS Status; + VOID *HobList; + VOID *PlatformHobList; + UINTN PlatformHobSize; + UINTN BufferSize; + UINTN FoundationHobSize; + EFI_HOB_MEMORY_ALLOCATION *MmProfileDataHob; // // Get platform HOBs @@ -270,6 +271,14 @@ CreatMmHobList ( ASSERT_EFI_ERROR (Status); + // + // Build memory allocation HOB in PEI HOB list for MM profile data. + // + MmProfileDataHob = NULL; + if (FeaturePcdGet (PcdCpuSmmProfileEnable)) { + MmProfileDataHob = BuildMmProfileDataHobInPeiHobList (); + } + // // Get size of foundation HOBs // diff --git a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.h b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.h index d99a64ec08..e167a0d8f1 100644 --- a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.h +++ b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.h @@ -126,4 +126,18 @@ CreateMmFoundationHobList ( IN EFI_MMRAM_HOB_DESCRIPTOR_BLOCK *Block ); +/** + Build memory allocation HOB in PEI HOB list for MM profile data. + + This function is to allocate memory for MM profile data. + + @return NULL if MM profile data memory allocation HOB build fail. + @return Pointer of MM profile data memory allocation HOB if build successfully. + +**/ +EFI_HOB_MEMORY_ALLOCATION * +BuildMmProfileDataHobInPeiHobList ( + VOID + ); + #endif diff --git a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf index bf0bdf735f..325a37342c 100644 --- a/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf +++ b/StandaloneMmPkg/Drivers/StandaloneMmIplPei/StandaloneMmIplPei.inf @@ -54,6 +54,7 @@ gEfiAcpiVariableGuid gMmAcpiS3EnableHobGuid gMmCpuSyncConfigHobGuid + gMmProfileDataHobGuid [Ppis] gEfiPeiMmControlPpiGuid @@ -69,6 +70,8 @@ gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmSyncMode ## CONSUMES gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmApSyncTimeout ## CONSUMES gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmApSyncTimeout2 ## CONSUMES + gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmProfileEnable ## CONSUMES + gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmProfileSize ## CONSUMES [Depex] gEfiPeiMmControlPpiGuid AND gEfiPeiMpServicesPpiGuid