From db368991526898148d0c85c65a8e56502e85949e Mon Sep 17 00:00:00 2001 From: xamidev Date: Sat, 21 Mar 2026 10:36:54 +0100 Subject: [PATCH] Add kprintf for DEBUG(); differentiated from printf() --- include/arch/x86.h | 2 ++ include/io/term/term.h | 1 + include/kernel.h | 5 +++- src/arch/x86/idt.c | 3 ++ src/arch/x86/syscall.c | 26 +++++++++++++++++ src/io/term/term.c | 64 ++++++++++++++++++++++++++++++++++++++++++ src/kapps/kshell.c | 20 +++++++++++++ src/kmain.c | 8 +----- 8 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 src/arch/x86/syscall.c create mode 100644 src/kapps/kshell.c diff --git a/include/arch/x86.h b/include/arch/x86.h index 1c677d8..2c04dcf 100644 --- a/include/arch/x86.h +++ b/include/arch/x86.h @@ -59,4 +59,6 @@ struct cpu_status_t { uint64_t iret_ss; }; +struct cpu_status_t* syscall_handler(struct cpu_status_t* regs); + #endif \ No newline at end of file diff --git a/include/io/term/term.h b/include/io/term/term.h index 4ce1a32..c4b8cfb 100644 --- a/include/io/term/term.h +++ b/include/io/term/term.h @@ -11,5 +11,6 @@ void kputs(const char* str); void term_init(void); int printf(const char* fmt, ...); void internal_putc(int c, void *_); +int kprintf(const char* fmt, ...); #endif diff --git a/include/kernel.h b/include/kernel.h index 98b43ac..116ba09 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -21,7 +21,7 @@ enum ErrorCodes { #include extern volatile uint64_t ticks; -#define DEBUG(log, ...) printf("[%8u] debug: <%s>: " log "\r\n", ticks, __func__, ##__VA_ARGS__) +#define DEBUG(log, ...) kprintf("[%8u] debug: <%s>: " log "\r\n", ticks, __func__, ##__VA_ARGS__) /* #define DEBUG(log, ...) \ printf("debug: [%s]: " log "\r\n", __FILE__, ##__VA_ARGS__); \ @@ -38,6 +38,8 @@ void panic(struct cpu_status_t* ctx, const char* str); void hcf(void); void idle(void); +void pedicel_main(void* arg); + /* debug */ void debug_stack_trace(unsigned int max_frames); const char* debug_find_symbol(uintptr_t rip, uintptr_t* offset); @@ -58,6 +60,7 @@ struct init_status { bool serial; bool keyboard; bool timer; + bool all; }; #endif diff --git a/src/arch/x86/idt.c b/src/arch/x86/idt.c index 822033c..b698162 100644 --- a/src/arch/x86/idt.c +++ b/src/arch/x86/idt.c @@ -72,6 +72,9 @@ void idt_init() // Each vector handler is 16-byte aligned, so *16 = address of that handler idt_set_entry(i, vector_0_handler + (i*16), 0); } + + idt_set_entry(0x80, syscall_handler, 0); + idt_load(&idt); DEBUG("IDT initialized"); } diff --git a/src/arch/x86/syscall.c b/src/arch/x86/syscall.c new file mode 100644 index 0000000..380c5fc --- /dev/null +++ b/src/arch/x86/syscall.c @@ -0,0 +1,26 @@ +/* + * @author xamidev + * @brief System call handling + * @license GPL-3.0-only + */ + +#include +#include + +struct cpu_status_t* syscall_handler(struct cpu_status_t* regs) +{ + DEBUG("Syscall %lx with argument %lx", regs->rdi, regs->rsi); + + switch (regs->rdi) + { + case 0: + break; + case 1: + break; + default: + regs->rsi = 0xdeadbeef; + break; + } + + return regs; +} \ No newline at end of file diff --git a/src/io/term/term.c b/src/io/term/term.c index 65b11a1..e4f7546 100644 --- a/src/io/term/term.c +++ b/src/io/term/term.c @@ -65,6 +65,38 @@ void internal_putc(int c, void *_) } } +/* + * debug_putc - Internal DEBUG putchar function + * @c: char to print + * @_: (unused, for nanoprintf) + * + * Prints a character to the terminal if it's ready and if + * the kernel is still initializing, and also always to the + * serial interface if it's ready. + */ +void debug_putc(int c, void *_) +{ + (void)_; + char ch = (char)c; + + if (init.terminal && (!init.all || panic_count > 0)) { + if (panic_count == 0) { + spinlock_acquire(&term_lock); + flanterm_write(ft_ctx, &ch, 1); + spinlock_release(&term_lock); + } else { + flanterm_write(ft_ctx, &ch, 1); + } + } + + if (init.serial) { + if (ch == '\n') { + skputc('\r'); + } + skputc(ch); + } +} + /* * printf - Fromatted printing * @fmt: format string @@ -96,6 +128,38 @@ int printf(const char* fmt, ...) return -1; } +/* + * kprintf - Fromatted DEBUG printing + * @fmt: format string + * @...: variadic arguments + * + * Wrapper for nanoprintf; to be used only for + * kernel/debug messages. + * + * Return: + * - number of characters sent to the callback + * %-1 - error + */ +int kprintf(const char* fmt, ...) +{ + if (panic_count == 0) { + spinlock_acquire(&printf_lock); + va_list args; + va_start(args, fmt); + int ret = npf_vpprintf(debug_putc, NULL, fmt, args); + va_end(args); + spinlock_release(&printf_lock); + return ret; + } else { + va_list args; + va_start(args, fmt); + int ret = npf_vpprintf(debug_putc, NULL, fmt, args); + va_end(args); + return ret; + } + return -1; +} + /* * kputs - Kernel puts * @str: String to write diff --git a/src/kapps/kshell.c b/src/kapps/kshell.c new file mode 100644 index 0000000..b416d69 --- /dev/null +++ b/src/kapps/kshell.c @@ -0,0 +1,20 @@ +/* + * @author xamidev + * @brief PepperOS kernel shell + * @license GPL-3.0-only + */ + +#include + +/* + * pedicel_main - Kernel shell main function + * @arg: argument (optional) + * + * This is the entry point for the kernel shell process. + * It is used to start programs and to test different things + * on different real hardware easily. + */ +void pedicel_main(void* arg) +{ + printf("Entering the kernel shell...\r\n"); +} \ No newline at end of file diff --git a/src/kmain.c b/src/kmain.c index 1a83b8f..d85ef7a 100644 --- a/src/kmain.c +++ b/src/kmain.c @@ -66,12 +66,6 @@ extern struct process_t* processes_list; extern struct process_t* current_process; struct process_t* idle_proc; -// Never gets executed although pedicel is scheduled? -void pedicel_main(void* arg) -{ - printf("\n\n\rWelcome to PepperOS! Pedicel speaking.\r\nNothing left to do, let's go idle!\r\n"); -} - void idle_main(void* arg) { for (;;) { @@ -125,10 +119,10 @@ void kmain() process_init(); idle_proc = process_create("idle", (void*)idle_main, 0); process_create("pedicel", (void*)pedicel_main, 0); - process_create("thing", thing_main, NULL); scheduler_init(); printf(PEPPEROS_SPLASH); + init.all = true; idle(); }