From: Tulio A M Mendes Date: Tue, 10 Feb 2026 05:47:20 +0000 (-0300) Subject: refactor: remove all #if x86 guards from shell.c, migrate to kprintf + HAL X-Git-Url: https://projects.tadryanom.me/docs/static/gitweb.js?a=commitdiff_plain;h=c0a88a8652d761ca1e17b1a342266c96f21079db;p=AdrOS.git refactor: remove all #if x86 guards from shell.c, migrate to kprintf + HAL shell.c is generic kernel code and should not contain any architecture-specific guards or inline assembly. Changes: - Replace all uart_print() calls with kprintf() so output goes through the console subsystem and into the kernel log buffer - Replace x86 'cli; ud2' panic asm with hal_cpu_disable_interrupts() + hal_cpu_idle() loop - Replace #if x86 ring3 guard with arch_platform_usermode_test_start() which is already a generic wrapper with per-arch implementations - Use console_write() for echo/backspace (no log buffer pollution) - Remove unnecessary uart_console.h and vga_console.h includes Passes: make, cppcheck, QEMU smoke test. --- diff --git a/src/kernel/shell.c b/src/kernel/shell.c index d6e91f6..6b82c0e 100644 --- a/src/kernel/shell.c +++ b/src/kernel/shell.c @@ -1,14 +1,13 @@ #include "shell.h" #include "keyboard.h" -#include "uart_console.h" #include "console.h" #include "utils.h" #include "pmm.h" -#include "vga_console.h" -#include "process.h" +#include "process.h" #include "fs.h" #include "heap.h" +#include "arch/arch_platform.h" #include "hal/system.h" #include "hal/cpu.h" @@ -17,36 +16,36 @@ static char cmd_buffer[MAX_CMD_LEN]; static int cmd_index = 0; void print_prompt(void) { - uart_print("\nAdrOS $> "); + kprintf("\nAdrOS $> "); } void execute_command(char* cmd) { - uart_print("\n"); + kprintf("\n"); if (strcmp(cmd, "help") == 0) { - uart_print("Available commands:\n"); - uart_print(" help - Show this list\n"); - uart_print(" clear - Clear screen\n"); - uart_print(" ls - List files (Dummy)\n"); - uart_print(" cat - Read file content\n"); - uart_print(" mem - Show memory stats\n"); - uart_print(" panic - Trigger kernel panic\n"); - uart_print(" ring3 - Run x86 ring3 syscall test\n"); - uart_print(" reboot - Restart system\n"); - uart_print(" sleep - Sleep for N ticks\n"); - uart_print(" dmesg - Show kernel log buffer\n"); + kprintf("Available commands:\n"); + kprintf(" help - Show this list\n"); + kprintf(" clear - Clear screen\n"); + kprintf(" ls - List files (Dummy)\n"); + kprintf(" cat - Read file content\n"); + kprintf(" mem - Show memory stats\n"); + kprintf(" panic - Trigger kernel panic\n"); + kprintf(" ring3 - Run usermode syscall test\n"); + kprintf(" reboot - Restart system\n"); + kprintf(" sleep - Sleep for N ticks\n"); + kprintf(" dmesg - Show kernel log buffer\n"); } else if (strcmp(cmd, "ls") == 0) { if (!fs_root) { - uart_print("No filesystem mounted.\n"); + kprintf("No filesystem mounted.\n"); } else { - uart_print("Filesystem Mounted (InitRD).\n"); - uart_print("Try: cat test.txt\n"); + kprintf("Filesystem Mounted (InitRD).\n"); + kprintf("Try: cat test.txt\n"); } } else if (strncmp(cmd, "cat ", 4) == 0) { if (!fs_root) { - uart_print("No filesystem mounted.\n"); + kprintf("No filesystem mounted.\n"); } else { const char* fname = cmd + 4; fs_node_t* file = NULL; @@ -60,79 +59,63 @@ void execute_command(char* cmd) { file = vfs_lookup(abs); } if (file) { - uart_print("Reading "); uart_print(fname); uart_print("...\n"); + kprintf("Reading %s...\n", fname); uint8_t* buf = (uint8_t*)kmalloc(file->length + 1); if (buf) { uint32_t sz = vfs_read(file, 0, file->length, buf); buf[sz] = 0; - uart_print((char*)buf); - uart_print("\n"); + kprintf("%s\n", (char*)buf); kfree(buf); } else { - uart_print("OOM: File too big for heap.\n"); + kprintf("OOM: File too big for heap.\n"); } } else { - uart_print("File not found.\n"); + kprintf("File not found.\n"); } } } else if (strcmp(cmd, "clear") == 0) { - uart_print("\033[2J\033[1;1H"); + kprintf("\033[2J\033[1;1H"); } else if (strncmp(cmd, "sleep ", 6) == 0) { int ticks = atoi(cmd + 6); - uart_print("Sleeping for "); - uart_print(cmd + 6); - uart_print(" ticks...\n"); + kprintf("Sleeping for %s ticks...\n", cmd + 6); process_sleep(ticks); - uart_print("Woke up!\n"); + kprintf("Woke up!\n"); } else if (strcmp(cmd, "mem") == 0) { - uart_print("Memory Stats:\n"); - uart_print(" Total RAM: [TODO] MB\n"); + kprintf("Memory Stats:\n"); + kprintf(" Total RAM: [TODO] MB\n"); } else if (strcmp(cmd, "panic") == 0) { -#if defined(__i386__) || defined(__x86_64__) - __asm__ volatile("cli"); - __asm__ volatile("ud2"); -#else + hal_cpu_disable_interrupts(); for(;;) { hal_cpu_idle(); } -#endif } else if (strcmp(cmd, "ring3") == 0) { -#if defined(__i386__) - extern void x86_usermode_test_start(void); - x86_usermode_test_start(); -#else - uart_print("ring3 test only available on x86.\n"); -#endif + arch_platform_usermode_test_start(); } else if (strcmp(cmd, "dmesg") == 0) { char dmesg_buf[4096]; size_t n = klog_read(dmesg_buf, sizeof(dmesg_buf)); if (n > 0) { - uart_print(dmesg_buf); + console_write(dmesg_buf); } else { - uart_print("(empty)\n"); + kprintf("(empty)\n"); } } else if (strcmp(cmd, "reboot") == 0) { hal_system_reboot(); } else if (strlen(cmd) > 0) { - uart_print("Unknown command: "); - uart_print(cmd); - uart_print("\n"); + kprintf("Unknown command: %s\n", cmd); } print_prompt(); } void shell_callback(char c) { - char str[2] = { c, 0 }; - if (c == '\n') { cmd_buffer[cmd_index] = 0; execute_command(cmd_buffer); @@ -141,19 +124,20 @@ void shell_callback(char c) { else if (c == '\b') { if (cmd_index > 0) { cmd_index--; - uart_print("\b \b"); + console_write("\b \b"); } } else { if (cmd_index < MAX_CMD_LEN - 1) { cmd_buffer[cmd_index++] = c; - uart_print(str); + char str[2] = { c, 0 }; + console_write(str); } } } void shell_init(void) { - uart_print("[SHELL] Starting Shell...\n"); + kprintf("[SHELL] Starting Shell...\n"); cmd_index = 0; keyboard_set_callback(shell_callback); print_prompt();