From patchwork Wed Apr 25 06:26:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 134228 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp470978lji; Tue, 24 Apr 2018 23:27:20 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/nksrVFK7fEQj+1vYvlypzSE/4k+PqlpYwEUH3kZuDxI71mA8ocB60JHPuqP5S/3mQB8rq X-Received: by 10.98.242.13 with SMTP id m13mr7391913pfh.170.1524637640681; Tue, 24 Apr 2018 23:27:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524637640; cv=none; d=google.com; s=arc-20160816; b=SdS7J6KHJ4zz0EgtV1uHMIvgbtGQX6vXd3EE2izeg91UAPaj8tTTao0+vDwyUhKGrj 0cQX65z6OPXTSTUFFZT7dRw9GCzZ9B7+xsyKCILohPPUS6K3k6kiOOfTLh3bZ3cTCdmV miJbVJ017Sm8/t8Hxvr7dDiFMhTC5+/LZOrGvJPD2vhaSMj6h2Bi2QfF0vDD7DJ2PbTJ zHL9mEB8toEo8uFZesrSl/f+P2c3Wu5YuekZnQOtsb8I9baos5LB+CYf54HogT3XwP64 glcHfWMob4h17bzcGpAgv5eTWQJ/y4JCJUgB3xK/djLjE92UfUjgzR+uNz9rff+smk1M Qq6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=GxstDnPpKBuoMMwAmoI85QXMWYDKZl9Yr0/WjfQZahU=; b=SdPPqywqHc2DD6b1yPYlobvh//ywmaRv005gHosXKq/RCvW7CbdrH40lBg39bbunoc 7NDf6JgCZqKrs2fkOJisjRbimPF99nqhX33LnUz+BAqYOczCmJIHKzneFQikFFRzi+vg 3oytk/dPWQwQ8kSW8Kcx6xaXySnDuyuW6hSbzWNY4HnSOw+rsemIdXufado/z7VCGF4r RE4Tzrz9jrZkWpYVFsbc2u9QiPADcaS+cuwDFVDPHFao5MD5iav0WsLX3FXvTtQlligQ F4T7aB2snG1959QeToIABdLL3KhnP6lHXVEPZz8xa9ZMj+l0sPgWMZARmpkb9mx+1bnG zcWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O9esqQdG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o24si8177612pgn.36.2018.04.24.23.27.20; Tue, 24 Apr 2018 23:27:20 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=O9esqQdG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751512AbeDYG1S (ORCPT + 29 others); Wed, 25 Apr 2018 02:27:18 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:39379 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751323AbeDYG1M (ORCPT ); Wed, 25 Apr 2018 02:27:12 -0400 Received: by mail-pf0-f196.google.com with SMTP id z9so14319745pfe.6 for ; Tue, 24 Apr 2018 23:27:12 -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=GxstDnPpKBuoMMwAmoI85QXMWYDKZl9Yr0/WjfQZahU=; b=O9esqQdGUnlJZQLJPdJ3MqlFA02ztagtJc9NRoWcy2QjntdzemtUlw+7ZVXu7SAch8 oyRH9akUq5/OimLJWU6O7oeKvbRFvJOfP+hUT0KuNUse8IUKMUpHozUqMu9hb8JhMGuC ZPbAyz/ptejvOMg4tNDjHld4mEygsuxGLTHDE= 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=GxstDnPpKBuoMMwAmoI85QXMWYDKZl9Yr0/WjfQZahU=; b=VsdkKDtOOCUpw4+eNU/h9ymYt7RzBwUw7RmvFwLK3m3agrvCn8o3irODJSPhQwma+d YRAyezFzM76M0Y/xwLUQOiWFrWUhepRVe5wI1bDGw/L9aLgMLcjts7Ed2aMgZui5DLSt zZu3sTdnl+aDPP0+qXy5Sl6fLvGf26mf4A2LqKqEAhJel31XyF+N4qo4tc8mcQl0QvQu fFhS+/dYm2411N8qydAlzj71tl8rtn4cAJF4bFli2nHhSGKABIY5mlx7s1th7DmHbYJa A4Lc0vwNXQZD0GbhwoVMXaRFzVLkS6AJj3pnrfRlVuZ4zePCJK++0NAyMifvujf3zhdu pK6w== X-Gm-Message-State: ALQs6tAtE5fDlMtLZWRO74i7F0htzEe31e+ZUYfyglSqRWte0dQI6LBG T4TdE0jnlQ5P++XFDEXCSUZ2J3462nA= X-Received: by 2002:a17:902:684c:: with SMTP id f12-v6mr28188233pln.139.1524637631851; Tue, 24 Apr 2018 23:27:11 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id j125sm38606832pfg.188.2018.04.24.23.27.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 24 Apr 2018 23:27:11 -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 Cc: ard.biesheuvel@linaro.org, james.morse@arm.com, bhsharma@redhat.com, kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v9 05/11] arm64: kexec_file: load initrd and device-tree Date: Wed, 25 Apr 2018 15:26:23 +0900 Message-Id: <20180425062629.29404-6-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180425062629.29404-1-takahiro.akashi@linaro.org> References: <20180425062629.29404-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 a 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 --- arch/arm64/include/asm/kexec.h | 16 +++ arch/arm64/kernel/machine_kexec_file.c | 160 +++++++++++++++++++++++++ 2 files changed, 176 insertions(+) -- 2.17.0 diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index e17f0529a882..e4de1223715f 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 { + int kern_segment; + phys_addr_t dtb_mem; + void *dtb_buf; +}; + +struct kimage; + +extern int load_other_segments(struct kimage *image, + 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 f9ebf54ca247..b3b9b1725d8a 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -13,7 +13,26 @@ #include #include #include +#include #include +#include +#include +#include + +static int __dt_root_addr_cells; +static int __dt_root_size_cells; + +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); +} int arch_kexec_walk_mem(struct kexec_buf *kbuf, int (*func)(struct resource *, void *)) @@ -55,3 +74,144 @@ int arch_kexec_walk_mem(struct kexec_buf *kbuf, return ret; } + +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 range_len; + int ret; + + /* duplicate dt blob */ + buf_size = fdt_totalsize(initial_boot_params); + range_len = (__dt_root_addr_cells + __dt_root_size_cells) * sizeof(u32); + + if (initrd_load_addr) + buf_size += fdt_prop_len("linux,initrd-start", sizeof(u64)) + + fdt_prop_len("linux,initrd-end", sizeof(u64)); + + if (cmdline) + 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) + goto out_err; + + nodeoffset = fdt_path_offset(buf, "/chosen"); + if (nodeoffset < 0) + goto out_err; + + /* add bootargs */ + if (cmdline) { + ret = fdt_setprop(buf, nodeoffset, "bootargs", + cmdline, cmdline_len + 1); + if (ret) + goto out_err; + } + + /* add initrd-* */ + if (initrd_load_addr) { + value = cpu_to_fdt64(initrd_load_addr); + ret = fdt_setprop(buf, nodeoffset, "linux,initrd-start", + &value, sizeof(value)); + if (ret) + goto out_err; + + value = cpu_to_fdt64(initrd_load_addr + initrd_len); + ret = fdt_setprop(buf, nodeoffset, "linux,initrd-end", + &value, sizeof(value)); + if (ret) + 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, + char *initrd, unsigned long initrd_len, + char *cmdline, unsigned long cmdline_len) +{ + struct kexec_segment *kern_seg; + struct kexec_buf kbuf; + unsigned long initrd_load_addr = 0; + char *dtb = NULL; + unsigned long dtb_len = 0; + int ret = 0; + + kern_seg = &image->segment[image->arch.kern_segment]; + kbuf.image = image; + /* not allocate anything below the kernel */ + kbuf.buf_min = kern_seg->mem + kern_seg->memsz; + + /* 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(kern_seg->mem, 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); + image->arch.dtb_buf = NULL; + return ret; +}