Luna/kernel/src/main.cpp

151 lines
4.3 KiB
C++
Raw Normal View History

2022-09-07 13:02:23 +00:00
#define MODULE "main"
2022-09-05 14:13:51 +00:00
#include "acpi/RSDT.h"
#include "assert.h"
#include "bootboot.h"
#include "cpu/CPU.h"
#include "debug.h"
#include "gdt/GDT.h"
#include "init/Init.h"
#include "init/InitRD.h"
#include "interrupts/IDT.h"
#include "interrupts/Install.h"
#include "interrupts/Interrupts.h"
#include "io/PIC.h"
#include "io/Serial.h"
2022-09-07 08:33:22 +00:00
#include "log/Address.h"
2022-09-05 14:13:51 +00:00
#include "log/Log.h"
#include "memory/KernelHeap.h"
#include "memory/KernelMemoryManager.h"
#include "memory/Memory.h"
#include "memory/RangeAllocator.h"
2022-09-05 14:13:51 +00:00
#include "panic/hang.h"
#include "power/shutdown.h"
2022-09-05 14:13:51 +00:00
#include "render/BBRenderer.h"
#include "render/Draw.h"
#include "render/TextRenderer.h"
#include "scheduling/PIT.h"
#include "std/stdio.h"
#include "std/stdlib.h"
#include "std/string.h"
extern BOOTBOOT bootboot;
extern "C" void _start()
{
Init::check_magic();
Init::disable_smp(); // Put all other cores except the bootstrap one in an infinite loop
Debug::DebugStatus::the()->Init();
Debug::DebugStatus::the()->StartBootStage(Color::White);
Init::early_init();
Debug::DebugStatus::the()->PassBootStage(Color::White);
kinfoln("Hello World!");
Debug::DebugStatus::the()->StartBootStage(Color::Gray);
GDT::load();
Debug::DebugStatus::the()->PassBootStage(Color::Gray);
kinfoln("Loaded GDT");
Debug::DebugStatus::the()->StartBootStage(Color::Cyan);
Interrupts::install();
Debug::DebugStatus::the()->PassBootStage(Color::Cyan);
Debug::DebugStatus::the()->StartBootStage(Color::Blue);
IDT::load();
Debug::DebugStatus::the()->PassBootStage(Color::Blue);
kinfoln("Loaded IDT");
Debug::DebugStatus::the()->StartBootStage(Color::Green);
PIC::remap();
PIC::enable_master(0b11111100); // enable keyboard and PIT
PIC::enable_slave(0b11111111);
Debug::DebugStatus::the()->PassBootStage(Color::Green);
kinfoln("Prepared PIC");
Debug::DebugStatus::the()->StartBootStage(Color::Magenta);
PIT::initialize(100); // 100 times per second
Debug::DebugStatus::the()->PassBootStage(Color::Magenta);
kinfoln("Prepared PIT");
Debug::DebugStatus::the()->StartBootStage(Color::Yellow);
Interrupts::enable();
Debug::DebugStatus::the()->PassBootStage(Color::Yellow);
kinfoln("Interrupts enabled");
Debug::DebugStatus::the()->StartBootStage(Color{0x33, 0x33, 0x00, 0xFF});
2022-09-07 08:33:22 +00:00
ACPI::SDTHeader* rootSDT = (ACPI::SDTHeader*)KernelMemoryManager::get_unaligned_mapping(ACPI::GetRSDTOrXSDT());
2022-09-05 14:13:51 +00:00
bool isXSDT = false;
if (strncmp(rootSDT->Signature, "XSDT", 4) != 0)
{
if (strncmp(rootSDT->Signature, "RSDT", 4) != 0)
{
kerrorln("Invalid RootSDT signature");
Debug::DebugStatus::the()->FailBootStage();
while (1) halt();
}
}
else
isXSDT = true;
if (ACPI::ValidateSDTHeader(rootSDT))
{
2022-09-07 13:02:23 +00:00
kinfoln("%s: 0x%lx", isXSDT ? "XSDT is valid" : "RSDT is valid", (uint64_t)rootSDT);
2022-09-05 14:13:51 +00:00
Debug::DebugStatus::the()->PassBootStage(Color{0x33, 0x33, 0x00, 0xFF});
}
else
{
kinfoln(isXSDT ? "Invalid XSDT :(" : "Invalid RSDT :(");
Debug::DebugStatus::the()->FailBootStage();
while (1) halt();
}
Debug::DebugStatus::the()->StartBootStage(Color{0x00, 0x66, 0xCC, 0xFF});
ACPI::SDTHeader* fadt = (ACPI::SDTHeader*)ACPI::FindTable(rootSDT, "FACP");
if (!fadt)
{
kerrorln("FADT not found");
Debug::DebugStatus::the()->FailBootStage();
while (1) halt();
}
if (strncmp(fadt->Signature, "FACP", 4) != 0)
{
kerrorln("Invalid FADT signature");
Debug::DebugStatus::the()->FailBootStage();
while (1) halt();
}
else
{
if (ACPI::ValidateSDTHeader(fadt))
{
kinfoln("FADT is valid");
Debug::DebugStatus::the()->PassBootStage(Color{0x00, 0x66, 0xCC, 0xFF});
}
else
{
kinfoln("Invalid FADT :(");
Debug::DebugStatus::the()->FailBootStage();
while (1) halt();
}
}
2022-09-07 08:33:22 +00:00
ACPI::SDTHeader* madt = (ACPI::SDTHeader*)ACPI::FindTable(rootSDT, "APIC");
if (!madt)
{
kerrorln("MADT not found");
while (1) halt();
}
KernelMemoryManager::release_unaligned_mapping(rootSDT);
/*sleep(2500);
shutdown();*/
2022-09-06 09:48:06 +00:00
2022-09-05 14:13:51 +00:00
while (1) halt();
loop:
goto loop;
}