From 3dd00b900cf087b0f39d2626c957b228cafed52a Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Thu, 3 Mar 2016 11:57:21 -0800 Subject: Simplify boot code --- pk/configstring.c | 2 +- pk/encoding.h | 7 +++++-- pk/mentry.S | 30 ++++++++---------------------- pk/minit.c | 15 ++++----------- pk/mtrap.c | 2 +- pk/mtrap.h | 23 ++++++++++------------- pk/pk.h | 3 +-- pk/pk.ld | 2 +- 8 files changed, 31 insertions(+), 53 deletions(-) diff --git a/pk/configstring.c b/pk/configstring.c index b7d443f..288d211 100644 --- a/pk/configstring.c +++ b/pk/configstring.c @@ -179,7 +179,7 @@ static void query_harts(const char* config_string) query_result res = query_config_string(config_string, buf); if (!res.start) break; - uintptr_t* base = (uintptr_t*)get_uint(res); + csr_t* base = (csr_t*)get_uint(res); uintptr_t hart_id = base[CSR_MHARTID]; hls_init(hart_id, base); debug_printk("at %p, found hart %ld\n", base, hart_id); diff --git a/pk/encoding.h b/pk/encoding.h index 9189fde..f2fab36 100644 --- a/pk/encoding.h +++ b/pk/encoding.h @@ -61,8 +61,11 @@ #define IRQ_S_TIMER 5 #define IRQ_H_TIMER 6 #define IRQ_M_TIMER 7 -#define IRQ_COP 8 -#define IRQ_HOST 9 +#define IRQ_S_DEV 9 +#define IRQ_H_DEV 10 +#define IRQ_M_DEV 11 +#define IRQ_COP 12 +#define IRQ_HOST 13 #define DEFAULT_RSTVEC 0x0 #define DEFAULT_NMIVEC 0x4 diff --git a/pk/mentry.S b/pk/mentry.S index b8e9136..ee3a146 100644 --- a/pk/mentry.S +++ b/pk/mentry.S @@ -222,37 +222,23 @@ do_reset: li x31, 0 csrw mscratch, x0 - csrr a0, mhartid - bnez a0, .LmultiHart - # sp <- end of first full page after the end of the binary la sp, _end + 2*RISCV_PGSIZE - 1 li t0, -RISCV_PGSIZE and sp, sp, t0 addi sp, sp, -MENTRY_FRAME_SIZE - j init_first_hart + csrr a0, mhartid + slli a1, a0, RISCV_PGSHIFT + add sp, sp, a1 + + beqz a0, init_first_hart .LmultiHart: #if MAX_HARTS > 1 # make sure our hart id is within a valid range li a1, MAX_HARTS - bgeu a0, a1, .LmultiHart - - # signal we're ready to boot - li a1, 1 - sll a1, a1, a0 - la a2, booted_harts_mask - amoor.w x0, a1, (a2) - - # wait for main hart to grant us a stack -1:csrrw sp, mscratch, x0 - beqz sp, 1b - - j init_other_hart -#else - j .LmultiHart + bltu a0, a1, init_other_hart + wfi #endif - - # XXX depend on sbi_base to force its linkage - la x0, sbi_base + j .LmultiHart diff --git a/pk/minit.c b/pk/minit.c index 033e476..bf151e8 100644 --- a/pk/minit.c +++ b/pk/minit.c @@ -1,7 +1,6 @@ #include "vm.h" #include "mtrap.h" -volatile uint32_t booted_harts_mask; uintptr_t mem_size; uint32_t num_harts; @@ -72,21 +71,12 @@ static void fp_init() #endif } -void hls_init(uint32_t id, uintptr_t* csrs) +void hls_init(uint32_t id, csr_t* csrs) { hls_t* hls = OTHER_HLS(id); memset(hls, 0, sizeof(*hls)); - hls->hart_id = id; hls->csrs = csrs; hls->console_ibuf = -1; - - if (id != 0) { - while (((booted_harts_mask >> id) & 1) == 0) - ; - mb(); - // wake up the hart by granting it a stack - csrs[CSR_MSCRATCH] = (uintptr_t)(OTHER_STACK_TOP(id) - MENTRY_FRAME_SIZE); - } } static void hart_init() @@ -123,6 +113,9 @@ void init_other_hart() mb(); write_csr(sptbr, (uintptr_t)root_page_table >> RISCV_PGSHIFT); + // make sure hart 0 has discovered us + kassert(HLS()->csrs != NULL); + boot_other_hart(); } diff --git a/pk/mtrap.c b/pk/mtrap.c index f07a395..4e45b80 100644 --- a/pk/mtrap.c +++ b/pk/mtrap.c @@ -82,7 +82,7 @@ void __attribute__((noreturn)) bad_trap() static uintptr_t mcall_hart_id() { - return HLS()->hart_id; + return read_const_csr(mhartid); } void request_htif_keyboard_interrupt() diff --git a/pk/mtrap.h b/pk/mtrap.h index 705d09d..5d2154f 100644 --- a/pk/mtrap.h +++ b/pk/mtrap.h @@ -145,26 +145,24 @@ static insn_t __attribute__((always_inline)) get_insn(uintptr_t mepc, uintptr_t* return insn; } -static inline long __attribute__((pure)) isa() -{ - long res; - asm ("csrr %0, misa" : "=r"(res)); // not volatile, so don't use read_csr() - return res; -} +#define read_const_csr(reg) ({ unsigned long __tmp; \ + asm ("csrr %0, " #reg : "=r"(__tmp)); \ + __tmp; }) static inline int supports_extension(char ext) { - return isa() & (1 << (ext - 'A')); + return read_const_csr(misa) & (1 << (ext - 'A')); } static inline int xlen() { - return isa() < 0 ? 64 : 32; + return read_const_csr(misa) < 0 ? 64 : 32; } +typedef uintptr_t csr_t; // TODO this might become uint128_t for RV128 + typedef struct { - volatile uintptr_t* csrs; - int hart_id; + volatile csr_t* csrs; volatile int mipi_pending; volatile int sipi_pending; int console_ibuf; @@ -183,7 +181,7 @@ typedef struct { void request_htif_keyboard_interrupt(); -void hls_init(uint32_t hart_id, uintptr_t* csrs); +void hls_init(uint32_t hart_id, csr_t* csrs); #define MACHINE_STACK_TOP() ({ \ register uintptr_t sp asm ("sp"); \ @@ -191,8 +189,7 @@ void hls_init(uint32_t hart_id, uintptr_t* csrs); // hart-local storage, at top of stack #define HLS() ((hls_t*)(MACHINE_STACK_TOP() - HLS_SIZE)) -#define OTHER_STACK_TOP(id) (MACHINE_STACK_TOP() + RISCV_PGSIZE * ((id) - HLS()->hart_id)) -#define OTHER_HLS(id) ((hls_t*)((void*)HLS() + RISCV_PGSIZE * ((id) - HLS()->hart_id))) +#define OTHER_HLS(id) ((hls_t*)((void*)HLS() + RISCV_PGSIZE * ((id) - read_const_csr(mhartid)))) #define printk printm diff --git a/pk/pk.h b/pk/pk.h index 98738bf..7785ca9 100644 --- a/pk/pk.h +++ b/pk/pk.h @@ -4,7 +4,7 @@ #define _PK_H #ifdef __riscv_atomic -# define MAX_HARTS 32 // coupled to width of booted_harts_mask +# define MAX_HARTS 8 // arbitrary #else # define MAX_HARTS 1 #endif @@ -54,7 +54,6 @@ extern "C" { extern uintptr_t mem_size; extern int have_vm; extern uint32_t num_harts; -extern volatile uint32_t booted_harts_mask; struct mainvars* parse_args(struct mainvars*); void printk(const char* s, ...); diff --git a/pk/pk.ld b/pk/pk.ld index cf1d9de..77ba6cf 100644 --- a/pk/pk.ld +++ b/pk/pk.ld @@ -88,7 +88,7 @@ SECTIONS .sbi : { - *(.sbi) + sbi.o(.sbi) } _end = .; -- cgit v1.1