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.web10.1953.1665367537898209499 for ; Sun, 09 Oct 2022 19:05:38 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: arm.com, ip: 217.140.110.172, mailfrom: dimitrije.pavlov@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 9DD871480; Sun, 9 Oct 2022 19:05:43 -0700 (PDT) Received: from mammon-apollo-f36.ausrd.arm.com (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 513023F67D; Sun, 9 Oct 2022 19:05:37 -0700 (PDT) From: "Dimitrije Pavlov" To: devel@edk2.groups.io Cc: G Edhaya Chandran , Jeff Booher-Kaeding , Samer El-Haj-Mahmoud , Sunny Wang Subject: [edk2-test][PATCH v1 1/1] uefi-sct/SctPkg: Check for memory below 4G Date: Sun, 9 Oct 2022 21:05:30 -0500 Message-Id: <20221010020530.1261275-1-dimitrije.pavlov@arm.com> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4009 Check if there is usable memory below 4 GiB before testing for allocation without the EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_CYCLE attribute. Cc: G Edhaya Chandran Cc: Jeff Booher-Kaeding Cc: Samer El-Haj-Mahmoud Cc: Sunny Wang Signed-off-by: Dimitrije Pavlov --- uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/PciRootBridgeIo/BlackBoxTest/= PciRootBridgeIoBBTestFunction_2.c | 142 +++++++++++++++++++- 1 file changed, 140 insertions(+), 2 deletions(-) diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/PciRootBridgeIo/B= lackBoxTest/PciRootBridgeIoBBTestFunction_2.c b/uefi-sct/SctPkg/TestCase/= UEFI/EFI/Protocol/PciRootBridgeIo/BlackBoxTest/PciRootBridgeIoBBTestFunct= ion_2.c index 89adcba91e70..fafbf62f77c6 100644 --- a/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/PciRootBridgeIo/BlackBox= Test/PciRootBridgeIoBBTestFunction_2.c +++ b/uefi-sct/SctPkg/TestCase/UEFI/EFI/Protocol/PciRootBridgeIo/BlackBox= Test/PciRootBridgeIoBBTestFunction_2.c @@ -4118,7 +4118,69 @@ AllocateBuffer_Func ( UINTN AttributesNum; EFI_PCI_ROOT_BRIDGE_IO_DEVICE *RBDev; EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *RootBridgeIo; + EFI_MEMORY_DESCRIPTOR *MemoryMap; + EFI_MEMORY_DESCRIPTOR *Entry; + UINTN MemoryMapSize; + UINTN MapKey; + UINTN DescriptorSize; + UINT32 DescriptorVersion; + UINTN Iterator; + BOOLEAN UsableMemoryBelow4G; =20 + // + // Obtain the memory map size + // + MemoryMapSize =3D 0; + Status =3D gBS->GetMemoryMap ( + &MemoryMapSize, + NULL, + &MapKey, + &DescriptorSize, + &DescriptorVersion + ); + ASSERT (Status =3D=3D EFI_BUFFER_TOO_SMALL); + // + // Allocating a buffer for the memory map will change + // the memory map, so we increase the size here just in case + // + MemoryMapSize +=3D EFI_PAGE_SIZE; + Status =3D gBS->AllocatePool ( + EfiLoaderData, + MemoryMapSize, + (VOID **)&MemoryMap + ); + if (EFI_ERROR (Status)) { + return Status; + } + // + // Get the actual memory map + // + Status =3D gBS->GetMemoryMap ( + &MemoryMapSize, + MemoryMap, + &MapKey, + &DescriptorSize, + &DescriptorVersion + ); + if (EFI_ERROR (Status)) { + gBS->FreePool (MemoryMap); + } + + // + // Check each entry in the memory map for free memory below 4 GiB and = set + // UsableMemoryBelow4G accordingly + // + UsableMemoryBelow4G =3D FALSE; + for (Iterator =3D 0; Iterator < MemoryMapSize; Iterator +=3D Descripto= rSize) { + Entry =3D (EFI_MEMORY_DESCRIPTOR *)((UINTN)MemoryMap + Iterator); + if ( Entry->PhysicalStart < (EFI_PHYSICAL_ADDRESS)SIZE_4GB + && ( Entry->Type =3D=3D EfiConventionalMemory || Entry->Type =3D=3D= EfiPersistentMemory)) + { + UsableMemoryBelow4G =3D TRUE; + break; + } + } + gBS->FreePool (MemoryMap); =20 AllocateType =3D 0; =20 @@ -4188,7 +4250,14 @@ AllocateBuffer_Func ( =20 for (MemoryTypeNum =3D 0; MemoryTypeNum < 2; MemoryTypeNum++) { for (AttributesNum =3D 0; AttributesNum < 8; AttributesNum++) { - + // + // If there is no usable memory below 4 GiB, skip 32-bit allocatio= ns + // + if ( !UsableMemoryBelow4G + && !(Attributes[AttributesNum] & EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_= CYCLE)) + { + continue; + } Status =3D RootBridgeIo->AllocateBuffer ( RootBridgeIo, AllocateType, @@ -4298,7 +4367,69 @@ FreeBuffer_Func ( UINTN AttributesNum; EFI_PCI_ROOT_BRIDGE_IO_DEVICE *RBDev; EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *RootBridgeIo; + EFI_MEMORY_DESCRIPTOR *MemoryMap; + EFI_MEMORY_DESCRIPTOR *Entry; + UINTN MemoryMapSize; + UINTN MapKey; + UINTN DescriptorSize; + UINT32 DescriptorVersion; + UINTN Iterator; + BOOLEAN UsableMemoryBelow4G; =20 + // + // Obtain the memory map size + // + MemoryMapSize =3D 0; + Status =3D gBS->GetMemoryMap ( + &MemoryMapSize, + NULL, + &MapKey, + &DescriptorSize, + &DescriptorVersion + ); + ASSERT (Status =3D=3D EFI_BUFFER_TOO_SMALL); + // + // Allocating a buffer for the memory map will change + // the memory map, so we increase the size here just in case + // + MemoryMapSize +=3D EFI_PAGE_SIZE; + Status =3D gBS->AllocatePool ( + EfiLoaderData, + MemoryMapSize, + (VOID **)&MemoryMap + ); + if (EFI_ERROR (Status)) { + return Status; + } + // + // Get the actual memory map + // + Status =3D gBS->GetMemoryMap ( + &MemoryMapSize, + MemoryMap, + &MapKey, + &DescriptorSize, + &DescriptorVersion + ); + if (EFI_ERROR (Status)) { + gBS->FreePool (MemoryMap); + } + + // + // Check each entry in the memory map for free memory below 4 GiB and = set + // UsableMemoryBelow4G accordingly + // + UsableMemoryBelow4G =3D FALSE; + for (Iterator =3D 0; Iterator < MemoryMapSize; Iterator +=3D Descripto= rSize) { + Entry =3D (EFI_MEMORY_DESCRIPTOR *)((UINTN)MemoryMap + Iterator); + if ( Entry->PhysicalStart < (EFI_PHYSICAL_ADDRESS)SIZE_4GB + && ( Entry->Type =3D=3D EfiConventionalMemory || Entry->Type =3D=3D= EfiPersistentMemory)) + { + UsableMemoryBelow4G =3D TRUE; + break; + } + } + gBS->FreePool (MemoryMap); =20 AllocateType =3D 0 ; =20 @@ -4368,7 +4499,14 @@ FreeBuffer_Func ( =20 for (MemoryTypeNum =3D 0; MemoryTypeNum < 2; MemoryTypeNum++) { for (AttributesNum =3D 0; AttributesNum < 8; AttributesNum++) { - + // + // If there is no usable memory below 4 GiB, skip 32-bit allocatio= ns + // + if ( !UsableMemoryBelow4G + && !(Attributes[AttributesNum] & EFI_PCI_ATTRIBUTE_DUAL_ADDRESS_= CYCLE)) + { + continue; + } Status =3D RootBridgeIo->AllocateBuffer ( RootBridgeIo, AllocateType, --=20 2.37.3