From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by mx.groups.io with SMTP id smtpd.web10.3164.1662517093460558429 for ; Tue, 06 Sep 2022 19:18:13 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=V4FLIJ6a; spf=permerror, err=too many SPF records (domain: intel.com, ip: 192.55.52.93, mailfrom: min.m.xu@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662517093; x=1694053093; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=toNrRQRN9GrShD11EVOzRijkwXHoialLl46zODficwU=; b=V4FLIJ6acK6ExDHWT8NIfOSeNQQZUlxPgtpM9oztBbqGrxVByOAD11SS EwjJClB/RoxbIhVYt1bLOMNlytYEWfXZU15Y6ARbulMcXjNG1D5Pbbklf CcF2of3j/mqDllUJKsEXmDCIytUZQHcPRpvfmtiQK9hfzp93+ly5ZXH1Z cjHT9qgzszKbzPdq/df2NU45ZnisA+wM4fKOI8dgSp6SUtqI15qHK8KGV uy/Y1V6gZ8JgL6QxnU9b821q6K/exqjbrz60mq7d9eBoIuQvnvloRksVx UufLOGPkFzx/JAOTUD9sn/WVePbhyMDCwbp5f2BqGce7HNbVffJFvwhcz g==; X-IronPort-AV: E=McAfee;i="6500,9779,10462"; a="294344236" X-IronPort-AV: E=Sophos;i="5.93,295,1654585200"; d="scan'208";a="294344236" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Sep 2022 19:18:13 -0700 X-IronPort-AV: E=Sophos;i="5.93,295,1654585200"; d="scan'208";a="644414090" Received: from mxu9-mobl1.ccr.corp.intel.com ([10.238.4.118]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Sep 2022 19:18:11 -0700 From: "Min Xu" To: devel@edk2.groups.io Cc: Min M Xu , Erdem Aktas , Gerd Hoffmann , James Bottomley , Jiewen Yao , Tom Lendacky Subject: [PATCH 1/1] OvmfPkg: Store PageTablePool in TdxWorkArea Date: Wed, 7 Sep 2022 10:18:00 +0800 Message-Id: <20220907021800.690-1-min.m.xu@intel.com> X-Mailer: git-send-email 2.29.2.windows.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Min M Xu 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 Cc: Gerd Hoffmann Cc: James Bottomley Cc: Jiewen Yao Cc: Tom Lendacky Signed-off-by: Min M Xu --- 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 #include #include +#include #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