From d79c772c87367de84045f35c9b0abe3990854840 Mon Sep 17 00:00:00 2001 From: James Mintram Date: Sat, 2 Apr 2022 23:56:20 +0100 Subject: [PATCH] Kernel: Make MemoryManager compile on aarch64 --- Kernel/Arch/aarch64/CrashHandler.cpp | 2 + Kernel/Arch/aarch64/PageDirectory.cpp | 33 ++++++++++++++++ Kernel/Arch/aarch64/Processor.h | 14 +++++++ Kernel/Arch/aarch64/RegisterState.h | 2 + Kernel/Arch/x86/common/PageDirectory.cpp | 50 ++++++++++++++++++++++++ Kernel/CMakeLists.txt | 2 + Kernel/Memory/MemoryManager.cpp | 12 +++--- Kernel/Memory/MemoryManager.h | 1 + Kernel/Memory/PageDirectory.cpp | 18 +-------- Kernel/Memory/PageDirectory.h | 7 +++- 10 files changed, 119 insertions(+), 22 deletions(-) create mode 100644 Kernel/Arch/aarch64/PageDirectory.cpp create mode 100644 Kernel/Arch/x86/common/PageDirectory.cpp diff --git a/Kernel/Arch/aarch64/CrashHandler.cpp b/Kernel/Arch/aarch64/CrashHandler.cpp index 9b1d93a63a..27dc1a5759 100644 --- a/Kernel/Arch/aarch64/CrashHandler.cpp +++ b/Kernel/Arch/aarch64/CrashHandler.cpp @@ -4,6 +4,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include + #include #include diff --git a/Kernel/Arch/aarch64/PageDirectory.cpp b/Kernel/Arch/aarch64/PageDirectory.cpp new file mode 100644 index 0000000000..f87cfa9885 --- /dev/null +++ b/Kernel/Arch/aarch64/PageDirectory.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2018-2021, Andreas Kling + * Copyright (c) 2018-2022, James Mintram + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace Kernel::Memory { + +void PageDirectory::register_page_directory(PageDirectory*) +{ +} + +void PageDirectory::deregister_page_directory(PageDirectory*) +{ +} + +RefPtr PageDirectory::find_current() +{ + return nullptr; +} + +void activate_kernel_page_directory(PageDirectory const&) +{ +} + +void activate_page_directory(PageDirectory const&, Thread*) +{ +} + +} diff --git a/Kernel/Arch/aarch64/Processor.h b/Kernel/Arch/aarch64/Processor.h index 76d1e30788..79047ef46c 100644 --- a/Kernel/Arch/aarch64/Processor.h +++ b/Kernel/Arch/aarch64/Processor.h @@ -13,8 +13,14 @@ #include +class VirtualAddress; + namespace Kernel { +namespace Memory { +class PageDirectory; +}; + class Thread; // FIXME This needs to go behind some sort of platform abstraction @@ -41,6 +47,14 @@ public: return false; } + ALWAYS_INLINE static void flush_tlb_local(VirtualAddress&, size_t&) + { + } + + ALWAYS_INLINE static void flush_tlb(Memory::PageDirectory const*, VirtualAddress const&, size_t) + { + } + ALWAYS_INLINE static u32 current_id() { return 0; diff --git a/Kernel/Arch/aarch64/RegisterState.h b/Kernel/Arch/aarch64/RegisterState.h index 3f14084c09..d4db9f1014 100644 --- a/Kernel/Arch/aarch64/RegisterState.h +++ b/Kernel/Arch/aarch64/RegisterState.h @@ -9,6 +9,8 @@ namespace Kernel { struct RegisterState { + FlatPtr userspace_sp() const { return 0; } + FlatPtr ip() const { return 0; } }; struct DebugRegisterState { diff --git a/Kernel/Arch/x86/common/PageDirectory.cpp b/Kernel/Arch/x86/common/PageDirectory.cpp new file mode 100644 index 0000000000..4b93dfce53 --- /dev/null +++ b/Kernel/Arch/x86/common/PageDirectory.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018-2021, Andreas Kling + * Copyright (c) 2018-2022, James Mintram + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include +#include + +namespace Kernel::Memory { + +static Singleton> s_cr3_map; + +static IntrusiveRedBlackTree<&PageDirectory::m_tree_node>& cr3_map() +{ + VERIFY_INTERRUPTS_DISABLED(); + return *s_cr3_map; +} + +void PageDirectory::register_page_directory(PageDirectory* directory) +{ + cr3_map().insert(directory->cr3(), *directory); +} + +void PageDirectory::deregister_page_directory(PageDirectory* directory) +{ + cr3_map().remove(directory->cr3()); +} + +RefPtr PageDirectory::find_current() +{ + SpinlockLocker lock(s_mm_lock); + return cr3_map().find(read_cr3()); +} + +void activate_kernel_page_directory(PageDirectory const& pgd) +{ + write_cr3(pgd.cr3()); +} + +void activate_page_directory(PageDirectory const& pgd, Thread* current_thread) +{ + current_thread->regs().cr3 = pgd.cr3(); + write_cr3(pgd.cr3()); +} + +} diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index 43f7755835..a2307208f4 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -326,6 +326,7 @@ if ("${SERENITY_ARCH}" STREQUAL "i686" OR "${SERENITY_ARCH}" STREQUAL "x86_64") ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86/common/CPU.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86/common/CPUID.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86/common/Interrupts.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86/common/PageDirectory.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86/common/Processor.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86/common/ProcessorInfo.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Arch/x86/common/SafeMem.cpp @@ -396,6 +397,7 @@ else() Arch/aarch64/Mailbox.cpp Arch/aarch64/MainIdRegister.cpp Arch/aarch64/MMIO.cpp + Arch/aarch64/PageDirectory.cpp Arch/aarch64/Timer.cpp Arch/aarch64/UART.cpp Arch/aarch64/Utils.cpp diff --git a/Kernel/Memory/MemoryManager.cpp b/Kernel/Memory/MemoryManager.cpp index 0fe253488e..833ae22812 100644 --- a/Kernel/Memory/MemoryManager.cpp +++ b/Kernel/Memory/MemoryManager.cpp @@ -8,7 +8,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -78,7 +80,7 @@ UNMAP_AFTER_INIT MemoryManager::MemoryManager() SpinlockLocker lock(s_mm_lock); parse_memory_map(); - write_cr3(kernel_page_directory().cr3()); + activate_kernel_page_directory(kernel_page_directory()); protect_kernel_image(); // We're temporarily "committing" to two pages that we need to allocate below @@ -105,7 +107,7 @@ UNMAP_AFTER_INIT void MemoryManager::protect_kernel_image() auto& pte = *ensure_pte(kernel_page_directory(), VirtualAddress(i)); pte.set_writable(false); } - if (Processor::current().has_feature(CPUFeature::NX)) { + if (Processor::current().has_nx()) { // Disable execution of the kernel data, bss and heap segments. for (auto const* i = start_of_kernel_data; i < end_of_kernel_image; i += PAGE_SIZE) { auto& pte = *ensure_pte(kernel_page_directory(), VirtualAddress(i)); @@ -467,7 +469,7 @@ UNMAP_AFTER_INIT void MemoryManager::initialize_physical_pages() pte.set_physical_page_base(physical_page_array_current_page); pte.set_user_allowed(false); pte.set_writable(true); - if (Processor::current().has_feature(CPUFeature::NX)) + if (Processor::current().has_nx()) pte.set_execute_disabled(false); pte.set_global(true); pte.set_present(true); @@ -710,7 +712,7 @@ Region* MemoryManager::find_region_from_vaddr(VirtualAddress vaddr) { if (auto* region = kernel_region_from_vaddr(vaddr)) return region; - auto page_directory = PageDirectory::find_by_cr3(read_cr3()); + auto page_directory = PageDirectory::find_current(); if (!page_directory) return nullptr; VERIFY(page_directory->address_space()); @@ -1024,7 +1026,7 @@ void MemoryManager::enter_address_space(AddressSpace& space) SpinlockLocker lock(s_mm_lock); current_thread->regs().cr3 = space.page_directory().cr3(); - write_cr3(space.page_directory().cr3()); + activate_page_directory(space.page_directory(), current_thread); } void MemoryManager::flush_tlb_local(VirtualAddress vaddr, size_t page_count) diff --git a/Kernel/Memory/MemoryManager.h b/Kernel/Memory/MemoryManager.h index d7f1446bf6..73a230d40b 100644 --- a/Kernel/Memory/MemoryManager.h +++ b/Kernel/Memory/MemoryManager.h @@ -22,6 +22,7 @@ namespace Kernel { class PageDirectoryEntry; +class PageTableEntry; } struct KmallocGlobalData; diff --git a/Kernel/Memory/PageDirectory.cpp b/Kernel/Memory/PageDirectory.cpp index b6030ee9bb..d8c41cc198 100644 --- a/Kernel/Memory/PageDirectory.cpp +++ b/Kernel/Memory/PageDirectory.cpp @@ -20,20 +20,6 @@ extern u8 end_of_kernel_image[]; namespace Kernel::Memory { -static Singleton> s_cr3_map; - -static IntrusiveRedBlackTree<&PageDirectory::m_tree_node>& cr3_map() -{ - VERIFY_INTERRUPTS_DISABLED(); - return *s_cr3_map; -} - -RefPtr PageDirectory::find_by_cr3(FlatPtr cr3) -{ - SpinlockLocker lock(s_mm_lock); - return cr3_map().find(cr3); -} - UNMAP_AFTER_INIT NonnullRefPtr PageDirectory::must_create_kernel_page_directory() { auto directory = adopt_ref_if_nonnull(new (nothrow) PageDirectory).release_nonnull(); @@ -125,7 +111,7 @@ ErrorOr> PageDirectory::try_create_for_userspace(Vi MM.unquickmap_page(); } - cr3_map().insert(directory->cr3(), directory); + register_page_directory(directory); return directory; } @@ -150,7 +136,7 @@ PageDirectory::~PageDirectory() { if (is_cr3_initialized()) { SpinlockLocker lock(s_mm_lock); - cr3_map().remove(cr3()); + deregister_page_directory(this); } } diff --git a/Kernel/Memory/PageDirectory.h b/Kernel/Memory/PageDirectory.h index 88a8361d37..bdb1d8dfc5 100644 --- a/Kernel/Memory/PageDirectory.h +++ b/Kernel/Memory/PageDirectory.h @@ -23,7 +23,7 @@ class PageDirectory : public RefCounted { public: static ErrorOr> try_create_for_userspace(VirtualRangeAllocator const* parent_range_allocator = nullptr); static NonnullRefPtr must_create_kernel_page_directory(); - static RefPtr find_by_cr3(FlatPtr); + static RefPtr find_current(); ~PageDirectory(); @@ -62,6 +62,8 @@ public: private: PageDirectory(); + static void register_page_directory(PageDirectory* directory); + static void deregister_page_directory(PageDirectory* directory); AddressSpace* m_space { nullptr }; VirtualRangeAllocator m_range_allocator; @@ -77,4 +79,7 @@ private: RecursiveSpinlock m_lock; }; +void activate_kernel_page_directory(PageDirectory const& pgd); +void activate_page_directory(PageDirectory const& pgd, Thread* current_thread); + }