refactor: Optimize the context switch overhead
[lunaix-os.git] / lunaix-os / kernel / k_init.c
index b7c5668bd68ce2cb26bd9485381f0efe85e6effd..8c40b6b1df8953e7889ae8becb053729d69b8b42 100644 (file)
@@ -1,16 +1,20 @@
 #include <lunaix/common.h>
 #include <lunaix/common.h>
-#include <lunaix/tty/tty.h>
-
 #include <lunaix/device.h>
 #include <lunaix/foptions.h>
 #include <lunaix/input.h>
 #include <lunaix/device.h>
 #include <lunaix/foptions.h>
 #include <lunaix/input.h>
+#include <lunaix/isrm.h>
 #include <lunaix/lxconsole.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/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/mm/vmm.h>
 #include <lunaix/process.h>
 #include <lunaix/sched.h>
 #include <lunaix/spike.h>
+#include <lunaix/syscall.h>
+#include <lunaix/tty/tty.h>
 #include <lunaix/types.h>
 
 #include <arch/x86/boot/multiboot.h>
 #include <lunaix/types.h>
 
 #include <arch/x86/boot/multiboot.h>
@@ -20,9 +24,9 @@
 #include <klibc/stdio.h>
 #include <klibc/string.h>
 
 #include <klibc/stdio.h>
 #include <klibc/string.h>
 
-extern uint8_t __kernel_start;
-extern uint8_t __kernel_end;
-extern uint8_t __init_hhk_end;
+extern u8_t __kernel_start;
+extern u8_t __kernel_end;
+extern u8_t __init_hhk_end;
 
 #define PP_KERN_SHARED (PP_FGSHARED | PP_TKERN)
 
 
 #define PP_KERN_SHARED (PP_FGSHARED | PP_TKERN)
 
@@ -31,8 +35,6 @@ multiboot_info_t* _k_init_mb_info;
 
 x86_page_table* __kernel_ptd;
 
 
 x86_page_table* __kernel_ptd;
 
-struct proc_info tmp;
-
 extern void
 __proc0(); /* proc0.c */
 
 extern void
 __proc0(); /* proc0.c */
 
@@ -45,9 +47,12 @@ setup_memory(multiboot_memory_map_t* map, size_t map_size);
 void
 _kernel_pre_init()
 {
 void
 _kernel_pre_init()
 {
+    // interrupts
     _init_idt();
     _init_idt();
+    isrm_init();
     intr_routine_init();
 
     intr_routine_init();
 
+    // memory
     pmm_init(MEM_1MB + (_k_init_mb_info->mem_upper << 10));
     vmm_init();
 
     pmm_init(MEM_1MB + (_k_init_mb_info->mem_upper << 10));
     vmm_init();
 
@@ -55,38 +60,44 @@ _kernel_pre_init()
       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
 
     setup_memory((multiboot_memory_map_t*)_k_init_mb_info->mmap_addr, map_size);
       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
 
     setup_memory((multiboot_memory_map_t*)_k_init_mb_info->mmap_addr, map_size);
-
-    tty_init((void*)VGA_BUFFER_VADDR);
-    tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
-
-    __kernel_ptd = cpu_rcr3();
-
-    tmp = (struct proc_info){ .page_table = __kernel_ptd };
-
-    __current = &tmp;
 }
 
 void
 _kernel_init()
 {
     int errno = 0;
 }
 
 void
 _kernel_init()
 {
     int errno = 0;
+
+    // allocators
     cake_init();
     valloc_init();
 
     cake_init();
     valloc_init();
 
+    sched_init();
+
+    // crt
+    tty_init(ioremap(VGA_FRAMEBUFFER, PG_SIZE));
+    tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
+
+    // file system & device subsys
     vfs_init();
     fsm_init();
     input_init();
 
     vfs_init();
     fsm_init();
     input_init();
 
+    vfs_export_attributes();
+
+    lxconsole_init();
+
     if ((errno = vfs_mount_root("ramfs", NULL))) {
         panickf("Fail to mount root. (errno=%d)", errno);
     }
 
     vfs_mount("/dev", "devfs", NULL, 0);
     vfs_mount("/sys", "twifs", NULL, MNT_RO);
     if ((errno = vfs_mount_root("ramfs", NULL))) {
         panickf("Fail to mount root. (errno=%d)", errno);
     }
 
     vfs_mount("/dev", "devfs", NULL, 0);
     vfs_mount("/sys", "twifs", NULL, MNT_RO);
+    vfs_mount("/task", "taskfs", NULL, MNT_RO);
 
 
-    lxconsole_init();
+    lxconsole_spawn_ttydev();
+    device_init_builtin();
 
 
-    sched_init();
+    syscall_install();
 
     spawn_proc0();
 }
 
     spawn_proc0();
 }
@@ -115,14 +126,6 @@ spawn_proc0()
      * 目前的解决方案是2
      */
 
      * 目前的解决方案是2
      */
 
-    proc0->intr_ctx = (isr_param){ .registers = { .ds = KDATA_SEG,
-                                                  .es = KDATA_SEG,
-                                                  .fs = KDATA_SEG,
-                                                  .gs = KDATA_SEG },
-                                   .cs = KCODE_SEG,
-                                   .eip = (void*)__proc0,
-                                   .ss = KDATA_SEG,
-                                   .eflags = cpu_reflags() };
     proc0->parent = proc0;
 
     // 方案1:必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
     proc0->parent = proc0;
 
     // 方案1:必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
@@ -134,31 +137,38 @@ spawn_proc0()
     proc0->page_table = vmm_dup_vmspace(proc0->pid);
 
     // 直接切换到新的拷贝,进行配置。
     proc0->page_table = vmm_dup_vmspace(proc0->pid);
 
     // 直接切换到新的拷贝,进行配置。
-    cpu_lcr3(proc0->page_table);
+    cpu_ldvmspace(proc0->page_table);
 
     // 为内核创建一个专属栈空间。
     for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) {
 
     // 为内核创建一个专属栈空间。
     for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) {
-        uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
-        vmm_set_mapping(PD_REFERENCED,
+        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);
     }
 
                         KSTACK_START + (i << PG_SIZE_BITS),
                         pa,
                         PG_PREM_RW,
                         VMAP_NULL);
     }
 
-    // 手动设置进程上下文:用于第一次调度
-    asm volatile("movl %%esp, %%ebx\n"
-                 "movl %1, %%esp\n"
-                 "pushf\n"
-                 "pushl %2\n"
-                 "pushl %3\n"
-                 "pushl $0\n"
-                 "pushl $0\n"
-                 "movl %%esp, %0\n"
-                 "movl %%ebx, %%esp\n"
-                 : "=m"(proc0->intr_ctx.registers.esp)
-                 : "i"(KSTACK_TOP), "i"(KCODE_SEG), "r"(proc0->intr_ctx.eip)
-                 : "%ebx", "memory");
+    struct exec_param* execp =
+      (struct exec_param*)(KSTACK_TOP - sizeof(struct exec_param));
+    isr_param* isrp = (isr_param*)((ptr_t)execp - sizeof(isr_param));
+
+    *execp = (struct exec_param){ .cs = KCODE_SEG,
+                                  .eip = (ptr_t)__proc0,
+                                  .ss = KDATA_SEG,
+                                  .eflags = cpu_reflags() };
+    *isrp = (isr_param){ .registers = { .ds = KDATA_SEG,
+                                        .es = KDATA_SEG,
+                                        .fs = KDATA_SEG,
+                                        .gs = KDATA_SEG },
+                         .execp = execp };
+
+    proc0->intr_ctx = isrp;
+
+    // 加载x87默认配置
+    asm volatile("fninit\n"
+                 "fxsave (%%eax)" ::"a"(proc0->fxstate)
+                 : "memory");
 
     // 向调度器注册进程。
     commit_process(proc0);
 
     // 向调度器注册进程。
     commit_process(proc0);
@@ -172,9 +182,6 @@ spawn_proc0()
     assert_msg(0, "Unexpected Return");
 }
 
     assert_msg(0, "Unexpected Return");
 }
 
-extern void __usrtext_start;
-extern void __usrtext_end;
-
 // 按照 Memory map 标识可用的物理页
 void
 setup_memory(multiboot_memory_map_t* map, size_t map_size)
 // 按照 Memory map 标识可用的物理页
 void
 setup_memory(multiboot_memory_map_t* map, size_t map_size)
@@ -185,7 +192,7 @@ setup_memory(multiboot_memory_map_t* map, size_t map_size)
         multiboot_memory_map_t mmap = map[i];
         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
             // 整数向上取整除法
         multiboot_memory_map_t mmap = map[i];
         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
             // 整数向上取整除法
-            uintptr_t pg = map[i].addr_low + 0x0fffU;
+            ptr_t pg = map[i].addr_low + 0x0fffU;
             pmm_mark_chunk_free(pg >> PG_SIZE_BITS,
                                 map[i].len_low >> PG_SIZE_BITS);
         }
             pmm_mark_chunk_free(pg >> PG_SIZE_BITS,
                                 map[i].len_low >> PG_SIZE_BITS);
         }
@@ -193,24 +200,10 @@ setup_memory(multiboot_memory_map_t* map, size_t map_size)
 
     // 将内核占据的页,包括前1MB,hhk_init 设为已占用
     size_t pg_count = V2P(&__kernel_end) >> 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, 0);
-
-    size_t vga_buf_pgs = VGA_BUFFER_SIZE >> PG_SIZE_BITS;
-
-    // 首先,标记VGA部分为已占用,并且锁定
-    pmm_mark_chunk_occupied(
-      KERNEL_PID, VGA_BUFFER_PADDR >> PG_SIZE_BITS, vga_buf_pgs, PP_FGLOCKED);
-
-    // 重映射VGA文本缓冲区(以后会变成显存,i.e., framebuffer)
-    for (size_t i = 0; i < vga_buf_pgs; i++) {
-        vmm_set_mapping(PD_REFERENCED,
-                        VGA_BUFFER_VADDR + (i << PG_SIZE_BITS),
-                        VGA_BUFFER_PADDR + (i << PG_SIZE_BITS),
-                        PG_PREM_URW,
-                        VMAP_NULL);
-    }
+    pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED);
 
 
-    for (uintptr_t i = &__usrtext_start; i < &__usrtext_end; i += PG_SIZE) {
-        vmm_set_mapping(PD_REFERENCED, i, V2P(i), PG_PREM_UR, VMAP_NULL);
+    // 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));
     }
 }
     }
 }