diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-12-03 14:34:43 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-12-03 14:34:43 +0100 |
| commit | 2eead36f97faf5bdad1abd60a220c0af72a5526c (patch) | |
| tree | b3a058db55deaffc9555d4f2955a89ae7da40bea /include | |
| parent | 95c4b00e90e24d3bcb5d3b6e56f0e29b725ba9ce (diff) | |
| download | fbos-2eead36f97fa.tar.gz fbos-2eead36f97fa.zip | |
Run a hart lottery on SMP
On systems with SMP multiple harts will try to run the kernel, and they
will appear at random. But in this kernel, in order to keep things
simple, we want to make sure that *only one* hart is running the show,
as it greatly simplifies things on these kinds of systems.
The solution is similar to what Linux does, which is to allow the first
hart to initialize things, but then (and different to what Linux does),
it will infinitely stall all the other harts that arrive at a random
later point in time.
In order to make this more apparent, I have also added a print message
showing which hart is being used to run the whole thing.
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'include')
| -rw-r--r-- | include/fbos/compiler.h | 5 | ||||
| -rw-r--r-- | include/fbos/init.h | 25 | ||||
| -rw-r--r-- | include/fbos/printk.h | 10 |
3 files changed, 40 insertions, 0 deletions
diff --git a/include/fbos/compiler.h b/include/fbos/compiler.h index 67f3194..77413e9 100644 --- a/include/fbos/compiler.h +++ b/include/fbos/compiler.h @@ -39,6 +39,11 @@ typedef unsigned long size_t; typedef unsigned long uint64_t; typedef unsigned long uintptr_t; +// Strong type for atomic integer operations. +typedef struct { + int32_t value; +} atomic32_t; + /* * NULL */ diff --git a/include/fbos/init.h b/include/fbos/init.h index 9e0049e..dcffd02 100644 --- a/include/fbos/init.h +++ b/include/fbos/init.h @@ -5,6 +5,31 @@ #include <fbos/dt.h> #include <fbos/sched.h> +// Atomic value that holds how many harts have gone through the "hart lottery". +// This is in the same spirit as it happens on the Linux kernel: the RISC-V +// specification leaves open which hart will appear first into the kernel code. +// This greatly simplifies the specification and the hardware, but for the +// kernel this means that harts will appear randomly. In order to know which +// hart runs first, in Linux they run a "lottery": an atomic value holds how +// many harts hav already been seen. The first hart to appear will actually +// initialize things before bringing the others up, while the others will simply +// wait until the first hart frees the lock for them. +// +// That being said, here we only want *one* hart running. Hence, whichever hart +// wins the lottery, it's not only going to initialize the kernel, but it will +// also be the only one running the show. This is of course a waste of +// resources, but it's not like running fizz/buzz needs SMP and cores running at +// full speed. Actually, keeping things under a single hart simplifies things a +// lot. +// +// 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. // diff --git a/include/fbos/printk.h b/include/fbos/printk.h index e68d1eb..d6ca71e 100644 --- a/include/fbos/printk.h +++ b/include/fbos/printk.h @@ -9,9 +9,19 @@ */ #ifdef __KERNEL__ +// Print the given message and loop indefinitely. extern void die(const char *const message); + +// Print the given number as a single digit. +extern void print_digit(uint32_t digit); + +// Print the given message. extern void printk(const char *const message); + +// Print the given message which is exactly 'n' bytes long. extern void write(const char *const message, size_t n); + +// 'write' system call. extern void sys_write(const char *const message, size_t n); #else #include <stdio.h> |
