Skip to contentSkip to conversation

kexec-tools RISC-V port

PATCH Superseded Björn Töpel 2 participants 9 messages Apr 9, 2025 – Apr 22, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover kexec-tools RISC-V port

From: Björn Töpel <[email protected]>

Hi!

This is an attempt to finally get the RISC-V patches for kexec-tools upstream.

Simon suggested in [1] that:

Quoted 6 lines

In my mind the big question is how to move RISC-V support from that branch, to being merged into main.

IIRC there were some issues that needed to be addressed. Perhaps they are all addressed by this series, and with some appropriate squashing we can move forwards with a series based on main?

I talked to Nick last week, and he suggested that we'd work the RISC-V collab Github [2], but I think it's just easier to do it on the list.

I've been collecting a number of different trees out there, and tried to group them in a series.

Given that there is not support for RISC-V upstream yet, I didn't see the need to carry all the fixes/updates ("development history") as separate commits -- it doesn't help bugtracking/bisectability.

Instead the first commit message is a big one, and tries to give attribution to all involved parties. Input/thoughts are very much welcome!

To properly test this on RISC-V, the upstream Linux kernel has most things in 6.15-rc1, e.g.:
commit 28093cfef5dd ("riscv/kexec_file: Handle R_RISCV_64 in purgatory relocator") commit 3f7023171df4 ("riscv/purgatory: 4B align purgatory_start")

To cross-build kexec-tools:

  | ./bootstrap 
  | ./configure --host=riscv64-linux-gnu
  | make

For some tests, additional kernel patches are needed, that as of today is not in 6.15-rc1:
* To test "kexec -s -l Image", kexec_file_load with Image support is needed [3]
* To test "kexec -c -l vmlinux" with CONFIG_RELOCATABLE, a kbuild fix is needed [4]
* Typically if you load initrd, or a large kernel [5] (since a bunch of Reserved regions are not properly exposed).

I've tested combinations with crashkernel (-p), and regular (-c/-s + -e) for:
* ACPI/UEFI
* DT/UEFI
* DT
* w/ initrd
* w/o initrd
* ELF (userland loader + kexec_load, and kexec_file_load)
* Image (userland loader + kexec_load, and kexec_file_load)

I'm sure there are combinations I've missed, but at least from my POV we're in a pretty good shape.

All authors: Please let me know if you prefer another split of the patches. Or something else for that matter! ;-)

Let's get the ball rolling!
Björn

[1] https://lore.kernel.org/kexec/[email protected]/
[2] https://github.com/riscv-collab/kexec-tools.git
[3] https://lore.kernel.org/linux-riscv/[email protected]/
[4] https://lore.kernel.org/linux-riscv/[email protected]/
[5] https://lore.kernel.org/linux-riscv/[email protected]/

Li Zhengyu (1):
RISC-V: Enable kexec_file_load syscall

Nick Kossifidis (1):
RISC-V: Add support for riscv kexec/kdump on kexec-tools

Song Shuai (2):
RISC-V: Separate elf_riscv_find_pbase out
RISC-V: Support loading Image binary file

 configure.ac                            |   3 +
 include/elf.h                           |   1 +
 kexec/Makefile                          |   1 +
 kexec/arch/riscv/Makefile               |  37 ++
 kexec/arch/riscv/crashdump-riscv.c      | 140 +++++++
 kexec/arch/riscv/image-header.h         |  88 ++++
 kexec/arch/riscv/include/arch/options.h |  43 ++
 kexec/arch/riscv/iomem.h                |  10 +
 kexec/arch/riscv/kexec-elf-riscv.c      | 184 +++++++++
 kexec/arch/riscv/kexec-image-riscv.c    |  95 +++++
 kexec/arch/riscv/kexec-riscv.c          | 526 ++++++++++++++++++++++++
 kexec/arch/riscv/kexec-riscv.h          |  53 +++
 kexec/dt-ops.c                          | 442 +++++++++++++++++++-
 kexec/dt-ops.h                          |   7 +
 kexec/kexec-syscall.h                   |   7 +
 purgatory/Makefile                      |   1 +
 purgatory/arch/riscv/Makefile           |   7 +
 17 files changed, 1644 insertions(+), 1 deletion(-)
 create mode 100644 kexec/arch/riscv/Makefile
 create mode 100644 kexec/arch/riscv/crashdump-riscv.c
 create mode 100644 kexec/arch/riscv/image-header.h
 create mode 100644 kexec/arch/riscv/include/arch/options.h
 create mode 100644 kexec/arch/riscv/iomem.h
 create mode 100644 kexec/arch/riscv/kexec-elf-riscv.c
 create mode 100644 kexec/arch/riscv/kexec-image-riscv.c
 create mode 100644 kexec/arch/riscv/kexec-riscv.c
 create mode 100644 kexec/arch/riscv/kexec-riscv.h
 create mode 100644 purgatory/arch/riscv/Makefile

base-commit: a7fcd424c4c80dea5a2fd5ffa274ffeb8129c790

Signature · 1 line
2.45.2
#5 lore raw

PATCH 2/4 RISC-V: Enable kexec_file_load syscall

From: Li Zhengyu <[email protected]>

Create prepare_kexec_file_options() function to prepare the options to kexec_file_load syscall, and it would be used in elf_riscv_load() or the future image_riscv_load().

The patch comes from the RISC-V Linux kernel_file_load support[1], So its author should be Li Zhengyu.

[1]: https://lore.kernel.org/all/[email protected]/

---
 kexec/arch/riscv/kexec-elf-riscv.c |  5 ++--
 kexec/arch/riscv/kexec-riscv.c     | 39 ++++++++++++++++++++++++++++++
 kexec/arch/riscv/kexec-riscv.h     |  1 +
 kexec/kexec-syscall.h              |  3 +++
 4 files changed, 45 insertions(+), 3 deletions(-)
kexec/arch/riscv/kexec-elf-riscv.c +2 -3
index f3c011c4c1dc..2b9f66d782af 100644
@@ -112,6 +112,7 @@ void elf_riscv_usage(void)
 {
 }
 
+
 int elf_riscv_load(int argc, char **argv, const char *buf, off_t len,
 		   struct kexec_info *info)
 {
@@ -127,9 +128,7 @@ int elf_riscv_load(int argc, char **argv, const char *buf, off_t len,
 	int ret = 0;
 
 	if (info->file_mode) {
-		fprintf(stderr, "kexec_file not supported on this "
-				"architecture\n");
-		return -EINVAL;
+		return prepare_kexec_file_options(info);
 	}
 
 	/* Parse the ELF file */
kexec/arch/riscv/kexec-riscv.c +39 -0
index 38d9a394aadd..bbc25c5cba41 100644
@@ -17,6 +17,12 @@
 #include "kexec-riscv.h"
 #include "iomem.h"
 #include <stdbool.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#ifndef _O_BINARY
+#define _O_BINARY 0
+#endif
 
 const struct arch_map_entry arches[] = {
 	{ "riscv32", KEXEC_ARCH_RISCV },
@@ -141,6 +147,39 @@ void arch_usage(void)
 	printf(riscv_opts_usage);
 }
 
+int prepare_kexec_file_options(struct kexec_info *info)
+{
+	int fd;
+	ssize_t result;
+	struct stat stats;
+
+	if (arch_options.cmdline) {
+		info->command_line = (char *)arch_options.cmdline;
+		info->command_line_len = strlen(info->command_line) + 1;
+	}
+
+	if (!arch_options.initrd_path) {
+		info->initrd_fd = -1;
+		return 0;
+	}
+
+	fd = open(arch_options.initrd_path, O_RDONLY | _O_BINARY);
+	if (fd < 0) {
+		fprintf(stderr, "Cannot open `%s': %s\n", arch_options.initrd_path,
+				strerror(errno));
+		return -EINVAL;
+	}
+	result = fstat(fd, &stats);
+	if (result < 0) {
+		close(fd);
+		fprintf(stderr, "Cannot stat: %s: %s\n", arch_options.initrd_path,
+				strerror(errno));
+		return -EINVAL;
+	}
+	info->initrd_fd = fd;
+	return 0;
+}
+
 int arch_process_options(int argc, char **argv)
 {
 	static const struct option options[] = {
kexec/arch/riscv/kexec-riscv.h +1 -0
index c4323a65dacf..f136c7eab561 100644
@@ -23,6 +23,7 @@ extern struct memory_range elfcorehdr_mem;
 int load_elfcorehdr(struct kexec_info *info);
 
 /* kexec-riscv.c */
+int prepare_kexec_file_options(struct kexec_info *info);
 int load_extra_segments(struct kexec_info *info, uint64_t kernel_base,
 			uint64_t kernel_size, uint64_t max_addr);
 
kexec/kexec-syscall.h +3 -0
index 89591ad17e7d..9b1757877a85 100644
@@ -80,6 +80,9 @@
 #ifdef __hppa__
 #define __NR_kexec_file_load	355
 #endif
+#if defined(__riscv__) || defined(__riscv)
+#define __NR_kexec_file_load	294
+#endif
 
 #ifndef __NR_kexec_file_load
 /* system call not available for the arch */
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/4 RISC-V: Add support for riscv kexec/kdump on kexec-tools

In reply to Björn Töpel

From: Nick Kossifidis <[email protected]>

This patch adds support for loading the ELF kernel image. It parses the current/provided device tree to determine the system's memory layout, and /proc/iomem for the various kernel segments.

Tested on Qemu's rv64 virt machine and SoC of T-Head RISC-V Xuantie 910 CPU.

Now, some history: The first stab at supporting kexec-tools on RISC-V was done by Nick Kossifidis. The initial patch has since then had a number of improvements/fixes by other authors. Given, this is the first commit for RISC-V, carrying the fixes/changes commits in the upstream tree does not really add anything (bisectability).

Instead all the fixes that were applied to Nick's first commit is outlined below:

Yixun Lan, and Xianting Tian:
* Fixed a failure to fail to find free memory area for dtb load when using initrd image [1].
* Fixed memory range size calculation in kexec/arch/riscv/crashdump-riscv.c:85

Simon Horman:
* RISC-V: distribute purgatory/riscv/Makefile

Include purgatory/riscv/Makefile in distribution tarball.

Local patch as it is planned to suggest this as a fix for the patch that introduced this problem. [2]

Song Shuai:
* RISC-V: Fix the undeclared ‘EM_RISCV’ build failure

Use local elf.h instead of linux/elf.h to fix this build error:

```
kexec/arch/riscv/crashdump-riscv.c:17:13: error: ‘EM_RISCV’ undeclared here (not in a function); did you mean ‘EM_CRIS’?
.machine = EM_RISCV,
^~~~~~~~
EM_CRIS
```

* RISC-V: Correct the usage of command line option

RISC-V process OPT_CMDLINE with the "command-line" partten, but the riscv_opts_usage shows the "cmdline" option. So correct the usage's output.

* RISC-V: Use linux,usable-memory-range for crash kernel

Now we use "memeory::linux,usable-memory" to indicate the available memory for the crash kernel.

While booting with UEFI, the crash kernel would use efi.memmap to re-populate memblock and then first kernel's memory would be corrputed. Consequently, the /proc/vmcore file failed to create in my local test.

And according to "chosen" dtschema [3], the available memory for the crash kernel should be held via "chosen::linux,usable-memory-range" property which will re-cap memblock even after UEFI's re-population.

* RISC-V: Get memory ranges from iomem

When booting with UEFI, Linux marks the Runtime Code/Data memory as no-map and then exports it to "Reserved" iomem_resource.

Kexc-tools uses dtb_get_memory_ranges() function to get memory ranges via parsing dtb, but it can't see the Reserved EFI Runtime memory. That would corrupt EFI Runtime memory and fail the kexeced kernel to deal EFI stuff.

In my test, the kexeced kernel warned "efi: System table signature incorrect!" and then paniced at efi_call_rts() due to the null efi.runtime.

So we should use /proc/iomem to get memory ranges.

Björn Töpel:
* Massaged this commit message!
* Fixed up the build, by adding missing RV stub.
* RISC-V: Only cap the upper/end usable memory window

When loading the initrd in the kexec_load flow, memory for the segments are searched from end to start. Only the max_usable should be capped, after a successful initrd addtion.

Currently min/max usable is set to the same value, making it impossible from subsequent segment allocations to success.

* RISC-V: Make get_memory_ranges() properly exclude "Reserved" regions

The get_memory_ranges() did not exclude "Reserved" regions from "System RAM" regions. It simply added "Reserved" as IOMEM, and IOMEM is not considered when looking for holes to place kexec segments.

Instead, do a two pass of the /proc/iomem file. First pass, adds all the "System RAM" memory, and the second pass removes all intersecting "Reserved" regions.

[1] https://lore.kernel.org/linux-riscv/CALecT5gQWn0PRO4Q24b6qkrfVE5OxsCp65TuhWTb30ceK_OJ0A@mail.gmail.com/
[2] https://lore.kernel.org/kexec/[email protected]/
[3] https://github.com/devicetree-org/dt-schema/blob/main/dtschema/schemas/chosen.yaml

---
 configure.ac                            |   3 +
 include/elf.h                           |   1 +
 kexec/Makefile                          |   1 +
 kexec/arch/riscv/Makefile               |  35 ++
 kexec/arch/riscv/crashdump-riscv.c      | 140 ++++++++
 kexec/arch/riscv/include/arch/options.h |  43 +++
 kexec/arch/riscv/iomem.h                |  10 +
 kexec/arch/riscv/kexec-elf-riscv.c      | 255 ++++++++++++++
 kexec/arch/riscv/kexec-riscv.c          | 431 +++++++++++++++++++++++
 kexec/arch/riscv/kexec-riscv.h          |  32 ++
 kexec/dt-ops.c                          | 442 +++++++++++++++++++++++-
 kexec/dt-ops.h                          |   7 +
 kexec/kexec-syscall.h                   |   4 +
 purgatory/Makefile                      |   1 +
 purgatory/arch/riscv/Makefile           |   7 +
 15 files changed, 1411 insertions(+), 1 deletion(-)
 create mode 100644 kexec/arch/riscv/Makefile
 create mode 100644 kexec/arch/riscv/crashdump-riscv.c
 create mode 100644 kexec/arch/riscv/include/arch/options.h
 create mode 100644 kexec/arch/riscv/iomem.h
 create mode 100644 kexec/arch/riscv/kexec-elf-riscv.c
 create mode 100644 kexec/arch/riscv/kexec-riscv.c
 create mode 100644 kexec/arch/riscv/kexec-riscv.h
 create mode 100644 purgatory/arch/riscv/Makefile
configure.ac +3 -0
index 43cdb64bc91b..fa96d1782f4f 100644
@@ -61,6 +61,9 @@ case $target_cpu in
 	loongarch*)
 		ARCH="loongarch"
 		;;
+	riscv32|riscv64)
+		ARCH="riscv"
+		;;
 	* )
 		AC_MSG_ERROR([unsupported architecture $target_cpu])
 		;;
include/elf.h +1 -0
index 1c8d2ccf6d26..93a5ee5eeffa 100644
@@ -259,6 +259,7 @@ typedef struct
 #define EM_ARC_A5	93		/* ARC Cores Tangent-A5 */
 #define EM_XTENSA	94		/* Tensilica Xtensa Architecture */
 #define EM_AARCH64	183		/* ARM AARCH64 */
+#define EM_RISCV	243		/* RISC-V */
 #define EM_LOONGARCH	258		/* Loongson Loongarch*/
 #define EM_NUM		184
 
kexec/Makefile +1 -0
index e969d1e18c5a..8b5851decaea 100644
@@ -92,6 +92,7 @@ include $(srcdir)/kexec/arch/mips/Makefile
 include $(srcdir)/kexec/arch/cris/Makefile
 include $(srcdir)/kexec/arch/ppc/Makefile
 include $(srcdir)/kexec/arch/ppc64/Makefile
+include $(srcdir)/kexec/arch/riscv/Makefile
 include $(srcdir)/kexec/arch/s390/Makefile
 include $(srcdir)/kexec/arch/sh/Makefile
 include $(srcdir)/kexec/arch/x86_64/Makefile
kexec/arch/riscv/Makefile new file +35 -0
new file mode 100644
index 000000000000..f26cc9025e77
@@ -0,0 +1,35 @@
+#
+# kexec riscv
+#
+riscv_KEXEC_SRCS =  kexec/arch/riscv/kexec-riscv.c
+riscv_KEXEC_SRCS += kexec/arch/riscv/kexec-elf-riscv.c
+riscv_KEXEC_SRCS += kexec/arch/riscv/crashdump-riscv.c
+
+riscv_MEM_REGIONS = kexec/mem_regions.c
+
+riscv_DT_OPS += kexec/dt-ops.c
+
+riscv_ARCH_REUSE_INITRD =
+
+riscv_CPPFLAGS += -I $(srcdir)/kexec/
+
+dist += kexec/arch/riscv/Makefile $(riscv_KEXEC_SRCS)			\
+	kexec/arch/riscv/kexec-riscv.h					\
+	kexec/arch/riscv/include/arch/options.h
+
+ifdef HAVE_LIBFDT
+
+LIBS += -lfdt
+
+else
+
+include $(srcdir)/kexec/libfdt/Makefile.libfdt
+
+libfdt_SRCS += $(LIBFDT_SRCS:%=kexec/libfdt/%)
+
+riscv_CPPFLAGS += -I$(srcdir)/kexec/libfdt
+
+riscv_KEXEC_SRCS += $(libfdt_SRCS)
+
+endif
+
kexec/arch/riscv/crashdump-riscv.c new file +140 -0

140 lines not on this page · raw message

kexec/arch/riscv/include/arch/options.h new file +43 -0

43 lines not on this page · raw message

kexec/arch/riscv/iomem.h new file +10 -0
new file mode 100644
index 000000000000..7671e26a9594
@@ -0,0 +1,10 @@
+#ifndef IOMEM_H
+#define IOMEM_H
+
+#define SYSTEM_RAM		"System RAM\n"
+#define KERNEL_CODE		"Kernel code\n"
+#define KERNEL_DATA		"Kernel data\n"
+#define CRASH_KERNEL		"Crash kernel\n"
+#define IOMEM_RESERVED		"Reserved\n"
+
+#endif
kexec/arch/riscv/kexec-elf-riscv.c new file +255 -0

255 lines not on this page · raw message

kexec/arch/riscv/kexec-riscv.c new file +431 -0

431 lines not on this page · raw message

kexec/arch/riscv/kexec-riscv.h new file +32 -0
new file mode 100644
index 000000000000..c4323a65dacf
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2019 FORTH-ICS/CARV
+ *              Nick Kossifidis <[email protected]>
+ */
+
+struct fdt_image {
+	char	*buf;
+	off_t	size;
+};
+
+struct riscv_opts {
+	char *cmdline;
+	char *fdt_path;
+	char *initrd_path;
+	uint64_t initrd_start;
+	uint64_t initrd_end;
+	struct fdt_image *fdt;
+};
+
+/* crashdump-riscv.c */
+extern struct memory_range elfcorehdr_mem;
+int load_elfcorehdr(struct kexec_info *info);
+
+/* kexec-riscv.c */
+int load_extra_segments(struct kexec_info *info, uint64_t kernel_base,
+			uint64_t kernel_size, uint64_t max_addr);
+
+int elf_riscv_probe(const char *buf, off_t len);
+void elf_riscv_usage(void);
+int elf_riscv_load(int argc, char **argv, const char *buf, off_t len,
+		   struct kexec_info *info);
kexec/dt-ops.c +441 -1

460 lines not on this page · raw message

kexec/dt-ops.h +7 -0
index 03659ce283db..3014205d8e98 100644
@@ -11,4 +11,11 @@ int dtb_set_property(char **dtb, off_t *dtb_size, const char *node,
 
 int dtb_delete_property(char *dtb, const char *node, const char *prop);
 
+void dtb_extract_int_property(uint64_t *val, const void *buf, uint32_t cells);
+void dtb_fill_int_property(void *buf, uint64_t val, uint32_t cells);
+int dtb_add_range_property(char **dtb, off_t *dtb_size, uint64_t start, uint64_t end,
+                           const char *node, const char* parent);
+int dtb_get_memory_ranges(char *dtb, struct memory_ranges *mem_ranges,
+			  struct memory_ranges *extra_ranges);
+
 #endif
kexec/kexec-syscall.h +4 -0
index cc32c0162aec..89591ad17e7d 100644
@@ -140,6 +140,7 @@ static inline long kexec_file_load(int kernel_fd, int initrd_fd,
 #define KEXEC_ARCH_MIPS_LE (10 << 16)
 #define KEXEC_ARCH_MIPS    ( 8 << 16)
 #define KEXEC_ARCH_CRIS    (76 << 16)
+#define KEXEC_ARCH_RISCV   (243 << 16)
 #define KEXEC_ARCH_LOONGARCH	(258 << 16)
 
 #define KEXEC_MAX_SEGMENTS 16
@@ -187,5 +188,8 @@ static inline long kexec_file_load(int kernel_fd, int initrd_fd,
 #if defined(__loongarch__)
 #define KEXEC_ARCH_NATIVE	KEXEC_ARCH_LOONGARCH
 #endif
+#if defined(__riscv__) || defined(__riscv)
+#define KEXEC_ARCH_NATIVE	KEXEC_ARCH_RISCV
+#endif
 
 #endif /* KEXEC_SYSCALL_H */
purgatory/Makefile +1 -0
index 4d2d07134bd5..7d1175f2445c 100644
@@ -25,6 +25,7 @@ include $(srcdir)/purgatory/arch/ia64/Makefile
 include $(srcdir)/purgatory/arch/mips/Makefile
 include $(srcdir)/purgatory/arch/ppc/Makefile
 include $(srcdir)/purgatory/arch/ppc64/Makefile
+include $(srcdir)/purgatory/arch/riscv/Makefile
 include $(srcdir)/purgatory/arch/s390/Makefile
 include $(srcdir)/purgatory/arch/sh/Makefile
 include $(srcdir)/purgatory/arch/x86_64/Makefile
purgatory/arch/riscv/Makefile new file +7 -0
new file mode 100644
index 000000000000..49a6fef4a904
@@ -0,0 +1,7 @@
+#
+# Purgatory riscv
+#
+
+riscv_PURGATORY_SRCS =
+
+dist += purgatory/arch/riscv/Makefile $(riscv_PURGATORY_SRCS)
Signature · 7 lines
2.45.2


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#6 lore raw

PATCH 3/4 RISC-V: Separate elf_riscv_find_pbase out

In reply to Björn Töpel

From: Song Shuai <[email protected]>

The is the preparative patch for RISC-V kexec Image file support.

Separate the elf_riscv_find_pbase() function out to allow kernel_load syscall load Image binary file.

---
 kexec/arch/riscv/kexec-elf-riscv.c | 72 +-----------------------------
 kexec/arch/riscv/kexec-riscv.c     | 55 +++++++++++++++++++++++
 kexec/arch/riscv/kexec-riscv.h     | 13 ++++++
 3 files changed, 69 insertions(+), 71 deletions(-)
kexec/arch/riscv/kexec-elf-riscv.c +1 -71
index 2b9f66d782af..434873cf9f08 100644
@@ -12,76 +12,6 @@
 #include "kexec-syscall.h"	/* For KEXEC_ON_CRASH */
 #include "kexec-riscv.h"
 
-
-/*********\
-* HELPERS *
-\*********/
-
-/*
- * Go through the available physical memory regions and
- * find one that can hold an image of the specified size.
- * Note: This is called after get_memory_ranges so
- * info->memory_range[] should be populated. Also note that
- * memory ranges are sorted, so we'll return the first region
- * that's big enough for holding the image.
- */
-static int elf_riscv_find_pbase(struct kexec_info *info, off_t *addr,
-				off_t size)
-{
-	int i = 0;
-	off_t start = 0;
-	off_t end = 0;
-	int ret = 0;
-
-	/*
-	 * If this image is for a crash kernel, use the region
-	 * the primary kernel has already reserved for us.
-	 */
-	if (info->kexec_flags & KEXEC_ON_CRASH) {
-		ret = get_crash_kernel_load_range((uint64_t *) &start,
-						  (uint64_t *) &end);
-		if (!ret) {
-			/*
-			 * Kernel should be aligned to the nearest
-			 * hugepage (2MB for RV64, 4MB for RV32).
-			 */
-#if __riscv_xlen == 64
-			start = _ALIGN_UP(start, 0x200000);
-#else
-			start = _ALIGN_UP(start, 0x400000);
-#endif
-			if (end > start && ((end - start) >= size)) {
-				*addr = start;
-				return 0;
-			}
-
-			return -EFBIG;
-		} else
-			return ENOCRASHKERNEL;
-	}
-
-	for (i = 0; i < info->memory_ranges; i++) {
-		if (info->memory_range[i].type != RANGE_RAM)
-			continue;
-
-		start = info->memory_range[i].start;
-		end = info->memory_range[i].end;
-
-#if __riscv_xlen == 64
-		start = _ALIGN_UP(start, 0x200000);
-#else
-		start = _ALIGN_UP(start, 0x400000);
-#endif
-
-		if (end > start && ((end - start) >= size)) {
-			*addr = start;
-			return 0;
-		}
-	}
-
-	return -EFBIG;
-}
-
 /**************\
 * ENTRY POINTS *
 \**************/
@@ -182,7 +112,7 @@ int elf_riscv_load(int argc, char **argv, const char *buf, off_t len,
 		  kernel_size / 1024, old_base_addr, old_start_addr);
 
 	/* Get a continuous physical region that can hold the kernel */
-	ret = elf_riscv_find_pbase(info, &new_base_addr, kernel_size);
+	ret = riscv_find_pbase(info, &new_base_addr, kernel_size, KERNEL_ALIGN);
 	if (ret < 0) {
 		fprintf(stderr, "Could not find a memory region for the "
 				"provided ELF image\n");
kexec/arch/riscv/kexec-riscv.c +55 -0

61 lines not on this page · raw message

kexec/arch/riscv/kexec-riscv.h +13 -0
index f136c7eab561..618099291117 100644
@@ -4,6 +4,17 @@
  *              Nick Kossifidis <[email protected]>
  */
 
+/*
+ * Kernel should be aligned to the nearest
+ * hugepage (2MB for RV64, 4MB for RV32).
+ */
+
+#if __riscv_xlen == 64
+#define KERNEL_ALIGN 0x200000
+#else
+#define KERNEL_ALIGN 0x400000
+#endif
+
 struct fdt_image {
 	char	*buf;
 	off_t	size;
@@ -26,6 +37,8 @@ int load_elfcorehdr(struct kexec_info *info);
 int prepare_kexec_file_options(struct kexec_info *info);
 int load_extra_segments(struct kexec_info *info, uint64_t kernel_base,
 			uint64_t kernel_size, uint64_t max_addr);
+int riscv_find_pbase(struct kexec_info *info, off_t *addr,
+				off_t size, off_t align);
 
 int elf_riscv_probe(const char *buf, off_t len);
 void elf_riscv_usage(void);
Signature · 1 line
2.45.2
#7 lore raw

PATCH 4/4 RISC-V: Support loading Image binary file

In reply to Björn Töpel

From: Song Shuai <[email protected]>

Add image-riscv file_type to probe/load Image file type,

As for kexec_load, find the pbase aligned text_offset from image header and prepare segments for this syscall.

for kexec_file_load, setup the related options and let kernel part to deal with the Image.

---
 kexec/arch/riscv/Makefile            |  2 +
 kexec/arch/riscv/image-header.h      | 88 ++++++++++++++++++++++++++
 kexec/arch/riscv/kexec-image-riscv.c | 95 ++++++++++++++++++++++++++++
 kexec/arch/riscv/kexec-riscv.c       |  1 +
 kexec/arch/riscv/kexec-riscv.h       |  7 ++
 5 files changed, 193 insertions(+)
 create mode 100644 kexec/arch/riscv/image-header.h
 create mode 100644 kexec/arch/riscv/kexec-image-riscv.c
kexec/arch/riscv/Makefile +2 -0
index f26cc9025e77..37ef7603fc38 100644
@@ -3,6 +3,7 @@
 #
 riscv_KEXEC_SRCS =  kexec/arch/riscv/kexec-riscv.c
 riscv_KEXEC_SRCS += kexec/arch/riscv/kexec-elf-riscv.c
+riscv_KEXEC_SRCS += kexec/arch/riscv/kexec-image-riscv.c
 riscv_KEXEC_SRCS += kexec/arch/riscv/crashdump-riscv.c
 
 riscv_MEM_REGIONS = kexec/mem_regions.c
@@ -15,6 +16,7 @@ riscv_CPPFLAGS += -I $(srcdir)/kexec/
 
 dist += kexec/arch/riscv/Makefile $(riscv_KEXEC_SRCS)			\
 	kexec/arch/riscv/kexec-riscv.h					\
+	kexec/arch/riscv/image-header.h					\
 	kexec/arch/riscv/include/arch/options.h
 
 ifdef HAVE_LIBFDT
kexec/arch/riscv/image-header.h new file +88 -0

88 lines not on this page · raw message

kexec/arch/riscv/kexec-image-riscv.c new file +95 -0

95 lines not on this page · raw message

kexec/arch/riscv/kexec-riscv.c +1 -0
index 631659301749..f34b46831160 100644
@@ -33,6 +33,7 @@ const struct arch_map_entry arches[] = {
 
 struct file_type file_type[] = {
 	{"elf-riscv", elf_riscv_probe, elf_riscv_load, elf_riscv_usage},
+	{"image-riscv", image_riscv_probe, image_riscv_load, image_riscv_usage},
 };
 int file_types = sizeof(file_type) / sizeof(file_type[0]);
 
kexec/arch/riscv/kexec-riscv.h +7 -0
index 618099291117..cfb03779a022 100644
@@ -40,7 +40,14 @@ int load_extra_segments(struct kexec_info *info, uint64_t kernel_base,
 int riscv_find_pbase(struct kexec_info *info, off_t *addr,
 				off_t size, off_t align);
 
+/* kexec-elf-riscv.c */
 int elf_riscv_probe(const char *buf, off_t len);
 void elf_riscv_usage(void);
 int elf_riscv_load(int argc, char **argv, const char *buf, off_t len,
 		   struct kexec_info *info);
+
+/* kexec-image-riscv.c */
+int image_riscv_probe(const char *buf, off_t len);
+void image_riscv_usage(void);
+int image_riscv_load(int argc, char **argv, const char *buf, off_t len,
+		   struct kexec_info *info);
Signature · 1 line
2.45.2
#8 lore raw
In reply to Björn Töpel
Quoted 19 lines · Björn Töpel

From: Björn Töpel <[email protected]>

Hi!

This is an attempt to finally get the RISC-V patches for kexec-tools upstream.

Simon suggested in [1] that:

Quoted 6 lines

In my mind the big question is how to move RISC-V support from that branch, to being merged into main.

IIRC there were some issues that needed to be addressed. Perhaps they are all addressed by this series, and with some appropriate squashing we can move forwards with a series based on main?

I talked to Nick last week, and he suggested that we'd work the RISC-V collab Github [2], but I think it's just easier to do it on the list.

Quoted 6 lines

I've been collecting a number of different trees out there, and tried to group them in a series.

Given that there is not support for RISC-V upstream yet, I didn't see the need to carry all the fixes/updates ("development history") as separate commits -- it doesn't help bugtracking/bisectability.

Thanks Björn,

I haven't looked over the patches yet.
But in principle I agree with this approach.

Quoted 42 lines

Instead the first commit message is a big one, and tries to give attribution to all involved parties. Input/thoughts are very much welcome!

To properly test this on RISC-V, the upstream Linux kernel has most things in 6.15-rc1, e.g.:
commit 28093cfef5dd ("riscv/kexec_file: Handle R_RISCV_64 in purgatory relocator") commit 3f7023171df4 ("riscv/purgatory: 4B align purgatory_start")

To cross-build kexec-tools:

  | ./bootstrap 
  | ./configure --host=riscv64-linux-gnu
  | make

For some tests, additional kernel patches are needed, that as of today is not in 6.15-rc1:
* To test "kexec -s -l Image", kexec_file_load with Image support is needed [3]
* To test "kexec -c -l vmlinux" with CONFIG_RELOCATABLE, a kbuild fix is needed [4]
* Typically if you load initrd, or a large kernel [5] (since a bunch of Reserved regions are not properly exposed).

I've tested combinations with crashkernel (-p), and regular (-c/-s + -e) for:
* ACPI/UEFI
* DT/UEFI
* DT
* w/ initrd
* w/o initrd
* ELF (userland loader + kexec_load, and kexec_file_load)
* Image (userland loader + kexec_load, and kexec_file_load)

I'm sure there are combinations I've missed, but at least from my POV we're in a pretty good shape.

All authors: Please let me know if you prefer another split of the patches. Or something else for that matter! ;-)

Let's get the ball rolling!
Björn

...

#2 lore raw

Re: [PATCH 1/4] RISC-V: Add support for riscv kexec/kdump on kexec-tools

In reply to Björn Töpel
Quoted 110 lines · Björn Töpel

From: Nick Kossifidis <[email protected]>

This patch adds support for loading the ELF kernel image. It parses the current/provided device tree to determine the system's memory layout, and /proc/iomem for the various kernel segments.

Tested on Qemu's rv64 virt machine and SoC of T-Head RISC-V Xuantie 910 CPU.

Now, some history: The first stab at supporting kexec-tools on RISC-V was done by Nick Kossifidis. The initial patch has since then had a number of improvements/fixes by other authors. Given, this is the first commit for RISC-V, carrying the fixes/changes commits in the upstream tree does not really add anything (bisectability).

Instead all the fixes that were applied to Nick's first commit is outlined below:

Yixun Lan, and Xianting Tian:
* Fixed a failure to fail to find free memory area for dtb load when using initrd image [1].
* Fixed memory range size calculation in kexec/arch/riscv/crashdump-riscv.c:85

Simon Horman:
* RISC-V: distribute purgatory/riscv/Makefile

Include purgatory/riscv/Makefile in distribution tarball.

Local patch as it is planned to suggest this as a fix for the patch that introduced this problem. [2]

Song Shuai:
* RISC-V: Fix the undeclared ‘EM_RISCV’ build failure

Use local elf.h instead of linux/elf.h to fix this build error:

```
kexec/arch/riscv/crashdump-riscv.c:17:13: error: ‘EM_RISCV’ undeclared here (not in a function); did you mean ‘EM_CRIS’?
.machine = EM_RISCV,
^~~~~~~~
EM_CRIS
```

* RISC-V: Correct the usage of command line option

RISC-V process OPT_CMDLINE with the "command-line" partten, but the riscv_opts_usage shows the "cmdline" option. So correct the usage's output.

* RISC-V: Use linux,usable-memory-range for crash kernel

Now we use "memeory::linux,usable-memory" to indicate the available memory for the crash kernel.

While booting with UEFI, the crash kernel would use efi.memmap to re-populate memblock and then first kernel's memory would be corrputed. Consequently, the /proc/vmcore file failed to create in my local test.

And according to "chosen" dtschema [3], the available memory for the crash kernel should be held via "chosen::linux,usable-memory-range" property which will re-cap memblock even after UEFI's re-population.

* RISC-V: Get memory ranges from iomem

When booting with UEFI, Linux marks the Runtime Code/Data memory as no-map and then exports it to "Reserved" iomem_resource.

Kexc-tools uses dtb_get_memory_ranges() function to get memory ranges via parsing dtb, but it can't see the Reserved EFI Runtime memory. That would corrupt EFI Runtime memory and fail the kexeced kernel to deal EFI stuff.

In my test, the kexeced kernel warned "efi: System table signature incorrect!" and then paniced at efi_call_rts() due to the null efi.runtime.

So we should use /proc/iomem to get memory ranges.

Björn Töpel:
* Massaged this commit message!
* Fixed up the build, by adding missing RV stub.
* RISC-V: Only cap the upper/end usable memory window

When loading the initrd in the kexec_load flow, memory for the segments are searched from end to start. Only the max_usable should be capped, after a successful initrd addtion.

Currently min/max usable is set to the same value, making it impossible from subsequent segment allocations to success.

* RISC-V: Make get_memory_ranges() properly exclude "Reserved" regions

The get_memory_ranges() did not exclude "Reserved" regions from "System RAM" regions. It simply added "Reserved" as IOMEM, and IOMEM is not considered when looking for holes to place kexec segments.

Instead, do a two pass of the /proc/iomem file. First pass, adds all the "System RAM" memory, and the second pass removes all intersecting "Reserved" regions.

[1] https://lore.kernel.org/linux-riscv/CALecT5gQWn0PRO4Q24b6qkrfVE5OxsCp65TuhWTb30ceK_OJ0A@mail.gmail.com/
[2] https://lore.kernel.org/kexec/[email protected]/
[3] https://github.com/devicetree-org/dt-schema/blob/main/dtschema/schemas/chosen.yaml

Quoted 7 lines

Thanks Björn,

Overall I am happy with this patchset. But there are a few minor points I'd like addressed, which relate strictly to build coverage and the CI. I can handle these myself if you prefer.

Firstly, it would be really great if the following could be included at the end of the series, so there is some build coverage of RISC-V in the CI. (Feel free to rewrite or attribute however you see fit - it is a trival one liner.)

From: Simon Horman <[email protected]>
Subject: [PATCH] workflow: Add riscv64

Add riscv64 to matrix of build architectures.

---
 .github/workflows/build.yml | 1 +
 1 file changed, 1 insertion(+)
.github/workflows/build.yml +1 -0
index fd9ea6c8eca7..d1b3e74391ff 100644
@@ -19,6 +19,7 @@ jobs:
         - powerpc
         - powerpc64
         - powerpc64le
+        - riscv64
         - sh4
         - s390x
         - x86_64-x32

...

Quoted 12 lines
kexec/arch/riscv/Makefile new file +6 -0
new file mode 100644
index 000000000000..f26cc9025e77
@@ -0,0 +1,35 @@
+#
+# kexec riscv
+#
+riscv_KEXEC_SRCS =  kexec/arch/riscv/kexec-riscv.c
+riscv_KEXEC_SRCS += kexec/arch/riscv/kexec-elf-riscv.c
+riscv_KEXEC_SRCS += kexec/arch/riscv/crashdump-riscv.c

kexec/arch/riscv/Makefile line 16

Quoted 10 lines
kexec/arch/riscv/Makefile new file +10 -0
+
+riscv_MEM_REGIONS = kexec/mem_regions.c
+
+riscv_DT_OPS += kexec/dt-ops.c
+
+riscv_ARCH_REUSE_INITRD =
+
+riscv_CPPFLAGS += -I $(srcdir)/kexec/
+
+dist += kexec/arch/riscv/Makefile $(riscv_KEXEC_SRCS)			\

kexec/arch/riscv/iomem.h also needs to be added to dist so that it shows up in the distribution tarball.

kexec/arch/riscv/Makefile line 18

Quoted 2 lines
kexec/arch/riscv/Makefile new file +2 -0
+	kexec/arch/riscv/kexec-riscv.h					\
+	kexec/arch/riscv/include/arch/options.h

Also, it would be nice to sort these lines alphabetically.

Quoted 17 lines
kexec/arch/riscv/Makefile new file +17 -0
+
+ifdef HAVE_LIBFDT
+
+LIBS += -lfdt
+
+else
+
+include $(srcdir)/kexec/libfdt/Makefile.libfdt
+
+libfdt_SRCS += $(LIBFDT_SRCS:%=kexec/libfdt/%)
+
+riscv_CPPFLAGS += -I$(srcdir)/kexec/libfdt
+
+riscv_KEXEC_SRCS += $(libfdt_SRCS)
+
+endif
+

...

kexec/dt-ops.c line 11

Quoted 13 lines
kexec/dt-ops.c +2 -0
index 0a96b75f65aa..3e285ab2043b 100644
@@ -4,9 +4,11 @@
 #include <libfdt.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 
 #include "kexec.h"
 #include "dt-ops.h"
+#include "mem_regions.h"

As dt-ops.c will now use symbols that are implemented in mem_regions.c when the former is compiled the latter now must be compiled too.

In practice it is only mips where that is not the case. And I believe that can be addressed by squashing the following into this patch.

kexec/arch/mips/Makefile +2 -0
index 1fe788608fbe..fa87fbdb7897 100644
@@ -11,6 +11,8 @@ mips_FS2DT_INCLUDE = \
 	-include $(srcdir)/kexec/arch/mips/crashdump-mips.h \
 	-include $(srcdir)/kexec/arch/mips/kexec-mips.h
 
+mips_MEM_REGIONS = kexec/mem_regions.c
+
 mips_DT_OPS += kexec/dt-ops.c
 
 include $(srcdir)/kexec/libfdt/Makefile.libfdt

FTR, I believe this is the same mem_regions_alloc_and_add issue that I flagged in the thread [2].

And at the link below you can see a build failure with this patch, but not the fix above, applied:

https://github.com/horms/kexec-tools/actions/runs/14591381633/job/40927219243

#3 lore raw

Re: [PATCH 1/4] RISC-V: Add support for riscv kexec/kdump on kexec-tools

Simon!

Quoted 5 lines · Simon Horman

Thanks Björn,

Overall I am happy with this patchset. But there are a few minor points I'd like addressed, which relate strictly to build coverage and the CI. I can handle these myself if you prefer.

It's really up to you! What do you prefer? I can spin a v2 if that's easier for you (and then I'll try to address all the things you pointed out below)!

Björn

Quoted 123 lines

Firstly, it would be really great if the following could be included at the end of the series, so there is some build coverage of RISC-V in the CI. (Feel free to rewrite or attribute however you see fit - it is a trival one liner.)

From: Simon Horman <[email protected]>
Subject: [PATCH] workflow: Add riscv64

Add riscv64 to matrix of build architectures.

---
 .github/workflows/build.yml | 1 +
 1 file changed, 1 insertion(+)
.github/workflows/build.yml +1 -0
index fd9ea6c8eca7..d1b3e74391ff 100644
@@ -19,6 +19,7 @@ jobs:
         - powerpc
         - powerpc64
         - powerpc64le
+        - riscv64
         - sh4
         - s390x
         - x86_64-x32
 
...
Quoted 13 lines
kexec/arch/riscv/Makefile new file +6 -0
new file mode 100644
index 000000000000..f26cc9025e77
@@ -0,0 +1,35 @@
+#
+# kexec riscv
+#
+riscv_KEXEC_SRCS =  kexec/arch/riscv/kexec-riscv.c
+riscv_KEXEC_SRCS += kexec/arch/riscv/kexec-elf-riscv.c
+riscv_KEXEC_SRCS += kexec/arch/riscv/crashdump-riscv.c

kexec/arch/riscv/Makefile line 16

Quoted 10 lines
kexec/arch/riscv/Makefile new file +10 -0
+
+riscv_MEM_REGIONS = kexec/mem_regions.c
+
+riscv_DT_OPS += kexec/dt-ops.c
+
+riscv_ARCH_REUSE_INITRD =
+
+riscv_CPPFLAGS += -I $(srcdir)/kexec/
+
+dist += kexec/arch/riscv/Makefile $(riscv_KEXEC_SRCS)			\

kexec/arch/riscv/iomem.h also needs to be added to dist so that it shows up in the distribution tarball.

kexec/arch/riscv/Makefile line 18

Quoted 2 lines
kexec/arch/riscv/Makefile new file +2 -0
+	kexec/arch/riscv/kexec-riscv.h					\
+	kexec/arch/riscv/include/arch/options.h

Also, it would be nice to sort these lines alphabetically.

Quoted 17 lines
kexec/arch/riscv/Makefile new file +17 -0
+
+ifdef HAVE_LIBFDT
+
+LIBS += -lfdt
+
+else
+
+include $(srcdir)/kexec/libfdt/Makefile.libfdt
+
+libfdt_SRCS += $(LIBFDT_SRCS:%=kexec/libfdt/%)
+
+riscv_CPPFLAGS += -I$(srcdir)/kexec/libfdt
+
+riscv_KEXEC_SRCS += $(libfdt_SRCS)
+
+endif
+

...

kexec/dt-ops.c line 11

Quoted 13 lines
kexec/dt-ops.c +2 -0
index 0a96b75f65aa..3e285ab2043b 100644
@@ -4,9 +4,11 @@
 #include <libfdt.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 
 #include "kexec.h"
 #include "dt-ops.h"
+#include "mem_regions.h"

As dt-ops.c will now use symbols that are implemented in mem_regions.c when the former is compiled the latter now must be compiled too.

In practice it is only mips where that is not the case. And I believe that can be addressed by squashing the following into this patch.

kexec/arch/mips/Makefile +2 -0
index 1fe788608fbe..fa87fbdb7897 100644
@@ -11,6 +11,8 @@ mips_FS2DT_INCLUDE = \
 	-include $(srcdir)/kexec/arch/mips/crashdump-mips.h \
 	-include $(srcdir)/kexec/arch/mips/kexec-mips.h
 
+mips_MEM_REGIONS = kexec/mem_regions.c
+
 mips_DT_OPS += kexec/dt-ops.c
 
 include $(srcdir)/kexec/libfdt/Makefile.libfdt

FTR, I believe this is the same mem_regions_alloc_and_add issue that I flagged in the thread [2].

And at the link below you can see a build failure with this patch, but not the fix above, applied:

https://github.com/horms/kexec-tools/actions/runs/14591381633/job/40927219243

#4 lore raw

Re: [PATCH 1/4] RISC-V: Add support for riscv kexec/kdump on kexec-tools

Quoted 6 lines · Björn Töpel

Simon!

Quoted 2 lines · Simon Horman

Thanks Björn,

Quoted 7 lines
Quoted 3 lines

Overall I am happy with this patchset. But there are a few minor points I'd like addressed, which relate strictly to build coverage and the CI. I can handle these myself if you prefer.

It's really up to you! What do you prefer? I can spin a v2 if that's easier for you (and then I'll try to address all the things you pointed out below)!

Hi Björn,

A v2 would be perfect for me.

If you are so inclined and you push the patches to a Github repo then the workflow should run. It is what picked up the minor problems I've highlighted.

But in any case, let's stick to the email based workflow for patch submissions (for now).