2022-11-30 12:29:28 +00:00
|
|
|
#include "Log.h"
|
2022-11-15 18:10:32 +00:00
|
|
|
#include "arch/CPU.h"
|
2022-11-19 19:01:01 +00:00
|
|
|
#include "arch/Timer.h"
|
2023-07-30 16:25:44 +00:00
|
|
|
#include "binfmt/BinaryFormat.h"
|
2022-11-19 16:59:49 +00:00
|
|
|
#include "boot/Init.h"
|
2022-12-03 16:25:25 +00:00
|
|
|
#include "config.h"
|
2023-04-28 13:55:06 +00:00
|
|
|
#include "fs/InitRD.h"
|
2023-03-18 08:10:33 +00:00
|
|
|
#include "fs/devices/DeviceRegistry.h"
|
2023-02-03 21:18:52 +00:00
|
|
|
#include "fs/tmpfs/FileSystem.h"
|
2022-11-19 16:59:49 +00:00
|
|
|
#include "memory/MemoryManager.h"
|
2022-12-07 14:04:11 +00:00
|
|
|
#include "thread/Scheduler.h"
|
2022-12-07 11:25:42 +00:00
|
|
|
#include <luna/Units.h>
|
2022-10-16 13:31:58 +00:00
|
|
|
|
2023-05-10 17:15:47 +00:00
|
|
|
#ifdef ARCH_X86_64
|
|
|
|
#include "arch/x86_64/disk/ATA.h"
|
|
|
|
#endif
|
|
|
|
|
2023-04-24 19:20:44 +00:00
|
|
|
extern void set_host_name(StringView);
|
|
|
|
|
2022-12-18 17:43:34 +00:00
|
|
|
void reap_thread()
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
CPU::disable_interrupts();
|
|
|
|
auto dying_threads = Scheduler::check_for_dying_threads();
|
|
|
|
CPU::enable_interrupts();
|
|
|
|
|
2022-12-19 11:35:08 +00:00
|
|
|
dying_threads.consume([](Thread* thread) { Scheduler::reap_thread(thread); });
|
2022-12-18 17:43:34 +00:00
|
|
|
|
2023-05-04 21:43:00 +00:00
|
|
|
kernel_wait_for_event();
|
2022-12-18 17:43:34 +00:00
|
|
|
}
|
2022-12-16 17:18:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 18:14:33 +00:00
|
|
|
void oom_thread()
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
kernel_wait_for_event();
|
|
|
|
// OOM! Do everything we can to recover memory.
|
|
|
|
StorageCache::clear_caches();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-17 17:43:25 +00:00
|
|
|
[[noreturn]] void init()
|
2022-09-05 14:13:51 +00:00
|
|
|
{
|
2023-08-08 08:44:18 +00:00
|
|
|
{
|
|
|
|
kinfoln("Starting Moon %s %s", MOON_VERSION, MOON_RELEASE);
|
2022-11-19 11:30:36 +00:00
|
|
|
|
2023-08-08 08:44:18 +00:00
|
|
|
// Default hostname if nobody from userspace changes it
|
|
|
|
set_host_name("moon"_sv);
|
2023-04-24 19:20:44 +00:00
|
|
|
|
2023-08-08 08:44:18 +00:00
|
|
|
kinfoln("Current platform: %s", CPU::platform_string().chars());
|
|
|
|
kinfoln("Current processor: %s", CPU::identify().value_or("(unknown)"_sv).chars());
|
2022-11-13 13:29:15 +00:00
|
|
|
|
2023-08-08 08:44:18 +00:00
|
|
|
auto root = mark_critical(TmpFS::FileSystem::create(), "Failed to create initial ramfs");
|
|
|
|
mark_critical(VFS::mount_root(root), "Failed to mount the initial ramfs as the root filesystem");
|
|
|
|
mark_critical(InitRD::populate_vfs(), "Failed to load files from the initial ramdisk");
|
|
|
|
mark_critical(DeviceRegistry::init(), "Failed to register initial devices");
|
2023-03-10 23:52:39 +00:00
|
|
|
|
2023-08-08 08:44:18 +00:00
|
|
|
mark_critical(BinaryFormat::init(), "Failed to register initial binary formats");
|
2023-07-30 16:25:44 +00:00
|
|
|
|
2023-08-08 08:44:18 +00:00
|
|
|
auto init =
|
|
|
|
mark_critical(VFS::resolve_path("/bin/preinit", Credentials {}), "Can't find init in the initial ramfs!");
|
|
|
|
auto init_thread = mark_critical(Scheduler::new_userspace_thread(init, "/bin/preinit"),
|
|
|
|
"Failed to create PID 1 process for init");
|
2022-12-18 15:39:35 +00:00
|
|
|
|
2023-08-08 08:44:18 +00:00
|
|
|
auto reap = mark_critical(Scheduler::new_kernel_thread(reap_thread, "[reap]"),
|
|
|
|
"Failed to create the process reaper kernel thread");
|
|
|
|
Scheduler::set_reap_thread(reap);
|
2022-12-07 14:04:11 +00:00
|
|
|
|
2023-08-17 18:14:33 +00:00
|
|
|
auto oom = mark_critical(Scheduler::new_kernel_thread(oom_thread, "[oom]"),
|
|
|
|
"Failed to create the out-of-memory kernel thread");
|
|
|
|
Scheduler::set_oom_thread(oom);
|
|
|
|
|
2023-05-10 17:15:47 +00:00
|
|
|
#ifdef ARCH_X86_64
|
2023-08-08 08:44:18 +00:00
|
|
|
ATA::Controller::scan();
|
2023-05-10 17:15:47 +00:00
|
|
|
#endif
|
2023-01-23 19:07:17 +00:00
|
|
|
|
2023-08-08 08:44:18 +00:00
|
|
|
// Disable console logging before transferring control to userspace.
|
|
|
|
setup_log(log_debug_enabled(), log_serial_enabled(), false);
|
2023-01-08 14:32:59 +00:00
|
|
|
|
2023-08-08 08:44:18 +00:00
|
|
|
init_thread->wake_up();
|
|
|
|
}
|
2022-12-07 14:04:11 +00:00
|
|
|
|
2023-04-28 11:23:07 +00:00
|
|
|
kernel_exit();
|
2022-11-20 16:56:07 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 16:09:03 +00:00
|
|
|
extern "C" [[noreturn]] void _start()
|
|
|
|
{
|
|
|
|
Init::check_magic();
|
|
|
|
Init::early_init();
|
2023-04-28 11:23:07 +00:00
|
|
|
|
|
|
|
Timer::init();
|
|
|
|
|
|
|
|
Thread::init();
|
|
|
|
Scheduler::init();
|
|
|
|
|
2023-06-17 17:43:25 +00:00
|
|
|
Scheduler::new_kernel_thread(init, "[kinit]");
|
2023-04-28 11:23:07 +00:00
|
|
|
|
|
|
|
CPU::platform_finish_init();
|
|
|
|
|
|
|
|
CPU::enable_interrupts();
|
|
|
|
|
|
|
|
CPU::idle_loop();
|
2023-02-25 16:09:03 +00:00
|
|
|
}
|