kernel code cleanup

This commit is contained in:
2026-01-28 21:32:16 +11:00
parent a265558b98
commit 1326696724
9 changed files with 87 additions and 74 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
kernel/lib/kernelUtil.o Normal file

Binary file not shown.

View File

@@ -1,81 +1,13 @@
#include <stddef.h> #include "kernelUtil.h"
#include <stdint.h>
#include "BasicRenderer.h"
#include "cstr.h"
#include "efiMemory.h"
#include "memory.h"
#include "Bitmap.h"
#include "paging/PageFrameAllocator.h"
#include "paging/PageMapIndexer.h"
#include "paging/paging.h"
#include "paging/PageTableManager.h"
struct BootInfo {
Framebuffer* framebuffer;
PSF1_FONT* psf1_Font;
EFI_MEMORY_DESCRIPTOR* mMap;
uint64_t mMapSize;
uint64_t mMapDescriptorSize;
};
// 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;
}
extern uint64_t _KernelStart;
extern uint64_t _KernelEnd;
extern "C" void _start(BootInfo* bootInfo) { extern "C" void _start(BootInfo* bootInfo) {
KernelInfo kernelInfo = InitializeKernel(bootInfo);
BasicRenderer newRenderer = BasicRenderer(bootInfo->framebuffer, bootInfo->psf1_Font); BasicRenderer newRenderer = BasicRenderer(bootInfo->framebuffer, bootInfo->psf1_Font);
PageTableManager* pageTableManager = kernelInfo.pageTableManager;
newRenderer.Print("Setting up memory map..."); newRenderer.Print("Kernel initialized successfully! (we didn't crash lol)");
newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18}; newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18};
// display information of memory map // make sure we never return from our OS
uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize; while (true);
GlobalAllocator = PageFrameAllocator();
GlobalAllocator.ReadEFIMemoryMap(bootInfo->mMap, bootInfo->mMapSize, bootInfo->mMapDescriptorSize);
// ensure we don't absolutely fuck shit up
uint64_t kernelSize = (uint64_t)&_KernelEnd - (uint64_t)&_KernelStart;
uint64_t kernelPages = (uint64_t)kernelSize / 4096 + 1;
GlobalAllocator.LockPages(&_KernelStart, kernelPages);
// create page map
PageTable* PML4 = (PageTable*)GlobalAllocator.RequestPage();
memset(PML4, 0, 0x1000);
PageTableManager 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));
memset(bootInfo->framebuffer->BaseAddress, 0, bootInfo->framebuffer->BufferSize);
newRenderer.Print("Page map set up!");
pageTableManager.MapMemory((void*)0x600000000, (void*)0x80000);
uint64_t* test = (uint64_t*)0x600000000;
*test = 26;
newRenderer.cursorPosition = {0, newRenderer.cursorPosition.y + 18};
newRenderer.Print("this should be \"26\": ");
newRenderer.Print(to_string(*test));
return;
} }

51
kernel/src/kernelUtil.cpp Normal file
View File

@@ -0,0 +1,51 @@
// 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;
}

30
kernel/src/kernelUtil.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#include "BasicRenderer.h"
#include "cstr.h"
#include "efiMemory.h"
#include "memory.h"
#include "Bitmap.h"
#include "paging/PageFrameAllocator.h"
#include "paging/PageMapIndexer.h"
#include "paging/paging.h"
#include "paging/PageTableManager.h"
struct BootInfo {
Framebuffer* framebuffer;
PSF1_FONT* psf1_Font;
EFI_MEMORY_DESCRIPTOR* mMap;
uint64_t mMapSize;
uint64_t mMapDescriptorSize;
};
extern uint64_t _KernelStart;
extern uint64_t _KernelEnd;
struct KernelInfo {
PageTableManager* pageTableManager;
};
KernelInfo InitializeKernel(BootInfo* bootInfo);