From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web09.3237.1663027349527091004 for ; Mon, 12 Sep 2022 17:02:46 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=FtpVQ2V7; spf=pass (domain: intel.com, ip: 192.55.52.43, mailfrom: min.m.xu@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1663027365; x=1694563365; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=E3xp0tyvPWixSXZKVHZ6p50l6L+8xXp5Ve526/5T8yc=; b=FtpVQ2V7aOGC27cmY3fioA80Yydxc5krL1ZnVhWgWuRmSVRMZ5wKrrzO /nrC02vcfUjTCM3xnpRh7vh4NddKnM5+PJyfJzA3f1rSEvmAFl1CzBlzL HraAo2ae0hiZw9LjRAV7ieAv4zPOR56bkruEzavW3KVyoZEShNDg9SOZ5 s9ZrnblKsuRRq+PsXHIw3LS+iEAUB7EPjUbJcFhgyKyrL9XXybAJCYUab zgUEIC0qjZG4z0nEMGhBpbKRght7Mqu39ZubLEHhb0bpq2wnJEaLibIoe OrUm7x52ymCpNk7J/ehNNzxVbq2Mf1Lo5eQArkH/hlWzduiCyPkHniG1e Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10468"; a="384294464" X-IronPort-AV: E=Sophos;i="5.93,311,1654585200"; d="scan'208";a="384294464" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Sep 2022 17:02:45 -0700 X-IronPort-AV: E=Sophos;i="5.93,311,1654585200"; d="scan'208";a="678320919" Received: from mxu9-mobl1.ccr.corp.intel.com ([10.254.212.104]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Sep 2022 17:02:43 -0700 From: "Min Xu" To: devel@edk2.groups.io Cc: Min M Xu , Erdem Aktas , Gerd Hoffmann , James Bottomley , Jiewen Yao , Tom Lendacky Subject: [PATCH V4 07/10] OvmfPkg: Introduce lazy accept in PlatformInitLib and PlatformPei Date: Tue, 13 Sep 2022 08:02:11 +0800 Message-Id: <7b67ee2b4b49c696edb2c519840f50600c6d334f.1663026445.git.min.m.xu@intel.com> X-Mailer: git-send-email 2.29.2.windows.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Min M Xu RFC: https://bugzilla.tianocore.org/show_bug.cgi?id=3937 There are below major changes in PlatformInitLib/PlatformPei 1. ProcessHobList The unaccepted memory is accepted if it is under 4G address. Please be noted: in current stage, we only accept the memory under 4G. We will re-visit here in the future when on-demand accept memory is required. 2. TransferTdxHobList Transfer the unaccepted memory hob to EFI_RESOURCE_SYSTEM_MEMORY hob if it is accepted. Cc: Erdem Aktas Cc: Gerd Hoffmann Cc: James Bottomley Cc: Jiewen Yao Cc: Tom Lendacky Signed-off-by: Min Xu --- OvmfPkg/Library/PlatformInitLib/IntelTdx.c | 88 ++++++++++++++++++--- OvmfPkg/Library/PlatformInitLib/MemDetect.c | 2 + 2 files changed, 81 insertions(+), 9 deletions(-) diff --git a/OvmfPkg/Library/PlatformInitLib/IntelTdx.c b/OvmfPkg/Library/PlatformInitLib/IntelTdx.c index 797f880df035..acd114e38e46 100644 --- a/OvmfPkg/Library/PlatformInitLib/IntelTdx.c +++ b/OvmfPkg/Library/PlatformInitLib/IntelTdx.c @@ -7,6 +7,7 @@ **/ +#include #include #include #include @@ -25,6 +26,7 @@ #include #define ALIGNED_2MB_MASK 0x1fffff +#define MEGABYTE_SHIFT 20 /** This function will be called to accept pages. Only BSP accepts pages. @@ -375,11 +377,15 @@ ProcessHobList ( EFI_STATUS Status; EFI_PEI_HOB_POINTERS Hob; EFI_PHYSICAL_ADDRESS PhysicalEnd; + UINT64 ResourceLength; + UINT64 AccumulateAcceptedMemory; Status = EFI_SUCCESS; ASSERT (VmmHobList != NULL); Hob.Raw = (UINT8 *)VmmHobList; + AccumulateAcceptedMemory = 0; + // // Parse the HOB list until end of list or matching type is found. // @@ -393,7 +399,15 @@ ProcessHobList ( DEBUG ((DEBUG_INFO, "ResourceLength: 0x%llx\n", Hob.ResourceDescriptor->ResourceLength)); DEBUG ((DEBUG_INFO, "Owner: %g\n\n", &Hob.ResourceDescriptor->Owner)); - PhysicalEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength; + PhysicalEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength; + ResourceLength = Hob.ResourceDescriptor->ResourceLength; + + if (Hob.ResourceDescriptor->PhysicalStart >= BASE_4GB) { + // + // In current stage, we only accept the memory under 4G + // + break; + } Status = BspAcceptMemoryResourceRange ( Hob.ResourceDescriptor->PhysicalStart, @@ -402,6 +416,8 @@ ProcessHobList ( if (EFI_ERROR (Status)) { break; } + + AccumulateAcceptedMemory += ResourceLength; } } @@ -460,6 +476,60 @@ ProcessTdxHobList ( return Status; } +/** + * Build ResourceDescriptorHob for the unaccepted memory region. + * This memory region may be splitted into 2 parts because of lazy accept. + * + * @param Hob Point to the EFI_HOB_RESOURCE_DESCRIPTOR + * @return VOID + */ +VOID +BuildResourceDescriptorHobForUnacceptedMemory ( + IN EFI_HOB_RESOURCE_DESCRIPTOR *Hob + ) +{ + EFI_PHYSICAL_ADDRESS PhysicalStart; + EFI_PHYSICAL_ADDRESS PhysicalEnd; + UINT64 ResourceLength; + EFI_RESOURCE_TYPE ResourceType; + EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute; + UINT64 MaxAcceptedMemoryAddress; + + ASSERT (Hob->ResourceType == BZ3937_EFI_RESOURCE_MEMORY_UNACCEPTED); + + ResourceType = BZ3937_EFI_RESOURCE_MEMORY_UNACCEPTED; + ResourceAttribute = Hob->ResourceAttribute; + PhysicalStart = Hob->PhysicalStart; + ResourceLength = Hob->ResourceLength; + PhysicalEnd = PhysicalStart + ResourceLength; + + // + // In the first stage of lazy-accept, all the memory under 4G will be accepted. + // The memory above 4G will not be accepted. + // + MaxAcceptedMemoryAddress = BASE_4GB; + + if (PhysicalEnd <= MaxAcceptedMemoryAddress) { + // + // This memory region has been accepted. + // + ResourceType = EFI_RESOURCE_SYSTEM_MEMORY; + ResourceAttribute |= (EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED); + } else if (PhysicalStart >= MaxAcceptedMemoryAddress) { + // + // This memory region hasn't been accepted. + // So keep the ResourceType and ResourceAttribute unchange. + // + } + + BuildResourceDescriptorHob ( + ResourceType, + ResourceAttribute, + PhysicalStart, + ResourceLength + ); +} + /** Transfer the incoming HobList for the TD to the final HobList for Dxe. The Hobs transferred in this function are ResourceDescriptor hob and @@ -489,16 +559,16 @@ TransferTdxHobList ( ResourceAttribute = Hob.ResourceDescriptor->ResourceAttribute; if (ResourceType == BZ3937_EFI_RESOURCE_MEMORY_UNACCEPTED) { - ResourceType = EFI_RESOURCE_SYSTEM_MEMORY; - ResourceAttribute |= (EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED); + BuildResourceDescriptorHobForUnacceptedMemory (Hob.ResourceDescriptor); + } else { + BuildResourceDescriptorHob ( + ResourceType, + ResourceAttribute, + Hob.ResourceDescriptor->PhysicalStart, + Hob.ResourceDescriptor->ResourceLength + ); } - BuildResourceDescriptorHob ( - ResourceType, - ResourceAttribute, - Hob.ResourceDescriptor->PhysicalStart, - Hob.ResourceDescriptor->ResourceLength - ); break; case EFI_HOB_TYPE_MEMORY_ALLOCATION: BuildMemoryAllocationHob ( diff --git a/OvmfPkg/Library/PlatformInitLib/MemDetect.c b/OvmfPkg/Library/PlatformInitLib/MemDetect.c index 942eaf89cfcf..62132f9cacfa 100644 --- a/OvmfPkg/Library/PlatformInitLib/MemDetect.c +++ b/OvmfPkg/Library/PlatformInitLib/MemDetect.c @@ -42,6 +42,8 @@ Module Name: #include +#define MEGABYTE_SHIFT 20 + VOID EFIAPI PlatformQemuUc32BaseInitialization ( -- 2.29.2.windows.2