Unifying the Lunaix's Physical Memory Model (#28)
[lunaix-os.git] / lunaix-os / kernel / kinit.c
index 6cb5871cc284c7de0a5e8255c90a635803781e16..528a73867e7c189dd3f36db8092ec85bd6aedfa4 100644 (file)
-#include <lunaix/common.h>
+#include <lunaix/types.h>
+#include <lunaix/block.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/isrm.h>
-#include <lunaix/lxconsole.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>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/spike.h>
-#include <lunaix/syscall.h>
+#include <lunaix/trace.h>
 #include <lunaix/tty/tty.h>
-#include <lunaix/types.h>
+#include <lunaix/owloysius.h>
+#include <lunaix/pcontext.h>
+
+#include <hal/acpi/acpi.h>
+#include <hal/intc.h>
 
 #include <sys/abi.h>
-#include <sys/boot/multiboot.h>
-#include <sys/interrupts.h>
+#include <sys/mm/mm_defs.h>
 
-#include <klibc/stdio.h>
+#include <klibc/strfmt.h>
 #include <klibc/string.h>
 
-extern u8_t __kernel_start;
-extern u8_t __kernel_end;
-extern u8_t __init_hhk_end;
+void
+spawn_lunad();
 
-#define PP_KERN_SHARED (PP_FGSHARED | PP_TKERN)
+void
+kmem_init(struct boot_handoff* bhctx);
 
-// Set remotely by kernel/asm/x86/prologue.S
-multiboot_info_t* _k_init_mb_info;
+void
+kernel_bootstrap(struct boot_handoff* bhctx)
+{
+    vmm_init();
 
-x86_page_table* __kernel_ptd;
+    pmm_init(bhctx);
+    // now we can start reserving physical space
 
-extern void
-__proc0(); /* proc0.c */
+    /* Begin kernel bootstrapping sequence */
+    boot_begin(bhctx);
 
-void
-spawn_proc0();
+    tty_init(ioremap(0xB8000, PAGE_SIZE));
+    
+    /* Setup kernel memory layout and services */
+    kmem_init(bhctx);
 
-void
-setup_memory(multiboot_memory_map_t* map, size_t map_size);
+    // FIXME this goes to hal/gfxa
+    tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
 
-void
-_kernel_pre_init()
-{
-    // interrupts
-    exception_init();
+    boot_parse_cmdline(bhctx);
 
-    // memory
-    pmm_init(MEM_1MB + (_k_init_mb_info->mem_upper << 10));
-    vmm_init();
+    /* Prepare stack trace environment */
+    trace_modksyms_init(bhctx);
 
-    unsigned int map_size =
-      _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
+    device_scan_drivers();
 
-    setup_memory((multiboot_memory_map_t*)_k_init_mb_info->mmap_addr, map_size);
-}
+    invoke_init_function(on_earlyboot);
 
-void
-_kernel_init()
-{
-    int errno = 0;
+    device_sysconf_load();
 
-    // allocators
-    cake_init();
-    valloc_init();
+    /* Get intc online, this is the cornerstone when initing devices */
+    intc_init();
 
-    sched_init();
+    clock_init();
+    timer_init();
 
-    // crt
-    tty_init(ioremap(VGA_FRAMEBUFFER, PG_SIZE));
-    tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
+    /*
+        TODO autoload these init function that do not have dependency between
+       them
+    */
 
-    // file system & device subsys
+    /* Let's get fs online as soon as possible, as things rely on them */
     vfs_init();
     fsm_init();
     input_init();
+    block_init();
+    sched_init();
 
-    vfs_export_attributes();
-
-    lxconsole_init();
+    device_onboot_load();
 
-    if ((errno = vfs_mount_root("ramfs", NULL))) {
-        panickf("Fail to mount root. (errno=%d)", errno);
-    }
+    /* the bare metal are now happy, let's get software over with */
 
-    vfs_mount("/dev", "devfs", NULL, 0);
-    vfs_mount("/sys", "twifs", NULL, MNT_RO);
-    vfs_mount("/task", "taskfs", NULL, MNT_RO);
+    must_success(vfs_mount_root("ramfs", NULL));
+    must_success(vfs_mount("/dev", "devfs", NULL, 0));
+    
+    invoke_init_function(on_boot);
 
-    lxconsole_spawn_ttydev();
-    device_init_builtin();
+    must_success(vfs_unmount("/dev"));
 
-    syscall_install();
+    /* Finish up bootstrapping sequence, we are ready to spawn the root process
+     * and start geting into uspace
+     */
+    boot_end(bhctx);
+    boot_cleanup();
 
-    spawn_proc0();
+    spawn_lunad();
 }
 
+extern void
+lunad_main();
+
 /**
- * @brief 创建并运行proc0进程
+ * @brief 创建并运行Lunaix守护进程
  *
  */
 void
-spawn_proc0()
+spawn_lunad()
 {
-    struct proc_info* proc0 = alloc_process();
-
-    /**
-     * @brief
-     * 注意:这里和视频中说的不一样,属于我之后的一点微调。
-     * 在视频中,spawn_proc0是在_kernel_post_init的末尾才调用的。并且是直接跳转到_proc0
-     *
-     * 但是我后来发现,上述的方法会产生竞态条件。这是因为spawn_proc0被调用的时候,时钟中断已经开启,
-     * 而中断的产生会打乱栈的布局,从而使得下面的上下文设置代码产生未定义行为(Undefined
-     * Behaviour)。 为了保险起见,有两种办法:
-     *      1. 在创建proc0进程前关闭中断
-     *      2. 将_kernel_post_init搬进proc0进程
-     * (_kernel_post_init已经更名为init_platform)
-     *
-     * 目前的解决方案是2
-     */
-
-    proc0->parent = proc0;
-
-    // 方案1:必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
-    // cpu_disable_interrupt();
-
-    /* Ok... 首先fork进我们的零号进程,而后由那里,我们fork进init进程。 */
-
-    // 把当前虚拟地址空间(内核)复制一份。
-    proc0->page_table = vmm_dup_vmspace(proc0->pid);
-
-    // 直接切换到新的拷贝,进行配置。
-    cpu_chvmspace(proc0->page_table);
-
-    // 为内核创建一个专属栈空间。
-    for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) {
-        ptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
-        vmm_set_mapping(VMS_SELF,
-                        KSTACK_START + (i << PG_SIZE_BITS),
-                        pa,
-                        PG_PREM_RW,
-                        VMAP_NULL);
-    }
-
-    proc_init_transfer(proc0, KSTACK_TOP, (ptr_t)__proc0, 0);
-
-    // 向调度器注册进程。
-    commit_process(proc0);
-
-    // 由于时钟中断与APIC未就绪,我们需要手动进行第一次调度。这里也会同时隐式地恢复我们的eflags.IF位
-    proc0->state = PS_RUNNING;
-    asm volatile("pushl %0\n"
-                 "jmp switch_to\n" ::"r"(proc0));
-
-    /* Should not return */
-    assert_msg(0, "Unexpected Return");
+    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");
 }
 
-// 按照 Memory map 标识可用的物理页
 void
-setup_memory(multiboot_memory_map_t* map, size_t map_size)
+kmem_init(struct boot_handoff* bhctx)
 {
+    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);
 
-    // First pass, to mark the physical pages
-    for (unsigned int i = 0; i < map_size; i++) {
-        multiboot_memory_map_t mmap = map[i];
-        if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
-            // 整数向上取整除法
-            ptr_t pg = map[i].addr_low + 0x0fffU;
-            pmm_mark_chunk_free(pg >> PG_SIZE_BITS,
-                                map[i].len_low >> PG_SIZE_BITS);
-        }
-    }
-
-    // 将内核占据的页,包括前1MB,hhk_init 设为已占用
-    size_t pg_count = V2P(&__kernel_end) >> PG_SIZE_BITS;
-    pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED);
-
-    // reserve higher half
-    for (size_t i = L1_INDEX(KERNEL_MM_BASE); i < 1023; i++) {
-        assert(vmm_set_mapping(VMS_SELF, i << 22, 0, 0, VMAP_NOMAP));
-    }
+    // allocators
+    cake_init();
+    valloc_init();
 }