From nobody Mon Feb 9 06:26:52 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+93133+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+93133+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1662366938; cv=none; d=zohomail.com; s=zohoarc; b=fEl22fbk1dZeJxgFziG1wOTXkBzt/B7xCtQ8YqMRTGcSVEbpJFpnZ1SnDdsTrlLJgJ+IzCGgBr35qTGeYC8XKE9noRw+ek0MKrrWWnZPZVvHdbKGPNpcoM1Qf03KemK2RZ4mVJ0rW//cTkJiJ7CXQvvBd8LP2u7FNTFMVm312lM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1662366938; h=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=cttsZ+ae6RD7iqa0epoT1fMGWE0/Vye7jhDNy7K1yj0=; b=n8x5Jco42SFPW0wr+fjte5MULOw5Iy3BIdD4dlkqkfRcqgE6EdEs3y9pdal4cesVlyG6n7PPetHjUAe4Udfp8OgZF0bk+TLs4nSGbnHR8vA0LtBpM0NNMGn4z2JfbTd6AFrGW9w1W18/Z83VJwAuAp5OYd6vDGdTXCgP2tgz0ts= 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+93133+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 1662366938560725.0903776562723; Mon, 5 Sep 2022 01:35:38 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id Rj4pYY1788612xnrlpt9yUQx; Mon, 05 Sep 2022 01:35:38 -0700 X-Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by mx.groups.io with SMTP id smtpd.web09.22417.1662366920227192887 for ; Mon, 05 Sep 2022 01:35:37 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10460"; a="279355910" X-IronPort-AV: E=Sophos;i="5.93,290,1654585200"; d="scan'208";a="279355910" X-Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Sep 2022 01:35:37 -0700 X-IronPort-AV: E=Sophos;i="5.93,290,1654585200"; d="scan'208";a="675194451" X-Received: from mxu9-mobl1.ccr.corp.intel.com ([10.255.30.119]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Sep 2022 01:35:35 -0700 From: "Min Xu" To: devel@edk2.groups.io Cc: Min M Xu , Erdem Aktas , Gerd Hoffmann , James Bottomley , Jiewen Yao , Tom Lendacky Subject: [edk2-devel] [PATCH V3 08/12] OvmfPkg: Introduce lazy accept in PlatformInitLib and PlatformPei Date: Mon, 5 Sep 2022 16:35:02 +0800 Message-Id: <1cfb199b8cde681f11d25f5b471fa39c7032f9de.1662365866.git.min.m.xu@intel.com> In-Reply-To: References: MIME-Version: 1.0 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,min.m.xu@intel.com X-Gm-Message-State: H8SwMMAY2owCp1ELPrukTyKQx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1662366938; bh=nnMByCMdu32Nz/cSBn8TlZB7VfJFhSsyxmiacfMfhb4=; h=Cc:Date:From:Reply-To:Subject:To; b=effOdx7tRCv2Fr/rkyRBMplzBksVMNSsFNfSwSVRwS/tKxk0NPFv776WLCLyyRhu0P6 YQ7+L4de9nooYSP6t4REBTQ9KNDfhmU+Liy/9eNPOdY8gYqSnsMQdEQYCMuMRw8+D9MeX ACKPcKMu3kr45UTJVgeOvZXvxpL04E443h0= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1662366940710100013 Content-Type: text/plain; charset="utf-8" From: Min M Xu RFC: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3937 There are below major changes in PlatformInitLib/PlatformPei 1. ProcessHobList The unaccepted memory is accepted if it is under 4G address.If an EFI_RESOURCE_MEMORY_UNACCEPTED memory region is cross the 4G address, it will be split into 2 parts and only the left one (<4G) is accepted. The max accepted memory address is stored in Tdx workarea which will be used in TransferTdxHobList. 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. 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. 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 | 129 ++++++++++++++++++-- OvmfPkg/Library/PlatformInitLib/MemDetect.c | 2 + 2 files changed, 121 insertions(+), 10 deletions(-) diff --git a/OvmfPkg/Library/PlatformInitLib/IntelTdx.c b/OvmfPkg/Library/P= latformInitLib/IntelTdx.c index 396b14d919d2..333c69d1a854 100644 --- a/OvmfPkg/Library/PlatformInitLib/IntelTdx.c +++ b/OvmfPkg/Library/PlatformInitLib/IntelTdx.c @@ -7,6 +7,7 @@ =20 **/ =20 +#include #include #include #include @@ -24,7 +25,8 @@ #include #include =20 -#define ALIGNED_2MB_MASK 0x1fffff +#define ALIGNED_2MB_MASK 0x1fffff +#define MEGABYTE_SHIFT 20 =20 /** This function will be called to accept pages. Only BSP accepts pages. @@ -375,11 +377,18 @@ ProcessHobList ( EFI_STATUS Status; EFI_PEI_HOB_POINTERS Hob; EFI_PHYSICAL_ADDRESS PhysicalEnd; + TDX_WORK_AREA *WorkArea; + UINT64 ResourceLength; + UINT64 AccumulateAcceptedMemory; + UINT64 MaxAcceptedMemoryAddress; =20 Status =3D EFI_SUCCESS; ASSERT (VmmHobList !=3D NULL); Hob.Raw =3D (UINT8 *)VmmHobList; =20 + AccumulateAcceptedMemory =3D 0; + MaxAcceptedMemoryAddress =3D 0; + // // Parse the HOB list until end of list or matching type is found. // @@ -393,7 +402,16 @@ ProcessHobList ( DEBUG ((DEBUG_INFO, "ResourceLength: 0x%llx\n", Hob.ResourceDescri= ptor->ResourceLength)); DEBUG ((DEBUG_INFO, "Owner: %g\n\n", &Hob.ResourceDescriptor->Owne= r)); =20 - PhysicalEnd =3D Hob.ResourceDescriptor->PhysicalStart + Hob.Resour= ceDescriptor->ResourceLength; + PhysicalEnd =3D Hob.ResourceDescriptor->PhysicalStart + Hob.Res= ourceDescriptor->ResourceLength; + ResourceLength =3D Hob.ResourceDescriptor->ResourceLength; + + if (PhysicalEnd > SIZE_4GB) { + // + // In current stage, we only accept the memory under 4G + // + ResourceLength -=3D (PhysicalEnd - SIZE_4GB); + PhysicalEnd =3D SIZE_4GB; + } =20 Status =3D BspAcceptMemoryResourceRange ( Hob.ResourceDescriptor->PhysicalStart, @@ -402,12 +420,25 @@ ProcessHobList ( if (EFI_ERROR (Status)) { break; } + + AccumulateAcceptedMemory +=3D ResourceLength; + MaxAcceptedMemoryAddress =3D PhysicalEnd; } } =20 Hob.Raw =3D GET_NEXT_HOB (Hob); } =20 + // + // Record MaxAcceptedMemoryAddress in OvmfWorkArea. + // This information is useful later but in SEC phase we cannot use a glo= bal + // variable to pass this value. So it is stored in OvmfWorkarea. + // + WorkArea =3D (TDX_WORK_AREA *)FixedPcdGet32 (PcdOvmfWorkAreaBase); + ASSERT (WorkArea !=3D NULL); + ASSERT (WorkArea->Header.GuestType =3D=3D CcGuestTypeIntelTdx); + WorkArea->SecTdxWorkArea.MaxAcceptedMemoryAddress =3D MaxAcceptedMemoryA= ddress; + return Status; } =20 @@ -460,6 +491,74 @@ ProcessTdxHobList ( return Status; } =20 +/** + * 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 =3D=3D EFI_RESOURCE_MEMORY_UNACCEPTED); + + ResourceType =3D EFI_RESOURCE_MEMORY_UNACCEPTED; + ResourceAttribute =3D Hob->ResourceAttribute; + PhysicalStart =3D Hob->PhysicalStart; + ResourceLength =3D Hob->ResourceLength; + PhysicalEnd =3D PhysicalStart + ResourceLength; + + if (PhysicalEnd <=3D MaxAcceptedMemoryAddress) { + // + // This memory region has been accepted. + // + ResourceType =3D EFI_RESOURCE_SYSTEM_MEMORY; + ResourceAttribute |=3D (EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_= ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED); + } else if (PhysicalStart >=3D 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 =3D MaxAcceptedMemoryAddress - Hob->PhysicalSta= rt; + + // 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_A= TTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED), + Hob->PhysicalStart, + AcceptedResourceLength + ); + + PhysicalStart =3D Hob->PhysicalStart + AcceptedResourceLength; + ResourceLength -=3D 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 +576,16 @@ TransferTdxHobList ( EFI_PEI_HOB_POINTERS Hob; EFI_RESOURCE_TYPE ResourceType; EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute; + UINT64 MaxAcceptedMemoryAddress; + TDX_WORK_AREA *WorkArea; + + WorkArea =3D (TDX_WORK_AREA *)FixedPcdGet32 (PcdOvmfWorkAreaBase); + ASSERT (WorkArea !=3D NULL); + ASSERT (WorkArea->Header.GuestType =3D=3D CcGuestTypeIntelTdx); + MaxAcceptedMemoryAddress =3D WorkArea->SecTdxWorkArea.MaxAcceptedMemoryA= ddress; + if (MaxAcceptedMemoryAddress =3D=3D 0) { + MaxAcceptedMemoryAddress =3D MAX_UINT64; + } =20 // // PcdOvmfSecGhcbBase is used as the TD_HOB in Tdx guest. @@ -489,16 +598,16 @@ TransferTdxHobList ( ResourceAttribute =3D Hob.ResourceDescriptor->ResourceAttribute; =20 if (ResourceType =3D=3D EFI_RESOURCE_MEMORY_UNACCEPTED) { - ResourceType =3D EFI_RESOURCE_SYSTEM_MEMORY; - ResourceAttribute |=3D (EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RES= OURCE_ATTRIBUTE_INITIALIZED | EFI_RESOURCE_ATTRIBUTE_TESTED); + BuildResourceDescriptorHobForUnacceptedMemory (Hob.ResourceDescr= iptor, MaxAcceptedMemoryAddress); + } else { + BuildResourceDescriptorHob ( + ResourceType, + ResourceAttribute, + Hob.ResourceDescriptor->PhysicalStart, + Hob.ResourceDescriptor->ResourceLength + ); } =20 - 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: =20 #include =20 +#define MEGABYTE_SHIFT 20 + VOID EFIAPI PlatformQemuUc32BaseInitialization ( --=20 2.29.2.windows.2 -=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 (#93133): https://edk2.groups.io/g/devel/message/93133 Mute This Topic: https://groups.io/mt/93474519/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-