Boot framework rework (#45)
[lunaix-os.git] / lunaix-os / kernel / kinit.c
index ca28001f8bc66b255604297d727267455743ea3d..a08418303f030c558e36a425d1172990eb9d5722 100644 (file)
@@ -3,22 +3,23 @@
 #include <lunaix/boot_generic.h>
 #include <lunaix/device.h>
 #include <lunaix/foptions.h>
 #include <lunaix/boot_generic.h>
 #include <lunaix/device.h>
 #include <lunaix/foptions.h>
-#include <lunaix/fs/twifs.h>
 #include <lunaix/input.h>
 #include <lunaix/mm/cake.h>
 #include <lunaix/input.h>
 #include <lunaix/mm/cake.h>
-#include <lunaix/mm/mmio.h>
 #include <lunaix/mm/pmm.h>
 #include <lunaix/mm/pmm.h>
+#include <lunaix/mm/page.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/spike.h>
 #include <lunaix/trace.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/spike.h>
 #include <lunaix/trace.h>
-#include <lunaix/tty/tty.h>
 #include <lunaix/owloysius.h>
 #include <lunaix/hart_state.h>
 #include <lunaix/owloysius.h>
 #include <lunaix/hart_state.h>
+#include <lunaix/syslog.h>
+#include <lunaix/sections.h>
 
 #include <hal/acpi/acpi.h>
 
 #include <hal/acpi/acpi.h>
+#include <hal/devtree.h>
 
 #include <sys/abi.h>
 #include <sys/mm/mm_defs.h>
 
 #include <sys/abi.h>
 #include <sys/mm/mm_defs.h>
 #include <klibc/strfmt.h>
 #include <klibc/string.h>
 
 #include <klibc/strfmt.h>
 #include <klibc/string.h>
 
-void
-spawn_lunad();
+LOG_MODULE("kinit")
 
 
-void
-kmem_init(struct boot_handoff* bhctx);
+extern void
+lunad_main();
+
+/**
+ * @brief 创建并运行Lunaix守护进程
+ *
+ */
+static void
+spawn_lunad()
+{
+    int has_error;
+    struct thread* kthread;
+    
+    has_error = spawn_process(&kthread, (ptr_t)lunad_main, false);
+    assert_msg(!has_error, "failed to spawn lunad");
+
+    run(kthread);
+    
+    fail("Unexpected Return");
+}
+
+static void
+kmem_init(struct boot_handoff* bhctx)
+{
+    pte_t* ptep = mkptep_va(VMS_SELF, KERNEL_RESIDENT);
+
+    ptep = mkl0tep(ptep);
+
+    unsigned int i = ptep_vfn(ptep);
+    do {
+        if (l0tep_implie_vmnts(ptep)) {
+            ptep++;
+            continue;
+        }
+
+#if   LnT_ENABLED(1)
+        assert(mkl1t(ptep++, 0, KERNEL_PGTAB));
+#elif LnT_ENABLED(2)
+        assert(mkl2t(ptep++, 0, KERNEL_PGTAB));
+#elif LnT_ENABLED(3)
+        assert(mkl3t(ptep++, 0, KERNEL_PGTAB));
+#else
+        assert(mklft(ptep++, 0, KERNEL_PGTAB));
+#endif
+    } while (++i < MAX_PTEN);
+
+    // allocators
+    cake_init();
+    valloc_init();
+}
+
+static void
+__remap_and_load_dtb(struct boot_handoff* bhctx)
+{
+#ifdef CONFIG_USE_DEVICETREE
+    ptr_t dtb = bhctx->kexec.dtb_pa;
+
+    if (!dtb) {
+        return;
+    }
+
+    if (va_offset(dtb)) {
+        WARN("bad-aligned dtb location, expect page aligned");
+        return;
+    }
+
+    pte_t *ptep, pte;
+    size_t nr_pages;
+    bool loaded;
+    
+    pte  = mkpte(dtb, KERNEL_DATA);
+    ptep = mkptep_va(VMS_SELF, dtb_start);
+    nr_pages = leaf_count(CONFIG_DTB_MAXSIZE);
+
+    pmm_onhold_range(dtb, nr_pages);
+    vmm_set_ptes_contig(ptep, pte, PAGE_SIZE, nr_pages);
+
+    loaded = dt_load(dtb_start);
+    if (!loaded) {
+        ERROR("dtb load failed");
+    }
+#endif
+
+    return;
+}
 
 void
 kernel_bootstrap(struct boot_handoff* bhctx)
 
 void
 kernel_bootstrap(struct boot_handoff* bhctx)
@@ -43,13 +126,10 @@ kernel_bootstrap(struct boot_handoff* bhctx)
     /* Begin kernel bootstrapping sequence */
     boot_begin(bhctx);
 
     /* Begin kernel bootstrapping sequence */
     boot_begin(bhctx);
 
-    tty_init((void*)ioremap(0xB8000, PAGE_SIZE));
-    
     /* Setup kernel memory layout and services */
     kmem_init(bhctx);
 
     /* Setup kernel memory layout and services */
     kmem_init(bhctx);
 
-    // FIXME this goes to hal/gfxa
-    tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
+    __remap_and_load_dtb(bhctx);
 
     boot_parse_cmdline(bhctx);
 
 
     boot_parse_cmdline(bhctx);
 
@@ -86,8 +166,6 @@ kernel_bootstrap(struct boot_handoff* bhctx)
     
     invoke_init_function(on_boot);
 
     
     invoke_init_function(on_boot);
 
-    must_success(vfs_unmount("/dev"));
-
     /* Finish up bootstrapping sequence, we are ready to spawn the root process
      * and start geting into uspace
      */
     /* Finish up bootstrapping sequence, we are ready to spawn the root process
      * and start geting into uspace
      */
@@ -96,53 +174,3 @@ kernel_bootstrap(struct boot_handoff* bhctx)
     spawn_lunad();
 }
 
     spawn_lunad();
 }
 
-extern void
-lunad_main();
-
-/**
- * @brief 创建并运行Lunaix守护进程
- *
- */
-void
-spawn_lunad()
-{
-    int has_error;
-    struct thread* kthread;
-    
-    has_error = spawn_process(&kthread, (ptr_t)lunad_main, false);
-    assert_msg(!has_error, "failed to spawn lunad");
-
-    run(kthread);
-    
-    fail("Unexpected Return");
-}
-
-void
-kmem_init(struct boot_handoff* bhctx)
-{
-    pte_t* ptep = mkptep_va(VMS_SELF, KERNEL_RESIDENT);
-
-    ptep = mkl0tep(ptep);
-
-    unsigned int i = ptep_vfn(ptep);
-    do {
-        if (l0tep_impile_vmnts(ptep)) {
-            ptep++;
-            continue;
-        }
-
-#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 (++i < MAX_PTEN);
-
-    // allocators
-    cake_init();
-    valloc_init();
-}