From nobody Tue Feb 10 01:15:42 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+98676+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+98676+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1673957798; cv=none; d=zohomail.com; s=zohoarc; b=czM+EIlkEK7jbGSc564LO1XF/iXF3kGOHEnIrE8deTHxvGiughVBtS//sm5YjgQxDXKQrKaxmlzCPDl0wKIQvTNOthbgHkSz1W62WRz4J2CW8KfSYGcKYzHj0l6DVSI51W8p1rc5EP1Aq4GEHltFSwLJdo0bD8C+QUAdaTHnhE8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1673957798; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=X9bdXRXx3YjKqGHN6RXZgtKDNHUqyu9YRi+crP9td9g=; b=GvvZxzXkUYzSm+QyleonUj/e71FVN9y8xL8FXImFQymQ6ZPs/ZZ4pdMJQqijUrmvA3h03nxtCphdl/AaqclyxQpFisGwSD+axgcqdOvQx7Sl3t44o20G8d4U/8NZpV3pgtxA/ssrRVO+3BS7qK9OvwuyVOlcqy6KlpamAA9SzJw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+98676+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1673957798142178.496079666627; Tue, 17 Jan 2023 04:16:38 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id ZhsbYY1788612xaHwgIdyiEa; Tue, 17 Jan 2023 04:16:37 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web11.194670.1673957796973690462 for ; Tue, 17 Jan 2023 04:16:37 -0800 X-Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-564-_QXgpShNP9SzflxOTCecLw-1; Tue, 17 Jan 2023 07:16:32 -0500 X-MC-Unique: _QXgpShNP9SzflxOTCecLw-1 X-Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C2F1E181E3F2; Tue, 17 Jan 2023 12:16:31 +0000 (UTC) X-Received: from sirius.home.kraxel.org (unknown [10.39.192.124]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 42A43492B00; Tue, 17 Jan 2023 12:16:31 +0000 (UTC) X-Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id EBBFE1800624; Tue, 17 Jan 2023 13:16:29 +0100 (CET) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: Jiewen Yao , Oliver Steffen , =?UTF-8?q?L=C3=A1szl=C3=B3=20=C3=89rsek?= , Ard Biesheuvel , Pawel Polawski , Jordan Justen , Gerd Hoffmann Subject: [edk2-devel] [PATCH v4 1/5] OvmfPkg/PlatformInitLib: Add PlatformScanE820 and GetFirstNonAddressCB Date: Tue, 17 Jan 2023 13:16:25 +0100 Message-Id: <20230117121629.2149112-2-kraxel@redhat.com> In-Reply-To: <20230117121629.2149112-1-kraxel@redhat.com> References: <20230117121629.2149112-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,kraxel@redhat.com X-Gm-Message-State: DNkbD84Ai66p3oBmXS62gjqsx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1673957797; bh=UDE8hYx7YY0gJmjjpXizrbmkjDyptLy0egLIziYhKhc=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=cr81KHHmF+Nstp9Z2jzw0njf3B2+/cdFtTSfMewxblwWpxTmE4Kd4fhpxvtsdSvHBsG 6leGcdklm1mBHiE6S6pPzbZCXuAs9OXXaRsiW51ngqFqRJLPQMZq/f6Bj3u2WCrlDxEpz kVeg4exlTECxmU0MfOkkUWTdKSPdeOha7iw= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1673957798709100006 Content-Type: text/plain; charset="utf-8"; x-default="true" First step replacing the PlatformScanOrAdd64BitE820Ram() function. Add a PlatformScanE820() function which loops over the e280 entries from FwCfg and calls a callback for each of them. Add a GetFirstNonAddressCB() function which will store the first free address (right after the last RAM block) in PlatformInfoHob->FirstNonAddress. This replaces calls to PlatformScanOrAdd64BitE820Ram() with non-NULL MaxAddress. Write any actions done (setting FirstNonAddress) to the firmware log with INFO loglevel. Also drop local FirstNonAddress variables and use PlatformInfoHob->FirstNonAddress instead everywhere. Signed-off-by: Gerd Hoffmann Reviewed-by: Laszlo Ersek --- OvmfPkg/Library/PlatformInitLib/MemDetect.c | 115 ++++++++++++++++---- 1 file changed, 92 insertions(+), 23 deletions(-) diff --git a/OvmfPkg/Library/PlatformInitLib/MemDetect.c b/OvmfPkg/Library/= PlatformInitLib/MemDetect.c index 882805269b3e..1ea91f78cefd 100644 --- a/OvmfPkg/Library/PlatformInitLib/MemDetect.c +++ b/OvmfPkg/Library/PlatformInitLib/MemDetect.c @@ -251,6 +251,83 @@ PlatformScanOrAdd64BitE820Ram ( return EFI_SUCCESS; } =20 +typedef VOID (*E820_SCAN_CALLBACK) ( + EFI_E820_ENTRY64 *E820Entry, + EFI_HOB_PLATFORM_INFO *PlatformInfoHob + ); + +/** + Store first address not used by e820 RAM entries in + PlatformInfoHob->FirstNonAddress +**/ +VOID +PlatformGetFirstNonAddressCB ( + IN EFI_E820_ENTRY64 *E820Entry, + IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob + ) +{ + UINT64 Candidate; + + if (E820Entry->Type !=3D EfiAcpiAddressRangeMemory) { + return; + } + + Candidate =3D E820Entry->BaseAddr + E820Entry->Length; + if (PlatformInfoHob->FirstNonAddress < Candidate) { + DEBUG ((DEBUG_INFO, "%a: FirstNonAddress=3D0x%Lx\n", __FUNCTION__, Can= didate)); + PlatformInfoHob->FirstNonAddress =3D Candidate; + } +} + +/** + Iterate over the entries in QEMU's fw_cfg E820 RAM map, call the + passed callback for each entry. + + @param[in] Callback The callback function to be called. + + @param[in out] PlatformInfoHob PlatformInfo struct which is passed + through to the callback. + + @retval EFI_SUCCESS The fw_cfg E820 RAM map was found and p= rocessed. + + @retval EFI_PROTOCOL_ERROR The RAM map was found, but its size was= n't a + whole multiple of sizeof(EFI_E820_ENTRY= 64). No + RAM entry was processed. + + @return Error codes from QemuFwCfgFindFile(). N= o RAM + entry was processed. +**/ +STATIC +EFI_STATUS +PlatformScanE820 ( + IN E820_SCAN_CALLBACK Callback, + IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob + ) +{ + EFI_STATUS Status; + FIRMWARE_CONFIG_ITEM FwCfgItem; + UINTN FwCfgSize; + EFI_E820_ENTRY64 E820Entry; + UINTN Processed; + + Status =3D QemuFwCfgFindFile ("etc/e820", &FwCfgItem, &FwCfgSize); + if (EFI_ERROR (Status)) { + return Status; + } + + if (FwCfgSize % sizeof E820Entry !=3D 0) { + return EFI_PROTOCOL_ERROR; + } + + QemuFwCfgSelectItem (FwCfgItem); + for (Processed =3D 0; Processed < FwCfgSize; Processed +=3D sizeof E820E= ntry) { + QemuFwCfgReadBytes (sizeof E820Entry, &E820Entry); + Callback (&E820Entry, PlatformInfoHob); + } + + return EFI_SUCCESS; +} + /** Returns PVH memmap =20 @@ -384,23 +461,17 @@ PlatformGetSystemMemorySizeAbove4gb ( Return the highest address that DXE could possibly use, plus one. **/ STATIC -UINT64 +VOID PlatformGetFirstNonAddress ( IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob ) { - UINT64 FirstNonAddress; UINT32 FwCfgPciMmio64Mb; EFI_STATUS Status; FIRMWARE_CONFIG_ITEM FwCfgItem; UINTN FwCfgSize; UINT64 HotPlugMemoryEnd; =20 - // - // set FirstNonAddress to suppress incorrect compiler/analyzer warnings - // - FirstNonAddress =3D 0; - // // If QEMU presents an E820 map, then get the highest exclusive >=3D4GB = RAM // address from it. This can express an address >=3D 4GB+1TB. @@ -408,9 +479,10 @@ PlatformGetFirstNonAddress ( // Otherwise, get the flat size of the memory above 4GB from the CMOS (w= hich // can only express a size smaller than 1TB), and add it to 4GB. // - Status =3D PlatformScanOrAdd64BitE820Ram (FALSE, NULL, &FirstNonAddress); + PlatformInfoHob->FirstNonAddress =3D BASE_4GB; + Status =3D PlatformScanE820 (PlatformGetFirstN= onAddressCB, PlatformInfoHob); if (EFI_ERROR (Status)) { - FirstNonAddress =3D BASE_4GB + PlatformGetSystemMemorySizeAbove4gb (); + PlatformInfoHob->FirstNonAddress =3D BASE_4GB + PlatformGetSystemMemor= ySizeAbove4gb (); } =20 // @@ -420,7 +492,7 @@ PlatformGetFirstNonAddress ( // #ifdef MDE_CPU_IA32 if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode)) { - return FirstNonAddress; + return; } =20 #endif @@ -473,7 +545,7 @@ PlatformGetFirstNonAddress ( // determines the highest address plus one. The memory hotplug area (s= ee // below) plays no role for the firmware in this case. // - return FirstNonAddress; + return; } =20 // @@ -497,15 +569,15 @@ PlatformGetFirstNonAddress ( HotPlugMemoryEnd )); =20 - ASSERT (HotPlugMemoryEnd >=3D FirstNonAddress); - FirstNonAddress =3D HotPlugMemoryEnd; + ASSERT (HotPlugMemoryEnd >=3D PlatformInfoHob->FirstNonAddress); + PlatformInfoHob->FirstNonAddress =3D HotPlugMemoryEnd; } =20 // // SeaBIOS aligns both boundaries of the 64-bit PCI host aperture to 1GB= , so // that the host can map it with 1GB hugepages. Follow suit. // - PlatformInfoHob->PcdPciMmio64Base =3D ALIGN_VALUE (FirstNonAddress, (UIN= T64)SIZE_1GB); + PlatformInfoHob->PcdPciMmio64Base =3D ALIGN_VALUE (PlatformInfoHob->Firs= tNonAddress, (UINT64)SIZE_1GB); PlatformInfoHob->PcdPciMmio64Size =3D ALIGN_VALUE (PlatformInfoHob->PcdP= ciMmio64Size, (UINT64)SIZE_1GB); =20 // @@ -519,8 +591,8 @@ PlatformGetFirstNonAddress ( // // The useful address space ends with the 64-bit PCI host aperture. // - FirstNonAddress =3D PlatformInfoHob->PcdPciMmio64Base + PlatformInfoHob-= >PcdPciMmio64Size; - return FirstNonAddress; + PlatformInfoHob->FirstNonAddress =3D PlatformInfoHob->PcdPciMmio64Base += PlatformInfoHob->PcdPciMmio64Size; + return; } =20 /* @@ -781,7 +853,6 @@ PlatformAddressWidthInitialization ( IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob ) { - UINT64 FirstNonAddress; UINT8 PhysMemAddressWidth; EFI_STATUS Status; =20 @@ -794,7 +865,7 @@ PlatformAddressWidthInitialization ( // First scan host-provided hardware information to assess if the address // space is already known. If so, guest must use those values. // - Status =3D PlatformScanHostProvided64BitPciMmioEnd (&FirstNonAddress); + Status =3D PlatformScanHostProvided64BitPciMmioEnd (&PlatformInfoHob->Fi= rstNonAddress); =20 if (EFI_ERROR (Status)) { // @@ -806,13 +877,12 @@ PlatformAddressWidthInitialization ( // The DXL IPL keys off of the physical address bits advertized in the= CPU // HOB. To conserve memory, we calculate the minimum address width her= e. // - FirstNonAddress =3D PlatformGetFirstNonAddress (PlatformInfoHob); + PlatformGetFirstNonAddress (PlatformInfoHob); } =20 PlatformAddressWidthFromCpuid (PlatformInfoHob, TRUE); if (PlatformInfoHob->PhysMemAddressWidth !=3D 0) { // physical address width is known - PlatformInfoHob->FirstNonAddress =3D FirstNonAddress; PlatformDynamicMmioWindow (PlatformInfoHob); return; } @@ -823,13 +893,13 @@ PlatformAddressWidthInitialization ( // -> try be conservstibe to stay below the guaranteed minimum of // 36 phys bits (aka 64 GB). // - PhysMemAddressWidth =3D (UINT8)HighBitSet64 (FirstNonAddress); + PhysMemAddressWidth =3D (UINT8)HighBitSet64 (PlatformInfoHob->FirstNonAd= dress); =20 // // If FirstNonAddress is not an integral power of two, then we need an // additional bit. // - if ((FirstNonAddress & (FirstNonAddress - 1)) !=3D 0) { + if ((PlatformInfoHob->FirstNonAddress & (PlatformInfoHob->FirstNonAddres= s - 1)) !=3D 0) { ++PhysMemAddressWidth; } =20 @@ -857,7 +927,6 @@ PlatformAddressWidthInitialization ( ASSERT (PhysMemAddressWidth <=3D 48); #endif =20 - PlatformInfoHob->FirstNonAddress =3D FirstNonAddress; PlatformInfoHob->PhysMemAddressWidth =3D PhysMemAddressWidth; } =20 --=20 2.39.0 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#98676): https://edk2.groups.io/g/devel/message/98676 Mute This Topic: https://groups.io/mt/96328404/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-