Fix kernel mapping

This commit is contained in:
Alex 2023-03-27 20:30:19 +03:00
parent 98677c7b5b
commit 678744f65d
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
3 changed files with 11 additions and 4 deletions

View File

@ -136,6 +136,7 @@ NIF void MapKernel(PageTable4 *PT, BootInfo *Info)
uintptr_t BaseKernelMapAddress = (uintptr_t)Info->Kernel.PhysicalBase; uintptr_t BaseKernelMapAddress = (uintptr_t)Info->Kernel.PhysicalBase;
uintptr_t k; uintptr_t k;
/* Text section */
for (k = KernelStart; k < KernelTextEnd; k += PAGE_SIZE) for (k = KernelStart; k < KernelTextEnd; k += PAGE_SIZE)
{ {
va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::RW); va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::RW);
@ -143,6 +144,7 @@ NIF void MapKernel(PageTable4 *PT, BootInfo *Info)
BaseKernelMapAddress += PAGE_SIZE; BaseKernelMapAddress += PAGE_SIZE;
} }
/* Data section */
for (k = KernelTextEnd; k < KernelDataEnd; k += PAGE_SIZE) for (k = KernelTextEnd; k < KernelDataEnd; k += PAGE_SIZE)
{ {
va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::RW | PTFlag::G); va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::RW | PTFlag::G);
@ -150,13 +152,15 @@ NIF void MapKernel(PageTable4 *PT, BootInfo *Info)
BaseKernelMapAddress += PAGE_SIZE; BaseKernelMapAddress += PAGE_SIZE;
} }
/* Read only data section */
for (k = KernelDataEnd; k < KernelRoDataEnd; k += PAGE_SIZE) for (k = KernelDataEnd; k < KernelRoDataEnd; k += PAGE_SIZE)
{ {
va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::P | PTFlag::G); va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::G);
KernelAllocator.LockPage((void *)BaseKernelMapAddress); KernelAllocator.LockPage((void *)BaseKernelMapAddress);
BaseKernelMapAddress += PAGE_SIZE; BaseKernelMapAddress += PAGE_SIZE;
} }
/* BSS section */
for (k = KernelRoDataEnd; k < KernelEnd; k += PAGE_SIZE) for (k = KernelRoDataEnd; k < KernelEnd; k += PAGE_SIZE)
{ {
va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::RW | PTFlag::G); va.Map((void *)k, (void *)BaseKernelMapAddress, PTFlag::RW | PTFlag::G);
@ -164,14 +168,15 @@ NIF void MapKernel(PageTable4 *PT, BootInfo *Info)
BaseKernelMapAddress += PAGE_SIZE; BaseKernelMapAddress += PAGE_SIZE;
} }
/* Kernel file */
for (k = KernelFileStart; k < KernelFileEnd; k += PAGE_SIZE) for (k = KernelFileStart; k < KernelFileEnd; k += PAGE_SIZE)
{ {
va.Map((void *)k, (void *)k, PTFlag::RW | PTFlag::G); va.Map((void *)k, (void *)k, PTFlag::G);
KernelAllocator.LockPage((void *)k); KernelAllocator.LockPage((void *)k);
} }
debug("\nStart: %#llx - Text End: %#llx - RoEnd: %#llx - End: %#llx\nStart Physical: %#llx - End Physical: %#llx", debug("\nStart: %#llx - Text End: %#llx - RoEnd: %#llx - End: %#llx\nStart Physical: %#llx - End Physical: %#llx",
KernelStart, KernelTextEnd, KernelRoDataEnd, KernelEnd, Info->Kernel.PhysicalBase, BaseKernelMapAddress - PAGE_SIZE); KernelStart, KernelTextEnd, KernelRoDataEnd, KernelEnd, KernelFileStart, KernelFileEnd);
#ifdef DEBUG #ifdef DEBUG
if (EnableExternalMemoryTracer) if (EnableExternalMemoryTracer)

View File

@ -49,6 +49,8 @@ namespace Memory
return; return;
} }
Flags |= PTFlag::P;
PageMapIndexer Index = PageMapIndexer((uintptr_t)VirtualAddress); PageMapIndexer Index = PageMapIndexer((uintptr_t)VirtualAddress);
// Clear any flags that are not 1 << 0 (Present) - 1 << 5 (Accessed) because rest are for page table entries only // Clear any flags that are not 1 << 0 (Present) - 1 << 5 (Accessed) because rest are for page table entries only
uint64_t DirectoryFlags = Flags & 0x3F; uint64_t DirectoryFlags = Flags & 0x3F;

View File

@ -546,7 +546,7 @@ namespace Memory
* @param PhysicalAddress Physical address of the page. * @param PhysicalAddress Physical address of the page.
* @param Flags Flags of the page. Check PTFlag enum. * @param Flags Flags of the page. Check PTFlag enum.
*/ */
void Map(void *VirtualAddress, void *PhysicalAddress, uint64_t Flags); void Map(void *VirtualAddress, void *PhysicalAddress, uint64_t Flag = PTFlag::P);
/** /**
* @brief Map multiple pages. * @brief Map multiple pages.