From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: redhat.com, ip: 209.132.183.28, mailfrom: lersek@redhat.com) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by groups.io with SMTP; Tue, 24 Sep 2019 04:35:35 -0700 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 11FF569080; Tue, 24 Sep 2019 11:35:35 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-120-118.rdu2.redhat.com [10.10.120.118]) by smtp.corp.redhat.com (Postfix) with ESMTP id 678CC600CC; Tue, 24 Sep 2019 11:35:32 +0000 (UTC) From: "Laszlo Ersek" To: edk2-devel-groups-io Cc: Ard Biesheuvel , Boris Ostrovsky , Brijesh Singh , Igor Mammedov , Jiewen Yao , Joao M Martins , Jordan Justen , Jun Nakajima , Michael Kinney , Paolo Bonzini , Phillip Goerl , Yingwen Chen Subject: [PATCH wave 1 08/10] OvmfPkg/SEV: don't manage the lifecycle of the SMRAM at the default SMBASE Date: Tue, 24 Sep 2019 13:35:03 +0200 Message-Id: <20190924113505.27272-9-lersek@redhat.com> In-Reply-To: <20190924113505.27272-1-lersek@redhat.com> References: <20190924113505.27272-1-lersek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 24 Sep 2019 11:35:35 +0000 (UTC) Content-Transfer-Encoding: quoted-printable When OVMF runs in a SEV guest, the initial SMM Save State Map is (1) allocated as EfiBootServicesData type memory in OvmfPkg/PlatformPei, function AmdSevInitialize(), for preventing unintended information sharing with the hypervisor; (2) decrypted in AmdSevDxe; (3) re-encrypted in OvmfPkg/Library/SmmCpuFeaturesLib, function SmmCpuFeaturesSmmRelocationComplete(), which is called by PiSmmCpuDxeSmm right after initial SMBASE relocation; (4) released to DXE at the same location. The SMRAM at the default SMBASE is a superset of the initial Save State Map. The reserved memory allocation in InitializeRamRegions(), from the previous patch, must override the allocating and freeing in (1) and (4), respectively. (Note: the decrypting and re-encrypting in (2) and (3) are unaffected.) In AmdSevInitialize(), only assert the containment of the initial Save State Map, in the larger area already allocated by InitializeRamRegions()= . In SmmCpuFeaturesSmmRelocationComplete(), preserve the allocation of the initial Save State Map into OS runtime, as part of the allocation done by InitializeRamRegions(). Only assert containment. These changes only affect the normal boot path (the UEFI memory map is untouched during S3 resume). Cc: Ard Biesheuvel Cc: Boris Ostrovsky Cc: Brijesh Singh Cc: Igor Mammedov Cc: Jiewen Yao Cc: Joao M Martins Cc: Jordan Justen Cc: Jun Nakajima Cc: Michael Kinney Cc: Paolo Bonzini Cc: Phillip Goerl Cc: Yingwen Chen Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1512 Signed-off-by: Laszlo Ersek --- OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf | 4 ++++ OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c | 21 +++++++++++= ++++-- OvmfPkg/PlatformPei/AmdSev.c | 24 +++++++++++= +++++---- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf b/Ov= mfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf index dd316f2b1bd8..97a10afb6e27 100644 --- a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf +++ b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.inf @@ -30,5 +30,9 @@ [LibraryClasses] BaseMemoryLib DebugLib MemEncryptSevLib + PcdLib SmmServicesTableLib UefiBootServicesTableLib + +[Pcd] + gUefiOvmfPkgTokenSpaceGuid.PcdQ35SmramAtDefaultSmbase diff --git a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c b/Ovmf= Pkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c index 0bfdeda78d33..7ef7ed98342e 100644 --- a/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c +++ b/OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c @@ -6,14 +6,17 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ =20 +#include #include #include #include #include +#include #include #include #include #include +#include #include =20 // @@ -215,8 +218,22 @@ SmmCpuFeaturesSmmRelocationComplete ( =20 ZeroMem ((VOID *)MapPagesBase, EFI_PAGES_TO_SIZE (MapPagesCount)); =20 - Status =3D gBS->FreePages (MapPagesBase, MapPagesCount); - ASSERT_EFI_ERROR (Status); + if (PcdGetBool (PcdQ35SmramAtDefaultSmbase)) { + // + // The initial SMRAM Save State Map has been covered as part of a la= rger + // reserved memory allocation in PlatformPei's InitializeRamRegions(= ). That + // allocation is supposed to survive into OS runtime; we must not re= lease + // any part of it. Only re-assert the containment here. + // + ASSERT (SMM_DEFAULT_SMBASE <=3D MapPagesBase); + ASSERT ( + (MapPagesBase + EFI_PAGES_TO_SIZE (MapPagesCount) <=3D + SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE) + ); + } else { + Status =3D gBS->FreePages (MapPagesBase, MapPagesCount); + ASSERT_EFI_ERROR (Status); + } } =20 /** diff --git a/OvmfPkg/PlatformPei/AmdSev.c b/OvmfPkg/PlatformPei/AmdSev.c index 2ae8126ccf8a..e484f4b311fe 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 =20 #include "Platform.h" =20 @@ -83,10 +85,22 @@ AmdSevInitialize ( ); ASSERT_RETURN_ERROR (LocateMapStatus); =20 - 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 <=3D MapPagesBase); + ASSERT ( + (MapPagesBase + EFI_PAGES_TO_SIZE (MapPagesCount) <=3D + SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE) + ); + } else { + BuildMemoryAllocationHob ( + MapPagesBase, // BaseAddress + EFI_PAGES_TO_SIZE (MapPagesCount), // Length + EfiBootServicesData // MemoryType + ); + } } } --=20 2.19.1.3.g30247aa5d201