]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
x86/boot: shrink identity map to 16MB; map initrd via VMM
authorTulio A M Mendes <[email protected]>
Fri, 6 Feb 2026 11:54:59 +0000 (08:54 -0300)
committerTulio A M Mendes <[email protected]>
Fri, 6 Feb 2026 11:54:59 +0000 (08:54 -0300)
src/arch/x86/boot.S
src/kernel/main.c
src/kernel/scheduler.c

index 993d770e17bd4e65c26e03e0ec366755aef400ce..dfc68be116da5f1d72c173655539e6662f6524d8 100644 (file)
@@ -61,11 +61,12 @@ _start:
      */
 
     /*
-     * Map 0-64MB using 16 page tables.
-     * This keeps enough low physical memory identity-mapped for bring-up.
+     * Map 0-16MB using 4 page tables.
+     * With Multiboot2 info copied in arch_start and initrd mapped via VMM,
+     * we only need a small identity window for early bring-up.
      */
 
-    /* Fill PTs (boot_pt0..boot_pt15) */
+    /* Fill PTs (boot_pt0..boot_pt3) */
     mov $boot_pt0, %edi
     sub $KERNEL_VIRT_BASE, %edi /* Physical address of PT0 */
     xor %ebx, %ebx              /* pt_index = 0 */
@@ -86,14 +87,14 @@ _start:
     loop 2b
 
     inc %ebx
-    cmp $16, %ebx
+    cmp $4, %ebx
     jne 1b
 
     /* 3. Get Physical Address of Page Directory */
     mov $boot_pd, %edi
     sub $KERNEL_VIRT_BASE, %edi
 
-    /* Link PT0..PT15 into PD for both identity and higher-half mapping */
+    /* Link PT0..PT3 into PD for both identity and higher-half mapping */
     mov $boot_pt0, %edx
     sub $KERNEL_VIRT_BASE, %edx /* pt_phys = physical address of PT0 */
     mov $0, %ebx                /* i = 0 */
@@ -105,7 +106,7 @@ _start:
     mov %eax, 3072(%edi,%ebx,4)     /* PD[768+i] */
     add $4096, %edx
     inc %ebx
-    cmp $16, %ebx
+    cmp $4, %ebx
     jne 3b
 
     /* 6. Recursive Mapping (Optional, good for VMM later) at index 1023 */
@@ -176,7 +177,7 @@ boot_pd:
     .skip 4096
 .global boot_pt0
 boot_pt0:
-    .skip 4096*16
+    .skip 4096*4
 
 .align 16
 .global arch_boot_args
index 4b9fc7ca893d2005a2a6659ab46c0d0393ae4bf4..d2f1cfe31253c0c7032056436e950208e43530e2 100644 (file)
@@ -67,7 +67,26 @@ void kernel_main(const struct boot_info* bi) {
 
     // 9. Load InitRD (if available)
     if (bi && bi->initrd_start) {
-        fs_root = initrd_init((uint32_t)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 = initrd_virt_base + ((uintptr_t)bi->initrd_start - phys_start);
+        fs_root = initrd_init((uint32_t)initrd_virt);
     }
     
     // Start Shell as the main interaction loop
index 5ef82ef1d72d8e8f0846a6f1d1fcccd7d57b5984..9a5b43905812f7ccb6a2489b42abe9d561b0f8a2 100644 (file)
@@ -21,7 +21,7 @@ static void* pmm_alloc_page_low(void) {
         void* p = pmm_alloc_page();
         if (!p) return 0;
         // boot.S currently identity-maps a large low window; keep allocations within it.
-        if ((uint32_t)p < 0x04000000) {
+        if ((uint32_t)p < 0x01000000) {
             return p;
         }
         // Not safe to touch yet; put it back.