2022-12-23 10:33:23 +00:00
|
|
|
#include "InitRD.h"
|
2022-11-30 12:29:28 +00:00
|
|
|
#include "Log.h"
|
2022-11-15 18:10:32 +00:00
|
|
|
#include "arch/CPU.h"
|
2022-12-16 18:36:38 +00:00
|
|
|
#include "arch/MMU.h"
|
2022-11-19 19:01:01 +00:00
|
|
|
#include "arch/Timer.h"
|
2022-11-19 16:59:49 +00:00
|
|
|
#include "boot/Init.h"
|
2022-12-18 15:39:35 +00:00
|
|
|
#include "boot/bootboot.h"
|
2022-12-03 16:25:25 +00:00
|
|
|
#include "config.h"
|
2022-12-16 17:18:24 +00:00
|
|
|
#include "memory/Heap.h"
|
2022-12-16 18:36:38 +00:00
|
|
|
#include "memory/KernelVM.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-19 11:24:15 +00:00
|
|
|
#include <luna/CString.h>
|
2022-12-07 10:40:02 +00:00
|
|
|
#include <luna/Result.h>
|
2022-12-07 11:25:42 +00:00
|
|
|
#include <luna/Units.h>
|
2022-10-16 13:31:58 +00:00
|
|
|
|
2022-12-18 15:39:35 +00:00
|
|
|
extern const BOOTBOOT bootboot;
|
|
|
|
|
2022-12-07 14:55:58 +00:00
|
|
|
void async_thread()
|
2022-12-07 14:04:11 +00:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2022-12-07 14:55:58 +00:00
|
|
|
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);
|
2022-12-07 14:04:11 +00:00
|
|
|
|
2022-12-07 14:55:58 +00:00
|
|
|
CPU::wait_for_interrupt();
|
|
|
|
|
|
|
|
kernel_sleep(1000);
|
2022-12-07 14:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 17:18:24 +00:00
|
|
|
void heap_thread()
|
|
|
|
{
|
|
|
|
CPU::disable_interrupts();
|
|
|
|
dump_heap_usage();
|
2022-12-17 12:48:22 +00:00
|
|
|
kdbgln("Kernel uses %lu vm pages", KernelVM::used() / ARCH_PAGE_SIZE);
|
2022-12-18 17:43:34 +00:00
|
|
|
kernel_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
kernel_sleep(250);
|
|
|
|
}
|
2022-12-16 17:18:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 16:56:07 +00:00
|
|
|
Result<void> init()
|
2022-09-05 14:13:51 +00:00
|
|
|
{
|
2022-12-18 17:51:25 +00:00
|
|
|
kinfoln("Starting Moon %s, built on %s at %s", MOON_VERSION, __DATE__, __TIME__);
|
2022-11-13 10:25:15 +00:00
|
|
|
|
2022-11-30 16:16:36 +00:00
|
|
|
kinfoln("Current platform: %s", CPU::platform_string());
|
2022-11-19 11:30:36 +00:00
|
|
|
|
2022-12-04 14:55:12 +00:00
|
|
|
kinfoln("Current processor: %s", CPU::identify().value_or("(unknown)"));
|
2022-11-13 13:29:15 +00:00
|
|
|
|
2022-11-19 19:01:01 +00:00
|
|
|
Timer::init();
|
2022-11-16 16:37:18 +00:00
|
|
|
|
2022-12-16 17:18:24 +00: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 11:42:11 +00:00
|
|
|
|
2022-12-18 15:39:35 +00:00
|
|
|
TarStream::Entry entry;
|
2022-12-23 10:33:23 +00:00
|
|
|
while (TRY(g_initrd.read_next_entry().try_set_value_with_specific_error(entry, 0)))
|
2022-12-18 15:39:35 +00:00
|
|
|
{
|
|
|
|
if (entry.type == TarStream::EntryType::RegularFile)
|
2022-12-19 11:24:15 +00:00
|
|
|
{
|
2022-12-18 15:39:35 +00:00
|
|
|
kinfoln("Found file %s in initial ramdisk, of size %s", entry.name,
|
|
|
|
to_dynamic_unit(entry.size).release_value().chars());
|
2022-12-19 11:24:15 +00:00
|
|
|
|
|
|
|
if (!strcmp(entry.name, "sys/config"))
|
|
|
|
{
|
2022-12-23 10:33:23 +00:00
|
|
|
auto contents = TRY(g_initrd.read_contents_as_string(entry, 0, entry.size));
|
2022-12-19 11:24:15 +00:00
|
|
|
|
|
|
|
kinfoln("%s", contents.chars());
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 15:39:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 09:50:49 +00:00
|
|
|
Thread::init();
|
2022-12-07 14:04:11 +00:00
|
|
|
Scheduler::init();
|
|
|
|
|
2022-12-07 14:55:58 +00:00
|
|
|
TRY(Scheduler::new_kernel_thread(async_thread));
|
2022-12-16 17:18:24 +00:00
|
|
|
TRY(Scheduler::new_kernel_thread(heap_thread));
|
2022-12-18 17:43:34 +00:00
|
|
|
TRY(Scheduler::new_kernel_thread(reap_thread));
|
2022-12-07 14:04:11 +00:00
|
|
|
|
|
|
|
CPU::platform_finish_init();
|
|
|
|
|
|
|
|
CPU::enable_interrupts();
|
|
|
|
|
2022-11-20 16:56:07 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" [[noreturn]] void _start()
|
|
|
|
{
|
|
|
|
Init::check_magic();
|
|
|
|
Init::early_init();
|
|
|
|
auto rc = init();
|
2022-11-30 16:16:36 +00:00
|
|
|
if (rc.has_error()) kerrorln("Runtime error: %s", rc.error_string());
|
2022-12-07 14:04:11 +00:00
|
|
|
CPU::idle_loop();
|
2022-09-05 14:13:51 +00:00
|
|
|
}
|