X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/4c6d990440cdba6c7dd294adb7e435770ffcbcc4..14b86eca7f36eedde35cb47b41afc36f9097ea7c:/lunaix-os/kernel/k_init.c diff --git a/lunaix-os/kernel/k_init.c b/lunaix-os/kernel/k_init.c index 444aab0..eee475f 100644 --- a/lunaix-os/kernel/k_init.c +++ b/lunaix-os/kernel/k_init.c @@ -1,120 +1,212 @@ -#include +#include #include +#include +#include #include #include #include -#include +#include +#include #include +#include +#include + +#include #include #include +#include -#include +#include +#include -#include #include - +#include extern uint8_t __kernel_start; extern uint8_t __kernel_end; extern uint8_t __init_hhk_end; +#define PP_KERN_SHARED (PP_FGSHARED | PP_TKERN) + +// Set remotely by kernel/asm/x86/prologue.S +multiboot_info_t* _k_init_mb_info; + +x86_page_table* __kernel_ptd; + +struct proc_info tmp; + +LOG_MODULE("BOOT"); + +extern void +__proc0(); /* proc0.c */ + void -setup_memory(multiboot_memory_map_t* map, size_t map_size); +spawn_proc0(); void -setup_kernel_runtime(); +setup_memory(multiboot_memory_map_t* map, size_t map_size); void -_kernel_pre_init(multiboot_info_t* mb_info) { +_kernel_pre_init() +{ _init_idt(); + intr_routine_init(); - pmm_init(MEM_1MB + (mb_info->mem_upper << 10)); + pmm_init(MEM_1MB + (_k_init_mb_info->mem_upper << 10)); vmm_init(); + rtc_init(); tty_init((void*)VGA_BUFFER_PADDR); - tty_set_theme(VGA_COLOR_GREEN, VGA_COLOR_BLACK); -} + tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK); -void -_kernel_init(multiboot_info_t* mb_info) { - printf("[KERNEL] === Initialization === \n"); + __kernel_ptd = cpu_rcr3(); - printf("[MM] Mem: %d KiB, Extended Mem: %d KiB\n", - mb_info->mem_lower, - mb_info->mem_upper); + tmp = (struct proc_info){ .page_table = __kernel_ptd }; - unsigned int map_size = mb_info->mmap_length / sizeof(multiboot_memory_map_t); - setup_memory((multiboot_memory_map_t*)mb_info->mmap_addr, map_size); - setup_kernel_runtime(); + __current = &tmp; } -void -_kernel_post_init() { - printf("[KERNEL] === Post Initialization === \n"); - size_t hhk_init_pg_count = ((uintptr_t)(&__init_hhk_end)) >> PG_SIZE_BITS; - printf("[MM] Releaseing %d pages from 0x0.\n", hhk_init_pg_count); +void +_kernel_init() +{ + kprintf("[MM] Mem: %d KiB, Extended Mem: %d KiB\n", + _k_init_mb_info->mem_lower, + _k_init_mb_info->mem_upper); - // 清除 hhk_init 与前1MiB的映射 - for (size_t i = 0; i < hhk_init_pg_count; i++) { - vmm_unmap_page((void*)(i << PG_SIZE_BITS)); + unsigned int 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); + + // 为内核创建一个专属栈空间。 + for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) { + vmm_alloc_page(KERNEL_PID, + (void*)(KSTACK_START + (i << PG_SIZE_BITS)), + NULL, + PG_PREM_RW, + 0); } + kprintf(KINFO "[MM] Allocated %d pages for stack start at %p\n", + KSTACK_SIZE >> PG_SIZE_BITS, + KSTACK_START); + + sched_init(); - assert_msg(kalloc_init(), "Fail to initialize heap"); + spawn_proc0(); +} + +/** + * @brief 创建并运行proc0进程 + * + */ +void +spawn_proc0() +{ + struct proc_info proc0; + + /** + * @brief + * 注意:这里和视频中说的不一样,属于我之后的一点微调。 + * 在视频中,spawn_proc0是在_kernel_post_init的末尾才调用的。并且是直接跳转到_proc0 + * + * 但是我后来发现,上述的方法会产生竞态条件。这是因为spawn_proc0被调用的时候,时钟中断已经开启, + * 而中断的产生会打乱栈的布局,从而使得下面的上下文设置代码产生未定义行为(Undefined + * Behaviour)。 为了保险起见,有两种办法: + * 1. 在创建proc0进程前关闭中断 + * 2. 将_kernel_post_init搬进proc0进程 + * (_kernel_post_init已经更名为init_platform) + * + * 目前的解决方案是两者都使用 + */ + + init_proc(&proc0); + proc0.intr_ctx = (isr_param){ .registers.esp = KSTACK_TOP - 20, + .cs = KCODE_SEG, + .eip = (void*)__proc0, + .ss = KDATA_SEG, + .eflags = cpu_reflags() }; + + // 必须在读取eflags之后禁用。否则当进程被调度时,中断依然是关闭的! + cpu_disable_interrupt(); + setup_proc_mem(&proc0, PD_REFERENCED); + + // Ok... 首先fork进我们的零号进程,而后由那里,我们fork进init进程。 + /* + 这里是一些栈的设置,因为我们将切换到一个新的地址空间里,并且使用一个全新的栈。 + 让iret满意! + */ + asm volatile("movl %%cr3, %%eax\n" + "movl %%esp, %%ebx\n" + "movl %0, %%cr3\n" + "movl %1, %%esp\n" + "pushf\n" + "pushl %2\n" + "pushl %3\n" + "pushl $0\n" + "pushl $0\n" + "movl %%eax, %%cr3\n" + "movl %%ebx, %%esp\n" ::"r"(proc0.page_table), + "i"(KSTACK_TOP), + "i"(KCODE_SEG), + "r"(proc0.intr_ctx.eip) + : "%eax", "%ebx", "memory"); + + // 向调度器注册进程。 + push_process(&proc0); + + // 由于时钟中断未就绪,我们需要手动通知调度器进行第一次调度。这里也会同时隐式地恢复我们的eflags.IF位 + schedule(); + + /* Should not return */ + assert_msg(0, "Unexpected Return"); } // 按照 Memory map 标识可用的物理页 void -setup_memory(multiboot_memory_map_t* map, size_t map_size) { +setup_memory(multiboot_memory_map_t* map, size_t map_size) +{ + + // First pass, to mark the physical pages for (unsigned int i = 0; i < map_size; i++) { multiboot_memory_map_t mmap = map[i]; - printf("[MM] Base: 0x%x, len: %u KiB, type: %u\n", - map[i].addr_low, - map[i].len_low >> 10, - map[i].type); + kprintf("[MM] Base: 0x%x, len: %u KiB, type: %u\n", + map[i].addr_low, + map[i].len_low >> 10, + map[i].type); if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) { // 整数向上取整除法 uintptr_t pg = map[i].addr_low + 0x0fffU; - pmm_mark_chunk_free(pg >> PG_SIZE_BITS, map[i].len_low >> PG_SIZE_BITS); - printf("[MM] Freed %u pages start from 0x%x\n", - map[i].len_low >> PG_SIZE_BITS, - pg & ~0x0fffU); + pmm_mark_chunk_free(pg >> PG_SIZE_BITS, + map[i].len_low >> PG_SIZE_BITS); + kprintf(KINFO "[MM] Freed %u pages start from 0x%x\n", + map[i].len_low >> PG_SIZE_BITS, + pg & ~0x0fffU); } } - // 将内核占据的页设为已占用 - size_t pg_count = (uintptr_t)(&__kernel_end - &__kernel_start) >> PG_SIZE_BITS; - pmm_mark_chunk_occupied(V2P(&__kernel_start) >> PG_SIZE_BITS, pg_count); - printf("[MM] Allocated %d pages for kernel.\n", pg_count); - + // 将内核占据的页,包括前1MB,hhk_init 设为已占用 + size_t pg_count = V2P(&__kernel_end) >> PG_SIZE_BITS; + pmm_mark_chunk_occupied(KERNEL_PID, 0, pg_count, 0); + kprintf(KINFO "[MM] Allocated %d pages for kernel.\n", pg_count); size_t vga_buf_pgs = VGA_BUFFER_SIZE >> PG_SIZE_BITS; - + // 首先,标记VGA部分为已占用 - pmm_mark_chunk_occupied(VGA_BUFFER_PADDR >> PG_SIZE_BITS, vga_buf_pgs); - + pmm_mark_chunk_occupied( + KERNEL_PID, VGA_BUFFER_PADDR >> PG_SIZE_BITS, vga_buf_pgs, 0); + // 重映射VGA文本缓冲区(以后会变成显存,i.e., framebuffer) - for (size_t i = 0; i < vga_buf_pgs; i++) - { - vmm_map_page( - (void*)(VGA_BUFFER_VADDR + (i << PG_SIZE_BITS)), - (void*)(VGA_BUFFER_PADDR + (i << PG_SIZE_BITS)), - PG_PREM_RW - ); + for (size_t i = 0; i < vga_buf_pgs; i++) { + vmm_map_page(KERNEL_PID, + (void*)(VGA_BUFFER_VADDR + (i << PG_SIZE_BITS)), + (void*)(VGA_BUFFER_PADDR + (i << PG_SIZE_BITS)), + PG_PREM_URW); } - + // 更新VGA缓冲区位置至虚拟地址 tty_set_buffer((void*)VGA_BUFFER_VADDR); - printf("[MM] Mapped VGA to %p.\n", VGA_BUFFER_VADDR); + kprintf(KINFO "[MM] Mapped VGA to %p.\n", VGA_BUFFER_VADDR); } - -void -setup_kernel_runtime() { - // 为内核创建一个专属栈空间。 - for (size_t i = 0; i < (K_STACK_SIZE >> PG_SIZE_BITS); i++) { - vmm_alloc_page((void*)(K_STACK_START + (i << PG_SIZE_BITS)), PG_PREM_RW); - } - printf("[MM] Allocated %d pages for stack start at %p\n", K_STACK_SIZE>>PG_SIZE_BITS, K_STACK_START); -} \ No newline at end of file