public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Ankur Arora" <ankur.a.arora@oracle.com>
To: devel@edk2.groups.io
Cc: lersek@redhat.com, imammedo@redhat.com,
	boris.ostrovsky@oracle.com,
	Ankur Arora <ankur.a.arora@oracle.com>,
	Jordan Justen <jordan.l.justen@intel.com>,
	Ard Biesheuvel <ard.biesheuvel@arm.com>,
	Aaron Young <aaron.young@oracle.com>
Subject: [PATCH v8 06/10] OvmfPkg/SmmCpuFeaturesLib: init CPU ejection state
Date: Sun, 21 Feb 2021 23:19:24 -0800	[thread overview]
Message-ID: <20210222071928.1401820-7-ankur.a.arora@oracle.com> (raw)
In-Reply-To: <20210222071928.1401820-1-ankur.a.arora@oracle.com>

Init CPU_HOT_EJECT_DATA, which will be used to share CPU ejection
state between SmmCpuFeaturesLib (via PiSmmCpuDxeSmm) and CpuHotPlugSmm.

The init happens via SmmCpuFeaturesSmmRelocationComplete(), and so it
will run as part of the PiSmmCpuDxeSmm entry point function,
PiCpuSmmEntry(). Once inited, CPU_HOT_EJECT_DATA is exposed via
PcdCpuHotEjectDataAddress.

The CPU hot-eject handler (CPU_HOT_EJECT_DATA->Handler) is setup when
there is an ejection request via CpuHotplugSmm.

Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Igor Mammedov <imammedo@redhat.com>
Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: Aaron Young <aaron.young@oracle.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3132
Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com>
---

Notes:
    Addresses the following review comments:
     (1) Detail in commit message about context in which CPU_HOT_EJECT_DATA
      is inited.
     (2) Add in sorted order MemoryAllocationLib in LibraryClasses
     (3) Sort added includes in SmmCpuFeaturesLib.c
     (4a-4b) Fixup linkage directives for mCpuHotEjectData.
     (5) s/CpuHotEjectData/mCpuHotEjectData/
     (6,10a,10b) Remove dependence on PcdCpuHotPlugSupport
     (7) Make the tense structure consistent in block comment for
      InitCpuHotEject().
     (8) s/SmmCpuFeaturesSmmInitHotEject/InitCpuHotEject/
     (9) s/mMaxNumberOfCpus/MaxNumberOfCpus/
     (11) Remove a bunch of obvious comments.
     (14a,14b,14c) Use SafeUint functions and rework the allocation logic
      so we can just use a single allocation.
     (12) Remove the AllocatePool() cast.
     (13) Use a CpuDeadLoop() in case of failure; albeit via a goto, not
      inline.
     (15) Initialize the mCpuHotEjectData->QemuSelectorMap locally.
     (16) Fix indentation in PcdSet64S.
     (17) Change the cast in PcdSet64S() to UINTN.
     (18) Use RETURN_STATUS instead of EFI_STATUS.
     (19,20) Move the Handler logic in SmmCpuFeaturesRendezvousExit() into
      into a separate patch.

 .../SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf        |  4 +
 .../Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c  | 92 ++++++++++++++++++++++
 2 files changed, 96 insertions(+)

diff --git a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf
index 97a10afb6e27..8a426a4c10fb 100644
--- a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf
+++ b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf
@@ -30,9 +30,13 @@ [LibraryClasses]
   BaseMemoryLib
   DebugLib
   MemEncryptSevLib
+  MemoryAllocationLib
   PcdLib
+  SafeIntLib
   SmmServicesTableLib
   UefiBootServicesTableLib
 
 [Pcd]
+  gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber
+  gUefiOvmfPkgTokenSpaceGuid.PcdCpuHotEjectDataAddress
   gUefiOvmfPkgTokenSpaceGuid.PcdQ35SmramAtDefaultSmbase
diff --git a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c
index 7ef7ed98342e..adbfc90ad46e 100644
--- a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c
+++ b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c
@@ -11,10 +11,13 @@
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
 #include <Library/MemEncryptSevLib.h>
+#include <Library/MemoryAllocationLib.h>
 #include <Library/PcdLib.h>
+#include <Library/SafeIntLib.h>
 #include <Library/SmmCpuFeaturesLib.h>
 #include <Library/SmmServicesTableLib.h>
 #include <Library/UefiBootServicesTableLib.h>
+#include <Pcd/CpuHotEjectData.h>
 #include <PiSmm.h>
 #include <Register/Intel/SmramSaveStateMap.h>
 #include <Register/QemuSmramSaveStateMap.h>
@@ -171,6 +174,92 @@ SmmCpuFeaturesHookReturnFromSmm (
   return OriginalInstructionPointer;
 }
 
+STATIC CPU_HOT_EJECT_DATA *mCpuHotEjectData = NULL;
+
+/**
+  Initialize mCpuHotEjectData if PcdCpuMaxLogicalProcessorNumber > 1.
+
+  Also setup the corresponding PcdCpuHotEjectDataAddress.
+**/
+STATIC
+VOID
+InitCpuHotEjectData (
+  VOID
+  )
+{
+  UINTN          ArrayLen;
+  UINTN          BaseLen;
+  UINTN          TotalLen;
+  UINT32         Idx;
+  UINT32         MaxNumberOfCpus;
+  RETURN_STATUS  PcdStatus;
+
+  MaxNumberOfCpus = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
+
+  if (MaxNumberOfCpus == 1) {
+    return;
+  }
+
+  //
+  // We want the following lay out for CPU_HOT_EJECT_DATA:
+  //  UINTN alignment:  CPU_HOT_EJECT_DATA
+  //                  --- padding if needed ---
+  //  UINT64 alignment:  CPU_HOT_EJECT_DATA->QemuSelectorMap[]
+  //
+  // Accordingly, we allocate:
+  //   sizeof(*mCpuHotEjectData) + (MaxNumberOfCpus *
+  //     sizeof(mCpuHotEjectData->QemuSelectorMap[0])).
+  // Add sizeof(UINT64) to use as padding if needed.
+  //
+
+  if (RETURN_ERROR (SafeUintnMult (sizeof (*mCpuHotEjectData), 1, &BaseLen)) ||
+      RETURN_ERROR (SafeUintnMult (
+                      sizeof (mCpuHotEjectData->QemuSelectorMap[0]),
+                      MaxNumberOfCpus, &ArrayLen)) ||
+      RETURN_ERROR (SafeUintnAdd (BaseLen, ArrayLen, &TotalLen))||
+      RETURN_ERROR (SafeUintnAdd (TotalLen, sizeof (UINT64), &TotalLen))) {
+    DEBUG ((DEBUG_ERROR, "%a: invalid CPU_HOT_EJECT_DATA\n", __FUNCTION__));
+    goto Fatal;
+  }
+
+  mCpuHotEjectData = AllocatePool (TotalLen);
+  if (mCpuHotEjectData == NULL) {
+    ASSERT (mCpuHotEjectData != NULL);
+    goto Fatal;
+  }
+
+  mCpuHotEjectData->Handler = NULL;
+  mCpuHotEjectData->ArrayLength = MaxNumberOfCpus;
+
+  mCpuHotEjectData->QemuSelectorMap = (void *)mCpuHotEjectData +
+                                        sizeof (*mCpuHotEjectData);
+  mCpuHotEjectData->QemuSelectorMap =
+    (void *)ALIGN_VALUE ((UINTN)mCpuHotEjectData->QemuSelectorMap,
+                           sizeof (UINT64));
+  //
+  // We use mCpuHotEjectData->QemuSelectorMap to map
+  // ProcessorNum -> QemuSelector. Initialize to invalid values.
+  //
+  for (Idx = 0; Idx < mCpuHotEjectData->ArrayLength; Idx++) {
+    mCpuHotEjectData->QemuSelectorMap[Idx] = CPU_EJECT_QEMU_SELECTOR_INVALID;
+  }
+
+  //
+  // Expose address of CPU Hot eject Data structure
+  //
+  PcdStatus = PcdSet64S (PcdCpuHotEjectDataAddress,
+                (UINTN)(VOID *)mCpuHotEjectData);
+  if (RETURN_ERROR (PcdStatus)) {
+    ASSERT_EFI_ERROR (PcdStatus);
+    goto Fatal;
+  }
+
+  return;
+
+Fatal:
+  CpuDeadLoop ();
+}
+
 /**
   Hook point in normal execution mode that allows the one CPU that was elected
   as monarch during System Management Mode initialization to perform additional
@@ -188,6 +277,9 @@ SmmCpuFeaturesSmmRelocationComplete (
   UINTN      MapPagesBase;
   UINTN      MapPagesCount;
 
+
+  InitCpuHotEjectData ();
+
   if (!MemEncryptSevIsEnabled ()) {
     return;
   }
-- 
2.9.3


  parent reply	other threads:[~2021-02-22  7:19 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-22  7:19 [PATCH v8 00/10] support CPU hot-unplug Ankur Arora
2021-02-22  7:19 ` [PATCH v8 01/10] OvmfPkg/CpuHotplugSmm: refactor hotplug logic Ankur Arora
2021-02-22 11:49   ` [edk2-devel] " Laszlo Ersek
2021-02-22  7:19 ` [PATCH v8 02/10] OvmfPkg/CpuHotplugSmm: collect hot-unplug events Ankur Arora
2021-02-22 12:27   ` [edk2-devel] " Laszlo Ersek
2021-02-22 22:03     ` Ankur Arora
2021-02-23 16:44       ` Laszlo Ersek
2021-02-22  7:19 ` [PATCH v8 03/10] OvmfPkg/CpuHotplugSmm: add Qemu Cpu Status helper Ankur Arora
2021-02-22 12:31   ` [edk2-devel] " Laszlo Ersek
2021-02-22 22:22     ` Ankur Arora
2021-02-22  7:19 ` [PATCH v8 04/10] OvmfPkg/CpuHotplugSmm: introduce UnplugCpus() Ankur Arora
2021-02-22 12:39   ` [edk2-devel] " Laszlo Ersek
2021-02-22 22:22     ` Ankur Arora
2021-02-22  7:19 ` [PATCH v8 05/10] OvmfPkg/CpuHotplugSmm: define CPU_HOT_EJECT_DATA Ankur Arora
2021-02-22 13:06   ` [edk2-devel] " Laszlo Ersek
2021-02-22 22:33     ` Ankur Arora
2021-02-22  7:19 ` Ankur Arora [this message]
2021-02-22 14:19   ` [edk2-devel] [PATCH v8 06/10] OvmfPkg/SmmCpuFeaturesLib: init CPU ejection state Laszlo Ersek
2021-02-23  7:37     ` Ankur Arora
2021-02-22  7:19 ` [PATCH v8 07/10] OvmfPkg/SmmCpuFeaturesLib: call CPU hot-eject handler Ankur Arora
2021-02-22 14:53   ` [edk2-devel] " Laszlo Ersek
2021-02-23  7:37     ` Ankur Arora
2021-02-23 16:52       ` Laszlo Ersek
2021-02-23  7:45     ` Paolo Bonzini
2021-02-23 17:06       ` Laszlo Ersek
2021-02-23 17:18         ` Paolo Bonzini
2021-02-23 20:46           ` Ankur Arora
2021-02-22  7:19 ` [PATCH v8 08/10] OvmfPkg/CpuHotplugSmm: add EjectCpu() Ankur Arora
2021-02-23 20:36   ` [edk2-devel] " Laszlo Ersek
2021-02-23 20:51     ` Ankur Arora
2021-02-22  7:19 ` [PATCH v8 09/10] OvmfPkg/CpuHotplugSmm: do actual CPU hot-eject Ankur Arora
2021-02-23 21:39   ` [edk2-devel] " Laszlo Ersek
2021-02-24  3:44     ` Ankur Arora
2021-02-25 19:22       ` Laszlo Ersek
2021-02-22  7:19 ` [PATCH v8 10/10] OvmfPkg/SmmControl2Dxe: negotiate CPU hot-unplug Ankur Arora
2021-02-23 21:52   ` [edk2-devel] " Laszlo Ersek

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=20210222071928.1401820-7-ankur.a.arora@oracle.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