From patchwork Tue Jun 30 10:17:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 50448 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f69.google.com (mail-la0-f69.google.com [209.85.215.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 549EE229DF for ; Tue, 30 Jun 2015 10:19:48 +0000 (UTC) Received: by lagx9 with SMTP id x9sf2026396lag.2 for ; Tue, 30 Jun 2015 03:19:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:subject:date:message-id :in-reply-to:references:cc:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version :content-type:content-transfer-encoding:sender:errors-to :x-original-sender:x-original-authentication-results:mailing-list; bh=NAfGyv3KSLYwzu6Uh7W8Ei9RtIxW0kxJPSf3JzYCIig=; b=OVyDKi3z7JDZIuNbYQPCb+UxlQvofWK017eMqIIxQRcHSBiSOnLk5c4Bv137PWaoOE TUt+bTu3CG4/PdeMyn/A0yMugvDaTBX+sfgfW/bG/NZERLmzz17cx0lgBPOkRxPXiJ9n 6qaoOAh2dBdY8B+fo4/IOqcwrAWZTY6FPlxGVZXSNpv8Vk/98+m7Od4x3pZh9lO5hDA1 2fHKe5iUbrUGPZU97U+n4YFEW30FjeXCSKSkQVYuBJ1ra+jctFyBcXejXaQZpJYolAWA is29KSzk3gnJXoKB1UgamteneINF7zdCBSpkDSPNOP3V6k45MHeE2vx878msbqsIVmIN /KvA== X-Gm-Message-State: ALoCoQmkDlDKJ6J6yKaCYOz5w1P5airViKeE2tRqd+pmwCCQnhQx/YcsvBRKywqBH8Uq3BVtAsVD X-Received: by 10.180.182.110 with SMTP id ed14mr11391033wic.5.1435659587289; Tue, 30 Jun 2015 03:19:47 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.21.230 with SMTP id y6ls25687lae.20.gmail; Tue, 30 Jun 2015 03:19:47 -0700 (PDT) X-Received: by 10.152.43.134 with SMTP id w6mr17072452lal.120.1435659587104; Tue, 30 Jun 2015 03:19:47 -0700 (PDT) Received: from mail-la0-f44.google.com (mail-la0-f44.google.com. [209.85.215.44]) by mx.google.com with ESMTPS id q15si27294015lal.76.2015.06.30.03.19.47 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 Jun 2015 03:19:47 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.44 as permitted sender) client-ip=209.85.215.44; Received: by lagx9 with SMTP id x9so7256594lag.1 for ; Tue, 30 Jun 2015 03:19:47 -0700 (PDT) X-Received: by 10.152.4.163 with SMTP id l3mr19163339lal.35.1435659586952; Tue, 30 Jun 2015 03:19:46 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp2333981lbb; Tue, 30 Jun 2015 03:19:45 -0700 (PDT) X-Received: by 10.70.93.69 with SMTP id cs5mr41734032pdb.165.1435659584834; Tue, 30 Jun 2015 03:19:44 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id mj9si69449562pab.42.2015.06.30.03.19.44 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 Jun 2015 03:19:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) client-ip=2001:1868:205::9; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1Z9sca-0004ga-MS; Tue, 30 Jun 2015 10:18:32 +0000 Received: from mail-wg0-f41.google.com ([74.125.82.41]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1Z9sc4-0004Rd-9x for linux-arm-kernel@lists.infradead.org; Tue, 30 Jun 2015 10:18:02 +0000 Received: by wgck11 with SMTP id k11so5409969wgc.0 for ; Tue, 30 Jun 2015 03:17:38 -0700 (PDT) X-Received: by 10.194.192.98 with SMTP id hf2mr37882838wjc.23.1435659458138; Tue, 30 Jun 2015 03:17:38 -0700 (PDT) Received: from localhost.localdomain ([185.13.106.91]) by mx.google.com with ESMTPSA id y19sm16272083wia.15.2015.06.30.03.17.35 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 30 Jun 2015 03:17:37 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org, matt.fleming@intel.com, mark.rutland@arm.com Subject: [PATCH 2/2] arm64/efi: don't pad between EFI_MEMORY_RUNTIME regions Date: Tue, 30 Jun 2015 12:17:23 +0200 Message-Id: <1435659443-17625-3-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1435659443-17625-1-git-send-email-ard.biesheuvel@linaro.org> References: <1435659443-17625-1-git-send-email-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150630_031800_536112_CA22BA4F X-CRM114-Status: GOOD ( 19.74 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [74.125.82.41 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [74.125.82.41 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.0 RCVD_IN_MSPIKE_WL Mailspike good senders Cc: roy.franz@linaro.org, Ard Biesheuvel , lersek@redhat.com, leif.lindholm@linaro.org, msalter@redhat.com X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ard.biesheuvel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.44 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 The new Properties Table feature introduced in UEFIv2.5 may split memory regions that cover PE/COFF memory images into separate code and data regions. Since the relative offset of PE/COFF .text and .data segments cannot be changed on the fly, this means that we can no longer pad out those regions to be mappable using 64 KB pages. Unfortunately, there is no annotation in the UEFI memory map that identifies data regions that were split off from a code region, so we must apply this logic to all runtime code and data regions. So instead of rounding each memory region to 64 KB alignment at both ends, only round down regions that are not directly preceded by another runtime region. Since the UEFI spec does not mandate that the memory map be sorted, this means we also need to sort it first. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/arm-stub.c | 58 +++++++++++++++----- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/drivers/firmware/efi/libstub/arm-stub.c b/drivers/firmware/efi/libstub/arm-stub.c index e29560e6b40b..dd9e2addc9fb 100644 --- a/drivers/firmware/efi/libstub/arm-stub.c +++ b/drivers/firmware/efi/libstub/arm-stub.c @@ -13,6 +13,7 @@ */ #include +#include #include #include "efistub.h" @@ -305,6 +306,13 @@ fail: */ #define EFI_RT_VIRTUAL_BASE 0x40000000 +static int cmp_mem_desc(const void *a, const void *b) +{ + efi_memory_desc_t const *left = a, *right = b; + + return (left->phys_addr > right->phys_addr) ? 1 : -1; +} + /* * efi_get_virtmap() - create a virtual mapping for the EFI memory map * @@ -317,33 +325,53 @@ void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, int *count) { u64 efi_virt_base = EFI_RT_VIRTUAL_BASE; - efi_memory_desc_t *out = runtime_map; + efi_memory_desc_t *in, *prev = NULL, *out = runtime_map; int l; - for (l = 0; l < map_size; l += desc_size) { - efi_memory_desc_t *in = (void *)memory_map + l; + /* + * To work around potential issues with the Properties Table feature + * introduced in UEFI 2.5 (which may split PE/COFF memory images + * into several RuntimeServicesCode and RuntimeServicesData regions + * whose relative offset in memory needs to be retained), we need to + * sort the memory map before traversing it, and avoid padding out those + * regions to 64 KB granularity. + */ + sort(memory_map, map_size / desc_size, desc_size, cmp_mem_desc, NULL); + + for (l = 0; l < map_size; l += desc_size, prev = in) { u64 paddr, size; + in = (void *)memory_map + l; if (!(in->attribute & EFI_MEMORY_RUNTIME)) continue; + paddr = in->phys_addr; + size = in->num_pages * EFI_PAGE_SIZE; + /* * Make the mapping compatible with 64k pages: this allows * a 4k page size kernel to kexec a 64k page size kernel and * vice versa. */ - paddr = round_down(in->phys_addr, SZ_64K); - size = round_up(in->num_pages * EFI_PAGE_SIZE + - in->phys_addr - paddr, SZ_64K); - - /* - * Avoid wasting memory on PTEs by choosing a virtual base that - * is compatible with section mappings if this region has the - * appropriate size and physical alignment. (Sections are 2 MB - * on 4k granule kernels) - */ - if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M) - efi_virt_base = round_up(efi_virt_base, SZ_2M); + if (!prev || + !(paddr == (prev->phys_addr + + prev->num_pages * EFI_PAGE_SIZE) && + (prev->attribute & EFI_MEMORY_RUNTIME))) { + + paddr = round_down(in->phys_addr, SZ_64K); + size += in->phys_addr - paddr; + + /* + * Avoid wasting memory on PTEs by choosing a virtual + * base that is compatible with section mappings if this + * region has the appropriate size and physical + * alignment. (Sections are 2 MB on 4k granule kernels) + */ + if (IS_ALIGNED(in->phys_addr, SZ_2M) && size >= SZ_2M) + efi_virt_base = round_up(efi_virt_base, SZ_2M); + else + efi_virt_base = round_up(efi_virt_base, SZ_64K); + } in->virt_addr = efi_virt_base + in->phys_addr - paddr; efi_virt_base += size;