From patchwork Tue Mar 3 11:03:49 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 45355 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f197.google.com (mail-lb0-f197.google.com [209.85.217.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id B076A21429 for ; Tue, 3 Mar 2015 11:15:20 +0000 (UTC) Received: by lbiz11 with SMTP id z11sf7940580lbi.2 for ; Tue, 03 Mar 2015 03:15:19 -0800 (PST) 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=CKvvCc5bHDSsviYA8fJxeDV5O5maDFaq9yXBgw0rEDE=; b=C/IX2WDcCPK2q79GMUXnboikQymXSizXUZldctANZ93xCBY7+zLDtmhn+u96V88QAr njGbUoXBlovHetfDGb7m4mHXHP+6vf2vFQMKAoXbDvtSOMnFSCvJdof12Fu+aKUZR5mI D8xExbHWAyzMEcd94mg7hDFyYzEdeCE9T+llqK0yH5lDzJLuyTsu2rB+VXm5+vmEuWgD sH8dksc58GJclTYGg0pSLgH9c/HeXNAK8rjIQXBwIULhVPVdNS6PshEiDKmJCBwpNj25 NmfQr/FO42DlLl+pvxPs1xam5hXIs4pZtfyKsdO/BQhwQzAd0CST+kIF1SxCAATgs0Xw dGVg== X-Gm-Message-State: ALoCoQkrx61kXWb8D0/YFAsHPSfs//HKk2+ZHWUXOV6QUjQ+CDsXanumXH8Hx73RA+ppQrqRaWJ2 X-Received: by 10.180.13.98 with SMTP id g2mr2870921wic.3.1425381319618; Tue, 03 Mar 2015 03:15:19 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.153.7.172 with SMTP id dd12ls27784lad.20.gmail; Tue, 03 Mar 2015 03:15:19 -0800 (PST) X-Received: by 10.152.36.226 with SMTP id t2mr9836944laj.53.1425381319424; Tue, 03 Mar 2015 03:15:19 -0800 (PST) Received: from mail-la0-f46.google.com (mail-la0-f46.google.com. [209.85.215.46]) by mx.google.com with ESMTPS id o3si283948lbh.93.2015.03.03.03.15.19 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 03 Mar 2015 03:15:19 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.46 as permitted sender) client-ip=209.85.215.46; Received: by labge10 with SMTP id ge10so36564420lab.12 for ; Tue, 03 Mar 2015 03:15:19 -0800 (PST) X-Received: by 10.152.178.197 with SMTP id da5mr11355227lac.56.1425381319306; Tue, 03 Mar 2015 03:15:19 -0800 (PST) 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.35.133 with SMTP id h5csp492899lbj; Tue, 3 Mar 2015 03:15:18 -0800 (PST) X-Received: by 10.68.132.229 with SMTP id ox5mr55564951pbb.94.1425381317578; Tue, 03 Mar 2015 03:15:17 -0800 (PST) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id ej13si709684pdb.37.2015.03.03.03.15.16 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 03 Mar 2015 03:15:17 -0800 (PST) Received-SPF: none (google.com: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org does not designate permitted sender hosts) 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 1YSkl4-0001u8-41; Tue, 03 Mar 2015 11:13:02 +0000 Received: from mail-wg0-f47.google.com ([74.125.82.47]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YSkdB-0002TV-9X for linux-arm-kernel@lists.infradead.org; Tue, 03 Mar 2015 11:04:54 +0000 Received: by wggx12 with SMTP id x12so39198314wgg.6 for ; Tue, 03 Mar 2015 03:04:28 -0800 (PST) X-Received: by 10.180.35.33 with SMTP id e1mr43803822wij.49.1425380668565; Tue, 03 Mar 2015 03:04:28 -0800 (PST) Received: from ards-macbook-pro.local ([213.143.61.132]) by mx.google.com with ESMTPSA id k6sm20058411wia.6.2015.03.03.03.04.25 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 03 Mar 2015 03:04:27 -0800 (PST) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, mark.rutland@arm.com, roy.franz@linaro.org, leif.lindholm@linaro.org, catalin.marinas@arm.com, will.deacon@arm.com, grant.likely@linaro.org, robh@kernel.org, msalter@redhat.com Subject: [PATCH 4/5] arm64/efi: ensure that Image does not cross a 512 MB boundary Date: Tue, 3 Mar 2015 12:03:49 +0100 Message-Id: <1425380630-3684-5-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1425380630-3684-1-git-send-email-ard.biesheuvel@linaro.org> References: <1425380630-3684-1-git-send-email-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150303_030453_529084_D3C7FCA1 X-CRM114-Status: GOOD ( 19.31 ) X-Spam-Score: -0.7 (/) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-0.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [74.125.82.47 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [74.125.82.47 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_MSPIKE_WL Mailspike good senders Cc: matt.fleming@intel.com, Ard Biesheuvel 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.46 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 Update the Image placement logic used by the stub to make absolutely sure that Image is placed in such a way that the early init code will always be able to map it. This means the entire static memory footprint of Image should be inside the same naturally aligned 512 MB region. First of all, the preferred offset of dram_base + TEXT_OFFSET is only suitable if it doesn't result in the Image crossing a 512 MB alignment boundary, which could be the case if dram_base itself is close to the end of a naturally aligned 512 MB region. Also, when moving the kernel Image, we need to verify that the new destination region does not cross a 512 MB alignment boundary either. If that is the case, we retry the allocation with the alignment chosen such that the resulting region will always be suitable. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/efi-stub.c | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/arch/arm64/kernel/efi-stub.c b/arch/arm64/kernel/efi-stub.c index f5374065ad53..5f8175979be8 100644 --- a/arch/arm64/kernel/efi-stub.c +++ b/arch/arm64/kernel/efi-stub.c @@ -22,14 +22,40 @@ efi_status_t __init handle_kernel_image(efi_system_table_t *sys_table, efi_loaded_image_t *image) { efi_status_t status; - unsigned long kernel_size, kernel_memsize = 0; + unsigned long kernel_size, kernel_memsize; + unsigned long preferred_offset; - /* Relocate the image, if required. */ kernel_size = _edata - _text; - if (*image_addr != (dram_base + TEXT_OFFSET)) { - kernel_memsize = kernel_size + (_end - _edata); - status = efi_low_alloc(sys_table, kernel_memsize + TEXT_OFFSET, - SZ_2M, reserve_addr); + kernel_memsize = kernel_size + (_end - _edata); + + /* + * The kernel Image should be located as close as possible to the base + * of system RAM, but must not cross a 512 MB alignment boundary. + */ + preferred_offset = dram_base + TEXT_OFFSET; + if ((preferred_offset & (SZ_512M - 1)) + kernel_memsize > SZ_512M) + preferred_offset = round_up(dram_base, SZ_512M) + TEXT_OFFSET; + + if (*image_addr != preferred_offset) { + unsigned long alloc_size = kernel_memsize + TEXT_OFFSET; + unsigned long alloc_align = SZ_2M; + +again: + status = efi_low_alloc(sys_table, alloc_size, alloc_align, + reserve_addr); + + /* + * Check whether the new allocation crosses a 512 MB alignment + * boundary. If so, retry with the alignment set to a power of + * two upper bound of the allocation size. That is guaranteed + * to produce a suitable allocation, but may waste more memory. + */ + if (status == EFI_SUCCESS && + ((*reserve_addr & (SZ_512M - 1)) + alloc_size) > SZ_512M) { + efi_free(sys_table, alloc_size, *reserve_addr); + alloc_align = roundup_pow_of_two(alloc_size); + goto again; + } if (status != EFI_SUCCESS) { pr_efi_err(sys_table, "Failed to relocate kernel\n"); return status;