Merge branch 'master' into prog-loader
[lunaix-os.git] / lunaix-os / kernel / k_init.c
index 32164c88aa832cafad68c919068cf2dbe227d679..a2edf1a9d5c66007b5ada75b4e93aa0e80ad92cb 100644 (file)
@@ -33,8 +33,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 */
 
@@ -60,12 +58,6 @@ _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);
-
-    __kernel_ptd = cpu_rcr3();
-
-    tmp = (struct proc_info){ .page_table = __kernel_ptd };
-
-    __current = &tmp;
 }
 
 void
 }
 
 void
@@ -77,6 +69,8 @@ _kernel_init()
     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);
     // crt
     tty_init(ioremap(VGA_FRAMEBUFFER, PG_SIZE));
     tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK);
@@ -88,6 +82,8 @@ _kernel_init()
 
     vfs_export_attributes();
 
 
     vfs_export_attributes();
 
+    lxconsole_init();
+
     if ((errno = vfs_mount_root("ramfs", NULL))) {
         panickf("Fail to mount root. (errno=%d)", errno);
     }
     if ((errno = vfs_mount_root("ramfs", NULL))) {
         panickf("Fail to mount root. (errno=%d)", errno);
     }
@@ -96,9 +92,8 @@ _kernel_init()
     vfs_mount("/sys", "twifs", NULL, MNT_RO);
     vfs_mount("/task", "taskfs", NULL, MNT_RO);
 
     vfs_mount("/sys", "twifs", NULL, MNT_RO);
     vfs_mount("/task", "taskfs", NULL, MNT_RO);
 
-    lxconsole_init();
-
-    sched_init();
+    lxconsole_spawn_ttydev();
+    device_init_builtin();
 
     syscall_install();
 
 
     syscall_install();
 
@@ -132,11 +127,7 @@ spawn_proc0()
     proc0->intr_ctx = (isr_param){ .registers = { .ds = KDATA_SEG,
                                                   .es = KDATA_SEG,
                                                   .fs = KDATA_SEG,
     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() };
+                                                  .gs = KDATA_SEG } };
     proc0->parent = proc0;
 
     // 方案1:必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
     proc0->parent = proc0;
 
     // 方案1:必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的!
@@ -153,26 +144,21 @@ spawn_proc0()
     // 为内核创建一个专属栈空间。
     for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) {
         uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
     // 为内核创建一个专属栈空间。
     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,
+        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));
+
+    *execp = (struct exec_param){ .cs = KCODE_SEG,
+                                  .eip = (void*)__proc0,
+                                  .ss = KDATA_SEG,
+                                  .eflags = cpu_reflags() };
+    proc0->intr_ctx.execp = execp;
 
     // 加载x87默认配置
     asm volatile("fninit\n"
 
     // 加载x87默认配置
     asm volatile("fninit\n"
@@ -215,6 +201,11 @@ setup_memory(multiboot_memory_map_t* map, size_t map_size)
     pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED);
 
     for (uintptr_t i = &__usrtext_start; i < &__usrtext_end; i += PG_SIZE) {
     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);
+        vmm_set_mapping(VMS_SELF, 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));
     }
 }
     }
 }