A Total Overhaul on the Lunaix's Virtual Memory Model (#26)
[lunaix-os.git] / lunaix-os / kernel / boot_helper.c
index fa87bff5fae8777df9e121dbc998c85eacd74065..f7ad1584d6c2cfe2b1b84f5ab15174adbb426dbf 100644 (file)
@@ -1,10 +1,10 @@
 #include <klibc/string.h>
 #include <lunaix/boot_generic.h>
 #include <klibc/string.h>
 #include <lunaix/boot_generic.h>
-#include <lunaix/mm/page.h>
 #include <lunaix/mm/pmm.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/spike.h>
 #include <lunaix/mm/pmm.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/spike.h>
-#include <sys/mm/mempart.h>
+#include <lunaix/kcmd.h>
+#include <sys/mm/mm_defs.h>
 
 /**
  * @brief Reserve memory for kernel bootstrapping initialization
 
 /**
  * @brief Reserve memory for kernel bootstrapping initialization
@@ -15,26 +15,36 @@ void
 boot_begin(struct boot_handoff* bhctx)
 {
     bhctx->prepare(bhctx);
 boot_begin(struct boot_handoff* bhctx)
 {
     bhctx->prepare(bhctx);
+    
+    // Identity-map the first 3GiB address spaces
+    pte_t* ptep  = mkl0tep(mkptep_va(VMS_SELF, 0));
+    pte_t pte    = mkpte_prot(KERNEL_DATA);
+    size_t count = page_count(KERNEL_RESIDENT, L0T_SIZE);
+
+    vmm_set_ptes_contig(ptep, pte_mkhuge(pte), L0T_SIZE, count);
 
     struct boot_mmapent *mmap = bhctx->mem.mmap, *mmapent;
     for (size_t i = 0; i < bhctx->mem.mmap_len; i++) {
         mmapent = &mmap[i];
 
     struct boot_mmapent *mmap = bhctx->mem.mmap, *mmapent;
     for (size_t i = 0; i < bhctx->mem.mmap_len; i++) {
         mmapent = &mmap[i];
-        size_t size_pg = PN(ROUNDUP(mmapent->size, PG_SIZE));
+        size_t size_pg = leaf_count(mmapent->size);
+        pfn_t start_pfn = pfn(mmapent->start);
 
         if (mmapent->type == BOOT_MMAP_FREE) {
 
         if (mmapent->type == BOOT_MMAP_FREE) {
-            pmm_mark_chunk_free(PN(mmapent->start), size_pg);
+            pmm_mark_chunk_free(start_pfn, size_pg);
             continue;
         }
             continue;
         }
+    }
 
 
-        ptr_t pa = PG_ALIGN(mmapent->start);
-        for (size_t j = 0; j < size_pg && pa < KERNEL_EXEC;
-             j++, pa += PM_PAGE_SIZE) {
-            vmm_set_mapping(VMS_SELF, pa, pa, PG_PREM_RW, VMAP_IGNORE);
-        }
+    /* Reserve region for all loaded modules */
+    for (size_t i = 0; i < bhctx->mods.mods_num; i++) {
+        struct boot_modent* mod = &bhctx->mods.entries[i];
+        unsigned int counts = leaf_count(mod->end - mod->start);
+
+        pmm_mark_chunk_occupied(pfn(mod->start), counts, PP_FGLOCKED);
     }
 }
 
     }
 }
 
-extern u8_t __kexec_boot_end; /* link/linker.ld */
+extern u8_t __kboot_end; /* link/linker.ld */
 
 /**
  * @brief Release memory for kernel bootstrapping initialization
 
 /**
  * @brief Release memory for kernel bootstrapping initialization
@@ -47,20 +57,14 @@ boot_end(struct boot_handoff* bhctx)
     struct boot_mmapent *mmap = bhctx->mem.mmap, *mmapent;
     for (size_t i = 0; i < bhctx->mem.mmap_len; i++) {
         mmapent = &mmap[i];
     struct boot_mmapent *mmap = bhctx->mem.mmap, *mmapent;
     for (size_t i = 0; i < bhctx->mem.mmap_len; i++) {
         mmapent = &mmap[i];
-        size_t size_pg = PN(ROUNDUP(mmapent->size, PG_SIZE));
-
-        if (mmapent->start >= KERNEL_EXEC || mmapent->type == BOOT_MMAP_FREE) {
-            continue;
-        }
+        size_t size_pg = leaf_count(mmapent->size);
 
         if (mmapent->type == BOOT_MMAP_RCLM) {
 
         if (mmapent->type == BOOT_MMAP_RCLM) {
-            pmm_mark_chunk_free(PN(mmapent->start), size_pg);
+            pmm_mark_chunk_free(pfn(mmapent->start), size_pg);
         }
 
         }
 
-        ptr_t pa = PG_ALIGN(mmapent->start);
-        for (size_t j = 0; j < size_pg && pa < KERNEL_EXEC;
-             j++, pa += PM_PAGE_SIZE) {
-            vmm_del_mapping(VMS_SELF, pa);
+        if (mmapent->type == BOOT_MMAP_FREE) {
+            continue;
         }
     }
 
         }
     }
 
@@ -74,9 +78,16 @@ boot_end(struct boot_handoff* bhctx)
 void
 boot_cleanup()
 {
 void
 boot_cleanup()
 {
-    // clean up
-    for (size_t i = 0; i < (ptr_t)(&__kexec_boot_end); i += PG_SIZE) {
-        vmm_del_mapping(VMS_SELF, (ptr_t)i);
-        pmm_free_page(KERNEL_PID, (ptr_t)i);
+    pte_t* ptep  = mkl0tep(mkptep_va(VMS_SELF, 0));
+    size_t count = page_count(KERNEL_RESIDENT, L0T_SIZE);
+    vmm_unset_ptes(ptep, count);
+}
+
+void
+boot_parse_cmdline(struct boot_handoff* bhctx) {
+    if (!bhctx->kexec.len) {
+        return;
     }
     }
+
+    kcmd_parse_cmdline(bhctx->kexec.cmdline);
 }
\ No newline at end of file
 }
\ No newline at end of file