From 8d07bce12aed48d30a35bb5a80918037a92e1098 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 6 Aug 2019 10:31:20 +0200 Subject: [PATCH] Kernel: Break region_from_vaddr() into {user,kernel}_region_from_vaddr Sometimes you're only interested in either user OR kernel regions but not both. Let's break this into two functions so the caller can choose what he's interested in. --- Kernel/VM/MemoryManager.cpp | 53 +++++++++++++++++++------------------ Kernel/VM/MemoryManager.h | 3 +++ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/Kernel/VM/MemoryManager.cpp b/Kernel/VM/MemoryManager.cpp index 178ecd14150..51e75a2f94f 100644 --- a/Kernel/VM/MemoryManager.cpp +++ b/Kernel/VM/MemoryManager.cpp @@ -255,42 +255,43 @@ void MemoryManager::initialize() s_the = new MemoryManager; } +Region* MemoryManager::kernel_region_from_vaddr(VirtualAddress vaddr) +{ + ASSERT(vaddr.get() >= 0xc0000000); + for (auto& region : MM.m_kernel_regions) { + if (region->contains(vaddr)) + return region; + } + return nullptr; +} + +Region* MemoryManager::user_region_from_vaddr(Process& process, VirtualAddress vaddr) +{ + // FIXME: Use a binary search tree (maybe red/black?) or some other more appropriate data structure! + for (auto& region : process.m_regions) { + if (region.contains(vaddr)) + return ®ion; + } + dbg() << process << " Couldn't find user region for " << vaddr; + return nullptr; +} + Region* MemoryManager::region_from_vaddr(Process& process, VirtualAddress vaddr) { ASSERT_INTERRUPTS_DISABLED(); - if (vaddr.get() >= 0xc0000000) { - for (auto& region : MM.m_kernel_regions) { - if (region->contains(vaddr)) - return region; - } - } + if (vaddr.get() >= 0xc0000000) + return kernel_region_from_vaddr(vaddr); - // FIXME: Use a binary search tree (maybe red/black?) or some other more appropriate data structure! - for (auto& region : process.m_regions) { - if (region.contains(vaddr)) - return ®ion; - } - dbgprintf("%s(%u) Couldn't find region for L%x (CR3=%x)\n", process.name().characters(), process.pid(), vaddr.get(), process.page_directory().cr3()); - return nullptr; + return user_region_from_vaddr(process, vaddr); } const Region* MemoryManager::region_from_vaddr(const Process& process, VirtualAddress vaddr) { - if (vaddr.get() >= 0xc0000000) { - for (auto& region : MM.m_kernel_regions) { - if (region->contains(vaddr)) - return region; - } - } + if (vaddr.get() >= 0xc0000000) + return kernel_region_from_vaddr(vaddr); - // FIXME: Use a binary search tree (maybe red/black?) or some other more appropriate data structure! - for (auto& region : process.m_regions) { - if (region.contains(vaddr)) - return ®ion; - } - dbgprintf("%s(%u) Couldn't find region for L%x (CR3=%x)\n", process.name().characters(), process.pid(), vaddr.get(), process.page_directory().cr3()); - return nullptr; + return user_region_from_vaddr(const_cast(process), vaddr); } bool MemoryManager::zero_page(Region& region, unsigned page_index_in_region) diff --git a/Kernel/VM/MemoryManager.h b/Kernel/VM/MemoryManager.h index 9e9d6e12305..6c4884a3199 100644 --- a/Kernel/VM/MemoryManager.h +++ b/Kernel/VM/MemoryManager.h @@ -105,6 +105,9 @@ private: static Region* region_from_vaddr(Process&, VirtualAddress); static const Region* region_from_vaddr(const Process&, VirtualAddress); + static Region* user_region_from_vaddr(Process&, VirtualAddress); + static Region* kernel_region_from_vaddr(VirtualAddress); + bool copy_on_write(Region&, unsigned page_index_in_region); bool page_in_from_inode(Region&, unsigned page_index_in_region); bool zero_page(Region& region, unsigned page_index_in_region);