1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:27:35 +00:00

Kernel: Use an IntrusiveRedBlackTree for storing the cr3 mappings

This ensures we don't allocate when intializing the PageDirectory.
This commit is contained in:
Idan Horowitz 2021-09-08 02:40:44 +03:00
parent 0279fb4dd3
commit bee2de4b31
2 changed files with 8 additions and 4 deletions

View file

@ -17,9 +17,9 @@ extern u8 end_of_kernel_image[];
namespace Kernel::Memory { namespace Kernel::Memory {
static Singleton<HashMap<FlatPtr, PageDirectory*>> s_cr3_map; static Singleton<IntrusiveRedBlackTree<FlatPtr, PageDirectory, RawPtr<PageDirectory>, &PageDirectory::m_tree_node>> s_cr3_map;
static HashMap<FlatPtr, PageDirectory*>& cr3_map() static IntrusiveRedBlackTree<FlatPtr, PageDirectory, RawPtr<PageDirectory>, &PageDirectory::m_tree_node>& cr3_map()
{ {
VERIFY_INTERRUPTS_DISABLED(); VERIFY_INTERRUPTS_DISABLED();
return *s_cr3_map; return *s_cr3_map;
@ -28,7 +28,7 @@ static HashMap<FlatPtr, PageDirectory*>& cr3_map()
RefPtr<PageDirectory> PageDirectory::find_by_cr3(FlatPtr cr3) RefPtr<PageDirectory> PageDirectory::find_by_cr3(FlatPtr cr3)
{ {
SpinlockLocker lock(s_mm_lock); SpinlockLocker lock(s_mm_lock);
return cr3_map().get(cr3).value_or({}); return cr3_map().find(cr3);
} }
UNMAP_AFTER_INIT NonnullRefPtr<PageDirectory> PageDirectory::must_create_kernel_page_directory() UNMAP_AFTER_INIT NonnullRefPtr<PageDirectory> PageDirectory::must_create_kernel_page_directory()
@ -132,7 +132,7 @@ KResultOr<NonnullRefPtr<PageDirectory>> PageDirectory::try_create_for_userspace(
auto* new_pd = MM.quickmap_pd(*directory, 0); auto* new_pd = MM.quickmap_pd(*directory, 0);
memcpy(new_pd, &buffer, sizeof(PageDirectoryEntry)); memcpy(new_pd, &buffer, sizeof(PageDirectoryEntry));
cr3_map().set(directory->cr3(), directory.ptr()); cr3_map().insert(directory->cr3(), directory);
return directory; return directory;
} }

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <AK/IntrusiveRedBlackTree.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/RefPtr.h> #include <AK/RefPtr.h>
#include <Kernel/Forward.h> #include <Kernel/Forward.h>
@ -46,6 +47,9 @@ public:
RecursiveSpinlock& get_lock() { return m_lock; } RecursiveSpinlock& get_lock() { return m_lock; }
// This has to be public to let the global singleton access the member pointer
IntrusiveRedBlackTreeNode<FlatPtr, PageDirectory, RawPtr<PageDirectory>> m_tree_node;
private: private:
PageDirectory(); PageDirectory();