From patchwork Mon May 28 14:40:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 137084 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp2908957lji; Mon, 28 May 2018 07:40:41 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrP7OCdbBZ+PR63awbJYBiwts/dGMM4cq5dchhC1BN/3rmNkJ9crbRlT7UjbtFprT2Ks04R X-Received: by 2002:a63:744c:: with SMTP id e12-v6mr10913954pgn.4.1527518441549; Mon, 28 May 2018 07:40:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527518441; cv=none; d=google.com; s=arc-20160816; b=0YAyhQDsC1vbfj3GZMc+ZKORDvWUuqF4OxsVnHAEs52DCjrkOuyFROfP8U5gk6ljsc OWq+1xlFfwEdfbtf8Idc7CzdoXYk+mXdtO6YUbMyWkAdbfptmouq1mtcMhHG8Wclmwca k197J2OZgLp2C8dYFVRP6dH67zENMqai4KNM5NvPCa0+K2Rv+YlX3G+//BtAt8Nar7ku YR05LGsuonesmhgJdPN3AMVreHKUboPX8ADv3FeazTDTH6POzPIh4yLJruu0V/VHoebQ hdwtPNpD7rqouL1DsMB1j611InfMaz/qlthEhVZ5fA5RIgXl6IP48WFiqVkVmqAlNwBN cf1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=yvOh/19Q5Hl221IvWSUiOpyv7bm670b/XlPYx/uPDJM=; b=Ma0j5XttGWRh248yY9/h1iEPFX8rPlSkSfkghgUBCiBVV3siIu7/ounJJdlRip+u32 +jr3B3RrLV+3jVbX8xVZA012cawOa2dUggwrzlg5QeR1DDiYp4UsyCuDut+6yncM2hVq ax2CN04b5FFpPChKfHylxU3Sr4JbE2d+7YSl11vOgrSsr+1+8XzUp/6WrO81Vl74Yy5d CGbQWoeAo+50ImNs9cOQ7iP19RFbC9nzlszp6T7lSyWGeR8N3DkqU2+vSncPj031QfZz aFwTNyc6dEu0jgggNCkjABKT7vBtEPrBWYivraV3Dq7aHg+XbvBcqEQHwki31KBx3Gi2 489g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=AhUs3h4+; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id n4-v6si24017664pgn.691.2018.05.28.07.40.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 May 2018 07:40:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) client-ip=2001:19d0:306:5::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=AhUs3h4+; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 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 776342096DCEE; Mon, 28 May 2018 07:40:38 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:400c:c09::229; helo=mail-wm0-x229.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wm0-x229.google.com (mail-wm0-x229.google.com [IPv6:2a00:1450:400c:c09::229]) (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 0FE8720348603 for ; Mon, 28 May 2018 07:40:36 -0700 (PDT) Received: by mail-wm0-x229.google.com with SMTP id 18-v6so27177241wml.2 for ; Mon, 28 May 2018 07:40:35 -0700 (PDT) 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=u6zCqFp2IaaQxo92GOnrrGKK5FBwiSrmNNmvxZqOObs=; b=AhUs3h4+pKPZqzwXhEgwPjrExAsJoqdTvyowA2u7EwtNMA0tDkSYmWJ/mHd3IkFoS/ YGoXESOx+zT9OKK3+zYj6utSuRB011IJTvfqBJwpwMc0mD8QGKTtYZoaV8SqnihXXdgW u2bl7Evi6A5FJ/WZ+PV403eEPUuGfgSN1n1L0= 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=u6zCqFp2IaaQxo92GOnrrGKK5FBwiSrmNNmvxZqOObs=; b=BNtfH13hVNs1BdrPguBiWf1b0W/kbqrsYRR8krL87M9w/esjT9Vgnsw3wtbRcNKQG0 pdJm9rctA02by2JGe0qjoMbb4BSqAEsJoUKk8UbT7C7CypDSQ6aigLYaOHd7muM2niIw zOcg+FhZddjdnnzVYzGtHGgQE1wGNSctV2ZbwS5CWJ3Mt09ngDgux+M9gRHPOXWTuoGh xR1zzA154y9+cyzVUGRKyNr6q1xxM/xkq/ZKcWdafaxtWQp9e9KHHVVPalN8D++RVKan oEuTeTKoFapSxnw+IMzG1ESFo/Cr0svwWmC0Jqy/AdC7yvh7dygotFqHVhRKdSkVgRMB NNJg== X-Gm-Message-State: ALKqPwe0ZtvE/xBN+Hsoj5aMJ65KsqvMgvAWuwnVHFA/Wouj7k4QgqOF QlWiEyLeYyv4ap0pPQeUJ8+5CC3B/lY= X-Received: by 2002:a1c:6dd3:: with SMTP id b80-v6mr7968979wmi.32.1527518434317; Mon, 28 May 2018 07:40:34 -0700 (PDT) Received: from localhost.localdomain ([2a01:e35:3995:5470:200:1aff:fe1b:b328]) by smtp.gmail.com with ESMTPSA id y18-v6sm4177938wrl.53.2018.05.28.07.40.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 May 2018 07:40:33 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.01.org Date: Mon, 28 May 2018 16:40:22 +0200 Message-Id: <20180528144024.10809-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528144024.10809-1-ard.biesheuvel@linaro.org> References: <20180528144024.10809-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH v3 3/5] MdePkg/DxeServicesLib: introduce AllocatePeiAccessiblePages 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: , Cc: Ard Biesheuvel MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" Add a routine to DxeServicesLib 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. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ard Biesheuvel --- MdePkg/Include/Library/DxeServicesLib.h | 23 ++++++- MdePkg/Library/DxeServicesLib/Allocate.c | 54 +++++++++++++++ MdePkg/Library/DxeServicesLib/DxeServicesLib.inf | 11 +++- MdePkg/Library/DxeServicesLib/X64/Allocate.c | 69 ++++++++++++++++++++ 4 files changed, 155 insertions(+), 2 deletions(-) -- 2.17.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel Reviewed-by: Laszlo Ersek Signed-off-by: Ard Biesheuvel Reviewed-by: Liming Gao diff --git a/MdePkg/Include/Library/DxeServicesLib.h b/MdePkg/Include/Library/DxeServicesLib.h index 7c1c62236d96..20aee68af558 100644 --- a/MdePkg/Include/Library/DxeServicesLib.h +++ b/MdePkg/Include/Library/DxeServicesLib.h @@ -305,5 +305,26 @@ GetFileDevicePathFromAnyFv ( OUT EFI_DEVICE_PATH_PROTOCOL **FvFileDevicePath ); -#endif +/** + 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 +AllocatePeiAccessiblePages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages + ); + +#endif diff --git a/MdePkg/Library/DxeServicesLib/Allocate.c b/MdePkg/Library/DxeServicesLib/Allocate.c new file mode 100644 index 000000000000..4d118f766d49 --- /dev/null +++ b/MdePkg/Library/DxeServicesLib/Allocate.c @@ -0,0 +1,54 @@ +/** @file + DxeServicesLib memory allocation routines + + Copyright (c) 2018, Linaro, Ltd. All rights reserved.
+ + This program and the accompanying materials are licensed and made available + under the terms and conditions of the BSD License which accompanies this + distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php. + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include +#include +#include + +/** + 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 +AllocatePeiAccessiblePages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages + ) +{ + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS Memory; + + if (Pages == 0) { + return NULL; + } + + Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory); + if (EFI_ERROR (Status)) { + return NULL; + } + return (VOID *)(UINTN)Memory; +} diff --git a/MdePkg/Library/DxeServicesLib/DxeServicesLib.inf b/MdePkg/Library/DxeServicesLib/DxeServicesLib.inf index bd2faf2f6f2d..50ae24f8ee22 100644 --- a/MdePkg/Library/DxeServicesLib/DxeServicesLib.inf +++ b/MdePkg/Library/DxeServicesLib/DxeServicesLib.inf @@ -27,12 +27,18 @@ [Defines] LIBRARY_CLASS = DxeServicesLib|DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SAL_DRIVER DXE_SMM_DRIVER SMM_CORE UEFI_APPLICATION UEFI_DRIVER # -# VALID_ARCHITECTURES = IA32 X64 IPF EBC +# VALID_ARCHITECTURES = IA32 X64 IPF EBC ARM AARCH64 # [Sources] DxeServicesLib.c +[Sources.IA32, Sources.IPF, Sources.EBC, Sources.ARM, Sources.AARCH64] + Allocate.c + +[Sources.X64] + X64/Allocate.c + [Packages] MdePkg/MdePkg.dec @@ -44,6 +50,9 @@ [LibraryClasses] UefiLib UefiBootServicesTableLib +[LibraryClasses.X64] + HobLib + [Guids] gEfiFileInfoGuid ## SOMETIMES_CONSUMES ## UNDEFINED diff --git a/MdePkg/Library/DxeServicesLib/X64/Allocate.c b/MdePkg/Library/DxeServicesLib/X64/Allocate.c new file mode 100644 index 000000000000..b6d34ba20881 --- /dev/null +++ b/MdePkg/Library/DxeServicesLib/X64/Allocate.c @@ -0,0 +1,69 @@ +/** @file + DxeServicesLib memory allocation routines + + Copyright (c) 2018, Linaro, Ltd. All rights reserved.
+ + This program and the accompanying materials are licensed and made available + under the terms and conditions of the BSD License which accompanies this + distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php. + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include +#include +#include +#include + +/** + 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 +AllocatePeiAccessiblePages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages + ) +{ + EFI_STATUS Status; + EFI_ALLOCATE_TYPE AllocType; + EFI_PHYSICAL_ADDRESS Memory; + EFI_HOB_HANDOFF_INFO_TABLE *PhitHob; + + if (Pages == 0) { + return NULL; + } + + AllocType = AllocateAnyPages; + // + // A X64 build of DXE may be combined with a 32-bit build of PEI, and so we + // need to check the memory limit set by PEI, and allocate below 4 GB if the + // limit is set to 4 GB or lower. + // + PhitHob = (EFI_HOB_HANDOFF_INFO_TABLE *)GetHobList (); + if (PhitHob->EfiFreeMemoryTop <= MAX_UINT32) { + AllocType = AllocateMaxAddress; + Memory = MAX_UINT32; + } + + Status = gBS->AllocatePages (AllocType, MemoryType, Pages, &Memory); + if (EFI_ERROR (Status)) { + return NULL; + } + return (VOID *)(UINTN)Memory; +}