Files
SplenkOS/kernel/src/kernelUtil.cpp

51 lines
1.6 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"
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;
}
KernelInfo InitializeKernel(BootInfo* bootInfo){
PrepareMemory(bootInfo);
memset(bootInfo->framebuffer->BaseAddress, 0, bootInfo->framebuffer->BufferSize);
return kernelInfo;
}