]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg: Implement library support for VMGEXIT
authorTom Lendacky <thomas.lendacky@amd.com>
Wed, 12 Aug 2020 20:21:36 +0000 (15:21 -0500)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sun, 16 Aug 2020 16:45:42 +0000 (16:45 +0000)
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2198

To support handling #VC exceptions and issuing VMGEXIT instructions,
create a library with functions that can be used to perform these
#VC/VMGEXIT related operations. This includes functions for:
  - Handling #VC exceptions
  - Preparing for and issuing a VMGEXIT
  - Performing MMIO-related write operations to support flash emulation
  - Performing AP related boot opeations

The base functions in this driver will not do anything and will return
an error if a return value is required. It is expected that other packages
(like OvmfPkg) will create a version of the library to fully support an
SEV-ES guest.

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/Include/Library/VmgExitLib.h [new file with mode: 0644]
UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.c [new file with mode: 0644]
UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf [new file with mode: 0644]
UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.uni [new file with mode: 0644]
UefiCpuPkg/UefiCpuPkg.dec
UefiCpuPkg/UefiCpuPkg.dsc

diff --git a/UefiCpuPkg/Include/Library/VmgExitLib.h b/UefiCpuPkg/Include/Library/VmgExitLib.h
new file mode 100644 (file)
index 0000000..45fc27d
--- /dev/null
@@ -0,0 +1,103 @@
+/** @file\r
+  Public header file for the VMGEXIT Support library class.\r
+\r
+  This library class defines some routines used when invoking the VMGEXIT\r
+  instruction in support of SEV-ES and to handle #VC exceptions.\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
+#ifndef __VMG_EXIT_LIB_H__\r
+#define __VMG_EXIT_LIB_H__\r
+\r
+#include <Protocol/DebugSupport.h>\r
+#include <Register/Amd/Ghcb.h>\r
+\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
+  @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
+/**\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
+  @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
+  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
+  @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
+  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
+#endif\r
diff --git a/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.c b/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.c
new file mode 100644 (file)
index 0000000..bb265e1
--- /dev/null
@@ -0,0 +1,121 @@
+/** @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
diff --git a/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf b/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf
new file mode 100644 (file)
index 0000000..d8770a2
--- /dev/null
@@ -0,0 +1,27 @@
+## @file\r
+#  VMGEXIT 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
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = VmgExitLibNull\r
+  MODULE_UNI_FILE                = VmgExitLibNull.uni\r
+  FILE_GUID                      = 3cd7368f-ef9b-4a9b-9571-2ed93813677e\r
+  MODULE_TYPE                    = BASE\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = VmgExitLib\r
+\r
+[Sources.common]\r
+  VmgExitLibNull.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  UefiCpuPkg/UefiCpuPkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+\r
diff --git a/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.uni b/UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.uni
new file mode 100644 (file)
index 0000000..8639bc0
--- /dev/null
@@ -0,0 +1,15 @@
+// /** @file\r
+// VMGEXIT support library instance.\r
+//\r
+// VMGEXIT support library instance.\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
+\r
+#string STR_MODULE_ABSTRACT             #language en-US "VMGEXIT support NULL library instance"\r
+\r
+#string STR_MODULE_DESCRIPTION          #language en-US "VMGEXIT support NULL library instance."\r
+\r
index d52912ddca1f28291d405c087ab093813ed06bc1..17228cb5a84fd49d5d22c5fc7b99de14a3e4fb0d 100644 (file)
@@ -53,6 +53,9 @@
   ##\r
   MpInitLib|Include/Library/MpInitLib.h\r
 \r
+  ##  @libraryclass  Provides function to support VMGEXIT processing.\r
+  VmgExitLib|Include/Library/VmgExitLib.h\r
+\r
 [Guids]\r
   gUefiCpuPkgTokenSpaceGuid      = { 0xac05bf33, 0x995a, 0x4ed4, { 0xaa, 0xb8, 0xef, 0x7a, 0xe8, 0xf, 0x5c, 0xb0 }}\r
   gMsegSmramGuid                 = { 0x5802bce4, 0xeeee, 0x4e33, { 0xa1, 0x30, 0xeb, 0xad, 0x27, 0xf0, 0xe4, 0x39 }}\r
index 964720048dd75876a68c70354124af89fe442460..b2b6d78a71b021b52520beccfa1099d3dbb04984 100644 (file)
@@ -56,6 +56,7 @@
   PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/BasePeCoffGetEntryPointLib.inf\r
   PeCoffExtraActionLib|MdePkg/Library/BasePeCoffExtraActionLibNull/BasePeCoffExtraActionLibNull.inf\r
   TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf\r
+  VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf\r
 \r
 [LibraryClasses.common.SEC]\r
   PlatformSecLib|UefiCpuPkg/Library/PlatformSecLibNull/PlatformSecLibNull.inf\r
   UefiCpuPkg/Library/SmmCpuPlatformHookLibNull/SmmCpuPlatformHookLibNull.inf\r
   UefiCpuPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf\r
   UefiCpuPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLibStm.inf\r
+  UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf\r
   UefiCpuPkg/PiSmmCommunication/PiSmmCommunicationPei.inf\r
   UefiCpuPkg/PiSmmCommunication/PiSmmCommunicationSmm.inf\r
   UefiCpuPkg/SecCore/SecCore.inf\r