Skip to content

Commit 8a41004

Browse files
suncepingmergify[bot]
authored andcommitted
OvmfPkg/BaseMemEncryptTdxLib: Handle retry result of MapGPA
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4572 According to section 3.2 of the [GHCI] document, if the return status of MapGPA is "TDG.VP.VMCALL_RETRY", TD must retry this operation for the pages in the region starting at the GPA specified in R11. In this patch, when a retry state is detected, TDVF needs to retry the mapping with the specified address from the output results of TdVmCall. Reference: [GHCI]: TDX Guest-Host-Communication Interface v1.0 https://cdrdv2.intel.com/v1/dl/getContent/726790 Cc: Erdem Aktas <erdemaktas@google.com> Cc: James Bottomley <jejb@linux.ibm.com> Cc: Jiewen Yao <jiewen.yao@intel.com> Cc: Min Xu <min.m.xu@intel.com> Cc: Tom Lendacky <thomas.lendacky@amd.com> Cc: Michael Roth <michael.roth@amd.com> Cc: Gerd Hoffmann <kraxel@redhat.com> Acked-by: Gerd Hoffmann <kraxel@redhat.com> Reviewed-by: Jiewen Yao <jiewen.yao@intel.com> Signed-off-by: Ceping Sun <cepingx.sun@intel.com>
1 parent 212cf07 commit 8a41004

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

OvmfPkg/Library/BaseMemEncryptTdxLib/MemoryEncryption.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ typedef enum {
3838

3939
STATIC PAGE_TABLE_POOL *mPageTablePool = NULL;
4040

41+
#define MAX_RETRIES_PER_PAGE 3
42+
4143
/**
4244
Returns boolean to indicate whether to indicate which, if any, memory encryption is enabled
4345
@@ -527,6 +529,13 @@ SetOrClearSharedBit (
527529
EFI_STATUS Status;
528530
EDKII_MEMORY_ACCEPT_PROTOCOL *MemoryAcceptProtocol;
529531

532+
UINT64 MapGpaRetryAddr;
533+
UINT32 RetryCount;
534+
UINT64 EndAddress;
535+
536+
MapGpaRetryAddr = 0;
537+
RetryCount = 0;
538+
530539
AddressEncMask = GetMemEncryptionAddressMask ();
531540

532541
//
@@ -540,7 +549,37 @@ SetOrClearSharedBit (
540549
PhysicalAddress &= ~AddressEncMask;
541550
}
542551

543-
TdStatus = TdVmCall (TDVMCALL_MAPGPA, PhysicalAddress, Length, 0, 0, NULL);
552+
EndAddress = PhysicalAddress + Length;
553+
while (RetryCount < MAX_RETRIES_PER_PAGE) {
554+
TdStatus = TdVmCall (TDVMCALL_MAPGPA, PhysicalAddress, Length, 0, 0, &MapGpaRetryAddr);
555+
if (TdStatus != TDVMCALL_STATUS_RETRY) {
556+
break;
557+
}
558+
559+
DEBUG ((DEBUG_VERBOSE, "%a: TdVmcall(MAPGPA) Retry PhysicalAddress is %llx, MapGpaRetryAddr is %llx\n", __func__, PhysicalAddress, MapGpaRetryAddr));
560+
561+
if ((MapGpaRetryAddr < PhysicalAddress) || (MapGpaRetryAddr >= EndAddress)) {
562+
DEBUG ((
563+
DEBUG_ERROR,
564+
"%a: TdVmcall(MAPGPA) failed with MapGpaRetryAddr(%llx) less than PhysicalAddress(%llx) or more than or equal to EndAddress(%llx) \n",
565+
__func__,
566+
MapGpaRetryAddr,
567+
PhysicalAddress,
568+
EndAddress
569+
));
570+
break;
571+
}
572+
573+
if (MapGpaRetryAddr == PhysicalAddress) {
574+
RetryCount++;
575+
continue;
576+
}
577+
578+
PhysicalAddress = MapGpaRetryAddr;
579+
Length = EndAddress - PhysicalAddress;
580+
RetryCount = 0;
581+
}
582+
544583
if (TdStatus != 0) {
545584
DEBUG ((DEBUG_ERROR, "%a: TdVmcall(MAPGPA) failed with %llx\n", __func__, TdStatus));
546585
ASSERT (FALSE);

0 commit comments

Comments
 (0)