[kvm-unit-tests PATCH] riscv: Report exceptions taken before thread_info_init()
Andrew Jones
andrew.jones at linux.dev
Fri Nov 20 06:40:29 PST 2026
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 at 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
More information about the kvm-riscv
mailing list