From 22a681a07ab09922435ad0a9f665290a30f6b85b Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 4 Dec 2024 17:23:57 +0100 Subject: Pass the hart ID on start_kernel instead MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of storing the hard ID as given by the bootloader into a C variable, pass it directly into the 'start_kernel' function since we don't need to tamper the value as originally laid out on the 'a0' register. Signed-off-by: Miquel Sabaté Solà --- include/fbos/init.h | 11 +++-------- include/fbos/printk.h | 2 +- kernel/head.S | 17 ++++++----------- kernel/main.c | 7 ++----- kernel/printk.c | 2 +- 5 files changed, 13 insertions(+), 26 deletions(-) diff --git a/include/fbos/init.h b/include/fbos/init.h index f253645..592a7f1 100644 --- a/include/fbos/init.h +++ b/include/fbos/init.h @@ -25,11 +25,6 @@ // Instantiated in kernel/main.c extern atomic32_t hart_lottery; -// ID of the hart that is running the show. -// -// Instantiated in kernel/main.c -extern uint32_t hart_id; - // Tracks the amount of seconds that have elapsed since activating timer // interrupts. // @@ -52,9 +47,9 @@ void setup_interrupts(void); /* * This is the main entry point of the kernel after head.S is done. This - * function can (and will) assume that everything has been reset and that we can - * start the whole thing. + * function expects exactly two arguments, which are exactly the same as they + * are passed to the `_start` function in kernel/head.S by the bootloader. */ -void start_kernel(void *dtb); +void start_kernel(uint64_t hart_id, void *dtb); #endif /* __FBOS_INIT_H */ diff --git a/include/fbos/printk.h b/include/fbos/printk.h index d6ca71e..d118734 100644 --- a/include/fbos/printk.h +++ b/include/fbos/printk.h @@ -13,7 +13,7 @@ extern void die(const char *const message); // Print the given number as a single digit. -extern void print_digit(uint32_t digit); +extern void print_digit(uint64_t digit); // Print the given message. extern void printk(const char *const message); diff --git a/kernel/head.S b/kernel/head.S index 638e5e8..45e66a8 100644 --- a/kernel/head.S +++ b/kernel/head.S @@ -71,11 +71,6 @@ _start_kernel: amoadd.w a3, a2, (a3) bnez a3, .Lhart_wait - // Store the hart id as we will tamper with the 'a0' register later when - // performing the call to `start_kernel`. - la a3, hart_id - sw a0, 0(a3) - // Explicitely nullify the 'gp' and 'sscratch' registers, as they are a bit // special but we are not using them. For the rest of the registers, we // explicitely do not care to reset them. @@ -90,12 +85,12 @@ _start_kernel: // Point 'sp' the our global stack. See fbos/sched.h for more details. la sp, stack + STACK_SIZE - // The `start_kernel` function requires an argument to be passed, which is - // the pointer to the `fdt` blob. The bootloader puts this on the `a1` - // register, so let's move it now to `a0`. - mv a0, a1 - - // Start the kernel. + // Start the kernel. Notice that both 'a0' and 'a1' have been left + // untouched. This is no coincidence as the values as passed from the + // bootloader for both these two registers will be passed down to the kernel + // as is. As with the Linux kernel these two registers contain: + // - a0: boot hart id. + // - a1: pointer to the flattened device tree blob. tail start_kernel // We really shouldn't reach this point. If so, at least mask again all diff --git a/kernel/main.c b/kernel/main.c index 41619d6..3ed8551 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -5,11 +5,8 @@ #include #include -/* - * Data related to harts and set by kernel/head.S. Defined in fbos/init.h. - */ +// Defined in fbos/init.h. atomic32_t hart_lottery __section(".sdata"); -uint32_t hart_id; // Defined in fbos/sched.h. uint64_t stack[STACK_SIZE / sizeof(uint64_t)]; @@ -30,7 +27,7 @@ struct dt_info info = { .initrd_end = 0, }; -__noreturn __kernel void start_kernel(void *dtb) +__noreturn __kernel void start_kernel(uint64_t hart_id, void *dtb) { printk("Welcome to FizzBuzz OS!\n"); diff --git a/kernel/printk.c b/kernel/printk.c index 1cc68db..b67eea7 100644 --- a/kernel/printk.c +++ b/kernel/printk.c @@ -13,7 +13,7 @@ __kernel __noreturn void die(const char *const message) ; } -__kernel void print_digit(uint32_t digit) +__kernel void print_digit(uint64_t digit) { char buffer[2]; -- cgit v1.2.3