#include "Log.h" #include "arch/CPU.h" #include "arch/MMU.h" #include "arch/Serial.h" #include "arch/Timer.h" #include "boot/Init.h" #include "boot/bootboot.h" #include "config.h" #include "memory/Heap.h" #include "memory/KernelVM.h" #include "memory/MemoryManager.h" #include "thread/Scheduler.h" #include #include #include extern const BOOTBOOT bootboot; void async_thread() { while (true) { Thread* current = Scheduler::current(); kinfoln("Ticks: %lu, %lu user, %lu kernel, %lu idle", current->ticks, current->ticks_in_user, current->ticks_in_kernel, Scheduler::idle()->ticks); CPU::wait_for_interrupt(); kernel_sleep(1000); } } void heap_thread() { CPU::disable_interrupts(); dump_heap_usage(); kdbgln("Kernel uses %lu vm pages", KernelVM::used() / ARCH_PAGE_SIZE); kernel_exit(); } void reap_thread() { while (true) { CPU::disable_interrupts(); auto dying_threads = Scheduler::check_for_dying_threads(); CPU::enable_interrupts(); if (dying_threads.count()) { Thread* thread_to_reap = dying_threads.expect_first(); Thread* next_thread = thread_to_reap; while (thread_to_reap) { next_thread = dying_threads.next(thread_to_reap).value_or(nullptr); kinfoln("reap: reaping thread with id %zu", thread_to_reap->id); auto stack = thread_to_reap->stack; kinfoln("deleting thread stack @ %#lx, has %zu bytes of stack", stack.bottom(), stack.bytes()); MemoryManager::unmap_owned_and_free_vm(stack.bottom(), stack.bytes() / ARCH_PAGE_SIZE).release_value(); delete thread_to_reap; thread_to_reap = next_thread; } } kernel_sleep(250); } } Result 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()); TarStream stream((void*)bootboot.initrd_ptr, bootboot.initrd_size); TarStream::Entry entry; while (TRY(stream.read_next_entry().try_set_value_with_specific_error(entry, 0))) { if (entry.type == TarStream::EntryType::RegularFile) kinfoln("Found file %s in initial ramdisk, of size %s", entry.name, to_dynamic_unit(entry.size).release_value().chars()); } Thread::init(); Scheduler::init(); TRY(Scheduler::new_kernel_thread(async_thread)); TRY(Scheduler::new_kernel_thread(heap_thread)); TRY(Scheduler::new_kernel_thread(reap_thread)); CPU::platform_finish_init(); CPU::enable_interrupts(); return {}; } extern "C" [[noreturn]] void _start() { Init::check_magic(); Init::early_init(); auto rc = init(); if (rc.has_error()) kerrorln("Runtime error: %s", rc.error_string()); CPU::idle_loop(); }