X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/9b8e0c494de6b447b44454112748f702dffec90d..2a49908006b177c4d6354309333d06b1b96e4887:/lunaix-os/kernel/k_init.c diff --git a/lunaix-os/kernel/k_init.c b/lunaix-os/kernel/k_init.c index e98be88..d399251 100644 --- a/lunaix-os/kernel/k_init.c +++ b/lunaix-os/kernel/k_init.c @@ -1,18 +1,20 @@ #include -#include - #include #include #include #include #include +#include #include #include #include +#include #include #include #include #include +#include +#include #include #include @@ -22,9 +24,9 @@ #include #include -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) @@ -127,11 +129,7 @@ spawn_proc0() 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之后禁用。否则当进程被调度时,中断依然是关闭的! @@ -147,7 +145,7 @@ spawn_proc0() // 为内核创建一个专属栈空间。 for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) { - uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0); + ptr_t pa = pmm_alloc_page(KERNEL_PID, 0); vmm_set_mapping(VMS_SELF, KSTACK_START + (i << PG_SIZE_BITS), pa, @@ -155,19 +153,14 @@ spawn_proc0() 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 = (ptr_t)__proc0, + .ss = KDATA_SEG, + .eflags = cpu_reflags() }; + proc0->intr_ctx.execp = execp; // 加载x87默认配置 asm volatile("fninit\n" @@ -186,8 +179,8 @@ spawn_proc0() assert_msg(0, "Unexpected Return"); } -extern void __usrtext_start; -extern void __usrtext_end; +extern u8_t __usrtext_start; +extern u8_t __usrtext_end; // 按照 Memory map 标识可用的物理页 void @@ -199,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) { // 整数向上取整除法 - 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); } @@ -209,7 +202,8 @@ setup_memory(multiboot_memory_map_t* map, size_t map_size) size_t pg_count = V2P(&__kernel_end) >> PG_SIZE_BITS; pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, PP_FGLOCKED); - for (uintptr_t i = &__usrtext_start; i < &__usrtext_end; i += PG_SIZE) { + for (ptr_t i = (ptr_t)&__usrtext_start; i < (ptr_t)&__usrtext_end; + i += PG_SIZE) { vmm_set_mapping(VMS_SELF, i, V2P(i), PG_PREM_UR, VMAP_NULL); }