OcAppleBootCompatLib: Added free memory debugging

This commit is contained in:
vit9696 2019-11-04 20:10:15 +03:00
parent 53ea852bc3
commit af3b167177
3 changed files with 72 additions and 0 deletions

View File

@ -200,6 +200,18 @@ CountRuntimePages (
OUT UINTN *DescriptorCount OPTIONAL
);
/**
Calculate number of free pages in the memory map.
@param[out] LowerMemory Number of free pages below 4 GB, optional.
@retval Number of free pages.
**/
UINTN
CountFreePages (
OUT UINTN *LowerMemory OPTIONAL
);
/**
Return pointer to PML4 table in PageTable and PWT and PCD flags in Flags.

View File

@ -101,12 +101,24 @@ OcAbcInitialize (
{
EFI_STATUS Status;
BOOT_COMPAT_CONTEXT *BootCompat;
UINTN LowMemory;
UINTN TotalMemory;
Status = InstallAbcProtocol ();
if (EFI_ERROR (Status)) {
return Status;
}
DEBUG_CODE_BEGIN ();
TotalMemory = CountFreePages (&LowMemory);
DEBUG ((
DEBUG_INFO,
"OCABC: Firmware has %Lu free pages (%Lu in lower 4 GB)\n",
(UINT64) TotalMemory,
(UINT64) LowMemory
));
DEBUG_CODE_END ();
BootCompat = GetBootCompatContext ();
CopyMem (

View File

@ -380,3 +380,51 @@ CountRuntimePages (
return PageNum;
}
UINTN
CountFreePages (
OUT UINTN *LowerMemory OPTIONAL
)
{
UINTN MemoryMapSize;
UINTN DescriptorSize;
EFI_MEMORY_DESCRIPTOR *MemoryMap;
EFI_MEMORY_DESCRIPTOR *EntryWalker;
UINTN FreePages;
FreePages = 0;
if (LowerMemory != NULL) {
*LowerMemory = 0;
}
MemoryMap = GetCurrentMemoryMap (&MemoryMapSize, &DescriptorSize, NULL, NULL);
if (MemoryMap == NULL) {
return 0;
}
for (
EntryWalker = MemoryMap;
(UINT8 *) EntryWalker < ((UINT8 *) MemoryMap + MemoryMapSize);
EntryWalker = NEXT_MEMORY_DESCRIPTOR (EntryWalker, DescriptorSize)) {
if (EntryWalker->Type != EfiConventionalMemory) {
continue;
}
FreePages += EntryWalker->NumberOfPages;
if (LowerMemory == NULL || EntryWalker->PhysicalStart >= BASE_4GB) {
continue;
}
if (EntryWalker->PhysicalStart + EFI_PAGES_TO_SIZE (EntryWalker->NumberOfPages) > BASE_4GB) {
*LowerMemory += EFI_SIZE_TO_PAGES (BASE_4GB - EntryWalker->PhysicalStart);
} else {
*LowerMemory += EntryWalker->NumberOfPages;
}
}
FreePool (MemoryMap);
return FreePages;
}