Backport riscv64 support

This commit is contained in:
Andrew Lukoshko 2025-07-18 11:14:11 +00:00
parent 9ca16ff1e9
commit 3010f4b640
5 changed files with 2345 additions and 1 deletions

View File

@ -0,0 +1,129 @@
From 027e6e237427b00b986d9c51fe65944b8fdcc85b Mon Sep 17 00:00:00 2001
From: Li Zhengyu <lizhengyu3@huawei.com>
Date: Tue, 22 Apr 2025 18:22:58 +0200
Subject: [PATCH] RISC-V: Enable kexec_file_load syscall
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/20220408100914.150110-1-lizhengyu3@huawei.com/
Signed-off-by: Song Shuai <songshuaishuai@tinylab.org>
Signed-off-by: Simon Horman <horms@kernel.org>
---
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(-)
diff --git a/kexec/arch/riscv/kexec-elf-riscv.c b/kexec/arch/riscv/kexec-elf-riscv.c
index f3c011c4..2b9f66d7 100644
--- a/kexec/arch/riscv/kexec-elf-riscv.c
+++ b/kexec/arch/riscv/kexec-elf-riscv.c
@@ -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 */
diff --git a/kexec/arch/riscv/kexec-riscv.c b/kexec/arch/riscv/kexec-riscv.c
index 38d9a394..bbc25c5c 100644
--- a/kexec/arch/riscv/kexec-riscv.c
+++ b/kexec/arch/riscv/kexec-riscv.c
@@ -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[] = {
diff --git a/kexec/arch/riscv/kexec-riscv.h b/kexec/arch/riscv/kexec-riscv.h
index c4323a65..f136c7ea 100644
--- a/kexec/arch/riscv/kexec-riscv.h
+++ b/kexec/arch/riscv/kexec-riscv.h
@@ -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);
diff --git a/kexec/kexec-syscall.h b/kexec/kexec-syscall.h
index 89591ad1..9b175787 100644
--- a/kexec/kexec-syscall.h
+++ b/kexec/kexec-syscall.h
@@ -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 */

View File

@ -0,0 +1,205 @@
From 363087cd156f870dfd07fef04077696daa77acaa Mon Sep 17 00:00:00 2001
From: Song Shuai <songshuaishuai@tinylab.org>
Date: Tue, 22 Apr 2025 18:22:59 +0200
Subject: [PATCH] RISC-V: Separate elf_riscv_find_pbase out
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.
Signed-off-by: Song Shuai <songshuaishuai@tinylab.org>
Signed-off-by: Simon Horman <horms@kernel.org>
---
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(-)
diff --git a/kexec/arch/riscv/kexec-elf-riscv.c b/kexec/arch/riscv/kexec-elf-riscv.c
index 2b9f66d7..434873cf 100644
--- a/kexec/arch/riscv/kexec-elf-riscv.c
+++ b/kexec/arch/riscv/kexec-elf-riscv.c
@@ -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");
diff --git a/kexec/arch/riscv/kexec-riscv.c b/kexec/arch/riscv/kexec-riscv.c
index bbc25c5c..63165930 100644
--- a/kexec/arch/riscv/kexec-riscv.c
+++ b/kexec/arch/riscv/kexec-riscv.c
@@ -50,6 +50,61 @@ static struct fdt_image provided_fdt = {0};
* COMMON HELPERS *
\****************/
+/*
+ * Go through the available physical memory regions and
+ * find one that can hold an image of the specified size
+ * and start address should be aligned up with `align`.
+ * 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.
+ */
+int riscv_find_pbase(struct kexec_info *info, off_t *addr,
+ off_t size, off_t align)
+{
+ 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) {
+ start = _ALIGN_UP(start, align);
+
+ 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;
+
+ start = _ALIGN_UP(start, align);
+
+ if (end > start && ((end - start) >= size)) {
+ *addr = start;
+ return 0;
+ }
+ }
+
+ return -EFBIG;
+}
+
int load_extra_segments(struct kexec_info *info, uint64_t kernel_base,
uint64_t kernel_size, uint64_t max_addr)
{
diff --git a/kexec/arch/riscv/kexec-riscv.h b/kexec/arch/riscv/kexec-riscv.h
index f136c7ea..61809929 100644
--- a/kexec/arch/riscv/kexec-riscv.h
+++ b/kexec/arch/riscv/kexec-riscv.h
@@ -4,6 +4,17 @@
* Nick Kossifidis <mick@ics.forth.gr>
*/
+/*
+ * 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);

View File

@ -0,0 +1,271 @@
From b257f311f5f0d32969f58098b03458b5bd728419 Mon Sep 17 00:00:00 2001
From: Song Shuai <songshuaishuai@tinylab.org>
Date: Tue, 22 Apr 2025 18:23:00 +0200
Subject: [PATCH] RISC-V: Support loading Image binary file
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.
Signed-off-by: Song Shuai <songshuaishuai@tinylab.org>
Signed-off-by: Simon Horman <horms@kernel.org>
---
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
diff --git a/kexec/arch/riscv/Makefile b/kexec/arch/riscv/Makefile
index 9937fbb5..18a997b0 100644
--- a/kexec/arch/riscv/Makefile
+++ b/kexec/arch/riscv/Makefile
@@ -3,6 +3,7 @@
#
riscv_KEXEC_SRCS = kexec/arch/riscv/crashdump-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/kexec-riscv.c
riscv_DT_OPS += kexec/dt-ops.c
@@ -14,6 +15,7 @@ riscv_ARCH_REUSE_INITRD =
riscv_CPPFLAGS += -I $(srcdir)/kexec/
dist += $(riscv_KEXEC_SRCS) \
+ kexec/arch/riscv/image-header.h \
kexec/arch/riscv/include/arch/options.h \
kexec/arch/riscv/iomem.h \
kexec/arch/riscv/kexec-riscv.h \
diff --git a/kexec/arch/riscv/image-header.h b/kexec/arch/riscv/image-header.h
new file mode 100644
index 00000000..a6775462
--- /dev/null
+++ b/kexec/arch/riscv/image-header.h
@@ -0,0 +1,88 @@
+/*
+ * RISCV64 binary image header.
+ * token from arm64/image-header.h
+ */
+
+#if !defined(__RISCV_IMAGE_HEADER_H)
+#define __RISCV_IMAGE_HEADER_H
+
+#include <endian.h>
+#include <stdint.h>
+
+/**
+ * struct riscv_image_header - riscv kernel image header.
+ *
+ **/
+struct riscv_image_header {
+ uint32_t code0;
+ uint32_t code1;
+ uint64_t text_offset;
+ uint64_t image_size;
+ uint64_t flags;
+ uint32_t version;
+ uint32_t res1;
+ uint64_t res2;
+ uint64_t magic;
+ uint32_t magic2;
+ uint32_t res3;
+};
+
+#define RISCV_IMAGE_MAGIC 0x5643534952
+#define RISCV_IMAGE_MAGIC2 0x05435352
+
+#define RISCV_HEADER_VERSION_MAJOR 0
+#define RISCV_HEADER_VERSION_MINOR 2
+
+#define RISCV_HEADER_VERSION (RISCV_HEADER_VERSION_MAJOR << 16 | \
+ RISCV_HEADER_VERSION_MINOR)
+
+
+static const uint64_t riscv_image_flag_be = (1UL << 0);
+
+/**
+ * riscv_header_check_magic - Helper to check the riscv image header.
+ *
+ * Returns non-zero if header is OK.
+ */
+
+static inline int riscv_header_check_magic(const struct riscv_image_header *h)
+{
+ if (!h)
+ return 0;
+
+ return (h->version >= RISCV_HEADER_VERSION && h->magic2 == RISCV_IMAGE_MAGIC2);
+}
+
+/**
+ * riscv_header_check_endiannes - Helper to check the riscv image header.
+ *
+ * Returns non-zero if the image was built as big endian.
+ */
+
+static inline int riscv_header_check_endiannes(const struct riscv_image_header *h)
+{
+ if (!h)
+ return 0;
+
+ return (le64toh(h->flags) & riscv_image_flag_be) >> 0;
+}
+
+
+
+static inline uint64_t riscv_header_text_offset(const struct riscv_image_header *h)
+{
+ if (!h)
+ return 0;
+
+ return le64toh(h->text_offset);
+}
+
+static inline uint64_t riscv_header_image_size(const struct riscv_image_header *h)
+{
+ if (!h)
+ return 0;
+
+ return le64toh(h->image_size);
+}
+
+#endif
diff --git a/kexec/arch/riscv/kexec-image-riscv.c b/kexec/arch/riscv/kexec-image-riscv.c
new file mode 100644
index 00000000..6ae7e579
--- /dev/null
+++ b/kexec/arch/riscv/kexec-image-riscv.c
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * RISC-V kexec binary image support.
+ *
+ * Author: Song Shuai <songhshuaishuai@tinylab.org>
+ */
+
+#define _GNU_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include "image-header.h"
+#include "kexec.h"
+#include "kexec-riscv.h"
+#include "kexec-syscall.h"
+#include "arch/options.h"
+
+int image_riscv_probe(const char *kernel_buf, off_t kernel_size)
+{
+ const struct riscv_image_header *h;
+
+ if (kernel_size < sizeof(struct riscv_image_header)) {
+ dbgprintf("%s: No riscv image header.\n", __func__);
+ return -1;
+ }
+
+ h = (const struct riscv_image_header *)(kernel_buf);
+
+ if (!riscv_header_check_magic(h)) {
+ dbgprintf("%s: Bad riscv image header.\n", __func__);
+ return -1;
+ }
+
+ return 0;
+}
+
+int image_riscv_load(int argc, char **argv, const char *kernel_buf,
+ off_t kernel_size, struct kexec_info *info)
+{
+ const struct riscv_image_header *h;
+ unsigned long text_offset, image_size;
+ off_t new_base_addr = 0;
+
+ int ret;
+
+ if (info->file_mode) {
+ return prepare_kexec_file_options(info);
+ }
+
+ h = (const struct riscv_image_header *)(kernel_buf);
+
+ /* Check header */
+ if (!h->image_size){
+ dbgprintf("Kernel image size is NULL\n");
+ ret = EFAILED;
+ goto exit;
+ }
+
+ if(riscv_header_check_endiannes(h)){
+ dbgprintf("Kernel image was built as big endian\n");
+ ret = EFAILED;
+ goto exit;
+ }
+
+ text_offset = riscv_header_text_offset(h);
+ image_size = riscv_header_image_size(h);
+
+ /* Setup the entry and segments */
+
+ ret = riscv_find_pbase(info, &new_base_addr, image_size, text_offset);
+ if (ret < 0) {
+ fprintf(stderr, "Could not find a memory region for the "
+ "provided Image\n");
+ goto exit;
+ }
+
+ info->entry = (void *) new_base_addr;
+ dbgprintf("Entry point for the Image: 0x%lX\n", new_base_addr);
+
+ add_segment(info, kernel_buf, kernel_size, new_base_addr, image_size);
+
+ ret = load_extra_segments(info, text_offset, image_size, ULONG_MAX);
+exit:
+ if (ret)
+ fprintf(stderr, "kexec: load failed.\n");
+ return ret;
+}
+
+void image_riscv_usage(void)
+{
+ printf(
+" An RISC-V binary image, uncompressed, little endian.\n"
+" Typically an Image file.\n\n");
+}
diff --git a/kexec/arch/riscv/kexec-riscv.c b/kexec/arch/riscv/kexec-riscv.c
index 63165930..f34b4683 100644
--- a/kexec/arch/riscv/kexec-riscv.c
+++ b/kexec/arch/riscv/kexec-riscv.c
@@ -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]);
diff --git a/kexec/arch/riscv/kexec-riscv.h b/kexec/arch/riscv/kexec-riscv.h
index 61809929..cfb03779 100644
--- a/kexec/arch/riscv/kexec-riscv.h
+++ b/kexec/arch/riscv/kexec-riscv.h
@@ -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);

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
Name: kexec-tools
Version: 2.0.31
Release: 1%{?dist}
Release: 1%{?dist}.alma.1
URL: https://kernel.org/pub/linux/utils/kernel/kexec
License: GPL-2.0-only
Summary: The kexec/kdump userspace component
@ -41,6 +41,15 @@ BuildRequires: zlib-devel
# Patches 601 onward are generic patches
#
# Backport RISC-V (riscv64) support
# Initially done by David Abdurachmanov <davidlt@rivosinc.com>
# These patches will be part of the next release (v2.0.32)
# Merged upstream
Patch1001: b3fd54b022fe5ba09ebff184d4d22466672946cf.patch
Patch1002: 027e6e237427b00b986d9c51fe65944b8fdcc85b.patch
Patch1003: 363087cd156f870dfd07fef04077696daa77acaa.patch
Patch1004: b257f311f5f0d32969f58098b03458b5bd728419.patch
%description
kexec-tools provides /sbin/kexec binary that facilitates a new
kernel to boot using the kernel's kexec feature either on a
@ -51,6 +60,11 @@ component of the kernel's kexec feature.
%prep
%setup -q
%patch1001 -p1
%patch1002 -p1
%patch1003 -p1
%patch1004 -p1
mkdir -p -m755 kcp
%ifarch ppc
@ -87,6 +101,9 @@ rm -f %{buildroot}/%{_libdir}/kexec-tools/kexec_test
%doc TODO
%changelog
* Fri Jul 18 2025 Andrew Lukoshko <alukoshko@almalinux.org> - 2.0.31-1.alma.1
- Backport riscv64 support
* Thu Apr 24 2025 Lichen Liu <lichliu@redhat.com> - 2.0.31-1
- Update to 2.0.31
- Resolves: RHEL-86467