From patchwork Fri Jul 24 09:41:53 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 51409 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f198.google.com (mail-lb0-f198.google.com [209.85.217.198]) by patches.linaro.org (Postfix) with ESMTPS id D729220323 for ; Fri, 24 Jul 2015 09:43:54 +0000 (UTC) Received: by lbcjj5 with SMTP id jj5sf5647705lbc.1 for ; Fri, 24 Jul 2015 02:43:53 -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=eFtmqP55d0PiNVf4XQlHNj4xgd64wXf0lTGPEEqoVH4=; b=Qh/YNdcTgPrU0XlRpvXd+ln0c4Cc5t9psQxH4jNfAX6/rA5Mijft73ogrg/wg36cLs y7r7wTz8HfAJej7EvrE05RKoM+UiWF1lxt5VNBAbgAir4V/WhngyrwN9EFPzKOrnP3JV 0LxN1BUznpQQlDZy1VWbpexFELrz0sClfzAFUGnwdxBrPHIf6FYbzOH5W8j9ugYKsjiW 7PlIcUiXUzOh9gXpVuzB6guFw16o7GEayL01YCzvdmWNEZClircu18PCCA6QC+sDprkb NT19u4uIHE8yeSbh1qM88a1vKtf+mX8oQmZzfurZX2QTO/kPtp/zJpl+9uq58dQ/TpzY S3Uw== X-Gm-Message-State: ALoCoQkqnc5MSE/ycDXFV0dhYipmZ9Dhh+/G2nJ/cYgpSMa5B6270kTilzVJq9jn5e91HVkyuB0d X-Received: by 10.112.148.101 with SMTP id tr5mr5914591lbb.13.1437731033829; Fri, 24 Jul 2015 02:43:53 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.4.227 with SMTP id n3ls377830lan.0.gmail; Fri, 24 Jul 2015 02:43:53 -0700 (PDT) X-Received: by 10.112.50.66 with SMTP id a2mr12893056lbo.68.1437731033613; Fri, 24 Jul 2015 02:43:53 -0700 (PDT) Received: from mail-la0-f48.google.com (mail-la0-f48.google.com. [209.85.215.48]) by mx.google.com with ESMTPS id h9si7034544lam.85.2015.07.24.02.43.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Jul 2015 02:43:53 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.48 as permitted sender) client-ip=209.85.215.48; Received: by laah7 with SMTP id h7so10923105laa.0 for ; Fri, 24 Jul 2015 02:43:53 -0700 (PDT) X-Received: by 10.152.25.228 with SMTP id f4mr12574898lag.112.1437731033466; Fri, 24 Jul 2015 02:43:53 -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 l6csp1055401lba; Fri, 24 Jul 2015 02:43:52 -0700 (PDT) X-Received: by 10.50.56.10 with SMTP id w10mr4999016igp.3.1437731031943; Fri, 24 Jul 2015 02:43:51 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id nx10si19393105pdb.51.2015.07.24.02.43.50 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Jul 2015 02:43:51 -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 1ZIZUw-0000hE-Ux; Fri, 24 Jul 2015 09:42:34 +0000 Received: from mail-wi0-f169.google.com ([209.85.212.169]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZIZUt-0000Tv-6e for linux-arm-kernel@lists.infradead.org; Fri, 24 Jul 2015 09:42:32 +0000 Received: by wicmv11 with SMTP id mv11so57438205wic.0 for ; Fri, 24 Jul 2015 02:42:08 -0700 (PDT) X-Received: by 10.181.12.111 with SMTP id ep15mr5296928wid.15.1437730928120; Fri, 24 Jul 2015 02:42:08 -0700 (PDT) Received: from localhost.localdomain ([185.13.106.68]) by smtp.gmail.com with ESMTPSA id bg6sm11744960wjc.13.2015.07.24.02.42.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 24 Jul 2015 02:42:07 -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] arm64/efi: prefer AllocatePages() over efi_low_alloc() for vmlinux Date: Fri, 24 Jul 2015 11:41:53 +0200 Message-Id: <1437730913-18077-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_024231_406433_6C87F240 X-CRM114-Status: GOOD ( 15.36 ) 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.169 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [209.85.212.169 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 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.48 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 --- arch/arm64/kernel/efi-stub.c | 47 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kernel/efi-stub.c b/arch/arm64/kernel/efi-stub.c index f5374065ad53..c8df74d14368 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,52 @@ 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; /* 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'. + // + *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) { + memcpy((void *)*reserve_addr, (void *)*image_addr, + kernel_size); + *image_addr = *reserve_addr; + *reserve_size = kernel_memsize; + } else { + status = efi_low_alloc(sys_table_arg, + kernel_memsize + TEXT_OFFSET, + SZ_2M, reserve_addr); + + if (status == EFI_SUCCESS) { + memcpy((void *)*reserve_addr + TEXT_OFFSET, + (void *)*image_addr, + kernel_size); + *image_addr = *reserve_addr + TEXT_OFFSET; + *reserve_size = kernel_memsize + TEXT_OFFSET; + } + } if (status != EFI_SUCCESS) { - pr_efi_err(sys_table, "Failed to relocate kernel\n"); + pr_efi_err(sys_table_arg, "Failed to relocate kernel\n"); return status; } - memcpy((void *)*reserve_addr + TEXT_OFFSET, (void *)*image_addr, - kernel_size); - *image_addr = *reserve_addr + TEXT_OFFSET; - *reserve_size = kernel_memsize + TEXT_OFFSET; }