From e134202611a50b358c147c92bfb8a7f443030b8b Mon Sep 17 00:00:00 2001 From: Kimplul Date: Mon, 8 Jul 2024 17:43:59 +0300 Subject: fix LLVM + Anything extern is right out as LLVM doesn't produce correct code for them. Maybe if I added some extra attributes but I'm skeptical. Replaced with static variables and getters/setters. + Inline ASM is apparently a bit buggy, so use an assembly stub when jumping to init. + Minimize work done in main() to minimize chance of LLVM doing something silly. Still not 100% certain that I shouldn't just write the main() as an assembly stub in arch/riscv64 to be absolutely sure everything works as intended. + Make .kernel.start section SHF_ALLOC, otherwise lld complains about pc-relative addressing Probably some other stuff as well that I'm forgetting right now. But at least with LLVM14 LTO seems to work, which is pretty cool? --- arch/riscv64/kernel/proc.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'arch/riscv64/kernel/proc.c') diff --git a/arch/riscv64/kernel/proc.c b/arch/riscv64/kernel/proc.c index 73b0984..19bd52f 100644 --- a/arch/riscv64/kernel/proc.c +++ b/arch/riscv64/kernel/proc.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -16,6 +17,27 @@ #include "regs.h" #include "csr.h" +/** Assembly implementation for actually jumping to the init process, defined in + * start.S. Quite a few constants that could be implemented in assembly as well + * but this is maybe a bit more convenient. + * + * @param pid Should always be 0 to indicate that the kernel is the originator. + * @param tid Thread ID of the current thread. + * @param code Should always be SYS_USER_SPAWNED to indicate that a new core has + * come online. + * @param fdt Address of flattened device tree within userspace memory. + * @param initrd Ditto for initial ramdisk. + * @param proc Process ID, should be constant 1. + * @param stack_top Stack address. + */ +__noreturn void riscv_run_init(sys_arg_t pid, + sys_arg_t tid, + sys_arg_t code, + sys_arg_t fdt, + sys_arg_t initrd, + sys_arg_t proc, /* not strictly speaking necessary but eh */ + sys_arg_t stack_top); + void run_init(struct tcb *t, vm_t fdt, vm_t initrd) { csr_write(CSR_SSCRATCH, t); @@ -28,20 +50,10 @@ void run_init(struct tcb *t, vm_t fdt, vm_t initrd) * works for now. */ vm_t stack_top = t->thread_stack + t->thread_stack_size; + info("jumping to %lx\n", (long)t->callback); + bkl_unlock(); - __asm__ volatile ("mv sp, %0\n" - "li a0, %1\n" - "mv a1, %2\n" - "li a2, %3\n" - "mv a3, %4\n" - "mv a4, %5\n" - "li a5, %6\n" - "sret\n" - : - : "r" (stack_top), - "K" (0), "r" (t->tid), "K" (SYS_USER_SPAWNED), - "r" (fdt), "r" (initrd), "K" (1) - : "memory"); + riscv_run_init(0, t->tid, SYS_USER_SPAWNED, fdt, initrd, 1, stack_top); /* we should never reach this */ unreachable(); } -- cgit v1.3