Kernel: Improve MMU

We don't have to invalidate page in AllocatePage() if we don't make
any changes. We also should not assert on deallocating non-present
pages, just return early :)
This commit is contained in:
Bananymous 2023-01-26 02:35:11 +02:00
parent 589f338357
commit 35d2e27292
2 changed files with 15 additions and 8 deletions

View File

@ -112,10 +112,12 @@ void MMU::UnAllocatePage(uintptr_t address)
uint32_t pte = (address & 0x001FF000) >> 12;
uint64_t* page_directory = (uint64_t*)(m_highest_paging_struct[pdpte] & PAGE_MASK);
ASSERT(page_directory[pde] & PRESENT);
if (!(page_directory[pde] & PRESENT))
return;
uint64_t* page_table = (uint64_t*)(page_directory[pde] & PAGE_MASK);
ASSERT(page_table[pte] & PRESENT);
if (!(page_table[pte] & PRESENT))
return;
page_table[pte] = 0;

View File

@ -97,10 +97,11 @@ void MMU::AllocatePage(uintptr_t address)
uint64_t* pt = (uint64_t*)(pd[pde] & PAGE_MASK);
if (!(pt[pte] & PRESENT))
{
pt[pte] = address | READ_WRITE | PRESENT;
asm volatile("invlpg (%0)" :: "r"(address) : "memory");
}
}
void MMU::AllocateRange(uintptr_t address, ptrdiff_t size)
{
@ -122,16 +123,20 @@ void MMU::UnAllocatePage(uintptr_t address)
uint64_t pte = (address >> 12) & 0x1FF;
uint64_t* pml4 = m_highest_paging_struct;
ASSERT(pml4[pml4e] & PRESENT);
if (!(pml4[pml4e] & PRESENT))
return;
uint64_t* pdpt = (uint64_t*)(pml4[pml4e] & PAGE_MASK);
ASSERT(pdpt[pdpte] & PRESENT);
if (!(pdpt[pdpte] & PRESENT))
return;
uint64_t* pd = (uint64_t*)(pdpt[pdpte] & PAGE_MASK);
ASSERT(pd[pde] & PRESENT);
if (!(pd[pde] & PRESENT))
return;
uint64_t* pt = (uint64_t*)(pd[pde] & PAGE_MASK);
ASSERT(pt[pte] & PRESENT);
if (!(pt[pte] & PRESENT))
return;
pt[pte] = 0;