Skip to contentSkip to conversation

riscv: Report exceptions taken before thread_info_init()

PATCH New Andrew Jones 1 participant 1 message Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: Report exceptions taken before thread_info_init()

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.

---
 lib/riscv/processor.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
lib/riscv/processor.c +18 -2
index b055acc94e41..aa1ac417f1e9 100644
@@ -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);
Signature · 7 lines
2.56.0


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv