X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/92f6e64a6da763c45ff9f4ab5eafcab3d8766dcb..b60166b327a9108b07e3069fa6568a451529ffd9:/lunaix-os/kernel/lunad.c?ds=inline diff --git a/lunaix-os/kernel/lunad.c b/lunaix-os/kernel/lunad.c new file mode 100644 index 0000000..99cd066 --- /dev/null +++ b/lunaix-os/kernel/lunad.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +LOG_MODULE("PROC0") + +void +init_platform(); + +int +mount_bootmedium() +{ + struct v_dnode* dnode; + int errno = 0; + struct device* dev = probe_boot_medium(); + if (!dev) { + ERROR("fail to acquire device. (%d)", errno); + return 0; + } + + if ((errno = vfs_mount("/mnt/lunaix-os", "iso9660", dev, 0))) { + ERROR("fail to mount boot medium. (%d)", errno); + return 0; + } + + return 1; +} + +int +exec_initd() +{ + int errno = 0; + + if ((errno = exec_kexecve("/mnt/lunaix-os/usr/bin/init", NULL, NULL))) { + goto fail; + } + + fail("should not reach"); + +fail: + ERROR("fail to load initd. (%d)", errno); + return 0; +} + +static void +lunad_do_usr() { + // No, these are not preemptive + cpu_disable_interrupt(); + + if (!mount_bootmedium() || !exec_initd()) { + fail("failed to initd"); + } +} + +/** + * @brief LunaixOS的内核进程,该进程永远为可执行。 + * + * 这主要是为了保证调度器在没有进程可调度时依然有事可做。 + * + * 同时,该进程也负责fork出我们的init进程。 + * + */ +void _preemptible +lunad_main() +{ + /* + * We must defer boot code/data cleaning to here, after we successfully + * escape that area + */ + boot_cleanup(); + + spawn_kthread((ptr_t)init_platform); + + /* + NOTE Kernel preemption after this point. + + More specifically, it is not a real kernel preemption (as in preemption + happened at any point of kernel, except those marked explicitly). + In Lunaix, things are designed in an non-preemptive fashion, we implement + kernel preemption the other way around: only selected kernel functions which, + of course, with great care of preemptive assumption, will goes into kernel + thread (which is preemptive!) + */ + + cpu_enable_interrupt(); + while (1) + { + cleanup_detached_threads(); + sched_pass(); + } +} + +void +init_platform() +{ + device_postboot_load(); + invoke_init_function(on_postboot); + + twifs_register_plugins(); + + // FIXME Re-design needed!! + // sdbg_init(); + + assert(!spawn_process(NULL, (ptr_t)lunad_do_usr, true)); + + exit_thread(NULL); +} \ No newline at end of file