Skip to content

Commit

Permalink
UefiCpuPkg/PiSmmCpuDxeSmm:Check resource HOB range before mapping
Browse files Browse the repository at this point in the history
This commit is to check if the resource HOB range does not
exceed the max supported physical address.
The function BuildMemoryMapFromResDescHobs is to build Memory
Region from resource HOBs. Then the memory maps will be used
during creating or modifying SMM page table. If the resource
HOB range exceeds the max supported physical address, then
subsequent calling of PageTableMap() will fail.

Signed-off-by: Dun Tan <dun.tan@intel.com>
  • Loading branch information
td36 committed Nov 12, 2024
1 parent f1674e6 commit bb4499d
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions UefiCpuPkg/PiSmmCpuDxeSmm/NonMmramMapStandaloneMm.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,14 @@ BuildMemoryMapFromResDescHobs (
EFI_PEI_HOB_POINTERS Hob;
UINTN Count;
UINTN Index;
EFI_PHYSICAL_ADDRESS MaxPhysicalAddress;
EFI_PHYSICAL_ADDRESS ResourceHobEnd;

ASSERT (MemoryRegion != NULL && MemoryRegionCount != NULL);

*MemoryRegion = NULL;
*MemoryRegionCount = 0;
MaxPhysicalAddress = LShiftU64 (1, mPhysicalAddressBits);

//
// Get the count.
Expand All @@ -138,11 +141,22 @@ BuildMemoryMapFromResDescHobs (
Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
while (Hob.Raw != NULL) {
if ((Hob.ResourceDescriptor->ResourceAttribute & MM_RESOURCE_ATTRIBUTE_LOGGING) == 0) {
//
// Resource HOBs describe all accessible non-smram regions.
// Logging attribute range is treated as not present. Not-present ranges are not included in this memory map.
//
Count++;
ResourceHobEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;

if (ResourceHobEnd <= MaxPhysicalAddress) {
//
// Resource HOBs describe all accessible non-smram regions.
// Logging attribute range is treated as not present. Not-present ranges are not included in this memory map.
//
Count++;
} else {
DEBUG ((
DEBUG_ERROR,
"The resource HOB range [0x%lx, 0x%lx] which exceeds the max supported physical address won't be mapped.",
Hob.ResourceDescriptor->PhysicalStart,
ResourceHobEnd
));
}
}

Hob.Raw = GET_NEXT_HOB (Hob);
Expand All @@ -158,15 +172,19 @@ BuildMemoryMapFromResDescHobs (
Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
while (Hob.Raw != NULL) {
if ((Hob.ResourceDescriptor->ResourceAttribute & MM_RESOURCE_ATTRIBUTE_LOGGING) == 0) {
ASSERT (Index < Count);
(*MemoryRegion)[Index].Base = Hob.ResourceDescriptor->PhysicalStart;
(*MemoryRegion)[Index].Length = Hob.ResourceDescriptor->ResourceLength;
(*MemoryRegion)[Index].Attribute = EFI_MEMORY_XP;
if (Hob.ResourceDescriptor->ResourceAttribute == EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED) {
(*MemoryRegion)[Index].Attribute |= EFI_MEMORY_RO;
ResourceHobEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;

if (ResourceHobEnd <= MaxPhysicalAddress) {
ASSERT (Index < Count);
(*MemoryRegion)[Index].Base = Hob.ResourceDescriptor->PhysicalStart;
(*MemoryRegion)[Index].Length = Hob.ResourceDescriptor->ResourceLength;
(*MemoryRegion)[Index].Attribute = EFI_MEMORY_XP;
if (Hob.ResourceDescriptor->ResourceAttribute == EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED) {
(*MemoryRegion)[Index].Attribute |= EFI_MEMORY_RO;
}

Index++;
}

Index++;
}

Hob.Raw = GET_NEXT_HOB (Hob);
Expand Down

0 comments on commit bb4499d

Please sign in to comment.