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 M Xu <min.m.xu@intel.com>,
	Erdem Aktas <erdemaktas@google.com>,
	Gerd Hoffmann <kraxel@redhat.com>,
	James Bottomley <jejb@linux.ibm.com>,
	Jiewen Yao <jiewen.yao@intel.com>,
	Tom Lendacky <thomas.lendacky@amd.com>
Subject: [PATCH 13/14] OvmfPkg: Realize EfiMemoryAcceptProtocol in TdxDxe
Date: Mon,  6 Jun 2022 11:00:01 +0800	[thread overview]
Message-ID: <1a17ee9c780bbe6a8ea53f4d69e497584b1af896.1654420876.git.min.m.xu@intel.com> (raw)
In-Reply-To: <cover.1654420875.git.min.m.xu@intel.com>

From: Min M Xu <min.m.xu@intel.com>

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

Memory usage may exceed the amount accepted at the begining (SEC), TDVF
needs to accept memory dynamically when OUT_OF_RESOURCE occurs.

EfiMemoryAcceptProtocol is defined in MdePkg and is implementated /
Installed in TdxDxe for Intel TDX memory acceptance.

Cc: Erdem Aktas <erdemaktas@google.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
---
 OvmfPkg/TdxDxe/TdxDxe.c   | 103 ++++++++++++++++++++++++++++++++++++++
 OvmfPkg/TdxDxe/TdxDxe.inf |   2 +
 2 files changed, 105 insertions(+)

diff --git a/OvmfPkg/TdxDxe/TdxDxe.c b/OvmfPkg/TdxDxe/TdxDxe.c
index 2318db989792..7cc9bf5f8c98 100644
--- a/OvmfPkg/TdxDxe/TdxDxe.c
+++ b/OvmfPkg/TdxDxe/TdxDxe.c
@@ -24,6 +24,7 @@
 #include <Library/HobLib.h>
 #include <Protocol/Cpu.h>
 #include <Protocol/MpInitLibDepProtocols.h>
+#include <Protocol/MemoryAccept.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <ConfidentialComputingGuestAttr.h>
 #include <IndustryStandard/Tdx.h>
@@ -32,6 +33,95 @@
 #include <TdxAcpiTable.h>
 #include <Library/MemEncryptTdxLib.h>
 
+#define ALIGNED_2MB_MASK  0x1fffff
+EFI_HANDLE  mTdxDxeHandle = NULL;
+
+EFI_STATUS
+EFIAPI
+TdxMemoryAccept (
+  IN EFI_MEMORY_ACCEPT_PROTOCOL  *This,
+  IN EFI_PHYSICAL_ADDRESS        StartAddress,
+  IN UINTN                       Size
+  )
+{
+  EFI_STATUS  Status;
+  UINT32      AcceptPageSize;
+  UINT64      StartAddress1;
+  UINT64      StartAddress2;
+  UINT64      StartAddress3;
+  UINT64      Length1;
+  UINT64      Length2;
+  UINT64      Length3;
+  UINT64      Pages;
+
+  AcceptPageSize = FixedPcdGet32 (PcdTdxAcceptPageSize);
+  StartAddress1  = 0;
+  StartAddress2  = 0;
+  StartAddress3  = 0;
+  Length1        = 0;
+  Length2        = 0;
+  Length3        = 0;
+
+  if (Size == 0) {
+    return EFI_SUCCESS;
+  }
+
+  if (ALIGN_VALUE (StartAddress, SIZE_2MB) != StartAddress) {
+    StartAddress1 = StartAddress;
+    Length1       = ALIGN_VALUE (StartAddress, SIZE_2MB) - StartAddress;
+    if (Length1 >= Size) {
+      Length1 = Size;
+    }
+
+    StartAddress += Length1;
+    Size         -= Length1;
+  }
+
+  if (Size > SIZE_2MB) {
+    StartAddress2 = StartAddress;
+    Length2       = Size & ~(UINT64)ALIGNED_2MB_MASK;
+    StartAddress += Length2;
+    Size         -= Length2;
+  }
+
+  if (Size) {
+    StartAddress3 = StartAddress;
+    Length3       = Size;
+  }
+
+  Status = EFI_SUCCESS;
+  if (Length1 > 0) {
+    Pages  = Length1 / SIZE_4KB;
+    Status = TdAcceptPages (StartAddress1, Pages, SIZE_4KB);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+  }
+
+  if (Length2 > 0) {
+    Pages  = Length2 / AcceptPageSize;
+    Status = TdAcceptPages (StartAddress2, Pages, AcceptPageSize);
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+  }
+
+  if (Length3 > 0) {
+    Pages  = Length3 / SIZE_4KB;
+    Status = TdAcceptPages (StartAddress3, Pages, SIZE_4KB);
+    ASSERT (!EFI_ERROR (Status));
+    if (EFI_ERROR (Status)) {
+      return Status;
+    }
+  }
+
+  return Status;
+}
+
+EFI_MEMORY_ACCEPT_PROTOCOL  mMemoryAcceptProtocol = {
+  TdxMemoryAccept
+};
+
 VOID
 SetPcdSettings (
   EFI_HOB_PLATFORM_INFO  *PlatformInfoHob
@@ -277,6 +367,19 @@ TdxDxeEntryPoint (
          NULL
          );
 
+  //
+  // Install MemoryAccept protocol for TDX
+  //
+  Status = gBS->InstallProtocolInterface (
+                  &mTdxDxeHandle,
+                  &gEfiMemoryAcceptProtocolGuid,
+                  EFI_NATIVE_INTERFACE,
+                  &mMemoryAcceptProtocol
+                  );
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "Install EfiMemoryAcceptProtocol failed.\n"));
+  }
+
   //
   // Call TDINFO to get actual number of cpus in domain
   //
diff --git a/OvmfPkg/TdxDxe/TdxDxe.inf b/OvmfPkg/TdxDxe/TdxDxe.inf
index a7e0abda1522..e24a000f768c 100644
--- a/OvmfPkg/TdxDxe/TdxDxe.inf
+++ b/OvmfPkg/TdxDxe/TdxDxe.inf
@@ -52,6 +52,7 @@
   gEfiAcpiTableProtocolGuid                        ## CONSUMES
   gEfiMpInitLibMpDepProtocolGuid
   gEfiMpInitLibUpDepProtocolGuid
+  gEfiMemoryAcceptProtocolGuid
 
 [Pcd]
   gUefiOvmfPkgTokenSpaceGuid.PcdPciIoBase
@@ -68,3 +69,4 @@
   gEfiMdePkgTokenSpaceGuid.PcdConfidentialComputingGuestAttr
   gEfiMdeModulePkgTokenSpaceGuid.PcdTdxSharedBitMask
   gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack
+  gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPageSize
-- 
2.29.2.windows.2


  parent reply	other threads:[~2022-06-06  3:01 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-06  2:59 [PATCH 00/14] Introduce Lazy-accept for Tdx guest Min Xu
2022-06-06  2:59 ` [PATCH 01/14] MdeModulePkg: Add PrePiHob.h Min Xu
2022-06-06  2:59 ` [PATCH 02/14] MdePkg: Increase EFI_RESOURCE_MAX_MEMORY_TYPE Min Xu
2022-06-06  3:23   ` Yao, Jiewen
2022-06-07 10:39   ` Gerd Hoffmann
2022-06-08  0:02     ` [edk2-devel] " Min Xu
2022-06-06  2:59 ` [PATCH 03/14] OvmfPkg: Use EFI_RESOURCE_MEMORY_UNACCEPTED which defined in MdeModulePkg Min Xu
2022-06-06  2:59 ` [PATCH 04/14] MdePkg: Add UEFI Unaccepted memory definition Min Xu
2022-06-06  3:23   ` Yao, Jiewen
     [not found]     ` <004301d87ec6$72e53d00$58afb700$@byosoft.com.cn>
2022-06-15  5:39       ` Min Xu
2022-06-06  2:59 ` [PATCH 05/14] MdeModulePkg: Update Dxe to handle unaccepted memory type Min Xu
     [not found]   ` <005601d87ec8$7f6f7700$7e4e6500$@byosoft.com.cn>
2022-06-15  6:18     ` [edk2-devel] " Min Xu
2022-06-06  2:59 ` [PATCH 06/14] ShellPkg: Update shell command memmap to show unaccepted memory Min Xu
2022-06-06  3:01   ` Ni, Ray
2022-06-06  3:19     ` Min Xu
2022-06-06  2:59 ` [PATCH 07/14] OvmfPkg: Add PCD and DEFINEs for Lazy Accept page Min Xu
2022-06-07 10:45   ` Gerd Hoffmann
2022-06-08  0:06     ` Min Xu
2022-06-08  6:18       ` Gerd Hoffmann
2022-06-15  3:07         ` Min Xu
2022-06-15  8:05           ` Gerd Hoffmann
2022-06-15 20:51             ` Dionna Glaze
2022-06-16  0:05               ` [edk2-devel] " Min Xu
2022-06-16  5:51                 ` Gerd Hoffmann
2022-06-16  7:33                   ` Min Xu
2022-06-16 13:09                   ` Lendacky, Thomas
2022-06-16 16:44                 ` Dionna Glaze
2022-06-16 18:33                   ` Lendacky, Thomas
2022-06-16 23:47                     ` Dionna Glaze
2022-06-14 16:10   ` Dionna Glaze
2022-06-15  3:08     ` Min Xu
2022-06-06  2:59 ` [PATCH 08/14] OvmfPkg: Use PcdOvmfWorkAreaBase instead of PcdSevEsWorkAreaBase Min Xu
2022-06-07 10:48   ` Gerd Hoffmann
2022-06-08  0:08     ` Min Xu
2022-06-06  2:59 ` [PATCH 09/14] OvmfPkg: Add MaxAcceptedMemoryAddress in TDX work area Min Xu
2022-06-06  2:59 ` [PATCH 10/14] OvmfPkg: Introduce lazy accept in PlatformInitLib and PlatformPei Min Xu
2022-06-06  2:59 ` [PATCH 11/14] OvmfPkg: Update ConstructFwHobList for lazy accept Min Xu
2022-06-06  3:00 ` [PATCH 12/14] MdePkg: The prototype definition of EfiMemoryAcceptProtocol Min Xu
2022-06-06  3:22   ` Yao, Jiewen
2022-06-08  0:49     ` Min Xu
2022-06-06  3:00 ` Min Xu [this message]
2022-06-06  3:00 ` [PATCH 14/14] MdeModulePkg: Pool and page functions accept memory when OOM occurs Min Xu
2022-08-11  3:17 ` [PATCH 00/14] Introduce Lazy-accept for Tdx guest Min Xu
2022-08-16  6:47   ` Gerd Hoffmann
2022-09-22 20:29     ` Dionna Glaze

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=1a17ee9c780bbe6a8ea53f4d69e497584b1af896.1654420876.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