From patchwork Wed Mar 11 16:00:19 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 45687 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f69.google.com (mail-wg0-f69.google.com [74.125.82.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id CBF15214BF for ; Wed, 11 Mar 2015 16:00:55 +0000 (UTC) Received: by wghk14 with SMTP id k14sf7321686wgh.3 for ; Wed, 11 Mar 2015 09:00:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=jzfoLfGimgHD2PVXwZ6Uq31qcmdgHJ0pquI63IOlbd0=; b=mUwUt/IKLh0gopFk3QOWaaTLznBivgefwdTnRPgzgTzJCIqOH3zoSaFPI/s1FF160F 2Dsmj2oBqol49gz2+0s7WLh1/Shy8yS6BzmYT+Sz5PNOI9fRUgeqiMmtBy1qoKM6R8CF N7Zli8XpwGuORXd6T71x1F9dWbeV/EbE+hwVDGqr8odIAktrFbtVwx/kR8Pp9c8F00/w piltBdSxdXxwnL6On5AJ9WsDVYuwuYAYMVMkmPOn3xZEk1uKRNgdYBVPm0WpHgwLywm+ MPcpi25HEb83J6wXDKptcP5B0O6M8Kb7Di+QuEO8ohsSqToPVngE+80cECKk3/YpLu1W gEZA== X-Gm-Message-State: ALoCoQn9zqGGScDRjHtIgUUAKDd5ogrmElGW42rcD5yE1xndhD7JuKnxdjqy8RbMkvaVDId52peP X-Received: by 10.152.4.229 with SMTP id n5mr5610908lan.1.1426089655114; Wed, 11 Mar 2015 09:00:55 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.8.169 with SMTP id s9ls183238laa.8.gmail; Wed, 11 Mar 2015 09:00:54 -0700 (PDT) X-Received: by 10.112.42.164 with SMTP id p4mr28742690lbl.119.1426089654970; Wed, 11 Mar 2015 09:00:54 -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 pp6si2559479lbb.143.2015.03.11.09.00.54 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Mar 2015 09:00:54 -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 labmn12 with SMTP id mn12so9815234lab.8 for ; Wed, 11 Mar 2015 09:00:54 -0700 (PDT) X-Received: by 10.152.22.67 with SMTP id b3mr34500344laf.117.1426089654871; Wed, 11 Mar 2015 09:00:54 -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.35.133 with SMTP id h5csp2869401lbj; Wed, 11 Mar 2015 09:00:54 -0700 (PDT) X-Received: by 10.60.92.71 with SMTP id ck7mr19391150oeb.14.1426089641978; Wed, 11 Mar 2015 09:00:41 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id cm5si6042889pdb.228.2015.03.11.09.00.41; Wed, 11 Mar 2015 09:00:41 -0700 (PDT) Received-SPF: none (google.com: devicetree-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752076AbbCKQAk (ORCPT + 5 others); Wed, 11 Mar 2015 12:00:40 -0400 Received: from mail-we0-f179.google.com ([74.125.82.179]:42969 "EHLO mail-we0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751423AbbCKQAj (ORCPT ); Wed, 11 Mar 2015 12:00:39 -0400 Received: by wesq59 with SMTP id q59so10245505wes.9 for ; Wed, 11 Mar 2015 09:00:38 -0700 (PDT) X-Received: by 10.194.221.100 with SMTP id qd4mr77430236wjc.113.1426089638825; Wed, 11 Mar 2015 09:00:38 -0700 (PDT) Received: from ards-macbook-pro.local ([213.143.60.209]) by mx.google.com with ESMTPSA id l6sm5990041wjx.33.2015.03.11.09.00.36 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 11 Mar 2015 09:00:38 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, robh@kernel.org, mark.rutland@arm.com, catalin.marinas@arm.com, will.deacon@arm.com, linux@arm.linux.org.uk, linuxppc-dev@lists.ozlabs.org, devicetree@vger.kernel.org Cc: Ard Biesheuvel Subject: [PATCH v2 4/5] arm64/efi: ensure that Image does not cross a 512 MB boundary Date: Wed, 11 Mar 2015 17:00:19 +0100 Message-Id: <1426089620-9459-5-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1426089620-9459-1-git-send-email-ard.biesheuvel@linaro.org> References: <1426089620-9459-1-git-send-email-ard.biesheuvel@linaro.org> Sender: devicetree-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: devicetree@vger.kernel.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 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Update the Image placement logic used by the stub to make absolutely sure that the Image is placed such that the early init code will always be able to map it. This means the entire static memory footprint of the 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 | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kernel/efi-stub.c b/arch/arm64/kernel/efi-stub.c index f5374065ad53..3b67ca4e2f2e 100644 --- a/arch/arm64/kernel/efi-stub.c +++ b/arch/arm64/kernel/efi-stub.c @@ -21,15 +21,40 @@ efi_status_t __init handle_kernel_image(efi_system_table_t *sys_table, unsigned long dram_base, efi_loaded_image_t *image) { + const unsigned long kernel_size = _edata - _text; + const unsigned long kernel_memsize = _end - _text; + unsigned long preferred_offset; efi_status_t status; - unsigned long kernel_size, kernel_memsize = 0; - - /* 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); + + /* + * The kernel Image should be located as close as possible to the + * base of system RAM, but its static memory footprint 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) { + const unsigned long alloc_size = kernel_memsize + TEXT_OFFSET; + + status = efi_low_alloc(sys_table, alloc_size, SZ_2M, + 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); + + status = efi_low_alloc(sys_table, alloc_size, + roundup_pow_of_two(alloc_size), + reserve_addr); + } if (status != EFI_SUCCESS) { pr_efi_err(sys_table, "Failed to relocate kernel\n"); return status;