public inbox for linux-riscv@lists.infradead.org
 help / color / mirror / Atom feed
From: "Björn Töpel" <bjorn@kernel.org>
To: Simon Horman <horms@kernel.org>,
	Simon Horman <horms@verge.net.au>,
	Nick Kossifidis <mick@ics.forth.gr>,
	Song Shuai <songshuaishuai@tinylab.org>,
	Li Zhengyu <lizhengyu3@huawei.com>,
	kexec@lists.infradead.org
Cc: Dave Young <dyoung@redhat.com>, Yixun Lan <yixun.lan@gmail.com>,
	Xianting Tian <xianting.tian@linux.alibaba.com>,
	linux-riscv@lists.infradead.org
Subject: [PATCH 3/4] RISC-V: Separate elf_riscv_find_pbase out
Date: Wed,  9 Apr 2025 22:14:25 +0200	[thread overview]
Message-ID: <20250409201428.648717-4-bjorn@kernel.org> (raw)
In-Reply-To: <20250409201428.648717-1-bjorn@kernel.org>

From: Song Shuai <songshuaishuai@tinylab.org>

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>
---
 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 2b9f66d782af..434873cf9f08 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 bbc25c5cba41..631659301749 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 f136c7eab561..618099291117 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);
-- 
2.45.2


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  parent reply	other threads:[~2025-04-09 20:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-09 20:14 [PATCH 0/4] kexec-tools RISC-V port Björn Töpel
2025-04-09 20:14 ` [PATCH 1/4] RISC-V: Add support for riscv kexec/kdump on kexec-tools Björn Töpel
2025-04-22 10:48   ` Simon Horman
2025-04-22 12:07     ` Björn Töpel
2025-04-22 13:54       ` Simon Horman
2025-04-09 20:14 ` [PATCH 2/4] RISC-V: Enable kexec_file_load syscall Björn Töpel
2025-04-09 20:14 ` Björn Töpel [this message]
2025-04-09 20:14 ` [PATCH 4/4] RISC-V: Support loading Image binary file Björn Töpel
2025-04-14  8:40 ` [PATCH 0/4] kexec-tools RISC-V port Simon Horman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250409201428.648717-4-bjorn@kernel.org \
    --to=bjorn@kernel.org \
    --cc=dyoung@redhat.com \
    --cc=horms@kernel.org \
    --cc=horms@verge.net.au \
    --cc=kexec@lists.infradead.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=lizhengyu3@huawei.com \
    --cc=mick@ics.forth.gr \
    --cc=songshuaishuai@tinylab.org \
    --cc=xianting.tian@linux.alibaba.com \
    --cc=yixun.lan@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox