From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id 2C4D2AC0E27 for ; Fri, 17 May 2024 09:49:52 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=k1zv31tdgBdo16xivSAQVNclcLeDCgkyhGG8UWyRFcQ=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1715939390; v=1; b=GDa53iMRhqc+2WWqnIgJZ9nK3iI2Qw4e7u0DC3Cvsc1ZoWHuTrxo5BvRjGhbvlzznrdd8xqc xc53S3Qp5S//jAXpjXVRFNKGoRVBDMO1VQKvsRw0VLbgLBXiYdsQ5kSiItL7RfgoTXvqUpLRtYv o4jEK1utDzlMXRbXDyaXPvqJ3ErFPDJH80Lybgw7XUxjOHeGb1jo2WMYu8KlwwCnCYxhlyDWyRu uXbYTjtKIi+JG4QTjBYuA0Sk2plMVo8fr2NAlnO0Au9HWhecM2jc9f2i/RcuJtHqOidHe2Pu/jb Rta3TPCkiXyu8LSfe9RzM1z2Ap1BglKMDK7Nf0EWiZFCw== X-Received: by 127.0.0.2 with SMTP id 3e9SYY7687511xNaGPETSuO3; Fri, 17 May 2024 02:49:50 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by mx.groups.io with SMTP id smtpd.web10.36016.1715939370910548931 for ; Fri, 17 May 2024 02:49:50 -0700 X-CSE-ConnectionGUID: 4JxTS9ltQrOLAn9aAJmOvA== X-CSE-MsgGUID: kA4qSNCyTniS2YgLWMA3Jg== X-IronPort-AV: E=McAfee;i="6600,9927,11074"; a="11570307" X-IronPort-AV: E=Sophos;i="6.08,167,1712646000"; d="scan'208";a="11570307" X-Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 May 2024 02:49:50 -0700 X-CSE-ConnectionGUID: D9wiWhEnT5m46wxRG1h3dg== X-CSE-MsgGUID: x4sszRGSRzCe4JHj6px7gQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,167,1712646000"; d="scan'208";a="31739341" X-Received: from unknown (HELO shwdeopenlab702.ccr.corp.intel.com) ([10.239.55.43]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 May 2024 02:49:49 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Ray Ni , Liming Gao , Jiaxin Wu Subject: [edk2-devel] [PATCH 8/9] MdeModulePkg: Refine InitVariableCache() Date: Fri, 17 May 2024 17:49:16 +0800 Message-Id: <20240517094917.513-9-dun.tan@intel.com> In-Reply-To: <20240517094917.513-1-dun.tan@intel.com> References: <20240517094917.513-1-dun.tan@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Fri, 17 May 2024 02:49:50 -0700 Resent-From: dun.tan@intel.com Reply-To: devel@edk2.groups.io,dun.tan@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: QWQVT7Agj1bpzx2Z1l51VPPvx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=GDa53iMR; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io Refine the code logic in InitVariableCache(). In this commit, three times calling of InitVariableCache() for different type cache are merged into one calling. This commit is to make the code looks cleaner and doesn't change any code functionality. Signed-off-by: Dun Tan Cc: Ray Ni Cc: Liming Gao Cc: Jiaxin Wu --- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 198 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------- 1 file changed, 95 insertions(+), 103 deletions(-) diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c index 68a249c5ac..6efe5cee10 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c @@ -144,54 +144,6 @@ AtRuntime ( 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 needed for the UEFI variable store cache - buffer that is allocated. On output, the actual size of the buffer allocated. - If TotalVariableCacheSize is zero, a buffer will not be allocated and the - function will return with EFI_SUCCESS. - - @retval EFI_SUCCESS The variable cache was allocated and initialized successfully. - @retval EFI_INVALID_PARAMETER A given pointer is NULL or an invalid variable 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; - EFI_STATUS Status; - - if (TotalVariableCacheSize == NULL) { - return EFI_INVALID_PARAMETER; - } - - if (*TotalVariableCacheSize == 0) { - return EFI_SUCCESS; - } - - if ((VariableCacheBuffer == NULL) || (*TotalVariableCacheSize < sizeof (VARIABLE_STORE_HEADER))) { - return EFI_INVALID_PARAMETER; - } - - *TotalVariableCacheSize = ALIGN_VALUE (*TotalVariableCacheSize, sizeof (UINT32)); - - VariableCacheStorePtr = *VariableCacheBuffer; - SetMem32 ((VOID *)VariableCacheStorePtr, *TotalVariableCacheSize, (UINT32)0xFFFFFFFF); - - ZeroMem ((VOID *)VariableCacheStorePtr, sizeof (VARIABLE_STORE_HEADER)); - VariableCacheStorePtr->Size = (UINT32)*TotalVariableCacheSize; - VariableCacheStorePtr->Format = VARIABLE_STORE_FORMATTED; - VariableCacheStorePtr->State = VARIABLE_STORE_HEALTHY; - - return EFI_SUCCESS; -} - /** Initialize the communicate buffer using DataSize and Function. @@ -1554,6 +1506,92 @@ Done: return Status; } +/** + Initialize the variable cache buffer as an empty variable store. + + @param[in] VariableCacheBuffer A pointer to pointer of a cache variable store. + @param[in] TotalVariableCacheSize The size needed for the UEFI variable store cache buffer that is allocated. + +**/ +VOID +InitVariableStoreHeader ( + IN VARIABLE_STORE_HEADER *VariableCacheBuffer, + IN UINTN TotalVariableCacheSize + ) +{ + if (TotalVariableCacheSize > 0) { + ASSERT ((VariableCacheBuffer != NULL) && (TotalVariableCacheSize >= sizeof (VARIABLE_STORE_HEADER))); + + SetMem32 ((VOID *)VariableCacheBuffer, TotalVariableCacheSize, (UINT32)0xFFFFFFFF); + ZeroMem ((VOID *)VariableCacheBuffer, sizeof (VARIABLE_STORE_HEADER)); + VariableCacheBuffer->Size = (UINT32)TotalVariableCacheSize; + VariableCacheBuffer->Format = VARIABLE_STORE_FORMATTED; + VariableCacheBuffer->State = VARIABLE_STORE_HEALTHY; + } +} + +/** + Initialize the runtime variable cache related content. + + @retval EFI_SUCCESS Initialize the runtime variable cache related content successfully. + @retval Others Could not initialize the runtime variable cache related content successfully. + +**/ +EFI_STATUS +InitVariableCache ( + VOID + ) +{ + EFI_STATUS Status; + UINTN ExpectedHobCacheSize; + UINTN ExpectedNvCacheSize; + UINTN ExpectedVolatileCacheSize; + UINTN AllocatedHobCacheSize; + UINTN AllocatedNvCacheSize; + UINTN AllocatedVolatileCacheSize; + EFI_HOB_GUID_TYPE *GuidHob; + VARIABLE_RUNTIME_CACHE_INFO *VariableRuntimeCacheHob; + + DEBUG ((DEBUG_INFO, "Variable driver runtime cache is enabled.\n")); + // + // Get needed runtime cache buffer size and check if auth variables are to be used from SMM + // + Status = GetRuntimeCacheInfo ( + &ExpectedHobCacheSize, + &ExpectedNvCacheSize, + &ExpectedVolatileCacheSize, + &mVariableAuthFormat + ); + if (!EFI_ERROR (Status)) { + GuidHob = GetFirstGuidHob (&gEdkiiVariableRuntimeCacheInfoHobGuid); + ASSERT (GuidHob != NULL); + VariableRuntimeCacheHob = GET_GUID_HOB_DATA (GuidHob); + AllocatedHobCacheSize = EFI_PAGES_TO_SIZE (VariableRuntimeCacheHob->RuntimeHobCachePages); + AllocatedNvCacheSize = EFI_PAGES_TO_SIZE (VariableRuntimeCacheHob->RuntimeNvCachePages); + AllocatedVolatileCacheSize = EFI_PAGES_TO_SIZE (VariableRuntimeCacheHob->RuntimeVolatileCachePages); + + ASSERT ( + (AllocatedHobCacheSize >= ExpectedHobCacheSize) && + (AllocatedNvCacheSize >= ExpectedNvCacheSize) && + (AllocatedVolatileCacheSize >= ExpectedVolatileCacheSize) + ); + + mVariableRuntimeHobCacheBuffer = (VARIABLE_STORE_HEADER *)(UINTN)VariableRuntimeCacheHob->RuntimeHobCacheBuffer; + mVariableRuntimeNvCacheBuffer = (VARIABLE_STORE_HEADER *)(UINTN)VariableRuntimeCacheHob->RuntimeNvCacheBuffer; + mVariableRuntimeVolatileCacheBuffer = (VARIABLE_STORE_HEADER *)(UINTN)VariableRuntimeCacheHob->RuntimeVolatileCacheBuffer; + mVariableRuntimeCachePendingUpdate = &VariableRuntimeCacheHob->CacheInfoFlag->PendingUpdate; + mVariableRuntimeCacheReadLock = &VariableRuntimeCacheHob->CacheInfoFlag->ReadLock; + mHobFlushComplete = &VariableRuntimeCacheHob->CacheInfoFlag->HobFlushComplete; + mVariableRuntimeHobCacheBufferSize = AllocatedHobCacheSize; + + InitVariableStoreHeader (mVariableRuntimeHobCacheBuffer, AllocatedHobCacheSize); + InitVariableStoreHeader (mVariableRuntimeNvCacheBuffer, AllocatedNvCacheSize); + InitVariableStoreHeader (mVariableRuntimeVolatileCacheBuffer, AllocatedVolatileCacheSize); + } + + return Status; +} + /** Sends the runtime variable cache context information to SMM. @@ -1640,14 +1678,7 @@ SmmVariableReady ( IN VOID *Context ) { - EFI_STATUS Status; - UINTN RuntimeNvCacheSize; - UINTN RuntimeVolatileCacheSize; - UINTN AllocatedHobCacheSize; - UINTN AllocatedNvCacheSize; - UINTN AllocatedVolatileCacheSize; - EFI_HOB_GUID_TYPE *GuidHob; - VARIABLE_RUNTIME_CACHE_INFO *VariableRuntimeCacheHob; + EFI_STATUS Status; Status = gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable); if (EFI_ERROR (Status)) { @@ -1672,57 +1703,18 @@ SmmVariableReady ( mVariableBufferPhysical = mVariableBuffer; if (FeaturePcdGet (PcdEnableVariableRuntimeCache)) { - DEBUG ((DEBUG_INFO, "Variable driver runtime cache is enabled.\n")); - // - // Get needed runtime cache buffer size and check if auth variables are to be used from SMM - // - Status = GetRuntimeCacheInfo ( - &mVariableRuntimeHobCacheBufferSize, - &RuntimeNvCacheSize, - &RuntimeVolatileCacheSize, - &mVariableAuthFormat - ); + Status = InitVariableCache (); if (!EFI_ERROR (Status)) { - GuidHob = GetFirstGuidHob (&gEdkiiVariableRuntimeCacheInfoHobGuid); - ASSERT (GuidHob != NULL); - VariableRuntimeCacheHob = GET_GUID_HOB_DATA (GuidHob); - AllocatedHobCacheSize = EFI_PAGES_TO_SIZE (VariableRuntimeCacheHob->RuntimeHobCachePages); - AllocatedNvCacheSize = EFI_PAGES_TO_SIZE (VariableRuntimeCacheHob->RuntimeNvCachePages); - AllocatedVolatileCacheSize = EFI_PAGES_TO_SIZE (VariableRuntimeCacheHob->RuntimeVolatileCachePages); - - ASSERT ( - (AllocatedHobCacheSize >= mVariableRuntimeHobCacheBufferSize) && - (AllocatedNvCacheSize >= RuntimeNvCacheSize) && - (AllocatedVolatileCacheSize >= RuntimeVolatileCacheSize) - ); - - mVariableRuntimeHobCacheBuffer = (VARIABLE_STORE_HEADER *)(UINTN)VariableRuntimeCacheHob->RuntimeHobCacheBuffer; - mVariableRuntimeNvCacheBuffer = (VARIABLE_STORE_HEADER *)(UINTN)VariableRuntimeCacheHob->RuntimeNvCacheBuffer; - mVariableRuntimeVolatileCacheBuffer = (VARIABLE_STORE_HEADER *)(UINTN)VariableRuntimeCacheHob->RuntimeVolatileCacheBuffer; - mVariableRuntimeCachePendingUpdate = &VariableRuntimeCacheHob->CacheInfoFlag->PendingUpdate; - mVariableRuntimeCacheReadLock = &VariableRuntimeCacheHob->CacheInfoFlag->ReadLock; - mHobFlushComplete = &VariableRuntimeCacheHob->CacheInfoFlag->HobFlushComplete; - mVariableRuntimeHobCacheBufferSize = AllocatedHobCacheSize; - - Status = InitVariableCache (&mVariableRuntimeHobCacheBuffer, &mVariableRuntimeHobCacheBufferSize); + Status = SendRuntimeVariableCacheContextToSmm (); if (!EFI_ERROR (Status)) { - Status = InitVariableCache (&mVariableRuntimeNvCacheBuffer, &RuntimeNvCacheSize); - if (!EFI_ERROR (Status)) { - Status = InitVariableCache (&mVariableRuntimeVolatileCacheBuffer, &RuntimeVolatileCacheSize); - if (!EFI_ERROR (Status)) { - Status = SendRuntimeVariableCacheContextToSmm (); - if (!EFI_ERROR (Status)) { - SyncRuntimeCache (); - } - } - } + SyncRuntimeCache (); } + } - if (EFI_ERROR (Status)) { - mVariableRuntimeHobCacheBuffer = NULL; - mVariableRuntimeNvCacheBuffer = NULL; - mVariableRuntimeVolatileCacheBuffer = NULL; - } + if (EFI_ERROR (Status)) { + mVariableRuntimeHobCacheBuffer = NULL; + mVariableRuntimeNvCacheBuffer = NULL; + mVariableRuntimeVolatileCacheBuffer = NULL; } ASSERT_EFI_ERROR (Status); -- 2.31.1.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#119025): https://edk2.groups.io/g/devel/message/119025 Mute This Topic: https://groups.io/mt/106150806/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-