]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/MpInitLib: Allocate a separate SEV-ES AP reset stack area
authorLendacky, Thomas <thomas.lendacky@amd.com>
Fri, 14 May 2021 20:28:45 +0000 (15:28 -0500)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sat, 29 May 2021 11:33:16 +0000 (11:33 +0000)
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3324

The SEV-ES stacks currently share a page with the reset code and data.
Separate the SEV-ES stacks from the reset vector code and data to avoid
possible stack overflows from overwriting the code and/or data.

When SEV-ES is enabled, invoke the GetWakeupBuffer() routine a second time
to allocate a new area, below the reset vector and data.

Both the PEI and DXE versions of GetWakeupBuffer() are changed so that
when PcdSevEsIsEnabled is true, they will track the previous reset buffer
allocation in order to ensure that the new buffer allocation is below the
previous allocation. When PcdSevEsIsEnabled is false, the original logic
is followed.

Fixes: 7b7508ad784d16a5208c8d12dff43aef6df0835b
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Marvin Häuser <mhaeuser@posteo.de>
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
Message-Id: <3cae2ac836884b131725866264e0a0e1897052de.1621024125.git.thomas.lendacky@amd.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg/Library/MpInitLib/PeiMpLib.c

index 7839c249760ec13c102619a3a10d698cffeae4c5..93fc63bf93e3070d5e3ddffae14158d72aacc74c 100644 (file)
@@ -29,6 +29,11 @@ VOID             *mReservedApLoopFunc = NULL;
 UINTN            mReservedTopOfApStack;\r
 volatile UINT32  mNumberToFinish = 0;\r
 \r
+//\r
+// Begin wakeup buffer allocation below 0x88000\r
+//\r
+STATIC EFI_PHYSICAL_ADDRESS mSevEsDxeWakeupBuffer = 0x88000;\r
+\r
 /**\r
   Enable Debug Agent to support source debugging on AP function.\r
 \r
@@ -102,7 +107,14 @@ GetWakeupBuffer (
   // LagacyBios driver depends on CPU Arch protocol which guarantees below\r
   // allocation runs earlier than LegacyBios driver.\r
   //\r
-  StartAddress = 0x88000;\r
+  if (PcdGetBool (PcdSevEsIsEnabled)) {\r
+    //\r
+    // SEV-ES Wakeup buffer should be under 0x88000 and under any previous one\r
+    //\r
+    StartAddress = mSevEsDxeWakeupBuffer;\r
+  } else {\r
+    StartAddress = 0x88000;\r
+  }\r
   Status = gBS->AllocatePages (\r
                   AllocateMaxAddress,\r
                   MemoryType,\r
@@ -112,6 +124,11 @@ GetWakeupBuffer (
   ASSERT_EFI_ERROR (Status);\r
   if (EFI_ERROR (Status)) {\r
     StartAddress = (EFI_PHYSICAL_ADDRESS) -1;\r
+  } else if (PcdGetBool (PcdSevEsIsEnabled)) {\r
+    //\r
+    // Next SEV-ES wakeup buffer allocation must be below this allocation\r
+    //\r
+    mSevEsDxeWakeupBuffer = StartAddress;\r
   }\r
 \r
   DEBUG ((DEBUG_INFO, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",\r
index dc2a54aa31e894cac939e2b22ba2bc9252f4a11d..b9a06747edbf1ba504def3fed7febe656bfa7be1 100644 (file)
@@ -1164,20 +1164,6 @@ GetApResetVectorSize (
            AddressMap->SwitchToRealSize +\r
            sizeof (MP_CPU_EXCHANGE_INFO);\r
 \r
-  //\r
-  // The AP reset stack is only used by SEV-ES guests. Do not add to the\r
-  // allocation if SEV-ES is not enabled.\r
-  //\r
-  if (PcdGetBool (PcdSevEsIsEnabled)) {\r
-    //\r
-    // Stack location is based on APIC ID, so use the total number of\r
-    // processors for calculating the total stack area.\r
-    //\r
-    Size += AP_RESET_STACK_SIZE * PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
-\r
-    Size = ALIGN_VALUE (Size, CPU_STACK_ALIGNMENT);\r
-  }\r
-\r
   return Size;\r
 }\r
 \r
@@ -1192,6 +1178,7 @@ AllocateResetVector (
   )\r
 {\r
   UINTN           ApResetVectorSize;\r
+  UINTN           ApResetStackSize;\r
 \r
   if (CpuMpData->WakeupBuffer == (UINTN) -1) {\r
     ApResetVectorSize = GetApResetVectorSize (&CpuMpData->AddressMap);\r
@@ -1207,9 +1194,39 @@ AllocateResetVector (
                                     CpuMpData->AddressMap.ModeTransitionOffset\r
                                     );\r
     //\r
-    // The reset stack starts at the end of the buffer.\r
+    // The AP reset stack is only used by SEV-ES guests. Do not allocate it\r
+    // if SEV-ES is not enabled.\r
     //\r
-    CpuMpData->SevEsAPResetStackStart = CpuMpData->WakeupBuffer + ApResetVectorSize;\r
+    if (PcdGetBool (PcdSevEsIsEnabled)) {\r
+      //\r
+      // Stack location is based on ProcessorNumber, so use the total number\r
+      // of processors for calculating the total stack area.\r
+      //\r
+      ApResetStackSize = (AP_RESET_STACK_SIZE *\r
+                          PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
+\r
+      //\r
+      // Invoke GetWakeupBuffer a second time to allocate the stack area\r
+      // below 1MB. The returned buffer will be page aligned and sized and\r
+      // below the previously allocated buffer.\r
+      //\r
+      CpuMpData->SevEsAPResetStackStart = GetWakeupBuffer (ApResetStackSize);\r
+\r
+      //\r
+      // Check to be sure that the "allocate below" behavior hasn't changed.\r
+      // This will also catch a failed allocation, as "-1" is returned on\r
+      // failure.\r
+      //\r
+      if (CpuMpData->SevEsAPResetStackStart >= CpuMpData->WakeupBuffer) {\r
+        DEBUG ((\r
+          DEBUG_ERROR,\r
+          "SEV-ES AP reset stack is not below wakeup buffer\n"\r
+          ));\r
+\r
+        ASSERT (FALSE);\r
+        CpuDeadLoop ();\r
+      }\r
+    }\r
   }\r
   BackupAndPrepareWakeupBuffer (CpuMpData);\r
 }\r
index 3989bd6a7a9fcd6a5ce57475bb5c0e3d33531b9f..90015c650c68127096ee338b37ac8429ac53d482 100644 (file)
@@ -11,6 +11,8 @@
 #include <Guid/S3SmmInitDone.h>\r
 #include <Ppi/ShadowMicrocode.h>\r
 \r
+STATIC UINT64 mSevEsPeiWakeupBuffer = BASE_1MB;\r
+\r
 /**\r
   S3 SMM Init Done notification function.\r
 \r
@@ -220,7 +222,13 @@ GetWakeupBuffer (
         // Need memory under 1MB to be collected here\r
         //\r
         WakeupBufferEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;\r
-        if (WakeupBufferEnd > BASE_1MB) {\r
+        if (PcdGetBool (PcdSevEsIsEnabled) &&\r
+            WakeupBufferEnd > mSevEsPeiWakeupBuffer) {\r
+          //\r
+          // SEV-ES Wakeup buffer should be under 1MB and under any previous one\r
+          //\r
+          WakeupBufferEnd = mSevEsPeiWakeupBuffer;\r
+        } else if (WakeupBufferEnd > BASE_1MB) {\r
           //\r
           // Wakeup buffer should be under 1MB\r
           //\r
@@ -244,6 +252,15 @@ GetWakeupBuffer (
           }\r
           DEBUG ((DEBUG_INFO, "WakeupBufferStart = %x, WakeupBufferSize = %x\n",\r
                                WakeupBufferStart, WakeupBufferSize));\r
+\r
+          if (PcdGetBool (PcdSevEsIsEnabled)) {\r
+            //\r
+            // Next SEV-ES wakeup buffer allocation must be below this\r
+            // allocation\r
+            //\r
+            mSevEsPeiWakeupBuffer = WakeupBufferStart;\r
+          }\r
+\r
           return (UINTN)WakeupBufferStart;\r
         }\r
       }\r