2022-12-23 12:09:21 +00:00
|
|
|
#include "ELF.h"
|
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"
|
2023-01-23 19:07:17 +00:00
|
|
|
#include "arch/PCI.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-03 16:25:25 +00:00
|
|
|
#include "config.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-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"
|
2023-02-27 15:23:51 +00:00
|
|
|
#include "video/Framebuffer.h"
|
|
|
|
#include "video/TextConsole.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 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
|
|
|
|
|
|
|
kernel_sleep(250);
|
|
|
|
}
|
2022-12-16 17:18:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 15:23:51 +00:00
|
|
|
static void identify_processor()
|
2022-09-05 14:13:51 +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)"));
|
2023-02-27 15:23:51 +00:00
|
|
|
}
|
2022-11-13 13:29:15 +00:00
|
|
|
|
2023-02-27 15:23:51 +00:00
|
|
|
static void identify_memory()
|
|
|
|
{
|
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());
|
2023-02-27 15:23:51 +00:00
|
|
|
}
|
2022-11-30 11:42:11 +00:00
|
|
|
|
2023-02-27 15:23:51 +00:00
|
|
|
static Result<void> try_init_vfs()
|
|
|
|
{
|
2023-02-03 21:18:52 +00:00
|
|
|
VFS::root_fs = TRY(TmpFS::FileSystem::create());
|
|
|
|
|
2023-03-18 08:10:33 +00:00
|
|
|
TRY(DeviceRegistry::init());
|
|
|
|
|
2023-03-11 09:23:46 +00:00
|
|
|
InitRD::populate_vfs();
|
2023-03-10 23:52:39 +00:00
|
|
|
|
2023-02-27 15:23:51 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_vfs()
|
|
|
|
{
|
|
|
|
try_init_vfs().release_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Result<void> try_init_userspace()
|
|
|
|
{
|
2023-03-18 18:23:18 +00:00
|
|
|
auto init = TRY(VFS::resolve_path("/bin/init"));
|
2022-12-19 11:24:15 +00:00
|
|
|
|
2023-03-18 18:23:18 +00:00
|
|
|
TRY(Scheduler::new_userspace_thread(init));
|
2022-12-18 15:39:35 +00:00
|
|
|
|
2023-02-27 15:23:51 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_userspace()
|
|
|
|
{
|
|
|
|
try_init_userspace().release_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void create_reaper()
|
|
|
|
{
|
|
|
|
Scheduler::new_kernel_thread(reap_thread).release_value();
|
|
|
|
}
|
2022-12-07 14:04:11 +00:00
|
|
|
|
2023-02-27 15:23:51 +00:00
|
|
|
static void scan_pci()
|
|
|
|
{
|
2023-01-23 20:24:05 +00: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 });
|
2023-02-27 15:23:51 +00:00
|
|
|
}
|
2023-01-23 19:07:17 +00:00
|
|
|
|
2023-02-27 15:23:51 +00:00
|
|
|
static void transfer_control()
|
|
|
|
{
|
2023-01-08 14:32:59 +00:00
|
|
|
// Disable console logging before transferring control to userspace.
|
|
|
|
setup_log(log_debug_enabled(), log_serial_enabled(), false);
|
|
|
|
|
2022-12-07 14:04:11 +00:00
|
|
|
CPU::enable_interrupts();
|
2023-02-27 15:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SplashAction
|
|
|
|
{
|
|
|
|
const char* message;
|
|
|
|
void (*action)(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr SplashAction actions[] = {
|
|
|
|
{ "Identify Processor", identify_processor }, { "Initialize Timer", Timer::init },
|
|
|
|
{ "Identify System Memory", identify_memory }, { "Initialize Threads", Thread::init },
|
|
|
|
{ "Initialize Scheduler", Scheduler::init }, { "Initialize File System", init_vfs },
|
|
|
|
{ "Initialize Userspace", init_userspace }, { "Create Reaper Thread", create_reaper },
|
|
|
|
{ "Find Available Devices", scan_pci }, { "Final CPU Initialization", CPU::platform_finish_init },
|
|
|
|
{ "Start Userspace", transfer_control },
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr u32 WHITE = 0xffffffff;
|
|
|
|
static constexpr u32 RED = 0xffff0000;
|
|
|
|
static constexpr u32 BLACK = 0xff000000;
|
|
|
|
|
|
|
|
static void update_splash(const char* message, u32 current, u32 total)
|
|
|
|
{
|
|
|
|
const u32 line_height = Framebuffer::height() / 2;
|
|
|
|
const u32 line_begin = 100;
|
|
|
|
const u32 line_length = Framebuffer::width() - 200;
|
|
|
|
|
|
|
|
if (current != total)
|
|
|
|
{
|
|
|
|
const u32 line_completed = (line_length / total) * current;
|
|
|
|
|
|
|
|
Framebuffer::rect(line_begin, line_height, line_completed, 2, WHITE);
|
|
|
|
Framebuffer::rect(line_begin + line_completed, line_height, line_length - line_completed, 2, RED);
|
|
|
|
}
|
|
|
|
else { Framebuffer::rect(line_begin, line_height, line_length, 2, WHITE); }
|
|
|
|
|
|
|
|
Framebuffer::rect(line_begin, line_height + 20, line_length, 16, BLACK);
|
|
|
|
|
|
|
|
TextConsole::move_to(Framebuffer::width() / 2 - 100, line_height + 20);
|
2023-02-27 15:27:59 +00:00
|
|
|
TextConsole::printf("%s (%d%%)", message, (100 / total) * current).release_value();
|
2023-02-27 15:23:51 +00:00
|
|
|
|
|
|
|
TextConsole::move_to(0, 0);
|
|
|
|
|
|
|
|
for (int i = 0; i < 3000000; i++) CPU::pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<void> init()
|
|
|
|
{
|
|
|
|
kinfoln("Starting Moon %s, built on %s at %s", MOON_VERSION, __DATE__, __TIME__);
|
|
|
|
|
|
|
|
constexpr usize total_actions = sizeof(actions) / sizeof(actions[0]);
|
|
|
|
|
|
|
|
for (usize i = 0; i < total_actions; i++)
|
|
|
|
{
|
|
|
|
#ifndef DEBUG_MODE
|
|
|
|
update_splash(actions[i].message, (u32)i, (u32)(total_actions - 1));
|
|
|
|
#else
|
|
|
|
(void)update_splash;
|
|
|
|
#endif
|
|
|
|
actions[i].action();
|
|
|
|
}
|
2022-12-07 14:04:11 +00:00
|
|
|
|
2022-11-20 16:56:07 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-02-25 16:09:03 +00:00
|
|
|
[[noreturn]] void init_wrapper()
|
2022-11-20 16:56:07 +00:00
|
|
|
{
|
|
|
|
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();
|
2023-01-02 12:07:29 +00:00
|
|
|
}
|
2023-02-25 16:09:03 +00:00
|
|
|
|
2023-02-25 16:42:32 +00:00
|
|
|
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.
|
|
|
|
kdbgln("stack guard page: %p", (void*)address);
|
|
|
|
|
|
|
|
// The actual stack.
|
|
|
|
Stack stack { address + ARCH_PAGE_SIZE, BOOTSTRAP_STACK_PAGES * ARCH_PAGE_SIZE };
|
|
|
|
|
|
|
|
return stack.top();
|
|
|
|
}
|
|
|
|
|
2023-02-25 16:09:03 +00:00
|
|
|
extern "C" [[noreturn]] void _start()
|
|
|
|
{
|
|
|
|
Init::check_magic();
|
|
|
|
Init::early_init();
|
2023-02-25 16:42:32 +00:00
|
|
|
u64 bootstrap_stack_top = allocate_initial_kernel_stack();
|
|
|
|
CPU::bootstrap_switch_stack(bootstrap_stack_top, (void*)init_wrapper);
|
2023-02-25 16:09:03 +00:00
|
|
|
}
|