diff mbox series

[v3,2/2] riscv: kexec_file: Support loading Image binary file

Message ID 20250409193004.643839-3-bjorn@kernel.org (mailing list archive)
State New
Headers show
Series riscv: kexec_file: Support loading Image binary file | expand

Checks

Context Check Description
bjorn/pre-ci_am success Success
bjorn/build-rv32-defconfig success build-rv32-defconfig
bjorn/build-rv64-clang-allmodconfig success build-rv64-clang-allmodconfig
bjorn/build-rv64-gcc-allmodconfig success build-rv64-gcc-allmodconfig
bjorn/build-rv64-nommu-k210-defconfig success build-rv64-nommu-k210-defconfig
bjorn/build-rv64-nommu-k210-virt success build-rv64-nommu-k210-virt
bjorn/checkpatch warning checkpatch
bjorn/dtb-warn-rv64 success dtb-warn-rv64
bjorn/header-inline success header-inline
bjorn/kdoc success kdoc
bjorn/module-param success module-param
bjorn/verify-fixes success verify-fixes
bjorn/verify-signedoff success verify-signedoff

Commit Message

Björn Töpel April 9, 2025, 7:29 p.m. UTC
From: Song Shuai <songshuaishuai@tinylab.org>

This patch creates image_kexec_ops to load Image binary file
for kexec_file_load() syscall.

Signed-off-by: Song Shuai <songshuaishuai@tinylab.org>
Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
---
 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        | 96 ++++++++++++++++++++++++++
 arch/riscv/kernel/machine_kexec_file.c |  1 +
 5 files changed, 101 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/kernel/kexec_image.c

Comments

yunhui cui April 10, 2025, 1:44 a.m. UTC | #1
Hi Björn,

On Thu, Apr 10, 2025 at 3:31 AM Björn Töpel <bjorn@kernel.org> wrote:
>
> From: Song Shuai <songshuaishuai@tinylab.org>
>
> This patch creates image_kexec_ops to load Image binary file
> for kexec_file_load() syscall.
>
> Signed-off-by: Song Shuai <songshuaishuai@tinylab.org>
> Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
> ---
>  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        | 96 ++++++++++++++++++++++++++
>  arch/riscv/kernel/machine_kexec_file.c |  1 +
>  5 files changed, 101 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 d56305c8e631..0ead29826419 100644
> --- a/arch/riscv/kernel/Makefile
> +++ b/arch/riscv/kernel/Makefile
> @@ -107,7 +107,7 @@ obj-$(CONFIG_HOTPLUG_CPU)   += cpu-hotplug.o
>  obj-$(CONFIG_PARAVIRT)         += paravirt.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_VMCORE_INFO)      += vmcore_info.o
>
> diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c
> new file mode 100644
> index 000000000000..26a81774a78a
> --- /dev/null
> +++ b/arch/riscv/kernel/kexec_image.c
> @@ -0,0 +1,96 @@
> +// 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);

It is necessary to ensure that the kernel entry is aligned to
PMD_SIZE; otherwise, BUG_ON() in setup_vm() will be triggered.


> +
> +       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 99bd5a5f4234..e36104af2e24 100644
> --- a/arch/riscv/kernel/machine_kexec_file.c
> +++ b/arch/riscv/kernel/machine_kexec_file.c
> @@ -18,6 +18,7 @@
>
>  const struct kexec_file_ops * const kexec_file_loaders[] = {
>         &elf_kexec_ops,
> +       &image_kexec_ops,
>         NULL
>  };
>
> --
> 2.45.2
>
>

Thanks,
Yunhui
Björn Töpel April 10, 2025, 6:40 a.m. UTC | #2
Hey Yunhui!

yunhui cui <cuiyunhui@bytedance.com> writes:

> Hi Björn,
>
> On Thu, Apr 10, 2025 at 3:31 AM Björn Töpel <bjorn@kernel.org> wrote:

...

>> +       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);
>
> It is necessary to ensure that the kernel entry is aligned to
> PMD_SIZE; otherwise, BUG_ON() in setup_vm() will be triggered.

Indeed, and text_offset is that [1]. FWIW, the text_offset name in the
Image has always weird/confusing to me.


Björn

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/riscv/kernel/head.S#n47
diff mbox series

Patch

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 d56305c8e631..0ead29826419 100644
--- a/arch/riscv/kernel/Makefile
+++ b/arch/riscv/kernel/Makefile
@@ -107,7 +107,7 @@  obj-$(CONFIG_HOTPLUG_CPU)	+= cpu-hotplug.o
 obj-$(CONFIG_PARAVIRT)		+= paravirt.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_VMCORE_INFO)	+= vmcore_info.o
 
diff --git a/arch/riscv/kernel/kexec_image.c b/arch/riscv/kernel/kexec_image.c
new file mode 100644
index 000000000000..26a81774a78a
--- /dev/null
+++ b/arch/riscv/kernel/kexec_image.c
@@ -0,0 +1,96 @@ 
+// 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 99bd5a5f4234..e36104af2e24 100644
--- a/arch/riscv/kernel/machine_kexec_file.c
+++ b/arch/riscv/kernel/machine_kexec_file.c
@@ -18,6 +18,7 @@ 
 
 const struct kexec_file_ops * const kexec_file_loaders[] = {
 	&elf_kexec_ops,
+	&image_kexec_ops,
 	NULL
 };