19UINT64 mNumberOfDuplicatedAcceptedPages;
21#define TDX_ACCEPTPAGE_MAX_RETRIED 3
25UINT32 mTdxAcceptPageLevelMap[2] = {
30#define INVALID_ACCEPT_PAGELEVEL ARRAY_SIZE(mTdxAcceptPageLevelMap)
46 for (Index = 0; Index <
ARRAY_SIZE (mTdxAcceptPageLevelMap); Index++) {
47 if (mTdxAcceptPageLevelMap[Index] == PageSize) {
75 IN UINT64 StartAddress,
76 IN UINT64 NumberOfPages,
90 if ((StartAddress & ~0xFFFFFFFFFF000ULL) != 0) {
92 DEBUG ((DEBUG_ERROR,
"Accept page address(0x%llx) is not valid. [63:52] and [11:0] must be 0\n", StartAddress));
93 return EFI_INVALID_PARAMETER;
96 Address = StartAddress;
99 if (GpaPageLevel == INVALID_ACCEPT_PAGELEVEL) {
101 DEBUG ((DEBUG_ERROR,
"Accept page size must be 4K/2M. Invalid page size - 0x%llx\n", PageSize));
102 return EFI_INVALID_PARAMETER;
106 for (Index = 0; Index < NumberOfPages; Index++) {
110 TdxStatus =
TdCall (TDCALL_TDACCEPTPAGE, Address | GpaPageLevel, 0, 0, 0);
111 if (TdxStatus != TDX_EXIT_REASON_SUCCESS) {
112 if ((TdxStatus & ~0xFFFFULL) == TDX_EXIT_REASON_PAGE_ALREADY_ACCEPTED) {
116 mNumberOfDuplicatedAcceptedPages++;
117 DEBUG ((DEBUG_WARN,
"Page at Address (0x%llx) has already been accepted. - %d\n", Address, mNumberOfDuplicatedAcceptedPages));
118 }
else if ((TdxStatus & ~0xFFFFULL) == TDX_EXIT_REASON_PAGE_SIZE_MISMATCH) {
122 DEBUG ((DEBUG_VERBOSE,
"Address %llx cannot be accepted in PageLevel of %d\n", Address, GpaPageLevel));
124 if (GpaPageLevel == 0) {
128 DEBUG ((DEBUG_ERROR,
"AcceptPage cannot fallback from PageLevel %d\n", GpaPageLevel));
129 Status = EFI_INVALID_PARAMETER;
135 PageSize2 = mTdxAcceptPageLevelMap[GpaPageLevel - 1];
137 if (EFI_ERROR (Status)) {
141 }
else if ((TdxStatus & ~0xFFFFULL) == TDX_EXIT_REASON_OPERAND_BUSY) {
148 if (Retried > TDX_ACCEPTPAGE_MAX_RETRIED) {
151 "Address %llx (%d) failed to be accepted because of OPERAND_BUSY. Retried %d time.\n",
156 Status = EFI_INVALID_PARAMETER;
167 "Address %llx (%d) failed to be accepted. Error = 0x%llx\n",
172 Status = EFI_INVALID_PARAMETER;
EFI_STATUS EFIAPI TdAcceptPages(IN UINT64 StartAddress, IN UINT64 NumberOfPages, IN UINT32 PageSize)
UINT32 GetGpaPageLevel(UINT32 PageSize)
UINTN EFIAPI TdCall(IN UINT64 Leaf, IN UINT64 Arg1, IN UINT64 Arg2, IN UINT64 Arg3, IN OUT VOID *Results)
#define ARRAY_SIZE(Array)
#define DEBUG(Expression)