Fix paging breaking on VirtualBox. Dynamically figure out the starting location.

This commit is contained in:
Curle 2021-06-12 01:49:43 +01:00
parent 54559a977d
commit 77933c3aa4
Signed by: TheCurle
GPG Key ID: 5942F13718443F79

View File

@ -39,8 +39,10 @@ void InitPaging() {
.PML4 = (size_t*) ReadControlRegister(3)
};
size_t AddressToFind = (size_t) &fb;
size_t AddressToFind = KernelAddr + 0x2000;
size_t BootldrAddress = 0x8000;
KernelLocation = DecodeVirtualAddressNoDirect(&BootloaderAddressSpace, AddressToFind);
SerialPrintf("[ Mem] Double check: Kernel physically starts at 0x%p (0x%p), ends at 0x%p.\r\n", KernelLocation, AddressToFind, KERNEL_END);
SerialPrintf("[ Mem] Identity mapping the entirety of physical memory\r\n");
@ -48,7 +50,6 @@ void InitPaging() {
size_t Addr = i * 4096;
MapVirtualPageNoDirect(&KernelAddressSpace, Addr, Addr, DEFAULT_PAGE_FLAGS);
MapVirtualPageNoDirect(&KernelAddressSpace, Addr, TO_DIRECT(Addr), DEFAULT_PAGE_FLAGS);
// TODO: Map kernel mem
}
SerialPrintf("[ Mem] Mapping 0x%x bytes of bootloader structure, starting at 0x%p\r\n", bootldr.size, BootldrAddress);
@ -56,9 +57,11 @@ void InitPaging() {
MapVirtualPageNoDirect(&KernelAddressSpace, i, KERNEL_REGION + (i - BootldrAddress), 0x3);
// This allows the code to actually run
SerialPrintf("[ Mem] Mapping 0x%x bytes of kernel, starting at 0x%p\r\n", KernelEnd - KernelAddr, KERNEL_PHYSICAL + KERNEL_TEXT);
for(size_t i = KERNEL_PHYSICAL + KERNEL_TEXT; i < (KernelEnd - KernelAddr) + KERNEL_PHYSICAL; i += PAGE_SIZE)
MapVirtualPageNoDirect(&KernelAddressSpace, i, (i - KERNEL_PHYSICAL) + KERNEL_REGION, 0x3);
SerialPrintf("[ Mem] Mapping 0x%x bytes of kernel, starting at 0x%p\r\n", KERNEL_END - KERNEL_PHYSICAL, KERNEL_PHYSICAL);
for(size_t i = KERNEL_PHYSICAL; i < KERNEL_END; i += PAGE_SIZE)
MapVirtualPageNoDirect(&KernelAddressSpace, i, (i - KERNEL_PHYSICAL) + KERNEL_REGION + KERNEL_TEXT, 0x3);
// TODO: The above mapping loses the ELF header.
// This allows us to write to the screen
SerialPrintf("[ Mem] Mapping 0x%x bytes of framebuffer, starting at 0x%p\r\n", bootldr.fb_size, FB_PHYSICAL);
@ -74,12 +77,10 @@ void InitPaging() {
// Make sure everything is sane
SerialPrintf("[ Mem] Diagnostic: Querying existing page tables\r\n");
AddressToFind = (size_t) &(bootldr);
size_t BootloaderAddress = DecodeVirtualAddressNoDirect(&BootloaderAddressSpace, AddressToFind);
size_t KernelDisoveredAddress = DecodeVirtualAddressNoDirect(&KernelAddressSpace, AddressToFind);
SerialPrintf("[ Mem] Diagnostic: Existing pagetables put 0x%p at 0x%p + 0x%p.\r\n", AddressToFind, BootloaderAddress, AddressToFind & ~STACK_TOP);
SerialPrintf("[ Mem] Diagnostic: Our pagetables put 0x%p at 0x%p + 0x%p.\r\n", AddressToFind, KernelDisoveredAddress, AddressToFind & ~STACK_TOP);
SerialPrintf("[ Mem] %s\r\n", BootloaderAddress == KernelDisoveredAddress ? "These match. Continuing." : "These do not match. Continuing with caution..");
size_t KernelAddress = DecodeVirtualAddressNoDirect(&KernelAddressSpace, AddressToFind);
SerialPrintf("[ Mem] Diagnostic: Our pagetables put 0x%p at 0x%p + 0x%p.\r\n", AddressToFind, KernelAddress, AddressToFind & ~STACK_TOP);
SerialPrintf("[ Mem] Diagnostic: Existing pagetables put 0x%p at 0x%p + 0x%p.\r\n", AddressToFind, KERNEL_PHYSICAL, AddressToFind & ~STACK_TOP);
SerialPrintf("[ Mem] %s\r\n", KernelAddress == KERNEL_PHYSICAL ? "These match. Continuing." : "These do not match. Continuing with caution..");
//if(BootloaderAddress != KernelDisoveredAddress)
//for(;;) {}