X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/b91be0033265b5b6bd716bd39cb0971da41906c4..7b8a1bcad75628f9add4590db2bb9b8e418ee8eb:/lunaix-os/kernel/kinit.c diff --git a/lunaix-os/kernel/kinit.c b/lunaix-os/kernel/kinit.c index 726295c..5884e5b 100644 --- a/lunaix-os/kernel/kinit.c +++ b/lunaix-os/kernel/kinit.c @@ -1,8 +1,10 @@ -#include +#include +#include +#include #include #include +#include #include -#include #include #include #include @@ -13,27 +15,19 @@ #include #include #include -#include +#include #include -#include - -#include -#include -#include - -#include -#include - -extern u8_t __kernel_start; -extern u8_t __kernel_end; -extern u8_t __init_hhk_end; +#include -#define PP_KERN_SHARED (PP_FGSHARED | PP_TKERN) +#include +#include -// Set remotely by kernel/asm/x86/prologue.S -multiboot_info_t* _k_init_mb_info; +#include +#include +#include -x86_page_table* __kernel_ptd; +#include +#include extern void __proc0(); /* proc0.c */ @@ -42,62 +36,67 @@ void spawn_proc0(); void -setup_memory(multiboot_memory_map_t* map, size_t map_size); +kmem_init(struct boot_handoff* bhctx); void -_kernel_pre_init() +kernel_bootstrap(struct boot_handoff* bhctx) { - // interrupts - _init_idt(); - isrm_init(); - intr_routine_init(); - - // memory - pmm_init(MEM_1MB + (_k_init_mb_info->mem_upper << 10)); + pmm_init(bhctx->mem.size); vmm_init(); - unsigned int map_size = - _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t); + /* Begin kernel bootstrapping sequence */ + boot_begin(bhctx); - setup_memory((multiboot_memory_map_t*)_k_init_mb_info->mmap_addr, map_size); -} + /* Setup kernel memory layout and services */ + kmem_init(bhctx); -void -_kernel_init() -{ - int errno = 0; + boot_parse_cmdline(bhctx); - // allocators - cake_init(); - valloc_init(); + /* Prepare stack trace environment */ + trace_modksyms_init(bhctx); - sched_init(); + device_scan_drivers(); // crt - tty_init(ioremap(VGA_FRAMEBUFFER, PG_SIZE)); + tty_init(ioremap(0xB8000, PG_SIZE)); tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + lxconsole_init(); - // file system & device subsys + device_sysconf_load(); + + /* Get intc online, this is the cornerstone when initing devices */ + intc_init(); + + clock_init(); + timer_init(); + + /* + TODO autoload these init function that do not have dependency between + them + */ + + /* 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(call_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); spawn_proc0(); } @@ -140,51 +139,38 @@ spawn_proc0() cpu_chvmspace(proc0->page_table); // 为内核创建一个专属栈空间。 - for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) { + for (size_t i = 0; i < KERNEL_STACK_SIZE; i += PG_SIZE) { 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); + vmm_set_mapping(VMS_SELF, KERNEL_STACK + i, pa, PG_PREM_RW, VMAP_NULL); } - proc_init_transfer(proc0, KSTACK_TOP, (ptr_t)__proc0, 0); + proc_init_transfer(proc0, KERNEL_STACK_END, (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)); + switch_context(proc0); /* Should not return */ assert_msg(0, "Unexpected Return"); } -// 按照 Memory map 标识可用的物理页 void -setup_memory(multiboot_memory_map_t* map, size_t map_size) +kmem_init(struct boot_handoff* bhctx) { - - // 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); - } - } - + extern u8_t __kexec_end; // 将内核占据的页,包括前1MB,hhk_init 设为已占用 - size_t pg_count = V2P(&__kernel_end) >> PG_SIZE_BITS; + size_t pg_count = ((ptr_t)&__kexec_end - KERNEL_EXEC) >> 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++) { + for (size_t i = L1_INDEX(KERNEL_EXEC); i < 1023; i++) { assert(vmm_set_mapping(VMS_SELF, i << 22, 0, 0, VMAP_NOMAP)); } + + // allocators + cake_init(); + valloc_init(); }