From nobody Sat Nov 2 16:27:55 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zoho.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1487814530097164.69650344315085; Wed, 22 Feb 2017 17:48:50 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 596DD8227B; Wed, 22 Feb 2017 17:48:38 -0800 (PST) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 575028226D for ; Wed, 22 Feb 2017 17:48:36 -0800 (PST) Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E76D04E4D9; Thu, 23 Feb 2017 01:48:36 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-134.phx2.redhat.com [10.3.116.134]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v1N1mJFP012836; Wed, 22 Feb 2017 20:48:36 -0500 X-Original-To: edk2-devel@ml01.01.org From: Laszlo Ersek To: edk2-devel-01 Date: Thu, 23 Feb 2017 02:48:13 +0100 Message-Id: <20170223014814.10937-12-lersek@redhat.com> In-Reply-To: <20170223014814.10937-1-lersek@redhat.com> References: <20170223014814.10937-1-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 23 Feb 2017 01:48:36 +0000 (UTC) Subject: [edk2] [PATCH 11/12] OvmfPkg/SmmControl2Dxe: save fw_cfg boot script with QemuFwCfgS3Lib X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jordan Justen MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" We cannot entirely eliminate the manual boot script building in this driver, as it also programs lower-level chipset registers (SMI_EN, GEN_PMCON_1) at S3 resume, not just registers exposed via fw_cfg. We can nonetheless replace the manually built opcodes for the latter class of registers with QemuFwCfgS3Lib function calls. We preserve the ordering between the two sets of registers (low-level chipset first, fw_cfg second). This patch demonstrates that manual handling of S3SaveState protocol installation can be combined with QemuFwCfgS3Lib, even without upsetting the original order between boot script fragments. An S3SaveState notify function running at TPL_CALLBACK can safely queue another S3SaveState notify function at TPL_CALLBACK with QemuFwCfgS3TransferOwnership(). Cc: Jordan Justen Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D394 Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Laszlo Ersek --- OvmfPkg/SmmControl2Dxe/SmiFeatures.h | 5 +- OvmfPkg/SmmControl2Dxe/SmiFeatures.c | 224 +++++++------------- OvmfPkg/SmmControl2Dxe/SmmControl2Dxe.c | 5 +- 3 files changed, 77 insertions(+), 157 deletions(-) diff --git a/OvmfPkg/SmmControl2Dxe/SmiFeatures.h b/OvmfPkg/SmmControl2Dxe/= SmiFeatures.h index 9d5f1dbcb57e..3f3a5d3ea9b1 100644 --- a/OvmfPkg/SmmControl2Dxe/SmiFeatures.h +++ b/OvmfPkg/SmmControl2Dxe/SmiFeatures.h @@ -37,13 +37,10 @@ NegotiateSmiFeatures ( /** Append a boot script fragment that will re-select the previously negotia= ted SMI features during S3 resume. - - @param[in] S3SaveState The EFI_S3_SAVE_STATE_PROTOCOL instance to appen= d to - the S3 boot script with. **/ VOID SaveSmiFeatures ( - IN EFI_S3_SAVE_STATE_PROTOCOL *S3SaveState + VOID ); =20 #endif diff --git a/OvmfPkg/SmmControl2Dxe/SmiFeatures.c b/OvmfPkg/SmmControl2Dxe/= SmiFeatures.c index bd257f15d955..ecdab2fd9a86 100644 --- a/OvmfPkg/SmmControl2Dxe/SmiFeatures.c +++ b/OvmfPkg/SmmControl2Dxe/SmiFeatures.c @@ -18,6 +18,7 @@ #include #include #include +#include =20 #include "SmiFeatures.h" =20 @@ -29,29 +30,26 @@ =20 // // Provides a scratch buffer (allocated in EfiReservedMemoryType type memo= ry) -// for the S3 boot script fragment to write to and read from. The buffer -// captures a combined fw_cfg item selection + write command using the DMA -// access method. Note that we don't trust the runtime OS to preserve the -// contents of the buffer, the boot script will first rewrite it. +// for the S3 boot script fragment to write to and read from. // #pragma pack (1) -typedef struct { - FW_CFG_DMA_ACCESS Access; - UINT64 Features; +typedef union { + UINT64 Features; + UINT8 FeaturesOk; } SCRATCH_BUFFER; #pragma pack () =20 // // These carry the selector keys of the "etc/smi/requested-features" and // "etc/smi/features-ok" fw_cfg files from NegotiateSmiFeatures() to -// SaveSmiFeatures(). +// AppendFwCfgBootScript(). // STATIC FIRMWARE_CONFIG_ITEM mRequestedFeaturesItem; STATIC FIRMWARE_CONFIG_ITEM mFeaturesOkItem; =20 // // Carries the negotiated SMI features from NegotiateSmiFeatures() to -// SaveSmiFeatures(). +// AppendFwCfgBootScript(). // STATIC UINT64 mSmiFeatures; =20 @@ -168,157 +166,81 @@ FatalError: } =20 /** + FW_CFG_BOOT_SCRIPT_APPEND_FUNCTION provided to QemuFwCfgS3Lib. +**/ +STATIC +VOID +EFIAPI +AppendFwCfgBootScript ( + IN OUT VOID *Context, OPTIONAL + IN OUT VOID *ExternalScratchBuffer + ) +{ + SCRATCH_BUFFER *ScratchBuffer; + RETURN_STATUS Status; + + ScratchBuffer =3D ExternalScratchBuffer; + + // + // Write the negotiated feature bitmap into "etc/smi/requested-features". + // + ScratchBuffer->Features =3D mSmiFeatures; + Status =3D QemuFwCfgS3WriteBytes (mRequestedFeaturesItem, + sizeof ScratchBuffer->Features); + if (RETURN_ERROR (Status)) { + goto FatalError; + } + + // + // Read back "etc/smi/features-ok". This invokes the feature validation & + // lockdown. (The validation succeeded at first boot.) + // + Status =3D QemuFwCfgS3ReadBytes (mFeaturesOkItem, + sizeof ScratchBuffer->FeaturesOk); + if (RETURN_ERROR (Status)) { + goto FatalError; + } + + // + // If "etc/smi/features-ok" read as 1, we're good. Otherwise, hang the S3 + // resume process. + // + Status =3D QemuFwCfgS3CheckValue (&ScratchBuffer->FeaturesOk, + sizeof ScratchBuffer->FeaturesOk, MAX_UINT8, 1); + if (RETURN_ERROR (Status)) { + goto FatalError; + } + + DEBUG ((DEBUG_VERBOSE, "%a: SMI feature negotiation boot script saved\n", + __FUNCTION__)); + return; + +FatalError: + ASSERT (FALSE); + CpuDeadLoop (); +} + + +/** Append a boot script fragment that will re-select the previously negotia= ted SMI features during S3 resume. - - @param[in] S3SaveState The EFI_S3_SAVE_STATE_PROTOCOL instance to appen= d to - the S3 boot script with. **/ VOID SaveSmiFeatures ( - IN EFI_S3_SAVE_STATE_PROTOCOL *S3SaveState + VOID ) { - SCRATCH_BUFFER *ScratchBuffer; - EFI_STATUS Status; - UINT64 AccessAddress; - UINT32 ControlPollData; - UINT32 ControlPollMask; - UINT16 FeaturesOkItemAsUint16; - UINT8 FeaturesOkData; - UINT8 FeaturesOkMask; + RETURN_STATUS Status; =20 - ScratchBuffer =3D AllocateReservedPool (sizeof *ScratchBuffer); - if (ScratchBuffer =3D=3D NULL) { - DEBUG ((DEBUG_ERROR, "%a: scratch buffer allocation failed\n", - __FUNCTION__)); - goto FatalError; - } - - // - // Populate the scratch buffer with a select + write fw_cfg DMA command = that - // will write the negotiated feature bitmap into - // "etc/smi/requested-features". - // - ScratchBuffer->Access.Control =3D SwapBytes32 ( - (UINT32)mRequestedFeaturesItem << 16 | - FW_CFG_DMA_CTL_SELECT | - FW_CFG_DMA_CTL_WRITE - ); - ScratchBuffer->Access.Length =3D SwapBytes32 ( - (UINT32)sizeof ScratchBuffer->Features); - ScratchBuffer->Access.Address =3D SwapBytes64 ( - (UINTN)&ScratchBuffer->Features); - ScratchBuffer->Features =3D mSmiFeatures; - - // - // Copy the scratch buffer into the boot script. When replayed, this - // EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE will restore the current contents of= the - // scratch buffer, in-place. // - Status =3D S3SaveState->Write ( - S3SaveState, // This - EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE, // OpCode - EfiBootScriptWidthUint8, // Width - (UINT64)(UINTN)ScratchBuffer, // Address - sizeof *ScratchBuffer, // Count - (VOID*)ScratchBuffer // Buffer - ); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "%a:%d: EFI_BOOT_SCRIPT_MEM_WRITE_OPCODE: %r\n", - __FUNCTION__, __LINE__, Status)); - goto FatalError; - } - - // - // Append an opcode that will write the address of the scratch buffer to= the - // fw_cfg DMA address register, which consists of two 32-bit IO ports. T= he - // second (highest address, least significant) write will start the tran= sfer. + // We are already running at TPL_CALLBACK, on the stack of + // OnS3SaveStateInstalled(). But that's okay, we can easily queue more + // notification functions while executing a notification function. // - AccessAddress =3D SwapBytes64 ((UINTN)&ScratchBuffer->Access); - Status =3D S3SaveState->Write ( - S3SaveState, // This - EFI_BOOT_SCRIPT_IO_WRITE_OPCODE, // OpCode - EfiBootScriptWidthUint32, // Width - (UINT64)FW_CFG_IO_DMA_ADDRESS, // Address - (UINTN)2, // Count - &AccessAddress // Buffer - ); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "%a:%d: EFI_BOOT_SCRIPT_IO_WRITE_OPCODE: %r\n", - __FUNCTION__, __LINE__, Status)); - goto FatalError; + Status =3D QemuFwCfgS3TransferOwnership (AppendFwCfgBootScript, NULL, + sizeof (SCRATCH_BUFFER)); + if (RETURN_ERROR (Status)) { + ASSERT (FALSE); + CpuDeadLoop (); } - - // - // The EFI_BOOT_SCRIPT_MEM_POLL_OPCODE will wait until the Control word = reads - // as zero (transfer complete). As timeout we use MAX_UINT64 * 100ns, wh= ich - // is approximately 58494 years. - // - ControlPollData =3D 0; - ControlPollMask =3D MAX_UINT32; - Status =3D S3SaveState->Write ( - S3SaveState, // This - EFI_BOOT_SCRIPT_MEM_POLL_OPCODE, // OpC= ode - EfiBootScriptWidthUint32, // Wid= th - (UINT64)(UINTN)&ScratchBuffer->Access.Control, // Add= ress - &ControlPollData, // Data - &ControlPollMask, // Dat= aMask - MAX_UINT64 // Del= ay - ); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "%a:%d: EFI_BOOT_SCRIPT_MEM_POLL_OPCODE: %r\n", - __FUNCTION__, __LINE__, Status)); - goto FatalError; - } - - // - // Select the "etc/smi/features-ok" fw_cfg file, which invokes the featu= re - // validation & lockdown. (The validation succeeded at first boot.) - // - FeaturesOkItemAsUint16 =3D (UINT16)mFeaturesOkItem; - Status =3D S3SaveState->Write ( - S3SaveState, // This - EFI_BOOT_SCRIPT_IO_WRITE_OPCODE, // OpCode - EfiBootScriptWidthUint16, // Width - (UINT64)FW_CFG_IO_SELECTOR, // Address - (UINTN)1, // Count - &FeaturesOkItemAsUint16 // Buffer - ); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "%a:%d: EFI_BOOT_SCRIPT_IO_WRITE_OPCODE: %r\n", - __FUNCTION__, __LINE__, Status)); - goto FatalError; - } - - // - // Read the contents (one byte) of "etc/smi/features-ok". If the value is - // one, we're good. Otherwise, continue reading the data port: QEMU retu= rns 0 - // past the end of the fw_cfg item, so this will hang the resume process, - // which matches our intent. - // - FeaturesOkData =3D 1; - FeaturesOkMask =3D MAX_UINT8; - Status =3D S3SaveState->Write ( - S3SaveState, // This - EFI_BOOT_SCRIPT_IO_POLL_OPCODE, // OpCode - EfiBootScriptWidthUint8, // Width - (UINT64)(UINTN)FW_CFG_IO_DATA, // Address - &FeaturesOkData, // Data - &FeaturesOkMask, // DataMask - MAX_UINT64 // Delay - ); - if (EFI_ERROR (Status)) { - DEBUG ((DEBUG_ERROR, "%a:%d: EFI_BOOT_SCRIPT_IO_POLL_OPCODE: %r\n", - __FUNCTION__, __LINE__, Status)); - goto FatalError; - } - - DEBUG ((DEBUG_VERBOSE, "%a: ScratchBuffer@%p\n", __FUNCTION__, - (VOID *)ScratchBuffer)); - return; - -FatalError: - ASSERT (FALSE); - CpuDeadLoop (); } diff --git a/OvmfPkg/SmmControl2Dxe/SmmControl2Dxe.c b/OvmfPkg/SmmControl2D= xe/SmmControl2Dxe.c index bb79fce0855b..e1cd3d02ac36 100644 --- a/OvmfPkg/SmmControl2Dxe/SmmControl2Dxe.c +++ b/OvmfPkg/SmmControl2Dxe/SmmControl2Dxe.c @@ -378,14 +378,15 @@ OnS3SaveStateInstalled ( CpuDeadLoop (); } =20 + DEBUG ((DEBUG_VERBOSE, "%a: chipset boot script saved\n", __FUNCTION__)); + // // Append a boot script fragment that re-selects the negotiated SMI feat= ures. // if (mSmiFeatureNegotiation) { - SaveSmiFeatures (S3SaveState); + SaveSmiFeatures (); } =20 - DEBUG ((EFI_D_VERBOSE, "%a: boot script fragment saved\n", __FUNCTION__)= ); gBS->CloseEvent (Event); mS3SaveStateInstalled =3D NULL; } --=20 2.9.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel