diff --git a/OVMFbin/OVMF_VARS-pure-efi.fd b/OVMFbin/OVMF_VARS-pure-efi.fd index dc5d36f..f6bce0b 100644 Binary files a/OVMFbin/OVMF_VARS-pure-efi.fd and b/OVMFbin/OVMF_VARS-pure-efi.fd differ diff --git a/kernel/bin/CustomOS.img b/kernel/bin/CustomOS.img index 4b24e93..6a76328 100644 Binary files a/kernel/bin/CustomOS.img and b/kernel/bin/CustomOS.img differ diff --git a/kernel/bin/kernel.elf b/kernel/bin/kernel.elf index 3afc65f..6de9165 100755 Binary files a/kernel/bin/kernel.elf and b/kernel/bin/kernel.elf differ diff --git a/kernel/lib/PageFrameAllocator.o b/kernel/lib/PageFrameAllocator.o deleted file mode 100644 index c0e72b0..0000000 Binary files a/kernel/lib/PageFrameAllocator.o and /dev/null differ diff --git a/kernel/lib/kernel.o b/kernel/lib/kernel.o index 7cf5c3a..667557c 100644 Binary files a/kernel/lib/kernel.o and b/kernel/lib/kernel.o differ diff --git a/kernel/lib/kernelUtil.o b/kernel/lib/kernelUtil.o new file mode 100644 index 0000000..71589d4 Binary files /dev/null and b/kernel/lib/kernelUtil.o differ diff --git a/kernel/src/kernel.cpp b/kernel/src/kernel.cpp index e934860..9e0ae7d 100644 --- a/kernel/src/kernel.cpp +++ b/kernel/src/kernel.cpp @@ -1,81 +1,13 @@ -#include -#include -#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; +#include "kernelUtil.h" extern "C" void _start(BootInfo* bootInfo) { - - - + KernelInfo kernelInfo = InitializeKernel(bootInfo); 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}; - // display information of memory map - uint64_t mMapEntries = bootInfo->mMapSize / bootInfo->mMapDescriptorSize; - - 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; + // make sure we never return from our OS + while (true); } \ No newline at end of file diff --git a/kernel/src/kernelUtil.cpp b/kernel/src/kernelUtil.cpp new file mode 100644 index 0000000..10347aa --- /dev/null +++ b/kernel/src/kernelUtil.cpp @@ -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; +} \ No newline at end of file diff --git a/kernel/src/kernelUtil.h b/kernel/src/kernelUtil.h new file mode 100644 index 0000000..9beba51 --- /dev/null +++ b/kernel/src/kernelUtil.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#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); \ No newline at end of file