]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
kernel: move arch-specific init into arch_platform hooks
authorTulio A M Mendes <[email protected]>
Sat, 7 Feb 2026 16:21:47 +0000 (13:21 -0300)
committerTulio A M Mendes <[email protected]>
Sat, 7 Feb 2026 16:21:47 +0000 (13:21 -0300)
include/arch/arch_platform.h [new file with mode: 0644]
src/arch/arm/arch_platform.c [new file with mode: 0644]
src/arch/mips/arch_platform.c [new file with mode: 0644]
src/arch/riscv/arch_platform.c [new file with mode: 0644]
src/arch/x86/arch_platform.c [new file with mode: 0644]
src/kernel/main.c

diff --git a/include/arch/arch_platform.h b/include/arch/arch_platform.h
new file mode 100644 (file)
index 0000000..e51dbe5
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef ARCH_PLATFORM_H
+#define ARCH_PLATFORM_H
+
+#include "kernel/boot_info.h"
+
+int arch_platform_setup(const struct boot_info* bi);
+int arch_platform_start_userspace(const struct boot_info* bi);
+void arch_platform_usermode_test_start(void);
+
+#endif
diff --git a/src/arch/arm/arch_platform.c b/src/arch/arm/arch_platform.c
new file mode 100644 (file)
index 0000000..efeff31
--- /dev/null
@@ -0,0 +1,14 @@
+#include "arch/arch_platform.h"
+
+int arch_platform_setup(const struct boot_info* bi) {
+    (void)bi;
+    return -1;
+}
+
+int arch_platform_start_userspace(const struct boot_info* bi) {
+    (void)bi;
+    return -1;
+}
+
+void arch_platform_usermode_test_start(void) {
+}
diff --git a/src/arch/mips/arch_platform.c b/src/arch/mips/arch_platform.c
new file mode 100644 (file)
index 0000000..efeff31
--- /dev/null
@@ -0,0 +1,14 @@
+#include "arch/arch_platform.h"
+
+int arch_platform_setup(const struct boot_info* bi) {
+    (void)bi;
+    return -1;
+}
+
+int arch_platform_start_userspace(const struct boot_info* bi) {
+    (void)bi;
+    return -1;
+}
+
+void arch_platform_usermode_test_start(void) {
+}
diff --git a/src/arch/riscv/arch_platform.c b/src/arch/riscv/arch_platform.c
new file mode 100644 (file)
index 0000000..efeff31
--- /dev/null
@@ -0,0 +1,14 @@
+#include "arch/arch_platform.h"
+
+int arch_platform_setup(const struct boot_info* bi) {
+    (void)bi;
+    return -1;
+}
+
+int arch_platform_start_userspace(const struct boot_info* bi) {
+    (void)bi;
+    return -1;
+}
+
+void arch_platform_usermode_test_start(void) {
+}
diff --git a/src/arch/x86/arch_platform.c b/src/arch/x86/arch_platform.c
new file mode 100644 (file)
index 0000000..5338daf
--- /dev/null
@@ -0,0 +1,78 @@
+#include "arch/arch_platform.h"
+
+#include "elf.h"
+#include "fs.h"
+#include "keyboard.h"
+#include "syscall.h"
+#include "timer.h"
+#include "uart_console.h"
+#include "uaccess.h"
+#include "vga_console.h"
+#include "vmm.h"
+
+#include "hal/cpu.h"
+#include "hal/usermode.h"
+
+#if defined(__i386__)
+extern void x86_usermode_test_start(void);
+#endif
+
+#if defined(__i386__)
+static uint8_t ring0_trap_stack[16384] __attribute__((aligned(16)));
+#endif
+
+int arch_platform_setup(const struct boot_info* bi) {
+    (void)bi;
+#if defined(__i386__)
+    vmm_init();
+
+    vga_init();
+    vga_set_color(0x0A, 0x00);
+    vga_print("[AdrOS] Kernel Initialized (VGA).\n");
+
+    syscall_init();
+    keyboard_init();
+
+    return 0;
+#else
+    return -1;
+#endif
+}
+
+int arch_platform_start_userspace(const struct boot_info* bi) {
+    (void)bi;
+#if defined(__i386__)
+    if (!fs_root) return -1;
+
+    uintptr_t entry = 0;
+    uintptr_t user_sp = 0;
+    if (elf32_load_user_from_initrd("init.elf", &entry, &user_sp) != 0) {
+        return -1;
+    }
+
+    uart_print("[ELF] starting init.elf\n");
+
+    uart_print("[ELF] user_range_ok(entry)=");
+    uart_put_char(user_range_ok((const void*)entry, 1) ? '1' : '0');
+    uart_print(" user_range_ok(stack)=");
+    uart_put_char(user_range_ok((const void*)(user_sp - 16), 16) ? '1' : '0');
+    uart_print("\n");
+
+    hal_cpu_set_kernel_stack((uintptr_t)&ring0_trap_stack[sizeof(ring0_trap_stack)]);
+
+    if (hal_usermode_enter(entry, user_sp) < 0) {
+        uart_print("[USER] usermode enter not supported on this architecture.\n");
+        return -1;
+    }
+
+    return 0;
+#else
+    return -1;
+#endif
+}
+
+void arch_platform_usermode_test_start(void) {
+#if defined(__i386__)
+    x86_usermode_test_start();
+#endif
+}
index 0ee7a411fb75e1db3fddaba85dc8a5cf055b8fdc..cfc292a44b024c05f7fe8b01dd857a8b01b0a04e 100644 (file)
 #include "timer.h"
 #include "initrd.h"
 #include "fs.h"
- #include "elf.h"
- #include "uaccess.h"
 
 #include "kernel/boot_info.h"
 
 #include "syscall.h"
 
+#include "arch/arch_platform.h"
+
 #include "hal/cpu.h"
 #include "hal/mm.h"
-#include "hal/usermode.h"
-
-#if defined(__i386__)
-extern void x86_usermode_test_start(void);
-
-static uint8_t ring0_trap_stack[16384] __attribute__((aligned(16)));
 
 static int cmdline_has_token(const char* cmdline, const char* token) {
     if (!cmdline || !token) return 0;
@@ -48,7 +42,6 @@ static int cmdline_has_token(const char* cmdline, const char* token) {
 
     return 0;
 }
-#endif
 
 /* Check if the compiler thinks we are targeting the wrong operating system. */
 #if defined(__linux__)
@@ -68,21 +61,13 @@ void kernel_main(const struct boot_info* bi) {
     
     // 3. Initialize Virtual Memory Manager
     uart_print("[AdrOS] Initializing VMM...\n");
-#if defined(__i386__)
-    vmm_init(); 
+    if (arch_platform_setup(bi) < 0) {
+        uart_print("[WARN] VMM/IDT/Sched not implemented for this architecture yet.\n");
+        goto done;
+    }
 
-    // VGA console depends on higher-half mapping (VMM)
-    vga_init();
-    vga_set_color(0x0A, 0x00);
-    vga_print("[AdrOS] Kernel Initialized (VGA).\n");
-    
     // 4. Initialize Kernel Heap
     kheap_init();
-
-    syscall_init();
-    
-    // 6. Initialize Drivers
-    keyboard_init();
     
     // 7. Initialize Multitasking
     uart_print("[AdrOS] Initializing Scheduler...\n");
@@ -104,38 +89,16 @@ void kernel_main(const struct boot_info* bi) {
         }
     }
 
-#if defined(__i386__)
-    if (fs_root) {
-        uintptr_t entry = 0;
-        uintptr_t user_sp = 0;
-        if (elf32_load_user_from_initrd("init.elf", &entry, &user_sp) == 0) {
-            uart_print("[ELF] starting init.elf\n");
-
-            uart_print("[ELF] user_range_ok(entry)=");
-            uart_put_char(user_range_ok((const void*)entry, 1) ? '1' : '0');
-            uart_print(" user_range_ok(stack)=");
-            uart_put_char(user_range_ok((const void*)(user_sp - 16), 16) ? '1' : '0');
-            uart_print("\n");
-
-            hal_cpu_set_kernel_stack((uintptr_t)&ring0_trap_stack[sizeof(ring0_trap_stack)]);
-            if (hal_usermode_enter(entry, user_sp) < 0) {
-                uart_print("[USER] usermode enter not supported on this architecture.\n");
-            }
-        }
-    }
+    (void)arch_platform_start_userspace(bi);
 
     if (bi && cmdline_has_token(bi->cmdline, "ring3")) {
-        x86_usermode_test_start();
+        arch_platform_usermode_test_start();
     }
-#endif
     
     // Start Shell as the main interaction loop
     shell_init();
     
-#else
-    uart_print("[WARN] VMM/IDT/Sched not implemented for this architecture yet.\n");
-#endif
-
+done:
     uart_print("Welcome to AdrOS (x86/ARM/RISC-V/MIPS)!\n");
 
     // Infinite loop acting as Idle Task