#include "Log.h" #include "arch/CPU.h" #include "arch/PCI.h" #include "arch/Timer.h" #include "boot/Init.h" #include "config.h" #include "fs/InitRD.h" #include "fs/devices/DeviceRegistry.h" #include "fs/tmpfs/FileSystem.h" #include "memory/MemoryManager.h" #include "thread/Scheduler.h" #include extern void set_host_name(StringView); void reap_thread() { while (true) { CPU::disable_interrupts(); auto dying_threads = Scheduler::check_for_dying_threads(); CPU::enable_interrupts(); dying_threads.consume([](Thread* thread) { Scheduler::reap_thread(thread); }); kernel_wait_for_event(); } } Result init() { kinfoln("Starting Moon %s, built on %s at %s", MOON_VERSION, __DATE__, __TIME__); // Default hostname if nobody from userspace changes it set_host_name("moon"_sv); kinfoln("Current platform: %s", CPU::platform_string().chars()); kinfoln("Current processor: %s", CPU::identify().value_or("(unknown)"_sv).chars()); kinfoln("Total memory: %s", to_dynamic_unit(MemoryManager::total()).release_value().chars()); kinfoln("Free memory: %s", to_dynamic_unit(MemoryManager::free()).release_value().chars()); kinfoln("Used memory: %s", to_dynamic_unit(MemoryManager::used()).release_value().chars()); kinfoln("Reserved memory: %s", to_dynamic_unit(MemoryManager::reserved()).release_value().chars()); auto root = TRY(TmpFS::FileSystem::create()); TRY(VFS::mount_root(root)); TRY(InitRD::populate_vfs()); TRY(DeviceRegistry::init()); auto init = TRY(VFS::resolve_path("/bin/init", Credentials {})); auto init_thread = TRY(Scheduler::new_userspace_thread(init, "/bin/init")); auto reap = Scheduler::new_kernel_thread(reap_thread, "[reap]").release_value(); Scheduler::set_reap_thread(reap); PCI::scan( [](const PCI::Device& device) { kinfoln("Found PCI mass storage device %.4x:%.4x, at address %u:%u:%u", device.id.vendor, device.id.device, device.address.bus, device.address.slot, device.address.function); }, { .klass = 1 }); // Disable console logging before transferring control to userspace. setup_log(log_debug_enabled(), log_serial_enabled(), false); init_thread->wake_up(); kernel_exit(); } [[noreturn]] void init_wrapper() { auto rc = init(); if (rc.has_error()) kerrorln("Runtime error: %s", rc.error_string()); kernel_exit(); } extern "C" [[noreturn]] void _start() { Init::check_magic(); Init::early_init(); Timer::init(); Thread::init(); Scheduler::init(); Scheduler::new_kernel_thread(init_wrapper, "[kinit]"); CPU::platform_finish_init(); CPU::enable_interrupts(); CPU::idle_loop(); }