From nobody Fri Apr 3 10:24:46 2026 Received: from out30-97.freemail.mail.aliyun.com (out30-97.freemail.mail.aliyun.com [115.124.30.97]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1FA8C3EFD20 for ; Tue, 24 Mar 2026 11:45:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.97 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774352741; cv=none; b=klwngVtdPQAvC9UqBLkZQsd6x1nwntLvKSwKzYTXwRIgYxT0T6KGW+vsOmL6vPB3TYYUqukRoBycCqHkWNGD+XEo/gMsoUSoA0F31SMJ2LHzsFQQyjABZQmWPgr9PrcBS7bSWzyT9aOx0CNdf5h3OzctJNTNnDaCjhX/ZXiznkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774352741; c=relaxed/simple; bh=gg3lQsmI4jyRIUGqNxvr5jKqkNCRyq80FcWp/8wmIoc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NOevGGDVWueWWTSMk2zVf/Z1k74uc1tbkoe5eBXrhSyEA0On0fC80ykeF+RdyjCWDZXbtSgawxB/ugCed2zozpQE552cKB9TGY5qsoF2oQsCaJNGNUIoeHL70cqKHpTZ5updernIXqUDwA+Zz9rSS7S3PddsTc2kHHYFlYUVdzs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=F7F+OOuJ; arc=none smtp.client-ip=115.124.30.97 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="F7F+OOuJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1774352736; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=E9l5eM2dvXocKxkWzJcB7vhB/kHEeAYIIZKXRijN8cA=; b=F7F+OOuJgrmDJvH8KkV25ijFAi8MXm1kFRLR3AgcYhHJmB79sob5Z+zHKlKXyxYXKtdF9hV7eeOvIu4VxFpcnBpnNYjpCYkb6vKdt9f7fkcnoWANMdazfJgO777Q2AKUqFQyqlB29pXMVoZcOoaEJBooZI3GymyS/7lQKljWKTc= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037026112;MF=fangyu.yu@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0X.eWVFP_1774352733; Received: from localhost.localdomain(mailfrom:fangyu.yu@linux.alibaba.com fp:SMTPD_---0X.eWVFP_1774352733 cluster:ay36) by smtp.aliyun-inc.com; Tue, 24 Mar 2026 19:45:34 +0800 From: fangyu.yu@linux.alibaba.com To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, songshuaishuai@tinylab.org, bjorn@rivosinc.com, ardb@kernel.org, arnd@arndb.de, bhelgaas@google.com, richard.lyu@suse.com, tzimmermann@suse.de, nathan@kernel.org Cc: guoren@kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Fangyu Yu Subject: [PATCH 1/4] riscv: Add kexec trampoline text section to vmlinux.lds.S Date: Tue, 24 Mar 2026 19:45:24 +0800 Message-Id: <20260324114527.91494-2-fangyu.yu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20260324114527.91494-1-fangyu.yu@linux.alibaba.com> References: <20260324114527.91494-1-fangyu.yu@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Fangyu Yu 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 --- 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 =3D sysfb_primary_displa= y; =20 #endif =20 +#ifdef CONFIG_KEXEC_CORE +#define KEXEC_TRAMP_TEXT \ + . =3D ALIGN(PAGE_SIZE); \ + __kexec_tramp_text_start =3D .; \ + KEEP(*(.kexec.tramp.text)) \ + KEEP(*(.kexec.tramp.text.*)) \ + __kexec_tramp_text_end =3D .; \ + ASSERT((__kexec_tramp_text_end - __kexec_tramp_text_start) <=3D PAGE_SIZ= E, \ + ".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.ld= s.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 =3D .; } =20 --=20 2.50.1 From nobody Fri Apr 3 10:24:46 2026 Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 658663F54C1 for ; Tue, 24 Mar 2026 11:45:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.101 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774352744; cv=none; b=CD2Bvi7bSEQznU1swf6Qr7lOSDbwOxQnk0lSebjgasnUdLWwDQL+rrZCnKAEhIKCW1iSN1W551am9wrINmBfF1KybQuAE1PdpSfn1i3Wdr+LPywZtpvFUQdt1vBnAed7xSruE9KQN4ZBvcQvjwLfmPUETP+P/d9qFuxsnDT9UJ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774352744; c=relaxed/simple; bh=iUkD25uL5C9JhoIWRlMib3XOU/cUwiMjYOoR6k1Addg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NKK8fIKgagr3hyF6vmz0F4tqCHvpy/EUV5GLlh9i05j7YHz8nUmwX5kxSYBG+idmRPF1+tVaVksLfkCZ6FbQzdhkJ0i91FNBkWlBqaHiXIQu256yS7815KZgGcBV6HFHDBfVrNRFz38GI4HQzWOpEEugc9J4ZVBQmsUfzLzSXj4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=Us6KYrPB; arc=none smtp.client-ip=115.124.30.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="Us6KYrPB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1774352737; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=eGoUX5+U64mMyKbsYD4ZIBWAtxP/BeYqHD20VtSl4vc=; b=Us6KYrPBmSQ1bEwWXQujO8gnjEFS5409MX4jswHDMWVtnnDuTo/bV3gnv8bkI1OVofqsdP4ShJDEvlrHjon9JOxGZP/XoDnShRGmV13630CyxtTtemx0Fh59FompiGcVFJgD5sy2UhteqqFGp1jIehqFUNuUxMFTc+Z6omL0uuY= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R781e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037033178;MF=fangyu.yu@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0X.eWVFc_1774352735; Received: from localhost.localdomain(mailfrom:fangyu.yu@linux.alibaba.com fp:SMTPD_---0X.eWVFc_1774352735 cluster:ay36) by smtp.aliyun-inc.com; Tue, 24 Mar 2026 19:45:35 +0800 From: fangyu.yu@linux.alibaba.com To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, songshuaishuai@tinylab.org, bjorn@rivosinc.com, ardb@kernel.org, arnd@arndb.de, bhelgaas@google.com, richard.lyu@suse.com, tzimmermann@suse.de, nathan@kernel.org Cc: guoren@kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Fangyu Yu Subject: [PATCH 2/4] riscv: kexec: Place norelocate trampoline into .kexec.tramp.text Date: Tue, 24 Mar 2026 19:45:25 +0800 Message-Id: <20260324114527.91494-3-fangyu.yu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20260324114527.91494-1-fangyu.yu@linux.alibaba.com> References: <20260324114527.91494-1-fangyu.yu@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Fangyu Yu 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 --- 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 l= ong kernel_start, unsigned long cmdline_len); #endif =20 +#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_r= elocate.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: =20 =20 /* 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 --=20 2.50.1 From nobody Fri Apr 3 10:24:46 2026 Received: from out30-118.freemail.mail.aliyun.com (out30-118.freemail.mail.aliyun.com [115.124.30.118]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B2113F65EB for ; Tue, 24 Mar 2026 11:45:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.118 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774352751; cv=none; b=eDR4Jdvh1m8Gc/JHkpPe6lDE8YX9WXxwDfIJYExtg2KmjDH3i6udI3NYWPUSla1pIlSo6XjGt3FAy6uYSOM9PZBbkzx1lLNpBJ998MjHMGjWjmL3Q6JbmVaugCaLjBcNOCnNvu+qvLTnw/Zhm1aYepk5uCQRMlB0OOq28CNW2cY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774352751; c=relaxed/simple; bh=wZRqnfDZn0H0D2XdXK1rrl/nAmou2a718Iss63kB8O0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ppqq6WfoRkrX6FwMqXbOzqCdW+GcAHbv1FHmrhUWg73DrEi/tM3q/S6ZfEGXEz0l+f1XbM854ddh/WIYPRUjsCb0r8epy7PaVCsbkkAM7cbHa3Uwo0OYQDcVT0c8wdK5BnHV7QRDQdqJMM2rrF/huaPxP6FyKpu+Zn8a+GSCSAo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=DSItUImd; arc=none smtp.client-ip=115.124.30.118 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="DSItUImd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1774352738; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=5u8vaxNzmtFsbfL7LvvEbSUs+WLXyb+uBK2uxrcg2Kc=; b=DSItUImd/GrwOVb+BVohtcBmdN2MZD+8kQJYVMnmd4xyV8iY9t0ZW43VQSAQIzV99s3l+tRh7++Vj7qAqjE+v03XmyU9Z2iRdxqf7o9vqmRVN9gmt/lFYzudPb0V5rx0chvZt0YaO/OdKeebSQze05V7lGBV7pGzyOSpaqyOZuQ= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037009110;MF=fangyu.yu@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0X.eWVFq_1774352736; Received: from localhost.localdomain(mailfrom:fangyu.yu@linux.alibaba.com fp:SMTPD_---0X.eWVFq_1774352736 cluster:ay36) by smtp.aliyun-inc.com; Tue, 24 Mar 2026 19:45:37 +0800 From: fangyu.yu@linux.alibaba.com To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, songshuaishuai@tinylab.org, bjorn@rivosinc.com, ardb@kernel.org, arnd@arndb.de, bhelgaas@google.com, richard.lyu@suse.com, tzimmermann@suse.de, nathan@kernel.org Cc: guoren@kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Fangyu Yu Subject: [PATCH 3/4] riscv: kexec: Build trampoline page tables for crash kernel entry Date: Tue, 24 Mar 2026 19:45:26 +0800 Message-Id: <20260324114527.91494-4-fangyu.yu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20260324114527.91494-1-fangyu.yu@linux.alibaba.com> References: <20260324114527.91494-1-fangyu.yu@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Fangyu Yu 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 --- 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 #include =20 +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 =3D pgd_index(va); + pgd =3D (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 =3D p4d_index(va); + p4d =3D (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 =3D pud_index(va); + pud =3D (pud_t *)kexec_tramp_pud + index; + set_pud(pud, pfn_pud(PFN_DOWN(__pa_symbol(kexec_tramp_pmd)), PAGE_TABLE)= ); + } + + index =3D pmd_index(va); + if (pgtable_l4_enabled) + pmd =3D (pmd_t *)kexec_tramp_pmd + index; + else + pmd =3D (pmd_t *)kexec_tramp_pud + index; + set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa_symbol(kexec_tramp_pte)), PAGE_TABLE)); + + index =3D pte_index(va); + pte =3D (pte_t *)kexec_tramp_pte + index; + set_pte(pte, pfn_pte(PFN_DOWN(pa), PAGE_KERNEL_EXEC)); + + index =3D pgd_index(pa); + pgd =3D (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 =3D p4d_index(pa); + p4d =3D (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 =3D pud_index(pa); + pud =3D (pud_t *)kexec_tramp_pud2 + index; + set_pud(pud, pfn_pud(PFN_DOWN(__pa_symbol(kexec_tramp_pmd2)), PAGE_TABLE= )); + } + + index =3D pmd_index(pa); + if (pgtable_l4_enabled) + pmd =3D (pmd_t *)kexec_tramp_pmd2 + index; + else + pmd =3D (pmd_t *)kexec_tramp_pud2 + index; + set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa_symbol(kexec_tramp_pte2)), PAGE_TABLE)= ); + + index =3D pte_index(pa); + pte =3D (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) =20 if (image->type !=3D KEXEC_TYPE_CRASH) kexec_method =3D control_code_buffer; - else + else { kexec_method =3D (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)); + } =20 pr_notice("Will call new kernel at %08lx from hart id %lx\n", jump_addr, this_hart_id); --=20 2.50.1 From nobody Fri Apr 3 10:24:46 2026 Received: from out30-132.freemail.mail.aliyun.com (out30-132.freemail.mail.aliyun.com [115.124.30.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1093E3F65EC for ; Tue, 24 Mar 2026 11:45:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.132 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774352743; cv=none; b=Oad5yt3sLJYKogOMr4RstRg2GvmJwuCdM3XNdJpYp/1BW09DYJvb3mhdWMJCZ8G7AUbax+ndxJSowV47KK8cI+RIFjQe9zTdEutflZG+yFjOFjXDl3Ah1pHDKjIer2WkGasGOvedmNEvvMQT7EU8lLu20S/t/gXx4cSb5S256ck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774352743; c=relaxed/simple; bh=13wwAn/56zr31sHDdtFxthT3PZU59x99sW5T4jYrFAg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QtwZD5UzNKHs7PUjyeY9lU64UUcagHaoFF3TLnayQXHl4UrFJg+vm6DS0s1JjEAjdey2E9aBLXGHFeDvbn7JRszPWJrC7u6/y1QrAQJ5kQrCG9SS/IVzR3PnZ/5iIhkIyfJoyoDhEIk7RNKjrw0QAJd+5qWNlNH8Sy5WFknV/kM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=YKzP2YXG; arc=none smtp.client-ip=115.124.30.132 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="YKzP2YXG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1774352740; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=jPF1Ju7Ns0lO+Yge9p5ER6y+YNSyxbvGG2vjrwoYHYQ=; b=YKzP2YXGuIFD4OAbbLCqlrCF4pIsnrXXJrztMSq9f1KP5wnQ40m5wvvJyQBNlGwwSVFe0rD1APyhsrA5gu/Bz6YeHVBl3KP7uGdUSY/6bVn8epbSaEfkui41jJa0inQ6FUUumMPl1iZAr4xHgcaVKplNW8ERmzwvVuhuCXZ/1no= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033032089153;MF=fangyu.yu@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0X.eWVGD_1774352737; Received: from localhost.localdomain(mailfrom:fangyu.yu@linux.alibaba.com fp:SMTPD_---0X.eWVGD_1774352737 cluster:ay36) by smtp.aliyun-inc.com; Tue, 24 Mar 2026 19:45:38 +0800 From: fangyu.yu@linux.alibaba.com To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, songshuaishuai@tinylab.org, bjorn@rivosinc.com, ardb@kernel.org, arnd@arndb.de, bhelgaas@google.com, richard.lyu@suse.com, tzimmermann@suse.de, nathan@kernel.org Cc: guoren@kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Fangyu Yu Subject: [PATCH 4/4] riscv: kexec: Switch to trampoline page table before norelocate Date: Tue, 24 Mar 2026 19:45:27 +0800 Message-Id: <20260324114527.91494-5-fangyu.yu@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20260324114527.91494-1-fangyu.yu@linux.alibaba.com> References: <20260324114527.91494-1-fangyu.yu@linux.alibaba.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Fangyu Yu 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(=3DPA). On the second entry, t3 contains the physical address of riscv_kexec_norelocate, so the PC comparison matches and execution continues under trampoline VA(=3DPA). 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 --- 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_r= elocate.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: =20 =20 /* 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 =3D=3D 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(=3DPA). + */ + 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 =20 /* - * 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 =20 SYM_CODE_END(riscv_kexec_norelocate) =20 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 #include =20 +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 =3D __pa_symbol(&riscv_kexec_norelocate); + kexec_tramp_satp =3D PFN_DOWN(__pa_symbol(kexec_tramp_pgd)) | satp_mode; } =20 pr_notice("Will call new kernel at %08lx from hart id %lx\n", @@ -277,6 +281,15 @@ machine_kexec(struct kimage *image) =20 /* 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(); --=20 2.50.1