From: fangyu.yu@linux.alibaba.com
To: Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Alexandre Ghiti <alex@ghiti.fr>, Anup Patel <anup@brainfault.org>,
Atish Patra <atishp@atishpatra.org>,
Nick Kossifidis <mick@ics.forth.gr>
Cc: "Song Shuai" <songshuaishuai@tinylab.org>,
"Björn Töpel" <bjorn@rivosinc.com>,
"Ard Biesheuvel" <ardb@kernel.org>,
"Conor Dooley" <conor.dooley@microchip.com>,
"Arnd Bergmann" <arnd@arndb.de>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"Richard Lyu" <richard.lyu@suse.com>,
"Nam Cao" <namcao@linutronix.de>,
"Jisheng Zhang" <jszhang@kernel.org>,
"Nathan Chancellor" <nathan@kernel.org>,
guoren@kernel.org, linux-riscv@lists.infradead.org,
linux-kernel@vger.kernel.org, kexec@lists.infradead.org,
kvm-riscv@lists.infradead.org, kvm@vger.kernel.org,
"Fangyu Yu" <fangyu.yu@linux.alibaba.com>
Subject: [PATCH v2 3/7] riscv: kexec: Build trampoline page tables for crash kernel entry
Date: Tue, 26 May 2026 20:50:05 +0800 [thread overview]
Message-ID: <20260526125009.2404-4-fangyu.yu@linux.alibaba.com> (raw)
In-Reply-To: <20260526125009.2404-1-fangyu.yu@linux.alibaba.com>
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 | 67 +++++++++++++++++++++++++++++++
1 file changed, 67 insertions(+)
diff --git a/arch/riscv/kernel/machine_kexec.c b/arch/riscv/kernel/machine_kexec.c
index 2306ce3e5f22..9b4b495e7c15 100644
--- a/arch/riscv/kernel/machine_kexec.c
+++ b/arch/riscv/kernel/machine_kexec.c
@@ -18,6 +18,65 @@
#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 map_tramp_page(p4d_t *p4ds, pud_t *puds, pmd_t *pmds, pte_t *ptes,
+ unsigned long va, unsigned long pa)
+{
+ pgd_t *pgd = (pgd_t *)kexec_tramp_pgd + pgd_index(va);
+ pmd_t *pmd;
+
+ if (pgtable_l5_enabled) {
+ p4d_t *p4d;
+
+ set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa_symbol(p4ds)), PAGE_TABLE));
+ p4d = (p4d_t *)p4ds + p4d_index(va);
+ if (pgtable_l4_enabled)
+ set_p4d(p4d, pfn_p4d(PFN_DOWN(__pa_symbol(puds)),
+ PAGE_TABLE));
+ else
+ set_p4d(p4d, pfn_p4d(PFN_DOWN(__pa_symbol(pmds)),
+ PAGE_TABLE));
+ } else {
+ set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa_symbol(puds)), PAGE_TABLE));
+ }
+
+ if (pgtable_l4_enabled) {
+ pud_t *pud = (pud_t *)puds + pud_index(va);
+
+ set_pud(pud, pfn_pud(PFN_DOWN(__pa_symbol(pmds)), PAGE_TABLE));
+ pmd = (pmd_t *)pmds + pmd_index(va);
+ } else {
+ pmd = (pmd_t *)puds + pmd_index(va);
+ }
+ set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa_symbol(ptes)), PAGE_TABLE));
+
+ set_pte((pte_t *)ptes + pte_index(va),
+ pfn_pte(PFN_DOWN(pa), PAGE_KERNEL_EXEC));
+}
+
+static void riscv_kexec_build_tramp(unsigned long va, unsigned long pa)
+{
+ /* VA -> PA: map the trampoline page via its kernel VA. */
+ map_tramp_page(kexec_tramp_p4d, kexec_tramp_pud,
+ kexec_tramp_pmd, kexec_tramp_pte, va, pa);
+
+ /*
+ * PA -> PA: identity-map the same page so the second-pass code
+ * can keep executing after the kernel VA mapping is dropped.
+ */
+ map_tramp_page(kexec_tramp_p4d2, kexec_tramp_pud2,
+ kexec_tramp_pmd2, kexec_tramp_pte2, pa, pa);
+}
+
/*
* machine_kexec_prepare - Initialize kexec
*
@@ -73,6 +132,14 @@ machine_kexec_prepare(struct kimage *image)
/* Mark the control page executable */
set_memory_x((unsigned long) control_code_buffer, 1);
+ } else {
+ /*
+ * Crash kexec uses riscv_kexec_norelocate as a trampoline.
+ * Pre-build the trampoline page tables here so the panic
+ * path only has to switch satp and jump.
+ */
+ riscv_kexec_build_tramp((unsigned long)__kexec_tramp_text_start,
+ __pa_symbol(__kexec_tramp_text_start));
}
return 0;
--
2.50.1
next prev parent reply other threads:[~2026-05-26 12:50 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-26 12:50 [PATCH v2 0/7] riscv: kexec: Fix VCPU crash on kexec/kdump under KVM fangyu.yu
2026-05-26 12:50 ` [PATCH v2 1/7] riscv: Add kexec trampoline text section to vmlinux.lds.S fangyu.yu
2026-05-26 12:50 ` [PATCH v2 2/7] riscv: kexec: Place norelocate trampoline into .kexec.tramp.text fangyu.yu
2026-05-26 12:50 ` fangyu.yu [this message]
2026-05-26 12:50 ` [PATCH v2 4/7] riscv: kexec: Switch to trampoline page table before norelocate fangyu.yu
2026-05-26 12:50 ` [PATCH v2 5/7] riscv: kexec: Always build the trampoline page table fangyu.yu
2026-05-26 12:50 ` [PATCH v2 6/7] riscv: kexec: Add the relocate-trampoline wrapper fangyu.yu
2026-05-26 12:50 ` [PATCH v2 7/7] riscv: kexec: Route normal kexec through the trampoline page table fangyu.yu
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=20260526125009.2404-4-fangyu.yu@linux.alibaba.com \
--to=fangyu.yu@linux.alibaba.com \
--cc=alex@ghiti.fr \
--cc=anup@brainfault.org \
--cc=aou@eecs.berkeley.edu \
--cc=ardb@kernel.org \
--cc=arnd@arndb.de \
--cc=atishp@atishpatra.org \
--cc=bjorn@rivosinc.com \
--cc=conor.dooley@microchip.com \
--cc=guoren@kernel.org \
--cc=jszhang@kernel.org \
--cc=kexec@lists.infradead.org \
--cc=kvm-riscv@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=mick@ics.forth.gr \
--cc=namcao@linutronix.de \
--cc=nathan@kernel.org \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=richard.lyu@suse.com \
--cc=songshuaishuai@tinylab.org \
--cc=tzimmermann@suse.de \
/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
all inboxes | Powered by JetHome®