From 32e880127366fabc3e8b4fecb642f1cc06b8fd71 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Mon, 2 Dec 2024 20:47:19 +0100 Subject: Prepend the name of the task on debug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 'printk' and 'sys_write' calls, prepend the name of the task that is being executed for each message. This is an easy way to show off that we are doing the right thing with the 'tp' register, even if we don't do much with it. Signed-off-by: Miquel Sabaté Solà --- include/fbos/printk.h | 2 +- include/fbos/sched.h | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/fbos/printk.h b/include/fbos/printk.h index 5a12d7c..d5f4804 100644 --- a/include/fbos/printk.h +++ b/include/fbos/printk.h @@ -11,7 +11,7 @@ #ifdef __KERNEL__ extern void die(const char *const message); extern void printk(const char *const message); -extern void write(const char *const message, size_t n); +extern void sys_write(const char *const message, size_t n); #else #include #include diff --git a/include/fbos/sched.h b/include/fbos/sched.h index f43b6e5..e8edafd 100644 --- a/include/fbos/sched.h +++ b/include/fbos/sched.h @@ -12,6 +12,9 @@ enum task_id { TASK_FIZZBUZZ = 3, }; +// Maximum length for the name of the process. +#define TASK_NAME_LEN 16 + // All the information we need to grab for processes. struct task_struct { // The stack allocated for the process. As you can see when initializing @@ -20,6 +23,9 @@ struct task_struct { // for simple `sp` values. void *stack; + // Name of the task to be printed for debugging purposes. + const char name[TASK_NAME_LEN]; + // The address for the binary entry. const void *entry_addr; }; @@ -27,14 +33,26 @@ struct task_struct { // Tasks available on this kernel. extern struct task_struct tasks[4]; -// Set the return address to U-mode to the given task. -__kernel __always_inline void set_return_address_to(int task_id) +// Prepare for switching to the given task. Note that this will not actually +// jump into the given task, but it will prepare the relevant registers before +// performing the actual jump. +__kernel __always_inline void prepare_switch_to(int task_id) { + register struct task_struct *current asm("tp"); + asm volatile("csrc sstatus, %0\n\t" "mv t0, %1\n\t" "csrw sepc, t0" ::"r"(1 << 8), "r"(tasks[task_id].entry_addr) : "t0"); + + current = &tasks[task_id]; + + // Not really unused, but I was getting into lots of petty trouble with GCC + // depending if running with/without DEBUG on. Hence, let's set 'tp' in C + // instead of with inline assembly, even if the compiler thinks the variable + // is set but not used. + __unused(current); } #endif // __FBOS_SCHED_H_ -- cgit v1.2.3