From patchwork Fri Mar 3 15:11:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 94848 Delivered-To: patch@linaro.org Received: by 10.140.82.71 with SMTP id g65csp280249qgd; Fri, 3 Mar 2017 07:11:54 -0800 (PST) X-Received: by 10.99.127.27 with SMTP id a27mr3984951pgd.87.1488553914169; Fri, 03 Mar 2017 07:11:54 -0800 (PST) Return-Path: Received: from ml01.01.org (ml01.01.org. [198.145.21.10]) by mx.google.com with ESMTPS id k189si4072455pgd.267.2017.03.03.07.11.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Mar 2017 07:11:54 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) client-ip=198.145.21.10; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 198.145.21.10 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 7DE9C8221B; Fri, 3 Mar 2017 07:11:53 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received: from mail-wr0-x236.google.com (mail-wr0-x236.google.com [IPv6:2a00:1450:400c:c0c::236]) (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 38A9F8220E for ; Fri, 3 Mar 2017 07:11:51 -0800 (PST) Received: by mail-wr0-x236.google.com with SMTP id g10so75611114wrg.2 for ; Fri, 03 Mar 2017 07:11:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2jrsvirfXwl422T3/KmOkrEwBQTWZoEHp+d6Ey9/ZUc=; b=jC26o4OXO0K/+mb4vBrTVWsZ1B0lKPLfTf41tnDh43dlg6EcxA1t6njYKCvoIBmS+7 uhX1wmUyRg+jYLGSvgGeug1d1hLVasZHYjyrth/spWVesPcIsVXDqGDfDd62ItH3VCM0 Z3+1hKb/sVxazvTABqzdo7bxJ65TgxIWeaWLg= 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:in-reply-to :references; bh=2jrsvirfXwl422T3/KmOkrEwBQTWZoEHp+d6Ey9/ZUc=; b=jnOfZFRJjwvntx+Bwxse+restUhZEhap9Ai7hn7Q2dQg5ok+2EwhfB2SO9/FbTRtHr +Z+3bHUUdagPx/+UYazoGdhN7Ke6JL30af+kH1CEKuaimlSeou/dQknhBQoyy3QVDB1u 0wIuEdHjoBtWC/8UQjTNzyDZ1CKPmXQlNuQhjbh7QB89hKLrHIV/DG5++bQLYd9izCWS cJuFGr7Hs1GcwcR6dfwk+yRxy9RjBz1IdjgzGXnI3C8pKD31v3WQbStGG44SeAUHbWQB R5gun4Pqa9eWJKng1BAxbf16eT01MkvGiXYeI1ua8ar92/glhbRXvuQHNFU0Ev86Ydhg tQeQ== X-Gm-Message-State: AMke39lhF+BIJ1Mkz/abtUUV5ezDuAtiIK/zU3IWgRerNq6kirM8WuW3PSaGM3vY4pzfrKAn X-Received: by 10.223.169.164 with SMTP id b33mr3322522wrd.132.1488553909559; Fri, 03 Mar 2017 07:11:49 -0800 (PST) Received: from localhost.localdomain ([105.147.1.203]) by smtp.gmail.com with ESMTPSA id w207sm3428523wmw.1.2017.03.03.07.11.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 03 Mar 2017 07:11:48 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org, liming.gao@intel.com, michael.d.kinney@intel.com Date: Fri, 3 Mar 2017 15:11:34 +0000 Message-Id: <1488553894-3520-5-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488553894-3520-1-git-send-email-ard.biesheuvel@linaro.org> References: <1488553894-3520-1-git-send-email-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH v4 4/4] 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: jiewen.yao@intel.com, star.zeng@intel.com, Ard Biesheuvel MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" 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 Reviewed-by: Star Zeng --- MdeModulePkg/Core/Pei/Memory/MemoryServices.c | 39 +++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) -- 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c index 4efe14313ca5..719372e06128 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: aligning allocation to %d KB\n", + Granularity / 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_MASK, + 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));