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à --- kernel/head.S | 6 +++--- kernel/main.c | 8 ++++---- kernel/printk.c | 44 +++++++++++++++++++++++++++++++++++++++----- kernel/trap.c | 10 +++++----- 4 files changed, 51 insertions(+), 17 deletions(-) (limited to 'kernel') diff --git a/kernel/head.S b/kernel/head.S index 31f423e..9a12833 100644 --- a/kernel/head.S +++ b/kernel/head.S @@ -94,9 +94,9 @@ _start_kernel: li t6, 0 csrw sscratch, 0 - // Point tp and sp to the init task. This is not crucial because in the end - // we are not doing anything with the 'tp' register, and our processes - // actually don't do anything on the stack. + // Point tp and sp to the init task. The 'tp' register will always point to + // the current process being executed, and it will be shown on debug when + // printing out messages. la tp, tasks la sp, tasks + THREAD_SIZE diff --git a/kernel/main.c b/kernel/main.c index ecb93da..9c524c4 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -10,10 +10,10 @@ unsigned long stack[4][THREAD_SIZE / sizeof(unsigned long)]; // Initialize the list of structs by providing a fixed stack address and empty // values everywhere else. struct task_struct tasks[4] = { - [TASK_INIT] = { .stack = stack[0], .entry_addr = nullptr, }, - [TASK_FIZZ] = { .stack = stack[1], .entry_addr = nullptr, }, - [TASK_BUZZ] = { .stack = stack[2], .entry_addr = nullptr, }, - [TASK_FIZZBUZZ] = { .stack = stack[3], .entry_addr = nullptr, }, + [TASK_INIT] = { .stack = stack[0], .name = "init", .entry_addr = nullptr, }, + [TASK_FIZZ] = { .stack = stack[1], .name = "fizz", .entry_addr = nullptr, }, + [TASK_BUZZ] = { .stack = stack[2], .name = "buzz", .entry_addr = nullptr, }, + [TASK_FIZZBUZZ] = { .stack = stack[3], .name = "fizzbuzz", .entry_addr = nullptr, }, }; // Defined in fbos/init.h. diff --git a/kernel/printk.c b/kernel/printk.c index b3c925a..bd2bcab 100644 --- a/kernel/printk.c +++ b/kernel/printk.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -12,8 +13,40 @@ void __noreturn __kernel die(const char *const message) ; } +__kernel void write(const char *const message, size_t n) +{ + struct sbi_ret ret = sbi_ecall2(DBCN_EXT, DBCN_WRITE, n, (unsigned long)message); + if (ret.error != SBI_SUCCESS) { + die(nullptr); + } +} + +#ifdef __DEBUG__ +__kernel void print_task_prefix(void) +{ + register struct task_struct *current asm("tp"); + + if (!current) { + return; + } + + size_t len = strlen(current->name); + if (!len) { + return; + } + + write("[=> ", 4); + write(current->name, len); + write("] ", 2); +} +#endif + __kernel void printk(const char *const message) { +#ifdef __DEBUG__ + print_task_prefix(); +#endif + size_t len = strlen(message); if (!len) { return; @@ -22,10 +55,11 @@ __kernel void printk(const char *const message) write(message, len); } -__kernel void write(const char *const message, size_t n) +__kernel void sys_write(const char *const message, size_t n) { - struct sbi_ret ret = sbi_ecall2(DBCN_EXT, DBCN_WRITE, n, (unsigned long)message); - if (ret.error != SBI_SUCCESS) { - die(nullptr); - } +#ifdef __DEBUG__ + print_task_prefix(); +#endif + + write(message, n); } diff --git a/kernel/trap.c b/kernel/trap.c index 54bf6aa..29f1c21 100644 --- a/kernel/trap.c +++ b/kernel/trap.c @@ -52,7 +52,7 @@ __kernel __always_inline void exception_handler(uint64_t cause) die("Bad syscall\n"); } - write(message, n); + sys_write(message, n); } /* @@ -75,7 +75,7 @@ __aligned(4) __s_interrupt __kernel void interrupt_handler(void) if (IS_EXCEPTION(cause)) { exception_handler(cause); - set_return_address_to(TASK_INIT); + prepare_switch_to(TASK_INIT); goto end; } @@ -91,11 +91,11 @@ __aligned(4) __s_interrupt __kernel void interrupt_handler(void) // BEHOLD! The fizz buzz logic! :D seconds_elapsed += 1; if ((seconds_elapsed % 15) == 0) { - set_return_address_to(TASK_FIZZBUZZ); + prepare_switch_to(TASK_FIZZBUZZ); } else if ((seconds_elapsed % 5) == 0) { - set_return_address_to(TASK_BUZZ); + prepare_switch_to(TASK_BUZZ); } else if ((seconds_elapsed % 3) == 0) { - set_return_address_to(TASK_FIZZ); + prepare_switch_to(TASK_FIZZ); } // Re-enable timer interrupts. -- cgit v1.2.3