From 35d2e272921b88bfdc67bfceec3ec91793ec08ac Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 26 Jan 2023 02:35:11 +0200 Subject: [PATCH] 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 :) --- kernel/arch/i386/MMU.cpp | 6 ++++-- kernel/arch/x86_64/MMU.cpp | 17 +++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/kernel/arch/i386/MMU.cpp b/kernel/arch/i386/MMU.cpp index 593b826c06..f435e886bc 100644 --- a/kernel/arch/i386/MMU.cpp +++ b/kernel/arch/i386/MMU.cpp @@ -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; diff --git a/kernel/arch/x86_64/MMU.cpp b/kernel/arch/x86_64/MMU.cpp index 1e122ad5ef..ac05c32c17 100644 --- a/kernel/arch/x86_64/MMU.cpp +++ b/kernel/arch/x86_64/MMU.cpp @@ -97,9 +97,10 @@ 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"); + 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;