From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mx.groups.io with SMTP id smtpd.web08.25221.1626682115753516692 for ; Mon, 19 Jul 2021 01:08:36 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: arm.com, ip: 217.140.110.172, mailfrom: sunny.wang@arm.com) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3F524D6E; Mon, 19 Jul 2021 01:08:34 -0700 (PDT) Received: from localhost.localdomain (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id C54A23F73D; Mon, 19 Jul 2021 01:08:32 -0700 (PDT) From: "Sunny Wang" To: devel@edk2.groups.io Cc: Sunny Wang , Samer El-Haj-Mahmoud , G Edhaya Chandran , Barton Gao , Sunny Wang Subject: [edk2-test][PATCH v1 1/1] uefi-sct/SctPkg: Update page alignment calculations Date: Mon, 19 Jul 2021 16:07:59 +0800 Message-Id: <20210719080759.824-1-Sunny.Wang@arm.com> X-Mailer: git-send-email 2.31.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable This is to fix the SCT BS.AllocatePages failures (not found) with the case that the Start address is not aligned to 64k. For example, The following is available memory region for testing: 0000000082012000-00000000EB6D9FFF 00000000000696C8 With the current page alignment calculation, we will get: Start address is 0x82020000 PageNum is 0x696B8 In BS.AllocatePages, it will make the end address align with 64k, so PageNum will be changed from 0x696B8 to 0x696C0. Therefore, the end address will become 0xEB6E0000 which is larger than 0xEB6D9FFF, so we get not found error in the end. Therefore, the calculation for getting the PageNum should be updated to PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)) so that we won't get a wrong PageNum to allocate a memory with a size larger than available space's size. With this solution, the example above will get 0x696A8 as calculated PageNum. Then, in BS.AllocatePages, the PageNum will be changed from 0x696A8 to 0x696B0. Therefore, the end address will become 0xEB6D0000 that is smaller than 0xEB6D9FFF, so we get not found error in the end. I also tested this solution on two ARM platforms (NXP1046A and RPi4). Cc: Samer El-Haj-Mahmoud Cc: G Edhaya Chandran Cc: Barton Gao Signed-off-by: Sunny Wang --- .../MemoryAllocationServicesBBTestFunction.c | 110 +++++++++++------- 1 file changed, 66 insertions(+), 44 deletions(-) diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocat= ionServices/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c b/uefi-= sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationServices/BlackB= oxTest/MemoryAllocationServicesBBTestFunction.c index bf8cd3b3..cdfac992 100644 --- a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationServ= ices/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c +++ b/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/MemoryAllocationServ= ices/BlackBoxTest/MemoryAllocationServicesBBTestFunction.c @@ -2,6 +2,7 @@ =20 Copyright 2006 - 2013 Unified EFI, Inc.
Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.
+ Copyright (c) 2021, ARM Limited. All rights reserved. =20 This program and the accompanying materials are licensed and made available under the terms and conditions of the = BSD License @@ -24,7 +25,7 @@ Abstract: =20 --*/ =20 -#include "SctLib.h" +#include "SctLib.h" #include "MemoryAllocationServicesBBTestMain.h" =20 #define ALLOCATEPAGES_MEMORYTYPE_NUM 16 @@ -700,14 +701,17 @@ BBTestAllocatePagesInterfaceTest ( PageNum =3D (UINTN)Descriptor.NumberOfPages; Start =3D Descriptor.PhysicalStart; =20 - // - // Some memory types need more alignment than 4K, so - // - if (PageNum <=3D 0x10) { + // + // Calculate New Start address and PageNum with 64k alignment to + // cover the case that some memory types' alignment is more than + // 4k. If the available memory is less than 192k, the memory + // allocation call will be skipped. + // + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { break; } - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); =20 Memory =3D Start; =20 @@ -830,14 +834,17 @@ BBTestAllocatePagesInterfaceTest ( PageNum =3D (UINTN)Descriptor.NumberOfPages; Start =3D Descriptor.PhysicalStart; =20 - // - // Some memory types need more alignment than 4K, so - // - if (PageNum <=3D 0x10) { + // + // Calculate New Start address and PageNum with 64k alignment to + // cover the case that some memory types' alignment is more than + // 4k. If the available memory is less than 192k, the memory + // allocation call will be skipped. + // + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { break; } - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); =20 Memory =3D Start; =20 @@ -953,14 +960,17 @@ BBTestAllocatePagesInterfaceTest ( PageNum =3D (UINTN)Descriptor.NumberOfPages; Start =3D Descriptor.PhysicalStart; =20 - // - // Some memory types need more alignment than 4K, so - // - if (PageNum <=3D 0x10) { + // + // Calculate New Start address and PageNum with 64k alignment to + // cover the case that some memory types' alignment is more than + // 4k. If the available memory is less than 192k, the memory + // allocation call will be skipped. + // + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { break; } - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); =20 Memory =3D Start + (SctLShiftU64 (PageNum/3, EFI_PAGE_SHIFT) & 0= xFFFFFFFFFFFF0000); =20 @@ -1076,14 +1086,17 @@ BBTestAllocatePagesInterfaceTest ( PageNum =3D (UINTN)Descriptor.NumberOfPages; Start =3D Descriptor.PhysicalStart; =20 - // - // Some memory types need more alignment than 4K, so - // - if (PageNum <=3D 0x10) { + // + // Calculate New Start address and PageNum with 64k alignment to + // cover the case that some memory types' alignment is more than + // 4k. If the available memory is less than 192k, the memory + // allocation call will be skipped. + // + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { break; } - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); =20 Memory =3D Start + (SctLShiftU64 (PageNum * 2 / 3, EFI_PAGE_SHI= FT) & 0xFFFFFFFFFFFF0000); =20 @@ -1206,14 +1219,17 @@ BBTestAllocatePagesInterfaceTest ( PageNum =3D (UINTN)Descriptor.NumberOfPages; Start =3D Descriptor.PhysicalStart; =20 - // - // Some memory types need more alignment than 4K, so - // - if (PageNum <=3D 0x10) { + // + // Calculate New Start address and PageNum with 64k alignment to + // cover the case that some memory types' alignment is more than + // 4k. If the available memory is less than 192k, the memory + // allocation call will be skipped. + // + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { break; } - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); =20 Memory =3D Start; =20 @@ -1329,14 +1345,17 @@ BBTestAllocatePagesInterfaceTest ( PageNum =3D (UINTN)Descriptor.NumberOfPages; Start =3D Descriptor.PhysicalStart; =20 - // - // Some memory types need more alignment than 4K, so - // - if (PageNum <=3D 0x10) { + // + // Calculate New Start address and PageNum with 64k alignment to + // cover the case that some memory types' alignment is more than + // 4k. If the available memory is less than 192k, the memory + // allocation call will be skipped. + // + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { break; } - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); =20 Memory =3D Start; =20 @@ -1468,14 +1487,17 @@ BBTestAllocatePagesInterfaceTest ( PageNum =3D (UINTN)Descriptor.NumberOfPages; Start =3D Descriptor.PhysicalStart; =20 - // - // Some memory types need more alignment than 4K, so - // - if (PageNum <=3D 0x10) { + // + // Calculate New Start address and PageNum with 64k alignment to + // cover the case that some memory types' alignment is more than + // 4k. If the available memory is less than 192k, the memory + // allocation call will be skipped. + // + if (PageNum < (3 * EFI_SIZE_TO_PAGES(0x10000))) { break; } - Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; - PageNum =3D PageNum - EFI_SIZE_TO_PAGES(0x10000); + Start =3D (Start + 0xFFFF) & 0xFFFFFFFFFFFF0000; + PageNum =3D PageNum - (2 * EFI_SIZE_TO_PAGES(0x10000)); =20 Memory =3D Start; =20 @@ -1923,4 +1945,4 @@ BBTestFreePoolInterfaceTest ( =20 FreeMemoryMap (); return EFI_SUCCESS; -} +} --=20 2.31.0.windows.1