Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if the non-MMRAM buffer is valid in API MmIsBufferOutsideMmValid of StandaloneMmMemLib. #6225

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 61 additions & 28 deletions StandaloneMmPkg/Core/StandaloneMmCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,13 +665,7 @@ MigrateMemoryAllocationHobs (
Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, HobStart);
while (Hob.Raw != NULL) {
MemoryAllocationHob = (EFI_HOB_MEMORY_ALLOCATION *)Hob.Raw;
if ((MemoryAllocationHob->AllocDescriptor.MemoryType == EfiBootServicesData) &&
(MmIsBufferOutsideMmValid (
MemoryAllocationHob->AllocDescriptor.MemoryBaseAddress,
MemoryAllocationHob->AllocDescriptor.MemoryLength
))
)
{
if (MemoryAllocationHob->AllocDescriptor.MemoryType == EfiBootServicesData) {
if (!IsZeroGuid (&MemoryAllocationHob->AllocDescriptor.Name)) {
MemoryInMmram = AllocatePages (EFI_SIZE_TO_PAGES (MemoryAllocationHob->AllocDescriptor.MemoryLength));
if (MemoryInMmram != NULL) {
Expand Down Expand Up @@ -705,30 +699,81 @@ MigrateMemoryAllocationHobs (
}
}

/** Returns the HOB list size.
/**
This function is responsible for validating the input HOB list and
initializing a new HOB list in MMRAM based on the input HOB list.

@param [in] HobStart Pointer to the start of the HOB list.
@param [in] HobStart Pointer to the start of the HOB list.
@param [in] MmramRanges Pointer to the Mmram ranges.
@param [in] MmramRangeCount Count of Mmram ranges.

@retval Size of the HOB list.
@retval Pointer to the new location of hob list in MMRAM.
**/
UINTN
GetHobListSize (
IN VOID *HobStart
VOID *
InitializeMmHobList (
IN VOID *HobStart,
IN EFI_MMRAM_DESCRIPTOR *MmramRanges,
IN UINTN MmramRangeCount
)
{
VOID *MmHobStart;
UINTN HobSize;
EFI_STATUS Status;
EFI_PEI_HOB_POINTERS Hob;
UINTN Index;
EFI_PHYSICAL_ADDRESS MmramBase;
EFI_PHYSICAL_ADDRESS MmramEnd;
EFI_PHYSICAL_ADDRESS ResourceHobBase;
EFI_PHYSICAL_ADDRESS ResourceHobEnd;

ASSERT (HobStart != NULL);

Hob.Raw = (UINT8 *)HobStart;
while (!END_OF_HOB_LIST (Hob)) {
Hob.Raw = GET_NEXT_HOB (Hob);
if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
ResourceHobBase = Hob.ResourceDescriptor->PhysicalStart;
ResourceHobEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;

for (Index = 0; Index < MmramRangeCount; Index++) {
MmramBase = MmramRanges[Index].PhysicalStart;
MmramEnd = MmramRanges[Index].PhysicalStart + MmramRanges[Index].PhysicalSize;

if ((MmramBase < ResourceHobEnd) && (MmramEnd > ResourceHobBase)) {
//
// The Resource HOB is to describe the accessible non-Mmram range.
// All Resource HOB should not overlapp with any Mmram range.
//
DEBUG ((
DEBUG_ERROR,
"The resource HOB range [0x%lx, 0x%lx] overlaps with MMRAM range\n",
ResourceHobBase,
ResourceHobEnd
));
CpuDeadLoop ();
}
}
}
}

//
// Need plus END_OF_HOB_LIST
//
return (UINTN)Hob.Raw - (UINTN)HobStart + sizeof (EFI_HOB_GENERIC_HEADER);
HobSize = (UINTN)Hob.Raw - (UINTN)HobStart + sizeof (EFI_HOB_GENERIC_HEADER);
DEBUG ((DEBUG_INFO, "HobSize - 0x%x\n", HobSize));

MmHobStart = AllocatePool (HobSize);
DEBUG ((DEBUG_INFO, "MmHobStart - 0x%x\n", MmHobStart));
ASSERT (MmHobStart != NULL);
CopyMem (MmHobStart, HobStart, HobSize);

DEBUG ((DEBUG_INFO, "MmInstallConfigurationTable For HobList\n"));
Status = MmInstallConfigurationTable (&gMmCoreMmst, &gEfiHobListGuid, MmHobStart, HobSize);
ASSERT_EFI_ERROR (Status);

MigrateMemoryAllocationHobs (MmHobStart);

return MmHobStart;
}

/**
Expand All @@ -752,8 +797,6 @@ StandaloneMmMain (
{
EFI_STATUS Status;
UINTN Index;
VOID *MmHobStart;
UINTN HobSize;
VOID *Registration;
EFI_HOB_GUID_TYPE *MmramRangesHob;
EFI_MMRAM_HOB_DESCRIPTOR_BLOCK *MmramRangesHobData;
Expand Down Expand Up @@ -806,21 +849,11 @@ StandaloneMmMain (
// It is done in the constructor of StandaloneMmCoreMemoryAllocationLib(),
// so that the library linked with StandaloneMmCore can use AllocatePool() in
// the constructor.

DEBUG ((DEBUG_INFO, "MmInstallConfigurationTable For HobList\n"));
//
//
// Install HobList
//
HobSize = GetHobListSize (HobStart);
DEBUG ((DEBUG_INFO, "HobSize - 0x%x\n", HobSize));
MmHobStart = AllocatePool (HobSize);
DEBUG ((DEBUG_INFO, "MmHobStart - 0x%x\n", MmHobStart));
ASSERT (MmHobStart != NULL);
CopyMem (MmHobStart, HobStart, HobSize);
Status = MmInstallConfigurationTable (&gMmCoreMmst, &gEfiHobListGuid, MmHobStart, HobSize);
ASSERT_EFI_ERROR (Status);
MigrateMemoryAllocationHobs (MmHobStart);
gHobList = MmHobStart;
gHobList = InitializeMmHobList (HobStart, MmramRanges, MmramRangeCount);

//
// Register notification for EFI_MM_CONFIGURATION_PROTOCOL registration and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
and MM driver) and/or specific dedicated hardware.

Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2015 - 2024, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent

**/
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include "StandaloneMmMemLibInternal.h"
//
// Maximum support address used to check input buffer
//
Expand All @@ -25,7 +23,7 @@ extern EFI_PHYSICAL_ADDRESS mMmMemLibInternalMaximumSupportAddress;

**/
VOID
MmMemLibInternalCalculateMaximumSupportAddress (
MmMemLibCalculateMaximumSupportAddress (
VOID
)
{
Expand All @@ -35,29 +33,43 @@ MmMemLibInternalCalculateMaximumSupportAddress (
}

/**
Initialize cached Mmram Ranges from HOB.

@retval EFI_UNSUPPORTED The routine is unable to extract MMRAM information.
@retval EFI_SUCCESS MmRanges are populated successfully.
Initialize valid non-Mmram Ranges from Resource HOB.

**/
EFI_STATUS
MmMemLibInternalPopulateMmramRanges (
VOID
MmMemLibInitializeValidNonMmramRanges (
VOID
)
{
// Not implemented for AARCH64.
return EFI_SUCCESS;
}

/**
Deinitialize cached Mmram Ranges.
Deinitialize cached non-Mmram Ranges.

**/
VOID
MmMemLibInternalFreeMmramRanges (
MmMemLibFreeValidNonMmramRanges (
VOID
)
{
// Not implemented for AARCH64.
}

/**
This function check if the buffer is valid non-MMRAM memory range.

@param[in] Buffer The buffer start address to be checked.
@param[in] Length The buffer length to be checked.

@retval TRUE This buffer is valid non-MMRAM memory range.
@retval FALSE This buffer is not valid non-MMRAM memory range.
**/
BOOLEAN
MmMemLibIsValidNonMmramRange (
IN EFI_PHYSICAL_ADDRESS Buffer,
IN UINT64 Length
)
{
return TRUE;
}
82 changes: 9 additions & 73 deletions StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,20 @@
all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
and MM driver) and/or specific dedicated hardware.

Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2015 - 2024, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.<BR>

SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <PiMm.h>

#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>

EFI_MMRAM_DESCRIPTOR *mMmMemLibInternalMmramRanges;
UINTN mMmMemLibInternalMmramCount;
#include "StandaloneMmMemLibInternal.h"

//
// Maximum support address used to check input buffer
//
EFI_PHYSICAL_ADDRESS mMmMemLibInternalMaximumSupportAddress = 0;

/**
Calculate and save the maximum support address.

**/
VOID
MmMemLibInternalCalculateMaximumSupportAddress (
VOID
);

/**
Initialize cached Mmram Ranges from HOB.

@retval EFI_UNSUPPORTED The routine is unable to extract MMRAM information.
@retval EFI_SUCCESS MmRanges are populated successfully.

**/
EFI_STATUS
MmMemLibInternalPopulateMmramRanges (
VOID
);

/**
Deinitialize cached Mmram Ranges.

**/
VOID
MmMemLibInternalFreeMmramRanges (
VOID
);

/**
This function check if the buffer is valid per processor architecture and not overlap with MMRAM.

Expand All @@ -73,8 +36,6 @@ MmIsBufferOutsideMmValid (
IN UINT64 Length
)
{
UINTN Index;

//
// Check override.
// NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
Expand All @@ -96,29 +57,7 @@ MmIsBufferOutsideMmValid (
return FALSE;
}

for (Index = 0; Index < mMmMemLibInternalMmramCount; Index++) {
if (((Buffer >= mMmMemLibInternalMmramRanges[Index].CpuStart) &&
(Buffer < mMmMemLibInternalMmramRanges[Index].CpuStart + mMmMemLibInternalMmramRanges[Index].PhysicalSize)) ||
((mMmMemLibInternalMmramRanges[Index].CpuStart >= Buffer) &&
(mMmMemLibInternalMmramRanges[Index].CpuStart < Buffer + Length)))
{
DEBUG ((
DEBUG_ERROR,
"MmIsBufferOutsideMmValid: Overlap: Buffer (0x%lx) - Length (0x%lx), ",
Buffer,
Length
));
DEBUG ((
DEBUG_ERROR,
"CpuStart (0x%lx) - PhysicalSize (0x%lx)\n",
mMmMemLibInternalMmramRanges[Index].CpuStart,
mMmMemLibInternalMmramRanges[Index].PhysicalSize
));
return FALSE;
}
}

return TRUE;
return MmMemLibIsValidNonMmramRange (Buffer, Length);
}

/**
Expand Down Expand Up @@ -288,19 +227,17 @@ MemLibConstructor (
IN EFI_MM_SYSTEM_TABLE *MmSystemTable
)
{
EFI_STATUS Status;

//
// Calculate and save maximum support address
//
MmMemLibInternalCalculateMaximumSupportAddress ();
MmMemLibCalculateMaximumSupportAddress ();

//
// Initialize cached Mmram Ranges from HOB.
// Initialize valid non-Mmram Ranges from Resource HOB.
//
Status = MmMemLibInternalPopulateMmramRanges ();
MmMemLibInitializeValidNonMmramRanges ();

return Status;
return EFI_SUCCESS;
}

/**
Expand All @@ -320,9 +257,8 @@ MemLibDestructor (
)
{
//
// Deinitialize cached Mmram Ranges.
// Deinitialize cached non-Mmram Ranges.
//
MmMemLibInternalFreeMmramRanges ();

MmMemLibFreeValidNonMmramRanges ();
return EFI_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# all MMRAM range via MM_ACCESS_PROTOCOL, including the range for firmware (like MM Core
# and MM driver) and/or specific dedicated hardware.
#
# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2015 - 2024, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.<BR>
# Copyright (c) Microsoft Corporation.
#
Expand All @@ -33,6 +33,7 @@

[Sources.Common]
StandaloneMmMemLib.c
StandaloneMmMemLibInternal.h

[Sources.IA32, Sources.X64]
X86StandaloneMmMemLibInternal.c
Expand All @@ -49,7 +50,3 @@
DebugLib
HobLib
MemoryAllocationLib

[Guids]
gEfiMmPeiMmramMemoryReserveGuid ## SOMETIMES_CONSUMES ## HOB
gEfiSmmSmramMemoryGuid ## SOMETIMES_CONSUMES ## HOB
Loading
Loading