* [PATCH 0/4] Add crashdump support in guest kernel
@ 2026-03-24 11:45 fangyu.yu
2026-03-24 11:45 ` [PATCH 1/4] riscv: Add kexec trampoline text section to vmlinux.lds.S fangyu.yu
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: fangyu.yu @ 2026-03-24 11:45 UTC (permalink / raw)
To: pjw, palmer, aou, alex, songshuaishuai, bjorn, ardb, arnd,
bhelgaas, richard.lyu, tzimmermann, nathan
Cc: guoren, kvm-riscv, linux-riscv, linux-kernel, Fangyu Yu
From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
In a RISC-V kernel, crashdump needs to hand off execution to the crash
kernel after tearing down the current kernel address space. However,
under virtualization the guest uses two-stage address translation, PC
does not jump to stvec after setting satp to zero.
This patch set introduces a dedicated kexec trampoline text section and
builds a minimal trampoline page table for it. The crash handoff is then
reworked into a two-pass trampoline:
1. First enter via the kernel VA, install the trampoline page table,
and jump to the trampoline VA(=PA) of the norelocate code;
2. Continue execution with trampoline VA(=PA) and directly jump to the
crash kernel entry point with jr, instead of depending on a stvec
redirection.
With this, crashdump in RISC-V guests becomes robust against the
two-stage translation.
Fangyu Yu (4):
riscv: Add kexec trampoline text section to vmlinux.lds.S
riscv: kexec: Place norelocate trampoline into .kexec.tramp.text
riscv: kexec: Build trampoline page tables for crash kernel entry
riscv: kexec: Switch to trampoline page table before norelocate
arch/riscv/include/asm/kexec.h | 9 +++
arch/riscv/kernel/image-vars.h | 13 ++++
arch/riscv/kernel/kexec_relocate.S | 34 +++++++--
arch/riscv/kernel/machine_kexec.c | 116 ++++++++++++++++++++++++++++-
arch/riscv/kernel/vmlinux.lds.S | 1 +
5 files changed, 166 insertions(+), 7 deletions(-)
--
2.50.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/4] riscv: Add kexec trampoline text section to vmlinux.lds.S
2026-03-24 11:45 [PATCH 0/4] Add crashdump support in guest kernel fangyu.yu
@ 2026-03-24 11:45 ` fangyu.yu
2026-03-24 11:45 ` [PATCH 2/4] riscv: kexec: Place norelocate trampoline into .kexec.tramp.text fangyu.yu
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: fangyu.yu @ 2026-03-24 11:45 UTC (permalink / raw)
To: pjw, palmer, aou, alex, songshuaishuai, bjorn, ardb, arnd,
bhelgaas, richard.lyu, tzimmermann, nathan
Cc: guoren, kvm-riscv, linux-riscv, linux-kernel, Fangyu Yu
From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
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@linux.alibaba.com>
---
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
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/4] riscv: kexec: Place norelocate trampoline into .kexec.tramp.text
2026-03-24 11:45 [PATCH 0/4] Add crashdump support in guest kernel fangyu.yu
2026-03-24 11:45 ` [PATCH 1/4] riscv: Add kexec trampoline text section to vmlinux.lds.S fangyu.yu
@ 2026-03-24 11:45 ` fangyu.yu
2026-03-24 11:45 ` [PATCH 3/4] riscv: kexec: Build trampoline page tables for crash kernel entry fangyu.yu
2026-03-24 11:45 ` [PATCH 4/4] riscv: kexec: Switch to trampoline page table before norelocate fangyu.yu
3 siblings, 0 replies; 5+ messages in thread
From: fangyu.yu @ 2026-03-24 11:45 UTC (permalink / raw)
To: pjw, palmer, aou, alex, songshuaishuai, bjorn, ardb, arnd,
bhelgaas, richard.lyu, tzimmermann, nathan
Cc: guoren, kvm-riscv, linux-riscv, linux-kernel, Fangyu Yu
From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
Move riscv_kexec_norelocate out of the generic .text section and into
a dedicated executable trampoline section, .kexec.tramp.text.
Signed-off-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>
---
arch/riscv/include/asm/kexec.h | 9 +++++++++
arch/riscv/kernel/kexec_relocate.S | 2 +-
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/include/asm/kexec.h b/arch/riscv/include/asm/kexec.h
index b9ee8346cc8c..315dce0d9ca6 100644
--- a/arch/riscv/include/asm/kexec.h
+++ b/arch/riscv/include/asm/kexec.h
@@ -75,4 +75,13 @@ int load_extra_segments(struct kimage *image, unsigned long kernel_start,
unsigned long cmdline_len);
#endif
+#ifndef __ASSEMBLY__
+#ifdef CONFIG_MMU
+#define __kexec_tramp_text __section(".kexec.tramp.text")
+#else
+#define __kexec_tramp_text
+#endif
+#endif
+extern char __kexec_tramp_text_start[];
+
#endif
diff --git a/arch/riscv/kernel/kexec_relocate.S b/arch/riscv/kernel/kexec_relocate.S
index de0a4b35d01e..af6b99f5b0fd 100644
--- a/arch/riscv/kernel/kexec_relocate.S
+++ b/arch/riscv/kernel/kexec_relocate.S
@@ -147,7 +147,7 @@ riscv_kexec_relocate_end:
/* Used for jumping to crashkernel */
-.section ".text"
+.section ".kexec.tramp.text", "ax"
SYM_CODE_START(riscv_kexec_norelocate)
/*
* s0: (const) Phys address to jump to
--
2.50.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/4] riscv: kexec: Build trampoline page tables for crash kernel entry
2026-03-24 11:45 [PATCH 0/4] Add crashdump support in guest kernel fangyu.yu
2026-03-24 11:45 ` [PATCH 1/4] riscv: Add kexec trampoline text section to vmlinux.lds.S fangyu.yu
2026-03-24 11:45 ` [PATCH 2/4] riscv: kexec: Place norelocate trampoline into .kexec.tramp.text fangyu.yu
@ 2026-03-24 11:45 ` fangyu.yu
2026-03-24 11:45 ` [PATCH 4/4] riscv: kexec: Switch to trampoline page table before norelocate fangyu.yu
3 siblings, 0 replies; 5+ messages in thread
From: fangyu.yu @ 2026-03-24 11:45 UTC (permalink / raw)
To: pjw, palmer, aou, alex, songshuaishuai, bjorn, ardb, arnd,
bhelgaas, richard.lyu, tzimmermann, nathan
Cc: guoren, kvm-riscv, linux-riscv, linux-kernel, Fangyu Yu
From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
Crash kexec uses riscv_kexec_norelocate as a trampoline to jump into
the crashkernel. Add a small helper to build dedicated 4KB page tables
that map the trampoline page as executable.
Two mappings are installed:
- VA(__kexec_tramp_text_start) -> PA(__kexec_tramp_text_start)
- PA(__kexec_tramp_text_start) -> PA(__kexec_tramp_text_start)
This allows the trampoline to run regardless of whether it is entered
via its linked virtual address or its physical address.
Signed-off-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>
---
arch/riscv/kernel/machine_kexec.c | 103 +++++++++++++++++++++++++++++-
1 file changed, 102 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/kernel/machine_kexec.c b/arch/riscv/kernel/machine_kexec.c
index 2306ce3e5f22..4e522a64a614 100644
--- a/arch/riscv/kernel/machine_kexec.c
+++ b/arch/riscv/kernel/machine_kexec.c
@@ -18,6 +18,98 @@
#include <linux/interrupt.h>
#include <linux/irq.h>
+static pgd_t kexec_tramp_pgd[PTRS_PER_PGD] __aligned(PAGE_SIZE);
+static p4d_t kexec_tramp_p4d[PTRS_PER_P4D] __aligned(PAGE_SIZE);
+static pud_t kexec_tramp_pud[PTRS_PER_PUD] __aligned(PAGE_SIZE);
+static pmd_t kexec_tramp_pmd[PTRS_PER_PMD] __aligned(PAGE_SIZE);
+static pte_t kexec_tramp_pte[PTRS_PER_PTE] __aligned(PAGE_SIZE);
+static p4d_t kexec_tramp_p4d2[PTRS_PER_P4D] __aligned(PAGE_SIZE);
+static pud_t kexec_tramp_pud2[PTRS_PER_PUD] __aligned(PAGE_SIZE);
+static pmd_t kexec_tramp_pmd2[PTRS_PER_PMD] __aligned(PAGE_SIZE);
+static pte_t kexec_tramp_pte2[PTRS_PER_PTE] __aligned(PAGE_SIZE);
+
+static void riscv_kexec_build_tramp(unsigned long va, unsigned long pa)
+{
+ pgd_t *pgd;
+ pud_t *pud;
+ p4d_t *p4d;
+ pmd_t *pmd;
+ pte_t *pte;
+ int index;
+
+ index = pgd_index(va);
+ pgd = (pgd_t *)kexec_tramp_pgd + index;
+ if (pgtable_l5_enabled)
+ set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa_symbol(kexec_tramp_p4d)),
+ PAGE_TABLE));
+ else
+ set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa_symbol(kexec_tramp_pud)),
+ PAGE_TABLE));
+
+ if (pgtable_l5_enabled) {
+ index = p4d_index(va);
+ p4d = (p4d_t *)kexec_tramp_p4d + index;
+ if (pgtable_l4_enabled)
+ set_p4d(p4d, pfn_p4d(PFN_DOWN(__pa_symbol(kexec_tramp_pud)),
+ PAGE_TABLE));
+ else
+ set_p4d(p4d, pfn_p4d(PFN_DOWN(__pa_symbol(kexec_tramp_pmd)),
+ PAGE_TABLE));
+ }
+
+ if (pgtable_l4_enabled) {
+ index = pud_index(va);
+ pud = (pud_t *)kexec_tramp_pud + index;
+ set_pud(pud, pfn_pud(PFN_DOWN(__pa_symbol(kexec_tramp_pmd)), PAGE_TABLE));
+ }
+
+ index = pmd_index(va);
+ if (pgtable_l4_enabled)
+ pmd = (pmd_t *)kexec_tramp_pmd + index;
+ else
+ pmd = (pmd_t *)kexec_tramp_pud + index;
+ set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa_symbol(kexec_tramp_pte)), PAGE_TABLE));
+
+ index = pte_index(va);
+ pte = (pte_t *)kexec_tramp_pte + index;
+ set_pte(pte, pfn_pte(PFN_DOWN(pa), PAGE_KERNEL_EXEC));
+
+ index = pgd_index(pa);
+ pgd = (pgd_t *)kexec_tramp_pgd + index;
+ if (pgtable_l5_enabled)
+ set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa_symbol(kexec_tramp_p4d2)), PAGE_TABLE));
+ else
+ set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa_symbol(kexec_tramp_pud2)), PAGE_TABLE));
+
+ if (pgtable_l5_enabled) {
+ index = p4d_index(pa);
+ p4d = (p4d_t *)kexec_tramp_p4d2 + index;
+ if (pgtable_l4_enabled)
+ set_p4d(p4d, pfn_p4d(PFN_DOWN(__pa_symbol(kexec_tramp_pud2)),
+ PAGE_TABLE));
+ else
+ set_p4d(p4d, pfn_p4d(PFN_DOWN(__pa_symbol(kexec_tramp_pmd2)),
+ PAGE_TABLE));
+ }
+
+ if (pgtable_l4_enabled) {
+ index = pud_index(pa);
+ pud = (pud_t *)kexec_tramp_pud2 + index;
+ set_pud(pud, pfn_pud(PFN_DOWN(__pa_symbol(kexec_tramp_pmd2)), PAGE_TABLE));
+ }
+
+ index = pmd_index(pa);
+ if (pgtable_l4_enabled)
+ pmd = (pmd_t *)kexec_tramp_pmd2 + index;
+ else
+ pmd = (pmd_t *)kexec_tramp_pud2 + index;
+ set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa_symbol(kexec_tramp_pte2)), PAGE_TABLE));
+
+ index = pte_index(pa);
+ pte = (pte_t *)kexec_tramp_pte2 + index;
+ set_pte(pte, pfn_pte(PFN_DOWN(pa), PAGE_KERNEL_EXEC));
+}
+
/*
* machine_kexec_prepare - Initialize kexec
*
@@ -164,8 +256,17 @@ machine_kexec(struct kimage *image)
if (image->type != KEXEC_TYPE_CRASH)
kexec_method = control_code_buffer;
- else
+ else {
kexec_method = (riscv_kexec_method) &riscv_kexec_norelocate;
+ /*
+ * Build two 4KB identity-mapping page tables for the
+ * trampoline page:
+ * - VA(__kexec_tramp_text_start) -> PA(__kexec_tramp_text_start)
+ * - PA(__kexec_tramp_text_start) -> PA(__kexec_tramp_text_start)
+ */
+ riscv_kexec_build_tramp((unsigned long)__kexec_tramp_text_start,
+ __pa_symbol(__kexec_tramp_text_start));
+ }
pr_notice("Will call new kernel at %08lx from hart id %lx\n",
jump_addr, this_hart_id);
--
2.50.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 4/4] riscv: kexec: Switch to trampoline page table before norelocate
2026-03-24 11:45 [PATCH 0/4] Add crashdump support in guest kernel fangyu.yu
` (2 preceding siblings ...)
2026-03-24 11:45 ` [PATCH 3/4] riscv: kexec: Build trampoline page tables for crash kernel entry fangyu.yu
@ 2026-03-24 11:45 ` fangyu.yu
3 siblings, 0 replies; 5+ messages in thread
From: fangyu.yu @ 2026-03-24 11:45 UTC (permalink / raw)
To: pjw, palmer, aou, alex, songshuaishuai, bjorn, ardb, arnd,
bhelgaas, richard.lyu, tzimmermann, nathan
Cc: guoren, kvm-riscv, linux-riscv, linux-kernel, Fangyu Yu
From: Fangyu Yu <fangyu.yu@linux.alibaba.com>
Make riscv_kexec_norelocate a two-pass trampoline so it can
drop the kernel page tables while still executing from a
mapped address.
On the first entry, t3 is initialized to 0 by machine_kexec().
Loads the physical address of riscv_kexec_norelocate and the
trampoline SATP value, switches to the trampoline page table,
and jumps to the trampoline VA(=PA).
On the second entry, t3 contains the physical address of
riscv_kexec_norelocate, so the PC comparison matches and
execution continues under trampoline VA(=PA).
Since the trampoline page table is already active, replace the
previous stvec-based handoff with a direct jump to the target
entry (jr a2).
Signed-off-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>
---
arch/riscv/kernel/kexec_relocate.S | 32 +++++++++++++++++++++++++-----
arch/riscv/kernel/machine_kexec.c | 13 ++++++++++++
2 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/arch/riscv/kernel/kexec_relocate.S b/arch/riscv/kernel/kexec_relocate.S
index af6b99f5b0fd..2b9892bf04f2 100644
--- a/arch/riscv/kernel/kexec_relocate.S
+++ b/arch/riscv/kernel/kexec_relocate.S
@@ -147,13 +147,35 @@ riscv_kexec_relocate_end:
/* Used for jumping to crashkernel */
+.extern kexec_tramp_satp
+.extern riscv_kexec_norelocate_pa
.section ".kexec.tramp.text", "ax"
SYM_CODE_START(riscv_kexec_norelocate)
+ /*
+ * Two-pass entry:
+ * - 1st entry: t3 == 0 (initialized by machine_kexec()).
+ *
+ * - 2nd entry: t3 holds the physical address of
+ * riscv_kexec_norelocate, so auipc matches t3 and we fall through
+ * to label 1 to continue execution under trampoline VA(=PA).
+ */
+ auipc t0, 0
+ beq t0, t3, 1f
+
+ la t0, riscv_kexec_norelocate_pa
+ REG_L t3, 0(t0)
+ la t0, kexec_tramp_satp
+ REG_L t1, 0(t0)
+ csrw CSR_SATP, t1
+ sfence.vma x0, x0
+
+ jr t3
/*
* s0: (const) Phys address to jump to
* s1: (const) Phys address of the FDT image
* s2: (const) The hartid of the current hart
*/
+1:
mv s0, a1
mv s1, a2
mv s2, a3
@@ -199,13 +221,13 @@ SYM_CODE_START(riscv_kexec_norelocate)
csrw CSR_SSCRATCH, zero
/*
- * Switch to physical addressing
- * This will also trigger a jump to CSR_STVEC
- * which in this case is the address of the new
- * kernel.
+ * We are already executing from the trampoline VA with the trampoline
+ * page table installed, so there is no need to rely on the old flow
+ * of programming stvec and taking the implicit trap on SATP switch.
+ * Jump directly to the target entry instead.
*/
- csrw CSR_STVEC, a2
csrw CSR_SATP, zero
+ jr a2
SYM_CODE_END(riscv_kexec_norelocate)
diff --git a/arch/riscv/kernel/machine_kexec.c b/arch/riscv/kernel/machine_kexec.c
index 4e522a64a614..d78e7928c6cf 100644
--- a/arch/riscv/kernel/machine_kexec.c
+++ b/arch/riscv/kernel/machine_kexec.c
@@ -18,6 +18,8 @@
#include <linux/interrupt.h>
#include <linux/irq.h>
+unsigned long kexec_tramp_satp;
+unsigned long riscv_kexec_norelocate_pa;
static pgd_t kexec_tramp_pgd[PTRS_PER_PGD] __aligned(PAGE_SIZE);
static p4d_t kexec_tramp_p4d[PTRS_PER_P4D] __aligned(PAGE_SIZE);
static pud_t kexec_tramp_pud[PTRS_PER_PUD] __aligned(PAGE_SIZE);
@@ -266,6 +268,8 @@ machine_kexec(struct kimage *image)
*/
riscv_kexec_build_tramp((unsigned long)__kexec_tramp_text_start,
__pa_symbol(__kexec_tramp_text_start));
+ riscv_kexec_norelocate_pa = __pa_symbol(&riscv_kexec_norelocate);
+ kexec_tramp_satp = PFN_DOWN(__pa_symbol(kexec_tramp_pgd)) | satp_mode;
}
pr_notice("Will call new kernel at %08lx from hart id %lx\n",
@@ -277,6 +281,15 @@ machine_kexec(struct kimage *image)
/* Jump to the relocation code */
pr_notice("Bye...\n");
+ /*
+ * Initialize t3 to 0 for riscv_kexec_norelocate().
+ *
+ * The norelocate trampoline uses t3 as a scratch register to record/
+ * compare against the current PC when switching to the trampoline
+ * page table. Keep t3 untouched from here until we branch into
+ * riscv_kexec_norelocate.
+ */
+ asm volatile ("li t3, 0x0" ::: "t3");
kexec_method(first_ind_entry, jump_addr, fdt_addr,
this_hart_id, kernel_map.va_pa_offset);
unreachable();
--
2.50.1
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-03-24 11:47 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-24 11:45 [PATCH 0/4] Add crashdump support in guest kernel fangyu.yu
2026-03-24 11:45 ` [PATCH 1/4] riscv: Add kexec trampoline text section to vmlinux.lds.S fangyu.yu
2026-03-24 11:45 ` [PATCH 2/4] riscv: kexec: Place norelocate trampoline into .kexec.tramp.text fangyu.yu
2026-03-24 11:45 ` [PATCH 3/4] riscv: kexec: Build trampoline page tables for crash kernel entry fangyu.yu
2026-03-24 11:45 ` [PATCH 4/4] riscv: kexec: Switch to trampoline page table before norelocate fangyu.yu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox