Skip to content
Open
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
18 changes: 13 additions & 5 deletions MdeModulePkg/Core/Dxe/DxeMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ extern EFI_GUID *gDxeCoreFileName;
extern EFI_LOADED_IMAGE_PROTOCOL *gDxeCoreLoadedImage;

extern EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1];
// MU_CHANGE BEGIN: PEI Bins
extern BOOLEAN mMemoryTypeInformationInitialized;
extern EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1];
extern EFI_PHYSICAL_ADDRESS mDefaultMaximumAddress;
extern EFI_PHYSICAL_ADDRESS mDefaultBaseAddress;
// MU_CHANGE END: PEI Bins

extern BOOLEAN gDispatcherRunning;
extern EFI_RUNTIME_ARCH_PROTOCOL gRuntimeTemplate;
Expand All @@ -284,11 +290,13 @@ CoreInitializePool (
VOID
);

VOID
CoreSetMemoryTypeInformationRange (
IN EFI_PHYSICAL_ADDRESS Start,
IN UINT64 Length
);
// MU_CHANGE BEGIN: PEI Bins
// VOID
// CoreSetMemoryTypeInformationRange (
// IN EFI_PHYSICAL_ADDRESS Start,
// IN UINT64 Length
// );
// MU_CHANGE END: PEI Bins

/**
Called to initialize the memory map and add descriptors to
Expand Down
1 change: 1 addition & 0 deletions MdeModulePkg/Core/Dxe/DxeMain.inf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
Mem/Page.c
Mem/MemData.c
Mem/Imem.h
Mem/MemoryBin.c # MU_CHANGE: PEI Bins
Mem/MemoryProfileRecord.c
Mem/HeapGuard.c
Mem/HeapGuard.h
Expand Down
201 changes: 124 additions & 77 deletions MdeModulePkg/Core/Dxe/Gcd/Gcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
#include "DxeMain.h"
#include "Gcd.h"
#include "Mem/HeapGuard.h"
#include <MemoryBin.h> // MU_CHANGE: PEI Bins

#define MINIMUM_INITIAL_MEMORY_SIZE 0x10000

Expand Down Expand Up @@ -2164,30 +2165,32 @@ CoreConvertResourceDescriptorHobAttributesToCapabilities (
return Capabilities;
}

/**
Calculate total memory bin size neeeded.

@return The total memory bin size neeeded.

**/
UINT64
CalculateTotalMemoryBinSizeNeeded (
VOID
)
{
UINTN Index;
UINT64 TotalSize;

//
// Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
//
TotalSize = 0;
for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {
TotalSize += LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT);
}

return TotalSize;
}
// MU_CHANGE BEGIN: PEI Bins - Moved to MemoryBin.c
// /**
// Calculate total memory bin size neeeded.
//
// @return The total memory bin size neeeded.
//
// **/
// UINT64
// CalculateTotalMemoryBinSizeNeeded (
// VOID
// )
// {
// UINTN Index;
// UINT64 TotalSize;
//
// //
// // Loop through each memory type in the order specified by the gMemoryTypeInformation[] array
// //
// TotalSize = 0;
// for (Index = 0; gMemoryTypeInformation[Index].Type != EfiMaxMemoryType; Index++) {
// TotalSize += LShiftU64 (gMemoryTypeInformation[Index].NumberOfPages, EFI_PAGE_SHIFT);
// }
//
// return TotalSize;
// }
// MU_CHANGE END: PEI Bins

/**
Find the largest region in the specified region that is not covered by an existing memory allocation
Expand Down Expand Up @@ -2270,25 +2273,28 @@ CoreInitializeMemoryServices (
)
{
EFI_PEI_HOB_POINTERS Hob;
EFI_MEMORY_TYPE_INFORMATION *EfiMemoryTypeInformation;
UINTN DataSize;
// MU_CHANGE BEGIN: PEI Bins
// EFI_MEMORY_TYPE_INFORMATION *EfiMemoryTypeInformation;
// UINTN DataSize;
// MU_CHANGE END: PEI Bins
BOOLEAN Found;
EFI_HOB_HANDOFF_INFO_TABLE *PhitHob;
EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;
EFI_HOB_RESOURCE_DESCRIPTOR *PhitResourceHob;
EFI_HOB_RESOURCE_DESCRIPTOR *MemoryTypeInformationResourceHob;
UINTN Count;
// UINTN Count; // MU_CHANGE: PEI Bins
EFI_PHYSICAL_ADDRESS BaseAddress;
UINT64 Length;
UINT64 Attributes;
UINT64 Capabilities;
EFI_PHYSICAL_ADDRESS TestedMemoryBaseAddress;
UINT64 TestedMemoryLength;
EFI_PHYSICAL_ADDRESS HighAddress;
EFI_HOB_GUID_TYPE *GuidHob;
// EFI_HOB_GUID_TYPE *GuidHob; // MU_CHANGE: PEI Bins
UINT32 ReservedCodePageNumber;
UINT64 MinimalMemorySizeNeeded;
EFI_PHYSICAL_ADDRESS ResourceHobMemoryTop; // MU_CHANGE
EFI_PHYSICAL_ADDRESS ResourceHobMemoryTop;
EFI_STATUS Status; // MU_CHANGE: PEI Bins

//
// Point at the first HOB. This must be the PHIT HOB.
Expand Down Expand Up @@ -2327,57 +2333,68 @@ CoreInitializeMemoryServices (
+ EFI_PAGES_TO_SIZE (ReservedCodePageNumber);
}

//
// See if a Memory Type Information HOB is available
//
MemoryTypeInformationResourceHob = NULL;
GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);
if (GuidHob != NULL) {
EfiMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);
DataSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
if ((EfiMemoryTypeInformation != NULL) && (DataSize > 0) && (DataSize <= (EfiMaxMemoryType + 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION))) {
CopyMem (&gMemoryTypeInformation, EfiMemoryTypeInformation, DataSize);

//
// Look for Resource Descriptor HOB with a ResourceType of System Memory
// and an Owner GUID of gEfiMemoryTypeInformationGuid. If more than 1 is
// found, then set MemoryTypeInformationResourceHob to NULL.
//
Count = 0;
for (Hob.Raw = *HobStart; !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {
if ((GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) && (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR2)) {
continue;
}

ResourceHob = Hob.ResourceDescriptor;
if (!CompareGuid (&ResourceHob->Owner, &gEfiMemoryTypeInformationGuid)) {
continue;
}

Count++;
if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {
continue;
}

if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != TESTED_MEMORY_ATTRIBUTES) {
continue;
}

if (ResourceHob->ResourceLength >= CalculateTotalMemoryBinSizeNeeded ()) {
MemoryTypeInformationResourceHob = ResourceHob;
}
}

if (Count > 1) {
MemoryTypeInformationResourceHob = NULL;
}
}
// MU_CHANGE BEGIN: PEI Bins - Replaced inline HOB search with shared MemoryBin functions
// //
// // See if a Memory Type Information HOB is available
// //
// MemoryTypeInformationResourceHob = NULL;
// GuidHob = GetFirstGuidHob (&gEfiMemoryTypeInformationGuid);
// if (GuidHob != NULL) {
// EfiMemoryTypeInformation = GET_GUID_HOB_DATA (GuidHob);
// DataSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
// if ((EfiMemoryTypeInformation != NULL) && (DataSize > 0) && (DataSize <= (EfiMaxMemoryType + 1) * sizeof (EFI_MEMORY_TYPE_INFORMATION))) {
// CopyMem (&gMemoryTypeInformation, EfiMemoryTypeInformation, DataSize);
//
// //
// // Look for Resource Descriptor HOB with a ResourceType of System Memory
// // and an Owner GUID of gEfiMemoryTypeInformationGuid. If more than 1 is
// // found, then set MemoryTypeInformationResourceHob to NULL.
// //
// Count = 0;
// for (Hob.Raw = *HobStart; !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {
// if ((GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) && (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR2)) {
// continue;
// }
//
// ResourceHob = Hob.ResourceDescriptor;
// if (!CompareGuid (&ResourceHob->Owner, &gEfiMemoryTypeInformationGuid)) {
// continue;
// }
//
// Count++;
// if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {
// continue;
// }
//
// if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != TESTED_MEMORY_ATTRIBUTES) {
// continue;
// }
//
// if (ResourceHob->ResourceLength >= CalculateTotalMemoryBinSizeNeeded ()) {
// MemoryTypeInformationResourceHob = ResourceHob;
// }
// }
//
// if (Count > 1) {
// MemoryTypeInformationResourceHob = NULL;
// }
// }
// }
Status = PopulateMemoryTypeInformation (gMemoryTypeInformation);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "No Memory Type Information HOB found, S4 resume will likely fail\n"));
}

MemoryTypeInformationResourceHob = GetMemoryTypeInformationResourceHob (
HobStart,
gMemoryTypeInformation
);

//
// Include the total memory bin size needed to make sure memory bin could be allocated successfully.
//
MinimalMemorySizeNeeded = MINIMUM_INITIAL_MEMORY_SIZE + CalculateTotalMemoryBinSizeNeeded ();
MinimalMemorySizeNeeded = MINIMUM_INITIAL_MEMORY_SIZE + CalculateTotalMemoryBinSizeNeeded (0, gMemoryTypeInformation);
// MU_CHANGE END: PEI Bins

//
// Find the Resource Descriptor HOB that contains PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
Expand Down Expand Up @@ -2586,16 +2603,29 @@ CoreInitializeMemoryServices (
Capabilities = CoreConvertResourceDescriptorHobAttributesToCapabilities (EfiGcdMemoryTypeSystemMemory, Attributes);
}

if (MemoryTypeInformationResourceHob != NULL) {
// MU_CHANGE BEGIN: PEI Bins
// if (MemoryTypeInformationResourceHob != NULL) {
if ((Status == EFI_SUCCESS) && (MemoryTypeInformationResourceHob != NULL)) {
// MU_CHANGE END: PEI Bins
//
// If a Memory Type Information Resource HOB was found, then use the address
// range of the Memory Type Information Resource HOB as the preferred
// address range for the Memory Type Information bins.
//
// MU_CHANGE BEGIN: PEI Bins
// CoreSetMemoryTypeInformationRange (
// MemoryTypeInformationResourceHob->PhysicalStart,
// MemoryTypeInformationResourceHob->ResourceLength
// );
CoreSetMemoryTypeInformationRange (
MemoryTypeInformationResourceHob->PhysicalStart,
MemoryTypeInformationResourceHob->ResourceLength
MemoryTypeInformationResourceHob->ResourceLength,
gMemoryTypeInformation,
&mMemoryTypeInformationInitialized,
mMemoryTypeStatistics,
&mDefaultMaximumAddress
);
// MU_CHANGE END: PEI Bins
}

//
Expand Down Expand Up @@ -2843,6 +2873,23 @@ CoreInitializeGcdServices (
RShiftU64 (MemoryHob->AllocDescriptor.MemoryLength, EFI_PAGE_SHIFT),
Descriptor.Capabilities & (~EFI_MEMORY_RUNTIME)
);

// MU_CHANGE BEGIN: PEI Bins - Update memory bin statistics from PEI allocation HOBs
// if this Memory Allocation HOB came from PEI, update the memory bin statistics
if (CompareGuid (&MemoryHob->AllocDescriptor.Name, &gEfiMemoryTypeInformationGuid)) {
UpdateMemoryStatistics (
EfiConventionalMemory,
MemoryHob->AllocDescriptor.MemoryType,
MemoryHob->AllocDescriptor.MemoryBaseAddress,
EFI_SIZE_TO_PAGES ((UINT32)MemoryHob->AllocDescriptor.MemoryLength),
&mMemoryTypeInformationInitialized,
mMemoryTypeStatistics,
gMemoryTypeInformation,
mDefaultBaseAddress,
mDefaultMaximumAddress
);
}
// MU_CHANGE END: PEI Bins
}
}
}
Expand Down
Loading
Loading