From nobody Fri Dec 27 03:18:30 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1503072080754503.20658583867555; Fri, 18 Aug 2017 09:01:20 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 5981B21CFA60C; Fri, 18 Aug 2017 08:58:40 -0700 (PDT) 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 431762095B9D7 for ; Fri, 18 Aug 2017 08:58:37 -0700 (PDT) Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Aug 2017 09:00:52 -0700 Received: from shwdeopenpsi068.ccr.corp.intel.com ([10.239.9.22]) by orsmga003.jf.intel.com with ESMTP; 18 Aug 2017 09:00:51 -0700 X-Original-To: edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.41,393,1498546800"; d="scan'208";a="1005243138" From: Star Zeng To: edk2-devel@lists.01.org Date: Sat, 19 Aug 2017 00:00:37 +0800 Message-Id: <1503072038-134760-5-git-send-email-star.zeng@intel.com> X-Mailer: git-send-email 2.7.0.windows.1 In-Reply-To: <1503072038-134760-1-git-send-email-star.zeng@intel.com> References: <1503072038-134760-1-git-send-email-star.zeng@intel.com> Subject: [edk2] [PATCH 4/5] MdePkg PeiMemoryAllocationLib: Update InternalAllocateAlignedPages 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: , Cc: Liming Gao , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Update InternalAllocateAlignedPages to use PeiServicesFreePages. Let the InternalAllocateAlignedPages in PeiMemoryAllocationLib use same algorithm with InternalAllocateAlignedPages in UefiMemoryAllocationLib. Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Star Zeng --- .../PeiMemoryAllocationLib/MemoryAllocationLib.c | 172 +++++------------= ---- 1 file changed, 40 insertions(+), 132 deletions(-) diff --git a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c b/= MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c index 37273dde561a..755f89c72ea2 100644 --- a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c @@ -181,15 +181,12 @@ InternalAllocateAlignedPages ( IN UINTN Alignment ) { - EFI_PHYSICAL_ADDRESS Memory; - EFI_PHYSICAL_ADDRESS AlignedMemory; - EFI_PEI_HOB_POINTERS Hob; - BOOLEAN SkipBeforeMemHob; - BOOLEAN SkipAfterMemHob; - EFI_PHYSICAL_ADDRESS HobBaseAddress; - UINT64 HobLength; - EFI_MEMORY_TYPE HobMemoryType; - UINTN TotalPages; + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS Memory; + UINTN AlignedMemory; + UINTN AlignmentMask; + UINTN UnalignedPages; + UINTN RealPages; =20 // // Alignment must be a power of two or zero. @@ -199,139 +196,50 @@ InternalAllocateAlignedPages ( if (Pages =3D=3D 0) { return NULL; } - // - // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not over= flow. - // - ASSERT (Pages <=3D (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));=20 - - // - // We would rather waste some memory to save PEI code size. - // meaning in addition to the requested size for the aligned mem, - // we simply reserve an overhead memory equal to Alignmemt(page-aligned)= , no matter what. - // The overhead mem size could be reduced later with more involved mallo= c mechanisms - // (e.g., somthing that can detect the alignment boundary before allocat= ing memory or=20 - // can request that memory be allocated at a certain address that is al= eady aligned). - // - TotalPages =3D Pages + (Alignment <=3D EFI_PAGE_SIZE ? 0 : EFI_SIZE_TO_P= AGES(Alignment)); - Memory =3D (EFI_PHYSICAL_ADDRESS) (UINTN) InternalAllocatePages (MemoryT= ype, TotalPages); - if (Memory =3D=3D 0) { - DEBUG((DEBUG_INFO, "Out of memory resource! \n")); - return NULL; - } - DEBUG ((DEBUG_INFO, "Allocated Memory unaligned: Address =3D 0x%LX, Page= s =3D 0x%X, Type =3D %d \n", Memory, TotalPages, (UINTN) MemoryType)); - - // - // Alignment calculation - // - AlignedMemory =3D Memory; if (Alignment > EFI_PAGE_SIZE) { - AlignedMemory =3D ALIGN_VALUE (Memory, Alignment); - } - DEBUG ((DEBUG_INFO, "After aligning to 0x%X bytes: Address =3D 0x%LX, Pa= ges =3D 0x%X \n", Alignment, AlignedMemory, Pages)); - - // - // In general three HOBs cover the total allocated space. - // The aligned portion is covered by the aligned mem HOB and - // the unaligned(to be freed) portions before and after the aligned port= ion are covered by newly created HOBs. - // - // Before mem HOB covers the region between "Memory" and "AlignedMemory" - // Aligned mem HOB covers the region between "AlignedMemory" and "Aligne= dMemory + EFI_PAGES_TO_SIZE(Pages)" - // After mem HOB covers the region between "AlignedMemory + EFI_PAGES_TO= _SIZE(Pages)" and "Memory + EFI_PAGES_TO_SIZE(TotalPages)" - // - // The before or after mem HOBs need to be skipped under special cases w= here the aligned portion - // touches either the top or bottom of the original allocated space. - // - SkipBeforeMemHob =3D FALSE; - SkipAfterMemHob =3D FALSE; - if (Memory =3D=3D AlignedMemory) { - SkipBeforeMemHob =3D TRUE; - } - if ((Memory + EFI_PAGES_TO_SIZE(TotalPages)) =3D=3D (AlignedMemory + EFI= _PAGES_TO_SIZE(Pages))) { // - // This condition is never met in the current implementation. - // There is always some after-mem since the overhead mem(used in Total= Pages) - // is no less than Alignment. + // Calculate the total number of pages since alignment is larger than = page size. // - SkipAfterMemHob =3D TRUE; - } - - // =20 - // Search for the mem HOB referring to the original(unaligned) allocatio= n=20 - // and update the size and type if needed. - // - Hob.Raw =3D GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION); - while (Hob.Raw !=3D NULL) { - if (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress =3D=3D Mem= ory) { - break; - } - Hob.Raw =3D GET_NEXT_HOB (Hob); - Hob.Raw =3D GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw); - } - ASSERT (Hob.Raw !=3D NULL); - if (SkipBeforeMemHob) { + AlignmentMask =3D Alignment - 1; + RealPages =3D Pages + EFI_SIZE_TO_PAGES (Alignment); // - // Use this HOB as aligned mem HOB as there is no portion before it. + // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not ov= erflow. // - HobLength =3D EFI_PAGES_TO_SIZE(Pages); - Hob.MemoryAllocation->AllocDescriptor.MemoryLength =3D HobLength; + ASSERT (RealPages > Pages); + + Status =3D PeiServicesAllocatePages (MemoryType, RealPages, &M= emory); + if (EFI_ERROR (Status)) { + return NULL; + } + AlignedMemory =3D ((UINTN) Memory + AlignmentMask) & ~AlignmentMask; + UnalignedPages =3D EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory); + if (UnalignedPages > 0) { + // + // Free first unaligned page(s). + // + Status =3D PeiServicesFreePages (Memory, UnalignedPages); + ASSERT_EFI_ERROR (Status); + } + Memory =3D (EFI_PHYSICAL_ADDRESS) (AlignedMemory + EFI_PAGES_T= O_SIZE (Pages)); + UnalignedPages =3D RealPages - Pages - UnalignedPages; + if (UnalignedPages > 0) { + // + // Free last unaligned page(s). + // + Status =3D PeiServicesFreePages (Memory, UnalignedPages); + ASSERT_EFI_ERROR (Status); + } } else { // - // Use this HOB as before mem HOB and create a new HOB for the aligned= portion=20 + // Do not over-allocate pages in this case. // - HobLength =3D (AlignedMemory - Memory);=20 - Hob.MemoryAllocation->AllocDescriptor.MemoryLength =3D HobLength; - Hob.MemoryAllocation->AllocDescriptor.MemoryType =3D EfiConventionalMe= mory; - } - - HobBaseAddress =3D Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddre= ss; - HobMemoryType =3D Hob.MemoryAllocation->AllocDescriptor.MemoryType; - - // - // Build the aligned mem HOB if needed - // - if (!SkipBeforeMemHob) { - DEBUG((DEBUG_INFO, "Updated before-mem HOB with BaseAddress =3D %LX, L= ength =3D %LX, MemoryType =3D %d \n", - HobBaseAddress, HobLength, (UINTN) HobMemoryType)); - - HobBaseAddress =3D AlignedMemory; - HobLength =3D EFI_PAGES_TO_SIZE(Pages); - HobMemoryType =3D MemoryType; - - BuildMemoryAllocationHob ( - HobBaseAddress, - HobLength, - HobMemoryType - ); - - DEBUG((DEBUG_INFO, "Created aligned-mem HOB with BaseAddress =3D %LX, = Length =3D %LX, MemoryType =3D %d \n", - HobBaseAddress, HobLength, (UINTN) HobMemoryType)); - } else { - if (HobBaseAddress !=3D 0) { - DEBUG((DEBUG_INFO, "Updated aligned-mem HOB with BaseAddress =3D %LX= , Length =3D %LX, MemoryType =3D %d \n", - HobBaseAddress, HobLength, (UINTN) HobMemoryType)); + Status =3D PeiServicesAllocatePages (MemoryType, Pages, &Memory); + if (EFI_ERROR (Status)) { + return NULL; } + AlignedMemory =3D (UINTN) Memory; } - - - // - // Build the after mem HOB if needed - // - if (!SkipAfterMemHob) { - HobBaseAddress =3D AlignedMemory + EFI_PAGES_TO_SIZE(Pages); - HobLength =3D (Memory + EFI_PAGES_TO_SIZE(TotalPages)) - (AlignedMemor= y + EFI_PAGES_TO_SIZE(Pages)); - HobMemoryType =3D EfiConventionalMemory; - - BuildMemoryAllocationHob ( - HobBaseAddress, - HobLength, - HobMemoryType - ); - - DEBUG((DEBUG_INFO, "Created after-mem HOB with BaseAddress =3D %LX, Le= ngth =3D %LX, MemoryType =3D %d \n", - HobBaseAddress, HobLength, (UINTN) HobMemoryType)); - } - - return (VOID *) (UINTN) AlignedMemory; + return (VOID *) AlignedMemory; } =20 /** --=20 2.7.0.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel