From 6c0f5aae02eb80d99e1082e3aa23474712e71b5a Mon Sep 17 00:00:00 2001 From: Tulio A M Mendes Date: Sat, 7 Feb 2026 13:27:02 -0300 Subject: [PATCH] kernel: introduce init_start to manage initrd and userspace bring-up --- include/kernel/init.h | 8 +++++++ src/kernel/init.c | 49 +++++++++++++++++++++++++++++++++++++++++++ src/kernel/main.c | 38 ++------------------------------- 3 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 include/kernel/init.h create mode 100644 src/kernel/init.c diff --git a/include/kernel/init.h b/include/kernel/init.h new file mode 100644 index 0000000..0aee3c4 --- /dev/null +++ b/include/kernel/init.h @@ -0,0 +1,8 @@ +#ifndef KERNEL_INIT_H +#define KERNEL_INIT_H + +#include "kernel/boot_info.h" + +void init_start(const struct boot_info* bi); + +#endif diff --git a/src/kernel/init.c b/src/kernel/init.c new file mode 100644 index 0000000..d463a41 --- /dev/null +++ b/src/kernel/init.c @@ -0,0 +1,49 @@ +#include "kernel/init.h" + +#include "arch/arch_platform.h" + +#include "fs.h" +#include "initrd.h" +#include "uart_console.h" + +#include "hal/mm.h" + +#include + +static int cmdline_has_token(const char* cmdline, const char* token) { + if (!cmdline || !token) return 0; + + for (size_t i = 0; cmdline[i] != 0; i++) { + size_t j = 0; + while (token[j] != 0 && cmdline[i + j] == token[j]) { + j++; + } + if (token[j] == 0) { + char before = (i == 0) ? ' ' : cmdline[i - 1]; + char after = cmdline[i + j]; + int before_ok = (before == ' ' || before == '\t'); + int after_ok = (after == 0 || after == ' ' || after == '\t'); + if (before_ok && after_ok) return 1; + } + } + + return 0; +} + +void init_start(const struct boot_info* bi) { + if (bi && bi->initrd_start) { + uintptr_t initrd_virt = 0; + if (hal_mm_map_physical_range((uintptr_t)bi->initrd_start, (uintptr_t)bi->initrd_end, + HAL_MM_MAP_RW, &initrd_virt) == 0) { + fs_root = initrd_init((uint32_t)initrd_virt); + } else { + uart_print("[INITRD] Failed to map initrd physical range.\n"); + } + } + + (void)arch_platform_start_userspace(bi); + + if (bi && cmdline_has_token(bi->cmdline, "ring3")) { + arch_platform_usermode_test_start(); + } +} diff --git a/src/kernel/main.c b/src/kernel/main.c index cfc292a..dbe975e 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -15,33 +15,14 @@ #include "fs.h" #include "kernel/boot_info.h" +#include "kernel/init.h" #include "syscall.h" #include "arch/arch_platform.h" #include "hal/cpu.h" -#include "hal/mm.h" -static int cmdline_has_token(const char* cmdline, const char* token) { - if (!cmdline || !token) return 0; - - for (size_t i = 0; cmdline[i] != 0; i++) { - size_t j = 0; - while (token[j] != 0 && cmdline[i + j] == token[j]) { - j++; - } - if (token[j] == 0) { - char before = (i == 0) ? ' ' : cmdline[i - 1]; - char after = cmdline[i + j]; - int before_ok = (before == ' ' || before == '\t'); - int after_ok = (after == 0 || after == ' ' || after == '\t'); - if (before_ok && after_ok) return 1; - } - } - - return 0; -} /* Check if the compiler thinks we are targeting the wrong operating system. */ #if defined(__linux__) @@ -78,22 +59,7 @@ void kernel_main(const struct boot_info* bi) { hal_cpu_enable_interrupts(); - // 9. Load InitRD (if available) - if (bi && bi->initrd_start) { - uintptr_t initrd_virt = 0; - if (hal_mm_map_physical_range((uintptr_t)bi->initrd_start, (uintptr_t)bi->initrd_end, - HAL_MM_MAP_RW, &initrd_virt) == 0) { - fs_root = initrd_init((uint32_t)initrd_virt); - } else { - uart_print("[INITRD] Failed to map initrd physical range.\n"); - } - } - - (void)arch_platform_start_userspace(bi); - - if (bi && cmdline_has_token(bi->cmdline, "ring3")) { - arch_platform_usermode_test_start(); - } + init_start(bi); // Start Shell as the main interaction loop shell_init(); -- 2.43.0