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 1/1] OvmfPkg: Store PageTablePool in TdxWorkArea
Date: Wed,  7 Sep 2022 10:18:00 +0800	[thread overview]
Message-ID: <20220907021800.690-1-min.m.xu@intel.com> (raw)

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

PeilessStartupLib is running in SEC phase. In that phase global variable
is not allowed. So the mPageTablePool cannot be used. In stead the
PageTablePool is stored in TdxWorkArea.

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 M Xu <min.m.xu@intel.com>
---
 OvmfPkg/Include/WorkArea.h                    |  1 +
 .../PeilessStartupLib/PeilessStartupLib.inf   |  1 +
 .../PeilessStartupLib/X64/VirtualMemory.c     | 82 +++++++++++++------
 3 files changed, 60 insertions(+), 24 deletions(-)

diff --git a/OvmfPkg/Include/WorkArea.h b/OvmfPkg/Include/WorkArea.h
index bf56fc4a6f65..0b2fb2f8c573 100644
--- a/OvmfPkg/Include/WorkArea.h
+++ b/OvmfPkg/Include/WorkArea.h
@@ -71,6 +71,7 @@ typedef struct _SEC_TDX_WORK_AREA {
   UINT32    PageTableReady;
   UINT32    Gpaw;
   UINT64    HobList;
+  UINT64    PageTablePool;
 } SEC_TDX_WORK_AREA;
 
 typedef struct _TDX_WORK_AREA {
diff --git a/OvmfPkg/Library/PeilessStartupLib/PeilessStartupLib.inf b/OvmfPkg/Library/PeilessStartupLib/PeilessStartupLib.inf
index def50b4b019e..eed9f27d3d01 100644
--- a/OvmfPkg/Library/PeilessStartupLib/PeilessStartupLib.inf
+++ b/OvmfPkg/Library/PeilessStartupLib/PeilessStartupLib.inf
@@ -88,3 +88,4 @@
   gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask    ## CONSUMES
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfDxeMemFvBase
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfDxeMemFvSize
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfWorkAreaBase
diff --git a/OvmfPkg/Library/PeilessStartupLib/X64/VirtualMemory.c b/OvmfPkg/Library/PeilessStartupLib/X64/VirtualMemory.c
index 6877e521e485..7e33f044993a 100644
--- a/OvmfPkg/Library/PeilessStartupLib/X64/VirtualMemory.c
+++ b/OvmfPkg/Library/PeilessStartupLib/X64/VirtualMemory.c
@@ -19,13 +19,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Guid/MemoryAllocationHob.h>
 #include <Register/Intel/Cpuid.h>
 #include <Library/PlatformInitLib.h>
+#include <WorkArea.h>
 #include "PageTables.h"
 
-//
-// Global variable to keep track current available memory used as page table.
-//
-PAGE_TABLE_POOL  *mPageTablePool = NULL;
-
 UINTN  mLevelShift[5] = {
   0,
   PAGING_L1_ADDRESS_SHIFT,
@@ -50,6 +46,32 @@ UINT64  mLevelSize[5] = {
   SIZE_512GB
 };
 
+PAGE_TABLE_POOL *
+GetPageTablePool (
+  VOID
+  )
+{
+  TDX_WORK_AREA  *TdxWorkArea;
+
+  TdxWorkArea = (TDX_WORK_AREA *)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaBase);
+  ASSERT (TdxWorkArea != NULL);
+
+  return (PAGE_TABLE_POOL *)TdxWorkArea->SecTdxWorkArea.PageTablePool;
+}
+
+VOID
+SetPageTablePool (
+  VOID  *PageTablePool
+  )
+{
+  TDX_WORK_AREA  *TdxWorkArea;
+
+  TdxWorkArea = (TDX_WORK_AREA *)(UINTN)FixedPcdGet32 (PcdOvmfWorkAreaBase);
+  ASSERT (TdxWorkArea != NULL);
+
+  TdxWorkArea->SecTdxWorkArea.PageTablePool = (UINT64)(UINTN)PageTablePool;
+}
+
 BOOLEAN
 IsSetNxForStack (
   VOID
@@ -283,7 +305,10 @@ InitializePageTablePool (
   IN UINTN  PoolPages
   )
 {
-  VOID  *Buffer;
+  VOID             *Buffer;
+  PAGE_TABLE_POOL  *PageTablePool;
+
+  PageTablePool = GetPageTablePool ();
 
   DEBUG ((DEBUG_INFO, "InitializePageTablePool PoolPages=%d\n", PoolPages));
 
@@ -303,20 +328,21 @@ InitializePageTablePool (
   //
   // Link all pools into a list for easier track later.
   //
-  if (mPageTablePool == NULL) {
-    mPageTablePool           = Buffer;
-    mPageTablePool->NextPool = mPageTablePool;
+  if (PageTablePool == NULL) {
+    PageTablePool           = Buffer;
+    PageTablePool->NextPool = PageTablePool;
+    SetPageTablePool (PageTablePool);
   } else {
-    ((PAGE_TABLE_POOL *)Buffer)->NextPool = mPageTablePool->NextPool;
-    mPageTablePool->NextPool              = Buffer;
-    mPageTablePool                        = Buffer;
+    ((PAGE_TABLE_POOL *)Buffer)->NextPool = PageTablePool->NextPool;
+    PageTablePool->NextPool               = Buffer;
+    PageTablePool                         = Buffer;
   }
 
   //
   // Reserve one page for pool header.
   //
-  mPageTablePool->FreePages = PoolPages - 1;
-  mPageTablePool->Offset    = EFI_PAGES_TO_SIZE (1);
+  PageTablePool->FreePages = PoolPages - 1;
+  PageTablePool->Offset    = EFI_PAGES_TO_SIZE (1);
 
   return TRUE;
 }
@@ -343,28 +369,33 @@ AllocatePageTableMemory (
   IN UINTN  Pages
   )
 {
-  VOID  *Buffer;
+  VOID             *Buffer;
+  PAGE_TABLE_POOL  *PageTablePool;
 
   if (Pages == 0) {
     return NULL;
   }
 
-  DEBUG ((DEBUG_INFO, "AllocatePageTableMemory. mPageTablePool=%p, Pages=%d\n", mPageTablePool, Pages));
+  PageTablePool = GetPageTablePool ();
+
+  DEBUG ((DEBUG_INFO, "AllocatePageTableMemory. PageTablePool=%p, Pages=%d\n", PageTablePool, Pages));
   //
   // Renew the pool if necessary.
   //
-  if ((mPageTablePool == NULL) ||
-      (Pages > mPageTablePool->FreePages))
+  if ((PageTablePool == NULL) ||
+      (Pages > PageTablePool->FreePages))
   {
     if (!InitializePageTablePool (Pages)) {
       return NULL;
+    } else {
+      PageTablePool = GetPageTablePool ();
     }
   }
 
-  Buffer = (UINT8 *)mPageTablePool + mPageTablePool->Offset;
+  Buffer = (UINT8 *)PageTablePool + PageTablePool->Offset;
 
-  mPageTablePool->Offset    += EFI_PAGES_TO_SIZE (Pages);
-  mPageTablePool->FreePages -= Pages;
+  PageTablePool->Offset    += EFI_PAGES_TO_SIZE (Pages);
+  PageTablePool->FreePages -= Pages;
 
   DEBUG ((
     DEBUG_INFO,
@@ -618,10 +649,13 @@ EnablePageTableProtection (
   PAGE_TABLE_POOL       *Pool;
   UINT64                PoolSize;
   EFI_PHYSICAL_ADDRESS  Address;
+  PAGE_TABLE_POOL       *PageTablePool;
 
   DEBUG ((DEBUG_INFO, "EnablePageTableProtection\n"));
 
-  if (mPageTablePool == NULL) {
+  PageTablePool = GetPageTablePool ();
+
+  if (PageTablePool == NULL) {
     return;
   }
 
@@ -632,10 +666,10 @@ EnablePageTableProtection (
   AsmWriteCr0 (AsmReadCr0 () & ~CR0_WP);
 
   //
-  // SetPageTablePoolReadOnly might update mPageTablePool. It's safer to
+  // SetPageTablePoolReadOnly might update PageTablePool. It's safer to
   // remember original one in advance.
   //
-  HeadPool = mPageTablePool;
+  HeadPool = PageTablePool;
   Pool     = HeadPool;
   do {
     Address  = (EFI_PHYSICAL_ADDRESS)(UINTN)Pool;
-- 
2.29.2.windows.2


                 reply	other threads:[~2022-09-07  2:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220907021800.690-1-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