X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/c166bd62fbb907f95f79f621e2a2fb4fdde08e01..9e622bd093f8e106b7e8a00f55620f13cc5cd87f:/lunaix-os/kernel/kinit.c?ds=sidebyside diff --git a/lunaix-os/kernel/kinit.c b/lunaix-os/kernel/kinit.c index 528a738..58a69e9 100644 --- a/lunaix-os/kernel/kinit.c +++ b/lunaix-os/kernel/kinit.c @@ -2,55 +2,139 @@ #include #include #include -#include -#include #include + #include -#include #include +#include #include #include + #include #include #include #include -#include #include -#include +#include +#include +#include -#include -#include +#include -#include -#include +#include -#include -#include +LOG_MODULE("kinit") -void -spawn_lunad(); +extern void +lunad_main(); -void -kmem_init(struct boot_handoff* bhctx); +/** + * @brief 创建并运行Lunaix守护进程 + * + */ +static void +spawn_lunad() +{ + int has_error; + struct thread* kthread; + + has_error = spawn_process(&kthread, (ptr_t)lunad_main, false); + assert_msg(!has_error, "failed to spawn lunad"); + + run(kthread); + + fail("Unexpected Return"); +} + +static void +kmem_init(struct boot_handoff* bhctx) +{ + pte_t* ptep = mkptep_va(VMS_SELF, KERNEL_RESIDENT); + + ptep = mkl0tep(ptep); + + unsigned int i = ptep_vfn(ptep); + do { + if (lntep_implie_vmnts(ptep, L0T_SIZE)) { + ptep++; + continue; + } + +#if LnT_ENABLED(1) + assert(mkl1t(ptep++, 0, KERNEL_PGTAB)); +#elif LnT_ENABLED(2) + assert(mkl2t(ptep++, 0, KERNEL_PGTAB)); +#elif LnT_ENABLED(3) + assert(mkl3t(ptep++, 0, KERNEL_PGTAB)); +#else + assert(mklft(ptep++, 0, KERNEL_PGTAB)); +#endif + } while (++i < MAX_PTEN); + + // allocators + cake_init(); + valloc_init(); +} + +static void +__remap_and_load_dtb(struct boot_handoff* bhctx) +{ +#ifdef CONFIG_USE_DEVICETREE + ptr_t dtb = bhctx->kexec.dtb_pa; + + if (!dtb) { + return; + } + + if (va_offset(dtb)) { + WARN("bad-aligned dtb location, expect page aligned"); + return; + } + + pte_t *ptep, pte; + size_t nr_pages; + bool loaded; + + pte = mkpte(dtb, KERNEL_DATA); + ptep = mkptep_va(VMS_SELF, dtb_start); + nr_pages = leaf_count(CONFIG_DTB_MAXSIZE); + + pmm_onhold_range(dtb, nr_pages); + vmm_set_ptes_contig(ptep, pte, PAGE_SIZE, nr_pages); + + loaded = dt_load(dtb_start); + if (!loaded) { + ERROR("dtb load failed"); + } +#endif + + return; +} + +static void +log_bootup_time() +{ + datetime_t dt; + + clock_walltime(&dt); + INFO("kernel boot at: %d/%d/%d %d:%d:%d", + dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second); +} void kernel_bootstrap(struct boot_handoff* bhctx) { - vmm_init(); - pmm_init(bhctx); // now we can start reserving physical space /* Begin kernel bootstrapping sequence */ boot_begin(bhctx); - tty_init(ioremap(0xB8000, PAGE_SIZE)); - /* Setup kernel memory layout and services */ kmem_init(bhctx); - // FIXME this goes to hal/gfxa - tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK); + INFO(); + INFO("Lunaix " CONFIG_LUNAIX_VER " (c) Lunaixsky 2022-2025"); boot_parse_cmdline(bhctx); @@ -59,22 +143,20 @@ kernel_bootstrap(struct boot_handoff* bhctx) device_scan_drivers(); - invoke_init_function(on_earlyboot); - + initfn_invoke_sysconf(); + + __remap_and_load_dtb(bhctx); device_sysconf_load(); - /* Get intc online, this is the cornerstone when initing devices */ - intc_init(); + // TODO register devtree hooks + // TODO re-scan devtree to bind devices. clock_init(); timer_init(); + log_bootup_time(); - /* - TODO autoload these init function that do not have dependency between - them - */ + initfn_invoke_earlyboot(); - /* Let's get fs online as soon as possible, as things rely on them */ vfs_init(); fsm_init(); input_init(); @@ -88,59 +170,13 @@ kernel_bootstrap(struct boot_handoff* bhctx) must_success(vfs_mount_root("ramfs", NULL)); must_success(vfs_mount("/dev", "devfs", NULL, 0)); - invoke_init_function(on_boot); - - must_success(vfs_unmount("/dev")); + initfn_invoke_boot(); /* Finish up bootstrapping sequence, we are ready to spawn the root process * and start geting into uspace */ boot_end(bhctx); - boot_cleanup(); spawn_lunad(); } -extern void -lunad_main(); - -/** - * @brief 创建并运行Lunaix守护进程 - * - */ -void -spawn_lunad() -{ - int has_error; - struct thread* kthread; - - has_error = spawn_process(&kthread, (ptr_t)lunad_main, false); - assert_msg(!has_error, "failed to spawn lunad"); - - run(kthread); - - fail("Unexpected Return"); -} - -void -kmem_init(struct boot_handoff* bhctx) -{ - pte_t* ptep = mkptep_va(VMS_SELF, KERNEL_RESIDENT); - ptep = mkl0tep(ptep); - - do { -#if LnT_ENABLED(1) - assert(mkl1t(ptep++, 0, KERNEL_DATA)); -#elif LnT_ENABLED(2) - assert(mkl2t(ptep++, 0, KERNEL_DATA)); -#elif LnT_ENABLED(3) - assert(mkl3t(ptep++, 0, KERNEL_DATA)); -#else - assert(mklft(ptep++, 0, KERNEL_DATA)); -#endif - } while (ptep_vfn(ptep) < MAX_PTEN - 2); - - // allocators - cake_init(); - valloc_init(); -}