TianoCore EDK2 master
SecVmgExitVcHandler.c
Go to the documentation of this file.
1
9#include <Base.h>
10#include <Uefi.h>
13#include <Library/VmgExitLib.h>
14#include <Register/Amd/Msr.h>
15
16#include "VmgExitVcHandler.h"
17
35EFIAPI
37 IN OUT EFI_EXCEPTION_TYPE *ExceptionType,
38 IN OUT EFI_SYSTEM_CONTEXT SystemContext
39 )
40{
42 GHCB *Ghcb;
43 GHCB *GhcbBackup;
44 EFI_STATUS VcRet;
45 BOOLEAN InterruptState;
46 SEV_ES_PER_CPU_DATA *SevEsData;
47
48 InterruptState = GetInterruptState ();
49 if (InterruptState) {
51 }
52
53 Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
54 ASSERT (Msr.GhcbInfo.Function == 0);
55 ASSERT (Msr.Ghcb != 0);
56
57 Ghcb = Msr.Ghcb;
58 GhcbBackup = NULL;
59
60 SevEsData = (SEV_ES_PER_CPU_DATA *)(Ghcb + 1);
61 SevEsData->VcCount++;
62
63 //
64 // Check for maximum SEC #VC nesting.
65 //
66 if (SevEsData->VcCount > VMGEXIT_MAXIMUM_VC_COUNT) {
67 VmgExitIssueAssert (SevEsData);
68 } else if (SevEsData->VcCount > 1) {
69 UINTN GhcbBackupSize;
70
71 //
72 // Be sure that the proper amount of pages are allocated
73 //
74 GhcbBackupSize = (VMGEXIT_MAXIMUM_VC_COUNT - 1) * sizeof (*Ghcb);
75 if (GhcbBackupSize > FixedPcdGet32 (PcdOvmfSecGhcbBackupSize)) {
76 //
77 // Not enough SEC backup pages allocated.
78 //
79 VmgExitIssueAssert (SevEsData);
80 }
81
82 //
83 // Save the active GHCB to a backup page.
84 // To access the correct backup page, increment the backup page pointer
85 // based on the current VcCount.
86 //
87 GhcbBackup = (GHCB *)FixedPcdGet32 (PcdOvmfSecGhcbBackupBase);
88 GhcbBackup += (SevEsData->VcCount - 2);
89
90 CopyMem (GhcbBackup, Ghcb, sizeof (*Ghcb));
91 }
92
93 VcRet = InternalVmgExitHandleVc (Ghcb, ExceptionType, SystemContext);
94
95 if (GhcbBackup != NULL) {
96 //
97 // Restore the active GHCB from the backup page.
98 //
99 CopyMem (Ghcb, GhcbBackup, sizeof (*Ghcb));
100 }
101
102 SevEsData->VcCount--;
103
104 if (InterruptState) {
106 }
107
108 return VcRet;
109}
UINT64 UINTN
#define NULL
Definition: Base.h:312
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
BOOLEAN EFIAPI GetInterruptState(VOID)
Definition: CpuBreakpoint.c:86
VOID EFIAPI EnableInterrupts(VOID)
Definition: CpuBreakpoint.c:67
VOID EFIAPI DisableInterrupts(VOID)
Definition: CpuBreakpoint.c:54
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define ASSERT(Expression)
Definition: DebugLib.h:391
#define MSR_SEV_ES_GHCB
Definition: Fam17Msr.h:24
UINT64 EFIAPI AsmReadMsr64(IN UINT32 Index)
Definition: GccInlinePriv.c:60
INTN EFI_EXCEPTION_TYPE
Definition: DebugSupport.h:35
#define FixedPcdGet32(TokenName)
Definition: PcdLib.h:92
EFI_STATUS EFIAPI VmgExitHandleVc(IN OUT EFI_EXCEPTION_TYPE *ExceptionType, IN OUT EFI_SYSTEM_CONTEXT SystemContext)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:28
EFI_STATUS EFIAPI InternalVmgExitHandleVc(IN OUT GHCB *Ghcb, IN OUT EFI_EXCEPTION_TYPE *ExceptionType, IN OUT EFI_SYSTEM_CONTEXT SystemContext)
VOID EFIAPI VmgExitIssueAssert(IN OUT SEV_ES_PER_CPU_DATA *SevEsData)