From 8ccf823c6792f1025923a6a220d3341dcdc51fde Mon Sep 17 00:00:00 2001 From: FyloZ Date: Sun, 22 Feb 2026 22:16:16 -0500 Subject: [PATCH] Add a default common print method --- src/kernel/common.h | 9 ++++++++- src/kernel/kernel.c | 4 +++- src/kernel/riscv64/common.c | 38 +++++++++++++++++++++++++++---------- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/kernel/common.h b/src/kernel/common.h index 073ce89..6eb9166 100644 --- a/src/kernel/common.h +++ b/src/kernel/common.h @@ -2,10 +2,17 @@ #define COMMON_H /** - * Sends a kernel panic message through the UART and hang the core. + * Sends a panic message through the default output of the kernel. * @param format The format of the message * @param ... The parameters of the message */ void panic(const char *format, ...); +/** + * Prints a string to the default output of the kernel. + * @param format The format of the string + * @param ... The parameters of the string + */ +void print(const char *format, ...); + #endif \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index f783d72..25c75cd 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -1,3 +1,4 @@ +#include "common.h" #include "syscon.h" #include "uart.h" @@ -5,7 +6,8 @@ void kmain(void) { uart_init(); - uart_printf("Hello world, %s!\n", ARCH); + + print("Hello world, %s!\n", ARCH); power_off(); } diff --git a/src/kernel/riscv64/common.c b/src/kernel/riscv64/common.c index 0621732..d06b92c 100644 --- a/src/kernel/riscv64/common.c +++ b/src/kernel/riscv64/common.c @@ -1,21 +1,39 @@ #include "../common.h" - +#include "../uart.h" #include -#include "../uart.h" +#define VPRINT(method, format, args) \ + VPRINT_(method, format, args) +#define VPRINT_(method, format, args) \ + method ## _ ## vprintf(format, args) + +#define PRINT(method, str)\ + PRINT_(method, str) +#define PRINT_(method, str)\ + method ## _ ## puts(str) + +#define DEFAULT_OUTPUT_METHOD uart +#define DEFAULT_PRINT_ARGS(format) \ + va_list args = nullptr; \ + va_start(args, format); \ + DEFAULT_VPRINT(format, args); \ + va_end(args) +#define DEFAULT_VPRINT(format, args) \ + VPRINT(DEFAULT_OUTPUT_METHOD, format, args) +#define DEFAULT_PRINT(str)\ + PRINT(DEFAULT_OUTPUT_METHOD, str) void panic(const char *format, ...) { - uart_puts("Kernel panic!"); - uart_puts("Reason:"); - - va_list args = nullptr; - va_start(args, format); + DEFAULT_PRINT("Kernel panic!"); + DEFAULT_PRINT("Reason:"); // Print the reason - uart_vprintf(format, args); - - va_end(args); + DEFAULT_PRINT_ARGS(format); // Hang asm volatile("wfi"); } + +void print(const char *format, ...) { + DEFAULT_PRINT_ARGS(format); +} \ No newline at end of file