]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: Allocate a separate SEV-ES AP reset stack area
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / DxeMpLib.c
index 9115ff9e3e30c6645e0e0a48e2e38a2f43e7b012..93fc63bf93e3070d5e3ddffae14158d72aacc74c 100644 (file)
@@ -12,6 +12,7 @@
 #include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/DebugAgentLib.h>\r
 #include <Library/DxeServicesTableLib.h>\r
+#include <Library/VmgExitLib.h>\r
 #include <Register/Amd/Fam17Msr.h>\r
 #include <Register/Amd/Ghcb.h>\r
 \r
@@ -28,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
@@ -85,6 +91,13 @@ GetWakeupBuffer (
 {\r
   EFI_STATUS              Status;\r
   EFI_PHYSICAL_ADDRESS    StartAddress;\r
+  EFI_MEMORY_TYPE         MemoryType;\r
+\r
+  if (PcdGetBool (PcdSevEsIsEnabled)) {\r
+    MemoryType = EfiReservedMemoryType;\r
+  } else {\r
+    MemoryType = EfiBootServicesData;\r
+  }\r
 \r
   //\r
   // Try to allocate buffer below 1M for waking vector.\r
@@ -94,16 +107,28 @@ 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
-                  EfiBootServicesData,\r
+                  MemoryType,\r
                   EFI_SIZE_TO_PAGES (WakeupBufferSize),\r
                   &StartAddress\r
                   );\r
   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
@@ -159,8 +184,11 @@ GetSevEsAPMemory (
   VOID\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  EFI_PHYSICAL_ADDRESS  StartAddress;\r
+  EFI_STATUS                Status;\r
+  EFI_PHYSICAL_ADDRESS      StartAddress;\r
+  MSR_SEV_ES_GHCB_REGISTER  Msr;\r
+  GHCB                      *Ghcb;\r
+  BOOLEAN                   InterruptState;\r
 \r
   //\r
   // Allocate 1 page for AP jump table page\r
@@ -176,6 +204,16 @@ GetSevEsAPMemory (
 \r
   DEBUG ((DEBUG_INFO, "Dxe: SevEsAPMemory = %lx\n", (UINTN) StartAddress));\r
 \r
+  //\r
+  // Save the SevEsAPMemory as the AP jump table.\r
+  //\r
+  Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);\r
+  Ghcb = Msr.Ghcb;\r
+\r
+  VmgInit (Ghcb, &InterruptState);\r
+  VmgExit (Ghcb, SVM_EXIT_AP_JUMP_TABLE, 0, (UINT64) (UINTN) StartAddress);\r
+  VmgDone (Ghcb, InterruptState);\r
+\r
   return (UINTN) StartAddress;\r
 }\r
 \r
@@ -330,17 +368,26 @@ RelocateApLoop (
   BOOLEAN                MwaitSupport;\r
   ASM_RELOCATE_AP_LOOP   AsmRelocateApLoopFunc;\r
   UINTN                  ProcessorNumber;\r
+  UINTN                  StackStart;\r
 \r
   MpInitLibWhoAmI (&ProcessorNumber);\r
   CpuMpData    = GetCpuMpData ();\r
   MwaitSupport = IsMwaitSupport ();\r
+  if (CpuMpData->SevEsIsEnabled) {\r
+    StackStart = CpuMpData->SevEsAPResetStackStart;\r
+  } else {\r
+    StackStart = mReservedTopOfApStack;\r
+  }\r
   AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP) (UINTN) mReservedApLoopFunc;\r
   AsmRelocateApLoopFunc (\r
     MwaitSupport,\r
     CpuMpData->ApTargetCState,\r
     CpuMpData->PmCodeSegment,\r
-    mReservedTopOfApStack - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
-    (UINTN) &mNumberToFinish\r
+    StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,\r
+    (UINTN) &mNumberToFinish,\r
+    CpuMpData->Pm16CodeSegment,\r
+    CpuMpData->SevEsAPBuffer,\r
+    CpuMpData->WakeupBuffer\r
     );\r
   //\r
   // It should never reach here\r
@@ -374,6 +421,21 @@ MpInitChangeApLoopCallback (
   while (mNumberToFinish > 0) {\r
     CpuPause ();\r
   }\r
+\r
+  if (CpuMpData->SevEsIsEnabled && (CpuMpData->WakeupBuffer != (UINTN) -1)) {\r
+    //\r
+    // There are APs present. Re-use reserved memory area below 1MB from\r
+    // WakeupBuffer as the area to be used for transitioning to 16-bit mode\r
+    // in support of booting of the AP by an OS.\r
+    //\r
+    CopyMem (\r
+      (VOID *) CpuMpData->WakeupBuffer,\r
+      (VOID *) (CpuMpData->AddressMap.RendezvousFunnelAddress +\r
+                  CpuMpData->AddressMap.SwitchToRealPM16ModeOffset),\r
+      CpuMpData->AddressMap.SwitchToRealPM16ModeSize\r
+      );\r
+  }\r
+\r
   DEBUG ((DEBUG_INFO, "%a() done!\n", __FUNCTION__));\r
 }\r
 \r