From 6ed1ec5946efbfa78c14d21374525323d54d5462 Mon Sep 17 00:00:00 2001 From: lzeng14 Date: Tue, 8 Jan 2013 02:07:14 +0000 Subject: [PATCH] Add the TPL raise/restore code for VariableSmmRuntimeDxe to avoid variable services reentry. Signed-off-by: Star Zeng Reviewed-by: Jiewen Yao git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@14038 6f19259b-4bc3-4df7-8a09-765794883524 --- .../RuntimeDxe/VariableSmmRuntimeDxe.c | 94 +++++++++++++++---- .../RuntimeDxe/VariableSmmRuntimeDxe.c | 92 +++++++++++++++--- 2 files changed, 155 insertions(+), 31 deletions(-) diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c index 442fe1dad7..e4d3aca193 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c @@ -4,7 +4,7 @@ and volatile storage space and install variable architecture protocol based on SMM variable module. -Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.
+Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -42,7 +42,51 @@ EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL; UINT8 *mVariableBuffer = NULL; UINT8 *mVariableBufferPhysical = NULL; UINTN mVariableBufferSize; +EFI_LOCK mVariableServicesLock; +/** + Acquires lock only at boot time. Simply returns at runtime. + + This is a temperary function that will be removed when + EfiAcquireLock() in UefiLib can handle the call in UEFI + Runtimer driver in RT phase. + It calls EfiAcquireLock() at boot time, and simply returns + at runtime. + + @param Lock A pointer to the lock to acquire. + +**/ +VOID +AcquireLockOnlyAtBootTime ( + IN EFI_LOCK *Lock + ) +{ + if (!EfiAtRuntime ()) { + EfiAcquireLock (Lock); + } +} + +/** + Releases lock only at boot time. Simply returns at runtime. + + This is a temperary function which will be removed when + EfiReleaseLock() in UefiLib can handle the call in UEFI + Runtimer driver in RT phase. + It calls EfiReleaseLock() at boot time and simply returns + at runtime. + + @param Lock A pointer to the lock to release. + +**/ +VOID +ReleaseLockOnlyAtBootTime ( + IN EFI_LOCK *Lock + ) +{ + if (!EfiAtRuntime ()) { + EfiReleaseLock (Lock); + } +} /** Initialize the communicate buffer using DataSize and Function. @@ -153,15 +197,17 @@ RuntimeServiceGetVariable ( if ((*DataSize != 0) && (Data == NULL)) { return EFI_INVALID_PARAMETER; } - + + AcquireLockOnlyAtBootTime(&mVariableServicesLock); + // // Init the communicate buffer. The buffer data size is: - // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize + DataSize. + // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize. // PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + StrSize (VariableName) + *DataSize; Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE); if (EFI_ERROR (Status)) { - return Status; + goto Done; } ASSERT (SmmVariableHeader != NULL); @@ -189,11 +235,13 @@ RuntimeServiceGetVariable ( } if (EFI_ERROR (Status)) { - return Status; + goto Done; } CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize); +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); return Status; } @@ -226,7 +274,9 @@ RuntimeServiceGetNextVariableName ( if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) { return EFI_INVALID_PARAMETER; } - + + AcquireLockOnlyAtBootTime(&mVariableServicesLock); + // // Init the communicate buffer. The buffer data size is: // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize. @@ -234,7 +284,7 @@ RuntimeServiceGetNextVariableName ( PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + *VariableNameSize; Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME); if (EFI_ERROR (Status)) { - return Status; + goto Done; } ASSERT (SmmGetNextVariableName != NULL); @@ -252,12 +302,14 @@ RuntimeServiceGetNextVariableName ( // *VariableNameSize = SmmGetNextVariableName->NameSize; if (EFI_ERROR (Status)) { - return Status; + goto Done; } CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid); CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize); +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); return Status; } @@ -302,7 +354,9 @@ RuntimeServiceSetVariable ( if (DataSize != 0 && Data == NULL) { return EFI_INVALID_PARAMETER; } - + + AcquireLockOnlyAtBootTime(&mVariableServicesLock); + // // Init the communicate buffer. The buffer data size is: // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize. @@ -310,7 +364,7 @@ RuntimeServiceSetVariable ( PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + StrSize (VariableName) + DataSize; Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE); if (EFI_ERROR (Status)) { - return Status; + goto Done; } ASSERT (SmmVariableHeader != NULL); @@ -325,7 +379,9 @@ RuntimeServiceSetVariable ( // Send data to SMM. // Status = SendCommunicateBuffer (PayloadSize); - + +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); return Status; } @@ -363,7 +419,9 @@ RuntimeServiceQueryVariableInfo ( if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) { return EFI_INVALID_PARAMETER; } - + + AcquireLockOnlyAtBootTime(&mVariableServicesLock); + // // Init the communicate buffer. The buffer data size is: // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize; @@ -371,7 +429,7 @@ RuntimeServiceQueryVariableInfo ( PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO); Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO); if (EFI_ERROR (Status)) { - return Status; + goto Done; } ASSERT (SmmQueryVariableInfo != NULL); @@ -382,7 +440,7 @@ RuntimeServiceQueryVariableInfo ( // Status = SendCommunicateBuffer (PayloadSize); if (EFI_ERROR (Status)) { - return Status; + goto Done; } // @@ -391,7 +449,9 @@ RuntimeServiceQueryVariableInfo ( *MaximumVariableSize = SmmQueryVariableInfo->MaximumVariableSize; *MaximumVariableStorageSize = SmmQueryVariableInfo->MaximumVariableStorageSize; *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize; - + +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); return EFI_SUCCESS; } @@ -589,7 +649,9 @@ VariableSmmRuntimeInitialize ( VOID *SmmVariableWriteRegistration; EFI_EVENT OnReadyToBootEvent; EFI_EVENT ExitBootServiceEvent; - + + EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY); + // // Smm variable service is ready // diff --git a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.c b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.c index 2ef44e3a11..1c06edd8cf 100644 --- a/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.c +++ b/SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmmRuntimeDxe.c @@ -52,7 +52,51 @@ EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL; UINT8 *mVariableBuffer = NULL; UINT8 *mVariableBufferPhysical = NULL; UINTN mVariableBufferSize; +EFI_LOCK mVariableServicesLock; +/** + Acquires lock only at boot time. Simply returns at runtime. + + This is a temperary function that will be removed when + EfiAcquireLock() in UefiLib can handle the call in UEFI + Runtimer driver in RT phase. + It calls EfiAcquireLock() at boot time, and simply returns + at runtime. + + @param Lock A pointer to the lock to acquire. + +**/ +VOID +AcquireLockOnlyAtBootTime ( + IN EFI_LOCK *Lock + ) +{ + if (!EfiAtRuntime ()) { + EfiAcquireLock (Lock); + } +} + +/** + Releases lock only at boot time. Simply returns at runtime. + + This is a temperary function which will be removed when + EfiReleaseLock() in UefiLib can handle the call in UEFI + Runtimer driver in RT phase. + It calls EfiReleaseLock() at boot time and simply returns + at runtime. + + @param Lock A pointer to the lock to release. + +**/ +VOID +ReleaseLockOnlyAtBootTime ( + IN EFI_LOCK *Lock + ) +{ + if (!EfiAtRuntime ()) { + EfiReleaseLock (Lock); + } +} /** Initialize the communicate buffer using DataSize and Function. @@ -169,15 +213,17 @@ RuntimeServiceGetVariable ( if ((*DataSize != 0) && (Data == NULL)) { return EFI_INVALID_PARAMETER; } - + + AcquireLockOnlyAtBootTime(&mVariableServicesLock); + // // Init the communicate buffer. The buffer data size is: - // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize + DataSize. + // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize. // PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + StrSize (VariableName) + *DataSize; Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_GET_VARIABLE); if (EFI_ERROR (Status)) { - return Status; + goto Done; } ASSERT (SmmVariableHeader != NULL); @@ -205,11 +251,13 @@ RuntimeServiceGetVariable ( } if (EFI_ERROR (Status)) { - return Status; + goto Done; } CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize, SmmVariableHeader->DataSize); +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); return Status; } @@ -242,7 +290,9 @@ RuntimeServiceGetNextVariableName ( if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) { return EFI_INVALID_PARAMETER; } - + + AcquireLockOnlyAtBootTime(&mVariableServicesLock); + // // Init the communicate buffer. The buffer data size is: // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize. @@ -250,7 +300,7 @@ RuntimeServiceGetNextVariableName ( PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name) + *VariableNameSize; Status = InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, PayloadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME); if (EFI_ERROR (Status)) { - return Status; + goto Done; } ASSERT (SmmGetNextVariableName != NULL); @@ -268,12 +318,14 @@ RuntimeServiceGetNextVariableName ( // *VariableNameSize = SmmGetNextVariableName->NameSize; if (EFI_ERROR (Status)) { - return Status; + goto Done; } CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid); CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableName->NameSize); +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); return Status; } @@ -321,7 +373,9 @@ RuntimeServiceSetVariable ( if (DataSize != 0 && Data == NULL) { return EFI_INVALID_PARAMETER; } - + + AcquireLockOnlyAtBootTime(&mVariableServicesLock); + // // Init the communicate buffer. The buffer data size is: // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize. @@ -329,7 +383,7 @@ RuntimeServiceSetVariable ( PayloadSize = OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) + StrSize (VariableName) + DataSize; Status = InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSize, SMM_VARIABLE_FUNCTION_SET_VARIABLE); if (EFI_ERROR (Status)) { - return Status; + goto Done; } ASSERT (SmmVariableHeader != NULL); @@ -344,7 +398,9 @@ RuntimeServiceSetVariable ( // Send data to SMM. // Status = SendCommunicateBuffer (PayloadSize); - + +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); return Status; } @@ -382,7 +438,9 @@ RuntimeServiceQueryVariableInfo ( if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) { return EFI_INVALID_PARAMETER; } - + + AcquireLockOnlyAtBootTime(&mVariableServicesLock); + // // Init the communicate buffer. The buffer data size is: // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + PayloadSize; @@ -390,7 +448,7 @@ RuntimeServiceQueryVariableInfo ( PayloadSize = sizeof (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO); Status = InitCommunicateBuffer ((VOID **)&SmmQueryVariableInfo, PayloadSize, SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO); if (EFI_ERROR (Status)) { - return Status; + goto Done; } ASSERT (SmmQueryVariableInfo != NULL); @@ -401,7 +459,7 @@ RuntimeServiceQueryVariableInfo ( // Status = SendCommunicateBuffer (PayloadSize); if (EFI_ERROR (Status)) { - return Status; + goto Done; } // @@ -410,7 +468,9 @@ RuntimeServiceQueryVariableInfo ( *MaximumVariableSize = SmmQueryVariableInfo->MaximumVariableSize; *MaximumVariableStorageSize = SmmQueryVariableInfo->MaximumVariableStorageSize; *RemainingVariableStorageSize = SmmQueryVariableInfo->RemainingVariableStorageSize; - + +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); return EFI_SUCCESS; } @@ -608,7 +668,9 @@ VariableSmmRuntimeInitialize ( VOID *SmmVariableWriteRegistration; EFI_EVENT OnReadyToBootEvent; EFI_EVENT ExitBootServiceEvent; - + + EfiInitializeLock (&mVariableServicesLock, TPL_NOTIFY); + // // Smm variable service is ready // -- 2.39.2