From nobody Wed Apr 24 01:41:59 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+49728+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+49728+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1572504358; cv=none; d=zoho.com; s=zohoarc; b=n5YoTQmczjSXyYgJeXVYwQJikoI+Qd2JBUbz29wimvX00nU/i+ebbvSOacnfmSH+uv5i6BwzrSk6uIMJkFZXnR9uam201wPHqaT7G7x1Ws6GxvJ4eyzBm14Omxkr4feooP42lSgdn0jGPQ66zsH+WzjaFDfjLb5cKTBJnNKiq+g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1572504358; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To; bh=Kqw7a10DUruP9CwSXtMAE0oAkNgP2ZTlfFDFL1FIMWM=; b=EwJvCOcAFYvFCvlm4/tsiY2y3GSdRbOXEJA1aFDRHwVih2OXJeF0Z/gHOEU30MFck3WPyquDgHLXLIhFAuJmyqiYsNla5+yx5aslZkk20u8Jr5bv7N8urNp90ovH7P9NZfBEY9kZD3c6UyWUY9QRKaDgb0EwY9UuUF1XNgI02nw= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+49728+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1572504358932800.6960240946014; Wed, 30 Oct 2019 23:45:58 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id h1PdYY1788612xwWSN6HphwV; Wed, 30 Oct 2019 23:45:58 -0700 X-Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web10.2581.1572504357840732993 for ; Wed, 30 Oct 2019 23:45:57 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Oct 2019 23:45:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,250,1569308400"; d="scan'208";a="351547489" X-Received: from makuback-desk1.amr.corp.intel.com ([10.7.159.162]) by orsmga004.jf.intel.com with ESMTP; 30 Oct 2019 23:45:57 -0700 From: "Kubacki, Michael A" To: devel@edk2.groups.io Cc: Dandan Bi , Ard Biesheuvel , Eric Dong , Laszlo Ersek , Liming Gao , Michael D Kinney , Ray Ni , Jian J Wang , Hao A Wu , Jiewen Yao Subject: [edk2-devel] [PATCH V6 07/10] MdeModulePkg/Variable: Add RT GetVariable() cache support Date: Wed, 30 Oct 2019 23:45:07 -0700 Message-Id: <20191031064510.23948-8-michael.a.kubacki@intel.com> In-Reply-To: <20191031064510.23948-1-michael.a.kubacki@intel.com> References: <20191031064510.23948-1-michael.a.kubacki@intel.com> Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,michael.a.kubacki@intel.com X-Gm-Message-State: f2eSpPIjdhHqTAzwsQphUTPbx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1572504358; bh=Wfphk30JgjgjcTv+uFCk1y2dWrTPdJoEhWFAuG10CKU=; h=Cc:Date:From:Reply-To:Subject:To; b=Pzk7FVpu5DqpXBh+RQ8TiDXIc3gSUDioax4DQky4l0zJgnoloIDmsKSX5U28y1oXL9U t5/3d+htDGV6OLc6dVBYGqQs6+5cGDko74F2nMTAsZ9l7COC9h7xqx/ucCaZ+k9i7Wzj2 GS8NiiVkynlA3EmRAszpNMlagp9fFf07zh4= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3D2220 This change reduces SMIs for GetVariable () by maintaining a UEFI variable cache in Runtime DXE in addition to the pre- existing cache in SMRAM. When the Runtime Service GetVariable() is invoked, a Runtime DXE cache is used instead of triggering an SMI to VariableSmm. This can improve overall system performance by servicing variable read requests without rendezvousing all cores into SMM. The runtime cache can be disabled with by setting the FeaturePCD gEfiMdeModulePkgTokenSpaceGuid.PcdEnableVariableRuntimeCache to FALSE. If the PCD is set to FALSE, the runtime cache will not be used and an SMI will be triggered for Runtime Service GetVariable () and GetNextVariableName () invocations. The following are important points regarding the behavior of the variable drivers when the variable runtime cache is enabled. 1. All of the non-volatile storage contents are loaded into the cache upon driver load. This one time load operation from storage is preferred as opposed to building the cache on demand. An on- demand cache would require a fallback SMI to load data into the cache as variables are requested. 2. SetVariable () requests will continue to always trigger an SMI. This occurs regardless of whether the variable is volatile or non-volatile. 3. Both volatile and non-volatile variables are cached in a runtime buffer. As is the case in the current EDK II variable driver, they continue to be cached in separate buffers. 4. The cache in Runtime DXE and SMM are intended to be exact copies of one another. All SMM variable accesses only return data from the SMM cache. The runtime caches are only updated after the variable I/O operation is successful in SMM. The runtime caches are only updated from SMM. 5. Synchronization mechanisms are in place to ensure the runtime cache content integrity with the SMM cache. These may result in updates to runtime cache that are the same in content but different in offset and size from updates to the SMM cache. When using SMM variables with runtime cache enabled, two caches will now be present. 1. "Runtime Cache" - Maintained in VariableSmmRuntimeDxe. Used to service Runtime Services GetVariable () and GetNextVariableName () callers. 2. "SMM Cache" - Maintained in VariableSmm to service SMM GetVariable () and GetNextVariableName () callers. a. This cache is retained so SMM modules do not operate on data outside SMRAM. Because a race condition can occur if an SMI occurs during the execution of runtime code reading from the runtime cache, a runtime cache read lock is introduced that explicitly moves pending updates from SMM to the runtime cache if an SMM update occurs while the runtime cache is locked. Note that it is not expected a Runtime services call will interrupt SMM processing since all CPU cores rendezvous in SMM. It is possible to view UEFI variable read and write statistics by setting the gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics FeaturePcd to TRUE and using the VariableInfo UEFI application in MdeModulePkg to dump variable statistics to the console. By doing so, a user can view the number of GetVariable () hits from the Runtime DXE variable driver (Runtime Cache hits) and the SMM variable driver (SMM Cache hits). SMM Cache hits for GetVariable () will occur when SMM modules invoke GetVariable (). Cc: Dandan Bi Cc: Ard Biesheuvel Cc: Eric Dong Cc: Laszlo Ersek Cc: Liming Gao Cc: Michael D Kinney Cc: Ray Ni Cc: Jian J Wang Cc: Hao A Wu Cc: Jiewen Yao Signed-off-by: Michael Kubacki --- MdeModulePkg/MdeModulePkg.dec | 12= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf | 2= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf | 2= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf | 20= +- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf | 2= + MdeModulePkg/Include/Guid/SmmVariableCommon.h | 29= +- MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h | 32= +- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h | 51= ++ MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c | 50= +- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c | 153= ++++++ MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c | 189= +++++++- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 512= +++++++++++++++++++- 12 files changed, 1013 insertions(+), 41 deletions(-) diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index d6bac974da..3ef8fe7644 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -641,6 +641,18 @@ # @Prompt Enable Device Path From Text support. gEfiMdeModulePkgTokenSpaceGuid.PcdDevicePathSupportDevicePathFromText|TR= UE|BOOLEAN|0x00010038 =20 + ## Indicates if the UEFI variable runtime cache should be enabled. + # This setting only applies if SMM variables are enabled. When enabled,= all variable + # data for Runtime Service GetVariable () and GetNextVariableName () ca= lls is retrieved + # from a runtime data buffer referred to as the "runtime cache". An SMI= is not triggered + # at all for these requests. Variables writes still trigger an SMI. Thi= s can greatly + # reduce overall system SMM usage as most boots tend to issue far more = variable reads + # than writes.

+ # TRUE - The UEFI variable runtime cache is enabled.
+ # FALSE - The UEFI variable runtime cache is disabled.
+ # @Prompt Enable the UEFI variable runtime cache. + gEfiMdeModulePkgTokenSpaceGuid.PcdEnableVariableRuntimeCache|FALSE|BOOLE= AN|0x00010039 + ## Indicates if the statistics about variable usage will be collected. T= his information is # stored as a vendor configuration table into the EFI system table. # Set this PCD to TRUE to use VariableInfo application in MdeModulePkg\= Application directory to get diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.= inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf index 08a5490787..ceea5d1ff9 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf @@ -40,6 +40,8 @@ VariableNonVolatile.h VariableParsing.c VariableParsing.h + VariableRuntimeCache.c + VariableRuntimeCache.h PrivilegePolymorphic.h Measurement.c TcgMorLockDxe.c diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf b/M= deModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf index 6dc2721b81..bc3033588d 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf @@ -49,6 +49,8 @@ VariableNonVolatile.h VariableParsing.c VariableParsing.h + VariableRuntimeCache.c + VariableRuntimeCache.h VarCheck.c Variable.h PrivilegePolymorphic.h diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD= xe.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.i= nf index 14894e6f13..b5a779a233 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf @@ -13,7 +13,7 @@ # may not be modified without authorization. If platform fails to protect= these resources, # the authentication service provided in this driver will be broken, and = the behavior is undefined. # -# Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent # ## @@ -39,6 +39,10 @@ VariableSmmRuntimeDxe.c PrivilegePolymorphic.h Measurement.c + VariableParsing.c + VariableParsing.h + VariableRuntimeCache.c + VariableRuntimeCache.h =20 [Packages] MdePkg/MdePkg.dec @@ -65,7 +69,21 @@ gEdkiiVariableLockProtocolGuid ## PRODUCES gEdkiiVarCheckProtocolGuid ## PRODUCES =20 +[FeaturePcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdEnableVariableRuntimeCache #= # CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics #= # CONSUMES + [Guids] + ## PRODUCES ## GUID # Signature of Variable store header + ## CONSUMES ## GUID # Signature of Variable store header + ## SOMETIMES_PRODUCES ## SystemTable + gEfiAuthenticatedVariableGuid + + ## PRODUCES ## GUID # Signature of Variable store header + ## CONSUMES ## GUID # Signature of Variable store header + ## SOMETIMES_PRODUCES ## SystemTable + gEfiVariableGuid + gEfiEventVirtualAddressChangeGuid ## CONSUMES ## Event gEfiEventExitBootServicesGuid ## CONSUMES ## Event ## CONSUMES ## GUID # Locate protocol diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneM= m.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf index f8a3742959..6e17f6cdf5 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf @@ -49,6 +49,8 @@ VariableNonVolatile.h VariableParsing.c VariableParsing.h + VariableRuntimeCache.c + VariableRuntimeCache.h VarCheck.c Variable.h PrivilegePolymorphic.h diff --git a/MdeModulePkg/Include/Guid/SmmVariableCommon.h b/MdeModulePkg/I= nclude/Guid/SmmVariableCommon.h index c527a59891..ceef44dfd2 100644 --- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h +++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h @@ -1,7 +1,7 @@ /** @file The file defined some common structures used for communicating between S= MM variable module and SMM variable wrapper module. =20 -Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.
+Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #ifndef _SMM_VARIABLE_COMMON_H_ #define _SMM_VARIABLE_COMMON_H_ =20 +#include #include =20 #define EFI_SMM_VARIABLE_WRITE_GUID \ @@ -66,6 +67,16 @@ typedef struct { #define SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET 10 =20 #define SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE 11 +// +// The payload for this function is SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIA= BLE_CACHE_CONTEXT +// +#define SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT 12 + +#define SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE 13 +// +// The payload for this function is SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_C= ACHE_INFO +// +#define SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO 14 =20 /// /// Size of SMM communicate header, without including the payload. @@ -120,4 +131,20 @@ typedef struct { UINTN VariablePayloadSize; } SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE; =20 +typedef struct { + BOOLEAN *ReadLock; + BOOLEAN *PendingUpdate; + BOOLEAN *HobFlushComplete; + VARIABLE_STORE_HEADER *RuntimeHobCache; + VARIABLE_STORE_HEADER *RuntimeNvCache; + VARIABLE_STORE_HEADER *RuntimeVolatileCache; +} SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT; + +typedef struct { + UINTN TotalHobStorageSize; + UINTN TotalNvStorageSize; + UINTN TotalVolatileStorageSize; + BOOLEAN AuthenticatedVariableUsage; +} SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO; + #endif // _SMM_VARIABLE_COMMON_H_ diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h b/MdeMod= ulePkg/Universal/Variable/RuntimeDxe/Variable.h index fb574b2e32..0b2bb6ae66 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h @@ -64,6 +64,21 @@ typedef enum { VariableStoreTypeMax } VARIABLE_STORE_TYPE; =20 +typedef struct { + UINT32 PendingUpdateOffset; + UINT32 PendingUpdateLength; + VARIABLE_STORE_HEADER *Store; +} VARIABLE_RUNTIME_CACHE; + +typedef struct { + BOOLEAN *ReadLock; + BOOLEAN *PendingUpdate; + BOOLEAN *HobFlushComplete; + VARIABLE_RUNTIME_CACHE VariableRuntimeHobCache; + VARIABLE_RUNTIME_CACHE VariableRuntimeNvCache; + VARIABLE_RUNTIME_CACHE VariableRuntimeVolatileCache; +} VARIABLE_RUNTIME_CACHE_CONTEXT; + typedef struct { VARIABLE_HEADER *CurrPtr; // @@ -79,14 +94,15 @@ typedef struct { } VARIABLE_POINTER_TRACK; =20 typedef struct { - EFI_PHYSICAL_ADDRESS HobVariableBase; - EFI_PHYSICAL_ADDRESS VolatileVariableBase; - EFI_PHYSICAL_ADDRESS NonVolatileVariableBase; - EFI_LOCK VariableServicesLock; - UINT32 ReentrantState; - BOOLEAN AuthFormat; - BOOLEAN AuthSupport; - BOOLEAN EmuNvMode; + EFI_PHYSICAL_ADDRESS HobVariableBase; + EFI_PHYSICAL_ADDRESS VolatileVariableBase; + EFI_PHYSICAL_ADDRESS NonVolatileVariableBase; + VARIABLE_RUNTIME_CACHE_CONTEXT VariableRuntimeCacheContext; + EFI_LOCK VariableServicesLock; + UINT32 ReentrantState; + BOOLEAN AuthFormat; + BOOLEAN AuthSupport; + BOOLEAN EmuNvMode; } VARIABLE_GLOBAL; =20 typedef struct { diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCach= e.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h new file mode 100644 index 0000000000..f9804a1d69 --- /dev/null +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h @@ -0,0 +1,51 @@ +/** @file + The common variable volatile store routines shared by the DXE_RUNTIME va= riable + module and the DXE_SMM variable module. + +Copyright (c) 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _VARIABLE_RUNTIME_CACHE_H_ +#define _VARIABLE_RUNTIME_CACHE_H_ + +#include "Variable.h" + +/** + Copies any pending updates to runtime variable caches. + + @retval EFI_UNSUPPORTED The volatile store to be updated is not = initialized properly. + @retval EFI_SUCCESS The volatile store was updated successfu= lly. + +**/ +EFI_STATUS +FlushPendingRuntimeVariableCacheUpdates ( + VOID + ); + +/** + Synchronizes the runtime variable caches with all pending updates outsid= e runtime. + + Ensures all conditions are met to maintain coherency for runtime cache u= pdates. This function will attempt + to write the given update (and any other pending updates) if the ReadLoc= k is available. Otherwise, the + update is added as a pending update for the given variable store and it = will be flushed to the runtime cache + at the next opportunity the ReadLock is available. + + @param[in] VariableRuntimeCache Variable runtime cache structure for the= runtime cache being synchronized. + @param[in] Offset Offset in bytes to apply the update. + @param[in] Length Length of data in bytes of the update. + + @retval EFI_SUCCESS The update was added as a pending update= successfully. If the variable runtime + cache ReadLock was available, the runtim= e cache was updated successfully. + @retval EFI_UNSUPPORTED The volatile store to be updated is not = initialized properly. + +**/ +EFI_STATUS +SynchronizeRuntimeVariableCache ( + IN VARIABLE_RUNTIME_CACHE *VariableRuntimeCache, + IN UINTN Offset, + IN UINTN Length + ); + +#endif diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeMod= ulePkg/Universal/Variable/RuntimeDxe/Variable.c index 0bd2f22e1a..29d6aca993 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c @@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "Variable.h" #include "VariableNonVolatile.h" #include "VariableParsing.h" +#include "VariableRuntimeCache.h" =20 VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal; =20 @@ -332,6 +333,12 @@ RecordVarErrorFlag ( // Update the data in NV cache. // *VarErrFlag =3D TempFlag; + Status =3D SynchronizeRuntimeVariableCache ( + &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCa= cheContext.VariableRuntimeNvCache, + (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, + sizeof (TempFlag) + ); + ASSERT_EFI_ERROR (Status); } } } @@ -766,12 +773,24 @@ Reclaim ( =20 Done: if (IsVolatile || mVariableModuleGlobal->VariableGlobal.EmuNvMode) { + Status =3D SynchronizeRuntimeVariableCache ( + &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCach= eContext.VariableRuntimeVolatileCache, + 0, + VariableStoreHeader->Size + ); + ASSERT_EFI_ERROR (Status); FreePool (ValidBuffer); } else { // // For NV variable reclaim, we use mNvVariableCache as the buffer, so = copy the data back. // - CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase, VariableStore= Header->Size); + CopyMem (mNvVariableCache, (UINT8 *) (UINTN) VariableBase, VariableSto= reHeader->Size); + Status =3D SynchronizeRuntimeVariableCache ( + &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCach= eContext.VariableRuntimeNvCache, + 0, + VariableStoreHeader->Size + ); + ASSERT_EFI_ERROR (Status); } =20 return Status; @@ -1614,6 +1633,7 @@ UpdateVariable ( VARIABLE_POINTER_TRACK *Variable; VARIABLE_POINTER_TRACK NvVariable; VARIABLE_STORE_HEADER *VariableStoreHeader; + VARIABLE_RUNTIME_CACHE *VolatileCacheInstance; UINT8 *BufferForMerge; UINTN MergedBufSize; BOOLEAN DataReady; @@ -2275,6 +2295,23 @@ UpdateVariable ( } =20 Done: + if (!EFI_ERROR (Status)) { + if (Variable->Volatile) { + VolatileCacheInstance =3D &(mVariableModuleGlobal->VariableGlobal.Va= riableRuntimeCacheContext.VariableRuntimeVolatileCache); + } else { + VolatileCacheInstance =3D &(mVariableModuleGlobal->VariableGlobal.Va= riableRuntimeCacheContext.VariableRuntimeNvCache); + } + + if (VolatileCacheInstance->Store !=3D NULL) { + Status =3D SynchronizeRuntimeVariableCache ( + VolatileCacheInstance, + 0, + VolatileCacheInstance->Store->Size + ); + ASSERT_EFI_ERROR (Status); + } + } + return Status; } =20 @@ -3200,6 +3237,14 @@ FlushHobVariableToFlash ( ErrorFlag =3D TRUE; } } + if (mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= VariableRuntimeHobCache.Store !=3D NULL) { + Status =3D SynchronizeRuntimeVariableCache ( + &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCa= cheContext.VariableRuntimeHobCache, + 0, + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCac= heContext.VariableRuntimeHobCache.Store->Size + ); + ASSERT_EFI_ERROR (Status); + } if (ErrorFlag) { // // We still have HOB variable(s) not flushed in flash. @@ -3210,6 +3255,9 @@ FlushHobVariableToFlash ( // All HOB variables have been flushed in flash. // DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been fl= ushed in flash.\n")); + if (mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContex= t.HobFlushComplete !=3D NULL) { + *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContex= t.HobFlushComplete) =3D TRUE; + } if (!AtRuntime ()) { FreePool ((VOID *) VariableStoreHeader); } diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCach= e.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c new file mode 100644 index 0000000000..bc93cc07d2 --- /dev/null +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c @@ -0,0 +1,153 @@ +/** @file + Functions related to managing the UEFI variable runtime cache. This file= should only include functions + used by the SMM UEFI variable driver. + + Caution: This module requires additional review when modified. + This driver will have external input - variable data. They may be input = in SMM mode. + This external input must be validated carefully to avoid security issue = like + buffer overflow, integer overflow. + +Copyright (c) 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "VariableParsing.h" +#include "VariableRuntimeCache.h" + +extern VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal; +extern VARIABLE_STORE_HEADER *mNvVariableCache; + +/** + Copies any pending updates to runtime variable caches. + + @retval EFI_UNSUPPORTED The volatile store to be updated is not = initialized properly. + @retval EFI_SUCCESS The volatile store was updated successfu= lly. + +**/ +EFI_STATUS +FlushPendingRuntimeVariableCacheUpdates ( + VOID + ) +{ + VARIABLE_RUNTIME_CACHE_CONTEXT *VariableRuntimeCacheContext; + + VariableRuntimeCacheContext =3D &mVariableModuleGlobal->VariableGlobal.V= ariableRuntimeCacheContext; + + if (VariableRuntimeCacheContext->VariableRuntimeNvCache.Store =3D=3D NUL= L || + VariableRuntimeCacheContext->VariableRuntimeVolatileCache.Store =3D= =3D NULL || + VariableRuntimeCacheContext->PendingUpdate =3D=3D NULL) { + return EFI_UNSUPPORTED; + } + + if (*(VariableRuntimeCacheContext->PendingUpdate)) { + if (VariableRuntimeCacheContext->VariableRuntimeHobCache.Store !=3D NU= LL && + mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0) { + CopyMem ( + (VOID *) ( + ((UINT8 *) (UINTN) VariableRuntimeCacheContext->VariableRuntimeH= obCache.Store) + + VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpda= teOffset + ), + (VOID *) ( + ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVari= ableBase) + + VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpda= teOffset + ), + VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdate= Length + ); + VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateLe= ngth =3D 0; + VariableRuntimeCacheContext->VariableRuntimeHobCache.PendingUpdateOf= fset =3D 0; + } + + CopyMem ( + (VOID *) ( + ((UINT8 *) (UINTN) VariableRuntimeCacheContext->VariableRuntimeNvC= ache.Store) + + VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateO= ffset + ), + (VOID *) ( + ((UINT8 *) (UINTN) mNvVariableCache) + + VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateO= ffset + ), + VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateLen= gth + ); + VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateLengt= h =3D 0; + VariableRuntimeCacheContext->VariableRuntimeNvCache.PendingUpdateOffse= t =3D 0; + + CopyMem ( + (VOID *) ( + ((UINT8 *) (UINTN) VariableRuntimeCacheContext->VariableRuntimeVol= atileCache.Store) + + VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingU= pdateOffset + ), + (VOID *) ( + ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileV= ariableBase) + + VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingU= pdateOffset + ), + VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpd= ateLength + ); + VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdat= eLength =3D 0; + VariableRuntimeCacheContext->VariableRuntimeVolatileCache.PendingUpdat= eOffset =3D 0; + *(VariableRuntimeCacheContext->PendingUpdate) =3D FALSE; + } + + return EFI_SUCCESS; +} + +/** + Synchronizes the runtime variable caches with all pending updates outsid= e runtime. + + Ensures all conditions are met to maintain coherency for runtime cache u= pdates. This function will attempt + to write the given update (and any other pending updates) if the ReadLoc= k is available. Otherwise, the + update is added as a pending update for the given variable store and it = will be flushed to the runtime cache + at the next opportunity the ReadLock is available. + + @param[in] VariableRuntimeCache Variable runtime cache structure for the= runtime cache being synchronized. + @param[in] Offset Offset in bytes to apply the update. + @param[in] Length Length of data in bytes of the update. + + @retval EFI_SUCCESS The update was added as a pending update= successfully. If the variable runtime + cache ReadLock was available, the runtim= e cache was updated successfully. + @retval EFI_UNSUPPORTED The volatile store to be updated is not = initialized properly. + +**/ +EFI_STATUS +SynchronizeRuntimeVariableCache ( + IN VARIABLE_RUNTIME_CACHE *VariableRuntimeCache, + IN UINTN Offset, + IN UINTN Length + ) +{ + if (VariableRuntimeCache =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } else if (VariableRuntimeCache->Store =3D=3D NULL) { + // The runtime cache may not be active or allocated yet. + // In either case, return EFI_SUCCESS instead of EFI_NOT_AVAILABLE_YET. + return EFI_SUCCESS; + } + + if (mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Pe= ndingUpdate =3D=3D NULL || + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Re= adLock =3D=3D NULL) { + return EFI_UNSUPPORTED; + } + + if (*(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= PendingUpdate) && + VariableRuntimeCache->PendingUpdateLength > 0) { + VariableRuntimeCache->PendingUpdateLength =3D + (UINT32) ( + MAX ( + (UINTN) (VariableRuntimeCache->PendingUpdateOffset + VariableRun= timeCache->PendingUpdateLength), + Offset + Length + ) - MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset, Offset) + ); + VariableRuntimeCache->PendingUpdateOffset =3D + (UINT32) MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset, Off= set); + } else { + VariableRuntimeCache->PendingUpdateLength =3D (UINT32) Length; + VariableRuntimeCache->PendingUpdateOffset =3D (UINT32) Offset; + } + *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Pend= ingUpdate) =3D TRUE; + + if (*(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= ReadLock) =3D=3D FALSE) { + return FlushPendingRuntimeVariableCacheUpdates (); + } + + return EFI_SUCCESS; +} diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/Mde= ModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c index 5e24bc4a62..caca5c3241 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c @@ -31,6 +31,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include "Variable.h" #include "VariableParsing.h" +#include "VariableRuntimeCache.h" + +extern VARIABLE_STORE_HEADER *mNvVariableCache; =20 BOOLEAN mAtRuntime = =3D FALSE; UINT8 *mVariableBufferPaylo= ad =3D NULL; @@ -451,25 +454,29 @@ SmmVariableGetStatistics ( EFI_STATUS EFIAPI SmmVariableHandler ( - IN EFI_HANDLE DispatchHandle, - IN CONST VOID *RegisterContext, - IN OUT VOID *CommBuffer, - IN OUT UINTN *CommBufferSize + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *RegisterContext, + IN OUT VOID *CommBuffer, + IN OUT UINTN *CommBufferSize ) { - EFI_STATUS Status; - SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHea= der; - SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader; - SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName; - SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo; - SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *GetPayloadSize; - VARIABLE_INFO_ENTRY *VariableInfo; - SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock; - SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProper= ty; - UINTN InfoSize; - UINTN NameBufferSize; - UINTN CommBufferPayloadSize; - UINTN TempCommBufferSize; + EFI_STATUS Status; + SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunc= tionHeader; + SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHead= er; + SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariable= Name; + SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableIn= fo; + SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *GetPayloadSize; + SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *RuntimeVariable= CacheContext; + SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *GetRuntimeCache= Info; + SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock; + SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariablePro= perty; + VARIABLE_INFO_ENTRY *VariableInfo; + VARIABLE_RUNTIME_CACHE_CONTEXT *VariableCacheCo= ntext; + VARIABLE_STORE_HEADER *VariableCache; + UINTN InfoSize; + UINTN NameBufferSize; + UINTN CommBufferPayloa= dSize; + UINTN TempCommBufferSi= ze; =20 // // If input is invalid, stop processing this SMI @@ -789,6 +796,154 @@ SmmVariableHandler ( ); CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, Co= mmBufferPayloadSize); break; + case SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT: + if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME= _VARIABLE_CACHE_CONTEXT)) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: SMM communi= cation buffer size invalid!\n")); + Status =3D EFI_ACCESS_DENIED; + goto EXIT; + } + if (mEndOfDxe) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Cannot init= context after end of DXE!\n")); + Status =3D EFI_ACCESS_DENIED; + goto EXIT; + } + + // + // Copy the input communicate buffer payload to the pre-allocated SM= M variable payload buffer. + // + CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, Co= mmBufferPayloadSize); + RuntimeVariableCacheContext =3D (SMM_VARIABLE_COMMUNICATE_RUNTIME_VA= RIABLE_CACHE_CONTEXT *) mVariableBufferPayload; + + // + // Verify required runtime cache buffers are provided. + // + if (RuntimeVariableCacheContext->RuntimeVolatileCache =3D=3D NULL || + RuntimeVariableCacheContext->RuntimeNvCache =3D=3D NULL || + RuntimeVariableCacheContext->PendingUpdate =3D=3D NULL || + RuntimeVariableCacheContext->ReadLock =3D=3D NULL || + RuntimeVariableCacheContext->HobFlushComplete =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Required ru= ntime cache buffer is NULL!\n")); + Status =3D EFI_ACCESS_DENIED; + goto EXIT; + } + + // + // Verify minimum size requirements for the runtime variable store b= uffers. + // + if ((RuntimeVariableCacheContext->RuntimeHobCache !=3D NULL && + RuntimeVariableCacheContext->RuntimeHobCache->Size < sizeof (VAR= IABLE_STORE_HEADER)) || + RuntimeVariableCacheContext->RuntimeVolatileCache->Size < sizeof= (VARIABLE_STORE_HEADER) || + RuntimeVariableCacheContext->RuntimeNvCache->Size < sizeof (VARI= ABLE_STORE_HEADER)) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: A runtime c= ache buffer size is invalid!\n")); + Status =3D EFI_ACCESS_DENIED; + goto EXIT; + } + + // + // Verify runtime buffers do not overlap with SMRAM ranges. + // + if (RuntimeVariableCacheContext->RuntimeHobCache !=3D NULL && + !VariableSmmIsBufferOutsideSmmValid ( + (UINTN) RuntimeVariableCacheContext->RuntimeHobCache, + (UINTN) RuntimeVariableCacheContext->RuntimeHobCache->Size)) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Runtime HOB= cache buffer in SMRAM or overflow!\n")); + Status =3D EFI_ACCESS_DENIED; + goto EXIT; + } + if (!VariableSmmIsBufferOutsideSmmValid ( + (UINTN) RuntimeVariableCacheContext->RuntimeVolatileCache, + (UINTN) RuntimeVariableCacheContext->RuntimeVolatileCache->Siz= e)) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Runtime vol= atile cache buffer in SMRAM or overflow!\n")); + Status =3D EFI_ACCESS_DENIED; + goto EXIT; + } + if (!VariableSmmIsBufferOutsideSmmValid ( + (UINTN) RuntimeVariableCacheContext->RuntimeNvCache, + (UINTN) RuntimeVariableCacheContext->RuntimeNvCache->Size)) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Runtime non= -volatile cache buffer in SMRAM or overflow!\n")); + Status =3D EFI_ACCESS_DENIED; + goto EXIT; + } + if (!VariableSmmIsBufferOutsideSmmValid ( + (UINTN) RuntimeVariableCacheContext->PendingUpdate, + sizeof (*(RuntimeVariableCacheContext->PendingUpdate)))) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Runtime cac= he pending update buffer in SMRAM or overflow!\n")); + Status =3D EFI_ACCESS_DENIED; + goto EXIT; + } + if (!VariableSmmIsBufferOutsideSmmValid ( + (UINTN) RuntimeVariableCacheContext->ReadLock, + sizeof (*(RuntimeVariableCacheContext->ReadLock)))) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Runtime cac= he read lock buffer in SMRAM or overflow!\n")); + Status =3D EFI_ACCESS_DENIED; + goto EXIT; + } + if (!VariableSmmIsBufferOutsideSmmValid ( + (UINTN) RuntimeVariableCacheContext->HobFlushComplete, + sizeof (*(RuntimeVariableCacheContext->HobFlushComplete)))) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Runtime cac= he HOB flush complete buffer in SMRAM or overflow!\n")); + Status =3D EFI_ACCESS_DENIED; + goto EXIT; + } + + VariableCacheContext =3D &mVariableModuleGlobal->VariableGlobal.Vari= ableRuntimeCacheContext; + VariableCacheContext->VariableRuntimeHobCache.Store =3D Runtime= VariableCacheContext->RuntimeHobCache; + VariableCacheContext->VariableRuntimeVolatileCache.Store =3D Runtime= VariableCacheContext->RuntimeVolatileCache; + VariableCacheContext->VariableRuntimeNvCache.Store =3D Runtime= VariableCacheContext->RuntimeNvCache; + VariableCacheContext->PendingUpdate =3D Runtime= VariableCacheContext->PendingUpdate; + VariableCacheContext->ReadLock =3D Runtime= VariableCacheContext->ReadLock; + VariableCacheContext->HobFlushComplete =3D Runtime= VariableCacheContext->HobFlushComplete; + + // Set up the intial pending request since the RT cache needs to be = in sync with SMM cache + VariableCacheContext->VariableRuntimeHobCache.PendingUpdateOffset = =3D 0; + VariableCacheContext->VariableRuntimeHobCache.PendingUpdateLength = =3D 0; + if (mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0 && + VariableCacheContext->VariableRuntimeHobCache.Store !=3D NULL) { + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mVariableModul= eGlobal->VariableGlobal.HobVariableBase; + VariableCacheContext->VariableRuntimeHobCache.PendingUpdateLength = =3D (UINT32) ((UINTN) GetEndPointer (VariableCache) - (UINTN) VariableCache= ); + CopyGuid (&(VariableCacheContext->VariableRuntimeHobCache.Store->S= ignature), &(VariableCache->Signature)); + } + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mVariableModule= Global->VariableGlobal.VolatileVariableBase; + VariableCacheContext->VariableRuntimeVolatileCache.PendingUpdateOffs= et =3D 0; + VariableCacheContext->VariableRuntimeVolatileCache.PendingUpdateLeng= th =3D (UINT32) ((UINTN) GetEndPointer (VariableCache) - (UINTN) Variable= Cache); + CopyGuid (&(VariableCacheContext->VariableRuntimeVolatileCache.Store= ->Signature), &(VariableCache->Signature)); + + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mNvVariableCach= e; + VariableCacheContext->VariableRuntimeNvCache.PendingUpdateOffset =3D= 0; + VariableCacheContext->VariableRuntimeNvCache.PendingUpdateLength =3D= (UINT32) ((UINTN) GetEndPointer (VariableCache) - (UINTN) VariableCache); + CopyGuid (&(VariableCacheContext->VariableRuntimeNvCache.Store->Sign= ature), &(VariableCache->Signature)); + + *(VariableCacheContext->PendingUpdate) =3D TRUE; + *(VariableCacheContext->ReadLock) =3D FALSE; + *(VariableCacheContext->HobFlushComplete) =3D FALSE; + + Status =3D EFI_SUCCESS; + break; + case SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE: + Status =3D FlushPendingRuntimeVariableCacheUpdates (); + break; + case SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO: + if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUN= TIME_CACHE_INFO)) { + DEBUG ((DEBUG_ERROR, "GetRuntimeCacheInfo: SMM communication buffe= r size invalid!\n")); + return EFI_SUCCESS; + } + GetRuntimeCacheInfo =3D (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_= INFO *) SmmVariableFunctionHeader->Data; + + if (mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0) { + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mVariableModul= eGlobal->VariableGlobal.HobVariableBase; + GetRuntimeCacheInfo->TotalHobStorageSize =3D VariableCache->Size; + } else { + GetRuntimeCacheInfo->TotalHobStorageSize =3D 0; + } + + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mVariableModule= Global->VariableGlobal.VolatileVariableBase; + GetRuntimeCacheInfo->TotalVolatileStorageSize =3D VariableCache->Siz= e; + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mNvVariableCach= e; + GetRuntimeCacheInfo->TotalNvStorageSize =3D (UINTN) VariableCache->S= ize; + GetRuntimeCacheInfo->AuthenticatedVariableUsage =3D mVariableModuleG= lobal->VariableGlobal.AuthFormat; + + Status =3D EFI_SUCCESS; + break; =20 default: Status =3D EFI_UNSUPPORTED; diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD= xe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c index 0a1888e5ef..3dee05fded 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c @@ -13,7 +13,7 @@ =20 InitCommunicateBuffer() is really function to check the variable data si= ze. =20 -Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
+Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -39,6 +39,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include =20 #include "PrivilegePolymorphic.h" +#include "VariableParsing.h" =20 EFI_HANDLE mHandle =3D NULL; EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable =3D NULL; @@ -46,8 +47,19 @@ EFI_EVENT mVirtualAddressChangeEv= ent =3D NULL; EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication =3D NULL; UINT8 *mVariableBuffer =3D NULL; UINT8 *mVariableBufferPhysical =3D NULL; +VARIABLE_INFO_ENTRY *mVariableInfo =3D NULL; +VARIABLE_STORE_HEADER *mVariableRuntimeHobCacheBuffer = =3D NULL; +VARIABLE_STORE_HEADER *mVariableRuntimeNvCacheBuffer = =3D NULL; +VARIABLE_STORE_HEADER *mVariableRuntimeVolatileCacheBuffer = =3D NULL; UINTN mVariableBufferSize; +UINTN mVariableRuntimeHobCacheBufferSize; +UINTN mVariableRuntimeNvCacheBufferSize; +UINTN mVariableRuntimeVolatileCacheBufferSize; UINTN mVariableBufferPayloadSize; +BOOLEAN mVariableRuntimeCachePendingUpdate; +BOOLEAN mVariableRuntimeCacheReadLock; +BOOLEAN mVariableAuthFormat; +BOOLEAN mHobFlushComplete; EFI_LOCK mVariableServicesLock; EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock; EDKII_VAR_CHECK_PROTOCOL mVarCheck; @@ -107,6 +119,72 @@ ReleaseLockOnlyAtBootTime ( } } =20 +/** + Return TRUE if ExitBootServices () has been called. + + @retval TRUE If ExitBootServices () has been called. FALSE if ExitBootSe= rvices () has not been called. +**/ +BOOLEAN +AtRuntime ( + VOID + ) +{ + return EfiAtRuntime (); +} + +/** + Initialize the variable cache buffer as an empty variable store. + + @param[out] VariableCacheBuffer A pointer to pointer of a cache = variable store. + @param[in,out] TotalVariableCacheSize On input, the minimum size neede= d for the UEFI variable store cache + buffer that is allocated. On out= put, the actual size of the buffer allocated. + If TotalVariableCacheSize is zer= o, a buffer will not be allocated and the + function will return with EFI_SU= CCESS. + + @retval EFI_SUCCESS The variable cache was allocated and ini= tialized successfully. + @retval EFI_INVALID_PARAMETER A given pointer is NULL or an invalid va= riable store size was specified. + @retval EFI_OUT_OF_RESOURCES Insufficient resources are available to = allocate the variable store cache buffer. + +**/ +EFI_STATUS +InitVariableCache ( + OUT VARIABLE_STORE_HEADER **VariableCacheBuffer, + IN OUT UINTN *TotalVariableCacheSize + ) +{ + VARIABLE_STORE_HEADER *VariableCacheStorePtr; + + if (TotalVariableCacheSize =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + if (*TotalVariableCacheSize =3D=3D 0) { + return EFI_SUCCESS; + } + if (VariableCacheBuffer =3D=3D NULL || *TotalVariableCacheSize < sizeof = (VARIABLE_STORE_HEADER)) { + return EFI_INVALID_PARAMETER; + } + *TotalVariableCacheSize =3D ALIGN_VALUE (*TotalVariableCacheSize, sizeof= (UINT32)); + + // + // Allocate NV Storage Cache and initialize it to all 1's (like an erase= d FV) + // + *VariableCacheBuffer =3D (VARIABLE_STORE_HEADER *) AllocateRuntimePages= ( + EFI_SIZE_TO_PAGES (*TotalVariableCacheSize) + ); + if (*VariableCacheBuffer =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + VariableCacheStorePtr =3D *VariableCacheBuffer; + SetMem32 ((VOID *) VariableCacheStorePtr, *TotalVariableCacheSize, (UINT= 32) 0xFFFFFFFF); + + ZeroMem ((VOID *) VariableCacheStorePtr, sizeof (VARIABLE_STORE_HEADER)); + VariableCacheStorePtr->Size =3D (UINT32) *TotalVariableCacheSize; + VariableCacheStorePtr->Format =3D VARIABLE_STORE_FORMATTED; + VariableCacheStorePtr->State =3D VARIABLE_STORE_HEALTHY; + + return EFI_SUCCESS; +} + /** Initialize the communicate buffer using DataSize and Function. =20 @@ -425,7 +503,169 @@ Done: } =20 /** - This code finds variable in storage blocks (Volatile or Non-Volatile). + Signals SMM to synchronize any pending variable updates with the runtime= cache(s). + +**/ +VOID +SyncRuntimeCache ( + VOID + ) +{ + // + // Init the communicate buffer. The buffer data size is: + // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE. + // + InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE= ); + + // + // Send data to SMM. + // + SendCommunicateBuffer (0); +} + +/** + Check whether a SMI must be triggered to retrieve pending cache updates. + + If the variable HOB was finished being flushed since the last check for = a runtime cache update, this function + will prevent the HOB cache from being used for future runtime cache hits. + +**/ +VOID +CheckForRuntimeCacheSync ( + VOID + ) +{ + if (mVariableRuntimeCachePendingUpdate) { + SyncRuntimeCache (); + } + ASSERT (!mVariableRuntimeCachePendingUpdate); + + // + // The HOB variable data may have finished being flushed in the runtime = cache sync update + // + if (mHobFlushComplete && mVariableRuntimeHobCacheBuffer !=3D NULL) { + if (!EfiAtRuntime ()) { + FreePages (mVariableRuntimeHobCacheBuffer, EFI_SIZE_TO_PAGES (mVaria= bleRuntimeHobCacheBufferSize)); + } + mVariableRuntimeHobCacheBuffer =3D NULL; + } +} + +/** + Finds the given variable in a runtime cache variable store. + + Caution: This function may receive untrusted input. + The data size is external input, so this function will validate it caref= ully to avoid buffer overflow. + + @param[in] VariableName Name of Variable to be found. + @param[in] VendorGuid Variable vendor GUID. + @param[out] Attributes Attribute value of the variable found. + @param[in, out] DataSize Size of Data found. If size is less t= han the + data, this value contains the require= d size. + @param[out] Data Data pointer. + + @retval EFI_SUCCESS Found the specified variable. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_NOT_FOUND The specified variable could not be f= ound. + +**/ +EFI_STATUS +FindVariableInRuntimeCache ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + OUT UINT32 *Attributes OPTIONAL, + IN OUT UINTN *DataSize, + OUT VOID *Data OPTIONAL + ) +{ + EFI_STATUS Status; + UINTN TempDataSize; + VARIABLE_POINTER_TRACK RtPtrTrack; + VARIABLE_STORE_TYPE StoreType; + VARIABLE_STORE_HEADER *VariableStoreList[VariableStoreTypeMax]; + + Status =3D EFI_NOT_FOUND; + + if (VariableName =3D=3D NULL || VendorGuid =3D=3D NULL || DataSize =3D= =3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // The UEFI specification restricts Runtime Services callers from invoki= ng the same or certain other Runtime Service + // functions prior to completion and return from a previous Runtime Serv= ice call. These restrictions prevent + // a GetVariable () or GetNextVariable () call from being issued until a= prior call has returned. The runtime + // cache read lock should always be free when entering this function. + // + ASSERT (!mVariableRuntimeCacheReadLock); + + mVariableRuntimeCacheReadLock =3D TRUE; + CheckForRuntimeCacheSync (); + + if (!mVariableRuntimeCachePendingUpdate) { + // + // 0: Volatile, 1: HOB, 2: Non-Volatile. + // The index and attributes mapping must be kept in this order as Find= Variable + // makes use of this mapping to implement search algorithm. + // + VariableStoreList[VariableStoreTypeVolatile] =3D mVariableRuntimeVolat= ileCacheBuffer; + VariableStoreList[VariableStoreTypeHob] =3D mVariableRuntimeHobCa= cheBuffer; + VariableStoreList[VariableStoreTypeNv] =3D mVariableRuntimeNvCac= heBuffer; + + for (StoreType =3D (VARIABLE_STORE_TYPE) 0; StoreType < VariableStoreT= ypeMax; StoreType++) { + if (VariableStoreList[StoreType] =3D=3D NULL) { + continue; + } + + RtPtrTrack.StartPtr =3D GetStartPointer (VariableStoreList[StoreType= ]); + RtPtrTrack.EndPtr =3D GetEndPointer (VariableStoreList[StoreType= ]); + RtPtrTrack.Volatile =3D (BOOLEAN) (StoreType =3D=3D VariableStoreTyp= eVolatile); + + Status =3D FindVariableEx (VariableName, VendorGuid, FALSE, &RtPtrTr= ack, mVariableAuthFormat); + if (!EFI_ERROR (Status)) { + break; + } + } + + if (!EFI_ERROR (Status)) { + // + // Get data size + // + TempDataSize =3D DataSizeOfVariable (RtPtrTrack.CurrPtr, mVariableAu= thFormat); + ASSERT (TempDataSize !=3D 0); + + if (*DataSize >=3D TempDataSize) { + if (Data =3D=3D NULL) { + Status =3D EFI_INVALID_PARAMETER; + goto Done; + } + + CopyMem (Data, GetVariableDataPtr (RtPtrTrack.CurrPtr, mVariableAu= thFormat), TempDataSize); + if (Attributes !=3D NULL) { + *Attributes =3D RtPtrTrack.CurrPtr->Attributes; + } + + *DataSize =3D TempDataSize; + + UpdateVariableInfo (VariableName, VendorGuid, RtPtrTrack.Volatile,= TRUE, FALSE, FALSE, TRUE, &mVariableInfo); + + Status =3D EFI_SUCCESS; + goto Done; + } else { + *DataSize =3D TempDataSize; + Status =3D EFI_BUFFER_TOO_SMALL; + goto Done; + } + } + } + +Done: + mVariableRuntimeCacheReadLock =3D FALSE; + + return Status; +} + +/** + Finds the given variable in a variable store in SMM. =20 Caution: This function may receive untrusted input. The data size is external input, so this function will validate it caref= ully to avoid buffer overflow. @@ -437,20 +677,18 @@ Done: data, this value contains the require= d size. @param[out] Data Data pointer. =20 + @retval EFI_SUCCESS Found the specified variable. @retval EFI_INVALID_PARAMETER Invalid parameter. - @retval EFI_SUCCESS Find the specified variable. - @retval EFI_NOT_FOUND Not found. - @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result. + @retval EFI_NOT_FOUND The specified variable could not be f= ound. =20 **/ EFI_STATUS -EFIAPI -RuntimeServiceGetVariable ( +FindVariableInSmm ( IN CHAR16 *VariableName, IN EFI_GUID *VendorGuid, OUT UINT32 *Attributes OPTIONAL, IN OUT UINTN *DataSize, - OUT VOID *Data + OUT VOID *Data OPTIONAL ) { EFI_STATUS Status; @@ -474,8 +712,6 @@ RuntimeServiceGetVariable ( return EFI_INVALID_PARAMETER; } =20 - AcquireLockOnlyAtBootTime(&mVariableServicesLock); - // // Init the communicate buffer. The buffer data size is: // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + = PayloadSize. @@ -488,7 +724,7 @@ RuntimeServiceGetVariable ( } PayloadSize =3D OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Nam= e) + VariableNameSize + TempDataSize; =20 - Status =3D InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSi= ze, SMM_VARIABLE_FUNCTION_GET_VARIABLE); + Status =3D InitCommunicateBuffer ((VOID **) &SmmVariableHeader, PayloadS= ize, SMM_VARIABLE_FUNCTION_GET_VARIABLE); if (EFI_ERROR (Status)) { goto Done; } @@ -534,11 +770,58 @@ RuntimeServiceGetVariable ( } =20 Done: + return Status; +} + +/** + This code finds variable in storage blocks (Volatile or Non-Volatile). + + Caution: This function may receive untrusted input. + The data size is external input, so this function will validate it caref= ully to avoid buffer overflow. + + @param[in] VariableName Name of Variable to be found. + @param[in] VendorGuid Variable vendor GUID. + @param[out] Attributes Attribute value of the variable found. + @param[in, out] DataSize Size of Data found. If size is less t= han the + data, this value contains the require= d size. + @param[out] Data Data pointer. + + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_SUCCESS Find the specified variable. + @retval EFI_NOT_FOUND Not found. + @retval EFI_BUFFER_TO_SMALL DataSize is too small for the result. + +**/ +EFI_STATUS +EFIAPI +RuntimeServiceGetVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + OUT UINT32 *Attributes OPTIONAL, + IN OUT UINTN *DataSize, + OUT VOID *Data + ) +{ + EFI_STATUS Status; + + if (VariableName =3D=3D NULL || VendorGuid =3D=3D NULL || DataSize =3D= =3D NULL) { + return EFI_INVALID_PARAMETER; + } + if (VariableName[0] =3D=3D 0) { + return EFI_NOT_FOUND; + } + + AcquireLockOnlyAtBootTime (&mVariableServicesLock); + if (FeaturePcdGet (PcdEnableVariableRuntimeCache)) { + Status =3D FindVariableInRuntimeCache (VariableName, VendorGuid, Attri= butes, DataSize, Data); + } else { + Status =3D FindVariableInSmm (VariableName, VendorGuid, Attributes, Da= taSize, Data); + } ReleaseLockOnlyAtBootTime (&mVariableServicesLock); + return Status; } =20 - /** This code Finds the Next available variable. =20 @@ -870,6 +1153,17 @@ OnReadyToBoot ( // SendCommunicateBuffer (0); =20 + // + // Install the system configuration table for variable info data captured + // + if (FeaturePcdGet (PcdEnableVariableRuntimeCache) && FeaturePcdGet (PcdV= ariableCollectStatistics)) { + if (mVariableAuthFormat) { + gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, mVar= iableInfo); + } else { + gBS->InstallConfigurationTable (&gEfiVariableGuid, mVariableInfo); + } + } + gBS->CloseEvent (Event); } =20 @@ -893,6 +1187,9 @@ VariableAddressChangeEvent ( { EfiConvertPointer (0x0, (VOID **) &mVariableBuffer); EfiConvertPointer (0x0, (VOID **) &mSmmCommunication); + EfiConvertPointer (EFI_OPTIONAL_PTR, (VOID **) &mVariableRuntimeHobCache= Buffer); + EfiConvertPointer (EFI_OPTIONAL_PTR, (VOID **) &mVariableRuntimeNvCacheB= uffer); + EfiConvertPointer (EFI_OPTIONAL_PTR, (VOID **) &mVariableRuntimeVolatile= CacheBuffer); } =20 /** @@ -969,6 +1266,159 @@ Done: return Status; } =20 +/** + This code gets information needed from SMM for runtime cache initializat= ion. + + @param[out] TotalHobStorageSize Output pointer for the total HOB= storage size in bytes. + @param[out] TotalNvStorageSize Output pointer for the total non= -volatile storage size in bytes. + @param[out] TotalVolatileStorageSize Output pointer for the total vol= atile storage size in bytes. + @param[out] AuthenticatedVariableUsage Output pointer that indicates if= authenticated variables are to be used. + + @retval EFI_SUCCESS Retrieved the size successfully. + @retval EFI_INVALID_PARAMETER TotalNvStorageSize parameter is = NULL. + @retval EFI_OUT_OF_RESOURCES The memory resources needed for = a CommBuffer are not available. + @retval Others Could not retrieve the size succ= essfully. + +**/ +EFI_STATUS +GetRuntimeCacheInfo ( + OUT UINTN *TotalHobStorageSize, + OUT UINTN *TotalNvStorageSize, + OUT UINTN *TotalVolatileStorageSize, + OUT BOOLEAN *AuthenticatedVariableUsage + ) +{ + EFI_STATUS Status; + SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *SmmGetRuntimeCacheI= nfo; + EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeade= r; + SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunction= Header; + UINTN CommSize; + UINT8 *CommBuffer; + + SmmGetRuntimeCacheInfo =3D NULL; + CommBuffer =3D mVariableBuffer; + + if (TotalHobStorageSize =3D=3D NULL || TotalNvStorageSize =3D=3D NULL ||= TotalVolatileStorageSize =3D=3D NULL || AuthenticatedVariableUsage =3D=3D = NULL) { + return EFI_INVALID_PARAMETER; + } + + if (CommBuffer =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + AcquireLockOnlyAtBootTime (&mVariableServicesLock); + + CommSize =3D SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEAD= ER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO); + ZeroMem (CommBuffer, CommSize); + + SmmCommunicateHeader =3D (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer; + CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGui= d); + SmmCommunicateHeader->MessageLength =3D SMM_VARIABLE_COMMUNICATE_HEADER_= SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO); + + SmmVariableFunctionHeader =3D (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCom= municateHeader->Data; + SmmVariableFunctionHeader->Function =3D SMM_VARIABLE_FUNCTION_GET_RUNTIM= E_CACHE_INFO; + SmmGetRuntimeCacheInfo =3D (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_I= NFO *) SmmVariableFunctionHeader->Data; + + // + // Send data to SMM. + // + Status =3D mSmmCommunication->Communicate (mSmmCommunication, CommBuffer= , &CommSize); + ASSERT_EFI_ERROR (Status); + if (CommSize <=3D SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) { + Status =3D EFI_BAD_BUFFER_SIZE; + goto Done; + } + + Status =3D SmmVariableFunctionHeader->ReturnStatus; + if (EFI_ERROR (Status)) { + goto Done; + } + + // + // Get data from SMM. + // + *TotalHobStorageSize =3D SmmGetRuntimeCacheInfo->TotalHobStorageSize; + *TotalNvStorageSize =3D SmmGetRuntimeCacheInfo->TotalNvStorageSize; + *TotalVolatileStorageSize =3D SmmGetRuntimeCacheInfo->TotalVolatileStora= geSize; + *AuthenticatedVariableUsage =3D SmmGetRuntimeCacheInfo->AuthenticatedVar= iableUsage; + +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); + return Status; +} + +/** + Sends the runtime variable cache context information to SMM. + + @retval EFI_SUCCESS Retrieved the size successfully. + @retval EFI_INVALID_PARAMETER TotalNvStorageSize parameter is NULL. + @retval EFI_OUT_OF_RESOURCES The memory resources needed for a Comm= Buffer are not available. + @retval Others Could not retrieve the size successful= ly.; + +**/ +EFI_STATUS +SendRuntimeVariableCacheContextToSmm ( + VOID + ) +{ + EFI_STATUS Status; + SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *SmmRuntimeVar= CacheContext; + EFI_SMM_COMMUNICATE_HEADER *SmmCommunicat= eHeader; + SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFu= nctionHeader; + UINTN CommSize; + UINT8 *CommBuffer; + + SmmRuntimeVarCacheContext =3D NULL; + CommBuffer =3D mVariableBuffer; + + if (CommBuffer =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + AcquireLockOnlyAtBootTime (&mVariableServicesLock); + + // + // Init the communicate buffer. The buffer data size is: + // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + = sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT); + // + CommSize =3D SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEAD= ER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT); + ZeroMem (CommBuffer, CommSize); + + SmmCommunicateHeader =3D (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer; + CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGui= d); + SmmCommunicateHeader->MessageLength =3D SMM_VARIABLE_COMMUNICATE_HEADER_= SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT); + + SmmVariableFunctionHeader =3D (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCom= municateHeader->Data; + SmmVariableFunctionHeader->Function =3D SMM_VARIABLE_FUNCTION_INIT_RUNTI= ME_VARIABLE_CACHE_CONTEXT; + SmmRuntimeVarCacheContext =3D (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE= _CACHE_CONTEXT *) SmmVariableFunctionHeader->Data; + + SmmRuntimeVarCacheContext->RuntimeHobCache =3D mVariableRuntimeHobCacheB= uffer; + SmmRuntimeVarCacheContext->RuntimeVolatileCache =3D mVariableRuntimeVola= tileCacheBuffer; + SmmRuntimeVarCacheContext->RuntimeNvCache =3D mVariableRuntimeNvCacheBuf= fer; + SmmRuntimeVarCacheContext->PendingUpdate =3D &mVariableRuntimeCachePendi= ngUpdate; + SmmRuntimeVarCacheContext->ReadLock =3D &mVariableRuntimeCacheReadLock; + SmmRuntimeVarCacheContext->HobFlushComplete =3D &mHobFlushComplete; + + // + // Send data to SMM. + // + Status =3D mSmmCommunication->Communicate (mSmmCommunication, CommBuffer= , &CommSize); + ASSERT_EFI_ERROR (Status); + if (CommSize <=3D SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) { + Status =3D EFI_BAD_BUFFER_SIZE; + goto Done; + } + + Status =3D SmmVariableFunctionHeader->ReturnStatus; + if (EFI_ERROR (Status)) { + goto Done; + } + +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); + return Status; +} + /** Initialize variable service and install Variable Architectural protocol. =20 @@ -985,7 +1435,7 @@ SmmVariableReady ( { EFI_STATUS Status; =20 - Status =3D gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOI= D **)&mSmmVariable); + Status =3D gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOI= D **) &mSmmVariable); if (EFI_ERROR (Status)) { return; } @@ -1007,6 +1457,42 @@ SmmVariableReady ( // mVariableBufferPhysical =3D mVariableBuffer; =20 + if (FeaturePcdGet (PcdEnableVariableRuntimeCache)) { + DEBUG ((DEBUG_INFO, "Variable driver runtime cache is enabled.\n")); + // + // Allocate runtime variable cache memory buffers. + // + Status =3D GetRuntimeCacheInfo ( + &mVariableRuntimeHobCacheBufferSize, + &mVariableRuntimeNvCacheBufferSize, + &mVariableRuntimeVolatileCacheBufferSize, + &mVariableAuthFormat + ); + if (!EFI_ERROR (Status)) { + Status =3D InitVariableCache (&mVariableRuntimeHobCacheBuffer, &mVar= iableRuntimeHobCacheBufferSize); + if (!EFI_ERROR (Status)) { + Status =3D InitVariableCache (&mVariableRuntimeNvCacheBuffer, &mVa= riableRuntimeNvCacheBufferSize); + if (!EFI_ERROR (Status)) { + Status =3D InitVariableCache (&mVariableRuntimeVolatileCacheBuff= er, &mVariableRuntimeVolatileCacheBufferSize); + if (!EFI_ERROR (Status)) { + Status =3D SendRuntimeVariableCacheContextToSmm (); + if (!EFI_ERROR (Status)) { + SyncRuntimeCache (); + } + } + } + } + if (EFI_ERROR (Status)) { + mVariableRuntimeHobCacheBuffer =3D NULL; + mVariableRuntimeNvCacheBuffer =3D NULL; + mVariableRuntimeVolatileCacheBuffer =3D NULL; + } + } + ASSERT_EFI_ERROR (Status); + } else { + DEBUG ((DEBUG_INFO, "Variable driver runtime cache is disabled.\n")); + } + gRT->GetVariable =3D RuntimeServiceGetVariable; gRT->GetNextVariableName =3D RuntimeServiceGetNextVariableName; gRT->SetVariable =3D RuntimeServiceSetVariable; --=20 2.16.2.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#49728): https://edk2.groups.io/g/devel/message/49728 Mute This Topic: https://groups.io/mt/40103026/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-