All of lore.kernel.org
 help / color / mirror / Atom feed
* [kvm-unit-tests PATCH] riscv: Report exceptions taken before thread_info_init()
@ 2026-11-20 14:40 Andrew Jones
  0 siblings, 0 replies; only message in thread
From: Andrew Jones @ 2026-11-20 14:40 UTC (permalink / raw)
  To: kvm-riscv

Until thread_info_init() runs on a hart, sscratch holds the hartid
rather than a pointer to the hart's thread_info. An exception taken
before then makes do_handle_exception() use the hartid as a pointer,
which faults again, so the hart loops taking exceptions forever
without printing anything. Only use sscratch as a thread_info pointer
when it points into cpus[]. Otherwise, report the exception and abort.

Signed-off-by: Andrew Jones <andrew.jones@linux.dev>
---
 lib/riscv/processor.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/lib/riscv/processor.c b/lib/riscv/processor.c
index b055acc94e41..aa1ac417f1e9 100644
--- a/lib/riscv/processor.c
+++ b/lib/riscv/processor.c
@@ -12,6 +12,17 @@
 
 extern unsigned long ImageBase;
 
+/*
+ * sscratch holds the hartid until thread_info_init() replaces it with
+ * a pointer to the hart's thread_info.
+ */
+static bool thread_info_valid(struct thread_info *info)
+{
+	uintptr_t p = (uintptr_t)info;
+
+	return p >= (uintptr_t)&cpus[0] && p < (uintptr_t)&cpus[NR_CPUS];
+}
+
 void show_regs(struct pt_regs *regs)
 {
 	struct thread_info *info = current_thread_info();
@@ -19,7 +30,10 @@ void show_regs(struct pt_regs *regs)
 	unsigned int w = __riscv_xlen / 4;
 
 	printf("Load address: %" PRIxPTR "\n", loadaddr);
-	printf("CPU%3d : hartid=%lx\n", info->cpu, info->hartid);
+	if (thread_info_valid(info))
+		printf("CPU%3d : hartid=%lx\n", info->cpu, info->hartid);
+	else
+		printf("CPU  ? : hartid=%lx\n", (unsigned long)info);
 	printf("status : %.*lx\n", w, regs->status);
 	printf("cause  : %.*lx\n", w, regs->cause);
 	printf("badaddr: %.*lx\n", w, regs->badaddr);
@@ -38,7 +52,9 @@ void do_handle_exception(struct pt_regs *regs)
 {
 	struct thread_info *info = current_thread_info();
 
-	if (regs->cause & CAUSE_IRQ_FLAG) {
+	if (!thread_info_valid(info)) {
+		printf("Exception before thread_info_init()\n");
+	} else if (regs->cause & CAUSE_IRQ_FLAG) {
 		unsigned long irq_cause = regs->cause & ~CAUSE_IRQ_FLAG;
 
 		assert(irq_cause < INTERRUPT_CAUSE_MAX);
-- 
2.56.0


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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-20 14:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 14:40 [kvm-unit-tests PATCH] riscv: Report exceptions taken before thread_info_init() Andrew Jones

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.