Fix nasty bug related to spinlock panic when lock is already held, because the tread pointer was never set on a per-hart basis.

This commit is contained in:
Imbus 2025-06-26 06:24:01 +02:00
parent 93295eaeb8
commit 58bface3c9
2 changed files with 12 additions and 2 deletions

View file

@ -40,6 +40,9 @@ static inline u64 r_tp() {
return x; return x;
} }
/** Write thread pointer */
static inline void w_tp(u64 x) { asm volatile("mv tp, %0" : : "r"(x)); }
/** /**
* Read the value of the sstatus register. * Read the value of the sstatus register.
* (Supervisor Status Register) * (Supervisor Status Register)

11
start.c
View file

@ -1,6 +1,8 @@
#include <config.h> #include <config.h>
#include <proc.h>
#include <riscv.h> #include <riscv.h>
#include <spinlock.h> #include <spinlock.h>
#include <string.h>
#include <types.h> #include <types.h>
#include <uart.h> #include <uart.h>
@ -18,8 +20,13 @@ volatile int greeted = 0;
/* This is where entry.S drops us of. All cores land here */ /* This is where entry.S drops us of. All cores land here */
void start() { void start() {
u64 id = r_mhartid();
u64 a = r_mhartid(); // Keep each CPU's hartid in its tp (thread pointer) register, for cpuid().
// This can then be retrieved with r_wp or cpuid(). It is used to index the
// cpus[] array in mycpu(), which in turn holds state for each individual
// cpu (struct Cpu).
w_tp(id);
acquire(&sl); acquire(&sl);
@ -29,7 +36,7 @@ void start() {
} }
uart_puts("Hart number: "); uart_puts("Hart number: ");
uart_putc(a + '0'); uart_putc(id + '0');
uart_putc('\n'); uart_putc('\n');
release(&sl); release(&sl);