diff --git a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c index 0bfdeda78d..7ef7ed9834 100644 --- a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c +++ b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c @@ -6,14 +6,17 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ +#include #include #include #include #include +#include #include #include #include #include +#include #include // @@ -215,8 +218,22 @@ SmmCpuFeaturesSmmRelocationComplete ( ZeroMem ((VOID *)MapPagesBase, EFI_PAGES_TO_SIZE (MapPagesCount)); - Status = gBS->FreePages (MapPagesBase, MapPagesCount); - ASSERT_EFI_ERROR (Status); + if (PcdGetBool (PcdQ35SmramAtDefaultSmbase)) { + // + // The initial SMRAM Save State Map has been covered as part of a larger + // reserved memory allocation in PlatformPei's InitializeRamRegions(). That + // allocation is supposed to survive into OS runtime; we must not release + // any part of it. Only re-assert the containment here. + // + ASSERT (SMM_DEFAULT_SMBASE <= MapPagesBase); + ASSERT ( + (MapPagesBase + EFI_PAGES_TO_SIZE (MapPagesCount) <= + SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE) + ); + } else { + Status = gBS->FreePages (MapPagesBase, MapPagesCount); + ASSERT_EFI_ERROR (Status); + } } /** diff --git a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf index dd316f2b1b..97a10afb6e 100644 --- a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf +++ b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf @@ -30,5 +30,9 @@ BaseMemoryLib DebugLib MemEncryptSevLib + PcdLib SmmServicesTableLib UefiBootServicesTableLib + +[Pcd] + gUefiOvmfPkgTokenSpaceGuid.PcdQ35SmramAtDefaultSmbase diff --git a/OvmfPkg/PlatformPei/AmdSev.c b/OvmfPkg/PlatformPei/AmdSev.c index 2ae8126ccf..e484f4b311 100644 --- a/OvmfPkg/PlatformPei/AmdSev.c +++ b/OvmfPkg/PlatformPei/AmdSev.c @@ -9,6 +9,7 @@ // // The package level header files this module uses // +#include #include #include #include @@ -16,6 +17,7 @@ #include #include #include +#include #include "Platform.h" @@ -83,10 +85,22 @@ AmdSevInitialize ( ); ASSERT_RETURN_ERROR (LocateMapStatus); - BuildMemoryAllocationHob ( - MapPagesBase, // BaseAddress - EFI_PAGES_TO_SIZE (MapPagesCount), // Length - EfiBootServicesData // MemoryType - ); + if (mQ35SmramAtDefaultSmbase) { + // + // The initial SMRAM Save State Map has been covered as part of a larger + // reserved memory allocation in InitializeRamRegions(). + // + ASSERT (SMM_DEFAULT_SMBASE <= MapPagesBase); + ASSERT ( + (MapPagesBase + EFI_PAGES_TO_SIZE (MapPagesCount) <= + SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE) + ); + } else { + BuildMemoryAllocationHob ( + MapPagesBase, // BaseAddress + EFI_PAGES_TO_SIZE (MapPagesCount), // Length + EfiBootServicesData // MemoryType + ); + } } }