2 VMGEXIT Support Library.
4 Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/BaseMemoryLib.h>
12 #include <Library/VmgExitLib.h>
13 #include <Register/Amd/Msr.h>
16 Check for VMGEXIT error
18 Check if the hypervisor has returned an error after completion of the VMGEXIT
19 by examining the SwExitInfo1 field of the GHCB.
21 @param[in] Ghcb A pointer to the GHCB
23 @retval 0 VMGEXIT succeeded.
24 @return Exception number to be propagated, VMGEXIT processing
34 GHCB_EVENT_INJECTION Event
;
35 GHCB_EXIT_INFO ExitInfo
;
38 ExitInfo
.Uint64
= Ghcb
->SaveArea
.SwExitInfo1
;
39 ASSERT ((ExitInfo
.Elements
.Lower32Bits
== 0) ||
40 (ExitInfo
.Elements
.Lower32Bits
== 1));
43 if (ExitInfo
.Elements
.Lower32Bits
== 0) {
47 if (ExitInfo
.Elements
.Lower32Bits
== 1) {
48 ASSERT (Ghcb
->SaveArea
.SwExitInfo2
!= 0);
51 // Check that the return event is valid
53 Event
.Uint64
= Ghcb
->SaveArea
.SwExitInfo2
;
54 if (Event
.Elements
.Valid
&&
55 Event
.Elements
.Type
== GHCB_EVENT_INJECTION_TYPE_EXCEPTION
) {
56 switch (Event
.Elements
.Vector
) {
60 // Use returned event as return code
62 Status
= Event
.Uint64
;
68 GHCB_EVENT_INJECTION GpEvent
;
71 GpEvent
.Elements
.Vector
= GP_EXCEPTION
;
72 GpEvent
.Elements
.Type
= GHCB_EVENT_INJECTION_TYPE_EXCEPTION
;
73 GpEvent
.Elements
.Valid
= 1;
75 Status
= GpEvent
.Uint64
;
84 Sets the necessary fields of the GHCB, invokes the VMGEXIT instruction and
85 then handles the return actions.
87 @param[in, out] Ghcb A pointer to the GHCB
88 @param[in] ExitCode VMGEXIT code to be assigned to the SwExitCode
90 @param[in] ExitInfo1 VMGEXIT information to be assigned to the
91 SwExitInfo1 field of the GHCB.
92 @param[in] ExitInfo2 VMGEXIT information to be assigned to the
93 SwExitInfo2 field of the GHCB.
95 @retval 0 VMGEXIT succeeded.
96 @return Exception number to be propagated, VMGEXIT
97 processing did not succeed.
109 Ghcb
->SaveArea
.SwExitCode
= ExitCode
;
110 Ghcb
->SaveArea
.SwExitInfo1
= ExitInfo1
;
111 Ghcb
->SaveArea
.SwExitInfo2
= ExitInfo2
;
114 // Guest memory is used for the guest-hypervisor communication, so fence
115 // the invocation of the VMGEXIT instruction to ensure GHCB accesses are
116 // synchronized properly.
122 return VmgExitErrorCheck (Ghcb
);
126 Perform pre-VMGEXIT initialization/preparation.
128 Performs the necessary steps in preparation for invoking VMGEXIT. Must be
129 called before setting any fields within the GHCB.
131 @param[in, out] Ghcb A pointer to the GHCB
140 SetMem (&Ghcb
->SaveArea
, sizeof (Ghcb
->SaveArea
), 0);
144 Perform post-VMGEXIT cleanup.
146 Performs the necessary steps to cleanup after invoking VMGEXIT. Must be
147 called after obtaining needed fields within the GHCB.
149 @param[in, out] Ghcb A pointer to the GHCB
161 Marks a field at the specified offset as valid in the GHCB.
163 The ValidBitmap area represents the areas of the GHCB that have been marked
164 valid. Set the bit in ValidBitmap for the input offset.
166 @param[in, out] Ghcb Pointer to the Guest-Hypervisor Communication Block
167 @param[in] Offset Qword offset in the GHCB to mark valid
174 IN GHCB_REGISTER Offset
180 OffsetIndex
= Offset
/ 8;
181 OffsetBit
= Offset
% 8;
183 Ghcb
->SaveArea
.ValidBitmap
[OffsetIndex
] |= (1 << OffsetBit
);
187 Checks if a specified offset is valid in the GHCB.
189 The ValidBitmap area represents the areas of the GHCB that have been marked
190 valid. Return whether the bit in the ValidBitmap is set for the input offset.
192 @param[in] Ghcb A pointer to the GHCB
193 @param[in] Offset Qword offset in the GHCB to mark valid
195 @retval TRUE Offset is marked valid in the GHCB
196 @retval FALSE Offset is not marked valid in the GHCB
203 IN GHCB_REGISTER Offset
209 OffsetIndex
= Offset
/ 8;
210 OffsetBit
= Offset
% 8;
212 return ((Ghcb
->SaveArea
.ValidBitmap
[OffsetIndex
] & (1 << OffsetBit
)) != 0);