From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id A12662095896C for ; Wed, 5 Jul 2017 10:46:38 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Jul 2017 10:48:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.40,312,1496127600"; d="scan'208";a="107404224" Received: from dwsu-mobl.amr.corp.intel.com (HELO localhost) ([10.254.57.106]) by orsmga002.jf.intel.com with ESMTP; 05 Jul 2017 10:48:17 -0700 MIME-Version: 1.0 To: "Gao, Liming" , Laszlo Ersek , edk2-devel-01 Message-ID: <149927689598.638.6499247930051801274@jljusten-skl> From: Jordan Justen In-Reply-To: <4A89E2EF3DFEDB4C8BFDE51014F606A14D74FBA1@shsmsx102.ccr.corp.intel.com> Cc: Ard Biesheuvel References: <20170628220645.26413-1-lersek@redhat.com> <20170628220645.26413-2-lersek@redhat.com> <4A89E2EF3DFEDB4C8BFDE51014F606A14D74FBA1@shsmsx102.ccr.corp.intel.com> User-Agent: alot/0.5.1 Date: Wed, 05 Jul 2017 10:48:16 -0700 Subject: Re: [PATCH 1/2] OvmfPkg: disable build-time relocation for DXEFV modules X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 05 Jul 2017 17:46:38 -0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On 2017-06-28 20:32:54, Gao, Liming wrote: > Laszlo: > LMFA feature doesn't do PE image rebase at build time. Only XIP > module needs to be rebased at build time. LMFA feature will > specify the loaded memory address for each PE image. At build > time, build tool records the memory address into the one field of > PE image. It doesn't rebase PE image. At boot time, > PeiCore/DxeCore/SmmCore will parse PE image, and try to load it at > the preferred memory address. If the preferred memory address is > not available, PE image will be loaded to other memory address. > LMFA feature only supports the source build EFI image, not support > the binary EFI image. This is a debug feature. > = > For this case, OvmfPkg DXEFV doesn't require to run as XIP. So, it > doesn't require rebase. I agree this change. > = Liming, With this flag set to FALSE, do we attempt to rebase the images, but just not fail if we can't rebase one of the modules? Or, will this disable rebasing of all modules in the FV? I don't think we really make use of this debug feature in OVMF today, but I was wondering what potential impact it might have. I thought in some cases by loading the modules at a fixed address it can help with loading symbols for debug, right? Laszlo, I don't think you need to hold off on the patches for this answer, so you can add an Acked-by from me for your v2. -Jordan > > >-----Original Message----- > >From: Laszlo Ersek [mailto:lersek@redhat.com] > >Sent: Thursday, June 29, 2017 6:07 AM > >To: edk2-devel-01 > >Cc: Ard Biesheuvel ; Justen, Jordan L > >; Gao, Liming > >Subject: [PATCH 1/2] OvmfPkg: disable build-time relocation for DXEFV > >modules > > > >When the GenFv utility from BaseTools composes a firmware volume, it > >checks whether modules in the firmware volume are subject to build-time > >relocation. The primary indication for relocation is whether the firmware > >volume has a nonzero base address, according to the [FD] section(s) in t= he > >FDF file that refer to the firmware volume. > > > >The idea behind build-time relocation is that XIP (execute in place) > >modules will not be relocated at boot-time: > > > >- Pre-DXE phase modules generally execute in place. > > > > (OVMF is no exception, despite the fact that we have writeable memory > > even in SEC: PEI_CORE and PEIMs run in-place from PEIFV, after SEC > > decompresses PEIFV and DXEFV from FVMAIN_COMPACT (flash) to RAM. > > PEI_CORE and the PEIMs are relocated at boot-time only after PlatformP= ei > > installs the permanent PEI RAM, and the RAM migration occurs.) > > > >- Modules dispatched by the DXE Core are generally relocated at boot-tim= e. > > However, this is not necessarily so, the LMFA (Load Modules at Fixed > > Address) feature apparently allows in-place execution for such modules > > as well, deriving the load address from the containing firmware volume= 's > > base address at build time. > > > > (LMFA is controlled by the > > gEfiMdeModulePkgTokenSpaceGuid.PcdLoadModuleAtFixAddressEnable > >fixed > > PCD, which we leave disabled in OVMF.) > > > >Therefore GenFv relocates even DXE and UEFI driver modules if the > >containing firmware volume has a nonzero base address. > > > >In OVMF, this is the case for both PEIV and DXEFV: > > > >> [FD.MEMFD] > >> BaseAddress =3D $(MEMFD_BASE_ADDRESS) > >> Size =3D 0xB00000 > >> ErasePolarity =3D 1 > >> BlockSize =3D 0x10000 > >> NumBlocks =3D 0xB0 > >> ... > >> 0x020000|0x0E0000 > >> > >gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvBase|gUefiOvmfPkgToke > >nSpaceGuid.PcdOvmfPeiMemFvSize > >> FV =3D PEIFV > >> > >> 0x100000|0xA00000 > >> > >gUefiOvmfPkgTokenSpaceGuid.PcdOvmfDxeMemFvBase|gUefiOvmfPkgTok > >enSpaceGuid.PcdOvmfDxeMemFvSize > >> FV =3D DXEFV > > > >While the build-time relocation certainly makes sense for PEIFV (see > >above), the reasons for which we specify DXEFV under [FD.MEMFD] are > >weaker: > > > >- we set the PcdOvmfDxeMemFvBase and PcdOvmfDxeMemFvSize PCDs > >here, > > > >- and we ascertain that DXEFV, when decompressed by SEC from > > FVMAIN_COMPACT, will fit into the area allotted here, at build time. > > > >In other words, the build-time relocation of the modules in DXEFV is a > >waste of resources. But, it gets worse: > > > >Build-time relocation of an executable is only possible if the on-disk a= nd > >in-memory layouts are identical, i.e., if the sections of the PE/COFF > >image adhere to the same alignment on disk and in memory. Put differentl= y, > >the FileAlignment and SectionAlignment headers must be equal. > > > >For boot-time modules that we build as part of edk2, both alignment valu= es > >are 0x20 bytes. For runtime modules that we build as part of edk2, both > >alignment values are 0x1000 bytes. This is why the DXEFV relocation, > >albeit wasteful, is also successful every time. > > > >Unfortunately, if we try to include a PE/COFF binary in DXEFV that > >originates from outside of edk2, the DXEFV relocation can fail due to the > >binary having unmatched FileAlignment and SectionAlignment headers. This > >is precisely the case with the E3522X2.EFI network driver for the e1000 > >NIC, from Intel's BootUtil / PREBOOT.EXE distribution. > > > >The solution is to use the FvForceRebase=3DFALSE override under [FV.DXEF= V]. > >This tells GenFv not to perform build-time relocation on the firmware > >volume, despite the FV having a nonzero base address. As stated above, > >this relocation has always been useless and wasteful in OVMF, because we > >never enable LMFA. > > > >(Put differently, E3522X2.EFI could never be loaded from an FV with LMFA > >enabled for the platform because E3522X2.EFI has unmatched FileAlignment > >and SectionAlignment headers.) > > > >In DXEFV we also have SMM drivers. Those are relocated at boot-time (into > >SMRAM) unconditionally, regardless of LMFA; SMRAM is always discovered at > >boot-time. > > > >Kudos to Ard and Liming for the PE/COFF sections & relocations > >explanation, and for the FvForceRebase=3DFALSE tip. > > > >I regression-tested this change in the following configurations (all with > >normal boot and S3 suspend/resume): > > > > IA32, q35, SMM, Linux > > IA32X64, q35, SMM, Linux > > IA32X64, q35, SMM, Windows-8.1 > > X64, i440fx, no-SMM, Linux > > > >Cc: Ard Biesheuvel > >Cc: Jordan Justen > >Cc: Liming Gao > >Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D613 > >Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D615 > >Suggested-by: Ard Biesheuvel > >Suggested-by: Liming Gao > >Contributed-under: TianoCore Contribution Agreement 1.0 > >Signed-off-by: Laszlo Ersek > >--- > > OvmfPkg/OvmfPkgIa32.fdf | 1 + > > OvmfPkg/OvmfPkgIa32X64.fdf | 1 + > > OvmfPkg/OvmfPkgX64.fdf | 1 + > > 3 files changed, 3 insertions(+) > > > >diff --git a/OvmfPkg/OvmfPkgIa32.fdf b/OvmfPkg/OvmfPkgIa32.fdf > >index 09c165882c3f..859457e9aae5 100644 > >--- a/OvmfPkg/OvmfPkgIa32.fdf > >+++ b/OvmfPkg/OvmfPkgIa32.fdf > >@@ -168,6 +168,7 @@ [FV.PEIFV] > > > >########################################################### > >##################### > > > > [FV.DXEFV] > >+FvForceRebase =3D FALSE > > FvNameGuid =3D 7CB8BDC9-F8EB-4F34-AAEA-3EE4AF6516A1 > > BlockSize =3D 0x10000 > > FvAlignment =3D 16 > >diff --git a/OvmfPkg/OvmfPkgIa32X64.fdf b/OvmfPkg/OvmfPkgIa32X64.fdf > >index 5233314139bc..2a0ed8313786 100644 > >--- a/OvmfPkg/OvmfPkgIa32X64.fdf > >+++ b/OvmfPkg/OvmfPkgIa32X64.fdf > >@@ -168,6 +168,7 @@ [FV.PEIFV] > > > >########################################################### > >##################### > > > > [FV.DXEFV] > >+FvForceRebase =3D FALSE > > FvNameGuid =3D 7CB8BDC9-F8EB-4F34-AAEA-3EE4AF6516A1 > > BlockSize =3D 0x10000 > > FvAlignment =3D 16 > >diff --git a/OvmfPkg/OvmfPkgX64.fdf b/OvmfPkg/OvmfPkgX64.fdf > >index 36150101e784..ca61fa125795 100644 > >--- a/OvmfPkg/OvmfPkgX64.fdf > >+++ b/OvmfPkg/OvmfPkgX64.fdf > >@@ -168,6 +168,7 @@ [FV.PEIFV] > > > >########################################################### > >##################### > > > > [FV.DXEFV] > >+FvForceRebase =3D FALSE > > FvNameGuid =3D 7CB8BDC9-F8EB-4F34-AAEA-3EE4AF6516A1 > > BlockSize =3D 0x10000 > > FvAlignment =3D 16 > >-- > >2.13.1.3.g8be5a757fa67 > > >=20