]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/MpInitLib: Prepare SEV-ES guest APs for OS use
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / DxeMpLib.c
index 9115ff9e3e30c6645e0e0a48e2e38a2f43e7b012..2c00d72ddefe1487b72189dd9d16d6a20cd4bd2b 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
@@ -85,6 +86,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
@@ -97,7 +105,7 @@ GetWakeupBuffer (
   StartAddress = 0x88000;\r
   Status = gBS->AllocatePages (\r
                   AllocateMaxAddress,\r
-                  EfiBootServicesData,\r
+                  MemoryType,\r
                   EFI_SIZE_TO_PAGES (WakeupBufferSize),\r
                   &StartAddress\r
                   );\r
@@ -159,8 +167,10 @@ 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
 \r
   //\r
   // Allocate 1 page for AP jump table page\r
@@ -176,6 +186,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);\r
+  VmgExit (Ghcb, SVM_EXIT_AP_JUMP_TABLE, 0, (UINT64) (UINTN) StartAddress);\r
+  VmgDone (Ghcb);\r
+\r
   return (UINTN) StartAddress;\r
 }\r
 \r
@@ -330,17 +350,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 +403,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