]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
kernel: introduce init_start to manage initrd and userspace bring-up
authorTulio A M Mendes <[email protected]>
Sat, 7 Feb 2026 16:27:02 +0000 (13:27 -0300)
committerTulio A M Mendes <[email protected]>
Sat, 7 Feb 2026 16:27:02 +0000 (13:27 -0300)
include/kernel/init.h [new file with mode: 0644]
src/kernel/init.c [new file with mode: 0644]
src/kernel/main.c

diff --git a/include/kernel/init.h b/include/kernel/init.h
new file mode 100644 (file)
index 0000000..0aee3c4
--- /dev/null
@@ -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 (file)
index 0000000..d463a41
--- /dev/null
@@ -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 <stddef.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;
+}
+
+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();
+    }
+}
index cfc292a44b024c05f7fe8b01dd857a8b01b0a04e..dbe975e4cef52acf977f6cc774a19d8515e2814f 100644 (file)
 #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();