]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
kernel: move initrd mapping and usermode entry behind HAL
authorTulio A M Mendes <[email protected]>
Sat, 7 Feb 2026 16:08:30 +0000 (13:08 -0300)
committerTulio A M Mendes <[email protected]>
Sat, 7 Feb 2026 16:08:30 +0000 (13:08 -0300)
include/hal/mm.h [new file with mode: 0644]
include/hal/usermode.h [new file with mode: 0644]
src/hal/arm/mm.c [new file with mode: 0644]
src/hal/arm/usermode.c [new file with mode: 0644]
src/hal/mips/mm.c [new file with mode: 0644]
src/hal/mips/usermode.c [new file with mode: 0644]
src/hal/riscv/mm.c [new file with mode: 0644]
src/hal/riscv/usermode.c [new file with mode: 0644]
src/hal/x86/mm.c [new file with mode: 0644]
src/hal/x86/usermode.c [new file with mode: 0644]
src/kernel/main.c

diff --git a/include/hal/mm.h b/include/hal/mm.h
new file mode 100644 (file)
index 0000000..7b89eff
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef HAL_MM_H
+#define HAL_MM_H
+
+#include <stdint.h>
+
+#define HAL_MM_MAP_RW  (1u << 0)
+
+int hal_mm_map_physical_range(uintptr_t phys_start, uintptr_t phys_end, uint32_t flags, uintptr_t* out_virt);
+
+#endif
diff --git a/include/hal/usermode.h b/include/hal/usermode.h
new file mode 100644 (file)
index 0000000..6a8062c
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef HAL_USERMODE_H
+#define HAL_USERMODE_H
+
+#include <stdint.h>
+
+int hal_usermode_enter(uintptr_t user_eip, uintptr_t user_esp);
+
+#endif
diff --git a/src/hal/arm/mm.c b/src/hal/arm/mm.c
new file mode 100644 (file)
index 0000000..a11e99a
--- /dev/null
@@ -0,0 +1,9 @@
+#include "hal/mm.h"
+
+int hal_mm_map_physical_range(uintptr_t phys_start, uintptr_t phys_end, uint32_t flags, uintptr_t* out_virt) {
+    (void)phys_start;
+    (void)phys_end;
+    (void)flags;
+    (void)out_virt;
+    return -1;
+}
diff --git a/src/hal/arm/usermode.c b/src/hal/arm/usermode.c
new file mode 100644 (file)
index 0000000..f58ba73
--- /dev/null
@@ -0,0 +1,7 @@
+#include "hal/usermode.h"
+
+int hal_usermode_enter(uintptr_t user_eip, uintptr_t user_esp) {
+    (void)user_eip;
+    (void)user_esp;
+    return -1;
+}
diff --git a/src/hal/mips/mm.c b/src/hal/mips/mm.c
new file mode 100644 (file)
index 0000000..a11e99a
--- /dev/null
@@ -0,0 +1,9 @@
+#include "hal/mm.h"
+
+int hal_mm_map_physical_range(uintptr_t phys_start, uintptr_t phys_end, uint32_t flags, uintptr_t* out_virt) {
+    (void)phys_start;
+    (void)phys_end;
+    (void)flags;
+    (void)out_virt;
+    return -1;
+}
diff --git a/src/hal/mips/usermode.c b/src/hal/mips/usermode.c
new file mode 100644 (file)
index 0000000..f58ba73
--- /dev/null
@@ -0,0 +1,7 @@
+#include "hal/usermode.h"
+
+int hal_usermode_enter(uintptr_t user_eip, uintptr_t user_esp) {
+    (void)user_eip;
+    (void)user_esp;
+    return -1;
+}
diff --git a/src/hal/riscv/mm.c b/src/hal/riscv/mm.c
new file mode 100644 (file)
index 0000000..a11e99a
--- /dev/null
@@ -0,0 +1,9 @@
+#include "hal/mm.h"
+
+int hal_mm_map_physical_range(uintptr_t phys_start, uintptr_t phys_end, uint32_t flags, uintptr_t* out_virt) {
+    (void)phys_start;
+    (void)phys_end;
+    (void)flags;
+    (void)out_virt;
+    return -1;
+}
diff --git a/src/hal/riscv/usermode.c b/src/hal/riscv/usermode.c
new file mode 100644 (file)
index 0000000..f58ba73
--- /dev/null
@@ -0,0 +1,7 @@
+#include "hal/usermode.h"
+
+int hal_usermode_enter(uintptr_t user_eip, uintptr_t user_esp) {
+    (void)user_eip;
+    (void)user_esp;
+    return -1;
+}
diff --git a/src/hal/x86/mm.c b/src/hal/x86/mm.c
new file mode 100644 (file)
index 0000000..e9be2ef
--- /dev/null
@@ -0,0 +1,33 @@
+#include "hal/mm.h"
+
+#include "vmm.h"
+
+#include <stddef.h>
+
+int hal_mm_map_physical_range(uintptr_t phys_start, uintptr_t phys_end, uint32_t flags, uintptr_t* out_virt) {
+    if (!out_virt) return -1;
+
+    if (phys_end < phys_start) phys_end = phys_start;
+
+    const uintptr_t virt_base = 0xE0000000U;
+
+    uintptr_t phys_start_aligned = phys_start & ~(uintptr_t)0xFFF;
+    uintptr_t phys_end_aligned = (phys_end + 0xFFF) & ~(uintptr_t)0xFFF;
+
+    uintptr_t size = phys_end_aligned - phys_start_aligned;
+    uintptr_t pages = size >> 12;
+
+    uint32_t vmm_flags = VMM_FLAG_PRESENT;
+    if (flags & HAL_MM_MAP_RW) vmm_flags |= VMM_FLAG_RW;
+
+    uintptr_t virt = virt_base;
+    uintptr_t phys = phys_start_aligned;
+    for (uintptr_t i = 0; i < pages; i++) {
+        vmm_map_page((uint64_t)phys, (uint64_t)virt, vmm_flags);
+        phys += 0x1000;
+        virt += 0x1000;
+    }
+
+    *out_virt = virt_base + (phys_start - phys_start_aligned);
+    return 0;
+}
diff --git a/src/hal/x86/usermode.c b/src/hal/x86/usermode.c
new file mode 100644 (file)
index 0000000..16219c8
--- /dev/null
@@ -0,0 +1,15 @@
+#include "hal/usermode.h"
+
+#if defined(__i386__)
+#include "arch/x86/usermode.h"
+#endif
+
+int hal_usermode_enter(uintptr_t user_eip, uintptr_t user_esp) {
+#if defined(__i386__)
+    x86_enter_usermode(user_eip, user_esp);
+#else
+    (void)user_eip;
+    (void)user_esp;
+    return -1;
+#endif
+}
index bab7a11103e84adde30e4b2744bee24436b1bb63..0ee7a411fb75e1db3fddaba85dc8a5cf055b8fdc 100644 (file)
@@ -11,7 +11,6 @@
 #include "shell.h"
 #include "heap.h"
 #include "timer.h"
-#include "multiboot2.h"
 #include "initrd.h"
 #include "fs.h"
  #include "elf.h"
 
 #include "kernel/boot_info.h"
 
-#include "gdt.h"
-
 #include "syscall.h"
 
 #include "hal/cpu.h"
-
-#if defined(__i386__)
-#include "arch/x86/usermode.h"
-#endif
+#include "hal/mm.h"
+#include "hal/usermode.h"
 
 #if defined(__i386__)
 extern void x86_usermode_test_start(void);
@@ -100,26 +95,13 @@ void kernel_main(const struct boot_info* bi) {
 
     // 9. Load InitRD (if available)
     if (bi && bi->initrd_start) {
-        const uintptr_t initrd_virt_base = 0xE0000000U;
-        uintptr_t phys_start = (uintptr_t)bi->initrd_start & ~(uintptr_t)0xFFF;
-        uintptr_t phys_end = (uintptr_t)bi->initrd_end;
-        if (phys_end < (uintptr_t)bi->initrd_start) {
-            phys_end = (uintptr_t)bi->initrd_start;
-        }
-        phys_end = (phys_end + 0xFFF) & ~(uintptr_t)0xFFF;
-
-        uintptr_t size = phys_end - phys_start;
-        uintptr_t pages = size >> 12;
-        uintptr_t virt = initrd_virt_base;
-        uintptr_t phys = phys_start;
-        for (uintptr_t i = 0; i < pages; i++) {
-            vmm_map_page((uint64_t)phys, (uint64_t)virt, VMM_FLAG_PRESENT | VMM_FLAG_RW);
-            phys += 0x1000;
-            virt += 0x1000;
+        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");
         }
-
-        uintptr_t initrd_virt = initrd_virt_base + ((uintptr_t)bi->initrd_start - phys_start);
-        fs_root = initrd_init((uint32_t)initrd_virt);
     }
 
 #if defined(__i386__)
@@ -136,7 +118,9 @@ void kernel_main(const struct boot_info* bi) {
             uart_print("\n");
 
             hal_cpu_set_kernel_stack((uintptr_t)&ring0_trap_stack[sizeof(ring0_trap_stack)]);
-            x86_enter_usermode(entry, user_sp);
+            if (hal_usermode_enter(entry, user_sp) < 0) {
+                uart_print("[USER] usermode enter not supported on this architecture.\n");
+            }
         }
     }