#include <Library/UefiBootServicesTableLib.h>\r
#include <Guid/ConfidentialComputingSevSnpBlob.h>\r
#include <Library/PcdLib.h>\r
+#include <Pi/PrePiDxeCis.h>\r
#include <Protocol/MemoryAccept.h>\r
\r
STATIC CONFIDENTIAL_COMPUTING_SNP_BLOB_LOCATION mSnpBootDxeTable = {\r
\r
STATIC EFI_HANDLE mAmdSevDxeHandle = NULL;\r
\r
+STATIC BOOLEAN mAcceptAllMemoryAtEBS = TRUE;\r
+\r
+STATIC EFI_EVENT mAcceptAllMemoryEvent = NULL;\r
+\r
#define IS_ALIGNED(x, y) ((((x) & ((y) - 1)) == 0))\r
\r
STATIC\r
return EFI_SUCCESS;\r
}\r
\r
+STATIC\r
+EFI_STATUS\r
+AcceptAllMemory (\r
+ VOID\r
+ )\r
+{\r
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR *AllDescMap;\r
+ UINTN NumEntries;\r
+ UINTN Index;\r
+ EFI_STATUS Status;\r
+\r
+ DEBUG ((DEBUG_INFO, "Accepting all memory\n"));\r
+\r
+ /*\r
+ * Get a copy of the memory space map to iterate over while\r
+ * changing the map.\r
+ */\r
+ Status = gDS->GetMemorySpaceMap (&NumEntries, &AllDescMap);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ for (Index = 0; Index < NumEntries; Index++) {\r
+ CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;\r
+\r
+ Desc = &AllDescMap[Index];\r
+ if (Desc->GcdMemoryType != EFI_GCD_MEMORY_TYPE_UNACCEPTED) {\r
+ continue;\r
+ }\r
+\r
+ Status = AmdSevMemoryAccept (\r
+ NULL,\r
+ Desc->BaseAddress,\r
+ Desc->Length\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ break;\r
+ }\r
+\r
+ Status = gDS->RemoveMemorySpace (Desc->BaseAddress, Desc->Length);\r
+ if (EFI_ERROR (Status)) {\r
+ break;\r
+ }\r
+\r
+ Status = gDS->AddMemorySpace (\r
+ EfiGcdMemoryTypeSystemMemory,\r
+ Desc->BaseAddress,\r
+ Desc->Length,\r
+ EFI_MEMORY_CPU_CRYPTO | EFI_MEMORY_XP | EFI_MEMORY_RO | EFI_MEMORY_RP\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ gBS->FreePool (AllDescMap);\r
+ return Status;\r
+}\r
+\r
+VOID\r
+EFIAPI\r
+ResolveUnacceptedMemory (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ if (!mAcceptAllMemoryAtEBS) {\r
+ return;\r
+ }\r
+\r
+ Status = AcceptAllMemory ();\r
+ ASSERT_EFI_ERROR (Status);\r
+}\r
+\r
STATIC EDKII_MEMORY_ACCEPT_PROTOCOL mMemoryAcceptProtocol = {\r
AmdSevMemoryAccept\r
};\r
);\r
ASSERT_EFI_ERROR (Status);\r
\r
+ // SEV-SNP support does not automatically imply unaccepted memory support,\r
+ // so make ExitBootServices accept all unaccepted memory if support is\r
+ // not communicated.\r
+ Status = gBS->CreateEventEx (\r
+ EVT_NOTIFY_SIGNAL,\r
+ TPL_CALLBACK,\r
+ ResolveUnacceptedMemory,\r
+ NULL,\r
+ &gEfiEventBeforeExitBootServicesGuid,\r
+ &mAcceptAllMemoryEvent\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "AllowUnacceptedMemory event creation for EventBeforeExitBootServices failed.\n"));\r
+ }\r
+\r
//\r
// If its SEV-SNP active guest then install the CONFIDENTIAL_COMPUTING_SEV_SNP_BLOB.\r
// It contains the location for both the Secrets and CPUID page.\r