Message ID | 20230914020044.1397356-3-songshuaishuai@tinylab.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp59036vqi; Wed, 13 Sep 2023 19:11:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHzZKklBI4nmEqRBEMxodZu/O+MlkEJb/QmXRC1zB1HPDce724iBQ2ExtyDMZzzu0ZwKHYy X-Received: by 2002:a05:6808:2116:b0:3a9:cfb5:4632 with SMTP id r22-20020a056808211600b003a9cfb54632mr5514398oiw.40.1694657476493; Wed, 13 Sep 2023 19:11:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694657476; cv=none; d=google.com; s=arc-20160816; b=ziymtpjbwIHJqykg7qSZGfZe0zRb2Xy5+VzkzJZ4hs0JbQ0wR2CItpyiobAhsKAaF7 2ehazErNNIvMsbJQ6FnLH5of5gksv2G94dnz++ABj2JXOG28eJNDHhQQ3k0rR/BIxopu A1LjL9nTppBw+kbTmA8h8ex3Jokwl/JtlsTiXtNdSy1jRSV+D8DuARdGR5wQJxiC6mqu Y99ZiRchvFiTKE14vm1LRPQtNtnt/4T17THWiggLiu2EoXAtcwEHGeVs4EcSRncbc+9o owsS1YhyLD1r2gT6LaxeFMCHbkXIBp+AkWUyLPdf6OGMOZb0R+hSKM4MqmhliSTDD6cI TwOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=krcCccJG5ET5qF67PbNaOGgLNE0B/j96EBzouKsFGn4=; fh=Kf2IcSxXu2lOC/ZqDfeaqJthEyoCZ+xmLKFwierCxZk=; b=ero2l64+n8cKimH5zba1+IaUw7XOAePifjXQL83WapOgMpijjn1UO8/RrLEa6alThj nqlxvfmQduyWwARkkgjkq9Qd8uyRjEEnCtmb4xJjffIsFmziLcc3gCXjvvnB14myJQjL fydKRAfTbHjnlB9u804NtF20vCBS967cZvTy2lwBf3xIYdgAPvzlh9ZO7pKfXArbaRxY aSImlM/lcPBEfiPmFUCeleyttV1NoSqWeZMMV04KxP7f+OLRIL6/VdcaUHgK44eikOw/ bDcopR5OUOwkEuqgj0sVTqu0okHh+RvRZClvScFPplLzT6173l8ArquttIF8HhXISMs8 hOKw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id k16-20020aa788d0000000b0069026fd5a48si573838pff.34.2023.09.13.19.11.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 19:11:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 3248582557A6; Wed, 13 Sep 2023 19:09:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233900AbjINCIc (ORCPT <rfc822;chrisfriedt@gmail.com> + 35 others); Wed, 13 Sep 2023 22:08:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233858AbjINCIS (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 13 Sep 2023 22:08:18 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.155.65.254]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DCDD2D76 for <linux-kernel@vger.kernel.org>; Wed, 13 Sep 2023 19:04:34 -0700 (PDT) X-QQ-mid: bizesmtp66t1694657009t7proppp Received: from localhost.localdomain ( [221.226.144.218]) by bizesmtp.qq.com (ESMTP) with id ; Thu, 14 Sep 2023 10:03:26 +0800 (CST) X-QQ-SSF: 01200000000000B0B000000A0000000 X-QQ-FEAT: JaBgqeDEvbUHWJAjg96BHZRrDBfTtM+Q5gAR3+eQHlcQOirxbSNE8O46Lm6d/ b8wBWaBnBiRA2XaIz7s3qFvCgDunQM2IfEK+mNkY+3SQNsAB1ppYibkiRvlaJDig9P2fEXs YK7LIobQfSJoxz8cMwgwxOh6lQwikTy2qQh4z9eEgchUMv/yg4g0oalfMAZXRd/7aoPJeL2 lQQx4biBBKOObg/v5RikVBwYHlD8Y09fwCBN71RIcK8uN+a8m3aleyIeNTnE7YkEOeX5b9y TZpRa23dxLyW+WSQI9X2bVc2Sk7A/ss7rnTPrGLWEz4vSL/+7jKUKBRjGhQ+GyvsK2/uGCC FAF63lHFbG0pFJtA1q8Nc/WAwCzCg8d4uoB+/proTAt3EmYzVDQ75aTwoun8A== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 806983590891785498 From: Song Shuai <songshuaishuai@tinylab.org> To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, lihuafei1@huawei.com, conor.dooley@microchip.com, liaochang1@huawei.com, guoren@kernel.org, ajones@ventanamicro.com, alexghiti@rivosinc.com, evan@rivosinc.com, sunilvl@ventanamicro.com, xianting.tian@linux.alibaba.com, samitolvanen@google.com, masahiroy@kernel.org, apatel@ventanamicro.com, jszhang@kernel.org, duwe@suse.de, eric.devolder@oracle.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Song Shuai <songshuaishuai@tinylab.org> Subject: [PATCH 2/2] riscv: kexec_file: Support loading Image binary file Date: Thu, 14 Sep 2023 10:00:44 +0800 Message-Id: <20230914020044.1397356-3-songshuaishuai@tinylab.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20230914020044.1397356-1-songshuaishuai@tinylab.org> References: <20230914020044.1397356-1-songshuaishuai@tinylab.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz4a-0 Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 13 Sep 2023 19:09:11 -0700 (PDT) X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776977157853277056 X-GMAIL-MSGID: 1776977157853277056 |
Series |
riscv: kexec_file: Support loading Image binary file
|
|
Commit Message
Song Shuai
Sept. 14, 2023, 2 a.m. UTC
This patch creates image_kexec_ops to load Image binary file
for kexec_file_load() syscall.
Signed-off-by: Song Shuai <songshuaishuai@tinylab.org>
---
arch/riscv/include/asm/image.h | 2 +
arch/riscv/include/asm/kexec.h | 1 +
arch/riscv/kernel/Makefile | 2 +-
arch/riscv/kernel/kexec_image.c | 97 ++++++++++++++++++++++++++
arch/riscv/kernel/machine_kexec_file.c | 1 +
5 files changed, 102 insertions(+), 1 deletion(-)
create mode 100644 arch/riscv/kernel/kexec_image.c
Comments
Song Shuai wrote: > This patch creates image_kexec_ops to load Image binary file > for kexec_file_load() syscall. > > Signed-off-by: Song Shuai <songshuaishuai@tinylab.org> > --- > arch/riscv/include/asm/image.h | 2 + > arch/riscv/include/asm/kexec.h | 1 + > arch/riscv/kernel/Makefile | 2 +- > arch/riscv/kernel/kexec_image.c | 97 ++++++++++++++++++++++++++ > arch/riscv/kernel/machine_kexec_file.c | 1 + > 5 files changed, 102 insertions(+), 1 deletion(-) > create mode 100644 arch/riscv/kernel/kexec_image.c > > diff --git a/arch/riscv/include/asm/image.h b/arch/riscv/include/asm/image.h > index e0b319af3681..8927a6ea1127 100644 > --- a/arch/riscv/include/asm/image.h > +++ b/arch/riscv/include/asm/image.h > @@ -30,6 +30,8 @@ > RISCV_HEADER_VERSION_MINOR) > > #ifndef __ASSEMBLY__ > +#define riscv_image_flag_field(flags, field)\ > + (((flags) >> field##_SHIFT) & field##_MASK) Hi Song, This macro is almost FIELD_GET from linux/bitfield.h .. > /** > * struct riscv_image_header - riscv kernel image header > * @code0: Executable code > diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h > index 518825fe4160..b9ee8346cc8c 100644 > --- a/arch/riscv/include/asm/kexec.h > +++ b/arch/riscv/include/asm/kexec.h > @@ -56,6 +56,7 @@ extern riscv_kexec_method riscv_kexec_norelocate; > > #ifdef CONFIG_KEXEC_FILE > extern const struct kexec_file_ops elf_kexec_ops; > +extern const struct kexec_file_ops image_kexec_ops; > > struct purgatory_info; > int arch_kexec_apply_relocations_add(struct purgatory_info *pi, > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > index 1c62c639e875..9ecba3231a36 100644 > --- a/arch/riscv/kernel/Makefile > +++ b/arch/riscv/kernel/Makefile > @@ -86,7 +86,7 @@ endif > obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o > obj-$(CONFIG_KGDB) += kgdb.o > obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o > -obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o machine_kexec_file.o > +obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o kexec_image.o machine_kexec_file.o > obj-$(CONFIG_CRASH_DUMP) += crash_dump.o > obj-$(CONFIG_CRASH_CORE) += crash_core.o > > diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c > new file mode 100644 > index 000000000000..b6aa7f59bd53 > --- /dev/null > +++ b/arch/riscv/kernel/kexec_image.c > @@ -0,0 +1,97 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * RISC-V Kexec image loader > + * > + */ > + > +#define pr_fmt(fmt) "kexec_file(Image): " fmt > + > +#include <linux/err.h> > +#include <linux/errno.h> > +#include <linux/kernel.h> > +#include <linux/kexec.h> > +#include <linux/pe.h> > +#include <linux/string.h> > +#include <asm/byteorder.h> > +#include <asm/image.h> > + > +static int image_probe(const char *kernel_buf, unsigned long kernel_len) > +{ > + const struct riscv_image_header *h = > + (const struct riscv_image_header *)(kernel_buf); > + > + if (!h || (kernel_len < sizeof(*h))) > + return -EINVAL; > + > + /* According to Documentation/riscv/boot-image-header.rst, > + * use "magic2" field to check when version >= 0.2. > + */ > + > + if (h->version >= RISCV_HEADER_VERSION && > + memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2))) > + return -EINVAL; > + > + return 0; > +} > + > +static void *image_load(struct kimage *image, > + char *kernel, unsigned long kernel_len, > + char *initrd, unsigned long initrd_len, > + char *cmdline, unsigned long cmdline_len) > +{ > + struct riscv_image_header *h; > + u64 flags; > + bool be_image, be_kernel; > + struct kexec_buf kbuf; > + int ret; > + > + /* Check Image header */ > + h = (struct riscv_image_header *)kernel; > + if (!h->image_size) { > + ret = -EINVAL; > + goto out; > + } > + > + /* Check endianness */ > + flags = le64_to_cpu(h->flags); > + be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); ..but here you're just testing a single bit, so since be_image is a bool it could just be be_image = flags & RISCV_IMAGE_FLAG_BE_MASK; /Emil > + be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); > + if (be_image != be_kernel) { > + ret = -EINVAL; > + goto out; > + } > + > + /* Load the kernel image */ > + kbuf.image = image; > + kbuf.buf_min = 0; > + kbuf.buf_max = ULONG_MAX; > + kbuf.top_down = false; > + > + kbuf.buffer = kernel; > + kbuf.bufsz = kernel_len; > + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; > + kbuf.memsz = le64_to_cpu(h->image_size); > + kbuf.buf_align = le64_to_cpu(h->text_offset); > + > + ret = kexec_add_buffer(&kbuf); > + if (ret) { > + pr_err("Error add kernel image ret=%d\n", ret); > + goto out; > + } > + > + image->start = kbuf.mem; > + > + pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", > + kbuf.mem, kbuf.bufsz, kbuf.memsz); > + > + ret = load_extra_segments(image, kbuf.mem, kbuf.memsz, > + initrd, initrd_len, cmdline, cmdline_len); > + > +out: > + return ret ? ERR_PTR(ret) : NULL; > +} > + > +const struct kexec_file_ops image_kexec_ops = { > + .probe = image_probe, > + .load = image_load, > +}; > diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c > index aedb8c16a283..5dc700834f1e 100644 > --- a/arch/riscv/kernel/machine_kexec_file.c > +++ b/arch/riscv/kernel/machine_kexec_file.c > @@ -17,6 +17,7 @@ > > const struct kexec_file_ops * const kexec_file_loaders[] = { > &elf_kexec_ops, > + &image_kexec_ops, > NULL > }; > > -- > 2.20.1 > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv
在 2023/9/14 16:37, Emil Renner Berthing 写道: > Song Shuai wrote: >> This patch creates image_kexec_ops to load Image binary file >> for kexec_file_load() syscall. >> >> Signed-off-by: Song Shuai <songshuaishuai@tinylab.org> >> --- >> arch/riscv/include/asm/image.h | 2 + >> arch/riscv/include/asm/kexec.h | 1 + >> arch/riscv/kernel/Makefile | 2 +- >> arch/riscv/kernel/kexec_image.c | 97 ++++++++++++++++++++++++++ >> arch/riscv/kernel/machine_kexec_file.c | 1 + >> 5 files changed, 102 insertions(+), 1 deletion(-) >> create mode 100644 arch/riscv/kernel/kexec_image.c >> >> diff --git a/arch/riscv/include/asm/image.h b/arch/riscv/include/asm/image.h >> index e0b319af3681..8927a6ea1127 100644 >> --- a/arch/riscv/include/asm/image.h >> +++ b/arch/riscv/include/asm/image.h >> @@ -30,6 +30,8 @@ >> RISCV_HEADER_VERSION_MINOR) >> >> #ifndef __ASSEMBLY__ >> +#define riscv_image_flag_field(flags, field)\ >> + (((flags) >> field##_SHIFT) & field##_MASK) > > Hi Song, > > This macro is almost FIELD_GET from linux/bitfield.h .. > >> /** >> * struct riscv_image_header - riscv kernel image header >> * @code0: Executable code >> diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h >> index 518825fe4160..b9ee8346cc8c 100644 >> --- a/arch/riscv/include/asm/kexec.h >> +++ b/arch/riscv/include/asm/kexec.h >> @@ -56,6 +56,7 @@ extern riscv_kexec_method riscv_kexec_norelocate; >> >> #ifdef CONFIG_KEXEC_FILE >> extern const struct kexec_file_ops elf_kexec_ops; >> +extern const struct kexec_file_ops image_kexec_ops; >> >> struct purgatory_info; >> int arch_kexec_apply_relocations_add(struct purgatory_info *pi, >> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile >> index 1c62c639e875..9ecba3231a36 100644 >> --- a/arch/riscv/kernel/Makefile >> +++ b/arch/riscv/kernel/Makefile >> @@ -86,7 +86,7 @@ endif >> obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o >> obj-$(CONFIG_KGDB) += kgdb.o >> obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o >> -obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o machine_kexec_file.o >> +obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o kexec_image.o machine_kexec_file.o >> obj-$(CONFIG_CRASH_DUMP) += crash_dump.o >> obj-$(CONFIG_CRASH_CORE) += crash_core.o >> >> diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c >> new file mode 100644 >> index 000000000000..b6aa7f59bd53 >> --- /dev/null >> +++ b/arch/riscv/kernel/kexec_image.c >> @@ -0,0 +1,97 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> +/* >> + * RISC-V Kexec image loader >> + * >> + */ >> + >> +#define pr_fmt(fmt) "kexec_file(Image): " fmt >> + >> +#include <linux/err.h> >> +#include <linux/errno.h> >> +#include <linux/kernel.h> >> +#include <linux/kexec.h> >> +#include <linux/pe.h> >> +#include <linux/string.h> >> +#include <asm/byteorder.h> >> +#include <asm/image.h> >> + >> +static int image_probe(const char *kernel_buf, unsigned long kernel_len) >> +{ >> + const struct riscv_image_header *h = >> + (const struct riscv_image_header *)(kernel_buf); >> + >> + if (!h || (kernel_len < sizeof(*h))) >> + return -EINVAL; >> + >> + /* According to Documentation/riscv/boot-image-header.rst, >> + * use "magic2" field to check when version >= 0.2. >> + */ >> + >> + if (h->version >= RISCV_HEADER_VERSION && >> + memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2))) >> + return -EINVAL; >> + >> + return 0; >> +} >> + >> +static void *image_load(struct kimage *image, >> + char *kernel, unsigned long kernel_len, >> + char *initrd, unsigned long initrd_len, >> + char *cmdline, unsigned long cmdline_len) >> +{ >> + struct riscv_image_header *h; >> + u64 flags; >> + bool be_image, be_kernel; >> + struct kexec_buf kbuf; >> + int ret; >> + >> + /* Check Image header */ >> + h = (struct riscv_image_header *)kernel; >> + if (!h->image_size) { >> + ret = -EINVAL; >> + goto out; >> + } >> + >> + /* Check endianness */ >> + flags = le64_to_cpu(h->flags); >> + be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); > > ..but here you're just testing a single bit, so since be_image is a bool it > could just be > be_image = flags & RISCV_IMAGE_FLAG_BE_MASK; > > /Emil Hi Emil, Sorry for the delayed response, The `flags` field currently only has bit-0 to indicate the kenrel endianness, your comment looks good in this case. While considering the future extension of the `flags` feild, the riscv_image_flag_field() is neccessiry to make callers to require the bits they want. So I prefer to keep this snippet. > >> + be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); >> + if (be_image != be_kernel) { >> + ret = -EINVAL; >> + goto out; >> + } >> + >> + /* Load the kernel image */ >> + kbuf.image = image; >> + kbuf.buf_min = 0; >> + kbuf.buf_max = ULONG_MAX; >> + kbuf.top_down = false; >> + >> + kbuf.buffer = kernel; >> + kbuf.bufsz = kernel_len; >> + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; >> + kbuf.memsz = le64_to_cpu(h->image_size); >> + kbuf.buf_align = le64_to_cpu(h->text_offset); >> + >> + ret = kexec_add_buffer(&kbuf); >> + if (ret) { >> + pr_err("Error add kernel image ret=%d\n", ret); >> + goto out; >> + } >> + >> + image->start = kbuf.mem; >> + >> + pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", >> + kbuf.mem, kbuf.bufsz, kbuf.memsz); >> + >> + ret = load_extra_segments(image, kbuf.mem, kbuf.memsz, >> + initrd, initrd_len, cmdline, cmdline_len); >> + >> +out: >> + return ret ? ERR_PTR(ret) : NULL; >> +} >> + >> +const struct kexec_file_ops image_kexec_ops = { >> + .probe = image_probe, >> + .load = image_load, >> +}; >> diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c >> index aedb8c16a283..5dc700834f1e 100644 >> --- a/arch/riscv/kernel/machine_kexec_file.c >> +++ b/arch/riscv/kernel/machine_kexec_file.c >> @@ -17,6 +17,7 @@ >> >> const struct kexec_file_ops * const kexec_file_loaders[] = { >> &elf_kexec_ops, >> + &image_kexec_ops, >> NULL >> }; >> >> -- >> 2.20.1 >> >> >> _______________________________________________ >> linux-riscv mailing list >> linux-riscv@lists.infradead.org >> http://lists.infradead.org/mailman/listinfo/linux-riscv >
Song Shuai wrote: > > > 在 2023/9/14 16:37, Emil Renner Berthing 写道: > > Song Shuai wrote: > >> This patch creates image_kexec_ops to load Image binary file > >> for kexec_file_load() syscall. > >> > >> Signed-off-by: Song Shuai <songshuaishuai@tinylab.org> > >> --- > >> arch/riscv/include/asm/image.h | 2 + > >> arch/riscv/include/asm/kexec.h | 1 + > >> arch/riscv/kernel/Makefile | 2 +- > >> arch/riscv/kernel/kexec_image.c | 97 ++++++++++++++++++++++++++ > >> arch/riscv/kernel/machine_kexec_file.c | 1 + > >> 5 files changed, 102 insertions(+), 1 deletion(-) > >> create mode 100644 arch/riscv/kernel/kexec_image.c > >> > >> diff --git a/arch/riscv/include/asm/image.h b/arch/riscv/include/asm/image.h > >> index e0b319af3681..8927a6ea1127 100644 > >> --- a/arch/riscv/include/asm/image.h > >> +++ b/arch/riscv/include/asm/image.h > >> @@ -30,6 +30,8 @@ > >> RISCV_HEADER_VERSION_MINOR) > >> > >> #ifndef __ASSEMBLY__ > >> +#define riscv_image_flag_field(flags, field)\ > >> + (((flags) >> field##_SHIFT) & field##_MASK) > > > > Hi Song, > > > > This macro is almost FIELD_GET from linux/bitfield.h .. > > > >> /** > >> * struct riscv_image_header - riscv kernel image header > >> * @code0: Executable code > >> diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h > >> index 518825fe4160..b9ee8346cc8c 100644 > >> --- a/arch/riscv/include/asm/kexec.h > >> +++ b/arch/riscv/include/asm/kexec.h > >> @@ -56,6 +56,7 @@ extern riscv_kexec_method riscv_kexec_norelocate; > >> > >> #ifdef CONFIG_KEXEC_FILE > >> extern const struct kexec_file_ops elf_kexec_ops; > >> +extern const struct kexec_file_ops image_kexec_ops; > >> > >> struct purgatory_info; > >> int arch_kexec_apply_relocations_add(struct purgatory_info *pi, > >> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > >> index 1c62c639e875..9ecba3231a36 100644 > >> --- a/arch/riscv/kernel/Makefile > >> +++ b/arch/riscv/kernel/Makefile > >> @@ -86,7 +86,7 @@ endif > >> obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o > >> obj-$(CONFIG_KGDB) += kgdb.o > >> obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o > >> -obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o machine_kexec_file.o > >> +obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o kexec_image.o machine_kexec_file.o > >> obj-$(CONFIG_CRASH_DUMP) += crash_dump.o > >> obj-$(CONFIG_CRASH_CORE) += crash_core.o > >> > >> diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c > >> new file mode 100644 > >> index 000000000000..b6aa7f59bd53 > >> --- /dev/null > >> +++ b/arch/riscv/kernel/kexec_image.c > >> @@ -0,0 +1,97 @@ > >> +// SPDX-License-Identifier: GPL-2.0 > >> +/* > >> + * RISC-V Kexec image loader > >> + * > >> + */ > >> + > >> +#define pr_fmt(fmt) "kexec_file(Image): " fmt > >> + > >> +#include <linux/err.h> > >> +#include <linux/errno.h> > >> +#include <linux/kernel.h> > >> +#include <linux/kexec.h> > >> +#include <linux/pe.h> > >> +#include <linux/string.h> > >> +#include <asm/byteorder.h> > >> +#include <asm/image.h> > >> + > >> +static int image_probe(const char *kernel_buf, unsigned long kernel_len) > >> +{ > >> + const struct riscv_image_header *h = > >> + (const struct riscv_image_header *)(kernel_buf); > >> + > >> + if (!h || (kernel_len < sizeof(*h))) > >> + return -EINVAL; > >> + > >> + /* According to Documentation/riscv/boot-image-header.rst, > >> + * use "magic2" field to check when version >= 0.2. > >> + */ > >> + > >> + if (h->version >= RISCV_HEADER_VERSION && > >> + memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2))) > >> + return -EINVAL; > >> + > >> + return 0; > >> +} > >> + > >> +static void *image_load(struct kimage *image, > >> + char *kernel, unsigned long kernel_len, > >> + char *initrd, unsigned long initrd_len, > >> + char *cmdline, unsigned long cmdline_len) > >> +{ > >> + struct riscv_image_header *h; > >> + u64 flags; > >> + bool be_image, be_kernel; > >> + struct kexec_buf kbuf; > >> + int ret; > >> + > >> + /* Check Image header */ > >> + h = (struct riscv_image_header *)kernel; > >> + if (!h->image_size) { > >> + ret = -EINVAL; > >> + goto out; > >> + } > >> + > >> + /* Check endianness */ > >> + flags = le64_to_cpu(h->flags); > >> + be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); > > > > ..but here you're just testing a single bit, so since be_image is a bool it > > could just be > > be_image = flags & RISCV_IMAGE_FLAG_BE_MASK; > > > > /Emil > Hi Emil, > > Sorry for the delayed response, > > The `flags` field currently only has bit-0 to indicate the kenrel > endianness, your comment looks good in this case. > > While considering the future extension of the `flags` feild, the > riscv_image_flag_field() is neccessiry to make callers to require the > bits they want. Right, but please don't invent your own FIELD_GET then. > > So I prefer to keep this snippet. > > > >> + be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); > >> + if (be_image != be_kernel) { > >> + ret = -EINVAL; > >> + goto out; > >> + } > >> + > >> + /* Load the kernel image */ > >> + kbuf.image = image; > >> + kbuf.buf_min = 0; > >> + kbuf.buf_max = ULONG_MAX; > >> + kbuf.top_down = false; > >> + > >> + kbuf.buffer = kernel; > >> + kbuf.bufsz = kernel_len; > >> + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; > >> + kbuf.memsz = le64_to_cpu(h->image_size); > >> + kbuf.buf_align = le64_to_cpu(h->text_offset); > >> + > >> + ret = kexec_add_buffer(&kbuf); > >> + if (ret) { > >> + pr_err("Error add kernel image ret=%d\n", ret); > >> + goto out; > >> + } > >> + > >> + image->start = kbuf.mem; > >> + > >> + pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", > >> + kbuf.mem, kbuf.bufsz, kbuf.memsz); > >> + > >> + ret = load_extra_segments(image, kbuf.mem, kbuf.memsz, > >> + initrd, initrd_len, cmdline, cmdline_len); > >> + > >> +out: > >> + return ret ? ERR_PTR(ret) : NULL; > >> +} > >> + > >> +const struct kexec_file_ops image_kexec_ops = { > >> + .probe = image_probe, > >> + .load = image_load, > >> +}; > >> diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c > >> index aedb8c16a283..5dc700834f1e 100644 > >> --- a/arch/riscv/kernel/machine_kexec_file.c > >> +++ b/arch/riscv/kernel/machine_kexec_file.c > >> @@ -17,6 +17,7 @@ > >> > >> const struct kexec_file_ops * const kexec_file_loaders[] = { > >> &elf_kexec_ops, > >> + &image_kexec_ops, > >> NULL > >> }; > >> > >> -- > >> 2.20.1 > >> > >> > >> _______________________________________________ > >> linux-riscv mailing list > >> linux-riscv@lists.infradead.org > >> http://lists.infradead.org/mailman/listinfo/linux-riscv > > > > -- > Thanks > Song Shuai > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv
在 2023/10/11 18:48, Emil Renner Berthing 写道: > Song Shuai wrote: >> >> >> 在 2023/9/14 16:37, Emil Renner Berthing 写道: >>> Song Shuai wrote: >>>> This patch creates image_kexec_ops to load Image binary file >>>> for kexec_file_load() syscall. >>>> >>>> Signed-off-by: Song Shuai <songshuaishuai@tinylab.org> >>>> --- >>>> arch/riscv/include/asm/image.h | 2 + >>>> arch/riscv/include/asm/kexec.h | 1 + >>>> arch/riscv/kernel/Makefile | 2 +- >>>> arch/riscv/kernel/kexec_image.c | 97 ++++++++++++++++++++++++++ >>>> arch/riscv/kernel/machine_kexec_file.c | 1 + >>>> 5 files changed, 102 insertions(+), 1 deletion(-) >>>> create mode 100644 arch/riscv/kernel/kexec_image.c >>>> >>>> diff --git a/arch/riscv/include/asm/image.h b/arch/riscv/include/asm/image.h >>>> index e0b319af3681..8927a6ea1127 100644 >>>> --- a/arch/riscv/include/asm/image.h >>>> +++ b/arch/riscv/include/asm/image.h >>>> @@ -30,6 +30,8 @@ >>>> RISCV_HEADER_VERSION_MINOR) >>>> >>>> #ifndef __ASSEMBLY__ >>>> +#define riscv_image_flag_field(flags, field)\ >>>> + (((flags) >> field##_SHIFT) & field##_MASK) >>> >>> Hi Song, >>> >>> This macro is almost FIELD_GET from linux/bitfield.h .. >>> >>>> /** >>>> * struct riscv_image_header - riscv kernel image header >>>> * @code0: Executable code >>>> diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h >>>> index 518825fe4160..b9ee8346cc8c 100644 >>>> --- a/arch/riscv/include/asm/kexec.h >>>> +++ b/arch/riscv/include/asm/kexec.h >>>> @@ -56,6 +56,7 @@ extern riscv_kexec_method riscv_kexec_norelocate; >>>> >>>> #ifdef CONFIG_KEXEC_FILE >>>> extern const struct kexec_file_ops elf_kexec_ops; >>>> +extern const struct kexec_file_ops image_kexec_ops; >>>> >>>> struct purgatory_info; >>>> int arch_kexec_apply_relocations_add(struct purgatory_info *pi, >>>> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile >>>> index 1c62c639e875..9ecba3231a36 100644 >>>> --- a/arch/riscv/kernel/Makefile >>>> +++ b/arch/riscv/kernel/Makefile >>>> @@ -86,7 +86,7 @@ endif >>>> obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o >>>> obj-$(CONFIG_KGDB) += kgdb.o >>>> obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o >>>> -obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o machine_kexec_file.o >>>> +obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o kexec_image.o machine_kexec_file.o >>>> obj-$(CONFIG_CRASH_DUMP) += crash_dump.o >>>> obj-$(CONFIG_CRASH_CORE) += crash_core.o >>>> >>>> diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c >>>> new file mode 100644 >>>> index 000000000000..b6aa7f59bd53 >>>> --- /dev/null >>>> +++ b/arch/riscv/kernel/kexec_image.c >>>> @@ -0,0 +1,97 @@ >>>> +// SPDX-License-Identifier: GPL-2.0 >>>> +/* >>>> + * RISC-V Kexec image loader >>>> + * >>>> + */ >>>> + >>>> +#define pr_fmt(fmt) "kexec_file(Image): " fmt >>>> + >>>> +#include <linux/err.h> >>>> +#include <linux/errno.h> >>>> +#include <linux/kernel.h> >>>> +#include <linux/kexec.h> >>>> +#include <linux/pe.h> >>>> +#include <linux/string.h> >>>> +#include <asm/byteorder.h> >>>> +#include <asm/image.h> >>>> + >>>> +static int image_probe(const char *kernel_buf, unsigned long kernel_len) >>>> +{ >>>> + const struct riscv_image_header *h = >>>> + (const struct riscv_image_header *)(kernel_buf); >>>> + >>>> + if (!h || (kernel_len < sizeof(*h))) >>>> + return -EINVAL; >>>> + >>>> + /* According to Documentation/riscv/boot-image-header.rst, >>>> + * use "magic2" field to check when version >= 0.2. >>>> + */ >>>> + >>>> + if (h->version >= RISCV_HEADER_VERSION && >>>> + memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2))) >>>> + return -EINVAL; >>>> + >>>> + return 0; >>>> +} >>>> + >>>> +static void *image_load(struct kimage *image, >>>> + char *kernel, unsigned long kernel_len, >>>> + char *initrd, unsigned long initrd_len, >>>> + char *cmdline, unsigned long cmdline_len) >>>> +{ >>>> + struct riscv_image_header *h; >>>> + u64 flags; >>>> + bool be_image, be_kernel; >>>> + struct kexec_buf kbuf; >>>> + int ret; >>>> + >>>> + /* Check Image header */ >>>> + h = (struct riscv_image_header *)kernel; >>>> + if (!h->image_size) { >>>> + ret = -EINVAL; >>>> + goto out; >>>> + } >>>> + >>>> + /* Check endianness */ >>>> + flags = le64_to_cpu(h->flags); >>>> + be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); >>> >>> ..but here you're just testing a single bit, so since be_image is a bool it >>> could just be >>> be_image = flags & RISCV_IMAGE_FLAG_BE_MASK; >>> >>> /Emil >> Hi Emil, >> >> Sorry for the delayed response, >> >> The `flags` field currently only has bit-0 to indicate the kenrel >> endianness, your comment looks good in this case. >> >> While considering the future extension of the `flags` feild, the >> riscv_image_flag_field() is neccessiry to make callers to require the >> bits they want. > > Right, but please don't invent your own FIELD_GET then. Hi Emil, I tried to use FIELD_PREP/FIELD_GET to set/get the RISCV_IMAGE_FLAG_BE, but using FIELD_PREP to define the __HEAD_FLAGS (used in head.S) emitted AS build error and the final .s file looked like: 883 .dword _end - _start 884 .dword (({ ({ do { } while (0); do { } while (0); do { } while (0); do { } while (0); do { } while (0); }); ((typeof((((1)) << (0))))(0) << (__builtin_ffsll((((1)) << (0))) - 1)) & ((((1)) << (0))); })) IIUC the FIELD_PREP can't work well in this asm context, and using FILED_GET without FIELD_PREP paired looks not good to me. The original riscv/*/image.h and the riscv_image_flag_filed() of this patch just do what arm64 does, so I still like to keep them. I also found that Patch1 has a build issue due to my previous uncareful cherry-picking, V2 will be sent to fix it if you're ok this patch without bitfield armed. I would like to listen to your advice. > >> >> So I prefer to keep this snippet. >>> >>>> + be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); >>>> + if (be_image != be_kernel) { >>>> + ret = -EINVAL; >>>> + goto out; >>>> + } >>>> + >>>> + /* Load the kernel image */ >>>> + kbuf.image = image; >>>> + kbuf.buf_min = 0; >>>> + kbuf.buf_max = ULONG_MAX; >>>> + kbuf.top_down = false; >>>> + >>>> + kbuf.buffer = kernel; >>>> + kbuf.bufsz = kernel_len; >>>> + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; >>>> + kbuf.memsz = le64_to_cpu(h->image_size); >>>> + kbuf.buf_align = le64_to_cpu(h->text_offset); >>>> + >>>> + ret = kexec_add_buffer(&kbuf); >>>> + if (ret) { >>>> + pr_err("Error add kernel image ret=%d\n", ret); >>>> + goto out; >>>> + } >>>> + >>>> + image->start = kbuf.mem; >>>> + >>>> + pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", >>>> + kbuf.mem, kbuf.bufsz, kbuf.memsz); >>>> + >>>> + ret = load_extra_segments(image, kbuf.mem, kbuf.memsz, >>>> + initrd, initrd_len, cmdline, cmdline_len); >>>> + >>>> +out: >>>> + return ret ? ERR_PTR(ret) : NULL; >>>> +} >>>> + >>>> +const struct kexec_file_ops image_kexec_ops = { >>>> + .probe = image_probe, >>>> + .load = image_load, >>>> +}; >>>> diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c >>>> index aedb8c16a283..5dc700834f1e 100644 >>>> --- a/arch/riscv/kernel/machine_kexec_file.c >>>> +++ b/arch/riscv/kernel/machine_kexec_file.c >>>> @@ -17,6 +17,7 @@ >>>> >>>> const struct kexec_file_ops * const kexec_file_loaders[] = { >>>> &elf_kexec_ops, >>>> + &image_kexec_ops, >>>> NULL >>>> }; >>>> >>>> -- >>>> 2.20.1 >>>> >>>> >>>> _______________________________________________ >>>> linux-riscv mailing list >>>> linux-riscv@lists.infradead.org >>>> http://lists.infradead.org/mailman/listinfo/linux-riscv >>> >> >> -- >> Thanks >> Song Shuai >> >> >> _______________________________________________ >> linux-riscv mailing list >> linux-riscv@lists.infradead.org >> http://lists.infradead.org/mailman/listinfo/linux-riscv >
Song Shuai wrote: > > > 在 2023/10/11 18:48, Emil Renner Berthing 写道: > > Song Shuai wrote: > >> > >> > >> 在 2023/9/14 16:37, Emil Renner Berthing 写道: > >>> Song Shuai wrote: > >>>> This patch creates image_kexec_ops to load Image binary file > >>>> for kexec_file_load() syscall. > >>>> > >>>> Signed-off-by: Song Shuai <songshuaishuai@tinylab.org> > >>>> --- > >>>> arch/riscv/include/asm/image.h | 2 + > >>>> arch/riscv/include/asm/kexec.h | 1 + > >>>> arch/riscv/kernel/Makefile | 2 +- > >>>> arch/riscv/kernel/kexec_image.c | 97 ++++++++++++++++++++++++++ > >>>> arch/riscv/kernel/machine_kexec_file.c | 1 + > >>>> 5 files changed, 102 insertions(+), 1 deletion(-) > >>>> create mode 100644 arch/riscv/kernel/kexec_image.c > >>>> > >>>> diff --git a/arch/riscv/include/asm/image.h b/arch/riscv/include/asm/image.h > >>>> index e0b319af3681..8927a6ea1127 100644 > >>>> --- a/arch/riscv/include/asm/image.h > >>>> +++ b/arch/riscv/include/asm/image.h > >>>> @@ -30,6 +30,8 @@ > >>>> RISCV_HEADER_VERSION_MINOR) > >>>> > >>>> #ifndef __ASSEMBLY__ > >>>> +#define riscv_image_flag_field(flags, field)\ > >>>> + (((flags) >> field##_SHIFT) & field##_MASK) > >>> > >>> Hi Song, > >>> > >>> This macro is almost FIELD_GET from linux/bitfield.h .. > >>> > >>>> /** > >>>> * struct riscv_image_header - riscv kernel image header > >>>> * @code0: Executable code > >>>> diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h > >>>> index 518825fe4160..b9ee8346cc8c 100644 > >>>> --- a/arch/riscv/include/asm/kexec.h > >>>> +++ b/arch/riscv/include/asm/kexec.h > >>>> @@ -56,6 +56,7 @@ extern riscv_kexec_method riscv_kexec_norelocate; > >>>> > >>>> #ifdef CONFIG_KEXEC_FILE > >>>> extern const struct kexec_file_ops elf_kexec_ops; > >>>> +extern const struct kexec_file_ops image_kexec_ops; > >>>> > >>>> struct purgatory_info; > >>>> int arch_kexec_apply_relocations_add(struct purgatory_info *pi, > >>>> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > >>>> index 1c62c639e875..9ecba3231a36 100644 > >>>> --- a/arch/riscv/kernel/Makefile > >>>> +++ b/arch/riscv/kernel/Makefile > >>>> @@ -86,7 +86,7 @@ endif > >>>> obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o > >>>> obj-$(CONFIG_KGDB) += kgdb.o > >>>> obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o > >>>> -obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o machine_kexec_file.o > >>>> +obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o kexec_image.o machine_kexec_file.o > >>>> obj-$(CONFIG_CRASH_DUMP) += crash_dump.o > >>>> obj-$(CONFIG_CRASH_CORE) += crash_core.o > >>>> > >>>> diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c > >>>> new file mode 100644 > >>>> index 000000000000..b6aa7f59bd53 > >>>> --- /dev/null > >>>> +++ b/arch/riscv/kernel/kexec_image.c > >>>> @@ -0,0 +1,97 @@ > >>>> +// SPDX-License-Identifier: GPL-2.0 > >>>> +/* > >>>> + * RISC-V Kexec image loader > >>>> + * > >>>> + */ > >>>> + > >>>> +#define pr_fmt(fmt) "kexec_file(Image): " fmt > >>>> + > >>>> +#include <linux/err.h> > >>>> +#include <linux/errno.h> > >>>> +#include <linux/kernel.h> > >>>> +#include <linux/kexec.h> > >>>> +#include <linux/pe.h> > >>>> +#include <linux/string.h> > >>>> +#include <asm/byteorder.h> > >>>> +#include <asm/image.h> > >>>> + > >>>> +static int image_probe(const char *kernel_buf, unsigned long kernel_len) > >>>> +{ > >>>> + const struct riscv_image_header *h = > >>>> + (const struct riscv_image_header *)(kernel_buf); > >>>> + > >>>> + if (!h || (kernel_len < sizeof(*h))) > >>>> + return -EINVAL; > >>>> + > >>>> + /* According to Documentation/riscv/boot-image-header.rst, > >>>> + * use "magic2" field to check when version >= 0.2. > >>>> + */ > >>>> + > >>>> + if (h->version >= RISCV_HEADER_VERSION && > >>>> + memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2))) > >>>> + return -EINVAL; > >>>> + > >>>> + return 0; > >>>> +} > >>>> + > >>>> +static void *image_load(struct kimage *image, > >>>> + char *kernel, unsigned long kernel_len, > >>>> + char *initrd, unsigned long initrd_len, > >>>> + char *cmdline, unsigned long cmdline_len) > >>>> +{ > >>>> + struct riscv_image_header *h; > >>>> + u64 flags; > >>>> + bool be_image, be_kernel; > >>>> + struct kexec_buf kbuf; > >>>> + int ret; > >>>> + > >>>> + /* Check Image header */ > >>>> + h = (struct riscv_image_header *)kernel; > >>>> + if (!h->image_size) { > >>>> + ret = -EINVAL; > >>>> + goto out; > >>>> + } > >>>> + > >>>> + /* Check endianness */ > >>>> + flags = le64_to_cpu(h->flags); > >>>> + be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); > >>> > >>> ..but here you're just testing a single bit, so since be_image is a bool it > >>> could just be > >>> be_image = flags & RISCV_IMAGE_FLAG_BE_MASK; > >>> > >>> /Emil > >> Hi Emil, > >> > >> Sorry for the delayed response, > >> > >> The `flags` field currently only has bit-0 to indicate the kenrel > >> endianness, your comment looks good in this case. > >> > >> While considering the future extension of the `flags` feild, the > >> riscv_image_flag_field() is neccessiry to make callers to require the > >> bits they want. > > > > Right, but please don't invent your own FIELD_GET then. > > Hi Emil, > > I tried to use FIELD_PREP/FIELD_GET to set/get the RISCV_IMAGE_FLAG_BE, > but using FIELD_PREP to define the __HEAD_FLAGS (used in head.S) emitted > AS build error and the final .s file looked like: > > 883 .dword _end - _start > 884 .dword (({ ({ do { } while (0); do { } while (0); do { } while > (0); do { } while (0); do { } while (0); }); ((typeof((((1)) << > (0))))(0) << (__builtin_ffsll((((1)) << (0))) - 1)) & ((((1)) << (0))); })) > > IIUC the FIELD_PREP can't work well in this asm context, and using > FILED_GET without FIELD_PREP paired looks not good to me. > > The original riscv/*/image.h and the riscv_image_flag_filed() of this > patch just do what arm64 does, so I still like to keep them. Hi Song, Your riscv_image_flag_field macro is already guarded by a #ifndef __ASSEMBLY__ and your patches above doesn't touch any __HEAD_FLAGS macro, so I don't know what you're trying to do with FIELD_PREP. What I'm saying is that instead of defining the riscv_image_flag_field macro you can just do -be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); +be_image = FIELD_GET(RISCV_IMAGE_FLAG_BE, flags); /Emil > I also found that Patch1 has a build issue due to my previous uncareful > cherry-picking, > V2 will be sent to fix it if you're ok this patch without bitfield armed. > > I would like to listen to your advice. > > > > >> > >> So I prefer to keep this snippet. > >>> > >>>> + be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); > >>>> + if (be_image != be_kernel) { > >>>> + ret = -EINVAL; > >>>> + goto out; > >>>> + } > >>>> + > >>>> + /* Load the kernel image */ > >>>> + kbuf.image = image; > >>>> + kbuf.buf_min = 0; > >>>> + kbuf.buf_max = ULONG_MAX; > >>>> + kbuf.top_down = false; > >>>> + > >>>> + kbuf.buffer = kernel; > >>>> + kbuf.bufsz = kernel_len; > >>>> + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; > >>>> + kbuf.memsz = le64_to_cpu(h->image_size); > >>>> + kbuf.buf_align = le64_to_cpu(h->text_offset); > >>>> + > >>>> + ret = kexec_add_buffer(&kbuf); > >>>> + if (ret) { > >>>> + pr_err("Error add kernel image ret=%d\n", ret); > >>>> + goto out; > >>>> + } > >>>> + > >>>> + image->start = kbuf.mem; > >>>> + > >>>> + pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", > >>>> + kbuf.mem, kbuf.bufsz, kbuf.memsz); > >>>> + > >>>> + ret = load_extra_segments(image, kbuf.mem, kbuf.memsz, > >>>> + initrd, initrd_len, cmdline, cmdline_len); > >>>> + > >>>> +out: > >>>> + return ret ? ERR_PTR(ret) : NULL; > >>>> +} > >>>> + > >>>> +const struct kexec_file_ops image_kexec_ops = { > >>>> + .probe = image_probe, > >>>> + .load = image_load, > >>>> +}; > >>>> diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c > >>>> index aedb8c16a283..5dc700834f1e 100644 > >>>> --- a/arch/riscv/kernel/machine_kexec_file.c > >>>> +++ b/arch/riscv/kernel/machine_kexec_file.c > >>>> @@ -17,6 +17,7 @@ > >>>> > >>>> const struct kexec_file_ops * const kexec_file_loaders[] = { > >>>> &elf_kexec_ops, > >>>> + &image_kexec_ops, > >>>> NULL > >>>> }; > >>>> > >>>> -- > >>>> 2.20.1
Hi Shuai, On Thu, Sep 14, 2023 at 10:09 AM Song Shuai <songshuaishuai@tinylaborg> wrote: > > This patch creates image_kexec_ops to load Image binary file > for kexec_file_load() syscall. > > Signed-off-by: Song Shuai <songshuaishuai@tinylab.org> > --- > arch/riscv/include/asm/image.h | 2 + > arch/riscv/include/asm/kexec.h | 1 + > arch/riscv/kernel/Makefile | 2 +- > arch/riscv/kernel/kexec_image.c | 97 ++++++++++++++++++++++++++ > arch/riscv/kernel/machine_kexec_file.c | 1 + > 5 files changed, 102 insertions(+), 1 deletion(-) > create mode 100644 arch/riscv/kernel/kexec_image.c > > diff --git a/arch/riscv/include/asm/image.h b/arch/riscv/include/asm/image.h > index e0b319af3681..8927a6ea1127 100644 > --- a/arch/riscv/include/asm/image.h > +++ b/arch/riscv/include/asm/image.h > @@ -30,6 +30,8 @@ > RISCV_HEADER_VERSION_MINOR) > > #ifndef __ASSEMBLY__ > +#define riscv_image_flag_field(flags, field)\ > + (((flags) >> field##_SHIFT) & field##_MASK) > /** > * struct riscv_image_header - riscv kernel image header > * @code0: Executable code > diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h > index 518825fe4160..b9ee8346cc8c 100644 > --- a/arch/riscv/include/asm/kexec.h > +++ b/arch/riscv/include/asm/kexec.h > @@ -56,6 +56,7 @@ extern riscv_kexec_method riscv_kexec_norelocate; > > #ifdef CONFIG_KEXEC_FILE > extern const struct kexec_file_ops elf_kexec_ops; > +extern const struct kexec_file_ops image_kexec_ops; > > struct purgatory_info; > int arch_kexec_apply_relocations_add(struct purgatory_info *pi, > diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > index 1c62c639e875..9ecba3231a36 100644 > --- a/arch/riscv/kernel/Makefile > +++ b/arch/riscv/kernel/Makefile > @@ -86,7 +86,7 @@ endif > obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o > obj-$(CONFIG_KGDB) += kgdb.o > obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o > -obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o machine_kexec_file.o > +obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o kexec_image.o machine_kexec_file.o > obj-$(CONFIG_CRASH_DUMP) += crash_dump.o > obj-$(CONFIG_CRASH_CORE) += crash_core.o > > diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c > new file mode 100644 > index 000000000000..b6aa7f59bd53 > --- /dev/null > +++ b/arch/riscv/kernel/kexec_image.c > @@ -0,0 +1,97 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * RISC-V Kexec image loader > + * > + */ > + > +#define pr_fmt(fmt) "kexec_file(Image): " fmt > + > +#include <linux/err.h> > +#include <linux/errno.h> > +#include <linux/kernel.h> > +#include <linux/kexec.h> > +#include <linux/pe.h> > +#include <linux/string.h> > +#include <asm/byteorder.h> > +#include <asm/image.h> > + > +static int image_probe(const char *kernel_buf, unsigned long kernel_len) > +{ > + const struct riscv_image_header *h = > + (const struct riscv_image_header *)(kernel_buf); > + > + if (!h || (kernel_len < sizeof(*h))) > + return -EINVAL; > + > + /* According to Documentation/riscv/boot-image-header.rst, > + * use "magic2" field to check when version >= 0.2. > + */ > + > + if (h->version >= RISCV_HEADER_VERSION && > + memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2))) > + return -EINVAL; > + > + return 0; > +} > + > +static void *image_load(struct kimage *image, > + char *kernel, unsigned long kernel_len, > + char *initrd, unsigned long initrd_len, > + char *cmdline, unsigned long cmdline_len) > +{ > + struct riscv_image_header *h; > + u64 flags; > + bool be_image, be_kernel; > + struct kexec_buf kbuf; > + int ret; > + > + /* Check Image header */ > + h = (struct riscv_image_header *)kernel; > + if (!h->image_size) { > + ret = -EINVAL; > + goto out; > + } > + > + /* Check endianness */ > + flags = le64_to_cpu(h->flags); > + be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); > + be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); > + if (be_image != be_kernel) { > + ret = -EINVAL; > + goto out; > + } > + > + /* Load the kernel image */ > + kbuf.image = image; > + kbuf.buf_min = 0; > + kbuf.buf_max = ULONG_MAX; > + kbuf.top_down = false; > + > + kbuf.buffer = kernel; > + kbuf.bufsz = kernel_len; > + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; > + kbuf.memsz = le64_to_cpu(h->image_size); > + kbuf.buf_align = le64_to_cpu(h->text_offset); > + > + ret = kexec_add_buffer(&kbuf); > + if (ret) { > + pr_err("Error add kernel image ret=%d\n", ret); > + goto out; > + } > + > + image->start = kbuf.mem; > + > + pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", > + kbuf.mem, kbuf.bufsz, kbuf.memsz); pr_info() or pr_debug()? > + > + ret = load_extra_segments(image, kbuf.mem, kbuf.memsz, > + initrd, initrd_len, cmdline, cmdline_len); > + > +out: > + return ret ? ERR_PTR(ret) : NULL; > +} > + > +const struct kexec_file_ops image_kexec_ops = { > + .probe = image_probe, > + .load = image_load, > +}; > diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c > index aedb8c16a283..5dc700834f1e 100644 > --- a/arch/riscv/kernel/machine_kexec_file.c > +++ b/arch/riscv/kernel/machine_kexec_file.c > @@ -17,6 +17,7 @@ > > const struct kexec_file_ops * const kexec_file_loaders[] = { > &elf_kexec_ops, > + &image_kexec_ops, > NULL > }; > > -- > 2.20.1 > I tested these two patches. It works when CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY is not enabled. When CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY is enabled, the error is as follows: [45.712019] Unknown rela relocation: 34 [45.712258] Error loading purgatory ret=-8 How to fix it? Thanks, Yunhui
Hi, yunhui: 在 2024/2/16 16:54, yunhui cui 写道: > Hi Shuai, > > On Thu, Sep 14, 2023 at 10:09 AM Song Shuai <songshuaishuai@tinylab.org> wrote: >> >> This patch creates image_kexec_ops to load Image binary file >> for kexec_file_load() syscall. >> >> Signed-off-by: Song Shuai <songshuaishuai@tinylab.org> >> --- >> arch/riscv/include/asm/image.h | 2 + >> arch/riscv/include/asm/kexec.h | 1 + >> arch/riscv/kernel/Makefile | 2 +- >> arch/riscv/kernel/kexec_image.c | 97 ++++++++++++++++++++++++++ >> arch/riscv/kernel/machine_kexec_file.c | 1 + >> 5 files changed, 102 insertions(+), 1 deletion(-) >> create mode 100644 arch/riscv/kernel/kexec_image.c >> >> diff --git a/arch/riscv/include/asm/image.h b/arch/riscv/include/asm/image.h >> index e0b319af3681..8927a6ea1127 100644 >> --- a/arch/riscv/include/asm/image.h >> +++ b/arch/riscv/include/asm/image.h >> @@ -30,6 +30,8 @@ >> RISCV_HEADER_VERSION_MINOR) >> >> #ifndef __ASSEMBLY__ >> +#define riscv_image_flag_field(flags, field)\ >> + (((flags) >> field##_SHIFT) & field##_MASK) >> /** >> * struct riscv_image_header - riscv kernel image header >> * @code0: Executable code >> diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h >> index 518825fe4160..b9ee8346cc8c 100644 >> --- a/arch/riscv/include/asm/kexec.h >> +++ b/arch/riscv/include/asm/kexec.h >> @@ -56,6 +56,7 @@ extern riscv_kexec_method riscv_kexec_norelocate; >> >> #ifdef CONFIG_KEXEC_FILE >> extern const struct kexec_file_ops elf_kexec_ops; >> +extern const struct kexec_file_ops image_kexec_ops; >> >> struct purgatory_info; >> int arch_kexec_apply_relocations_add(struct purgatory_info *pi, >> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile >> index 1c62c639e875..9ecba3231a36 100644 >> --- a/arch/riscv/kernel/Makefile >> +++ b/arch/riscv/kernel/Makefile >> @@ -86,7 +86,7 @@ endif >> obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o >> obj-$(CONFIG_KGDB) += kgdb.o >> obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o >> -obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o machine_kexec_file.o >> +obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o kexec_image.o machine_kexec_file.o >> obj-$(CONFIG_CRASH_DUMP) += crash_dump.o >> obj-$(CONFIG_CRASH_CORE) += crash_core.o >> >> diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c >> new file mode 100644 >> index 000000000000..b6aa7f59bd53 >> --- /dev/null >> +++ b/arch/riscv/kernel/kexec_image.c >> @@ -0,0 +1,97 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> +/* >> + * RISC-V Kexec image loader >> + * >> + */ >> + >> +#define pr_fmt(fmt) "kexec_file(Image): " fmt >> + >> +#include <linux/err.h> >> +#include <linux/errno.h> >> +#include <linux/kernel.h> >> +#include <linux/kexec.h> >> +#include <linux/pe.h> >> +#include <linux/string.h> >> +#include <asm/byteorder.h> >> +#include <asm/image.h> >> + >> +static int image_probe(const char *kernel_buf, unsigned long kernel_len) >> +{ >> + const struct riscv_image_header *h = >> + (const struct riscv_image_header *)(kernel_buf); >> + >> + if (!h || (kernel_len < sizeof(*h))) >> + return -EINVAL; >> + >> + /* According to Documentation/riscv/boot-image-header.rst, >> + * use "magic2" field to check when version >= 0.2. >> + */ >> + >> + if (h->version >= RISCV_HEADER_VERSION && >> + memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2))) >> + return -EINVAL; >> + >> + return 0; >> +} >> + >> +static void *image_load(struct kimage *image, >> + char *kernel, unsigned long kernel_len, >> + char *initrd, unsigned long initrd_len, >> + char *cmdline, unsigned long cmdline_len) >> +{ >> + struct riscv_image_header *h; >> + u64 flags; >> + bool be_image, be_kernel; >> + struct kexec_buf kbuf; >> + int ret; >> + >> + /* Check Image header */ >> + h = (struct riscv_image_header *)kernel; >> + if (!h->image_size) { >> + ret = -EINVAL; >> + goto out; >> + } >> + >> + /* Check endianness */ >> + flags = le64_to_cpu(h->flags); >> + be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); >> + be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); >> + if (be_image != be_kernel) { >> + ret = -EINVAL; >> + goto out; >> + } >> + >> + /* Load the kernel image */ >> + kbuf.image = image; >> + kbuf.buf_min = 0; >> + kbuf.buf_max = ULONG_MAX; >> + kbuf.top_down = false; >> + >> + kbuf.buffer = kernel; >> + kbuf.bufsz = kernel_len; >> + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; >> + kbuf.memsz = le64_to_cpu(h->image_size); >> + kbuf.buf_align = le64_to_cpu(h->text_offset); >> + >> + ret = kexec_add_buffer(&kbuf); >> + if (ret) { >> + pr_err("Error add kernel image ret=%d\n", ret); >> + goto out; >> + } >> + >> + image->start = kbuf.mem; >> + >> + pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", >> + kbuf.mem, kbuf.bufsz, kbuf.memsz); > > pr_info() or pr_debug()? > > >> + >> + ret = load_extra_segments(image, kbuf.mem, kbuf.memsz, >> + initrd, initrd_len, cmdline, cmdline_len); >> + >> +out: >> + return ret ? ERR_PTR(ret) : NULL; >> +} >> + >> +const struct kexec_file_ops image_kexec_ops = { >> + .probe = image_probe, >> + .load = image_load, >> +}; >> diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c >> index aedb8c16a283..5dc700834f1e 100644 >> --- a/arch/riscv/kernel/machine_kexec_file.c >> +++ b/arch/riscv/kernel/machine_kexec_file.c >> @@ -17,6 +17,7 @@ >> >> const struct kexec_file_ops * const kexec_file_loaders[] = { >> &elf_kexec_ops, >> + &image_kexec_ops, >> NULL >> }; >> >> -- >> 2.20.1 >> > > I tested these two patches. It works when > CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY is not enabled. When > CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY is enabled, the error is as > follows: > > [45.712019] Unknown rela relocation: 34 > [45.712258] Error loading purgatory ret=-8 > > How to fix it? > > Thanks, > Yunhui > Thanks for your attention to this series and RV kdump/kexec stack. As you noticed, this series and the kexec-tools RV support patches[1] didn't get any positive responses and have been suspended for a few months. And I'm now struggling in my own job, so really sorry that I have no time to continue these works. Although that, I would like to reply to your comments in case anyone wants to push this series: Comment1: Why rename elf_kexec.c to kexec_elf.c ? - s390 uses kexec_{elf,image}.c file names, so I followed it. Comment2: pr_info() or pr_debug()? - pr_debug() seems better. Comment3: Unknown rela relocation: 34 - The CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY was enabled when I posted this series, I didn't see this relocation error at that time. If you want to fix this relocation error, you need to figure out who hold the R_RISCV_ADD16 (34), and how it was generated (compiler options or other RV kernel configures). This commit 0f5f46a869a5 ("riscv: kexec: Remove -fPIE for PURGATORY_CFLAGS") may help. [1]: https://lore.kernel.org/kexec/DA84A55096BADE15+594e239a-ff6a-368f-ae92-b6cfebbf8dc0@tinylab.org/
Hi Song Shuai, On Mon, Feb 19, 2024 at 5:53 PM Song Shuai <songshuaishuai@tinylab.org> wrote: > > > Hi, yunhui: > > 在 2024/2/16 16:54, yunhui cui 写道: > > Hi Shuai, > > > > On Thu, Sep 14, 2023 at 10:09 AM Song Shuai <songshuaishuai@tinylab.org> wrote: > >> > >> This patch creates image_kexec_ops to load Image binary file > >> for kexec_file_load() syscall. > >> > >> Signed-off-by: Song Shuai <songshuaishuai@tinylab.org> > >> --- > >> arch/riscv/include/asm/image.h | 2 + > >> arch/riscv/include/asm/kexec.h | 1 + > >> arch/riscv/kernel/Makefile | 2 +- > >> arch/riscv/kernel/kexec_image.c | 97 ++++++++++++++++++++++++++ > >> arch/riscv/kernel/machine_kexec_file.c | 1 + > >> 5 files changed, 102 insertions(+), 1 deletion(-) > >> create mode 100644 arch/riscv/kernel/kexec_image.c > >> > >> diff --git a/arch/riscv/include/asm/image.h b/arch/riscv/include/asm/image.h > >> index e0b319af3681..8927a6ea1127 100644 > >> --- a/arch/riscv/include/asm/image.h > >> +++ b/arch/riscv/include/asm/image.h > >> @@ -30,6 +30,8 @@ > >> RISCV_HEADER_VERSION_MINOR) > >> > >> #ifndef __ASSEMBLY__ > >> +#define riscv_image_flag_field(flags, field)\ > >> + (((flags) >> field##_SHIFT) & field##_MASK) > >> /** > >> * struct riscv_image_header - riscv kernel image header > >> * @code0: Executable code > >> diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h > >> index 518825fe4160..b9ee8346cc8c 100644 > >> --- a/arch/riscv/include/asm/kexec.h > >> +++ b/arch/riscv/include/asm/kexec.h > >> @@ -56,6 +56,7 @@ extern riscv_kexec_method riscv_kexec_norelocate; > >> > >> #ifdef CONFIG_KEXEC_FILE > >> extern const struct kexec_file_ops elf_kexec_ops; > >> +extern const struct kexec_file_ops image_kexec_ops; > >> > >> struct purgatory_info; > >> int arch_kexec_apply_relocations_add(struct purgatory_info *pi, > >> diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile > >> index 1c62c639e875..9ecba3231a36 100644 > >> --- a/arch/riscv/kernel/Makefile > >> +++ b/arch/riscv/kernel/Makefile > >> @@ -86,7 +86,7 @@ endif > >> obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o > >> obj-$(CONFIG_KGDB) += kgdb.o > >> obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regso machine_kexec.o > >> -obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o machine_kexec_file.o > >> +obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o kexec_image.o machine_kexec_file.o > >> obj-$(CONFIG_CRASH_DUMP) += crash_dump.o > >> obj-$(CONFIG_CRASH_CORE) += crash_core.o > >> > >> diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c > >> new file mode 100644 > >> index 000000000000..b6aa7f59bd53 > >> --- /dev/null > >> +++ b/arch/riscv/kernel/kexec_image.c > >> @@ -0,0 +1,97 @@ > >> +// SPDX-License-Identifier: GPL-2.0 > >> +/* > >> + * RISC-V Kexec image loader > >> + * > >> + */ > >> + > >> +#define pr_fmt(fmt) "kexec_file(Image): " fmt > >> + > >> +#include <linux/err.h> > >> +#include <linux/errno.h> > >> +#include <linux/kernel.h> > >> +#include <linux/kexec.h> > >> +#include <linux/pe.h> > >> +#include <linux/string.h> > >> +#include <asm/byteorder.h> > >> +#include <asm/image.h> > >> + > >> +static int image_probe(const char *kernel_buf, unsigned long kernel_len) > >> +{ > >> + const struct riscv_image_header *h = > >> + (const struct riscv_image_header *)(kernel_buf); > >> + > >> + if (!h || (kernel_len < sizeof(*h))) > >> + return -EINVAL; > >> + > >> + /* According to Documentation/riscv/boot-image-header.rst, > >> + * use "magic2" field to check when version >= 0.2. > >> + */ > >> + > >> + if (h->version >= RISCV_HEADER_VERSION && > >> + memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2))) > >> + return -EINVAL; > >> + > >> + return 0; > >> +} > >> + > >> +static void *image_load(struct kimage *image, > >> + char *kernel, unsigned long kernel_len, > >> + char *initrd, unsigned long initrd_len, > >> + char *cmdline, unsigned long cmdline_len) > >> +{ > >> + struct riscv_image_header *h; > >> + u64 flags; > >> + bool be_image, be_kernel; > >> + struct kexec_buf kbuf; > >> + int ret; > >> + > >> + /* Check Image header */ > >> + h = (struct riscv_image_header *)kernel; > >> + if (!h->image_size) { > >> + ret = -EINVAL; > >> + goto out; > >> + } > >> + > >> + /* Check endianness */ > >> + flags = le64_to_cpu(h->flags); > >> + be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); > >> + be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); > >> + if (be_image != be_kernel) { > >> + ret = -EINVAL; > >> + goto out; > >> + } > >> + > >> + /* Load the kernel image */ > >> + kbuf.image = image; > >> + kbuf.buf_min = 0; > >> + kbuf.buf_max = ULONG_MAX; > >> + kbuf.top_down = false; > >> + > >> + kbuf.buffer = kernel; > >> + kbuf.bufsz = kernel_len; > >> + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; > >> + kbuf.memsz = le64_to_cpu(h->image_size); > >> + kbuf.buf_align = le64_to_cpu(h->text_offset); > >> + > >> + ret = kexec_add_buffer(&kbuf); > >> + if (ret) { > >> + pr_err("Error add kernel image ret=%d\n", ret); > >> + goto out; > >> + } > >> + > >> + image->start = kbuf.mem; > >> + > >> + pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", > >> + kbuf.mem, kbuf.bufsz, kbuf.memsz); > > > > pr_info() or pr_debug()? > > > > >> + > >> + ret = load_extra_segments(image, kbuf.mem, kbuf.memsz, > >> + initrd, initrd_len, cmdline, cmdline_len); > >> + > >> +out: > >> + return ret ? ERR_PTR(ret) : NULL; > >> +} > >> + > >> +const struct kexec_file_ops image_kexec_ops = { > >> + .probe = image_probe, > >> + .load = image_load, > >> +}; > >> diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c > >> index aedb8c16a283..5dc700834f1e 100644 > >> --- a/arch/riscv/kernel/machine_kexec_file.c > >> +++ b/arch/riscv/kernel/machine_kexec_file.c > >> @@ -17,6 +17,7 @@ > >> > >> const struct kexec_file_ops * const kexec_file_loaders[] = { > >> &elf_kexec_ops, > >> + &image_kexec_ops, > >> NULL > >> }; > >> > >> -- > >> 2.20.1 > >> > > > > I tested these two patches. It works when > > CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY is not enabled. When > > CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY is enabled, the error is as > > follows: > > > > [45.712019] Unknown rela relocation: 34 > > [45.712258] Error loading purgatory ret=-8 > > > > How to fix it? > > > Thanks, > > Yunhui > > > Thanks for your attention to this series and RV kdump/kexec stack. > > As you noticed, this series and the kexec-tools RV support patches[1] > didn't get any positive responses and have been suspended for a few > months. And I'm now struggling in my own job, so really sorry that I > have no time to continue these works. > > Although that, I would like to reply to your comments in case anyone > wants to push this series: > > Comment1: Why rename elf_kexec.c to kexec_elf.c ? > - s390 uses kexec_{elf,image}.c file names, so I followed it. Okay, so are arm64/x86. > Comment2: pr_info() or pr_debug()? > - pr_debug() seems better. > > Comment3: Unknown rela relocation: 34 > > - The CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY was enabled when I posted > this series, I didn't see this relocation error at that time. > > If you want to fix this relocation error, you need to figure out who > hold the R_RISCV_ADD16 (34), and how it was generated (compiler options > or other RV kernel configures). This commit 0f5f46a869a5 ("riscv: kexec: > Remove -fPIE for PURGATORY_CFLAGS") may help. Have you tested it with the top commit of linux-next? Can you share the .config? Thanks
diff --git a/arch/riscv/include/asm/image.h b/arch/riscv/include/asm/image.h index e0b319af3681..8927a6ea1127 100644 --- a/arch/riscv/include/asm/image.h +++ b/arch/riscv/include/asm/image.h @@ -30,6 +30,8 @@ RISCV_HEADER_VERSION_MINOR) #ifndef __ASSEMBLY__ +#define riscv_image_flag_field(flags, field)\ + (((flags) >> field##_SHIFT) & field##_MASK) /** * struct riscv_image_header - riscv kernel image header * @code0: Executable code diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h index 518825fe4160..b9ee8346cc8c 100644 --- a/arch/riscv/include/asm/kexec.h +++ b/arch/riscv/include/asm/kexec.h @@ -56,6 +56,7 @@ extern riscv_kexec_method riscv_kexec_norelocate; #ifdef CONFIG_KEXEC_FILE extern const struct kexec_file_ops elf_kexec_ops; +extern const struct kexec_file_ops image_kexec_ops; struct purgatory_info; int arch_kexec_apply_relocations_add(struct purgatory_info *pi, diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 1c62c639e875..9ecba3231a36 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -86,7 +86,7 @@ endif obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o obj-$(CONFIG_KGDB) += kgdb.o obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o -obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o machine_kexec_file.o +obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o kexec_image.o machine_kexec_file.o obj-$(CONFIG_CRASH_DUMP) += crash_dump.o obj-$(CONFIG_CRASH_CORE) += crash_core.o diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c new file mode 100644 index 000000000000..b6aa7f59bd53 --- /dev/null +++ b/arch/riscv/kernel/kexec_image.c @@ -0,0 +1,97 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * RISC-V Kexec image loader + * + */ + +#define pr_fmt(fmt) "kexec_file(Image): " fmt + +#include <linux/err.h> +#include <linux/errno.h> +#include <linux/kernel.h> +#include <linux/kexec.h> +#include <linux/pe.h> +#include <linux/string.h> +#include <asm/byteorder.h> +#include <asm/image.h> + +static int image_probe(const char *kernel_buf, unsigned long kernel_len) +{ + const struct riscv_image_header *h = + (const struct riscv_image_header *)(kernel_buf); + + if (!h || (kernel_len < sizeof(*h))) + return -EINVAL; + + /* According to Documentation/riscv/boot-image-header.rst, + * use "magic2" field to check when version >= 0.2. + */ + + if (h->version >= RISCV_HEADER_VERSION && + memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2))) + return -EINVAL; + + return 0; +} + +static void *image_load(struct kimage *image, + char *kernel, unsigned long kernel_len, + char *initrd, unsigned long initrd_len, + char *cmdline, unsigned long cmdline_len) +{ + struct riscv_image_header *h; + u64 flags; + bool be_image, be_kernel; + struct kexec_buf kbuf; + int ret; + + /* Check Image header */ + h = (struct riscv_image_header *)kernel; + if (!h->image_size) { + ret = -EINVAL; + goto out; + } + + /* Check endianness */ + flags = le64_to_cpu(h->flags); + be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); + be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); + if (be_image != be_kernel) { + ret = -EINVAL; + goto out; + } + + /* Load the kernel image */ + kbuf.image = image; + kbuf.buf_min = 0; + kbuf.buf_max = ULONG_MAX; + kbuf.top_down = false; + + kbuf.buffer = kernel; + kbuf.bufsz = kernel_len; + kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; + kbuf.memsz = le64_to_cpu(h->image_size); + kbuf.buf_align = le64_to_cpu(h->text_offset); + + ret = kexec_add_buffer(&kbuf); + if (ret) { + pr_err("Error add kernel image ret=%d\n", ret); + goto out; + } + + image->start = kbuf.mem; + + pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", + kbuf.mem, kbuf.bufsz, kbuf.memsz); + + ret = load_extra_segments(image, kbuf.mem, kbuf.memsz, + initrd, initrd_len, cmdline, cmdline_len); + +out: + return ret ? ERR_PTR(ret) : NULL; +} + +const struct kexec_file_ops image_kexec_ops = { + .probe = image_probe, + .load = image_load, +}; diff --git a/arch/riscv/kernel/machine_kexec_file.c b/arch/riscv/kernel/machine_kexec_file.c index aedb8c16a283..5dc700834f1e 100644 --- a/arch/riscv/kernel/machine_kexec_file.c +++ b/arch/riscv/kernel/machine_kexec_file.c @@ -17,6 +17,7 @@ const struct kexec_file_ops * const kexec_file_loaders[] = { &elf_kexec_ops, + &image_kexec_ops, NULL };