Boot framework rework (#45)
[lunaix-os.git] / lunaix-os / arch / x86 / mm / pmm.c
index aa35b86b99b4e167ef1a8efa36348e91760b38eb..73f9fb16bd2a333c4028c773a2f1be7ea9450255 100644 (file)
@@ -1,7 +1,6 @@
 #include <lunaix/mm/page.h>
 #include <lunaix/mm/pagetable.h>
-
-extern unsigned int __kexec_end[];
+#include <lunaix/sections.h>
 
 void
 pmm_arch_init_pool(struct pmem* memory)
@@ -12,13 +11,15 @@ pmm_arch_init_pool(struct pmem* memory)
 ptr_t
 pmm_arch_init_remap(struct pmem* memory, struct boot_handoff* bctx)
 {
-    size_t ppfn_total = pfn(bctx->mem.size) + 1;
+    size_t ppfn_total = pfn(bctx->mem.size);
     size_t pool_size  = ppfn_total * sizeof(struct ppage);
     
     size_t i = 0;
     struct boot_mmapent* ent;
-    for (; i < bctx->mem.mmap_len; i++) {
-        ent = &bctx->mem.mmap[i];
+
+restart:;
+    while (i < bctx->mem.mmap_len) {
+        ent = &bctx->mem.mmap[i++];
         if (free_memregion(ent) && ent->size > pool_size) {
             goto found;
         }
@@ -28,7 +29,7 @@ pmm_arch_init_remap(struct pmem* memory, struct boot_handoff* bctx)
     return 0;
 
 found:;
-    ptr_t kexec_end = to_kphysical(__kexec_end);
+    ptr_t kexec_end = to_kphysical(kernel_start);
     ptr_t aligned_pplist = MAX(ent->start, kexec_end);
 
 #ifdef CONFIG_ARCH_X86_64
@@ -38,7 +39,7 @@ found:;
 #endif
 
     if (aligned_pplist + pool_size > ent->start + ent->size) {
-        return 0;
+        goto restart;
     }
 
     // for x86_32, the upper bound of memory requirement for pplist