From patchwork Thu Oct 29 02:59:46 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 55741 Delivered-To: patch@linaro.org Received: by 10.112.61.134 with SMTP id p6csp291694lbr; Wed, 28 Oct 2015 20:01:30 -0700 (PDT) X-Received: by 10.68.68.197 with SMTP id y5mr56909211pbt.88.1446087690788; Wed, 28 Oct 2015 20:01:30 -0700 (PDT) Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id qw9si74899176pbb.172.2015.10.28.20.01.30 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Oct 2015 20:01: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; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) smtp.mailfrom=linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org; dkim=neutral (body hash did not verify) header.i=@linaro_org.20150623.gappssmtp.com 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 1ZrdRm-0007GN-W5; Thu, 29 Oct 2015 03:00:15 +0000 Received: from mail-io0-x22b.google.com ([2607:f8b0:4001:c06::22b]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZrdRg-00066c-T2 for linux-arm-kernel@lists.infradead.org; Thu, 29 Oct 2015 03:00:10 +0000 Received: by iodd200 with SMTP id d200so32028642iod.0 for ; Wed, 28 Oct 2015 19:59:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro_org.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=2U/8lgieXEs10XaDXo+cNDaDMsv1tq+2ORgpKcs67bM=; b=BAaultcQhPfhfuzu6/g1R8w5f18i2G6xpO/ir/5fd5R82t3vfhhYnJEbHhSUMUdIiz rUAZMWfcZFMf+V1i5mMPevJTyIL+54Mgd/T9em9gsKdMr79n3Yze4P9fCA94siH3JMqy eQinOyFZtUIt6/neEfTnMXff51FS3fxWWzMWcUUXPf5Nt1leLLb3a3G89MvCqGCwK9m/ yKwrQ2miJhP7WZ6jwqkufLm0rDGz89HvDmj2Xpu5oPn9mejOARI/kTIMv5YwK+/AMpdV 1AbuBwbyRO1bC51eCnIOH/40bB+qQpbCsacTLntEIvtxajMj8w2Hq1GntsbBV5yrJkki EdHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=2U/8lgieXEs10XaDXo+cNDaDMsv1tq+2ORgpKcs67bM=; b=h+ODQi+UvErpTFNMzKH54r1/VsjTp+F2Ot4Pw4h7h/nCORvh+aQO8c6Iw4q0Jo+IHj C0PfNTyxJLFaRwXTelDcN/Og4aGPToOd1EICOZlarnWOJEQG8ELVCbsQkpWwmoz29oJ2 PkC0+KgnV+Pg5B85MdVkGNPJpuC+/IlMezx+ViTNfdNFNwwFjaD8/OJ4eQIAckECLaw7 FQ5IRXh31DqHpzlLsL0XjrAWTCn9+pEl9LIcroSI9zC+XTPg4yZg9j84dLv2Ii43WSqY r3yryja1Gt9zCQnx+tjPLFdp6aag3041DFO+Z9fWpq1HwtKhZJX8XwU93j8WjV1BTfdd lLXg== X-Gm-Message-State: ALoCoQl/Hv7q5WYvcWarsNmqHiSHPR5ZajOj/qd9nsBeNPLouEQ6slRz9T5AVs/2/wMqhbGem895 MIME-Version: 1.0 X-Received: by 10.107.164.81 with SMTP id n78mr829910ioe.130.1446087586489; Wed, 28 Oct 2015 19:59:46 -0700 (PDT) Received: by 10.36.214.215 with HTTP; Wed, 28 Oct 2015 19:59:46 -0700 (PDT) In-Reply-To: <20151028182139.GC26777@leverpostej> References: <1446053844-27281-1-git-send-email-timur@codeaurora.org> <20151028180836.GB26777@leverpostej> <56311014.9020400@codeaurora.org> <20151028182139.GC26777@leverpostej> Date: Thu, 29 Oct 2015 11:59:46 +0900 Message-ID: Subject: Re: [PATCH] [v2] arm64: efi: make sure vmlinux load address aligned on 2MB From: Ard Biesheuvel To: Mark Rutland X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151028_200009_117182_5846D0E7 X-CRM114-Status: GOOD ( 23.18 ) 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 [2607:f8b0:4001:c06:0:0:0:22b listed in] [list.dnswl.org] -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.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 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: Matt Fleming , Mark Langsdorf , Jon Masters , Timur Tabi , Will Deacon , "linux-efi@vger.kernel.org" , Shanker Donthineni , Mark Salter , "linux-arm-kernel@lists.infradead.org" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org On 29 October 2015 at 03:21, Mark Rutland wrote: > On Wed, Oct 28, 2015 at 01:12:36PM -0500, Timur Tabi wrote: >> On 10/28/2015 01:08 PM, Mark Rutland wrote: >> >> >arm64: efi: ensure kernel is loaded at correct address >> > >> >The kernel image needs to be loaded text_offset_bytes from a 2M-aligned >> >base, per Documentation/arm64/booting.txt. If loaded at the wrong offset >> >modulo 2M, __create_page_tables will create incorrect page tables. >> > >> >The EFI stub implicitly assumes that dram_base (i.e. the lowest address >> >with a EFI_MEMORY_WB attribute) is 2M-aligned, and tries to load the >> >kernel at dram_base + TEXT_OFFSET. If dram_base is not 2M-aligned, the >> >kernel will be loaded at the wrong offset from 2M. >> >> Thanks, I'll use that. I messed up a couple other things, so I need >> to send out a v3 anyway. >> >> >>- *image_addr = *reserve_addr = dram_base + TEXT_OFFSET; >> >>+ *image_addr = *reserve_addr = >> >>+ round_up(dram_base, SZ_2M) + TEXT_OFFSET; >> > >> >We also need to fix the test for whether we need to relocate the kernel: >> >(*image_addr != (dram_base + TEXT_OFFSET)). >> > >> >When dram_base is not 2M aligned, that is broken, and it's been broken >> >since it was introduced in commit 3c7f255039a2ad6e ("arm64: efi: add EFI >> >stub") in v3.16. >> > >> >It's a bit hideous to fix the general case, though, it seems. >> >> Um, so I should I do something more in my v3 patch, or is this a >> change for a different patch? > > I think there should be a single patch, but please hold off v3 for a day > or so. I think there a few more edge cases here, and I'm currently > investigating. > Apologies for the drive-by nature of my contributions to this thread. I am currently travelling. I think the below should address both issues (and I even tried to compile it this time) -----------------8<----------------- _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel diff --git a/arch/arm64/kernel/efi-stub.c b/arch/arm64/kernel/efi-stub.c index 816120ece6bc..78dfbd34b6bf 100644 --- a/arch/arm64/kernel/efi-stub.c +++ b/arch/arm64/kernel/efi-stub.c @@ -25,10 +25,20 @@ unsigned long kernel_size, kernel_memsize = 0; unsigned long nr_pages; void *old_image_addr = (void *)*image_addr; + unsigned long preferred_offset; + + /* + * The preferred offset of the kernel Image is TEXT_OFFSET bytes beyond + * a 2 MB aligned base, which itself may be lower than dram_base, as + * long as the resulting offset equals or exceeds it. + */ + preferred_offset = round_down(dram_base, SZ_2M) + TEXT_OFFSET; + if (preferred_offset < dram_base) + preferred_offset += SZ_2M; /* Relocate the image, if required. */ kernel_size = _edata - _text; - if (*image_addr != (dram_base + TEXT_OFFSET)) { + if (*image_addr != preferred_offset) { kernel_memsize = kernel_size + (_end - _edata); /* @@ -42,7 +52,7 @@ * Mustang), we can still place the kernel at the address * 'dram_base + TEXT_OFFSET'. */ - *image_addr = *reserve_addr = dram_base + TEXT_OFFSET; + *image_addr = *reserve_addr = preferred_offset; nr_pages = round_up(kernel_memsize, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE; status = efi_call_early(allocate_pages, EFI_ALLOCATE_ADDRESS,