From: Pankaj Bansal <pankaj.ban...@nxp.com>

The BBTestAllocatePagesInterfaceTest tries to allocate pages for
different memory types.
While doing so, it tries to fix up the Start and PageNum for 64K
Page size. There are multiple issues with this:

1. 64K alignment is being done regardless of Processor type and Memory
   type. while this is correct for ARM64 Processor, it might not be so
   for other Processor types. Also 64K alignment for ARM64 Processor
   is needed for some Memory types not all.
2. The Start is being incremented by 64K, even if Start is already 64K
   aligned.
3. PageNum is being decreased by 16 pages indiscriminately, which might
   not be needed in all cases.

fix all these issues by correctly doing the alignment in all needed
cases.

Cc: Paul Yang <paul.y...@arm.com>
Cc: Samer El-Haj-Mahmoud <samer.el-haj-mahm...@arm.com>
Cc: Gaurav Jain <gaurav.j...@nxp.com>
Signed-off-by: Pankaj Bansal <pankaj.ban...@nxp.com>
---
 .../MemoryAllocationServicesBBTestFunction.c  | 148 +++++++++++++-----
 1 file changed, 106 insertions(+), 42 deletions(-)

diff --git 
a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationServices/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c
 
b/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationServices/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c
index d18fe1fc2b94..9ed9e6e0de74 100644
--- 
a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationServices/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c
+++ 
b/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationServices/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c
@@ -354,6 +354,7 @@ BBTestAllocatePagesInterfaceTest (
   EFI_TPL                              OldTpl;
   EFI_MEMORY_DESCRIPTOR                Descriptor;
   UINTN                                PageNum;
+  UINTN                                Alignment;
 
   //
   // Get the Standard Library Interface
@@ -700,14 +701,23 @@ BBTestAllocatePagesInterfaceTest (
         PageNum = (UINTN)Descriptor.NumberOfPages;
         Start   = Descriptor.PhysicalStart;
 
-        //
-        // Some memory types need more alignment than 4K, so
-        //
-        if (PageNum <= 0x10) {
+        Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
+
+        if  (AllocatePagesMemoryType[TypeIndex] == EfiACPIReclaimMemory   ||
+             AllocatePagesMemoryType[TypeIndex] == EfiACPIMemoryNVS       ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesCode ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesData) {
+
+          Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
+        }
+
+        Start   = (Start + Alignment - 1) & ~(Alignment - 1);
+        PageNum -= EFI_SIZE_TO_PAGES (Start - Descriptor.PhysicalStart);
+
+        PageNum &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
+        if (PageNum <= EFI_SIZE_TO_PAGES (Alignment)) {
           break;
         }
-        Start   = (Start + 0x10000) & 0xFFFFFFFFFFFF0000;
-        PageNum = PageNum - EFI_SIZE_TO_PAGES(0x10000);
 
         Memory  = Start;
 
@@ -830,14 +840,23 @@ BBTestAllocatePagesInterfaceTest (
         PageNum = (UINTN)Descriptor.NumberOfPages;
         Start   = Descriptor.PhysicalStart;
 
-        //
-        // Some memory types need more alignment than 4K, so
-        //
-        if (PageNum <= 0x10) {
+        Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
+
+        if  (AllocatePagesMemoryType[TypeIndex] == EfiACPIReclaimMemory   ||
+             AllocatePagesMemoryType[TypeIndex] == EfiACPIMemoryNVS       ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesCode ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesData) {
+
+          Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
+        }
+
+        Start   = (Start + Alignment - 1) & ~(Alignment - 1);
+        PageNum -= EFI_SIZE_TO_PAGES (Start - Descriptor.PhysicalStart);
+
+        PageNum &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
+        if (PageNum <= EFI_SIZE_TO_PAGES (Alignment)) {
           break;
         }
-        Start   = (Start + 0x10000) & 0xFFFFFFFFFFFF0000;
-        PageNum = PageNum - EFI_SIZE_TO_PAGES(0x10000);
 
         Memory  = Start;
 
@@ -953,14 +972,23 @@ BBTestAllocatePagesInterfaceTest (
         PageNum = (UINTN)Descriptor.NumberOfPages;
         Start   = Descriptor.PhysicalStart;
 
-        //
-        // Some memory types need more alignment than 4K, so
-        //
-        if (PageNum <= 0x10) {
+        Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
+
+        if  (AllocatePagesMemoryType[TypeIndex] == EfiACPIReclaimMemory   ||
+             AllocatePagesMemoryType[TypeIndex] == EfiACPIMemoryNVS       ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesCode ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesData) {
+
+          Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
+        }
+
+        Start   = (Start + Alignment - 1) & ~(Alignment - 1);
+        PageNum -= EFI_SIZE_TO_PAGES (Start - Descriptor.PhysicalStart);
+
+        PageNum &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
+        if (PageNum <= EFI_SIZE_TO_PAGES (Alignment)) {
           break;
         }
-        Start   = (Start + 0x10000) & 0xFFFFFFFFFFFF0000;
-        PageNum = PageNum - EFI_SIZE_TO_PAGES(0x10000);
 
         Memory = Start + (SctLShiftU64 (PageNum/3, EFI_PAGE_SHIFT) & 
0xFFFFFFFFFFFF0000);
 
@@ -1076,14 +1104,23 @@ BBTestAllocatePagesInterfaceTest (
         PageNum = (UINTN)Descriptor.NumberOfPages;
         Start   = Descriptor.PhysicalStart;
 
-        //
-        // Some memory types need more alignment than 4K, so
-        //
-        if (PageNum <= 0x10) {
+        Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
+
+        if  (AllocatePagesMemoryType[TypeIndex] == EfiACPIReclaimMemory   ||
+             AllocatePagesMemoryType[TypeIndex] == EfiACPIMemoryNVS       ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesCode ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesData) {
+
+          Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
+        }
+
+        Start   = (Start + Alignment - 1) & ~(Alignment - 1);
+        PageNum -= EFI_SIZE_TO_PAGES (Start - Descriptor.PhysicalStart);
+
+        PageNum &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
+        if (PageNum <= EFI_SIZE_TO_PAGES (Alignment)) {
           break;
         }
-        Start   = (Start + 0x10000) & 0xFFFFFFFFFFFF0000;
-        PageNum = PageNum - EFI_SIZE_TO_PAGES(0x10000);
 
         Memory  = Start + (SctLShiftU64 (PageNum * 2 / 3, EFI_PAGE_SHIFT) & 
0xFFFFFFFFFFFF0000);
 
@@ -1206,14 +1243,23 @@ BBTestAllocatePagesInterfaceTest (
         PageNum = (UINTN)Descriptor.NumberOfPages;
         Start   = Descriptor.PhysicalStart;
 
-        //
-        // Some memory types need more alignment than 4K, so
-        //
-        if (PageNum <= 0x10) {
+        Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
+
+        if  (AllocatePagesMemoryType[TypeIndex] == EfiACPIReclaimMemory   ||
+             AllocatePagesMemoryType[TypeIndex] == EfiACPIMemoryNVS       ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesCode ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesData) {
+
+          Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
+        }
+
+        Start   = (Start + Alignment - 1) & ~(Alignment - 1);
+        PageNum -= EFI_SIZE_TO_PAGES (Start - Descriptor.PhysicalStart);
+
+        PageNum &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
+        if (PageNum <= EFI_SIZE_TO_PAGES (Alignment)) {
           break;
         }
-        Start   = (Start + 0x10000) & 0xFFFFFFFFFFFF0000;
-        PageNum = PageNum - EFI_SIZE_TO_PAGES(0x10000);
 
         Memory  = Start;
 
@@ -1329,14 +1375,23 @@ BBTestAllocatePagesInterfaceTest (
         PageNum = (UINTN)Descriptor.NumberOfPages;
         Start   = Descriptor.PhysicalStart;
 
-        //
-        // Some memory types need more alignment than 4K, so
-        //
-        if (PageNum <= 0x10) {
+        Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
+
+        if  (AllocatePagesMemoryType[TypeIndex] == EfiACPIReclaimMemory   ||
+             AllocatePagesMemoryType[TypeIndex] == EfiACPIMemoryNVS       ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesCode ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesData) {
+
+          Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
+        }
+
+        Start   = (Start + Alignment - 1) & ~(Alignment - 1);
+        PageNum -= EFI_SIZE_TO_PAGES (Start - Descriptor.PhysicalStart);
+
+        PageNum &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
+        if (PageNum <= EFI_SIZE_TO_PAGES (Alignment)) {
           break;
         }
-        Start   = (Start + 0x10000) & 0xFFFFFFFFFFFF0000;
-        PageNum = PageNum - EFI_SIZE_TO_PAGES(0x10000);
 
         Memory  = Start;
 
@@ -1468,14 +1523,23 @@ BBTestAllocatePagesInterfaceTest (
         PageNum = (UINTN)Descriptor.NumberOfPages;
         Start   = Descriptor.PhysicalStart;
 
-        //
-        // Some memory types need more alignment than 4K, so
-        //
-        if (PageNum <= 0x10) {
+        Alignment = DEFAULT_PAGE_ALLOCATION_GRANULARITY;
+
+        if  (AllocatePagesMemoryType[TypeIndex] == EfiACPIReclaimMemory   ||
+             AllocatePagesMemoryType[TypeIndex] == EfiACPIMemoryNVS       ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesCode ||
+             AllocatePagesMemoryType[TypeIndex] == EfiRuntimeServicesData) {
+
+          Alignment = RUNTIME_PAGE_ALLOCATION_GRANULARITY;
+        }
+
+        Start   = (Start + Alignment - 1) & ~(Alignment - 1);
+        PageNum -= EFI_SIZE_TO_PAGES (Start - Descriptor.PhysicalStart);
+
+        PageNum &= ~(EFI_SIZE_TO_PAGES (Alignment) - 1);
+        if (PageNum <= EFI_SIZE_TO_PAGES (Alignment)) {
           break;
         }
-        Start   = (Start + 0x10000) & 0xFFFFFFFFFFFF0000;
-        PageNum = PageNum - EFI_SIZE_TO_PAGES(0x10000);
 
         Memory  = Start;
 
-- 
2.17.1


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#62055): https://edk2.groups.io/g/devel/message/62055
Mute This Topic: https://groups.io/mt/75299798/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to