Luna/kernel/src/main.cpp

148 lines
3.8 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"
2022-09-11 06:23:32 +00:00
#include "assert.h"
2022-09-07 13:02:54 +00:00
#include "config.h"
2022-09-05 14:13:51 +00:00
#include "cpu/CPU.h"
#include "gdt/GDT.h"
#include "init/Init.h"
2022-09-14 15:55:24 +00:00
#include "init/InitRD.h"
2022-09-05 14:13:51 +00:00
#include "interrupts/IDT.h"
#include "interrupts/Install.h"
#include "interrupts/Interrupts.h"
2022-09-21 15:57:02 +00:00
#include "io/PCI.h"
2022-09-05 14:13:51 +00:00
#include "io/PIC.h"
#include "io/Serial.h"
2022-09-05 14:13:51 +00:00
#include "log/Log.h"
#include "memory/Memory.h"
#include "memory/MemoryManager.h"
#include "memory/MemoryMap.h"
#include "memory/PMM.h"
2022-09-23 14:41:43 +00:00
#include "memory/VMM.h"
#include "misc/PCITypes.h"
2022-09-21 15:56:53 +00:00
#include "misc/reboot.h"
2022-09-25 14:56:00 +00:00
#include "panic/Panic.h"
2022-09-14 16:54:40 +00:00
#include "rand/Mersenne.h"
2022-09-10 16:42:40 +00:00
#include "render/Framebuffer.h"
2022-09-11 06:23:32 +00:00
#include "render/TextRenderer.h"
2022-09-05 14:13:51 +00:00
#include "std/stdio.h"
#include "std/stdlib.h"
#include "std/string.h"
2022-09-21 15:56:53 +00:00
#include "thread/PIT.h"
#include "thread/Scheduler.h"
2022-09-05 14:13:51 +00:00
extern "C" void _start()
{
Init::check_magic();
Init::disable_smp(); // Put all other cores except the bootstrap one in an infinite loop
Init::early_init();
kinfoln("Starting Moon %s", moon_version());
CPU::log_cpu_information();
Memory::walk_memory_map();
2022-09-05 14:13:51 +00:00
GDT::load();
kinfoln("Loaded GDT");
Interrupts::install();
IDT::load();
kinfoln("Loaded IDT");
PIC::remap();
PIC::enable_master(0b11111100); // enable keyboard and PIT
PIC::enable_slave(0b11111111);
kinfoln("Prepared PIC");
2022-09-21 05:29:44 +00:00
PIT::initialize(500); // 500 times per second
2022-09-05 14:13:51 +00:00
kinfoln("Prepared PIT");
Scheduler::init();
2022-09-05 14:13:51 +00:00
kinfoln("Prepared scheduler");
Scheduler::add_kernel_task([]() {
2022-09-20 18:49:31 +00:00
int64_t x = 0;
int64_t y = 0;
int64_t xvel = 10;
int64_t yvel = 10;
while (1)
{
2022-09-20 18:49:31 +00:00
sleep(5);
uint32_t color = (uint32_t)Mersenne::get();
uint32_t* colptr = &color;
x += xvel;
y += yvel;
if ((x + 10) >= framebuffer0.width())
{
xvel = -xvel;
x = (framebuffer0.width() - 10);
}
if ((y + 10) >= framebuffer0.height())
{
yvel = -yvel;
y = (framebuffer0.height() - 10);
}
if (xvel < 0 && (x - 10) < 0)
{
xvel = -xvel;
x = 0;
}
if (yvel < 0 && (y - 10) < 0)
{
yvel = -yvel;
y = 0;
}
framebuffer0.paint_rect(x, y, 10, 10, *(Color*)colptr);
}
});
Scheduler::add_kernel_task([]() {
while (1)
{
2022-09-20 18:49:31 +00:00
sleep(200);
uint32_t color = (uint32_t)Mersenne::get();
uint32_t* colptr = &color;
framebuffer0.paint_rect(Mersenne::get() % (framebuffer0.width() - 256),
Mersenne::get() % (framebuffer0.height() - 256), Mersenne::get() % 255,
Mersenne::get() % 255, *(Color*)colptr);
}
});
Scheduler::add_kernel_task([]() {
while (1)
{
sleep(400);
Scheduler::reap_tasks();
}
});
kinfoln("Prepared scheduler tasks");
2022-09-05 14:13:51 +00:00
ACPI::SDTHeader* rootSDT = ACPI::GetRSDTOrXSDT();
2022-09-10 16:42:40 +00:00
bool isXSDT = ACPI::IsXSDT();
if (!ACPI::ValidateRSDTOrXSDT(rootSDT)) kerrorln("Invalid %s", isXSDT ? "XSDT" : "RSDT");
2022-09-07 08:33:22 +00:00
2022-09-10 16:42:40 +00:00
framebuffer0.clear(Color::Cyan);
Interrupts::enable();
2022-09-06 09:48:06 +00:00
kinfoln("Interrupts enabled");
2022-09-23 15:24:45 +00:00
PCI::scan([](PCI::Device& dev) {
kinfoln("Found PCI device %x:%x, %s", dev.id().vendor, dev.id().device, pci_type_name(dev.type()));
2022-09-21 15:57:02 +00:00
});
kdbgln("System memory: %ld KB", Memory::get_system() / 1024);
kdbgln(" Free memory : %ld KB", PMM::get_free() / 1024);
kdbgln(" Used memory : %ld KB", PMM::get_used() / 1024);
kdbgln(" Reserved memory : %ld KB", PMM::get_reserved() / 1024);
Scheduler::exit();
2022-09-05 14:13:51 +00:00
}