[PATCH 1/4] riscv: Add kexec trampoline text section to vmlinux.lds.S

From: fangyu . yu

Date: Tue Mar 24 2026 - 07:57:59 EST


From: Fangyu Yu <fangyu.yu@xxxxxxxxxxxxxxxxx>

When CONFIG_KEXEC_CORE is enabled, add a dedicated .kexec.tramp.text
area to the RISC-V kernel linker script.

This introduces a KEXEC_TRAMP_TEXT linker snippet in image-vars.h and
uses it from vmlinux.lds.S to:
- align to PAGE_SIZE
- define __kexec_tramp_text_start/__kexec_tramp_text_end
- KEEP all .kexec.tramp.text* input sections
- ASSERT the trampoline text fits within one page

When kexec is disabled, KEXEC_TRAMP_TEXT expands to nothing.

Signed-off-by: Fangyu Yu <fangyu.yu@xxxxxxxxxxxxxxxxx>
---
arch/riscv/kernel/image-vars.h | 13 +++++++++++++
arch/riscv/kernel/vmlinux.lds.S | 1 +
2 files changed, 14 insertions(+)

diff --git a/arch/riscv/kernel/image-vars.h b/arch/riscv/kernel/image-vars.h
index 3bd9d06a8b8f..5419609ff89c 100644
--- a/arch/riscv/kernel/image-vars.h
+++ b/arch/riscv/kernel/image-vars.h
@@ -34,4 +34,17 @@ __efistub_sysfb_primary_display = sysfb_primary_display;

#endif

+#ifdef CONFIG_KEXEC_CORE
+#define KEXEC_TRAMP_TEXT \
+ . = ALIGN(PAGE_SIZE); \
+ __kexec_tramp_text_start = .; \
+ KEEP(*(.kexec.tramp.text)) \
+ KEEP(*(.kexec.tramp.text.*)) \
+ __kexec_tramp_text_end = .; \
+ ASSERT((__kexec_tramp_text_end - __kexec_tramp_text_start) <= PAGE_SIZE, \
+ ".kexec.tramp.text exceeds 4K");
+#else
+#define KEXEC_TRAMP_TEXT /* nothing */
+#endif
+
#endif /* __RISCV_KERNEL_IMAGE_VARS_H */
diff --git a/arch/riscv/kernel/vmlinux.lds.S b/arch/riscv/kernel/vmlinux.lds.S
index 997f9eb3b22b..c55316912c1d 100644
--- a/arch/riscv/kernel/vmlinux.lds.S
+++ b/arch/riscv/kernel/vmlinux.lds.S
@@ -45,6 +45,7 @@ SECTIONS
ENTRY_TEXT
IRQENTRY_TEXT
SOFTIRQENTRY_TEXT
+ KEXEC_TRAMP_TEXT
_etext = .;
}

--
2.50.1