From nobody Thu May 2 09:51:48 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.zoho.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 1488534226880675.1901199557385; Fri, 3 Mar 2017 01:43:46 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 56F4A82219; Fri, 3 Mar 2017 01:43:45 -0800 (PST) Received: from mail-wm0-x22a.google.com (mail-wm0-x22a.google.com [IPv6:2a00:1450:400c:c09::22a]) (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 69F4082210 for ; Fri, 3 Mar 2017 01:43:44 -0800 (PST) Received: by mail-wm0-x22a.google.com with SMTP id n11so10901711wma.0 for ; Fri, 03 Mar 2017 01:43:44 -0800 (PST) Received: from localhost.localdomain ([105.147.1.203]) by smtp.gmail.com with ESMTPSA id v1sm14520941wra.65.2017.03.03.01.43.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 03 Mar 2017 01:43:42 -0800 (PST) X-Original-To: edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=tku2tGL2DRbPuHbjPYFqJq6ZOl+vdzc7VbitzTje86U=; b=A/CzeQIoO067E/Oty7BAmNq9hhv0P2ViNPYRza15/Lw4EwaZWlekL1Y/zkqBLMee8U 8B81zAVmONdpZctS5PJAcggRpXupVMMXRofPoS9y3oNLzgHwYGJR0WmL24TmLfJRX2Cn 2KporNoG5IqVSMq8pPe9q1i4hRX9k9Pa0UG2A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=tku2tGL2DRbPuHbjPYFqJq6ZOl+vdzc7VbitzTje86U=; b=Ha6tNvXa33I7TsgwJi3192dIaG3TiHdI7T7i+o66XMCr4wAkomRcYwRNHbMSLXK7y5 GcQBW/VBEhBY5CHUU/b5HwjumxyefOgOjkqCSoAn0nsMSgu5p/j0Gr7de/96HarOV/s5 sJBBnL7KNbNcnhOooQkhKJuExbFjk3s7KFF8jvlbx2OZDol1/rJMuWSmBE3AQoVKUw/k Z2CWzMEIE6Y99Ksu4xw2JDiawPO8J/Oz8KYwCouO8YRacOxRDvblJnq9OlasUGCoiHsD IPEjIUx7Gz5DnXRBPkx0W0vf0mllfhonvi84DOKbUcr1acWJ+rvx5gqCltQKg1Yv/ndo Ac7g== X-Gm-Message-State: AMke39kIPx95AaEa3ErqN570TQQk+y8LEWk5mUGnCtSvGPVM+D02wbw1jjS1w0H/CUCaRTGM X-Received: by 10.28.129.83 with SMTP id c80mr2035487wmd.82.1488534222912; Fri, 03 Mar 2017 01:43:42 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org, liming.gao@intel.com, star.zeng@intel.com Date: Fri, 3 Mar 2017 09:43:38 +0000 Message-Id: <1488534218-25315-1-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 Subject: [edk2] [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: , Cc: Ard Biesheuvel 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" 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/C= ore/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; =20 if ((MemoryType !=3D EfiLoaderCode) && (MemoryType !=3D EfiLoaderData) && @@ -153,6 +155,20 @@ PeiAllocatePages ( return EFI_INVALID_PARAMETER; } =20 + Granularity =3D DEFAULT_PAGE_ALLOCATION_GRANULARITY; + + if (RUNTIME_PAGE_ALLOCATION_GRANULARITY > DEFAULT_PAGE_ALLOCATION_GRANU= LARITY && + (MemoryType =3D=3D EfiACPIReclaimMemory || + MemoryType =3D=3D EfiACPIMemoryNVS || + MemoryType =3D=3D EfiRuntimeServicesCode || + MemoryType =3D=3D EfiRuntimeServicesData)) { + + Granularity =3D RUNTIME_PAGE_ALLOCATION_GRANULARITY; + + DEBUG ((DEBUG_INFO, "AllocatePages: rounding up allocation to %d KB\n", + Granularity)); + } + PrivateData =3D PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices); Hob.Raw =3D PrivateData->HobList.Raw; =20 @@ -176,9 +192,27 @@ PeiAllocatePages ( } =20 // - // Check to see if on 4k boundary, If not aligned, make the allocation a= ligned. + // Check to see if on correct boundary for the memory type. + // If not aligned, make the allocation aligned. // - *(FreeMemoryTop) -=3D *(FreeMemoryTop) & 0xFFF; + Padding =3D *(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) -=3D Padding; + if (Padding >=3D 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 + ); + } =20 // // 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 th= at of the request pages. // + Pages =3D ALIGN_VALUE (Pages, EFI_SIZE_TO_PAGES (Granularity)); if (RemainingPages < Pages) { DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is availabl= e.\n", (UINT64) Pages)); DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource t= o be allocated.\n", (UINT64) RemainingPages)); diff --git a/MdeModulePkg/Core/Pei/PeiMain.h b/MdeModulePkg/Core/Pei/PeiMai= n.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, EITHE= R EXPRESS OR IMPLIED. /// #define PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE 0xff =20 +#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 /// --=20 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel