public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Min Xu" <min.m.xu@intel.com>
To: devel@edk2.groups.io
Cc: Min Xu <min.m.xu@intel.com>,
	Ard Biesheuvel <ardb+tianocore@kernel.org>,
	Jordan Justen <jordan.l.justen@intel.com>,
	Brijesh Singh <brijesh.singh@amd.com>,
	Erdem Aktas <erdemaktas@google.com>,
	James Bottomley <jejb@linux.ibm.com>,
	Jiewen Yao <jiewen.yao@intel.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	Gerd Hoffmann <kraxel@redhat.com>
Subject: [PATCH V4 27/31] OvmfPkg: Update IoMmuDxe to support TDX
Date: Mon, 13 Dec 2021 20:56:58 +0800	[thread overview]
Message-ID: <329c6fdb80d1277504ff4cf55ddc9e528fbe8091.1639399598.git.min.m.xu@intel.com> (raw)
In-Reply-To: <cover.1639399598.git.min.m.xu@intel.com>

RFC: https://bugzilla.tianocore.org/show_bug.cgi?id=3429

The IOMMU protocol driver provides capabilities to set a DMA access
attribute and methods to allocate, free, map and unmap the DMA memory
for the PCI Bus devices.

The current IoMmuDxe driver supports DMA operations inside SEV guest.
To support DMA operation in TDX guest,
CC_GUEST_IS_XXX (PcdConfidentialComputingGuestAttr) is used to determine
if it is SEV guest or TDX guest.

Due to security reasons all DMA operations inside the SEV/TDX guest must
be performed on shared pages. The IOMMU protocol driver for the SEV/TDX
guest uses a bounce buffer to map guest DMA buffer to shared pages in
order to provide the support for DMA operations inside SEV/TDX guest.

The call of SEV or TDX specific function to set/clear EncMask/SharedBit
is determined by CC_GUEST_IS_XXX (PcdConfidentialComputingGuestAttr).

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
---
 OvmfPkg/IoMmuDxe/AmdSevIoMmu.c | 103 +++++++++++++++++++++------------
 OvmfPkg/IoMmuDxe/AmdSevIoMmu.h |   6 +-
 OvmfPkg/IoMmuDxe/IoMmuDxe.c    |   6 +-
 OvmfPkg/IoMmuDxe/IoMmuDxe.inf  |   5 ++
 OvmfPkg/OvmfPkgX64.dsc         |   2 +
 5 files changed, 80 insertions(+), 42 deletions(-)

diff --git a/OvmfPkg/IoMmuDxe/AmdSevIoMmu.c b/OvmfPkg/IoMmuDxe/AmdSevIoMmu.c
index 16b5b4eac8bc..6b65897f032a 100644
--- a/OvmfPkg/IoMmuDxe/AmdSevIoMmu.c
+++ b/OvmfPkg/IoMmuDxe/AmdSevIoMmu.c
@@ -1,9 +1,9 @@
 /** @file
 
   The protocol provides support to allocate, free, map and umap a DMA buffer
-  for bus master (e.g PciHostBridge). When SEV is enabled, the DMA operations
-  must be performed on unencrypted buffer hence we use a bounce buffer to map
-  the guest buffer into an unencrypted DMA buffer.
+  for bus master (e.g PciHostBridge). When SEV or TDX is enabled, the DMA
+  operations must be performed on unencrypted buffer hence we use a bounce
+  buffer to map the guest buffer into an unencrypted DMA buffer.
 
   Copyright (c) 2017, AMD Inc. All rights reserved.<BR>
   Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
@@ -12,6 +12,8 @@
 
 **/
 
+#include <Library/PcdLib.h>
+#include <ConfidentialComputingGuestAttr.h>
 #include "AmdSevIoMmu.h"
 
 #define MAP_INFO_SIG  SIGNATURE_64 ('M', 'A', 'P', '_', 'I', 'N', 'F', 'O')
@@ -74,7 +76,7 @@ typedef struct {
 
 /**
   Provides the controller-specific addresses required to access system memory
-  from a DMA bus master. On SEV guest, the DMA operations must be performed on
+  from a DMA bus master. On SEV/TDX guest, the DMA operations must be performed on
   shared buffer hence we allocate a bounce buffer to map the HostAddress to a
   DeviceAddress. The Encryption attribute is removed from the DeviceAddress
   buffer.
@@ -250,14 +252,28 @@ IoMmuMap (
       goto FreeMapInfo;
   }
 
-  //
-  // Clear the memory encryption mask on the plaintext buffer.
-  //
-  Status = MemEncryptSevClearPageEncMask (
-             0,
-             MapInfo->PlainTextAddress,
-             MapInfo->NumberOfPages
-             );
+  if (CC_GUEST_IS_SEV (PcdGet64 (PcdConfidentialComputingGuestAttr))) {
+    //
+    // Clear the memory encryption mask on the plaintext buffer.
+    //
+    Status = MemEncryptSevClearPageEncMask (
+               0,
+               MapInfo->PlainTextAddress,
+               MapInfo->NumberOfPages
+               );
+  } else if (CC_GUEST_IS_TDX (PcdGet64 (PcdConfidentialComputingGuestAttr))) {
+    //
+    // Set the memory shared bit.
+    //
+    Status = MemEncryptTdxSetPageSharedBit (
+               0,
+               MapInfo->PlainTextAddress,
+               MapInfo->NumberOfPages
+               );
+  } else {
+    ASSERT (FALSE);
+  }
+
   ASSERT_EFI_ERROR (Status);
   if (EFI_ERROR (Status)) {
     CpuDeadLoop ();
@@ -358,7 +374,7 @@ IoMmuUnmapWorker (
   }
 
   MapInfo = (MAP_INFO *)Mapping;
-
+  Status  = EFI_SUCCESS;
   //
   // set CommonBufferHeader to suppress incorrect compiler/analyzer warnings
   //
@@ -404,15 +420,30 @@ IoMmuUnmapWorker (
       break;
   }
 
-  //
-  // Restore the memory encryption mask on the area we used to hold the
-  // plaintext.
-  //
-  Status = MemEncryptSevSetPageEncMask (
-             0,
-             MapInfo->PlainTextAddress,
-             MapInfo->NumberOfPages
-             );
+  if (CC_GUEST_IS_SEV (PcdGet64 (PcdConfidentialComputingGuestAttr))) {
+    //
+    // Restore the memory encryption mask on the area we used to hold the
+    // plaintext.
+    //
+    Status = MemEncryptSevSetPageEncMask (
+               0,
+               MapInfo->PlainTextAddress,
+               MapInfo->NumberOfPages
+               );
+  } else if (CC_GUEST_IS_TDX (PcdGet64 (PcdConfidentialComputingGuestAttr))) {
+    //
+    // Restore the memory shared bit mask on the area we used to hold the
+    // plaintext.
+    //
+    Status = MemEncryptTdxClearPageSharedBit (
+               0,
+               MapInfo->PlainTextAddress,
+               MapInfo->NumberOfPages
+               );
+  } else {
+    ASSERT (FALSE);
+  }
+
   ASSERT_EFI_ERROR (Status);
   if (EFI_ERROR (Status)) {
     CpuDeadLoop ();
@@ -739,7 +770,7 @@ IoMmuSetAttribute (
   return EFI_UNSUPPORTED;
 }
 
-EDKII_IOMMU_PROTOCOL  mAmdSev = {
+EDKII_IOMMU_PROTOCOL  mIoMmu = {
   EDKII_IOMMU_PROTOCOL_REVISION,
   IoMmuSetAttribute,
   IoMmuMap,
@@ -771,7 +802,7 @@ EDKII_IOMMU_PROTOCOL  mAmdSev = {
 STATIC
 VOID
 EFIAPI
-AmdSevExitBoot (
+IoMmuExitBoot (
   IN EFI_EVENT  Event,
   IN VOID       *EventToSignal
   )
@@ -779,11 +810,11 @@ AmdSevExitBoot (
   //
   // (1) The NotifyFunctions of all the events in
   //     EFI_EVENT_GROUP_EXIT_BOOT_SERVICES will have been queued before
-  //     AmdSevExitBoot() is entered.
+  //     IoMmuExitBoot() is entered.
   //
-  // (2) AmdSevExitBoot() is executing minimally at TPL_CALLBACK.
+  // (2) IoMmuExitBoot() is executing minimally at TPL_CALLBACK.
   //
-  // (3) AmdSevExitBoot() has been queued in unspecified order relative to the
+  // (3) IoMmuExitBoot() has been queued in unspecified order relative to the
   //     NotifyFunctions of all the other events in
   //     EFI_EVENT_GROUP_EXIT_BOOT_SERVICES whose NotifyTpl is the same as
   //     Event's.
@@ -791,13 +822,13 @@ AmdSevExitBoot (
   // Consequences:
   //
   // - If Event's NotifyTpl is TPL_CALLBACK, then some other NotifyFunctions
-  //   queued at TPL_CALLBACK may be invoked after AmdSevExitBoot() returns.
+  //   queued at TPL_CALLBACK may be invoked after IoMmuExitBoot() returns.
   //
   // - If Event's NotifyTpl is TPL_NOTIFY, then some other NotifyFunctions
-  //   queued at TPL_NOTIFY may be invoked after AmdSevExitBoot() returns; plus
+  //   queued at TPL_NOTIFY may be invoked after IoMmuExitBoot() returns; plus
   //   *all* NotifyFunctions queued at TPL_CALLBACK will be invoked strictly
   //   after all NotifyFunctions queued at TPL_NOTIFY, including
-  //   AmdSevExitBoot(), have been invoked.
+  //   IoMmuExitBoot(), have been invoked.
   //
   // - By signaling EventToSignal here, whose NotifyTpl is TPL_CALLBACK, we
   //   queue EventToSignal's NotifyFunction after the NotifyFunctions of *all*
@@ -823,7 +854,7 @@ AmdSevExitBoot (
 STATIC
 VOID
 EFIAPI
-AmdSevUnmapAllMappings (
+IoMmuUnmapAllMappings (
   IN EFI_EVENT  Event,
   IN VOID       *Context
   )
@@ -842,7 +873,7 @@ AmdSevUnmapAllMappings (
     NextNode = GetNextNode (&mMapInfos, Node);
     MapInfo  = CR (Node, MAP_INFO, Link, MAP_INFO_SIG);
     IoMmuUnmapWorker (
-      &mAmdSev, // This
+      &mIoMmu,  // This
       MapInfo,  // Mapping
       TRUE      // MemoryMapLocked
       );
@@ -855,7 +886,7 @@ AmdSevUnmapAllMappings (
 **/
 EFI_STATUS
 EFIAPI
-AmdSevInstallIoMmuProtocol (
+InstallIoMmuProtocol (
   VOID
   )
 {
@@ -871,7 +902,7 @@ AmdSevInstallIoMmuProtocol (
   Status = gBS->CreateEvent (
                   EVT_NOTIFY_SIGNAL,      // Type
                   TPL_CALLBACK,           // NotifyTpl
-                  AmdSevUnmapAllMappings, // NotifyFunction
+                  IoMmuUnmapAllMappings,  // NotifyFunction
                   NULL,                   // NotifyContext
                   &UnmapAllMappingsEvent  // Event
                   );
@@ -886,7 +917,7 @@ AmdSevInstallIoMmuProtocol (
   Status = gBS->CreateEvent (
                   EVT_SIGNAL_EXIT_BOOT_SERVICES, // Type
                   TPL_CALLBACK,                  // NotifyTpl
-                  AmdSevExitBoot,                // NotifyFunction
+                  IoMmuExitBoot,                 // NotifyFunction
                   UnmapAllMappingsEvent,         // NotifyContext
                   &ExitBootEvent                 // Event
                   );
@@ -898,7 +929,7 @@ AmdSevInstallIoMmuProtocol (
   Status = gBS->InstallMultipleProtocolInterfaces (
                   &Handle,
                   &gEdkiiIoMmuProtocolGuid,
-                  &mAmdSev,
+                  &mIoMmu,
                   NULL
                   );
   if (EFI_ERROR (Status)) {
diff --git a/OvmfPkg/IoMmuDxe/AmdSevIoMmu.h b/OvmfPkg/IoMmuDxe/AmdSevIoMmu.h
index 8244f28b57fd..8fdfa9968593 100644
--- a/OvmfPkg/IoMmuDxe/AmdSevIoMmu.h
+++ b/OvmfPkg/IoMmuDxe/AmdSevIoMmu.h
@@ -21,17 +21,17 @@
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
 #include <Library/MemEncryptSevLib.h>
+#include <Library/MemEncryptTdxLib.h>
 #include <Library/MemoryAllocationLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 
 /**
-  Install IOMMU protocol to provide the DMA support for PciHostBridge and
-  MemEncryptSevLib.
+  Install IOMMU protocol to provide the DMA support for PciHostBridge.
 
 **/
 EFI_STATUS
 EFIAPI
-AmdSevInstallIoMmuProtocol (
+InstallIoMmuProtocol (
   VOID
   );
 
diff --git a/OvmfPkg/IoMmuDxe/IoMmuDxe.c b/OvmfPkg/IoMmuDxe/IoMmuDxe.c
index bca8c14c4076..86777dd05c26 100644
--- a/OvmfPkg/IoMmuDxe/IoMmuDxe.c
+++ b/OvmfPkg/IoMmuDxe/IoMmuDxe.c
@@ -22,11 +22,11 @@ IoMmuDxeEntryPoint (
   EFI_HANDLE  Handle;
 
   //
-  // When SEV is enabled, install IoMmu protocol otherwise install the
+  // When SEV or TDX is enabled, install IoMmu protocol otherwise install the
   // placeholder protocol so that other dependent module can run.
   //
-  if (MemEncryptSevIsEnabled ()) {
-    Status = AmdSevInstallIoMmuProtocol ();
+  if (MemEncryptSevIsEnabled () || MemEncryptTdxIsEnabled ()) {
+    Status = InstallIoMmuProtocol ();
   } else {
     Handle = NULL;
 
diff --git a/OvmfPkg/IoMmuDxe/IoMmuDxe.inf b/OvmfPkg/IoMmuDxe/IoMmuDxe.inf
index 2ebd74e5558c..e10be1dcff49 100644
--- a/OvmfPkg/IoMmuDxe/IoMmuDxe.inf
+++ b/OvmfPkg/IoMmuDxe/IoMmuDxe.inf
@@ -26,16 +26,21 @@
   MdePkg/MdePkg.dec
   MdeModulePkg/MdeModulePkg.dec
   OvmfPkg/OvmfPkg.dec
+#  UefiCpuPkg/UefiCpuPkg.dec
 
 [LibraryClasses]
   BaseLib
   BaseMemoryLib
   DebugLib
   MemEncryptSevLib
+  MemEncryptTdxLib
   MemoryAllocationLib
   UefiBootServicesTableLib
   UefiDriverEntryPoint
 
+[Pcd]
+  gEfiMdePkgTokenSpaceGuid.PcdConfidentialComputingGuestAttr
+
 [Protocols]
   gEdkiiIoMmuProtocolGuid                     ## SOMETIME_PRODUCES
   gIoMmuAbsentProtocolGuid                    ## SOMETIME_PRODUCES
diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc
index b1ec012e4f83..4d23408f1d4c 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -180,6 +180,8 @@
   VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf
   LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf
   MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+  MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
+
 !if $(SMM_REQUIRE) == FALSE
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
 !endif
-- 
2.29.2.windows.2


  parent reply	other threads:[~2021-12-13 12:59 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-13 12:56 [PATCH V4 00/31] Enable Intel TDX in OvmfPkg (Config-A) Min Xu
2021-12-13 12:56 ` [PATCH V4 01/31] MdePkg: Add Tdx.h Min Xu
2021-12-13 12:56 ` [PATCH V4 02/31] MdePkg: Introduce basic Tdx functions in BaseLib Min Xu
2021-12-15  6:33   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 03/31] MdePkg: Add TdxLib to wrap Tdx operations Min Xu
2021-12-15  6:44   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 04/31] UefiCpuPkg: Extend VmgExitLibNull to handle #VE exception Min Xu
2021-12-13 12:56 ` [PATCH V4 05/31] OvmfPkg: Extend VmgExitLib " Min Xu
2021-12-15  6:56   ` [edk2-devel] " Gerd Hoffmann
2022-01-20  6:34     ` Min Xu
2021-12-13 12:56 ` [PATCH V4 06/31] UefiCpuPkg/CpuExceptionHandler: Add base support for the " Min Xu
2021-12-13 12:56 ` [PATCH V4 07/31] MdePkg: Add helper functions for Tdx guest in BaseIoLibIntrinsic Min Xu
2021-12-13 12:56 ` [PATCH V4 08/31] MdePkg: Support mmio " Min Xu
2021-12-15  7:02   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 09/31] MdePkg: Support IoFifo " Min Xu
2021-12-15  7:18   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 10/31] MdePkg: Support IoRead/IoWrite " Min Xu
2021-12-15  7:18   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 11/31] UefiCpuPkg: Support TDX in BaseXApicX2ApicLib Min Xu
2021-12-13 12:56 ` [PATCH V4 12/31] MdePkg: Add macro to check SEV / TDX guest Min Xu
2021-12-13 12:56 ` [PATCH V4 13/31] UefiCpuPkg: Enable Tdx support in MpInitLib Min Xu
2021-12-15  7:33   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 14/31] OvmfPkg: Add IntelTdx.h in OvmfPkg/Include/IndustryStandard Min Xu
2021-12-15  7:37   ` Gerd Hoffmann
2021-12-16  6:43     ` Min Xu
2021-12-13 12:56 ` [PATCH V4 15/31] OvmfPkg: Add TdxMailboxLib Min Xu
2021-12-15  7:47   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 16/31] MdePkg: Add EFI_RESOURCE_ATTRIBUTE_ENCRYPTED in PiHob.h Min Xu
2021-12-13 12:56 ` [PATCH V4 17/31] OvmfPkg: Update Sec to support Tdx Min Xu
2021-12-15  8:18   ` Gerd Hoffmann
2021-12-16  8:11     ` [edk2-devel] " Min Xu
2021-12-13 12:56 ` [PATCH V4 18/31] OvmfPkg: Check Tdx in QemuFwCfgPei to avoid DMA operation Min Xu
2021-12-15  8:19   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 19/31] MdeModulePkg: EFER should not be changed in TDX Min Xu
2021-12-13 12:56 ` [PATCH V4 20/31] MdeModulePkg: Add PcdTdxSharedBitMask Min Xu
2021-12-15  8:22   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 21/31] UefiCpuPkg: Update AddressEncMask in CpuPageTable Min Xu
2021-12-15  8:38   ` Gerd Hoffmann
2021-12-16  8:23     ` Min Xu
2021-12-13 12:56 ` [PATCH V4 22/31] OvmfPkg: Update PlatformPei to support TDX Min Xu
2021-12-15  8:53   ` Gerd Hoffmann
2022-01-20  9:07     ` [edk2-devel] " Min Xu
2022-01-13 19:18   ` Vishal Annapurve
2022-01-14  2:23     ` Min Xu
2022-01-14  2:37       ` Vishal Annapurve
2022-01-14  2:40         ` Min Xu
2021-12-13 12:56 ` [PATCH V4 23/31] OvmfPkg: Update AcpiPlatformDxe to alter MADT table Min Xu
2021-12-15  8:54   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 24/31] OvmfPkg: Add TdxDxe driver Min Xu
2021-12-15  9:05   ` Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 25/31] OvmfPkg/BaseMemEncryptTdxLib: Add TDX helper library Min Xu
2021-12-15  9:16   ` Gerd Hoffmann
2022-01-21  2:54     ` Min Xu
2022-01-21  8:04       ` Gerd Hoffmann
2022-01-21  8:31         ` [edk2-devel] " Min Xu
2021-12-13 12:56 ` [PATCH V4 26/31] OvmfPkg/QemuFwCfgLib: Support Tdx in QemuFwCfgDxe Min Xu
2021-12-15  9:16   ` Gerd Hoffmann
2021-12-13 12:56 ` Min Xu [this message]
2021-12-15  9:18   ` [PATCH V4 27/31] OvmfPkg: Update IoMmuDxe to support TDX Gerd Hoffmann
2021-12-13 12:56 ` [PATCH V4 28/31] OvmfPkg: Rename XenTimerDxe to LocalApicTimerDxe Min Xu
2021-12-15  9:19   ` Gerd Hoffmann
2021-12-13 12:57 ` [PATCH V4 29/31] UefiCpuPkg: Setting initial-count register as the last step Min Xu
2021-12-15  9:20   ` Gerd Hoffmann
2021-12-13 12:57 ` [PATCH V4 30/31] OvmfPkg: Switch timer in build time for OvmfPkg Min Xu
2021-12-15  9:21   ` Gerd Hoffmann
2021-12-13 12:57 ` [PATCH V4 31/31] OvmfPkg: Move LocalApicTimerDxe to UefiCpuPkg Min Xu
2021-12-15  9:26   ` Gerd Hoffmann
2021-12-16  8:29     ` [edk2-devel] " Min Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=329c6fdb80d1277504ff4cf55ddc9e528fbe8091.1639399598.git.min.m.xu@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox