109 lines
3.4 KiB
C++
109 lines
3.4 KiB
C++
#include "ELF.h"
|
|
#include "InitRD.h"
|
|
#include "Log.h"
|
|
#include "arch/CPU.h"
|
|
#include "arch/MMU.h"
|
|
#include "arch/PCI.h"
|
|
#include "arch/Timer.h"
|
|
#include "boot/Init.h"
|
|
#include "config.h"
|
|
#include "fs/devices/DeviceRegistry.h"
|
|
#include "fs/tmpfs/FileSystem.h"
|
|
#include "memory/Heap.h"
|
|
#include "memory/KernelVM.h"
|
|
#include "memory/MemoryManager.h"
|
|
#include "thread/Scheduler.h"
|
|
#include "video/Framebuffer.h"
|
|
#include "video/TextConsole.h"
|
|
#include <luna/CString.h>
|
|
#include <luna/Result.h>
|
|
#include <luna/Units.h>
|
|
|
|
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()
|
|
{
|
|
kinfoln("Starting Moon %s, built on %s at %s", MOON_VERSION, __DATE__, __TIME__);
|
|
|
|
kinfoln("Current platform: %s", CPU::platform_string());
|
|
kinfoln("Current processor: %s", CPU::identify().value_or("(unknown)"));
|
|
|
|
Timer::init();
|
|
|
|
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());
|
|
|
|
Thread::init();
|
|
Scheduler::init();
|
|
|
|
VFS::root_fs = TRY(TmpFS::FileSystem::create());
|
|
TRY(DeviceRegistry::init());
|
|
InitRD::populate_vfs();
|
|
|
|
auto init = TRY(VFS::resolve_path("/bin/init", Credentials {}));
|
|
TRY(Scheduler::new_userspace_thread(init, "/bin/init"));
|
|
|
|
Scheduler::new_kernel_thread(reap_thread, "[reap]").release_value();
|
|
|
|
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 });
|
|
|
|
CPU::platform_finish_init();
|
|
|
|
// Disable console logging before transferring control to userspace.
|
|
setup_log(log_debug_enabled(), log_serial_enabled(), false);
|
|
|
|
CPU::enable_interrupts();
|
|
|
|
return {};
|
|
}
|
|
|
|
[[noreturn]] void init_wrapper()
|
|
{
|
|
auto rc = init();
|
|
if (rc.has_error()) kerrorln("Runtime error: %s", rc.error_string());
|
|
CPU::idle_loop();
|
|
}
|
|
|
|
static constexpr u64 BOOTSTRAP_STACK_PAGES = 8;
|
|
|
|
// FIXME: Reclaim this memory as soon as we leave the init task (so as soon as the Scheduler runs a task switch)
|
|
static u64 allocate_initial_kernel_stack()
|
|
{
|
|
u64 address = MemoryManager::alloc_for_kernel(BOOTSTRAP_STACK_PAGES + 1, MMU::ReadWrite | MMU::NoExecute).value();
|
|
// First page is a guard page, the rest is stack.
|
|
MMU::unmap(address); // Unmap (without deallocating VM) one guard page so that attempts to access it fail with a
|
|
// non-present page fault.
|
|
|
|
// The actual stack.
|
|
Stack stack { address + ARCH_PAGE_SIZE, BOOTSTRAP_STACK_PAGES * ARCH_PAGE_SIZE };
|
|
|
|
return stack.top();
|
|
}
|
|
|
|
extern "C" [[noreturn]] void _start()
|
|
{
|
|
Init::check_magic();
|
|
Init::early_init();
|
|
u64 bootstrap_stack_top = allocate_initial_kernel_stack();
|
|
CPU::bootstrap_switch_stack(bootstrap_stack_top, (void*)init_wrapper);
|
|
}
|