]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/CpuExceptionHandler: Add base support for the #VC exception
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>
Mon, 17 Aug 2020 02:46:39 +0000 (02:46 +0000)
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2198

Add base support to handle #VC exceptions. Update the common exception
handlers to invoke the VmgExitHandleVc () function of the VmgExitLib
library when a #VC is encountered. A non-zero return code will propagate
to the targeted exception handler.

Under SEV-ES, a DR7 read or write intercept generates a #VC exception.
To avoid exception recursion, a #VC exception will not try to read and
push the actual debug registers into the EFI_SYSTEM_CONTEXT_X64 struct
and instead push zeroes. The #VC exception handler does not make use of
the debug registers from the saved context and the exception processing
exit code does not attempt to restore the debug register values.

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/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c
UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf
UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandlerLib.inf
UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiDxeSmmCpuException.c
UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLib.inf
UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.inf
UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/Xcode5ExceptionHandlerAsm.nasm
UefiCpuPkg/Library/CpuExceptionHandlerLib/Xcode5SecPeiCpuExceptionHandlerLib.inf

index 8adbd43fefb42581846156084a5c569a353bbbc6..c9003b10e5526800e74f1ea0b3a22c083f62ef24 100644 (file)
@@ -14,7 +14,7 @@
 //\r
 // 1 means an error code will be pushed, otherwise 0\r
 //\r
-CONST UINT32 mErrorCodeFlag = 0x00227d00;\r
+CONST UINT32 mErrorCodeFlag = 0x20227d00;\r
 \r
 //\r
 // Define the maximum message length\r
@@ -45,6 +45,14 @@ CONST CHAR8 *mExceptionNameStr[] = {
   "#XM - SIMD floating-point",\r
   "#VE - Virtualization",\r
   "#CP - Control Protection"\r
+  "Reserved",\r
+  "Reserved",\r
+  "Reserved",\r
+  "Reserved",\r
+  "Reserved",\r
+  "Reserved",\r
+  "Reserved",\r
+  "#VC - VMM Communication",\r
 };\r
 \r
 #define EXCEPTION_KNOWN_NAME_NUM  (sizeof (mExceptionNameStr) / sizeof (CHAR8 *))\r
index 61e2ec30b0891bf9f03b5cade83c2421e48f380f..07b34c92a8922c5b6bb5e3b64c1c6f7e81fe4ea9 100644 (file)
@@ -57,3 +57,4 @@
   PeCoffGetEntryPointLib\r
   MemoryAllocationLib\r
   DebugLib\r
+  VmgExitLib\r
index 093374944df685d26d2a8104ca5dc9e86452066b..feae7b3e06de4b393d2c5c37282761d41d899d54 100644 (file)
@@ -52,6 +52,7 @@
   HobLib\r
   MemoryAllocationLib\r
   SynchronizationLib\r
+  VmgExitLib\r
 \r
 [Pcd]\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard    # CONSUMES\r
index 6a2670d5591889c49fb4916110232f2374041c0f..892d349d4b37823125ce0524334aba0134a61b27 100644 (file)
@@ -6,8 +6,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 \r
 **/\r
 \r
-#include "CpuExceptionCommon.h"\r
 #include <Library/DebugLib.h>\r
+#include <Library/VmgExitLib.h>\r
+#include "CpuExceptionCommon.h"\r
 \r
 /**\r
   Internal worker function for common exception handler.\r
@@ -27,6 +28,23 @@ CommonExceptionHandlerWorker (
   RESERVED_VECTORS_DATA          *ReservedVectors;\r
   EFI_CPU_INTERRUPT_HANDLER      *ExternalInterruptHandler;\r
 \r
+  if (ExceptionType == VC_EXCEPTION) {\r
+    EFI_STATUS  Status;\r
+    //\r
+    // #VC needs to be handled immediately upon enabling exception handling\r
+    // and therefore can't use the RegisterCpuInterruptHandler() interface.\r
+    //\r
+    // Handle the #VC:\r
+    //   On EFI_SUCCESS - Exception has been handled, return\r
+    //   On other       - ExceptionType contains (possibly new) exception\r
+    //                    value\r
+    //\r
+    Status = VmgExitHandleVc (&ExceptionType, SystemContext);\r
+    if (!EFI_ERROR (Status)) {\r
+      return;\r
+    }\r
+  }\r
+\r
   ExceptionHandlerContext  = (EXCEPTION_HANDLER_CONTEXT *) (UINTN) (SystemContext.SystemContextIa32);\r
   ReservedVectors          = ExceptionHandlerData->ReservedVectors;\r
   ExternalInterruptHandler = ExceptionHandlerData->ExternalInterruptHandler;\r
index d4ae153c574205883882449919561346ed11a01a..01b5a2f1f4fcff4f7f93376d917f6b493044b780 100644 (file)
@@ -7,6 +7,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 **/\r
 \r
 #include <PiPei.h>\r
+#include <Library/VmgExitLib.h>\r
 #include "CpuExceptionCommon.h"\r
 \r
 CONST UINTN    mDoFarReturnFlag  = 0;\r
@@ -24,6 +25,24 @@ CommonExceptionHandler (
   IN EFI_SYSTEM_CONTEXT   SystemContext\r
   )\r
 {\r
+  if (ExceptionType == VC_EXCEPTION) {\r
+    EFI_STATUS  Status;\r
+    //\r
+    // #VC needs to be handled immediately upon enabling exception handling\r
+    // and therefore can't use the RegisterCpuInterruptHandler() interface\r
+    // (which isn't supported under Sec and Pei anyway).\r
+    //\r
+    // Handle the #VC:\r
+    //   On EFI_SUCCESS - Exception has been handled, return\r
+    //   On other       - ExceptionType contains (possibly new) exception\r
+    //                    value\r
+    //\r
+    Status = VmgExitHandleVc (&ExceptionType, SystemContext);\r
+    if (!EFI_ERROR (Status)) {\r
+      return;\r
+    }\r
+  }\r
+\r
   //\r
   // Initialize the serial port before dumping.\r
   //\r
index 6d25cafe2ca38ff8465d44d4c51006798addea47..967cb61ba6d9bb3df189756d851d22b1b8a8c8fb 100644 (file)
@@ -48,3 +48,4 @@
   PrintLib\r
   LocalApicLib\r
   PeCoffGetEntryPointLib\r
+  VmgExitLib\r
index 2ffbbccc302f01d41f41582cee11b78a8005850b..4cdb11c04ea0f88e4e48c17520bccfd3532e6c8e 100644 (file)
@@ -51,4 +51,5 @@
   LocalApicLib\r
   PeCoffGetEntryPointLib\r
   DebugLib\r
+  VmgExitLib\r
 \r
index 3814f9de3703c46e33ce696df99e862acf7f45e6..2a5545ecfd41ee9e97893a67ec1c6671978c20ab 100644 (file)
@@ -18,6 +18,8 @@
 ; CommonExceptionHandler()\r
 ;\r
 \r
+%define VC_EXCEPTION 29\r
+\r
 extern ASM_PFX(mErrorCodeFlag)    ; Error code flags for exceptions\r
 extern ASM_PFX(mDoFarReturnFlag)  ; Do far return flag\r
 extern ASM_PFX(CommonExceptionHandler)\r
@@ -224,6 +226,9 @@ HasErrorCode:
     push    rax\r
 \r
 ;; UINT64  Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
+    cmp     qword [rbp + 8], VC_EXCEPTION\r
+    je      VcDebugRegs          ; For SEV-ES (#VC) Debug registers ignored\r
+\r
     mov     rax, dr7\r
     push    rax\r
     mov     rax, dr6\r
@@ -236,7 +241,19 @@ HasErrorCode:
     push    rax\r
     mov     rax, dr0\r
     push    rax\r
+    jmp     DrFinish\r
+\r
+VcDebugRegs:\r
+;; UINT64  Dr0, Dr1, Dr2, Dr3, Dr6, Dr7 are skipped for #VC to avoid exception recursion\r
+    xor     rax, rax\r
+    push    rax\r
+    push    rax\r
+    push    rax\r
+    push    rax\r
+    push    rax\r
+    push    rax\r
 \r
+DrFinish:\r
 ;; FX_SAVE_STATE_X64 FxSaveState;\r
     sub rsp, 512\r
     mov rdi, rsp\r
index 19198f273137ead558224ece1a8fc15ff03c9451..26cae56cc5cf6b7608ce75197713a914a81a1780 100644 (file)
@@ -18,6 +18,8 @@
 ; CommonExceptionHandler()\r
 ;\r
 \r
+%define VC_EXCEPTION 29\r
+\r
 extern ASM_PFX(mErrorCodeFlag)    ; Error code flags for exceptions\r
 extern ASM_PFX(mDoFarReturnFlag)  ; Do far return flag\r
 extern ASM_PFX(CommonExceptionHandler)\r
@@ -225,6 +227,9 @@ HasErrorCode:
     push    rax\r
 \r
 ;; UINT64  Dr0, Dr1, Dr2, Dr3, Dr6, Dr7;\r
+    cmp     qword [rbp + 8], VC_EXCEPTION\r
+    je      VcDebugRegs          ; For SEV-ES (#VC) Debug registers ignored\r
+\r
     mov     rax, dr7\r
     push    rax\r
     mov     rax, dr6\r
@@ -237,7 +242,19 @@ HasErrorCode:
     push    rax\r
     mov     rax, dr0\r
     push    rax\r
+    jmp     DrFinish\r
+\r
+VcDebugRegs:\r
+;; UINT64  Dr0, Dr1, Dr2, Dr3, Dr6, Dr7 are skipped for #VC to avoid exception recursion\r
+    xor     rax, rax\r
+    push    rax\r
+    push    rax\r
+    push    rax\r
+    push    rax\r
+    push    rax\r
+    push    rax\r
 \r
+DrFinish:\r
 ;; FX_SAVE_STATE_X64 FxSaveState;\r
     sub rsp, 512\r
     mov rdi, rsp\r
index 7e21beaab6f2690fa6d803fd90f040284f0ea6d1..743c2aa76684f0e46163839c6338d76d18610f63 100644 (file)
@@ -53,3 +53,4 @@
   PrintLib\r
   LocalApicLib\r
   PeCoffGetEntryPointLib\r
+  VmgExitLib\r