Add a default common print method

This commit is contained in:
FyloZ 2026-02-22 22:16:16 -05:00
parent 1f25cc23da
commit 8ccf823c67
3 changed files with 39 additions and 12 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -1,21 +1,39 @@
#include "../common.h"
#include "../uart.h"
#include <stdarg.h>
#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);
}