Files
SplenkOS/kernel/src/kernelUtil.cpp

78 lines
2.5 KiB
C++
Raw Normal View History

2026-01-28 21:32:16 +11:00
// supposed to protect against stack smashing but we have no way to actually like
// do anything when we detect it soooo...
extern "C" void __stack_chk_fail(void) {
return;
}
#include "kernelUtil.h"
2026-01-29 08:04:16 +11:00
#include "gdt/gdt.h"
2026-01-29 08:36:04 +11:00
#include "interrupts/IDT.h"
#include "interrupts/interrupts.h"
2026-01-28 21:32:16 +11:00
KernelInfo kernelInfo;
PageTableManager pageTableManager = NULL;
void PrepareMemory(BootInfo* bootInfo){
uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize;
GlobalAllocator = PageFrameAllocator();
GlobalAllocator.ReadEFIMemoryMap(bootInfo->mMap, bootInfo->mMapSize, bootInfo->mMapDescriptorSize);
uint64_t kernelSize = (uint64_t)&_KernelEnd - (uint64_t)&_KernelStart;
uint64_t kernelPages = (uint64_t)kernelSize / 4096 + 1;
GlobalAllocator.LockPages(&_KernelStart, kernelPages);
PageTable* PML4 = (PageTable*)GlobalAllocator.RequestPage();
memset(PML4, 0, 0x1000);
pageTableManager = PageTableManager(PML4);
for (uint64_t t = 0; t < GetMemorySize(bootInfo->mMap, mMapEntries, bootInfo->mMapDescriptorSize); t+= 0x1000){
pageTableManager.MapMemory((void*)t, (void*)t);
}
uint64_t fbBase = (uint64_t)bootInfo->framebuffer->BaseAddress;
uint64_t fbSize = (uint64_t)bootInfo->framebuffer->BufferSize + 0x1000;
GlobalAllocator.LockPages((void*)fbBase, fbSize/ 0x1000 + 1);
for (uint64_t t = fbBase; t < fbBase + fbSize; t += 4096){
pageTableManager.MapMemory((void*)t, (void*)t);
}
asm ("mov %0, %%cr3" : : "r" (PML4));
kernelInfo.pageTableManager = &pageTableManager;
}
2026-01-29 08:36:04 +11:00
IDTR idtr;
void PrepareInterrupts() {
idtr.Limit = 0x0FFF;
idtr.Offset = (uint64_t)GlobalAllocator.RequestPage();
memset((void*)idtr.Offset, 0, 0x1000);
2026-01-28 21:32:16 +11:00
2026-01-29 08:36:04 +11:00
IDTDescEntry* int_PageFault = (IDTDescEntry*)(idtr.Offset + 0xE * sizeof(IDTDescEntry));
int_PageFault->SetOffset((uint64_t)PageFault_Handler);
int_PageFault->type_attr = IDT_TA_InterruptGate;
int_PageFault->selector = 0x08; // kernel code segment
// load idt
asm("lidt %0" : : "m" (idtr));
}
BasicRenderer r = BasicRenderer(NULL, NULL);
2026-01-28 21:32:16 +11:00
KernelInfo InitializeKernel(BootInfo* bootInfo){
2026-01-29 08:36:04 +11:00
r = BasicRenderer(bootInfo->framebuffer, bootInfo->psf1_Font);
GlobalRenderer = &r;
2026-01-28 21:32:16 +11:00
2026-01-29 08:04:16 +11:00
GDTDescriptor gdtDescriptor;
gdtDescriptor.Size = sizeof(GDT) - 1;
gdtDescriptor.Offset = (uint64_t)&DefaultGDT;
LoadGDT(&gdtDescriptor);
2026-01-28 21:32:16 +11:00
PrepareMemory(bootInfo);
memset(bootInfo->framebuffer->BaseAddress, 0, bootInfo->framebuffer->BufferSize);
2026-01-29 08:36:04 +11:00
PrepareInterrupts();
2026-01-28 21:32:16 +11:00
return kernelInfo;
}