]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/VmgExitLib/PeiDxeVmgExitVcHandler.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / Library / VmgExitLib / PeiDxeVmgExitVcHandler.c
1 /** @file
2 X64 #VC Exception Handler functon.
3
4 Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Base.h>
10 #include <Uefi.h>
11 #include <Library/BaseMemoryLib.h>
12 #include <Library/MemEncryptSevLib.h>
13 #include <Library/VmgExitLib.h>
14 #include <Register/Amd/Msr.h>
15
16 #include "VmgExitVcHandler.h"
17
18 /**
19 Handle a #VC exception.
20
21 Performs the necessary processing to handle a #VC exception.
22
23 @param[in, out] ExceptionType Pointer to an EFI_EXCEPTION_TYPE to be set
24 as value to use on error.
25 @param[in, out] SystemContext Pointer to EFI_SYSTEM_CONTEXT
26
27 @retval EFI_SUCCESS Exception handled
28 @retval EFI_UNSUPPORTED #VC not supported, (new) exception value to
29 propagate provided
30 @retval EFI_PROTOCOL_ERROR #VC handling failed, (new) exception value to
31 propagate provided
32
33 **/
34 EFI_STATUS
35 EFIAPI
36 VmgExitHandleVc (
37 IN OUT EFI_EXCEPTION_TYPE *ExceptionType,
38 IN OUT EFI_SYSTEM_CONTEXT SystemContext
39 )
40 {
41 MSR_SEV_ES_GHCB_REGISTER Msr;
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) {
50 DisableInterrupts ();
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 PEI/DXE #VC nesting.
65 //
66 if (SevEsData->VcCount > VMGEXIT_MAXIMUM_VC_COUNT) {
67 VmgExitIssueAssert (SevEsData);
68 } else if (SevEsData->VcCount > 1) {
69 //
70 // Nested #VC
71 //
72 if (SevEsData->GhcbBackupPages == NULL) {
73 VmgExitIssueAssert (SevEsData);
74 }
75
76 //
77 // Save the active GHCB to a backup page.
78 // To access the correct backup page, increment the backup page pointer
79 // based on the current VcCount.
80 //
81 GhcbBackup = (GHCB *)SevEsData->GhcbBackupPages;
82 GhcbBackup += (SevEsData->VcCount - 2);
83
84 CopyMem (GhcbBackup, Ghcb, sizeof (*Ghcb));
85 }
86
87 VcRet = InternalVmgExitHandleVc (Ghcb, ExceptionType, SystemContext);
88
89 if (GhcbBackup != NULL) {
90 //
91 // Restore the active GHCB from the backup page.
92 //
93 CopyMem (Ghcb, GhcbBackup, sizeof (*Ghcb));
94 }
95
96 SevEsData->VcCount--;
97
98 if (InterruptState) {
99 EnableInterrupts ();
100 }
101
102 return VcRet;
103 }