From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by mx.groups.io with SMTP id smtpd.web08.27749.1654484424914450989 for ; Sun, 05 Jun 2022 20:00:43 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=SJivAv+X; spf=pass (domain: intel.com, ip: 134.134.136.24, 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=1654484443; x=1686020443; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Bi2zJVbiJovMcoGJ7UPQXMI9Ord2ytDbH09dwq0nK6A=; b=SJivAv+XbdX9iKt9nR1yUOcbm+Xj03XD9s/akNNwYZoL86YNcgK38S1m 3Kav3wA3p78dTgg+uX4M9uY0Uy9wPSd01qQXrpg53Aq9fBSmBoDo+I4G8 PSEcMa+L2eGIP4nC+ZiiHRbu1tsirIuMQ/y4aYFu9wSeltVW3UTuUip1Z XdtEgSrk3RLA47H+Tq0eH7V13C12RbBSClU+7RZZ1wCBdkIfK0OgLggYG uX4ECLSNKhTQELXpOL19JVgFPVi5zU2cW0dCi9BIE0evn7pHn7VI3wqhO 1Wj/y0QFux1bzW5+o4sCGcFeRyDXwJiJaMzGS46jxVEI6uTn9i4DUXzOu g==; X-IronPort-AV: E=McAfee;i="6400,9594,10369"; a="276458143" X-IronPort-AV: E=Sophos;i="5.91,280,1647327600"; d="scan'208";a="276458143" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jun 2022 20:00:43 -0700 X-IronPort-AV: E=Sophos;i="5.91,280,1647327600"; d="scan'208";a="647285811" Received: from mxu9-mobl1.ccr.corp.intel.com ([10.255.29.66]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jun 2022 20:00:41 -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 10/14] OvmfPkg: Introduce lazy accept in PlatformInitLib and PlatformPei Date: Mon, 6 Jun 2022 10:59:58 +0800 Message-Id: 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 the accumulated accepted memory is smaller than the LazyAcceptMemSize. If a EFI_RESOURCE_MEMORY_UNACCEPTED hob is cross the LazyAcceptMemSize, it will be split into 2 parts and only the left one is accepted. The max accepted memory address is stored in Tdx workarea which will be used in TransferTdxHobList. 2. TransferTdxHobList Transfer the unaccepted memory hob to EFI_RESOURCE_SYSTEM_MEMORY hob if it is accepted. As it is mentioned in 1), there may be a EFI_RESOURCE_MEMORY_UNACCEPTED hob which only part of the memory describes in the hob is accepted. We also handles this situation in TransferTdxHobList. 3. PlatformAdjustSystemMemorySizeBelow4gbForLazyAccep The system memory size below 4GB may be larger than the accepted memory. This function is used to handle this situation. Cc: Erdem Aktas Cc: Gerd Hoffmann Cc: James Bottomley Cc: Jiewen Yao Cc: Tom Lendacky Signed-off-by: Min Xu --- OvmfPkg/Include/Library/PlatformInitLib.h | 6 + OvmfPkg/Library/PlatformInitLib/IntelTdx.c | 143 ++++++++++++++++-- OvmfPkg/Library/PlatformInitLib/MemDetect.c | 27 ++++ .../PlatformInitLib/PlatformInitLib.inf | 1 + OvmfPkg/PlatformPei/MemDetect.c | 5 + 5 files changed, 171 insertions(+), 11 deletions(-) diff --git a/OvmfPkg/Include/Library/PlatformInitLib.h b/OvmfPkg/Include/Library/PlatformInitLib.h index 2987a367cc9c..187efcf34e14 100644 --- a/OvmfPkg/Include/Library/PlatformInitLib.h +++ b/OvmfPkg/Include/Library/PlatformInitLib.h @@ -144,6 +144,12 @@ PlatformGetSystemMemorySizeBelow4gb ( IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob ); +UINT32 +EFIAPI +PlatformAdjustSystemMemorySizeBelow4gbForLazyAccept ( + IN UINT32 LowerMemorySize + ); + /** Initialize the PhysMemAddressWidth field in PlatformInfoHob based on guest RAM size. **/ diff --git a/OvmfPkg/Library/PlatformInitLib/IntelTdx.c b/OvmfPkg/Library/PlatformInitLib/IntelTdx.c index 396b14d919d2..7be466ee34d1 100644 --- a/OvmfPkg/Library/PlatformInitLib/IntelTdx.c +++ b/OvmfPkg/Library/PlatformInitLib/IntelTdx.c @@ -24,7 +24,8 @@ #include #include -#define ALIGNED_2MB_MASK 0x1fffff +#define ALIGNED_2MB_MASK 0x1fffff +#define MEGABYTE_SHIFT 20 /** This function will be called to accept pages. Only BSP accepts pages. @@ -375,15 +376,33 @@ ProcessHobList ( EFI_STATUS Status; EFI_PEI_HOB_POINTERS Hob; EFI_PHYSICAL_ADDRESS PhysicalEnd; + TDX_WORK_AREA *WorkArea; + UINT64 ResourceLength; + UINT64 AccumulateAcceptedMemory; + UINT64 LazyAcceptMemSize; + UINT64 MaxAcceptedMemoryAddress; Status = EFI_SUCCESS; ASSERT (VmmHobList != NULL); Hob.Raw = (UINT8 *)VmmHobList; + AccumulateAcceptedMemory = 0; + MaxAcceptedMemoryAddress = 0; + LazyAcceptMemSize = FixedPcdGet64 (PcdLazyAcceptPartialMemorySize); + // + // If specified accept size is zero, accept all of the memory. + // Else transfer the size in megabyte to the number in byte. + // + if (LazyAcceptMemSize == 0) { + LazyAcceptMemSize = MAX_UINT64; + } else { + LazyAcceptMemSize <<= MEGABYTE_SHIFT; + } + // // Parse the HOB list until end of list or matching type is found. // - while (!END_OF_HOB_LIST (Hob)) { + while (!END_OF_HOB_LIST (Hob) && AccumulateAcceptedMemory < LazyAcceptMemSize) { if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) { DEBUG ((DEBUG_INFO, "\nResourceType: 0x%x\n", Hob.ResourceDescriptor->ResourceType)); @@ -393,7 +412,17 @@ 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 (AccumulateAcceptedMemory + ResourceLength > LazyAcceptMemSize) { + // + // If the memory can't be accepted completely, accept the part of it to meet the + // PcdLazyAcceptPartialMemorySize. + // + ResourceLength = LazyAcceptMemSize - AccumulateAcceptedMemory; + PhysicalEnd = Hob.ResourceDescriptor->PhysicalStart + ResourceLength; + } Status = BspAcceptMemoryResourceRange ( Hob.ResourceDescriptor->PhysicalStart, @@ -402,12 +431,26 @@ ProcessHobList ( if (EFI_ERROR (Status)) { break; } + + AccumulateAcceptedMemory += ResourceLength; + MaxAcceptedMemoryAddress = PhysicalEnd; } } Hob.Raw = GET_NEXT_HOB (Hob); } + // + // Record MaxAcceptedMemoryAddress in OvmfWorkArea. + // This information is needed in TransferTdxHobList and ContructFwHobList(at PeilessStartupLib). + // But in SEC phase we cannot use a global variable to pass this value. So it has + // to be stored in OvmfWorkarea. + // + WorkArea = (TDX_WORK_AREA *)FixedPcdGet32 (PcdOvmfWorkAreaBase); + ASSERT (WorkArea != NULL); + ASSERT (WorkArea->Header.GuestType == CcGuestTypeIntelTdx); + WorkArea->SecTdxWorkArea.MaxAcceptedMemoryAddress = MaxAcceptedMemoryAddress; + return Status; } @@ -460,6 +503,74 @@ 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 + * @param MaxAcceptedMemoryAddress The max accepted memory address + * @return VOID + */ +VOID +BuildResourceDescriptorHobForUnacceptedMemory ( + IN EFI_HOB_RESOURCE_DESCRIPTOR *Hob, + IN UINT64 MaxAcceptedMemoryAddress + ) +{ + EFI_PHYSICAL_ADDRESS PhysicalStart; + EFI_PHYSICAL_ADDRESS PhysicalEnd; + UINT64 ResourceLength; + EFI_RESOURCE_TYPE ResourceType; + EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute; + UINT64 AcceptedResourceLength; + + ASSERT (Hob->ResourceType == EFI_RESOURCE_MEMORY_UNACCEPTED); + + ResourceType = EFI_RESOURCE_MEMORY_UNACCEPTED; + ResourceAttribute = Hob->ResourceAttribute; + PhysicalStart = Hob->PhysicalStart; + ResourceLength = Hob->ResourceLength; + PhysicalEnd = PhysicalStart + ResourceLength; + + 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. + // + } else { + // + // This memory region is splitted into 2 parts: + // the accepted and unaccepted. + // + AcceptedResourceLength = MaxAcceptedMemoryAddress - Hob->PhysicalStart; + + // We build the ResourceDescriptorHob for the accepted part. + // The unaccepted part will be build out side the if-else block. + BuildResourceDescriptorHob ( + EFI_RESOURCE_SYSTEM_MEMORY, + ResourceAttribute | (EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED), + Hob->PhysicalStart, + AcceptedResourceLength + ); + + PhysicalStart = Hob->PhysicalStart + AcceptedResourceLength; + ResourceLength -= AcceptedResourceLength; + } + + 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 @@ -477,6 +588,16 @@ TransferTdxHobList ( EFI_PEI_HOB_POINTERS Hob; EFI_RESOURCE_TYPE ResourceType; EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute; + UINT64 MaxAcceptedMemoryAddress; + TDX_WORK_AREA *WorkArea; + + WorkArea = (TDX_WORK_AREA *)FixedPcdGet32 (PcdOvmfWorkAreaBase); + ASSERT (WorkArea != NULL); + ASSERT (WorkArea->Header.GuestType == CcGuestTypeIntelTdx); + MaxAcceptedMemoryAddress = WorkArea->SecTdxWorkArea.MaxAcceptedMemoryAddress; + if (MaxAcceptedMemoryAddress == 0) { + MaxAcceptedMemoryAddress = MAX_UINT64; + } // // PcdOvmfSecGhcbBase is used as the TD_HOB in Tdx guest. @@ -489,16 +610,16 @@ TransferTdxHobList ( ResourceAttribute = Hob.ResourceDescriptor->ResourceAttribute; if (ResourceType == 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, MaxAcceptedMemoryAddress); + } 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 c28d7601f87e..2bb346f31d44 100644 --- a/OvmfPkg/Library/PlatformInitLib/MemDetect.c +++ b/OvmfPkg/Library/PlatformInitLib/MemDetect.c @@ -41,6 +41,8 @@ Module Name: #include +#define MEGABYTE_SHIFT 20 + VOID EFIAPI PlatformQemuUc32BaseInitialization ( @@ -288,6 +290,31 @@ GetHighestSystemMemoryAddressFromPvhMemmap ( return HighestAddress; } +UINT32 +EFIAPI +PlatformAdjustSystemMemorySizeBelow4gbForLazyAccept ( + IN UINT32 LowerMemorySize + ) +{ + #ifdef MDE_CPU_X64 + UINT64 LazyAcceptMemSize; + + LazyAcceptMemSize = FixedPcdGet64 (PcdLazyAcceptPartialMemorySize); + // + // If specified accept size is not zero, + // transfer the size in megabyte to the number in byte. + // + if (LazyAcceptMemSize != 0) { + LazyAcceptMemSize <<= MEGABYTE_SHIFT; + if (LazyAcceptMemSize < LowerMemorySize) { + LowerMemorySize = (UINT32)(UINTN)LazyAcceptMemSize; + } + } + + #endif + return LowerMemorySize; +} + UINT32 EFIAPI PlatformGetSystemMemorySizeBelow4gb ( diff --git a/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf b/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf index d2a0bec43452..affb2fb90408 100644 --- a/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf +++ b/OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf @@ -95,6 +95,7 @@ gUefiOvmfPkgTokenSpaceGuid.PcdGuidedExtractHandlerTableSize gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPageSize + gUefiOvmfPkgTokenSpaceGuid.PcdLazyAcceptPartialMemorySize [FeaturePcd] gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplSwitchToLongMode diff --git a/OvmfPkg/PlatformPei/MemDetect.c b/OvmfPkg/PlatformPei/MemDetect.c index 2e47b1322990..acc1d7f63ee8 100644 --- a/OvmfPkg/PlatformPei/MemDetect.c +++ b/OvmfPkg/PlatformPei/MemDetect.c @@ -279,6 +279,11 @@ PublishPeiMemory ( LowerMemorySize -= mPlatformInfoHob.Q35TsegMbytes * SIZE_1MB; } + // + // Adjustment for Lazy accept because it may accept part of the memory. + // + LowerMemorySize = PlatformAdjustSystemMemorySizeBelow4gbForLazyAccept (LowerMemorySize); + S3AcpiReservedMemoryBase = 0; S3AcpiReservedMemorySize = 0; -- 2.29.2.windows.2