Unifying the Lunaix's Physical Memory Model (#28)
[lunaix-os.git] / lunaix-os / kernel / kinit.c
index 2d911bd1318de4d885bf7d506baf7abfad3530bc..528a73867e7c189dd3f36db8092ec85bd6aedfa4 100644 (file)
@@ -7,7 +7,6 @@
 #include <lunaix/input.h>
 #include <lunaix/mm/cake.h>
 #include <lunaix/mm/mmio.h>
-#include <lunaix/mm/page.h>
 #include <lunaix/mm/pmm.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/mm/vmm.h>
@@ -23,7 +22,7 @@
 #include <hal/intc.h>
 
 #include <sys/abi.h>
-#include <sys/mm/mempart.h>
+#include <sys/mm/mm_defs.h>
 
 #include <klibc/strfmt.h>
 #include <klibc/string.h>
@@ -37,15 +36,22 @@ kmem_init(struct boot_handoff* bhctx);
 void
 kernel_bootstrap(struct boot_handoff* bhctx)
 {
-    pmm_init(bhctx->mem.size);
     vmm_init();
 
+    pmm_init(bhctx);
+    // now we can start reserving physical space
+
     /* Begin kernel bootstrapping sequence */
     boot_begin(bhctx);
 
+    tty_init(ioremap(0xB8000, PAGE_SIZE));
+    
     /* Setup kernel memory layout and services */
     kmem_init(bhctx);
 
+    // FIXME this goes to hal/gfxa
+    tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
+
     boot_parse_cmdline(bhctx);
 
     /* Prepare stack trace environment */
@@ -55,10 +61,6 @@ kernel_bootstrap(struct boot_handoff* bhctx)
 
     invoke_init_function(on_earlyboot);
 
-    // FIXME this goes to hal/gfxa
-    tty_init(ioremap(0xB8000, PG_SIZE));
-    tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
-
     device_sysconf_load();
 
     /* Get intc online, this is the cornerstone when initing devices */
@@ -94,6 +96,7 @@ kernel_bootstrap(struct boot_handoff* bhctx)
      * and start geting into uspace
      */
     boot_end(bhctx);
+    boot_cleanup();
 
     spawn_lunad();
 }
@@ -122,15 +125,20 @@ spawn_lunad()
 void
 kmem_init(struct boot_handoff* bhctx)
 {
-    extern u8_t __kexec_end;
-    // 将内核占据的页,包括前1MB,hhk_init 设为已占用
-    size_t pg_count = ((ptr_t)&__kexec_end - KERNEL_EXEC) >> PG_SIZE_BITS;
-    pmm_mark_chunk_occupied(0, pg_count, PP_FGLOCKED);
-
-    // reserve higher half
-    for (size_t i = L1_INDEX(KERNEL_EXEC); i < 1023; i++) {
-        assert(vmm_set_mapping(VMS_SELF, i << 22, 0, 0, VMAP_NOMAP));
-    }
+    pte_t* ptep = mkptep_va(VMS_SELF, KERNEL_RESIDENT);
+    ptep = mkl0tep(ptep);
+
+    do {
+#if   LnT_ENABLED(1)
+        assert(mkl1t(ptep++, 0, KERNEL_DATA));
+#elif LnT_ENABLED(2)
+        assert(mkl2t(ptep++, 0, KERNEL_DATA));
+#elif LnT_ENABLED(3)
+        assert(mkl3t(ptep++, 0, KERNEL_DATA));
+#else
+        assert(mklft(ptep++, 0, KERNEL_DATA));
+#endif
+    } while (ptep_vfn(ptep) < MAX_PTEN - 2);
 
     // allocators
     cake_init();