From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:400c:c09::235; helo=mail-wm0-x235.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wm0-x235.google.com (mail-wm0-x235.google.com [IPv6:2a00:1450:400c:c09::235]) (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 9CF8B203B992C for ; Tue, 22 May 2018 07:09:04 -0700 (PDT) Received: by mail-wm0-x235.google.com with SMTP id a67-v6so133274wmf.3 for ; Tue, 22 May 2018 07:09:04 -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=LHlwcmVHos8NZgj2kGrntQDGOb8hzisISq9LYld3ZUU=; b=djJvkh3QOuxwTexaH9J75kuD599gt+IXxy1LJFrMjyrUpy7dNp9xJt76FsUeG28eut VKdZMJiEiUKHJNiNTst5Tp8ird0+/jOuzqsCx1mYfu1o3JNRX0sNKEXHrivBfEVlx41Z YN+z1SYnV8Kj5FbDc8eOOv0EeXEUTOnn0op+0= 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=LHlwcmVHos8NZgj2kGrntQDGOb8hzisISq9LYld3ZUU=; b=ZCcjjb6uhc4Ou5pLBH6okmzQG23b2h1otEQCLX2cSQ0UnMtyluXNYG43zUGwlfMHQQ aTSjJd+Zk64c/pGzlkDTbNwNVNyrf7pfxgwJ4M4staiN4ncO2AU4F/9f6Ak9JkRSVeVd GiJUy0/bru0E8MJZsbxkTr8bXHQi0O0isX3t1A9K9OL3gTnB4dMd+vVTUy/XaPFYujPv NqWg4unnFg1v4QuG3mH8aBt1MJeJF0vkbGDa3gc8X59VFzp014R6eQThGt5ptsdMMv3z esy7Pu52vQKVlTa8p9FjTzuxQv5BmuU+Aq6nWFMjVWirgAFVhnDGxxw/WohLQYbpiLcV a23A== X-Gm-Message-State: ALKqPwdOQEdBouP7XikdVdC3Cmc4b/nrib2a/epxlxVMjCGf9wqMxq4f DsLTqREmpKKuiaZ735UG7+TnE1vfJ+E= X-Google-Smtp-Source: AB8JxZrdMAD67rijFcEoqGlDB77OHLzqMQvBKgH2TMWFEM9yAkXjQM0zNwW9C3w/prQdg8hsscAoTQ== X-Received: by 2002:a1c:a84d:: with SMTP id r74-v6mr1458905wme.114.1526998142791; Tue, 22 May 2018 07:09:02 -0700 (PDT) Received: from localhost.localdomain ([2a01:e35:3995:5470:200:1aff:fe1b:b328]) by smtp.gmail.com with ESMTPSA id r6-v6sm17755545wrg.73.2018.05.22.07.09.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 May 2018 07:09:02 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.01.org Cc: Ard Biesheuvel , Laszlo Ersek , Leif Lindholm , Michael D Kinney , Liming Gao , Star Zeng , Eric Dong , Dandan Bi Date: Tue, 22 May 2018 16:08:47 +0200 Message-Id: <20180522140850.30369-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180522140850.30369-1-ard.biesheuvel@linaro.org> References: <20180522140850.30369-1-ard.biesheuvel@linaro.org> Subject: [PATCH 3/6] MdePkg/UefiLib: introduce EfiAllocatePeiAccessiblePages 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: , X-List-Received-Date: Tue, 22 May 2018 14:09:05 -0000 Add a routine to UefiLib 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/UefiLib.h | 23 ++++++++++ MdePkg/Library/UefiLib/UefiLib.c | 48 ++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/MdePkg/Include/Library/UefiLib.h b/MdePkg/Include/Library/UefiLib.h index 256498e3fd8d..8fa077af41e0 100644 --- a/MdePkg/Include/Library/UefiLib.h +++ b/MdePkg/Include/Library/UefiLib.h @@ -1520,4 +1520,27 @@ EfiLocateProtocolBuffer ( OUT UINTN *NoProtocols, OUT VOID ***Buffer ); + +/** + 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 +EfiAllocatePeiAccessiblePages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages + ); + #endif diff --git a/MdePkg/Library/UefiLib/UefiLib.c b/MdePkg/Library/UefiLib/UefiLib.c index ba449a1c34ce..3a9d75149dd7 100644 --- a/MdePkg/Library/UefiLib/UefiLib.c +++ b/MdePkg/Library/UefiLib/UefiLib.c @@ -1715,3 +1715,51 @@ EfiLocateProtocolBuffer ( return EFI_SUCCESS; } + +/** + 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 +EfiAllocatePeiAccessiblePages ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages + ) +{ + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS Memory; + EFI_ALLOCATE_TYPE AllocType; + + if (Pages == 0) { + return NULL; + } + +#ifdef MDE_CPU_X64 + // + // On X64 systems, a X64 build of DXE may be combined with a 32-bit build of + // PEI, and so we need to allocate below 4 GB to ensure that the allocation + // is accessible by PEI. + // + AllocType = AllocateMaxAddress; + Memory = MAX_UINT32; +#else + AllocType = AllocateAnyPages; +#endif + Status = gBS->AllocatePages (AllocType, MemoryType, Pages, &Memory); + if (EFI_ERROR (Status)) { + return NULL; + } + return (VOID *)(UINTN)Memory; +} -- 2.17.0