From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by mx.groups.io with SMTP id smtpd.web12.8480.1648454962517853012 for ; Mon, 28 Mar 2022 01:09:37 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=mp5JUTdy; spf=pass (domain: intel.com, ip: 192.55.52.120, 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=1648454977; x=1679990977; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BzyTdsSVS+SuZ/CYlz0YmxXTj/koAbe0SUvyIIZpZOU=; b=mp5JUTdyB3W6ewmombQepSMW9cDCu6vAEEDd8oj7hyZhjtIu9jBXWAK2 94GcmwMTyN+xgPV6JKhw3nlbqg4HZ7ILL/c66iAnMVtrZHU116kdhf8zO Mgr7rKCVTCbu4vvhjnEucjBZu04gzF6aEjt1Po0DSnVD7GeCWn00f3TKM 8wc7Lnd03QCamRP2R+rcIdrnMBNP1oF4kS6mw9TeISi7MD0GpaR8yDD1i aXrF9d7K6eXYv+dTLvkCIkgpqPh+yMWJrg+67xWtQegIIlxLHL9mZ69R+ LZtMXxbj/YKsoLpw4kz+Aj2ucJhO3Pnv3KYg/yTRDU+McJjPhfFB864Ag Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10299"; a="257770838" X-IronPort-AV: E=Sophos;i="5.90,216,1643702400"; d="scan'208";a="257770838" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2022 01:09:32 -0700 X-IronPort-AV: E=Sophos;i="5.90,216,1643702400"; d="scan'208";a="563427188" Received: from mxu9-mobl1.ccr.corp.intel.com ([10.249.175.167]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Mar 2022 01:09:29 -0700 From: "Min Xu" To: devel@edk2.groups.io Cc: Min Xu , Ard Biesheuvel , Jordan Justen , Brijesh Singh , Erdem Aktas , James Bottomley , Jiewen Yao , Tom Lendacky , Gerd Hoffmann Subject: [PATCH V11 16/47] OvmfPkg: Add TdxMailboxLib Date: Mon, 28 Mar 2022 16:07:55 +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 RFC: https://bugzilla.tianocore.org/show_bug.cgi?id=3429 In Tdx BSP may issues commands to APs for some task, for example, to accept pages paralelly. BSP also need to wait until all the APs have done the task. TdxMailboxLib wraps these common funtions for BSP. Cc: Ard Biesheuvel Cc: Jordan Justen Cc: Brijesh Singh Cc: Erdem Aktas Cc: James Bottomley Cc: Jiewen Yao Cc: Tom Lendacky Cc: Gerd Hoffmann Acked-by: Gerd Hoffmann Signed-off-by: Min Xu --- OvmfPkg/Include/Library/TdxMailboxLib.h | 76 ++++++++++ OvmfPkg/Library/TdxMailboxLib/TdxMailbox.c | 141 ++++++++++++++++++ .../Library/TdxMailboxLib/TdxMailboxLib.inf | 52 +++++++ .../Library/TdxMailboxLib/TdxMailboxNull.c | 85 +++++++++++ OvmfPkg/OvmfPkg.dec | 4 + 5 files changed, 358 insertions(+) create mode 100644 OvmfPkg/Include/Library/TdxMailboxLib.h create mode 100644 OvmfPkg/Library/TdxMailboxLib/TdxMailbox.c create mode 100644 OvmfPkg/Library/TdxMailboxLib/TdxMailboxLib.inf create mode 100644 OvmfPkg/Library/TdxMailboxLib/TdxMailboxNull.c diff --git a/OvmfPkg/Include/Library/TdxMailboxLib.h b/OvmfPkg/Include/Library/TdxMailboxLib.h new file mode 100644 index 000000000000..166cab43bc02 --- /dev/null +++ b/OvmfPkg/Include/Library/TdxMailboxLib.h @@ -0,0 +1,76 @@ +/** @file + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef TDX_MAILBOX_LIB_H_ +#define TDX_MAILBOX_LIB_H_ + +#include +#include +#include +#include +#include +#include + +/** + This function will be called by BSP to get the CPU number. + + @retval CPU number +**/ +UINT32 +EFIAPI +GetCpusNum ( + VOID + ); + +/** + Get the address of Td mailbox. +**/ +volatile VOID * +EFIAPI +GetTdxMailBox ( + VOID + ); + +/** + This function will be called by BSP to wakeup APs the are spinning on mailbox + in protected mode + + @param[in] Command Command to send APs + @param[in] WakeupVector If used, address for APs to start executing + @param[in] WakeArgsX Args to pass to APs for excuting commands +**/ +VOID +EFIAPI +MpSendWakeupCommand ( + IN UINT16 Command, + IN UINT64 WakeupVector, + IN UINT64 WakeupArgs1, + IN UINT64 WakeupArgs2, + IN UINT64 WakeupArgs3, + IN UINT64 WakeupArgs4 + ); + +/** + BSP wait until all the APs arriving. It means the task triggered by BSP is started. +**/ +VOID +EFIAPI +MpSerializeStart ( + VOID + ); + +/** + BSP wait until all the APs arriving. It means the task triggered by BSP is ended. +**/ +VOID +EFIAPI +MpSerializeEnd ( + VOID + ); + +#endif diff --git a/OvmfPkg/Library/TdxMailboxLib/TdxMailbox.c b/OvmfPkg/Library/TdxMailboxLib/TdxMailbox.c new file mode 100644 index 000000000000..74cb55611fe3 --- /dev/null +++ b/OvmfPkg/Library/TdxMailboxLib/TdxMailbox.c @@ -0,0 +1,141 @@ +/** @file + + Copyright (c) 2008, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +volatile VOID *mMailBox = NULL; +UINT32 mNumOfCpus = 0; + +/** + This function will be called by BSP to get the CPU number. + + @retval CPU number +**/ +UINT32 +EFIAPI +GetCpusNum ( + VOID + ) +{ + if (mNumOfCpus == 0) { + mNumOfCpus = TdVCpuNum (); + } + + return mNumOfCpus; +} + +/** + Get the address of Td mailbox. +**/ +volatile VOID * +EFIAPI +GetTdxMailBox ( + VOID + ) +{ + if (mMailBox == NULL) { + mMailBox = (VOID *)(UINTN)PcdGet32 (PcdOvmfSecGhcbBackupBase); + } + + return mMailBox; +} + +/** + This function will be called by BSP to wakeup APs the are spinning on mailbox + in protected mode + + @param[in] Command Command to send APs + @param[in] WakeupVector If used, address for APs to start executing + @param[in] WakeArgsX Args to pass to APs for excuting commands +**/ +VOID +EFIAPI +MpSendWakeupCommand ( + IN UINT16 Command, + IN UINT64 WakeupVector, + IN UINT64 WakeupArgs1, + IN UINT64 WakeupArgs2, + IN UINT64 WakeupArgs3, + IN UINT64 WakeupArgs4 + ) +{ + volatile MP_WAKEUP_MAILBOX *MailBox; + + MailBox = (volatile MP_WAKEUP_MAILBOX *)GetTdxMailBox (); + MailBox->ApicId = MP_CPU_PROTECTED_MODE_MAILBOX_APICID_INVALID; + MailBox->WakeUpVector = 0; + MailBox->Command = MpProtectedModeWakeupCommandNoop; + MailBox->ApicId = MP_CPU_PROTECTED_MODE_MAILBOX_APICID_BROADCAST; + MailBox->WakeUpVector = WakeupVector; + MailBox->WakeUpArgs1 = WakeupArgs1; + MailBox->WakeUpArgs2 = WakeupArgs2; + MailBox->WakeUpArgs3 = WakeupArgs3; + MailBox->WakeUpArgs4 = WakeupArgs4; + AsmCpuid (0x01, NULL, NULL, NULL, NULL); + MailBox->Command = Command; + AsmCpuid (0x01, NULL, NULL, NULL, NULL); + return; +} + +/** + BSP wait until all the APs arriving. It means the task triggered by BSP is started. +**/ +VOID +EFIAPI +MpSerializeStart ( + VOID + ) +{ + volatile MP_WAKEUP_MAILBOX *MailBox; + UINT32 NumOfCpus; + + NumOfCpus = GetCpusNum (); + MailBox = (volatile MP_WAKEUP_MAILBOX *)GetTdxMailBox (); + + DEBUG ((DEBUG_VERBOSE, "Waiting for APs to arriving. NumOfCpus=%d, MailBox=%p\n", NumOfCpus, MailBox)); + while (MailBox->NumCpusArriving != (NumOfCpus -1)) { + CpuPause (); + } + + DEBUG ((DEBUG_VERBOSE, "Releasing APs\n")); + MailBox->NumCpusExiting = NumOfCpus; + InterlockedIncrement ((UINT32 *)&MailBox->NumCpusArriving); +} + +/** + BSP wait until all the APs arriving. It means the task triggered by BSP is ended. +**/ +VOID +EFIAPI +MpSerializeEnd ( + VOID + ) +{ + volatile MP_WAKEUP_MAILBOX *MailBox; + + MailBox = (volatile MP_WAKEUP_MAILBOX *)GetTdxMailBox (); + DEBUG ((DEBUG_VERBOSE, "Waiting for APs to finish\n")); + while (MailBox->NumCpusExiting != 1 ) { + CpuPause (); + } + + DEBUG ((DEBUG_VERBOSE, "Restarting APs\n")); + MailBox->Command = MpProtectedModeWakeupCommandNoop; + MailBox->NumCpusArriving = 0; + InterlockedDecrement ((UINT32 *)&MailBox->NumCpusExiting); +} diff --git a/OvmfPkg/Library/TdxMailboxLib/TdxMailboxLib.inf b/OvmfPkg/Library/TdxMailboxLib/TdxMailboxLib.inf new file mode 100644 index 000000000000..3cf3690a16c7 --- /dev/null +++ b/OvmfPkg/Library/TdxMailboxLib/TdxMailboxLib.inf @@ -0,0 +1,52 @@ +#/** @file +# +# TBD +# +# Copyright (c) 2018, Intel Corporation. All rights reserved.
+# Copyright (c) 2008, Apple Inc. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +#**/ + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = TdxMailboxLib + FILE_GUID = 2F81A9BA-748E-4519-BB11-A63A039D561E + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = TdxMailboxLib + +# +# VALID_ARCHITECTURES = X64 IA32 +# + +[Sources.IA32] + TdxMailboxNull.c + +[Sources.X64] + TdxMailbox.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UefiCpuPkg/UefiCpuPkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + BaseMemoryLib + PcdLib + UefiCpuLib + DebugAgentLib + IoLib + SynchronizationLib + MemoryAllocationLib + +[Guids] + +[Pcd] + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupBase + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupSize diff --git a/OvmfPkg/Library/TdxMailboxLib/TdxMailboxNull.c b/OvmfPkg/Library/TdxMailboxLib/TdxMailboxNull.c new file mode 100644 index 000000000000..35b070361eb1 --- /dev/null +++ b/OvmfPkg/Library/TdxMailboxLib/TdxMailboxNull.c @@ -0,0 +1,85 @@ +/** @file + + Copyright (c) 2008, Intel Corporation. All rights reserved.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include + +/** + This function will be called by BSP to get the CPU number. + + @retval CPU number +**/ +UINT32 +EFIAPI +GetCpusNum ( + VOID + ) +{ + ASSERT (FALSE); + return 0; +} + +/** + Get the address of Td mailbox. +**/ +volatile VOID * +EFIAPI +GetTdxMailBox ( + VOID + ) +{ + ASSERT (FALSE); + return (volatile VOID *)NULL; +} + +/** + This function will be called by BSP to wakeup APs the are spinning on mailbox + in protected mode + + @param[in] Command Command to send APs + @param[in] WakeupVector If used, address for APs to start executing + @param[in] WakeArgsX Args to pass to APs for excuting commands +**/ +VOID +EFIAPI +MpSendWakeupCommand ( + IN UINT16 Command, + IN UINT64 WakeupVector, + IN UINT64 WakeupArgs1, + IN UINT64 WakeupArgs2, + IN UINT64 WakeupArgs3, + IN UINT64 WakeupArgs4 + ) +{ + ASSERT (FALSE); +} + +/** + BSP wait until all the APs arriving. It means the task triggered by BSP is started. +**/ +VOID +EFIAPI +MpSerializeStart ( + VOID + ) +{ + ASSERT (FALSE); +} + +/** + BSP wait until all the APs arriving. It means the task triggered by BSP is ended. +**/ +VOID +EFIAPI +MpSerializeEnd ( + VOID + ) +{ + ASSERT (FALSE); +} diff --git a/OvmfPkg/OvmfPkg.dec b/OvmfPkg/OvmfPkg.dec index 7aa94ca02863..d373b5d6042e 100644 --- a/OvmfPkg/OvmfPkg.dec +++ b/OvmfPkg/OvmfPkg.dec @@ -109,6 +109,10 @@ # XenPlatformLib|Include/Library/XenPlatformLib.h + ## @libraryclass TdxMailboxLib + # + TdxMailboxLib|Include/Library/TdxMailboxLib.h + [Guids] gUefiOvmfPkgTokenSpaceGuid = {0x93bb96af, 0xb9f2, 0x4eb8, {0x94, 0x62, 0xe0, 0xba, 0x74, 0x56, 0x42, 0x36}} gEfiXenInfoGuid = {0xd3b46f3b, 0xd441, 0x1244, {0x9a, 0x12, 0x0, 0x12, 0x27, 0x3f, 0xc1, 0x4d}} -- 2.29.2.windows.2