]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/BaseMemEncryptTdxLib: Refactor error handle of SetOrClearSharedBit
authorMin M Xu <min.m.xu@intel.com>
Tue, 17 Jan 2023 23:52:32 +0000 (07:52 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Wed, 18 Jan 2023 05:11:07 +0000 (05:11 +0000)
The previous implementation of SetOrClearSharedBit doesn't handle the
error correctly. In this patch SetOrClearSharedBit is changed to return
error code so that the caller can handle it.

Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Michael Roth <michael.roth@amd.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c

index 503f626d75c64275e0bdfb2f21572ca25cdb03da..5b13042512ad138f92763eef1110333de3806110 100644 (file)
@@ -510,8 +510,11 @@ Split1GPageTo2M (
   @param[in]      PagetablePoint        Page table entry pointer (PTE).\r
   @param[in]      Mode                  Set or Clear shared bit\r
 \r
+  @retval         EFI_SUCCESS           Successfully set or clear the memory shared bit\r
+  @retval         Others                Other error as indicated\r
 **/\r
-STATIC VOID\r
+STATIC\r
+EFI_STATUS\r
 SetOrClearSharedBit (\r
   IN   OUT     UINT64              *PageTablePointer,\r
   IN           TDX_PAGETABLE_MODE  Mode,\r
@@ -520,7 +523,8 @@ SetOrClearSharedBit (
   )\r
 {\r
   UINT64                        AddressEncMask;\r
-  UINT64                        Status;\r
+  UINT64                        TdStatus;\r
+  EFI_STATUS                    Status;\r
   EDKII_MEMORY_ACCEPT_PROTOCOL  *MemoryAcceptProtocol;\r
 \r
   AddressEncMask = GetMemEncryptionAddressMask ();\r
@@ -536,16 +540,30 @@ SetOrClearSharedBit (
     PhysicalAddress   &= ~AddressEncMask;\r
   }\r
 \r
-  Status = TdVmCall (TDVMCALL_MAPGPA, PhysicalAddress, Length, 0, 0, NULL);\r
+  TdStatus = TdVmCall (TDVMCALL_MAPGPA, PhysicalAddress, Length, 0, 0, NULL);\r
+  if (TdStatus != 0) {\r
+    DEBUG ((DEBUG_ERROR, "%a: TdVmcall(MAPGPA) failed with %llx\n", __FUNCTION__, TdStatus));\r
+    ASSERT (FALSE);\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
 \r
   //\r
   // If changing shared to private, must accept-page again\r
   //\r
   if (Mode == ClearSharedBit) {\r
     Status = gBS->LocateProtocol (&gEdkiiMemoryAcceptProtocolGuid, NULL, (VOID **)&MemoryAcceptProtocol);\r
-    ASSERT (!EFI_ERROR (Status));\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR, "%a: Failed to locate MemoryAcceptProtocol with %r\n", __FUNCTION__, Status));\r
+      ASSERT (FALSE);\r
+      return Status;\r
+    }\r
+\r
     Status = MemoryAcceptProtocol->AcceptMemory (MemoryAcceptProtocol, PhysicalAddress, Length);\r
-    ASSERT (!EFI_ERROR (Status));\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR, "%a: Failed to AcceptMemory with %r\n", __FUNCTION__, Status));\r
+      ASSERT (FALSE);\r
+      return Status;\r
+    }\r
   }\r
 \r
   DEBUG ((\r
@@ -558,6 +576,8 @@ SetOrClearSharedBit (
     Mode,\r
     Status\r
     ));\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -747,7 +767,11 @@ SetMemorySharedOrPrivate (
       // If we have at least 1GB to go, we can just update this entry\r
       //\r
       if (!(PhysicalAddress & (BIT30 - 1)) && (Length >= BIT30)) {\r
-        SetOrClearSharedBit (&PageDirectory1GEntry->Uint64, Mode, PhysicalAddress, BIT30);\r
+        Status = SetOrClearSharedBit (&PageDirectory1GEntry->Uint64, Mode, PhysicalAddress, BIT30);\r
+        if (EFI_ERROR (Status)) {\r
+          goto Done;\r
+        }\r
+\r
         DEBUG ((\r
           DEBUG_VERBOSE,\r
           "%a:%a: updated 1GB entry for Physical=0x%Lx\n",\r
@@ -809,7 +833,11 @@ SetMemorySharedOrPrivate (
         // If we have at least 2MB left to go, we can just update this entry\r
         //\r
         if (!(PhysicalAddress & (BIT21-1)) && (Length >= BIT21)) {\r
-          SetOrClearSharedBit (&PageDirectory2MEntry->Uint64, Mode, PhysicalAddress, BIT21);\r
+          Status = SetOrClearSharedBit (&PageDirectory2MEntry->Uint64, Mode, PhysicalAddress, BIT21);\r
+          if (EFI_ERROR (Status)) {\r
+            goto Done;\r
+          }\r
+\r
           PhysicalAddress += BIT21;\r
           Length          -= BIT21;\r
         } else {\r
@@ -856,7 +884,11 @@ SetMemorySharedOrPrivate (
           goto Done;\r
         }\r
 \r
-        SetOrClearSharedBit (&PageTableEntry->Uint64, Mode, PhysicalAddress, EFI_PAGE_SIZE);\r
+        Status = SetOrClearSharedBit (&PageTableEntry->Uint64, Mode, PhysicalAddress, EFI_PAGE_SIZE);\r
+        if (EFI_ERROR (Status)) {\r
+          goto Done;\r
+        }\r
+\r
         PhysicalAddress += EFI_PAGE_SIZE;\r
         Length          -= EFI_PAGE_SIZE;\r
       }\r