+/** @file\r
+ VMGEXIT Base Support Library.\r
+\r
+ Copyright (C) 2020, Advanced Micro Devices, Inc. All rights reserved.<BR>\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <Base.h>\r
+#include <Uefi.h>\r
+#include <Library/VmgExitLib.h>\r
+\r
+/**\r
+ Perform VMGEXIT.\r
+\r
+ Sets the necessary fields of the GHCB, invokes the VMGEXIT instruction and\r
+ then handles the return actions.\r
+\r
+ The base library function returns an error in the form of a\r
+ GHCB_EVENT_INJECTION representing a GP_EXCEPTION.\r
+\r
+ @param[in, out] Ghcb A pointer to the GHCB\r
+ @param[in] ExitCode VMGEXIT code to be assigned to the SwExitCode\r
+ field of the GHCB.\r
+ @param[in] ExitInfo1 VMGEXIT information to be assigned to the\r
+ SwExitInfo1 field of the GHCB.\r
+ @param[in] ExitInfo2 VMGEXIT information to be assigned to the\r
+ SwExitInfo2 field of the GHCB.\r
+\r
+ @retval 0 VMGEXIT succeeded.\r
+ @return Exception number to be propagated, VMGEXIT\r
+ processing did not succeed.\r
+\r
+**/\r
+UINT64\r
+EFIAPI\r
+VmgExit (\r
+ IN OUT GHCB *Ghcb,\r
+ IN UINT64 ExitCode,\r
+ IN UINT64 ExitInfo1,\r
+ IN UINT64 ExitInfo2\r
+ )\r
+{\r
+ GHCB_EVENT_INJECTION Event;\r
+\r
+ Event.Uint64 = 0;\r
+ Event.Elements.Vector = GP_EXCEPTION;\r
+ Event.Elements.Type = GHCB_EVENT_INJECTION_TYPE_EXCEPTION;\r
+ Event.Elements.Valid = 1;\r
+\r
+ return Event.Uint64;\r
+}\r
+\r
+/**\r
+ Perform pre-VMGEXIT initialization/preparation.\r
+\r
+ Performs the necessary steps in preparation for invoking VMGEXIT. Must be\r
+ called before setting any fields within the GHCB.\r
+\r
+ The base library function does nothing.\r
+\r
+ @param[in, out] Ghcb A pointer to the GHCB\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+VmgInit (\r
+ IN OUT GHCB *Ghcb\r
+ )\r
+{\r
+}\r
+\r
+/**\r
+ Perform post-VMGEXIT cleanup.\r
+\r
+ Performs the necessary steps to cleanup after invoking VMGEXIT. Must be\r
+ called after obtaining needed fields within the GHCB.\r
+\r
+ The base library function does nothing.\r
+\r
+ @param[in, out] Ghcb A pointer to the GHCB\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+VmgDone (\r
+ IN OUT GHCB *Ghcb\r
+ )\r
+{\r
+}\r
+\r
+/**\r
+ Handle a #VC exception.\r
+\r
+ Performs the necessary processing to handle a #VC exception.\r
+\r
+ The base library function returns an error equal to VC_EXCEPTION,\r
+ to be propagated to the standard exception handling stack.\r
+\r
+ @param[in, out] ExceptionType Pointer to an EFI_EXCEPTION_TYPE to be set\r
+ as value to use on error.\r
+ @param[in, out] SystemContext Pointer to EFI_SYSTEM_CONTEXT\r
+\r
+ @retval EFI_SUCCESS Exception handled\r
+ @retval EFI_UNSUPPORTED #VC not supported, (new) exception value to\r
+ propagate provided\r
+ @retval EFI_PROTOCOL_ERROR #VC handling failed, (new) exception value to\r
+ propagate provided\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VmgExitHandleVc (\r
+ IN OUT EFI_EXCEPTION_TYPE *ExceptionType,\r
+ IN OUT EFI_SYSTEM_CONTEXT SystemContext\r
+ )\r
+{\r
+ *ExceptionType = VC_EXCEPTION;\r
+\r
+ return EFI_UNSUPPORTED;\r
+}\r