Luna/kernel/src/main.cpp

101 lines
2.8 KiB
C++
Raw Normal View History

2022-11-30 13:29:28 +01:00
#include "Log.h"
2022-11-15 19:10:32 +01:00
#include "arch/CPU.h"
#include "arch/MMU.h"
#include "arch/PCI.h"
2022-11-19 20:01:01 +01:00
#include "arch/Timer.h"
2022-11-19 17:59:49 +01:00
#include "boot/Init.h"
2022-12-03 17:25:25 +01:00
#include "config.h"
#include "fs/InitRD.h"
#include "fs/devices/DeviceRegistry.h"
#include "fs/tmpfs/FileSystem.h"
#include "memory/Heap.h"
#include "memory/KernelVM.h"
2022-11-19 17:59:49 +01:00
#include "memory/MemoryManager.h"
2022-12-07 15:04:11 +01:00
#include "thread/Scheduler.h"
#include "video/Framebuffer.h"
#include "video/TextConsole.h"
2022-12-19 12:24:15 +01:00
#include <luna/CString.h>
2022-12-07 11:40:02 +01:00
#include <luna/Result.h>
2022-12-07 12:25:42 +01:00
#include <luna/Units.h>
2022-10-16 15:31:58 +02:00
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_sleep(250);
}
}
Result<void> init()
2022-09-05 16:13:51 +02:00
{
kinfoln("Starting Moon %s, built on %s at %s", MOON_VERSION, __DATE__, __TIME__);
2022-11-19 12:30:36 +01:00
// Default hostname if nobody from userspace changes it
set_host_name("moon"_sv);
kinfoln("Current platform: %s", CPU::platform_string());
kinfoln("Current processor: %s", CPU::identify().value_or("(unknown)"));
2022-11-13 14:29:15 +01:00
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());
2022-11-30 12:42:11 +01:00
VFS::root_fs = TRY(TmpFS::FileSystem::create());
TRY(DeviceRegistry::init());
InitRD::populate_vfs();
auto init = TRY(VFS::resolve_path("/bin/init", Credentials {}));
auto init_thread = TRY(Scheduler::new_userspace_thread(init, "/bin/init"));
2023-03-24 21:05:38 +01:00
Scheduler::new_kernel_thread(reap_thread, "[reap]").release_value();
2022-12-07 15:04:11 +01:00
2023-01-23 21:24:05 +01:00
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->state = ThreadState::Runnable;
2022-12-07 15:04:11 +01:00
kernel_exit();
}
[[noreturn]] void init_wrapper()
{
auto rc = init();
2022-11-30 17:16:36 +01:00
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();
}