--- /dev/null
+/** @file\r
+ Append an ACPI S3 Boot Script fragment from the QEMU_LOADER_WRITE_POINTER\r
+ commands of QEMU's fully processed table linker/loader script.\r
+\r
+ Copyright (C) 2017, Red Hat, Inc.\r
+\r
+ This program and the accompanying materials are licensed and made available\r
+ under the terms and conditions of the BSD License which accompanies this\r
+ distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
+ WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+**/\r
+\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/QemuFwCfgLib.h>\r
+#include <Protocol/S3SaveState.h>\r
+\r
+#include "AcpiPlatform.h"\r
+\r
+\r
+//\r
+// Condensed structure for capturing the fw_cfg operations -- select, skip,\r
+// write -- inherent in executing a QEMU_LOADER_WRITE_POINTER command.\r
+//\r
+typedef struct {\r
+ UINT16 PointerItem; // resolved from QEMU_LOADER_WRITE_POINTER.PointerFile\r
+ UINT8 PointerSize; // copied as-is from QEMU_LOADER_WRITE_POINTER\r
+ UINT32 PointerOffset; // copied as-is from QEMU_LOADER_WRITE_POINTER\r
+ UINT64 PointerValue; // resolved from QEMU_LOADER_WRITE_POINTER.PointeeFile\r
+} CONDENSED_WRITE_POINTER;\r
+\r
+\r
+//\r
+// Context structure to accumulate CONDENSED_WRITE_POINTER objects from\r
+// QEMU_LOADER_WRITE_POINTER commands.\r
+//\r
+// Any pointers in this structure own the pointed-to objects; that is, when the\r
+// context structure is released, all pointed-to objects must be released too.\r
+//\r
+struct S3_CONTEXT {\r
+ CONDENSED_WRITE_POINTER *WritePointers; // one array element per processed\r
+ // QEMU_LOADER_WRITE_POINTER\r
+ // command\r
+ UINTN Allocated; // number of elements allocated for\r
+ // WritePointers\r
+ UINTN Used; // number of elements populated in\r
+ // WritePointers\r
+};\r
+\r
+\r
+//\r
+// Scratch buffer, allocated in EfiReservedMemoryType type memory, for the ACPI\r
+// S3 Boot Script opcodes to work on. We use the buffer to compose and to\r
+// replay several fw_cfg select+skip and write operations, using the DMA access\r
+// method. The fw_cfg operations will implement the actions dictated by\r
+// CONDENSED_WRITE_POINTER objects.\r
+//\r
+#pragma pack (1)\r
+typedef struct {\r
+ FW_CFG_DMA_ACCESS Access; // filled in from\r
+ // CONDENSED_WRITE_POINTER.PointerItem,\r
+ // CONDENSED_WRITE_POINTER.PointerSize,\r
+ // CONDENSED_WRITE_POINTER.PointerOffset\r
+ UINT64 PointerValue; // filled in from\r
+ // CONDENSED_WRITE_POINTER.PointerValue\r
+} SCRATCH_BUFFER;\r
+#pragma pack ()\r
+\r
+\r
+/**\r
+ Allocate an S3_CONTEXT object.\r
+\r
+ @param[out] S3Context The allocated S3_CONTEXT object is returned\r
+ through this parameter.\r
+\r
+ @param[in] WritePointerCount Number of CONDENSED_WRITE_POINTER elements to\r
+ allocate room for. WritePointerCount must be\r
+ positive.\r
+\r
+ @retval EFI_SUCCESS Allocation successful.\r
+\r
+ @retval EFI_OUT_OF_RESOURCES Out of memory.\r
+\r
+ @retval EFI_INVALID_PARAMETER WritePointerCount is zero.\r
+**/\r
+EFI_STATUS\r
+AllocateS3Context (\r
+ OUT S3_CONTEXT **S3Context,\r
+ IN UINTN WritePointerCount\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ S3_CONTEXT *Context;\r
+\r
+ if (WritePointerCount == 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Context = AllocateZeroPool (sizeof *Context);\r
+ if (Context == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ Context->WritePointers = AllocatePool (WritePointerCount *\r
+ sizeof *Context->WritePointers);\r
+ if (Context->WritePointers == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto FreeContext;\r
+ }\r
+\r
+ Context->Allocated = WritePointerCount;\r
+ *S3Context = Context;\r
+ return EFI_SUCCESS;\r
+\r
+FreeContext:\r
+ FreePool (Context);\r
+\r
+ return Status;\r
+}\r
+\r
+\r
+/**\r
+ Release an S3_CONTEXT object.\r
+\r
+ @param[in] S3Context The object to release.\r
+**/\r
+VOID\r
+ReleaseS3Context (\r
+ IN S3_CONTEXT *S3Context\r
+ )\r
+{\r
+ FreePool (S3Context->WritePointers);\r
+ FreePool (S3Context);\r
+}\r
+\r
+\r
+/**\r
+ Save the information necessary to replicate a QEMU_LOADER_WRITE_POINTER\r
+ command during S3 resume, in condensed format.\r
+\r
+ This function is to be called from ProcessCmdWritePointer(), after all the\r
+ sanity checks have passed, and before the fw_cfg operations are performed.\r
+\r
+ @param[in,out] S3Context The S3_CONTEXT object into which the caller wants\r
+ to save the information that was derived from\r
+ QEMU_LOADER_WRITE_POINTER.\r
+\r
+ @param[in] PointerItem The FIRMWARE_CONFIG_ITEM that\r
+ QEMU_LOADER_WRITE_POINTER.PointerFile was resolved\r
+ to, expressed as a UINT16 value.\r
+\r
+ @param[in] PointerSize Copied directly from\r
+ QEMU_LOADER_WRITE_POINTER.PointerSize.\r
+\r
+ @param[in] PointerOffset Copied directly from\r
+ QEMU_LOADER_WRITE_POINTER.PointerOffset.\r
+\r
+ @param[in] PointerValue The base address of the allocated / downloaded\r
+ fw_cfg blob that is identified by\r
+ QEMU_LOADER_WRITE_POINTER.PointeeFile.\r
+\r
+ @retval EFI_SUCCESS The information derived from\r
+ QEMU_LOADER_WRITE_POINTER has been successfully\r
+ absorbed into S3Context.\r
+\r
+ @retval EFI_OUT_OF_RESOURCES No room available in S3Context.\r
+**/\r
+EFI_STATUS\r
+SaveCondensedWritePointerToS3Context (\r
+ IN OUT S3_CONTEXT *S3Context,\r
+ IN UINT16 PointerItem,\r
+ IN UINT8 PointerSize,\r
+ IN UINT32 PointerOffset,\r
+ IN UINT64 PointerValue\r
+ )\r
+{\r
+ CONDENSED_WRITE_POINTER *Condensed;\r
+\r
+ if (S3Context->Used == S3Context->Allocated) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ Condensed = S3Context->WritePointers + S3Context->Used;\r
+ Condensed->PointerItem = PointerItem;\r
+ Condensed->PointerSize = PointerSize;\r
+ Condensed->PointerOffset = PointerOffset;\r
+ Condensed->PointerValue = PointerValue;\r
+ DEBUG ((DEBUG_VERBOSE, "%a: 0x%04x/[0x%08x+%d] := 0x%Lx (%Lu)\n",\r
+ __FUNCTION__, PointerItem, PointerOffset, PointerSize, PointerValue,\r
+ (UINT64)S3Context->Used));\r
+ ++S3Context->Used;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+ Translate and append the information from an S3_CONTEXT object to the ACPI S3\r
+ Boot Script.\r
+\r
+ The effects of a successful call to this function cannot be undone.\r
+\r
+ @param[in] S3Context The S3_CONTEXT object to translate to ACPI S3 Boot\r
+ Script opcodes.\r
+\r
+ @retval EFI_OUT_OF_RESOURCES Out of memory.\r
+\r
+ @retval EFI_SUCCESS The translation of S3Context to ACPI S3 Boot\r
+ Script opcodes has been successful.\r
+\r
+ @return Error codes from underlying functions.\r
+**/\r
+EFI_STATUS\r
+TransferS3ContextToBootScript (\r
+ IN CONST S3_CONTEXT *S3Context\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_S3_SAVE_STATE_PROTOCOL *S3SaveState;\r
+ SCRATCH_BUFFER *ScratchBuffer;\r
+ FW_CFG_DMA_ACCESS *Access;\r
+ UINT64 BigEndianAddressOfAccess;\r
+ UINT32 ControlPollData;\r
+ UINT32 ControlPollMask;\r
+ UINTN Index;\r
+\r
+ //\r
+ // If the following protocol lookup fails, it shall not happen due to an\r
+ // unexpected DXE driver dispatch order.\r
+ //\r
+ // Namely, this function is only invoked on QEMU. Therefore it is only\r
+ // reached after Platform BDS signals gRootBridgesConnectedEventGroupGuid\r
+ // (see OnRootBridgesConnected() in "EntryPoint.c"). Hence, because\r
+ // TransferS3ContextToBootScript() is invoked in BDS, all DXE drivers,\r
+ // including S3SaveStateDxe (producing EFI_S3_SAVE_STATE_PROTOCOL), have been\r
+ // dispatched by the time we get here. (S3SaveStateDxe is not expected to\r
+ // have any stricter-than-TRUE DEPEX -- not a DEPEX that gets unblocked only\r
+ // within BDS anyway.)\r
+ //\r
+ // Reaching this function also depends on QemuFwCfgS3Enabled(). That implies\r
+ // S3SaveStateDxe has not exited immediately due to S3 being disabled. Thus\r
+ // EFI_S3_SAVE_STATE_PROTOCOL can only be missing for genuinely unforeseeable\r
+ // reasons.\r
+ //\r
+ Status = gBS->LocateProtocol (&gEfiS3SaveStateProtocolGuid,\r
+ NULL /* Registration */, (VOID **)&S3SaveState);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "%a: LocateProtocol(): %r\n", __FUNCTION__, Status));\r
+ return Status;\r
+ }\r
+\r
+ ScratchBuffer = AllocateReservedPool (sizeof *ScratchBuffer);\r
+ if (ScratchBuffer == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ //\r
+ // Set up helper variables that we'll use identically for all\r
+ // CONDENSED_WRITE_POINTER elements.\r
+ //\r
+ Access = &ScratchBuffer->Access;\r
+ BigEndianAddressOfAccess = SwapBytes64 ((UINTN)Access);\r
+ ControlPollData = 0;\r
+ ControlPollMask = MAX_UINT32;\r
+\r
+ //\r
+ // For each CONDENSED_WRITE_POINTER, we need six ACPI S3 Boot Script opcodes:\r
+ // (1) restore an FW_CFG_DMA_ACCESS object in reserved memory that selects\r
+ // the writeable fw_cfg file PointerFile (through PointerItem), and skips\r
+ // to PointerOffset in it,\r
+ // (2) call QEMU with the FW_CFG_DMA_ACCESS object,\r
+ // (3) wait for the select+skip to finish,\r
+ // (4) restore a SCRATCH_BUFFER object in reserved memory that writes\r
+ // PointerValue (base address of the allocated / downloaded PointeeFile),\r
+ // of size PointerSize, into the fw_cfg file selected in (1), at the\r
+ // offset sought to in (1),\r
+ // (5) call QEMU with the FW_CFG_DMA_ACCESS object,\r
+ // (6) wait for the write to finish.\r
+ //\r
+ // EFI_S3_SAVE_STATE_PROTOCOL does not allow rolling back opcode additions,\r
+ // therefore we treat any failure here as fatal.\r
+ //\r
+ for (Index = 0; Index < S3Context->Used; ++Index) {\r
+ CONST CONDENSED_WRITE_POINTER *Condensed;\r
+\r
+ Condensed = &S3Context->WritePointers[Index];\r
+\r
+ //\r
+ // (1) restore an FW_CFG_DMA_ACCESS object in reserved memory that selects\r
+ // the writeable fw_cfg file PointerFile (through PointerItem), and\r
+ // skips to PointerOffset in it,\r
+ //\r
+ Access->Control = SwapBytes32 ((UINT32)Condensed->PointerItem << 16 |\r
+ FW_CFG_DMA_CTL_SELECT | FW_CFG_DMA_CTL_SKIP);\r
+ Access->Length = SwapBytes32 (Condensed->PointerOffset);\r
+ Access->Address = 0;\r
+ Status = S3SaveState->Write (\r
+ S3SaveState, // This\r
+ EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE, // OpCode\r
+ EfiBootScriptWidthUint8, // Width\r
+ (UINT64)(UINTN)Access, // Address\r
+ sizeof *Access, // Count\r
+ Access // Buffer\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 1: %r\n", __FUNCTION__,\r
+ (UINT64)Index, Status));\r
+ goto FatalError;\r
+ }\r
+\r
+ //\r
+ // (2) call QEMU with the FW_CFG_DMA_ACCESS object,\r
+ //\r
+ Status = S3SaveState->Write (\r
+ S3SaveState, // This\r
+ EFI_BOOT_SCRIPT_IO_WRITE_OPCODE, // OpCode\r
+ EfiBootScriptWidthUint32, // Width\r
+ (UINT64)0x514, // Address\r
+ (UINTN)2, // Count\r
+ &BigEndianAddressOfAccess // Buffer\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 2: %r\n", __FUNCTION__,\r
+ (UINT64)Index, Status));\r
+ goto FatalError;\r
+ }\r
+\r
+ //\r
+ // (3) wait for the select+skip to finish,\r
+ //\r
+ Status = S3SaveState->Write (\r
+ S3SaveState, // This\r
+ EFI_BOOT_SCRIPT_MEM_POLL_OPCODE, // OpCode\r
+ EfiBootScriptWidthUint32, // Width\r
+ (UINT64)(UINTN)&Access->Control, // Address\r
+ &ControlPollData, // Data\r
+ &ControlPollMask, // DataMask\r
+ MAX_UINT64 // Delay\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 3: %r\n", __FUNCTION__,\r
+ (UINT64)Index, Status));\r
+ goto FatalError;\r
+ }\r
+\r
+ //\r
+ // (4) restore a SCRATCH_BUFFER object in reserved memory that writes\r
+ // PointerValue (base address of the allocated / downloaded\r
+ // PointeeFile), of size PointerSize, into the fw_cfg file selected in\r
+ // (1), at the offset sought to in (1),\r
+ //\r
+ Access->Control = SwapBytes32 (FW_CFG_DMA_CTL_WRITE);\r
+ Access->Length = SwapBytes32 (Condensed->PointerSize);\r
+ Access->Address = SwapBytes64 ((UINTN)&ScratchBuffer->PointerValue);\r
+ ScratchBuffer->PointerValue = Condensed->PointerValue;\r
+ Status = S3SaveState->Write (\r
+ S3SaveState, // This\r
+ EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE, // OpCode\r
+ EfiBootScriptWidthUint8, // Width\r
+ (UINT64)(UINTN)ScratchBuffer, // Address\r
+ sizeof *ScratchBuffer, // Count\r
+ ScratchBuffer // Buffer\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 4: %r\n", __FUNCTION__,\r
+ (UINT64)Index, Status));\r
+ goto FatalError;\r
+ }\r
+\r
+ //\r
+ // (5) call QEMU with the FW_CFG_DMA_ACCESS object,\r
+ //\r
+ Status = S3SaveState->Write (\r
+ S3SaveState, // This\r
+ EFI_BOOT_SCRIPT_IO_WRITE_OPCODE, // OpCode\r
+ EfiBootScriptWidthUint32, // Width\r
+ (UINT64)0x514, // Address\r
+ (UINTN)2, // Count\r
+ &BigEndianAddressOfAccess // Buffer\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 5: %r\n", __FUNCTION__,\r
+ (UINT64)Index, Status));\r
+ goto FatalError;\r
+ }\r
+\r
+ //\r
+ // (6) wait for the write to finish.\r
+ //\r
+ Status = S3SaveState->Write (\r
+ S3SaveState, // This\r
+ EFI_BOOT_SCRIPT_MEM_POLL_OPCODE, // OpCode\r
+ EfiBootScriptWidthUint32, // Width\r
+ (UINT64)(UINTN)&Access->Control, // Address\r
+ &ControlPollData, // Data\r
+ &ControlPollMask, // DataMask\r
+ MAX_UINT64 // Delay\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "%a: Index %Lu opcode 6: %r\n", __FUNCTION__,\r
+ (UINT64)Index, Status));\r
+ goto FatalError;\r
+ }\r
+ }\r
+\r
+ DEBUG ((DEBUG_VERBOSE, "%a: boot script fragment saved, ScratchBuffer=%p\n",\r
+ __FUNCTION__, (VOID *)ScratchBuffer));\r
+ return EFI_SUCCESS;\r
+\r
+FatalError:\r
+ ASSERT (FALSE);\r
+ CpuDeadLoop ();\r
+ return Status;\r
+}\r
@param[in] Tracker The ORDERED_COLLECTION tracking the BLOB user\r
structures created thus far.\r
\r
+ @param[in,out] S3Context The S3_CONTEXT object capturing the fw_cfg actions\r
+ of successfully processed QEMU_LOADER_WRITE_POINTER\r
+ commands, to be replayed at S3 resume. S3Context\r
+ may be NULL if S3 is disabled.\r
+\r
@retval EFI_PROTOCOL_ERROR Malformed fw_cfg file name(s) have been found in\r
WritePointer. Or, the WritePointer command\r
references a file unknown to Tracker or the\r
does not fit in the given pointer size.\r
\r
@retval EFI_SUCCESS The pointer object inside the writeable fw_cfg\r
- file has been written.\r
+ file has been written. If S3Context is not NULL,\r
+ then WritePointer has been condensed into\r
+ S3Context.\r
+\r
+ @return Error codes propagated from\r
+ SaveCondensedWritePointerToS3Context(). The\r
+ pointer object inside the writeable fw_cfg file\r
+ has not been written.\r
**/\r
STATIC\r
EFI_STATUS\r
ProcessCmdWritePointer (\r
IN CONST QEMU_LOADER_WRITE_POINTER *WritePointer,\r
- IN CONST ORDERED_COLLECTION *Tracker\r
+ IN CONST ORDERED_COLLECTION *Tracker,\r
+ IN OUT S3_CONTEXT *S3Context OPTIONAL\r
)\r
{\r
RETURN_STATUS Status;\r
return EFI_PROTOCOL_ERROR;\r
}\r
\r
+ //\r
+ // If S3 is enabled, we have to capture the below fw_cfg actions in condensed\r
+ // form, to be replayed during S3 resume.\r
+ //\r
+ if (S3Context != NULL) {\r
+ EFI_STATUS SaveStatus;\r
+\r
+ SaveStatus = SaveCondensedWritePointerToS3Context (\r
+ S3Context,\r
+ (UINT16)PointerItem,\r
+ WritePointer->PointerSize,\r
+ WritePointer->PointerOffset,\r
+ PointerValue\r
+ );\r
+ if (EFI_ERROR (SaveStatus)) {\r
+ return SaveStatus;\r
+ }\r
+ }\r
+\r
QemuFwCfgSelectItem (PointerItem);\r
QemuFwCfgSkipBytes (WritePointer->PointerOffset);\r
QemuFwCfgWriteBytes (WritePointer->PointerSize, &PointerValue);\r
CONST QEMU_LOADER_ENTRY *WritePointerSubsetEnd;\r
ORIGINAL_ATTRIBUTES *OriginalPciAttributes;\r
UINTN OriginalPciAttributesCount;\r
+ S3_CONTEXT *S3Context;\r
ORDERED_COLLECTION *Tracker;\r
UINTN *InstalledKey;\r
INT32 Installed;\r
RestorePciDecoding (OriginalPciAttributes, OriginalPciAttributesCount);\r
LoaderEnd = LoaderStart + FwCfgSize / sizeof *LoaderEntry;\r
\r
+ S3Context = NULL;\r
+ if (QemuFwCfgS3Enabled ()) {\r
+ //\r
+ // Size the allocation pessimistically, assuming that all commands in the\r
+ // script are QEMU_LOADER_WRITE_POINTER commands.\r
+ //\r
+ Status = AllocateS3Context (&S3Context, LoaderEnd - LoaderStart);\r
+ if (EFI_ERROR (Status)) {\r
+ goto FreeLoader;\r
+ }\r
+ }\r
+\r
Tracker = OrderedCollectionInit (BlobCompare, BlobKeyCompare);\r
if (Tracker == NULL) {\r
Status = EFI_OUT_OF_RESOURCES;\r
- goto FreeLoader;\r
+ goto FreeS3Context;\r
}\r
\r
//\r
\r
case QemuLoaderCmdWritePointer:\r
Status = ProcessCmdWritePointer (&LoaderEntry->Command.WritePointer,\r
- Tracker);\r
+ Tracker, S3Context);\r
if (!EFI_ERROR (Status)) {\r
WritePointerSubsetEnd = LoaderEntry + 1;\r
}\r
Status = Process2ndPassCmdAddPointer (&LoaderEntry->Command.AddPointer,\r
Tracker, AcpiProtocol, InstalledKey, &Installed);\r
if (EFI_ERROR (Status)) {\r
- break;\r
+ goto UninstallAcpiTables;\r
}\r
}\r
}\r
\r
+ //\r
+ // Translating the condensed QEMU_LOADER_WRITE_POINTER commands to ACPI S3\r
+ // Boot Script opcodes has to be the last operation in this function, because\r
+ // if it succeeds, it cannot be undone.\r
+ //\r
+ if (S3Context != NULL) {\r
+ Status = TransferS3ContextToBootScript (S3Context);\r
+ }\r
+\r
+UninstallAcpiTables:\r
if (EFI_ERROR (Status)) {\r
//\r
// roll back partial installation\r
}\r
OrderedCollectionUninit (Tracker);\r
\r
+FreeS3Context:\r
+ if (S3Context != NULL) {\r
+ ReleaseS3Context (S3Context);\r
+ }\r
+\r
FreeLoader:\r
FreePool (LoaderStart);\r
\r