From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-fw-9103.amazon.com (smtp-fw-9103.amazon.com [207.171.188.200]) by mx.groups.io with SMTP id smtpd.web11.36354.1650923119243711636 for ; Mon, 25 Apr 2022 14:45:19 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=A1G9idrQ; spf=pass (domain: amazon.de, ip: 207.171.188.200, mailfrom: prvs=107c543a1=ncoleon@amazon.de) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1650923119; x=1682459119; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=1mNUfAUvCkwHisws9ROkQ14KX3bp1Kyco4x3LWzrpfI=; b=A1G9idrQncATusaNybOtJPhN392PXrArEyV9vKng5yewCclqERHBx2sq qhsfGe5A5ljvMxvw7M+X+p4lKzpU5qV80Dp5Lw+/mORHCjqy2cZQ66WO3 qvBIZzyhfLNE2hE6uAMsTFetRtfuDSrhXzdsBvC6bhYwRtIkWJcDyjdxo M=; X-IronPort-AV: E=Sophos;i="5.90,289,1643673600"; d="scan'208";a="1010546285" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-pdx-2b-22c2b493.us-west-2.amazon.com) ([10.25.36.210]) by smtp-border-fw-9103.sea19.amazon.com with ESMTP; 25 Apr 2022 21:45:02 +0000 Received: from EX13D49EUC003.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-pdx-2b-22c2b493.us-west-2.amazon.com (Postfix) with ESMTPS id 7982740E3C; Mon, 25 Apr 2022 21:45:02 +0000 (UTC) Received: from ub4014a598e6c52.ant.amazon.com (10.43.161.114) by EX13D49EUC003.ant.amazon.com (10.43.164.91) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Mon, 25 Apr 2022 21:44:59 +0000 From: "Ojeda Leon, Nicolas" To: CC: Nicolas Ojeda Leon , Alexander Graf , Gerd Hoffmann Subject: [PATCH v5 4/5] Ovmf/PlatformPei: Use host-provided GPA end if available Date: Mon, 25 Apr 2022 23:43:49 +0200 Message-ID: <27e815bf6eb94fd36b45a98e760c318a54422012.1650918674.git.ncoleon@amazon.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.43.161.114] X-ClientProxiedBy: EX13D05UWC002.ant.amazon.com (10.43.162.92) To EX13D49EUC003.ant.amazon.com (10.43.164.91) Content-Type: text/plain Read the "hardware-info" item from fw-cfg to extract specifications of PCI host bridges and analyze the 64-bit apertures of them to find out the highest 64-bit MMIO address required which determines the address space required by the guest, and, consequently, the FirstNonAddress used to calculate size of physical addresses. Using the static PeiHardwareInfoLib, read the fw-cfg file of hardware information to extract, one by one, all the host bridges. Find the last 64-bit MMIO address of each host bridge, using the HardwareInfoPciHostBridgeLib API, and compare it to an accumulate value to discover the highest address used, which corresponds to the highest value that must be included in the guest's physical address space. Given that platforms with multiple host bridges may provide the PCI apertures' addresses, the memory detection logic must take into account that, if the host provided the MMIO windows that can and must be used, the guest needs to take those values. Therefore, if the MMIO windows are found in the host-provided fw-cfg file, skip all the logic calculating the physical address size and just use the value provided. Since each PCI host bridge corresponds to an element in the information provided by the host, each of these must be analyzed looking for the highest address used. Cc: Alexander Graf Cc: Gerd Hoffmann Acked-by: Gerd Hoffmann Signed-off-by: Nicolas Ojeda Leon --- Notes: v5: - Moved all changes from MemDetect.c into recently created PlatformInitLib/MemDetect.c effectively re-basing this patch onto latest master - Adapted function name to use Platform____ convention without any changes in the logic OvmfPkg/Library/PlatformInitLib/MemDetect.c | 148 +++++++++++++++++++- OvmfPkg/PlatformPei/PlatformPei.inf | 1 + 2 files changed, 142 insertions(+), 7 deletions(-) diff --git a/OvmfPkg/Library/PlatformInitLib/MemDetect.c b/OvmfPkg/Library/PlatformInitLib/MemDetect.c index 4c1dedf863..daca619c11 100644 --- a/OvmfPkg/Library/PlatformInitLib/MemDetect.c +++ b/OvmfPkg/Library/PlatformInitLib/MemDetect.c @@ -27,6 +27,7 @@ Module Name: #include #include #include +#include #include #include #include @@ -491,6 +492,126 @@ PlatformGetFirstNonAddress ( return FirstNonAddress; } +/** + Iterate over the PCI host bridges resources information optionally provided + in fw-cfg and find the highest address contained in the PCI MMIO windows. If + the information is found, return the exclusive end; one past the last usable + address. + + @param[out] PciMmioAddressEnd Pointer to one-after End Address updated with + information extracted from host-provided data + or zero if no information available or an + error happened + + @retval EFI_SUCCESS PCI information was read and the output + parameter updated with the last valid + address in the 64-bit MMIO range. + @retval EFI_INVALID_PARAMETER Pointer parameter is invalid + @retval EFI_INCOMPATIBLE_VERSION Hardware information found in fw-cfg + has an incompatible format + @retval EFI_UNSUPPORTED Fw-cfg is not supported, thus host + provided information, if any, cannot be + read + @retval EFI_NOT_FOUND No PCI host bridge information provided + by the host. +**/ +STATIC +EFI_STATUS +PlatformScanHostProvided64BitPciMmioEnd ( + OUT UINT64 *PciMmioAddressEnd + ) +{ + EFI_STATUS Status; + HOST_BRIDGE_INFO HostBridge; + FIRMWARE_CONFIG_ITEM FwCfgItem; + UINTN FwCfgSize; + UINTN FwCfgReadIndex; + UINTN ReadDataSize; + UINT64 Above4GMmioEnd; + + if (PciMmioAddressEnd == NULL) { + return EFI_INVALID_PARAMETER; + } + + *PciMmioAddressEnd = 0; + Above4GMmioEnd = 0; + + Status = QemuFwCfgFindFile ("etc/hardware-info", &FwCfgItem, &FwCfgSize); + if (EFI_ERROR (Status)) { + return Status; + } + + QemuFwCfgSelectItem (FwCfgItem); + + FwCfgReadIndex = 0; + while (FwCfgReadIndex < FwCfgSize) { + Status = QemuFwCfgReadNextHardwareInfoByType ( + HardwareInfoTypeHostBridge, + sizeof (HostBridge), + FwCfgSize, + &HostBridge, + &ReadDataSize, + &FwCfgReadIndex + ); + + if (Status != EFI_SUCCESS) { + // + // No more data available to read in the file, break + // loop and finish process + // + break; + } + + Status = HardwareInfoPciHostBridgeLastMmioAddress ( + &HostBridge, + ReadDataSize, + TRUE, + &Above4GMmioEnd + ); + + if (Status != EFI_SUCCESS) { + // + // Error parsing MMIO apertures and extracting last MMIO + // address, reset PciMmioAddressEnd as if no information was + // found, to avoid moving forward with incomplete data, and + // bail out + // + DEBUG (( + DEBUG_ERROR, + "%a: ignoring malformed hardware information from fw_cfg\n", + __FUNCTION__ + )); + *PciMmioAddressEnd = 0; + return Status; + } + + if (Above4GMmioEnd > *PciMmioAddressEnd) { + *PciMmioAddressEnd = Above4GMmioEnd; + } + } + + if (*PciMmioAddressEnd > 0) { + // + // Host-provided PCI information was found and a MMIO window end + // derived from it. + // Increase the End address by one to have the output pointing to + // one after the address in use (exclusive end). + // + *PciMmioAddressEnd += 1; + + DEBUG (( + DEBUG_INFO, + "%a: Pci64End=0x%Lx\n", + __FUNCTION__, + *PciMmioAddressEnd + )); + + return EFI_SUCCESS; + } + + return EFI_NOT_FOUND; +} + /** Initialize the PhysMemAddressWidth field in PlatformInfoHob based on guest RAM size. **/ @@ -500,16 +621,29 @@ PlatformAddressWidthInitialization ( IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob ) { - UINT64 FirstNonAddress; - UINT8 PhysMemAddressWidth; + UINT64 FirstNonAddress; + UINT8 PhysMemAddressWidth; + EFI_STATUS Status; // - // As guest-physical memory size grows, the permanent PEI RAM requirements - // are dominated by the identity-mapping page tables built by the DXE IPL. - // The DXL IPL keys off of the physical address bits advertized in the CPU - // HOB. To conserve memory, we calculate the minimum address width here. + // First scan host-provided hardware information to assess if the address + // space is already known. If so, guest must use those values. // - FirstNonAddress = PlatformGetFirstNonAddress (PlatformInfoHob); + Status = PlatformScanHostProvided64BitPciMmioEnd (&FirstNonAddress); + + if (EFI_ERROR (Status)) { + // + // If the host did not provide valid hardware information leading to a + // hard-defined 64-bit MMIO end, fold back to calculating the minimum range + // needed. + // As guest-physical memory size grows, the permanent PEI RAM requirements + // are dominated by the identity-mapping page tables built by the DXE IPL. + // The DXL IPL keys off of the physical address bits advertized in the CPU + // HOB. To conserve memory, we calculate the minimum address width here. + // + FirstNonAddress = PlatformGetFirstNonAddress (PlatformInfoHob); + } + PhysMemAddressWidth = (UINT8)HighBitSet64 (FirstNonAddress); // diff --git a/OvmfPkg/PlatformPei/PlatformPei.inf b/OvmfPkg/PlatformPei/PlatformPei.inf index 00372fa0eb..258a016a02 100644 --- a/OvmfPkg/PlatformPei/PlatformPei.inf +++ b/OvmfPkg/PlatformPei/PlatformPei.inf @@ -65,6 +65,7 @@ PcdLib VmgExitLib PlatformInitLib + PeiHardwareInfoLib [Pcd] gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvBase -- 2.32.0 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879