From ec73db00574937064c7a7e4de8b4a3e2f83b5606 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 28 May 2023 21:03:08 +0300 Subject: [PATCH] Kernel: Move page macros to Types.h --- kernel/arch/x86_64/MMU.cpp | 61 +++++++++++++--------------- kernel/include/kernel/Memory/Types.h | 2 + 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/kernel/arch/x86_64/MMU.cpp b/kernel/arch/x86_64/MMU.cpp index 7cc64bf155..97cfcc886b 100644 --- a/kernel/arch/x86_64/MMU.cpp +++ b/kernel/arch/x86_64/MMU.cpp @@ -4,9 +4,6 @@ #include #include -#define FLAGS_MASK (PAGE_SIZE - 1) -#define PAGE_MASK (~FLAGS_MASK) - #define CLEANUP_STRUCTURE(s) \ do { \ for (uint64_t i = 0; i < 512; i++) \ @@ -80,30 +77,30 @@ namespace Kernel if (!(global_pml4[pml4e] & Flags::Present)) continue; - uint64_t* global_pdpt = (uint64_t*)(global_pml4[pml4e] & PAGE_MASK); + uint64_t* global_pdpt = (uint64_t*)(global_pml4[pml4e] & PAGE_ADDR_MASK); uint64_t* pdpt = allocate_page_aligned_page(); - pml4[pml4e] = (uint64_t)pdpt | (global_pml4[pml4e] & FLAGS_MASK); + pml4[pml4e] = (uint64_t)pdpt | (global_pml4[pml4e] & PAGE_FLAG_MASK); for (uint32_t pdpte = 0; pdpte < 512; pdpte++) { if (!(global_pdpt[pdpte] & Flags::Present)) continue; - uint64_t* global_pd = (uint64_t*)(global_pdpt[pdpte] & PAGE_MASK); + uint64_t* global_pd = (uint64_t*)(global_pdpt[pdpte] & PAGE_ADDR_MASK); uint64_t* pd = allocate_page_aligned_page(); - pdpt[pdpte] = (uint64_t)pd | (global_pdpt[pdpte] & FLAGS_MASK); + pdpt[pdpte] = (uint64_t)pd | (global_pdpt[pdpte] & PAGE_FLAG_MASK); for (uint32_t pde = 0; pde < 512; pde++) { if (!(global_pd[pde] & Flags::Present)) continue; - uint64_t* global_pt = (uint64_t*)(global_pd[pde] & PAGE_MASK); + uint64_t* global_pt = (uint64_t*)(global_pd[pde] & PAGE_ADDR_MASK); uint64_t* pt = allocate_page_aligned_page(); - pd[pde] = (uint64_t)pt | (global_pd[pde] & FLAGS_MASK); + pd[pde] = (uint64_t)pt | (global_pd[pde] & PAGE_FLAG_MASK); memcpy(pt, global_pt, PAGE_SIZE); } @@ -120,17 +117,17 @@ namespace Kernel { if (!(pml4[pml4e] & Flags::Present)) continue; - uint64_t* pdpt = (uint64_t*)(pml4[pml4e] & PAGE_MASK); + uint64_t* pdpt = (uint64_t*)(pml4[pml4e] & PAGE_ADDR_MASK); for (uint32_t pdpte = 0; pdpte < 512; pdpte++) { if (!(pdpt[pdpte] & Flags::Present)) continue; - uint64_t* pd = (uint64_t*)(pdpt[pdpte] & PAGE_MASK); + uint64_t* pd = (uint64_t*)(pdpt[pdpte] & PAGE_ADDR_MASK); for (uint32_t pde = 0; pde < 512; pde++) { if (!(pd[pde] & Flags::Present)) continue; - kfree((void*)(pd[pde] & PAGE_MASK)); + kfree((void*)(pd[pde] & PAGE_ADDR_MASK)); } kfree(pd); } @@ -143,7 +140,7 @@ namespace Kernel { uintptr_t rsp; read_rsp(rsp); - ASSERT(!is_page_free(rsp & PAGE_MASK)); + ASSERT(!is_page_free(rsp & PAGE_ADDR_MASK)); asm volatile("movq %0, %%cr3" :: "r"(m_highest_paging_struct)); s_current = this; @@ -157,7 +154,7 @@ namespace Kernel void MMU::identity_map_page(paddr_t address, flags_t flags) { - address &= PAGE_MASK; + address &= PAGE_ADDR_MASK; map_page_at(address, address, flags); } @@ -177,7 +174,7 @@ namespace Kernel ASSERT((address >> 48) == 0); - address &= PAGE_MASK; + address &= PAGE_ADDR_MASK; if (is_page_free(address)) { @@ -191,9 +188,9 @@ namespace Kernel uint64_t pte = (address >> 12) & 0x1FF; uint64_t* pml4 = m_highest_paging_struct; - uint64_t* pdpt = (uint64_t*)(pml4[pml4e] & PAGE_MASK); - uint64_t* pd = (uint64_t*)(pdpt[pdpte] & PAGE_MASK); - uint64_t* pt = (uint64_t*)(pd[pde] & PAGE_MASK); + uint64_t* pdpt = (uint64_t*)(pml4[pml4e] & PAGE_ADDR_MASK); + uint64_t* pd = (uint64_t*)(pdpt[pdpte] & PAGE_ADDR_MASK); + uint64_t* pt = (uint64_t*)(pd[pde] & PAGE_ADDR_MASK); pt[pte] = 0; CLEANUP_STRUCTURE(pt); @@ -236,26 +233,26 @@ namespace Kernel { if (!(pml4[pml4e] & Flags::Present)) pml4[pml4e] = (uint64_t)allocate_page_aligned_page(); - pml4[pml4e] = (pml4[pml4e] & PAGE_MASK) | flags; + pml4[pml4e] = (pml4[pml4e] & PAGE_ADDR_MASK) | flags; } - uint64_t* pdpt = (uint64_t*)(pml4[pml4e] & PAGE_MASK); + uint64_t* pdpt = (uint64_t*)(pml4[pml4e] & PAGE_ADDR_MASK); if ((pdpt[pdpte] & flags) != flags) { if (!(pdpt[pdpte] & Flags::Present)) pdpt[pdpte] = (uint64_t)allocate_page_aligned_page(); - pdpt[pdpte] = (pdpt[pdpte] & PAGE_MASK) | flags; + pdpt[pdpte] = (pdpt[pdpte] & PAGE_ADDR_MASK) | flags; } - uint64_t* pd = (uint64_t*)(pdpt[pdpte] & PAGE_MASK); + uint64_t* pd = (uint64_t*)(pdpt[pdpte] & PAGE_ADDR_MASK); if ((pd[pde] & flags) != flags) { if (!(pd[pde] & Flags::Present)) pd[pde] = (uint64_t)allocate_page_aligned_page(); - pd[pde] = (pd[pde] & PAGE_MASK) | flags; + pd[pde] = (pd[pde] & PAGE_ADDR_MASK) | flags; } - uint64_t* pt = (uint64_t*)(pd[pde] & PAGE_MASK); + uint64_t* pt = (uint64_t*)(pd[pde] & PAGE_ADDR_MASK); pt[pte] = paddr | flags; } @@ -275,15 +272,15 @@ namespace Kernel if (!(pml4[pml4e] & Flags::Present)) return 0; - uint64_t* pdpt = (uint64_t*)(pml4[pml4e] & PAGE_MASK); + uint64_t* pdpt = (uint64_t*)(pml4[pml4e] & PAGE_ADDR_MASK); if (!(pdpt[pdpte] & Flags::Present)) return 0; - uint64_t* pd = (uint64_t*)(pdpt[pdpte] & PAGE_MASK); + uint64_t* pd = (uint64_t*)(pdpt[pdpte] & PAGE_ADDR_MASK); if (!(pd[pde] & Flags::Present)) return 0; - uint64_t* pt = (uint64_t*)(pd[pde] & PAGE_MASK); + uint64_t* pt = (uint64_t*)(pd[pde] & PAGE_ADDR_MASK); if (!(pt[pte] & Flags::Present)) return 0; @@ -292,12 +289,12 @@ namespace Kernel MMU::flags_t MMU::get_page_flags(vaddr_t addr) const { - return get_page_data(addr) & FLAGS_MASK; + return get_page_data(addr) & PAGE_FLAG_MASK; } paddr_t MMU::physical_address_of(vaddr_t addr) const { - return get_page_data(addr) & PAGE_MASK; + return get_page_data(addr) & PAGE_ADDR_MASK; } vaddr_t MMU::get_free_page() const @@ -311,17 +308,17 @@ namespace Kernel { if (!(pml4[pml4e] & Flags::Present)) continue; - vaddr_t* pdpt = (vaddr_t*)(pml4[pml4e] & PAGE_MASK); + vaddr_t* pdpt = (vaddr_t*)(pml4[pml4e] & PAGE_ADDR_MASK); for (uint64_t pdpte = 0; pdpte < 512; pdpte++) { if (!(pdpt[pdpte] & Flags::Present)) continue; - vaddr_t* pd = (vaddr_t*)(pdpt[pdpte] & PAGE_MASK); + vaddr_t* pd = (vaddr_t*)(pdpt[pdpte] & PAGE_ADDR_MASK); for (uint64_t pde = 0; pde < 512; pde++) { if (!(pd[pde] & Flags::Present)) continue; - vaddr_t* pt = (vaddr_t*)(pd[pde] & PAGE_MASK); + vaddr_t* pt = (vaddr_t*)(pd[pde] & PAGE_ADDR_MASK); for (uint64_t pte = !(pml4e + pdpte + pde); pte < 512; pte++) { if (!(pt[pte] & Flags::Present)) diff --git a/kernel/include/kernel/Memory/Types.h b/kernel/include/kernel/Memory/Types.h index 3af4098f18..7d9f637e7e 100644 --- a/kernel/include/kernel/Memory/Types.h +++ b/kernel/include/kernel/Memory/Types.h @@ -1,6 +1,8 @@ #pragma once #define PAGE_SIZE 4096 +#define PAGE_FLAG_MASK ((uintptr_t)0xFFF) +#define PAGE_ADDR_MASK (~PAGE_FLAG_MASK) namespace Kernel {