]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/MpInitLib: Prepare SEV-ES guest APs for OS use
authorTom Lendacky <thomas.lendacky@amd.com>
Wed, 12 Aug 2020 20:21:43 +0000 (15:21 -0500)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 17 Aug 2020 02:46:39 +0000 (02:46 +0000)
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2198

Before UEFI transfers control to the OS, it must park the AP. This is
done using the AsmRelocateApLoop function to transition into 32-bit
non-paging mode. For an SEV-ES guest, a few additional things must be
done:
  - AsmRelocateApLoop must be updated to support SEV-ES. This means
    performing a VMGEXIT AP Reset Hold instead of an MWAIT or HLT loop.
  - Since the AP must transition to real mode, a small routine is copied
    to the WakeupBuffer area. Since the WakeupBuffer will be used by
    the AP during OS booting, it must be placed in reserved memory.
    Additionally, the AP stack must be located where it can be accessed
    in real mode.
  - Once the AP is in real mode it will transfer control to the
    destination specified by the OS in the SEV-ES AP Jump Table. The
    SEV-ES AP Jump Table address is saved by the hypervisor for the OS
    using the GHCB VMGEXIT AP Jump Table exit code.

Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Eric Dong <eric.dong@intel.com>
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm
UefiCpuPkg/Library/MpInitLib/MpLib.h
UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm

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
index 309d53bf3b37dc7a548c531cd01b6678e21d0bf0..7e81d24aa60ffda1cb084366ac64be0419423364 100644 (file)
@@ -226,7 +226,10 @@ SwitchToRealProcStart:
 SwitchToRealProcEnd:\r
 \r
 ;-------------------------------------------------------------------------------------\r
-;  AsmRelocateApLoop (MwaitSupport, ApTargetCState, PmCodeSegment, TopOfApStack, CountTofinish);\r
+;  AsmRelocateApLoop (MwaitSupport, ApTargetCState, PmCodeSegment, TopOfApStack, CountTofinish, Pm16CodeSegment, SevEsAPJumpTable, WakeupBuffer);\r
+;\r
+;  The last three parameters (Pm16CodeSegment, SevEsAPJumpTable and WakeupBuffer) are\r
+;  specific to SEV-ES support and are not applicable on IA32.\r
 ;-------------------------------------------------------------------------------------\r
 global ASM_PFX(AsmRelocateApLoop)\r
 ASM_PFX(AsmRelocateApLoop):\r
index b1a9d99cb3eb3d82b8e534672cea5f28b5ae7c20..02652eaae126ecffea8262ed0956383d81915f2e 100644 (file)
@@ -293,7 +293,8 @@ struct _CPU_MP_DATA {
   UINT64                         GhcbBase;\r
 };\r
 \r
-#define AP_RESET_STACK_SIZE 64\r
+#define AP_SAFE_STACK_SIZE  128\r
+#define AP_RESET_STACK_SIZE AP_SAFE_STACK_SIZE\r
 \r
 #pragma pack(1)\r
 \r
@@ -350,7 +351,10 @@ VOID
   IN UINTN                   ApTargetCState,\r
   IN UINTN                   PmCodeSegment,\r
   IN UINTN                   TopOfApStack,\r
-  IN UINTN                   NumberToFinish\r
+  IN UINTN                   NumberToFinish,\r
+  IN UINTN                   Pm16CodeSegment,\r
+  IN UINTN                   SevEsAPJumpTable,\r
+  IN UINTN                   WakeupBuffer\r
   );\r
 \r
 /**\r
index 6956b408d004103e67eba5e41ca3386649182962..5d30f35b201c5520ea881f98974378aa6e9382a6 100644 (file)
@@ -465,6 +465,10 @@ BITS 16
     ;     - IP for Real Mode (two bytes)\r
     ;     - CS for Real Mode (two bytes)\r
     ;\r
+    ; This label is also used with AsmRelocateApLoop. During MP finalization,\r
+    ; the code from PM16Mode to SwitchToRealProcEnd is copied to the start of\r
+    ; the WakeupBuffer, allowing a parked AP to be booted by an OS.\r
+    ;\r
 PM16Mode:\r
     mov        eax, cr0                    ; Read CR0\r
     btr        eax, 0                      ; Set PE=0\r
@@ -487,32 +491,96 @@ PM16Mode:
 SwitchToRealProcEnd:\r
 \r
 ;-------------------------------------------------------------------------------------\r
-;  AsmRelocateApLoop (MwaitSupport, ApTargetCState, PmCodeSegment, TopOfApStack, CountTofinish);\r
+;  AsmRelocateApLoop (MwaitSupport, ApTargetCState, PmCodeSegment, TopOfApStack, CountTofinish, Pm16CodeSegment, SevEsAPJumpTable, WakeupBuffer);\r
 ;-------------------------------------------------------------------------------------\r
 global ASM_PFX(AsmRelocateApLoop)\r
 ASM_PFX(AsmRelocateApLoop):\r
 AsmRelocateApLoopStart:\r
 BITS 64\r
+    cmp        qword [rsp + 56], 0  ; SevEsAPJumpTable\r
+    je         NoSevEs\r
+\r
+    ;\r
+    ; Perform some SEV-ES related setup before leaving 64-bit mode\r
+    ;\r
+    push       rcx\r
+    push       rdx\r
+\r
+    ;\r
+    ; Get the RDX reset value using CPUID\r
+    ;\r
+    mov        rax, 1\r
+    cpuid\r
+    mov        rsi, rax          ; Save off the reset value for RDX\r
+\r
+    ;\r
+    ; Prepare the GHCB for the AP_HLT_LOOP VMGEXIT call\r
+    ;   - Must be done while in 64-bit long mode so that writes to\r
+    ;     the GHCB memory will be unencrypted.\r
+    ;   - No NAE events can be generated once this is set otherwise\r
+    ;     the AP_RESET_HOLD SW_EXITCODE will be overwritten.\r
+    ;\r
+    mov        rcx, 0xc0010130\r
+    rdmsr                        ; Retrieve current GHCB address\r
+    shl        rdx, 32\r
+    or         rdx, rax\r
+\r
+    mov        rdi, rdx\r
+    xor        rax, rax\r
+    mov        rcx, 0x800\r
+    shr        rcx, 3\r
+    rep stosq                    ; Clear the GHCB\r
+\r
+    mov        rax, 0x80000004   ; VMGEXIT AP_RESET_HOLD\r
+    mov        [rdx + 0x390], rax\r
+\r
+    pop        rdx\r
+    pop        rcx\r
+\r
+NoSevEs:\r
     cli                          ; Disable interrupt before switching to 32-bit mode\r
     mov        rax, [rsp + 40]   ; CountTofinish\r
     lock dec   dword [rax]       ; (*CountTofinish)--\r
-    mov        rsp, r9\r
-    push       rcx\r
-    push       rdx\r
 \r
-    lea        rsi, [PmEntry]    ; rsi <- The start address of transition code\r
+    mov        r10, [rsp + 48]   ; Pm16CodeSegment\r
+    mov        rax, [rsp + 56]   ; SevEsAPJumpTable\r
+    mov        rbx, [rsp + 64]   ; WakeupBuffer\r
+    mov        rsp, r9           ; TopOfApStack\r
+\r
+    push       rax               ; Save SevEsAPJumpTable\r
+    push       rbx               ; Save WakeupBuffer\r
+    push       r10               ; Save Pm16CodeSegment\r
+    push       rcx               ; Save MwaitSupport\r
+    push       rdx               ; Save ApTargetCState\r
+\r
+    lea        rax, [PmEntry]    ; rax <- The start address of transition code\r
 \r
     push       r8\r
-    push       rsi\r
-    DB         0x48\r
-    retf\r
+    push       rax\r
+\r
+    ;\r
+    ; Clear R8 - R15, for reset, before going into 32-bit mode\r
+    ;\r
+    xor        r8, r8\r
+    xor        r9, r9\r
+    xor        r10, r10\r
+    xor        r11, r11\r
+    xor        r12, r12\r
+    xor        r13, r13\r
+    xor        r14, r14\r
+    xor        r15, r15\r
+\r
+    ;\r
+    ; Far return into 32-bit mode\r
+    ;\r
+o64 retf\r
+\r
 BITS 32\r
 PmEntry:\r
     mov        eax, cr0\r
     btr        eax, 31           ; Clear CR0.PG\r
     mov        cr0, eax          ; Disable paging and caches\r
 \r
-    mov        ebx, edx          ; Save EntryPoint to rbx, for rdmsr will overwrite rdx\r
     mov        ecx, 0xc0000080\r
     rdmsr\r
     and        ah, ~ 1           ; Clear LME\r
@@ -525,6 +593,8 @@ PmEntry:
     add        esp, 4\r
     pop        ecx,\r
     add        esp, 4\r
+\r
+MwaitCheck:\r
     cmp        cl, 1              ; Check mwait-monitor support\r
     jnz        HltLoop\r
     mov        ebx, edx           ; Save C-State to ebx\r
@@ -538,10 +608,50 @@ MwaitLoop:
     shl        eax, 4\r
     mwait\r
     jmp        MwaitLoop\r
+\r
 HltLoop:\r
+    pop        edx                ; PM16CodeSegment\r
+    add        esp, 4\r
+    pop        ebx                ; WakeupBuffer\r
+    add        esp, 4\r
+    pop        eax                ; SevEsAPJumpTable\r
+    add        esp, 4\r
+    cmp        eax, 0             ; Check for SEV-ES\r
+    je         DoHlt\r
+\r
+    cli\r
+    ;\r
+    ; SEV-ES is enabled, use VMGEXIT (GHCB information already\r
+    ; set by caller)\r
+    ;\r
+BITS 64\r
+    rep        vmmcall\r
+BITS 32\r
+\r
+    ;\r
+    ; Back from VMGEXIT AP_HLT_LOOP\r
+    ;   Push the FLAGS/CS/IP values to use\r
+    ;\r
+    push       word 0x0002        ; EFLAGS\r
+    xor        ecx, ecx\r
+    mov        cx, [eax + 2]      ; CS\r
+    push       cx\r
+    mov        cx, [eax]          ; IP\r
+    push       cx\r
+    push       word 0x0000        ; For alignment, will be discarded\r
+\r
+    push       edx\r
+    push       ebx\r
+\r
+    mov        edx, esi           ; Restore RDX reset value\r
+\r
+    retf\r
+\r
+DoHlt:\r
     cli\r
     hlt\r
-    jmp        HltLoop\r
+    jmp        DoHlt\r
+\r
 BITS 64\r
 AsmRelocateApLoopEnd:\r
 \r