From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mx.groups.io with SMTP id smtpd.web08.6299.1661821454642166759 for ; Mon, 29 Aug 2022 18:04:14 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=McfNoGUa; spf=permerror, err=too many SPF records (domain: intel.com, ip: 192.55.52.136, 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=1661821454; x=1693357454; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5zOsO6uY+eF3A5922xLz/6xARX6JeFOrL1XxuOJVm6o=; b=McfNoGUaDY8dVerbXCdAUcUrz2KaFSXzD9ydDLjgcmBloTRNkQhRpILG VrK2x5faLVwX99KtM5H2xA4RetQSOqZhhkmG14MGnAnp7N19nbbLdq2P0 LnCvCjnxCC9Jlp6JtWBlBTCTFliSz1NSCRF4bG3S/2Qxk44B9QeX88qNv yqfIOkaFlD9QMPWgBf92jjZRYDg5nEsG/+ceeiN6NohtzS3TbMq/uHga7 +RRwI2RpX8cZ39p0katndT9l3mIfc0pcMGr4AUEXbs9UkJ4/uXBbN3lrH gNjW40jYVvHz7s7QoCUM2M/DM3EnobpwFGaUCjzNImKXYoitqz5h6jEHl Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10454"; a="274794055" X-IronPort-AV: E=Sophos;i="5.93,274,1654585200"; d="scan'208";a="274794055" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Aug 2022 18:03:57 -0700 X-IronPort-AV: E=Sophos;i="5.93,273,1654585200"; d="scan'208";a="672612262" Received: from mxu9-mobl1.ccr.corp.intel.com ([10.238.4.200]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Aug 2022 18:03:55 -0700 From: "Min Xu" To: devel@edk2.groups.io Cc: Min M Xu , Gerd Hoffmann , Erdem Aktas , James Bottomley , Jiewen Yao , Tom Lendacky Subject: [PATCH V5 2/2] OvmfPkg: Update CcProbeLib to DxeCcProbeLib Date: Tue, 30 Aug 2022 09:03:40 +0800 Message-Id: X-Mailer: git-send-email 2.29.2.windows.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Min M Xu BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3974 CcProbeLib once was designed to probe the Confidential Computing guest type by checking the PcdOvmfWorkArea. But this memory is allocated with either EfiACPIMemoryNVS or EfiBootServicesData. It cannot be accessed after ExitBootService. Please see the detailed analysis in BZ#3974. To fix this issue, CcProbeLib is redesigned as 2 implementation: - SecPeiCcProbeLib - DxeCcProbeLib In SecPeiCcProbeLib we check the CC guest type by reading the PcdOvmfWorkArea. Because it is used in SEC / PEI and we don't worry about the issues in BZ#3974. In DxeCcProbeLib we cache the GuestType in Ovmf work area in a variable. After that the Guest type is returned with the cached value. So that we don't need to worry about the access to Ovmf work area after ExitBootService. To guarantee the GuestType is cached, we read the value in both constructor and CcProbe. Because in some corner case, the constructor may be called after CcProbe. For example in MdeModulePkg/Core/Dxe/DxeMain, BaseDebugLibSerialPortConstructor is called before DxeCcProbeLibConstructor. While CcProbe () is called in BaseDebugLibSerialPortConstructor. The reason why we probe CC guest type in 2 different ways is the global variable. Global variable cannot be used in SEC/PEI and CcProbe is called very frequently. Cc: Gerd Hoffmann Cc: Erdem Aktas Cc: James Bottomley Cc: Jiewen Yao Cc: Tom Lendacky Acked-by: Gerd Hoffmann Signed-off-by: Min Xu --- OvmfPkg/IntelTdx/IntelTdxX64.dsc | 3 +- OvmfPkg/Library/CcProbeLib/CcProbeLib.c | 31 --------- OvmfPkg/Library/CcProbeLib/DxeCcProbeLib.c | 68 +++++++++++++++++++ .../{CcProbeLib.inf => DxeCcProbeLib.inf} | 7 +- OvmfPkg/OvmfPkgX64.dsc | 5 +- 5 files changed, 78 insertions(+), 36 deletions(-) delete mode 100644 OvmfPkg/Library/CcProbeLib/CcProbeLib.c create mode 100644 OvmfPkg/Library/CcProbeLib/DxeCcProbeLib.c rename OvmfPkg/Library/CcProbeLib/{CcProbeLib.inf => DxeCcProbeLib.inf} (65%) diff --git a/OvmfPkg/IntelTdx/IntelTdxX64.dsc b/OvmfPkg/IntelTdx/IntelTdxX64.dsc index 71b1cf8e7090..1a7ecd503e50 100644 --- a/OvmfPkg/IntelTdx/IntelTdxX64.dsc +++ b/OvmfPkg/IntelTdx/IntelTdxX64.dsc @@ -140,7 +140,7 @@ PciCapLib|OvmfPkg/Library/BasePciCapLib/BasePciCapLib.inf PciCapPciSegmentLib|OvmfPkg/Library/BasePciCapPciSegmentLib/BasePciCapPciSegmentLib.inf PciCapPciIoLib|OvmfPkg/Library/UefiPciCapPciIoLib/UefiPciCapPciIoLib.inf - CcProbeLib|OvmfPkg/Library/CcProbeLib/CcProbeLib.inf + CcProbeLib|OvmfPkg/Library/CcProbeLib/DxeCcProbeLib.inf IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev.inf OemHookStatusCodeLib|MdeModulePkg/Library/OemHookStatusCodeLibNull/OemHookStatusCodeLibNull.inf SerialPortLib|PcAtChipsetPkg/Library/SerialIoLib/SerialIoLib.inf @@ -234,6 +234,7 @@ HobLib|EmbeddedPkg/Library/PrePiHobLib/PrePiHobLib.inf PrePiLib|EmbeddedPkg/Library/PrePiLib/PrePiLib.inf PeilessStartupLib|OvmfPkg/Library/PeilessStartupLib/PeilessStartupLib.inf + CcProbeLib|OvmfPkg/Library/CcProbeLib/SecPeiCcProbeLib.inf [LibraryClasses.common.DXE_CORE] HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf diff --git a/OvmfPkg/Library/CcProbeLib/CcProbeLib.c b/OvmfPkg/Library/CcProbeLib/CcProbeLib.c deleted file mode 100644 index d698e5c8d7f8..000000000000 --- a/OvmfPkg/Library/CcProbeLib/CcProbeLib.c +++ /dev/null @@ -1,31 +0,0 @@ -/** @file - - CcProbeLib is used to probe the Confidential computing guest type. - - Copyright (c) 2022, Intel Corporation. All rights reserved.
- SPDX-License-Identifier: BSD-2-Clause-Patent - -**/ - -#include -#include - -/** - Probe the ConfidentialComputing Guest type. See defition of - CC_GUEST_TYPE in . - - @return The guest type - -**/ -UINT8 -EFIAPI -CcProbe ( - VOID - ) -{ - OVMF_WORK_AREA *WorkArea; - - WorkArea = (OVMF_WORK_AREA *)FixedPcdGet32 (PcdOvmfWorkAreaBase); - - return WorkArea != NULL ? WorkArea->Header.GuestType : CcGuestTypeNonEncrypted; -} diff --git a/OvmfPkg/Library/CcProbeLib/DxeCcProbeLib.c b/OvmfPkg/Library/CcProbeLib/DxeCcProbeLib.c new file mode 100644 index 000000000000..868e32bf7f60 --- /dev/null +++ b/OvmfPkg/Library/CcProbeLib/DxeCcProbeLib.c @@ -0,0 +1,68 @@ +/** @file + + CcProbeLib is used to probe the Confidential computing guest type. + + Copyright (c) 2022, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include + +STATIC UINT8 mCcProbeGuestType = 0; +STATIC BOOLEAN mCcProbed = FALSE; + +/** + * Read the the ConfidentialComputing Guest type from Ovmf work-area. + * + * @return The ConfidentialComputing Guest type + */ +STATIC +UINT8 +ReadCcGuestType ( + VOID + ) +{ + OVMF_WORK_AREA *WorkArea; + + if (!mCcProbed) { + WorkArea = (OVMF_WORK_AREA *)FixedPcdGet32 (PcdOvmfWorkAreaBase); + mCcProbeGuestType = WorkArea != NULL ? WorkArea->Header.GuestType : CcGuestTypeNonEncrypted; + mCcProbed = TRUE; + } + + return mCcProbeGuestType; +} + +/** + Probe the ConfidentialComputing Guest type. See defition of + CC_GUEST_TYPE in . + + @return The guest type + +**/ +UINT8 +EFIAPI +CcProbe ( + VOID + ) +{ + return ReadCcGuestType (); +} + +/** + * Constructor of DxeCcProbeLib + * + * @return EFI_SUCCESS Successfully called of constructor + */ +EFI_STATUS +EFIAPI +DxeCcProbeLibConstructor ( + VOID + ) +{ + ReadCcGuestType (); + return EFI_SUCCESS; +} diff --git a/OvmfPkg/Library/CcProbeLib/CcProbeLib.inf b/OvmfPkg/Library/CcProbeLib/DxeCcProbeLib.inf similarity index 65% rename from OvmfPkg/Library/CcProbeLib/CcProbeLib.inf rename to OvmfPkg/Library/CcProbeLib/DxeCcProbeLib.inf index 5300c9ba2644..2fd0b4d46d10 100644 --- a/OvmfPkg/Library/CcProbeLib/CcProbeLib.inf +++ b/OvmfPkg/Library/CcProbeLib/DxeCcProbeLib.inf @@ -8,14 +8,15 @@ [Defines] INF_VERSION = 0x00010005 - BASE_NAME = CcProbeLib + BASE_NAME = DxeCcProbeLib FILE_GUID = 05184ec9-abb0-4491-8584-e388639a7c48 MODULE_TYPE = BASE VERSION_STRING = 1.0 - LIBRARY_CLASS = CcProbeLib + LIBRARY_CLASS = CcProbeLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_DRIVER UEFI_APPLICATION + CONSTRUCTOR = DxeCcProbeLibConstructor [Sources] - CcProbeLib.c + DxeCcProbeLib.c [Packages] MdePkg/MdePkg.dec diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 6e68f60dc90f..2741f4d988b7 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -199,7 +199,7 @@ !if $(SMM_REQUIRE) == FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf - CcProbeLib|OvmfPkg/Library/CcProbeLib/CcProbeLib.inf + CcProbeLib|OvmfPkg/Library/CcProbeLib/DxeCcProbeLib.inf !else CcProbeLib|MdePkg/Library/CcProbeLibNull/CcProbeLibNull.inf !endif @@ -287,6 +287,7 @@ !endif VmgExitLib|OvmfPkg/Library/VmgExitLib/SecVmgExitLib.inf MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLib.inf + CcProbeLib|OvmfPkg/Library/CcProbeLib/SecPeiCcProbeLib.inf [LibraryClasses.common.PEI_CORE] HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf @@ -303,6 +304,7 @@ DebugLib|OvmfPkg/Library/PlatformDebugLibIoPort/PlatformDebugLibIoPort.inf !endif PeCoffLib|MdePkg/Library/BasePeCoffLib/BasePeCoffLib.inf + CcProbeLib|OvmfPkg/Library/CcProbeLib/SecPeiCcProbeLib.inf [LibraryClasses.common.PEIM] HobLib|MdePkg/Library/PeiHobLib/PeiHobLib.inf @@ -332,6 +334,7 @@ PlatformInitLib|OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf + CcProbeLib|OvmfPkg/Library/CcProbeLib/SecPeiCcProbeLib.inf [LibraryClasses.common.DXE_CORE] HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf -- 2.29.2.windows.2