All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Jones <andrew.jones@linux.dev>
To: kvm-riscv@lists.infradead.org
Subject: [kvm-unit-tests PATCH] riscv: Report exceptions taken before thread_info_init()
Date: Fri, 20 Nov 2026 15:40:29 +0100	[thread overview]
Message-ID: <20261120144029.1430386-1-andrew.jones@linux.dev> (raw)

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

                 reply	other threads:[~2026-11-20 14:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261120144029.1430386-1-andrew.jones@linux.dev \
    --to=andrew.jones@linux.dev \
    --cc=kvm-riscv@lists.infradead.org \
    /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 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.