From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.93; helo=mga11.intel.com; envelope-from=michael.d.kinney@intel.com; receiver=edk2-devel@lists.01.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (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 0313E209603EB for ; Tue, 22 May 2018 10:40:05 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 May 2018 10:40:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,430,1520924400"; d="scan'208";a="41997728" Received: from orsmsx101.amr.corp.intel.com ([10.22.225.128]) by fmsmga008.fm.intel.com with ESMTP; 22 May 2018 10:40:05 -0700 Received: from orsmsx156.amr.corp.intel.com (10.22.240.22) by ORSMSX101.amr.corp.intel.com (10.22.225.128) with Microsoft SMTP Server (TLS) id 14.3.319.2; Tue, 22 May 2018 10:40:05 -0700 Received: from orsmsx113.amr.corp.intel.com ([169.254.9.119]) by ORSMSX156.amr.corp.intel.com ([169.254.8.42]) with mapi id 14.03.0319.002; Tue, 22 May 2018 10:40:04 -0700 From: "Kinney, Michael D" To: Ard Biesheuvel , "edk2-devel@lists.01.org" , "Kinney, Michael D" CC: Laszlo Ersek , Leif Lindholm , "Gao, Liming" , "Zeng, Star" , "Dong, Eric" , "Bi, Dandan" Thread-Topic: [PATCH 3/6] MdePkg/UefiLib: introduce EfiAllocatePeiAccessiblePages routine Thread-Index: AQHT8dZzxavP9QYiBUyTL/ThwdYQXKQ7/xqw Date: Tue, 22 May 2018 17:40:03 +0000 Message-ID: References: <20180522140850.30369-1-ard.biesheuvel@linaro.org> <20180522140850.30369-4-ard.biesheuvel@linaro.org> In-Reply-To: <20180522140850.30369-4-ard.biesheuvel@linaro.org> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: dlp-product: dlpe-windows dlp-version: 11.0.200.100 dlp-reaction: no-action x-originating-ip: [10.22.254.140] MIME-Version: 1.0 Subject: Re: [PATCH 3/6] MdePkg/UefiLib: introduce EfiAllocatePeiAccessiblePages routine X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 May 2018 17:40:06 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Ard, The corner case that does not work with this approach is when X64 DXE is combined with an X64 PEI. OVMF uses this and other platforms could choose to use X64 PEI phase. The other mismatch here is adding some PI Spec Concepts (e.g. PEI phase) to a UEFI library. Maybe DxeServicesLib would be a better place to put this type of API. One clue we have about the memory usage in the PEI phase is from the EFI_HOB_HANDOFF_INFO_TABLE HOB. /// /// The highest address location of memory that is allocated for use by t= he HOB producer /// phase. This address must be 4-KB aligned to meet page restrictions of= UEFI. /// EFI_PHYSICAL_ADDRESS EfiMemoryTop; /// /// The highest address location of free memory that is currently availab= le /// for use by the HOB producer phase. /// EFI_PHYSICAL_ADDRESS EfiFreeMemoryTop; So maybe we could have an X64 specific implementation of this new API that checks one of these HOB fields. If they are below 4GB, then allocate memory below 4GB. If one is above 4GB, then no restrictions. All other archs allocate with no restrictions. Now this approach will still allocate below 4GB for X64 PEI if the this HOB contains addressed below 4GB, but that would match the memory usage for that X64 PEI implementation. Best regards, Mike > -----Original Message----- > From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org] > Sent: Tuesday, May 22, 2018 7:09 AM > To: edk2-devel@lists.01.org > Cc: Ard Biesheuvel ; Laszlo > Ersek ; Leif Lindholm > ; Kinney, Michael D > ; Gao, Liming > ; Zeng, Star > ; Dong, Eric ; > Bi, Dandan > Subject: [PATCH 3/6] MdePkg/UefiLib: introduce > EfiAllocatePeiAccessiblePages routine >=20 > Add a routine to UefiLib that abstracts the allocation > of memory that > should be accessible by PEI after a warm reboot. We will > use it to > replace open coded implementations that limit the > address to < 4 GB, > which may not be possible on non-Intel systems that have > no 32-bit > addressable memory at all. >=20 > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Ard Biesheuvel > > --- > MdePkg/Include/Library/UefiLib.h | 23 ++++++++++ > MdePkg/Library/UefiLib/UefiLib.c | 48 > ++++++++++++++++++++ > 2 files changed, 71 insertions(+) >=20 > diff --git a/MdePkg/Include/Library/UefiLib.h > b/MdePkg/Include/Library/UefiLib.h > index 256498e3fd8d..8fa077af41e0 100644 > --- a/MdePkg/Include/Library/UefiLib.h > +++ b/MdePkg/Include/Library/UefiLib.h > @@ -1520,4 +1520,27 @@ EfiLocateProtocolBuffer ( > OUT UINTN *NoProtocols, > OUT VOID ***Buffer > ); > + > +/** > + Allocates one or more 4KB pages of a given type from > a memory region that is > + accessible to PEI. > + > + Allocates the number of 4KB pages of type > 'MemoryType' and returns a > + pointer to the allocated buffer. The buffer returned > is aligned on a 4KB > + boundary. If Pages is 0, then NULL is returned. If > there is not enough > + memory remaining to satisfy the request, then NULL is > returned. > + > + @param[in] MemoryType The memory type to > allocate > + @param[in] Pages The number of 4 KB > pages to allocate. > + > + @return A pointer to the allocated buffer or NULL if > allocation fails. > + > +**/ > +VOID * > +EFIAPI > +EfiAllocatePeiAccessiblePages ( > + IN EFI_MEMORY_TYPE MemoryType, > + IN UINTN Pages > + ); > + > #endif > diff --git a/MdePkg/Library/UefiLib/UefiLib.c > b/MdePkg/Library/UefiLib/UefiLib.c > index ba449a1c34ce..3a9d75149dd7 100644 > --- a/MdePkg/Library/UefiLib/UefiLib.c > +++ b/MdePkg/Library/UefiLib/UefiLib.c > @@ -1715,3 +1715,51 @@ EfiLocateProtocolBuffer ( >=20 > return EFI_SUCCESS; > } > + > +/** > + Allocates one or more 4KB pages of a given type from > a memory region that is > + accessible to PEI. > + > + Allocates the number of 4KB pages of type > 'MemoryType' and returns a > + pointer to the allocated buffer. The buffer returned > is aligned on a 4KB > + boundary. If Pages is 0, then NULL is returned. If > there is not enough > + memory remaining to satisfy the request, then NULL is > returned. > + > + @param[in] MemoryType The memory type to > allocate > + @param[in] Pages The number of 4 KB > pages to allocate. > + > + @return A pointer to the allocated buffer or NULL if > allocation fails. > + > +**/ > +VOID * > +EFIAPI > +EfiAllocatePeiAccessiblePages ( > + IN EFI_MEMORY_TYPE MemoryType, > + IN UINTN Pages > + ) > +{ > + EFI_STATUS Status; > + EFI_PHYSICAL_ADDRESS Memory; > + EFI_ALLOCATE_TYPE AllocType; > + > + if (Pages =3D=3D 0) { > + return NULL; > + } > + > +#ifdef MDE_CPU_X64 > + // > + // On X64 systems, a X64 build of DXE may be combined > with a 32-bit build of > + // PEI, and so we need to allocate below 4 GB to > ensure that the allocation > + // is accessible by PEI. > + // > + AllocType =3D AllocateMaxAddress; > + Memory =3D MAX_UINT32; > +#else > + AllocType =3D AllocateAnyPages; > +#endif > + Status =3D gBS->AllocatePages (AllocType, MemoryType, > Pages, &Memory); > + if (EFI_ERROR (Status)) { > + return NULL; > + } > + return (VOID *)(UINTN)Memory; > +} > -- > 2.17.0