From patchwork Fri Jul 24 11:38:27 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 51430 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by patches.linaro.org (Postfix) with ESMTPS id 35C0420323 for ; Fri, 24 Jul 2015 11:40:34 +0000 (UTC) Received: by lbbvz8 with SMTP id vz8sf6598026lbb.2 for ; Fri, 24 Jul 2015 04:40:33 -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 :precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:cc:mime-version:content-type :content-transfer-encoding:sender:errors-to:x-original-sender :x-original-authentication-results:mailing-list; bh=4IMbnxOTHnfk5hB619bqiUcEb7HYq7pzVbFX8EhKaas=; b=kEwU2MCtV1v0NWmSDyAC4VeAmzwad0AwXwk5HqfON4qQc4fxD1u1vcNAw877SNiGdE bIqEuz2Vtp8qPpJuUX2w2qSM50We4I0oXwz6sIAL3o3XPeYNVYTxlYLa9W2cO2OeJngv DF8mpbSqqjpAJIlS7akGylhWFB5Wp6x8LfFF82q4Y7J8fucpUVqi1wzSsYFQ85wl97uE lhceWRD0BPpZn9T0wpMu3kMQBkTfROO+N63mMdgiDM7KU3KLApPwrNk5CU8xaNsQ68hV g9Un8/GmvHX1TfISP2t7V3Fsm/R2ZGn6ofZRHH0BU+aN0qfytnXpwpM7T0xpk7TH9bkT +szw== X-Gm-Message-State: ALoCoQl00vFnPemdc4kbBGAHEYs+TSxAfbjjcV4i4u4R5+VUW/3RUXeTXp0Nevnjsus3yIT7cXRl X-Received: by 10.152.120.69 with SMTP id la5mr6004121lab.7.1437738033183; Fri, 24 Jul 2015 04:40:33 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.44.197 with SMTP id g5ls235423lam.99.gmail; Fri, 24 Jul 2015 04:40:32 -0700 (PDT) X-Received: by 10.112.47.73 with SMTP id b9mr13174425lbn.46.1437738032904; Fri, 24 Jul 2015 04:40:32 -0700 (PDT) Received: from mail-la0-f43.google.com (mail-la0-f43.google.com. [209.85.215.43]) by mx.google.com with ESMTPS id i6si7304411lai.30.2015.07.24.04.40.32 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Jul 2015 04:40:32 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.43 as permitted sender) client-ip=209.85.215.43; Received: by lahh5 with SMTP id h5so12467926lah.2 for ; Fri, 24 Jul 2015 04:40:32 -0700 (PDT) X-Received: by 10.112.198.74 with SMTP id ja10mr13203129lbc.19.1437738032788; Fri, 24 Jul 2015 04:40:32 -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.7.198 with SMTP id l6csp1120115lba; Fri, 24 Jul 2015 04:40:31 -0700 (PDT) X-Received: by 10.70.88.142 with SMTP id bg14mr30063832pdb.106.1437738030102; Fri, 24 Jul 2015 04:40:30 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id b12si20093189pat.64.2015.07.24.04.40.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Jul 2015 04:40:30 -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 1ZIbJe-0007fl-QO; Fri, 24 Jul 2015 11:39:02 +0000 Received: from mail-wi0-f178.google.com ([209.85.212.178]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZIbJb-0007Vd-8i for linux-arm-kernel@lists.infradead.org; Fri, 24 Jul 2015 11:39:00 +0000 Received: by wibxm9 with SMTP id xm9so24949741wib.0 for ; Fri, 24 Jul 2015 04:38:37 -0700 (PDT) X-Received: by 10.194.205.225 with SMTP id lj1mr25699597wjc.138.1437737917098; Fri, 24 Jul 2015 04:38:37 -0700 (PDT) Received: from localhost.localdomain ([185.13.106.68]) by smtp.gmail.com with ESMTPSA id k2sm3130763wif.4.2015.07.24.04.38.34 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 24 Jul 2015 04:38:36 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, mark.rutland@arm.com, msalter@redhat.com, linux-efi@vger.kernel.org, matt.fleming@intel.com, will.deacon@arm.com Subject: [PATCH v2] arm64/efi: prefer AllocatePages() over efi_low_alloc() for vmlinux Date: Fri, 24 Jul 2015 13:38:27 +0200 Message-Id: <1437737907-10477-1-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.9.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150724_043859_491727_DD8434DD X-CRM114-Status: GOOD ( 17.68 ) 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 [209.85.212.178 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [209.85.212.178 listed in wl.mailspike.net] -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.0 RCVD_IN_MSPIKE_WL Mailspike good senders X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Cc: Ard Biesheuvel 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.43 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 When allocating memory for the kernel image, try the AllocatePages() boot service to obtain memory at the preferred offset of 'dram_base + TEXT_OFFSET', and only revert to efi_low_alloc() if that fails. This is the only way to allocate at the base of DRAM if DRAM starts at 0x0, since efi_low_alloc() refuses to allocate at 0x0. Tested-by: Haojian Zhuang Signed-off-by: Ard Biesheuvel --- v2: - reshuffle code flow to make it more logical, and have only a single memcpy() invocation at the end of the function --- arch/arm64/kernel/efi-stub.c | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kernel/efi-stub.c b/arch/arm64/kernel/efi-stub.c index f5374065ad53..816120ece6bc 100644 --- a/arch/arm64/kernel/efi-stub.c +++ b/arch/arm64/kernel/efi-stub.c @@ -13,7 +13,7 @@ #include #include -efi_status_t __init handle_kernel_image(efi_system_table_t *sys_table, +efi_status_t __init handle_kernel_image(efi_system_table_t *sys_table_arg, unsigned long *image_addr, unsigned long *image_size, unsigned long *reserve_addr, @@ -23,21 +23,44 @@ efi_status_t __init handle_kernel_image(efi_system_table_t *sys_table, { efi_status_t status; unsigned long kernel_size, kernel_memsize = 0; + unsigned long nr_pages; + void *old_image_addr = (void *)*image_addr; /* 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); + + /* + * First, try a straight allocation at the preferred offset. + * This will work around the issue where, if dram_base == 0x0, + * efi_low_alloc() refuses to allocate at 0x0 (to prevent the + * address of the allocation to be mistaken for a FAIL return + * value or a NULL pointer). It will also ensure that, on + * platforms where the [dram_base, dram_base + TEXT_OFFSET) + * interval is partially occupied by the firmware (like on APM + * Mustang), we can still place the kernel at the address + * 'dram_base + TEXT_OFFSET'. + */ + *image_addr = *reserve_addr = dram_base + TEXT_OFFSET; + nr_pages = round_up(kernel_memsize, EFI_ALLOC_ALIGN) / + EFI_PAGE_SIZE; + status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS, + EFI_LOADER_DATA, nr_pages, + (efi_physical_addr_t *)reserve_addr); if (status != EFI_SUCCESS) { - pr_efi_err(sys_table, "Failed to relocate kernel\n"); - return status; + kernel_memsize += TEXT_OFFSET; + status = efi_low_alloc(sys_table_arg, kernel_memsize, + SZ_2M, reserve_addr); + + if (status != EFI_SUCCESS) { + pr_efi_err(sys_table_arg, "Failed to relocate kernel\n"); + return status; + } + *image_addr = *reserve_addr + TEXT_OFFSET; } - memcpy((void *)*reserve_addr + TEXT_OFFSET, (void *)*image_addr, - kernel_size); - *image_addr = *reserve_addr + TEXT_OFFSET; - *reserve_size = kernel_memsize + TEXT_OFFSET; + memcpy((void *)*image_addr, old_image_addr, kernel_size); + *reserve_size = kernel_memsize; }