From nobody Thu May 16 01:14:28 2024 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+110158+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+110158+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1698368287; cv=none; d=zohomail.com; s=zohoarc; b=oLk02HLtYCmyMciCip71rwb3m5JWoz7RGbc5mxbXpJegZ9/C9U+NYdRnLCrY/CyE5Krule47S7ajd/9hJ3PKV8yWxTLdXzFPta+6x/uAV+Di1T9jJzN8XMor7pVIOz1SeQhA2zmc5wG7XQYAvnrJnbna7fedGGV5sF6qAdm857w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1698368287; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=IoX0beQZ5u/IVdQ3o2K6WVEPoVHyL7kCZipKGFQ9Y/g=; b=H4ewkG08ZWSB2McgY9DUvnhdHBoBmrCf5cAgJodV0WVWCp1y+X4CyF3NEFoO796HcePYlGH56rb3D9GbwGHDXC/EoByPxSQN9EaSEsh5qqZ4wds3Yajafu/+GHzRn+XWY/viObOCMGAzZgArWPGIWrx5F4mmpNdRfJXGcSKVlMM= 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+110158+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 1698368287740222.30387663782255; Thu, 26 Oct 2023 17:58:07 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=tzYBZg93ZgSNuP5UrcpP12joL60TRgw4rqcGiff2A88=; 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=1698368287; v=1; b=URkFKpCtqErOofF68S3mvcIPBpDG8Yb6mB+3RDseqt5C4GMspkB5LS76R+rSVNKCqExkK7Xl UVqoi9haQgLmfSIYFbR6VpAWOAaDZ+NR4ndzSfrxHHKqAmTYt9495rFFG4P6mjLldToh3XATyES 0zJFB6OYq3+jBjQMMJG3Fwug= X-Received: by 127.0.0.2 with SMTP id a1hqYY1788612xp129Tq2T9F; Thu, 26 Oct 2023 17:58:07 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by mx.groups.io with SMTP id smtpd.web10.216621.1698368286934136500 for ; Thu, 26 Oct 2023 17:58:06 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10875"; a="384890086" X-IronPort-AV: E=Sophos;i="6.03,255,1694761200"; d="scan'208";a="384890086" X-Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Oct 2023 17:58:06 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10875"; a="1006585845" X-IronPort-AV: E=Sophos;i="6.03,255,1694761200"; d="scan'208";a="1006585845" X-Received: from cepingsx-mobl1.ccr.corp.intel.com ([10.239.49.140]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Oct 2023 17:58:04 -0700 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 V1 1/2] OvmfPkg/BaseMemEncryptTdxLib: Add TdVmCallMapGPA Date: Fri, 27 Oct 2023 08:57:37 +0800 Message-Id: <20231027005738.371-2-cepingx.sun@intel.com> In-Reply-To: <20231027005738.371-1-cepingx.sun@intel.com> References: <20231027005738.371-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: veQcPskFsoonX7lnQbsBQcUPx1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1698368290004100003 Content-Type: text/plain; charset="utf-8" From: Ceping Sun REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4572 According to section 3.2 of the [GHCI] spec, if the return status is "TDG.VP.VMCALL_RETRY", TD must retry this operation for the pages in the region starting at the GPA specified in R11. Currently, TDVF has not handled the retry results of MapGPA. For this, TDVF should add the API to output the GPA at which MapGPA failed in R11 to handle the retry results. 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 --- .../BaseMemEncryptTdxLib/MemoryEncryption.c | 19 +++ .../X64/TdVmCallMapGPA.nasm | 130 ++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 OvmfPkg/Library/BaseMemEncryptTdxLib/X64/TdVmCallMapGPA= .nasm diff --git a/OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c b/Ovmf= Pkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c index a01dc98852b8..b47f56b391a5 100644 --- a/OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c +++ b/OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c @@ -38,6 +38,25 @@ typedef enum { =20 STATIC PAGE_TABLE_POOL *mPageTablePool =3D NULL; =20 +/** + This function is used to help request the host VMM to map a GPA range as + private or shared-memory mappings. + @param[in] Address 4K aligned start GPA of address range. + @param[in] Length Size of GPA region to be mapped. + @param[in,out] Results Returned result of the GPA at which MapGPA fa= iled + + @return 0 A successful mapping + @return Other Some errors occurred while mapping +**/ + +UINTN +EFIAPI +TdVmCallMapGPA ( + IN UINT64 Address, + IN UINT64 Length, + IN OUT VOID *Results + ); + /** Returns boolean to indicate whether to indicate which, if any, memory en= cryption is enabled =20 diff --git a/OvmfPkg/Library/BaseMemEncryptTdxLib/X64/TdVmCallMapGPA.nasm b= /OvmfPkg/Library/BaseMemEncryptTdxLib/X64/TdVmCallMapGPA.nasm new file mode 100644 index 000000000000..37186bd0a0dd --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptTdxLib/X64/TdVmCallMapGPA.nasm @@ -0,0 +1,130 @@ +;-------------------------------------------------------------------------= ----- +;* +;* Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.
+;* SPDX-License-Identifier: BSD-2-Clause-Patent +;* +;* +;-------------------------------------------------------------------------= ----- + +DEFAULT REL +SECTION .text + +%define TDVMCALL_EXPOSE_REGS_MASK 0xffec +%define TDVMCALL 0x0 +%define TDVMCALL_MAPGPA 0x10001 +%define TDVMCALL_STATUS_RETRY 0x1 + +%macro tdcall 0 + db 0x66,0x0f,0x01,0xcc +%endmacro + +%macro tdcall_push_regs 0 + push rbp + mov rbp, rsp + push r15 + push r14 + push r13 + push r12 + push rbx + push rsi + push rdi +%endmacro + +%macro tdcall_pop_regs 0 + pop rdi + pop rsi + pop rbx + pop r12 + pop r13 + pop r14 + pop r15 + pop rbp +%endmacro + +%macro tdcall_regs_preamble 2 + mov rax, %1 + + xor rcx, rcx + mov ecx, %2 + + ; R10 =3D 0 (standard TDVMCALL) + + xor r10d, r10d + + ; Zero out unused (for standard TDVMCALL) registers to avoid leaking + ; secrets to the VMM. + + xor ebx, ebx + xor esi, esi + xor edi, edi + + xor edx, edx + xor ebp, ebp + xor r8d, r8d + xor r9d, r9d +%endmacro + +%macro tdcall_regs_postamble 0 + xor ebx, ebx + xor esi, esi + xor edi, edi + + xor ecx, ecx + xor edx, edx + xor r8d, r8d + xor r9d, r9d + xor r10d, r10d + xor r11d, r11d +%endmacro + +;-------------------------------------------------------------------------= ----- +; 0 =3D> RAX =3D TDCALL leaf +; M =3D> RCX =3D TDVMCALL register behavior +; 1 =3D> R10 =3D standard vs. vendor +; 0xa =3D> R11 =3D TDVMCALL function / MapGPA +; RCX =3D> R12 =3D p1 +; RDX =3D> R13 =3D p2 + +; UINT64 +; EFIAPI +; TdVmCallMapGPA ( +; UINT64 Address, // Rcx +; UINT64 Length, // Rdx +; UINT64 *Results // r8 +; ) +global ASM_PFX(TdVmCallMapGPA) +ASM_PFX(TdVmCallMapGPA): + tdcall_push_regs + + mov r11, TDVMCALL_MAPGPA + mov r12, rcx + mov r13, rdx + + push r8 + + tdcall_regs_preamble TDVMCALL, TDVMCALL_EXPOSE_REGS_MASK + + tdcall + + ; ignore return dataif TDCALL reports failure. + test rax, rax + jnz .no_return_data + + ; Propagate TDVMCALL success/failure to return value. + mov rax, r10 + + ; Retrieve the Val pointer. + pop r8 + test r8, r8 + jz .no_return_data + + ; On Retry, propagate TDVMCALL output value to output param + cmp rax, TDVMCALL_STATUS_RETRY + jnz .no_return_data + mov [r8], r11 +.no_return_data: + tdcall_regs_postamble + + tdcall_pop_regs + + ret --=20 2.34.1 -=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 (#110158): https://edk2.groups.io/g/devel/message/110158 Mute This Topic: https://groups.io/mt/102212638/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- From nobody Thu May 16 01:14:28 2024 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+110159+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+110159+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1698368293; cv=none; d=zohomail.com; s=zohoarc; b=Ci96iIrE8sv3oGzdYsONfJFU50MtSko/r4AxQrttBWv5CvoyBel7LhNrSONcVYSsMyO6V7POMKFPPJv8Z4MpB/VnYUdOOhYA5/OcSXeDnNhNAKyVl6p4hws09YbDvXzP9wWpD7xG9teX25Mp21uv/qKEBRBCYiSZniXADl/WJrw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1698368293; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=UjCKjFuVrF9dZggcp4lIwPktXoBIAg7RfW1lCKSlK8w=; b=VQiIp9tsJ3i8oOp4RqZTeujn62/0H4QCi0KhLA45PDVQYCaupvbmN8rhIEeJ5gg3cnZ1wRxGAUDLCT+acCygZEj2MfbOfVwmuLwhjRaWq5DbfJ4kcxmrhKBsxkqicROwY9uh45fH91xnjt8IBKoAtNStwegkkrKVRuk9cUSkiYM= 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+110159+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 1698368293796850.1104568531773; Thu, 26 Oct 2023 17:58:13 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=XuMylt9yoqNa5fNn/PDPrunt7tV+uywxeUjPHe74RNg=; 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=1698368293; v=1; b=E3ZeLM0PHA/jXj0Nsy2rkQa7LRD36m9r2nx2zWyVTqvr90BdfoKFHvqpYcjYkT4maGcEaGSF HPXQ+pcuefZhyKy17Qr1ysTvdS2bezVt0XKvVnO1DDiYkEA8vkS83z0kLwm307HMQkg0/kvU7pW gitmIgA3FkhXLQcF6XuMDOjE= X-Received: by 127.0.0.2 with SMTP id ERjZYY1788612xieAdgVVtBz; Thu, 26 Oct 2023 17:58:13 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by mx.groups.io with SMTP id smtpd.web11.87069.1698368292832669302 for ; Thu, 26 Oct 2023 17:58:13 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10875"; a="384890094" X-IronPort-AV: E=Sophos;i="6.03,255,1694761200"; d="scan'208";a="384890094" X-Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Oct 2023 17:58:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10875"; a="1006585853" X-IronPort-AV: E=Sophos;i="6.03,255,1694761200"; d="scan'208";a="1006585853" X-Received: from cepingsx-mobl1.ccr.corp.intel.com ([10.239.49.140]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Oct 2023 17:58:10 -0700 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 V1 2/2] OvmfPkg/BaseMemEncryptTdxLib: Handle retry result of MapGPA Date: Fri, 27 Oct 2023 08:57:38 +0800 Message-Id: <20231027005738.371-3-cepingx.sun@intel.com> In-Reply-To: <20231027005738.371-1-cepingx.sun@intel.com> References: <20231027005738.371-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: VgP1IBcM8bwbDZRJNBEzWttbx1787277AA= Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1698368295948100003 Content-Type: text/plain; charset="utf-8" From: Ceping Sun REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4572 According to section 3.2 of the [GHCI] document, if the result of MapGPA is "TDG.VP.VMCALL_RETRY", TDVF must retry mapping for pages in that region, starting with the GPA specified in R11. 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 --- .../BaseMemEncryptTdxLib.inf | 1 + .../BaseMemEncryptTdxLib/MemoryEncryption.c | 36 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf = b/OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf index 11768825f8ca..742b65a289ce 100644 --- a/OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf @@ -30,6 +30,7 @@ [Sources] VirtualMemory.h MemoryEncryption.c + X64/TdVmCallMapGPA.nasm =20 [LibraryClasses] BaseLib diff --git a/OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c b/Ovmf= Pkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c index b47f56b391a5..1f29f9194c30 100644 --- a/OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c +++ b/OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c @@ -38,6 +38,10 @@ typedef enum { =20 STATIC PAGE_TABLE_POOL *mPageTablePool =3D NULL; =20 +#define TDVMCALL_STATUS_RETRY 0x1 + +#define MAX_RETRIES_PER_PAGE 3 + /** This function is used to help request the host VMM to map a GPA range as private or shared-memory mappings. @@ -546,6 +550,13 @@ SetOrClearSharedBit ( EFI_STATUS Status; EDKII_MEMORY_ACCEPT_PROTOCOL *MemoryAcceptProtocol; =20 + UINT64 MapGpaRetryaddr; + UINT32 RetryCount; + UINT64 EndAddress; + + MapGpaRetryaddr =3D 0; + RetryCount =3D 0; + AddressEncMask =3D GetMemEncryptionAddressMask (); =20 // @@ -559,7 +570,30 @@ SetOrClearSharedBit ( PhysicalAddress &=3D ~AddressEncMask; } =20 - TdStatus =3D TdVmCall (TDVMCALL_MAPGPA, PhysicalAddress, Length, 0, 0, N= ULL); + while (RetryCount < MAX_RETRIES_PER_PAGE) { + TdStatus =3D TdVmCallMapGPA (PhysicalAddress, Length, &MapGpaRetryaddr= ); + if (TdStatus !=3D TDVMCALL_STATUS_RETRY) { + break; + } + + DEBUG ((DEBUG_VERBOSE, "%a: TdVmcall(MAPGPA) Retry PhysicalAddress is = %llx, MapGpaRetryaddr is %llx\n", __func__, PhysicalAddress, MapGpaRetryadd= r)); + + EndAddress =3D PhysicalAddress + Length; + if ((MapGpaRetryaddr < PhysicalAddress) || (MapGpaRetryaddr > EndAddre= ss)) { + DEBUG ((DEBUG_ERROR, "%a: TdVmcall(MAPGPA) failed Retry PhysicalAddr= ess is %llx, MapGpaRetryaddr is %llx\n", __func__, PhysicalAddress, MapGpaR= etryaddr)); + break; + } + + if (MapGpaRetryaddr =3D=3D PhysicalAddress) { + RetryCount++; + continue; + } + + PhysicalAddress =3D MapGpaRetryaddr; + Length =3D EndAddress - PhysicalAddress; + RetryCount =3D 0; + } + if (TdStatus !=3D 0) { DEBUG ((DEBUG_ERROR, "%a: TdVmcall(MAPGPA) failed with %llx\n", __func= __, TdStatus)); ASSERT (FALSE); --=20 2.34.1 -=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 (#110159): https://edk2.groups.io/g/devel/message/110159 Mute This Topic: https://groups.io/mt/102212640/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-