From patchwork Tue Feb 28 12:13:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 94608 Delivered-To: patch@linaro.org Received: by 10.140.20.113 with SMTP id 104csp1291447qgi; Tue, 28 Feb 2017 04:13:24 -0800 (PST) X-Received: by 10.99.122.71 with SMTP id j7mr2276470pgn.52.1488284004871; Tue, 28 Feb 2017 04:13:24 -0800 (PST) Return-Path: Received: from ml01.01.org (ml01.01.org. [198.145.21.10]) by mx.google.com with ESMTPS id w28si1620524pfk.112.2017.02.28.04.13.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Feb 2017 04:13:24 -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 460D581F3A; Tue, 28 Feb 2017 04:13:24 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received: from mail-wm0-x22e.google.com (mail-wm0-x22e.google.com [IPv6:2a00:1450:400c:c09::22e]) (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 2A76D81F28 for ; Tue, 28 Feb 2017 04:13:22 -0800 (PST) Received: by mail-wm0-x22e.google.com with SMTP id v77so83522059wmv.1 for ; Tue, 28 Feb 2017 04:13:22 -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; bh=4FYdnnIfbLPnBGy02WKAl72gEX2CkYEq0HkgAsCN6bs=; b=R5V17kPG8EKrCFzBmN3ZKyAt75vLT7surlv3r97tIwANJrG/HZZJN2PzK9ifa5NOV+ cUMINxdw9jxp2awaCJmZSXAsAc1u1Bi0xwwx/EBXWOAU0rYurPwK1RqLfbEWd3RIVv8k AO7PBSlAiGMyEPT3PS2P22xP0ErlP5fRVddUA= 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=4FYdnnIfbLPnBGy02WKAl72gEX2CkYEq0HkgAsCN6bs=; b=pQN1ME80fwxMkH8vmatD/TJwqXovw2TNG/Y7KW76qvIju+CbxeVsI6nA4Jg9mRJF/K qBR2D5xXKhPZ5EfXiFhRUOaizlXYfcJeVJc8OnxvuHGTEFdzoJI0InnSZWcCLsCAPd/B Lv2z3NSh52wr/5uYlUp0I+giu0GgUFbQRUXx7iKeIYMHXYgs+tB6fVNiiADYuMS1jrI3 WJVCcWhhMntiC5TbWkcokBb9zo8oxGGR2U13e8aU3olud5TkX39cwczrLa6hEj+L7oPn DbDXpO2tmhnegR4rMI4RcHEY8i8efCBWQ6D5bIWZ+dR2lzxO+NM0ZtmQXRj7I6AKibcc TJYA== X-Gm-Message-State: AMke39mkOid7JuExAx7l0+wdkduys14g+AvD7kDKp9WfBd2whk2TW5RwIRT0w4I2zeCW1r3a X-Received: by 10.28.210.139 with SMTP id j133mr17657022wmg.67.1488284000242; Tue, 28 Feb 2017 04:13:20 -0800 (PST) Received: from localhost.localdomain ([105.149.201.216]) by smtp.gmail.com with ESMTPSA id x193sm2403690wme.23.2017.02.28.04.13.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 28 Feb 2017 04:13:19 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org, leif.lindholm@linaro.org Date: Tue, 28 Feb 2017 12:13:10 +0000 Message-Id: <1488283992-32104-1-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 Subject: [edk2] [PATCH 1/3] ArmPkg/UncachedMemoryAllocationLib: restore mapping attributes after free 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 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" In order to play nice with platforms that use strict memory permission policies, restore the original mapping attributes when freeing uncached allocations. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel --- ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) -- 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel Reviewed-by: Leif Lindholm diff --git a/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c b/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c index f6c692f9a403..cd13a7da92e0 100644 --- a/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c +++ b/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c @@ -42,11 +42,6 @@ UncachedInternalAllocateAlignedPages ( -// -// Assume all of memory has the same cache attributes, unless we do our magic -// -UINT64 gAttributes; - typedef struct { EFI_PHYSICAL_ADDRESS Base; VOID *Allocation; @@ -54,6 +49,7 @@ typedef struct { EFI_MEMORY_TYPE MemoryType; BOOLEAN Allocated; LIST_ENTRY Link; + UINT64 Attributes; } FREE_PAGE_NODE; STATIC LIST_ENTRY mPageList = INITIALIZE_LIST_HEAD_VARIABLE (mPageList); @@ -153,10 +149,7 @@ AllocatePagesFromList ( } Status = gDS->GetMemorySpaceDescriptor (Memory, &Descriptor); - if (!EFI_ERROR (Status)) { - // We are making an assumption that all of memory has the same default attributes - gAttributes = Descriptor.Attributes; - } else { + if (EFI_ERROR (Status)) { gBS->FreePages (Memory, Pages); return Status; } @@ -181,6 +174,7 @@ AllocatePagesFromList ( NewNode->Pages = Pages; NewNode->Allocated = TRUE; NewNode->MemoryType = MemoryType; + NewNode->Attributes = Descriptor.Attributes; InsertTailList (&mPageList, &NewNode->Link); @@ -266,6 +260,10 @@ UncachedMemoryAllocationLibDestructor ( // We only free the non-allocated buffer if (OldNode->Allocated == FALSE) { gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)OldNode->Base, OldNode->Pages); + + gDS->SetMemorySpaceAttributes ((EFI_PHYSICAL_ADDRESS)(UINTN)OldNode->Base, + EFI_PAGES_TO_SIZE (OldNode->Pages), OldNode->Attributes); + RemoveEntryList (&OldNode->Link); FreePool (OldNode); } From patchwork Tue Feb 28 12:13:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 94609 Delivered-To: patch@linaro.org Received: by 10.140.20.113 with SMTP id 104csp1291459qgi; Tue, 28 Feb 2017 04:13:27 -0800 (PST) X-Received: by 10.98.150.70 with SMTP id c67mr2254692pfe.84.1488284006952; Tue, 28 Feb 2017 04:13:26 -0800 (PST) Return-Path: Received: from ml01.01.org (ml01.01.org. [198.145.21.10]) by mx.google.com with ESMTPS id p17si1626219pgi.67.2017.02.28.04.13.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Feb 2017 04:13:26 -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 7F4CF81F56; Tue, 28 Feb 2017 04:13:26 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received: from mail-wr0-x234.google.com (mail-wr0-x234.google.com [IPv6:2a00:1450:400c:c0c::234]) (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 D1F3381F28 for ; Tue, 28 Feb 2017 04:13:23 -0800 (PST) Received: by mail-wr0-x234.google.com with SMTP id g10so7497918wrg.2 for ; Tue, 28 Feb 2017 04:13:23 -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=EX48uUnEO1Aff6Et/rl/Zr7vysAHZnMKiBG1TZhmWbw=; b=Ncj75egN3qVUxUj9R7pPnmE88w+VLipTfXisUeNa5Xv0XSZaDZi5oUBR3hiCVoj7Lr NnULqNulLneHns0qfcdi/fRZJErEZy8RkpiitJvN/XRin9lRU/98kFG4DEeFJE8YGPng B7DsfR+ZImHIFKVze9e3tocMeLL4RWijV2siY= 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=EX48uUnEO1Aff6Et/rl/Zr7vysAHZnMKiBG1TZhmWbw=; b=Oq3beduL8APULnLcA8yiMZdoenLNxdShzRKsGVMtKWyjDF0VCR92NIJabbcOK+8GXA GggX8cAr9VPoRLhspWZckY272O90Ij3YeuiOIlD9F7SlFuhyZAznCxGHWUvig0ymoshJ foETiMFqfy1m4iBLt1jh1xTCKj38R/wdWWhoImlbBFi8PRial04SJIKxwj58EZj2MrMg Ag6HAgfAaxqKk/sQmUrihf3eYPynwqDa2EwY1np1C6TAOgVGkxlBpXmhEk7jM5sZuZWC 2+TujBZFGohIUqVIhMcTDaSEv0EycNx1LU5DHbzTAaL8zPD0ef1hrP6zoZheOcR9BdZy r7tA== X-Gm-Message-State: AMke39khHr4iYFep2DRJQS5ktKezSdilgAUK7paayDUWjwBCpxSoRMpHx6oARkZcxcCl60wm X-Received: by 10.223.160.220 with SMTP id n28mr2068059wrn.31.1488284002206; Tue, 28 Feb 2017 04:13:22 -0800 (PST) Received: from localhost.localdomain ([105.149.201.216]) by smtp.gmail.com with ESMTPSA id x193sm2403690wme.23.2017.02.28.04.13.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 28 Feb 2017 04:13:21 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org, leif.lindholm@linaro.org Date: Tue, 28 Feb 2017 12:13:11 +0000 Message-Id: <1488283992-32104-2-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488283992-32104-1-git-send-email-ard.biesheuvel@linaro.org> References: <1488283992-32104-1-git-send-email-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH 2/3] ArmPkg/UncachedMemoryAllocationLib: use CWG value to align pool allocations 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 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" Uncached pool allocations are aligned to the data cache line length under the assumption that this is sufficient to prevent cache maintenance from corrupting adjacent allocations. However, the value to use in such cases is architecturally called the Cache Writeback Granule (CWG), which is essentially the maximum Dcache line length rather than the minimum. Note that this is mostly a cosmetical fix, given that the pool allocation is turned into a page allocation later, and rounded up accordingly. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel --- ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel Reviewed-by: Leif Lindholm diff --git a/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c b/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c index cd13a7da92e0..0d8abad23433 100644 --- a/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c +++ b/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c @@ -545,7 +545,7 @@ UncachedInternalAllocatePool ( IN UINTN AllocationSize ) { - UINTN CacheLineLength = ArmDataCacheLineLength (); + UINTN CacheLineLength = ArmCacheWritebackGranule (); return UncachedInternalAllocateAlignedPool (MemoryType, AllocationSize, CacheLineLength); } From patchwork Tue Feb 28 12:13:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 94610 Delivered-To: patch@linaro.org Received: by 10.140.20.113 with SMTP id 104csp1291468qgi; Tue, 28 Feb 2017 04:13:28 -0800 (PST) X-Received: by 10.84.133.193 with SMTP id f59mr2686144plf.64.1488284008378; Tue, 28 Feb 2017 04:13:28 -0800 (PST) Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id o71si1609198pfi.195.2017.02.28.04.13.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Feb 2017 04:13:28 -0800 (PST) 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; 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 B37BD82169; Tue, 28 Feb 2017 04:13:26 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received: from mail-wm0-x22f.google.com (mail-wm0-x22f.google.com [IPv6:2a00:1450:400c:c09::22f]) (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 8E8B381F45 for ; Tue, 28 Feb 2017 04:13:25 -0800 (PST) Received: by mail-wm0-x22f.google.com with SMTP id u199so10120618wmd.1 for ; Tue, 28 Feb 2017 04:13:25 -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=2PJeDJx/oCwuAczTf8dB+hbItlKbISDKQs+mTTJxprI=; b=RvgmQHSzBXxKNiOL+Mp+QdLP6un7QQbWgMmbJXFeXnA3uExeXPyxW2Of5Yd4jBIkff MTxR2gRCic15/0bZn8iIXqF9lKqSaKeslB0En5GjEODK+trPUtt/w8KI/pQ0RpcOEVg5 Y9vKfLxdmaTZWExO/2C4n8Wzt31I9HhpocAC0= 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=2PJeDJx/oCwuAczTf8dB+hbItlKbISDKQs+mTTJxprI=; b=MXxo5ft7WgXpd9HSOFyyvUkOav4Oe5SnVAA/ZCMBeHE58fQuSO6N3anTEmhBCJxtcf dzsvYts6CKN5eMCig+rTkysmd0yJQGQOQfC8XcZTMokBe3PWz66bAwAzjDMyT/imvVvu RiyLKkiV2n0rbIRIZdCKGmOFhqfDVq5SP347CZRq3wG6e/q/mXaVUSOaYI1+EiWJNxqL UJ8vFVy9GxbssPvAR2iLDzzmugKLJ4h8whrrNlYN0nO2vMzTEwdhFi8HeQAK3jwgkCmk kiaIxB4ikQPoRvFuPFoNJMRzFyu95NWVx81o6pc0Ezevw1uskXxjk7NzXGG+5bzCY07W sCFw== X-Gm-Message-State: AMke39moznQdndGA/6NKulEpNHbTmF8Cl/CdJRXILYPS2ljSfhn9uu5ubOOwx1fWMf/VnWsv X-Received: by 10.28.97.194 with SMTP id v185mr2164037wmb.117.1488284004136; Tue, 28 Feb 2017 04:13:24 -0800 (PST) Received: from localhost.localdomain ([105.149.201.216]) by smtp.gmail.com with ESMTPSA id x193sm2403690wme.23.2017.02.28.04.13.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 28 Feb 2017 04:13:23 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org, leif.lindholm@linaro.org Date: Tue, 28 Feb 2017 12:13:12 +0000 Message-Id: <1488283992-32104-3-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488283992-32104-1-git-send-email-ard.biesheuvel@linaro.org> References: <1488283992-32104-1-git-send-email-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH 3/3] ArmPkg/UncachedMemoryAllocationLib: map uncached allocations non-executable 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 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" The primary use case for UncachedMemoryAllocationLib is non-coherent DMA, which implies that such regions are not used to fetch instructions from. So let's map them as non-executable, to avoid creating a security hole when the rest of the platform may be enforcing strict memory permissions on ordinary allocations. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel --- ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) -- 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel Reviewed-by: Leif Lindholm diff --git a/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c b/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c index 0d8abad23433..b4fbfbcb362b 100644 --- a/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c +++ b/ArmPkg/Library/UncachedMemoryAllocationLib/UncachedMemoryAllocationLib.c @@ -154,7 +154,8 @@ AllocatePagesFromList ( return Status; } - Status = gDS->SetMemorySpaceAttributes (Memory, EFI_PAGES_TO_SIZE (Pages), EFI_MEMORY_WC); + Status = gDS->SetMemorySpaceAttributes (Memory, EFI_PAGES_TO_SIZE (Pages), + EFI_MEMORY_WC | EFI_MEMORY_XP); if (EFI_ERROR (Status)) { gBS->FreePages (Memory, Pages); return Status;