From patchwork Wed Jul 11 07:41:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 10519091 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 496916028E for ; Wed, 11 Jul 2018 07:59:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 39E20289FB for ; Wed, 11 Jul 2018 07:59:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2BF8C28A0C; Wed, 11 Jul 2018 07:59:12 +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=unavailable 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 2DCCF289FB for ; Wed, 11 Jul 2018 07:59:11 +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=uRQcfAartbJex5q13Kk8c5E4zjKW6Auome+O21isRtc=; b=buwqHeupsX9KvxeuQDq+2RgASY 1gF/fO1l0unHnS91ConevCEO4ly/2HLSSP/jfPlXUlQToxct/AJrREq5MKwaIvHr0LDVEIAIX4i29 N6GSDrb4xaMuEgDM6T1Cdg+cQif359ypfKaXkI90Ob2o9IbgH/CAFeGCY28/uB1/GFnk2iI40rukG WpZ3epCoQQEdtNWLiYdTw9dF/KtIJhlfE1odaX5ZGnbvYirYv7BnalLSnU7RTuchMJjmMK1/Tg1H9 yjbb5PvkMFT7U87rW9F52Y8Mslvv5SNsxhKt+aNtamz5ZECXFV4EHslzgie8GygHfQws+CoGDPFmA PPcFWt6w==; 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 1fdA1j-0007br-Om; Wed, 11 Jul 2018 07:59:07 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fdA1E-0006ne-NS for linux-arm-kernel@bombadil.infradead.org; Wed, 11 Jul 2018 07:58:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.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=0PS6qjK3C7l7A2CIqkRFL2OjgXECavo5rLxY/13kKyg=; b=EItFnbVxy3lzeiifcDZprFGgz /w4UB2o6H8xT5raU+/rJ7g6xR2qS0TIPk27v+CRh3pKt/1EIe9IPpRdzQ3z++Mq5W7zMbENJ2GMv+ PgIGLHZriIuvGbXpYYCBDQOqW7jlwM5d7Ga6UnT0E8pbYACLuaEwaR8rU754gc5/1aXtf2ASa0QMS 2B2YXG7wrv2zkN6w/KAwt2DG0GuF1MrVsdcRW5kmxluJ4gX3UL5hfBxllZ/Y7QpIy+0Ot4fMBsgRP CrhA8hn+Jki0Jrwl2YwdVoMEf+eeKLLto5XkCBLBFJLl+zwQc6qd3JD92DBoXnH6EII15+ysT4MC1 7KiLGxUhw==; Received: from mail-pl0-x241.google.com ([2607:f8b0:400e:c01::241]) by casper.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fd9lz-0005Ai-Po for linux-arm-kernel@lists.infradead.org; Wed, 11 Jul 2018 07:42:54 +0000 Received: by mail-pl0-x241.google.com with SMTP id 6-v6so8801126plb.0 for ; Wed, 11 Jul 2018 00:42:41 -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=0PS6qjK3C7l7A2CIqkRFL2OjgXECavo5rLxY/13kKyg=; b=fte2i7vOo6y1qvB6gCpIOQEoM7stKeKyD896Gvfk0mpJPEe3JAvwdcIoODDdej7kuq shvasghY8A+/bec787iQKR/si1GwXNM2w8YuK/DRd60mLgGuMQhHoOQDidYxKZeRJm8a 7olb/Fnbn47MVuk5O5qLGyhy0V0Ez6CadBO6s= 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=0PS6qjK3C7l7A2CIqkRFL2OjgXECavo5rLxY/13kKyg=; b=NJkwgo+UigskqsKWjHEDm6ARyKeyu6h7nN2Na13wkpclZKCKQhpu04rW6a0GJMkqpH 5LSx8Zpc5gp7JMQTG2N/FwnUSD0XZA6L9D+R4wpRQ5TQGDS+q2nm+vBUc54Ii8sGVK2C XYs8boOir7Q+96KhCAF1oPk1he06/stit0ujec3Uks/DCoPzkkKWFRGYZRTytPWNJn23 BN2IHMn/KBzNRPpPu/JC2yKYHX0Zco/oz1Umy5J6S3L9uOzCbFRGu3V0u+AUn5XcNbN7 sxcqXa68/b1/ZXAxq98ila1amgnui66QdUHCx1RsxsFXOod4SppHQdJ+OyN2keKrhu49 uj0Q== X-Gm-Message-State: APt69E2TDQCdyReRqQA5Uuvr077QFG9Zd7BcvJxoMRVaxaFe/BQXBVL6 qBs2mrY7w3/Spp1vXPtOY6puxLGmM1g= X-Google-Smtp-Source: AAOMgpdjGNCM4bPEUE5ObkqA6fhStZeovPa1lFspn/11kqFaa5R9hHYj2WpZGbjV0FDzYHKUXNz6Bg== X-Received: by 2002:a17:902:d24:: with SMTP id 33-v6mr28182750plu.22.1531294960284; Wed, 11 Jul 2018 00:42:40 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id 10-v6sm44333804pfs.111.2018.07.11.00.42.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Jul 2018 00:42:39 -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 Subject: [PATCH v11 09/15] arm64: kexec_file: load initrd and device-tree Date: Wed, 11 Jul 2018 16:41:57 +0900 Message-Id: <20180711074203.3019-10-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180711074203.3019-1-takahiro.akashi@linaro.org> References: <20180711074203.3019-1-takahiro.akashi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180711_084251_839800_C57F5117 X-CRM114-Status: GOOD ( 20.62 ) 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 | 16 +++ arch/arm64/kernel/machine_kexec_file.c | 184 +++++++++++++++++++++++++ 2 files changed, 200 insertions(+) diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index e17f0529a882..01bbf6cebf12 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -93,6 +93,22 @@ 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 { + phys_addr_t dtb_mem; + void *dtb_buf; +}; + +struct kimage; + +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..ca00681c25c6 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -5,12 +5,196 @@ * 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_buf); + image->arch.dtb_buf = 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, + char **dtb_buf, size_t *dtb_buf_len) +{ + char *buf = NULL; + size_t buf_size; + int nodeoffset; + u64 value; + 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 + 1); + + 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) { + value = cpu_to_fdt64(initrd_load_addr); + ret = fdt_setprop_u64(buf, nodeoffset, "linux,initrd-start", + value); + if (ret) { + ret = -EINVAL; + goto out_err; + } + + value = cpu_to_fdt64(initrd_load_addr + initrd_len); + ret = fdt_setprop_u64(buf, nodeoffset, "linux,initrd-end", + value); + 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; + unsigned long initrd_load_addr = 0; + char *dtb = NULL; + unsigned long dtb_len = 0; + 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.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.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_mem = kbuf.mem; + image->arch.dtb_buf = dtb; + + 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; +}