MdeModulePkg: MemoryBins: make GoogleTest page-granularity aware

MemoryBinGoogleTest.PopulatesFromValidHob asserted fixed page counts for
each memory type.  PopulateMemoryTypeInformation, however, rounds the
runtime memory types (EfiReservedMemoryType, EfiACPIMemoryNVS,
EfiRuntimeServicesCode, EfiRuntimeServicesData) up to
RUNTIME_PAGE_ALLOCATION_GRANULARITY.  That granularity equals EFI_PAGE_SIZE
on IA32/X64, so the hard-coded values happened to match, but it is 64 KiB
on AArch64, where the same inputs round up to different page counts and the
test failed.

Compute the expected page counts with the same granularity rounding the
production code uses, so the test passes on all host architectures instead
of only x86.

Signed-off-by: Jeff Brasen <jbrasen@nvidia.com>
diff --git a/MdeModulePkg/Core/Dxe/Mem/GoogleTest/MemoryBinGoogleTest.cpp b/MdeModulePkg/Core/Dxe/Mem/GoogleTest/MemoryBinGoogleTest.cpp
index 486427b..ba5a50c 100644
--- a/MdeModulePkg/Core/Dxe/Mem/GoogleTest/MemoryBinGoogleTest.cpp
+++ b/MdeModulePkg/Core/Dxe/Mem/GoogleTest/MemoryBinGoogleTest.cpp
@@ -418,6 +418,36 @@
 //

 // Test: PopulateMemoryTypeInformation populates from valid HOB

 //

+//

+// Align a page count to the allocation granularity that

+// PopulateMemoryTypeInformation applies for the given memory type.  Runtime

+// memory types are rounded up to RUNTIME_PAGE_ALLOCATION_GRANULARITY, which is

+// larger than EFI_PAGE_SIZE on some architectures (e.g. 64 KiB on AArch64), so

+// the resulting page counts are architecture dependent.

+//

+static UINT32

+ExpectedAlignedPages (

+  EFI_MEMORY_TYPE  Type,

+  UINT32           NumberOfPages

+  )

+{

+  UINT32  Granularity;

+

+  if ((Type == EfiReservedMemoryType) ||

+      (Type == EfiACPIMemoryNVS) ||

+      (Type == EfiRuntimeServicesCode) ||

+      (Type == EfiRuntimeServicesData))

+  {

+    Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY;

+  } else {

+    Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY;

+  }

+

+  return (UINT32)EFI_SIZE_TO_PAGES (

+                   ALIGN_VALUE (EFI_PAGES_TO_SIZE ((UINTN)NumberOfPages), Granularity)

+                   );

+}

+

 TEST_F (BaseMemoryBinLibTest, PopulatesFromValidHob) {

   EFI_STATUS                   Status;

   UINT8                        GuidHobBuffer[sizeof (EFI_HOB_GUID_TYPE) + sizeof (EFI_MEMORY_TYPE_INFORMATION) * 7];

@@ -453,17 +483,17 @@
 

   ASSERT_EQ (Status, EFI_SUCCESS);

   ASSERT_EQ (gMemoryTypeInformation[0].Type, (UINT32)EfiReservedMemoryType);

-  ASSERT_EQ (gMemoryTypeInformation[0].NumberOfPages, (UINT32)5);

+  ASSERT_EQ (gMemoryTypeInformation[0].NumberOfPages, ExpectedAlignedPages (EfiReservedMemoryType, 5));

   ASSERT_EQ (gMemoryTypeInformation[1].Type, (UINT32)EfiRuntimeServicesCode);

-  ASSERT_EQ (gMemoryTypeInformation[1].NumberOfPages, (UINT32)10);

+  ASSERT_EQ (gMemoryTypeInformation[1].NumberOfPages, ExpectedAlignedPages (EfiRuntimeServicesCode, 10));

   ASSERT_EQ (gMemoryTypeInformation[2].Type, (UINT32)EfiRuntimeServicesData);

-  ASSERT_EQ (gMemoryTypeInformation[2].NumberOfPages, (UINT32)15);

+  ASSERT_EQ (gMemoryTypeInformation[2].NumberOfPages, ExpectedAlignedPages (EfiRuntimeServicesData, 15));

   ASSERT_EQ (gMemoryTypeInformation[3].Type, (UINT32)EfiACPIReclaimMemory);

-  ASSERT_EQ (gMemoryTypeInformation[3].NumberOfPages, (UINT32)20);

+  ASSERT_EQ (gMemoryTypeInformation[3].NumberOfPages, ExpectedAlignedPages (EfiACPIReclaimMemory, 20));

   ASSERT_EQ (gMemoryTypeInformation[4].Type, (UINT32)EfiACPIMemoryNVS);

-  ASSERT_EQ (gMemoryTypeInformation[4].NumberOfPages, (UINT32)25);

+  ASSERT_EQ (gMemoryTypeInformation[4].NumberOfPages, ExpectedAlignedPages (EfiACPIMemoryNVS, 25));

   ASSERT_EQ (gMemoryTypeInformation[5].Type, (UINT32)EfiPalCode);

-  ASSERT_EQ (gMemoryTypeInformation[5].NumberOfPages, (UINT32)8);

+  ASSERT_EQ (gMemoryTypeInformation[5].NumberOfPages, ExpectedAlignedPages (EfiPalCode, 8));

 }

 

 //