From patchwork Tue Jul 24 06:57:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 10541497 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7312114E2 for ; Tue, 24 Jul 2018 07:53:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6254D28773 for ; Tue, 24 Jul 2018 07:53:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5543A28777; Tue, 24 Jul 2018 07:53:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2B9FE28773 for ; Tue, 24 Jul 2018 07:53:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=WeOugFzigmxGq58N7ndkSr2BOhd1rzkmUQc2amySFxU=; b=FMWWGImXL1mLvw212+OlA9KtL8 4P1tKa7wuHQags4C2CKExsHsjbcs58e8VxiKupCXpphurgIuybvFM4MtTI6NU/IaJ+08neDpGSS0X 7XAYEJnfLVlJDdL42moNPPor8foV9UIMulZGk4E0ddUrDQPIRywFveLCZq7oVESUOOJ/34iwOfrpt 7KUpcM+NkKWTlV31LG0etPBGWSMzCJ/AalkMivaXFs3Ge9I6axAhzKDHcHglVv45uyk2zFev/+8u2 2EQJNsMMe/cooTLxxgqSpz6L1NshDe/lmxlZwrc0v7hFCujJCJg44QpxGBf0lEUqNr0bkngNJlHdB Or/rd78w==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fhs8U-0000ls-5i; Tue, 24 Jul 2018 07:53:34 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fhs89-0000PI-JD for linux-arm-kernel@bombadil.infradead.org; Tue, 24 Jul 2018 07:53:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Mz81qDhWmAiSNYpZZsOHlS2lDMScwrCRyCwmff186zc=; b=W8XqarNbaS/GHuew7gEy5dMFm 1ts8Aeys+EWwwVbV3GpNZZ6p4PYNMuk2okH/mppTKeTaOXAN4kZfARH7UnPlTG1JoLfgyGL8ZHpae iRqNxgtOOcaMiPgw8Q8nHuwbq+LSF01QpKbRE4OYkFEWVFvY8eIPYau0D45o6VfqrbdkjMX4tNCay binp8KmuUVGPXvvkSEjTUWwacsmYGNr6bswgrjFH16idjzRUncAt3iASbB3izZR4JQ7o+NN6St05p OGcupWtn/oh1FQefbeWK+bZ/685b7vdFxtjpKPawp8dRx6gAC3x73lBWWDoMCvFGem1TlWUGVK+zu zEou2jDEA==; Received: from mail-pl0-x241.google.com ([2607:f8b0:400e:c01::241]) by merlin.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fhrHS-0005fG-BN for linux-arm-kernel@lists.infradead.org; Tue, 24 Jul 2018 06:58:47 +0000 Received: by mail-pl0-x241.google.com with SMTP id o7-v6so1326445plk.10 for ; Mon, 23 Jul 2018 23:58:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Mz81qDhWmAiSNYpZZsOHlS2lDMScwrCRyCwmff186zc=; b=RQ5UcAXcbSjT7kEJqmhN965LpvOYV0Z3cRcGDGFFYVgWqyJibRLBHN7P/dzcX8Lig8 H6hdnIASudmJfZSMpKKwT7IMQfPIIZKbiQXoPMPBaJbnMIB/Q7+L5wJHC+4dNRP9hiOT 5KdDsi1ozfgrdztYwNUOZhOBlVcoBZcLDA/cA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Mz81qDhWmAiSNYpZZsOHlS2lDMScwrCRyCwmff186zc=; b=CqZL/zNtHj959yXWjJr3CSWojTvemJnqZ3upHlWKvWNLgFfMIw/HuNfqnRcP3nDuU9 +zOgMJoMW2SFio9hXinsTWrwdo6Lotco+jd2xacqt7g2fe2rTI0a1wZzkx0OOf30dZjM 96dRIdRYEoGdwtPGYt2z0+s7XLxWBZiggxe4BUmlyt9XYccxwdx9URErutcR5lQ9x6K5 /CFTgsIQBxMZn65dJLdedoff8hDRdF1BeaJGdI3sxekE6Jc2CWfLHHujggkV0g7TQVix 5bXW7/3R4Hj+gS5UC0O4Zl3KvpNUmvYFih7nrwMJVtzDlUjF0cMNYxiWTY/YGrgvgzL6 xpVQ== X-Gm-Message-State: AOUpUlHeRAWZNfVleiQzjFHn0DvrEt3EUJDqk00bLF8mr/Pr+kYHjfrl jmHQUg7w69tqpBwbw8C2ejD3gA== X-Google-Smtp-Source: AAOMgpeY6qMjDeti+aFQMQ0Lelief7UBB0CwC1tde/LJjK9Snmwgsw9zjfZlsFRSSWVFSbdvVPvrrg== X-Received: by 2002:a17:902:7482:: with SMTP id h2-v6mr16071369pll.185.1532415513949; Mon, 23 Jul 2018 23:58:33 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id 75-v6sm18663643pfr.115.2018.07.23.23.58.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Jul 2018 23:58:33 -0700 (PDT) From: AKASHI Takahiro To: catalin.marinas@arm.com, will.deacon@arm.com, dhowells@redhat.com, vgoyal@redhat.com, herbert@gondor.apana.org.au, davem@davemloft.net, dyoung@redhat.com, bhe@redhat.com, arnd@arndb.de, schwidefsky@de.ibm.com, heiko.carstens@de.ibm.com Subject: [PATCH v12 10/16] arm64: kexec_file: load initrd and device-tree Date: Tue, 24 Jul 2018 15:57:53 +0900 Message-Id: <20180724065759.19186-11-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180724065759.19186-1-takahiro.akashi@linaro.org> References: <20180724065759.19186-1-takahiro.akashi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180724_025846_418683_C22D30DD X-CRM114-Status: GOOD ( 22.69 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: ard.biesheuvel@linaro.org, bhsharma@redhat.com, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro , james.morse@arm.com, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP load_other_segments() is expected to allocate and place all the necessary memory segments other than kernel, including initrd and device-tree blob (and elf core header for crash). While most of the code was borrowed from kexec-tools' counterpart, users may not be allowed to specify dtb explicitly, instead, the dtb presented by the original boot loader is reused. arch_kimage_kernel_post_load_cleanup() is responsible for freeing arm64- specific data allocated in load_other_segments(). Signed-off-by: AKASHI Takahiro Cc: Catalin Marinas Cc: Will Deacon Reviewed-by: James Morse --- arch/arm64/include/asm/kexec.h | 17 +++ arch/arm64/kernel/machine_kexec_file.c | 182 +++++++++++++++++++++++++ 2 files changed, 199 insertions(+) diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index e17f0529a882..026f7e408f0c 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -93,6 +93,23 @@ static inline void crash_prepare_suspend(void) {} static inline void crash_post_resume(void) {} #endif +#ifdef CONFIG_KEXEC_FILE +#define ARCH_HAS_KIMAGE_ARCH + +struct kimage_arch { + void *dtb_buf; + unsigned long dtb_mem; +}; + +struct kimage; + +extern int arch_kimage_file_post_load_cleanup(struct kimage *image); +extern int load_other_segments(struct kimage *image, + unsigned long kernel_load_addr, unsigned long kernel_size, + char *initrd, unsigned long initrd_len, + char *cmdline, unsigned long cmdline_len); +#endif + #endif /* __ASSEMBLY__ */ #endif diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c index c38a8048ed00..b28fbb0659c9 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -5,12 +5,194 @@ * Copyright (C) 2018 Linaro Limited * Author: AKASHI Takahiro * + * Most code is derived from arm64 port of kexec-tools */ #define pr_fmt(fmt) "kexec_file: " fmt +#include +#include #include +#include +#include +#include +#include +#include const struct kexec_file_ops * const kexec_file_loaders[] = { NULL }; + +int arch_kimage_file_post_load_cleanup(struct kimage *image) +{ + vfree(image->arch.dtb); + image->arch.dtb = NULL; + + return kexec_image_post_load_cleanup_default(image); +} + +static int setup_dtb(struct kimage *image, + unsigned long initrd_load_addr, unsigned long initrd_len, + char *cmdline, unsigned long cmdline_len, + void **dtb_buf, unsigned long *dtb_buf_len) +{ + void *buf = NULL; + size_t buf_size; + int nodeoffset; + int ret; + + /* duplicate dt blob */ + buf_size = fdt_totalsize(initial_boot_params); + + if (initrd_load_addr) { + /* can be redundant, but trimmed at the end */ + buf_size += fdt_prop_len("linux,initrd-start", sizeof(u64)); + buf_size += fdt_prop_len("linux,initrd-end", sizeof(u64)); + } + + if (cmdline) + /* can be redundant, but trimmed at the end */ + buf_size += fdt_prop_len("bootargs", cmdline_len); + + buf = vmalloc(buf_size); + if (!buf) { + ret = -ENOMEM; + goto out_err; + } + + ret = fdt_open_into(initial_boot_params, buf, buf_size); + if (ret) { + ret = -EINVAL; + goto out_err; + } + + nodeoffset = fdt_path_offset(buf, "/chosen"); + if (nodeoffset < 0) { + ret = -EINVAL; + goto out_err; + } + + /* add bootargs */ + if (cmdline) { + ret = fdt_setprop_string(buf, nodeoffset, "bootargs", cmdline); + if (ret) { + ret = -EINVAL; + goto out_err; + } + } else { + ret = fdt_delprop(buf, nodeoffset, "bootargs"); + if (ret && (ret != -FDT_ERR_NOTFOUND)) { + ret = -EINVAL; + goto out_err; + } + } + + /* add initrd-* */ + if (initrd_load_addr) { + ret = fdt_setprop_u64(buf, nodeoffset, "linux,initrd-start", + initrd_load_addr); + if (ret) { + ret = -EINVAL; + goto out_err; + } + + ret = fdt_setprop_u64(buf, nodeoffset, "linux,initrd-end", + initrd_load_addr + initrd_len); + if (ret) { + ret = -EINVAL; + goto out_err; + } + } else { + ret = fdt_delprop(buf, nodeoffset, "linux,initrd-start"); + if (ret && (ret != -FDT_ERR_NOTFOUND)) { + ret = -EINVAL; + goto out_err; + } + + ret = fdt_delprop(buf, nodeoffset, "linux,initrd-end"); + if (ret && (ret != -FDT_ERR_NOTFOUND)) { + ret = -EINVAL; + goto out_err; + } + } + + /* trim a buffer */ + fdt_pack(buf); + *dtb_buf = buf; + *dtb_buf_len = fdt_totalsize(buf); + + return 0; + +out_err: + vfree(buf); + return ret; +} + +int load_other_segments(struct kimage *image, + unsigned long kernel_load_addr, + unsigned long kernel_size, + char *initrd, unsigned long initrd_len, + char *cmdline, unsigned long cmdline_len) +{ + struct kexec_buf kbuf; + void *dtb = NULL; + unsigned long initrd_load_addr = 0, dtb_len; + int ret = 0; + + kbuf.image = image; + /* not allocate anything below the kernel */ + kbuf.buf_min = kernel_load_addr + kernel_size; + + /* load initrd */ + if (initrd) { + kbuf.buffer = initrd; + kbuf.bufsz = initrd_len; + kbuf.mem = 0; + kbuf.memsz = initrd_len; + kbuf.buf_align = 0; + /* within 1GB-aligned window of up to 32GB in size */ + kbuf.buf_max = round_down(kernel_load_addr, SZ_1G) + + (unsigned long)SZ_1G * 32; + kbuf.top_down = false; + + ret = kexec_add_buffer(&kbuf); + if (ret) + goto out_err; + initrd_load_addr = kbuf.mem; + + pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n", + initrd_load_addr, initrd_len, initrd_len); + } + + /* load dtb blob */ + ret = setup_dtb(image, initrd_load_addr, initrd_len, + cmdline, cmdline_len, &dtb, &dtb_len); + if (ret) { + pr_err("Preparing for new dtb failed\n"); + goto out_err; + } + + kbuf.buffer = dtb; + kbuf.bufsz = dtb_len; + kbuf.mem = 0; + kbuf.memsz = dtb_len; + /* not across 2MB boundary */ + kbuf.buf_align = SZ_2M; + kbuf.buf_max = ULONG_MAX; + kbuf.top_down = true; + + ret = kexec_add_buffer(&kbuf); + if (ret) + goto out_err; + image->arch.dtb = dtb; + image->arch.dtb_mem = kbuf.mem; + + pr_debug("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n", + kbuf.mem, dtb_len, dtb_len); + + return 0; + +out_err: + vfree(dtb); + return ret; +}