From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 31FE4AC0DAB for ; Wed, 8 Nov 2023 08:36:53 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=NzZNHY4rzW4LS+1cjOgFK/S+Q7K2Q2R624ix8JunC/E=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1699432611; v=1; b=eMf9rHHlAU65vrwOrhWcG9JTd3Gag3a52ZifgiuyH0bXtQZ7agmN9ROccl4tG2Cdwc1JTK2a N00wUR8VLlxy8voCsRgTN8OEd02ABBSgA2ijP5/PWiQP2Wg9wMwyRWAKKtYMUXQ0M14ai0p/PcR GilW2OjG/uvg5Qs4GzHUlPTE= X-Received: by 127.0.0.2 with SMTP id 0Fe7YY7687511xcoBRNtpqRm; Wed, 08 Nov 2023 00:36:51 -0800 X-Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.115]) by mx.groups.io with SMTP id smtpd.web10.9316.1699432610679212078 for ; Wed, 08 Nov 2023 00:36:51 -0800 X-IronPort-AV: E=McAfee;i="6600,9927,10887"; a="389527251" X-IronPort-AV: E=Sophos;i="6.03,285,1694761200"; d="scan'208";a="389527251" X-Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 00:36:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10887"; a="936442452" X-IronPort-AV: E=Sophos;i="6.03,285,1694761200"; d="scan'208";a="936442452" X-Received: from cepingsx-mobl1.ccr.corp.intel.com ([10.239.49.140]) by orsmga005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Nov 2023 00:36:47 -0800 From: "sunceping" To: devel@edk2.groups.io Cc: Ceping Sun , Erdem Aktas , James Bottomley , Jiewen Yao , Min Xu , Tom Lendacky , Michael Roth , Gerd Hoffmann Subject: [edk2-devel] [PATCH V3 2/2] OvmfPkg/BaseMemEncryptTdxLib: Handle retry result of MapGPA Date: Wed, 8 Nov 2023 16:32:03 +0800 Message-Id: <20231108083203.1417-3-cepingx.sun@intel.com> In-Reply-To: <20231108083203.1417-1-cepingx.sun@intel.com> References: <20231108083203.1417-1-cepingx.sun@intel.com> MIME-Version: 1.0 Precedence: Bulk 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,cepingx.sun@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: pPHv5SYC8EGsK0wl1pxOa8Cwx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=eMf9rHHl; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io From: Ceping Sun REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4572 According to section 3.2 of the [GHCI] document, if the return status of MapGPA is "TDG.VP.VMCALL_RETRY", TD must retry this operation for the pages in the region starting at the GPA specified in R11. In this patch, when a retry state is detected, TDVF needs to retry the mapping with the specified address from the output results of TdVmCall. Reference: [GHCI]: TDX Guest-Host-Communication Interface v1.0 https://cdrdv2.intel.com/v1/dl/getContent/726790 Cc: Erdem Aktas Cc: James Bottomley Cc: Jiewen Yao Cc: Min Xu Cc: Tom Lendacky Cc: Michael Roth Cc: Gerd Hoffmann Signed-off-by: Ceping Sun --- MdePkg/Include/IndustryStandard/Tdx.h | 2 + .../BaseMemEncryptTdxLib/MemoryEncryption.c | 41 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/MdePkg/Include/IndustryStandard/Tdx.h b/MdePkg/Include/IndustryStandard/Tdx.h index 81df1361842b..2662761883e5 100644 --- a/MdePkg/Include/IndustryStandard/Tdx.h +++ b/MdePkg/Include/IndustryStandard/Tdx.h @@ -103,6 +103,8 @@ #define TDVMCALL_REPORT_FATAL_ERR 0x10003 #define TDVMCALL_SETUP_EVENT_NOTIFY 0x10004 +#define TDVMCALL_STATUS_RETRY 0x1 + #pragma pack(1) typedef struct { UINT64 Data[6]; diff --git a/OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c b/OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c index a01dc98852b8..a71b1efbca7a 100644 --- a/OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c +++ b/OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c @@ -38,6 +38,8 @@ typedef enum { STATIC PAGE_TABLE_POOL *mPageTablePool = NULL; +#define MAX_RETRIES_PER_PAGE 3 + /** Returns boolean to indicate whether to indicate which, if any, memory encryption is enabled @@ -527,6 +529,13 @@ SetOrClearSharedBit ( EFI_STATUS Status; EDKII_MEMORY_ACCEPT_PROTOCOL *MemoryAcceptProtocol; + UINT64 MapGpaRetryAddr; + UINT32 RetryCount; + UINT64 EndAddress; + + MapGpaRetryAddr = 0; + RetryCount = 0; + AddressEncMask = GetMemEncryptionAddressMask (); // @@ -540,7 +549,37 @@ SetOrClearSharedBit ( PhysicalAddress &= ~AddressEncMask; } - TdStatus = TdVmCall (TDVMCALL_MAPGPA, PhysicalAddress, Length, 0, 0, NULL); + EndAddress = PhysicalAddress + Length; + while (RetryCount < MAX_RETRIES_PER_PAGE) { + TdStatus = TdVmCall (TDVMCALL_MAPGPA, PhysicalAddress, Length, 0, 0, &MapGpaRetryAddr); + if (TdStatus != TDVMCALL_STATUS_RETRY) { + break; + } + + DEBUG ((DEBUG_VERBOSE, "%a: TdVmcall(MAPGPA) Retry PhysicalAddress is %llx, MapGpaRetryAddr is %llx\n", __func__, PhysicalAddress, MapGpaRetryAddr)); + + if ((MapGpaRetryAddr < PhysicalAddress) || (MapGpaRetryAddr >= EndAddress)) { + DEBUG (( + DEBUG_ERROR, + "%a: TdVmcall(MAPGPA) failed with MapGpaRetryAddr(%llx) less than PhysicalAddress(%llx) or more than or equal to EndAddress(%llx) \n", + __func__, + MapGpaRetryAddr, + PhysicalAddress, + EndAddress + )); + break; + } + + if (MapGpaRetryAddr == PhysicalAddress) { + RetryCount++; + continue; + } + + PhysicalAddress = MapGpaRetryAddr; + Length = EndAddress - PhysicalAddress; + RetryCount = 0; + } + if (TdStatus != 0) { DEBUG ((DEBUG_ERROR, "%a: TdVmcall(MAPGPA) failed with %llx\n", __func__, TdStatus)); ASSERT (FALSE); -- 2.34.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110905): https://edk2.groups.io/g/devel/message/110905 Mute This Topic: https://groups.io/mt/102460273/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-