From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io0-x233.google.com (mail-io0-x233.google.com [IPv6:2607:f8b0:4001:c06::233]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 78AFC82214 for ; Fri, 3 Mar 2017 01:59:24 -0800 (PST) Received: by mail-io0-x233.google.com with SMTP id z13so26993260iof.2 for ; Fri, 03 Mar 2017 01:59:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=MMdL+jLH5HX9dIClAJTSdaPk6hh11i6M+58mIYX+RoI=; b=f1SWc1fH67aTrNscbv5vRTxn+l13fosiSGFC1zC41MPeMy/rw1uAguKgyUE/hN+e5+ SryvvFz9FQ4M4p0CgIp0owFErV2kvU872jAOG1zbokEAkr6T9eN7Cpqly0ovguLy8w4f PJ59A0aOhOqoKYxNhNKILI/+QLwZszPnSBnVE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=MMdL+jLH5HX9dIClAJTSdaPk6hh11i6M+58mIYX+RoI=; b=l/43wV5C+SO2b7/rQ6ciclmPHBccWebH/YFglIHUuGAILe9eVRkuJvja4kHUYBUVfZ aPr3ku4Y2SC30YOxrVv1UuRx10Wuv0CS/7ZmJNmAjmm3pYHZNPKx3Pp2cJXMNxWHNeKf YPzcHuaVT3S9rTts7kfDvxswGPK22yAMHMQG7FprViyN34n7JImhKoPe/t6F6tWPSphP lrmHpqVRAgz9HFTDa+rn9oePm9YZCPrEmopEqdyN/mHy/M4wa8eSYbSZgASUkYRrrnDn CQvRcCPPXmERy9Kcw6U4u4CXnV5t6LbqeLzLuNSaH29OlahgL7yfEaGDKbSQJzllC3xP Cieg== X-Gm-Message-State: AMke39ldyo8cd8MbHw9cqruM7z1xoD1H85M/frisJC9kp0JLebs8aTZye49k3bxZGMzf8ihVx7ZE7qNONZboWyXQ X-Received: by 10.107.13.130 with SMTP id 124mr1734013ion.83.1488535163749; Fri, 03 Mar 2017 01:59:23 -0800 (PST) MIME-Version: 1.0 Received: by 10.107.10.27 with HTTP; Fri, 3 Mar 2017 01:59:23 -0800 (PST) In-Reply-To: <1488534218-25315-1-git-send-email-ard.biesheuvel@linaro.org> References: <1488534218-25315-1-git-send-email-ard.biesheuvel@linaro.org> From: Ard Biesheuvel Date: Fri, 3 Mar 2017 09:59:23 +0000 Message-ID: To: "edk2-devel@lists.01.org" , "Gao, Liming" , "Zeng, Star" Cc: Ard Biesheuvel Subject: Re: [PATCH v3] MdeModulePkg/PeiCore: honour minimal runtime allocation granularity X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 03 Mar 2017 09:59:24 -0000 Content-Type: text/plain; charset=UTF-8 On 3 March 2017 at 09:43, Ard Biesheuvel wrote: > Architectures such as AArch64 may run the OS with 16 KB or 64 KB sized > pages, and for this reason, the UEFI spec mandates a minimal allocation > granularity of 64 KB for regions that may require different memory > attributes at OS runtime. > > So make PeiCore's implementation of AllocatePages () take this into > account as well. > > Contributed-under: TianoCore Contribution Agreement 1.0 > Signed-off-by: Ard Biesheuvel > --- > v3: allocate a memory allocation HOB to cover the memory lost to rounding > > MdeModulePkg/Core/Pei/Memory/MemoryServices.c | 39 +++++++++++++++++++- > MdeModulePkg/Core/Pei/PeiMain.h | 18 +++++++++ > 2 files changed, 55 insertions(+), 2 deletions(-) > > diff --git a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c > index 4efe14313ca5..573fd606b4cc 100644 > --- a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c > +++ b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c > @@ -140,6 +140,8 @@ PeiAllocatePages ( > EFI_PHYSICAL_ADDRESS *FreeMemoryTop; > EFI_PHYSICAL_ADDRESS *FreeMemoryBottom; > UINTN RemainingPages; > + UINTN Granularity; > + UINTN Padding; > > if ((MemoryType != EfiLoaderCode) && > (MemoryType != EfiLoaderData) && > @@ -153,6 +155,20 @@ PeiAllocatePages ( > return EFI_INVALID_PARAMETER; > } > > + Granularity = DEFAULT_PAGE_ALLOCATION_GRANULARITY; > + > + if (RUNTIME_PAGE_ALLOCATION_GRANULARITY > DEFAULT_PAGE_ALLOCATION_GRANULARITY && > + (MemoryType == EfiACPIReclaimMemory || > + MemoryType == EfiACPIMemoryNVS || > + MemoryType == EfiRuntimeServicesCode || > + MemoryType == EfiRuntimeServicesData)) { > + > + Granularity = RUNTIME_PAGE_ALLOCATION_GRANULARITY; > + > + DEBUG ((DEBUG_INFO, "AllocatePages: rounding up allocation to %d KB\n", > + Granularity)); this needs to be divided by SIZE_1KB > + } > + > PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices); > Hob.Raw = PrivateData->HobList.Raw; > > @@ -176,9 +192,27 @@ PeiAllocatePages ( > } > > // > - // Check to see if on 4k boundary, If not aligned, make the allocation aligned. > + // Check to see if on correct boundary for the memory type. > + // If not aligned, make the allocation aligned. > // > - *(FreeMemoryTop) -= *(FreeMemoryTop) & 0xFFF; > + Padding = *(FreeMemoryTop) & (Granularity - 1); > + if ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom) < Padding) { > + DEBUG ((DEBUG_ERROR, "AllocatePages failed: Out of space after padding.\n")); > + return EFI_OUT_OF_RESOURCES; > + } > + > + *(FreeMemoryTop) -= Padding; > + if (Padding >= EFI_PAGE_SIZE) { > + // > + // Create a memory allocation HOB to cover > + // the pages that we will lose to rounding > + // > + BuildMemoryAllocationHob ( > + *(FreeMemoryTop), > + Padding & ~((UINTN)EFI_PAGE_SIZE - 1), > + EfiConventionalMemory > + ); > + } > > // > // Verify that there is sufficient memory to satisfy the allocation. > @@ -192,6 +226,7 @@ PeiAllocatePages ( > // > // The number of remaining pages needs to be greater than or equal to that of the request pages. > // > + Pages = ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity)); > if (RemainingPages < Pages) { > DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64) Pages)); > DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64) RemainingPages)); > diff --git a/MdeModulePkg/Core/Pei/PeiMain.h b/MdeModulePkg/Core/Pei/PeiMain.h > index 69eea514920b..e8358d3c4e6d 100644 > --- a/MdeModulePkg/Core/Pei/PeiMain.h > +++ b/MdeModulePkg/Core/Pei/PeiMain.h > @@ -55,6 +55,24 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. > /// > #define PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE 0xff > > +#if defined (MDE_CPU_AARCH64) > +/// > +/// 64-bit ARM systems allow the OS to execute with 64 KB page size, > +/// so for improved interoperability with the firmware, align the > +/// runtime regions to 64 KB as well > +/// > +#define RUNTIME_PAGE_ALLOCATION_GRANULARITY (SIZE_64KB) > +#define DEFAULT_PAGE_ALLOCATION_GRANULARITY (EFI_PAGE_SIZE) > + > +#else > +/// > +/// For generic EFI machines make the default allocations 4K aligned > +/// > +#define RUNTIME_PAGE_ALLOCATION_GRANULARITY (EFI_PAGE_SIZE) > +#define DEFAULT_PAGE_ALLOCATION_GRANULARITY (EFI_PAGE_SIZE) > + > +#endif > + > /// > /// Pei Core private data structures > /// > -- > 2.7.4 >