Kernel/x86: Protect the CR3->PD map with a spinlock

This can be accessed from multiple CPUs at the same time, so relying on
the interrupt flag is clearly insufficient.
This commit is contained in:
Andreas Kling 2022-08-22 15:34:58 +02:00
parent 6cd3695761
commit 4c081e0479
Notes: sideshowbarker 2024-07-17 08:03:39 +09:00

View file

@ -1,43 +1,41 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2022, James Mintram <me@jamesrm.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Singleton.h>
#include <Kernel/Arch/InterruptDisabler.h>
#include <Kernel/Memory/PageDirectory.h>
#include <Kernel/Thread.h>
namespace Kernel::Memory {
// FIXME: This needs real locking:
static Singleton<IntrusiveRedBlackTree<&PageDirectory::m_tree_node>> s_cr3_map;
struct CR3Map {
SpinlockProtected<IntrusiveRedBlackTree<&PageDirectory::m_tree_node>> map { LockRank::None };
};
static IntrusiveRedBlackTree<&PageDirectory::m_tree_node>& cr3_map()
{
VERIFY_INTERRUPTS_DISABLED();
return *s_cr3_map;
}
static Singleton<CR3Map> s_cr3_map;
void PageDirectory::register_page_directory(PageDirectory* directory)
{
InterruptDisabler disabler;
cr3_map().insert(directory->cr3(), *directory);
s_cr3_map->map.with([&](auto& map) {
map.insert(directory->cr3(), *directory);
});
}
void PageDirectory::deregister_page_directory(PageDirectory* directory)
{
InterruptDisabler disabler;
cr3_map().remove(directory->cr3());
s_cr3_map->map.with([&](auto& map) {
map.remove(directory->cr3());
});
}
LockRefPtr<PageDirectory> PageDirectory::find_current()
{
SpinlockLocker lock(s_mm_lock);
return cr3_map().find(read_cr3());
return s_cr3_map->map.with([&](auto& map) {
return map.find(read_cr3());
});
}
void activate_kernel_page_directory(PageDirectory const& pgd)