2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-08-19 15:26:07 +00:00
|
|
|
#include <AK/AtomicRefCounted.h>
|
2022-04-02 22:47:27 +00:00
|
|
|
#include <AK/Badge.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
#include <AK/HashMap.h>
|
2021-09-07 23:40:44 +00:00
|
|
|
#include <AK/IntrusiveRedBlackTree.h>
|
2022-08-24 13:56:26 +00:00
|
|
|
#include <AK/RefPtr.h>
|
2021-07-11 09:49:16 +00:00
|
|
|
#include <Kernel/Forward.h>
|
2022-04-03 19:41:56 +00:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/PhysicalPage.h>
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2021-08-06 11:49:36 +00:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2022-08-19 15:26:07 +00:00
|
|
|
class PageDirectory final : public AtomicRefCounted<PageDirectory> {
|
2019-04-03 13:13:07 +00:00
|
|
|
friend class MemoryManager;
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
public:
|
2022-08-19 18:53:40 +00:00
|
|
|
static ErrorOr<NonnullLockRefPtr<PageDirectory>> try_create_for_userspace();
|
|
|
|
static NonnullLockRefPtr<PageDirectory> must_create_kernel_page_directory();
|
|
|
|
static LockRefPtr<PageDirectory> find_current();
|
2019-08-06 09:19:16 +00:00
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
~PageDirectory();
|
|
|
|
|
2021-07-08 01:50:05 +00:00
|
|
|
void allocate_kernel_directory();
|
|
|
|
|
2021-06-28 01:23:21 +00:00
|
|
|
FlatPtr cr3() const
|
2021-06-25 23:00:40 +00:00
|
|
|
{
|
|
|
|
#if ARCH(X86_64)
|
|
|
|
return m_pml4t->paddr().get();
|
|
|
|
#else
|
|
|
|
return m_directory_table->paddr().get();
|
|
|
|
#endif
|
|
|
|
}
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2022-01-16 16:03:06 +00:00
|
|
|
bool is_cr3_initialized() const
|
|
|
|
{
|
|
|
|
#if ARCH(X86_64)
|
|
|
|
return m_pml4t;
|
|
|
|
#else
|
|
|
|
return m_directory_table;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:59:22 +00:00
|
|
|
AddressSpace* address_space() { return m_space; }
|
2022-04-01 17:58:27 +00:00
|
|
|
AddressSpace const* address_space() const { return m_space; }
|
2021-02-08 14:45:40 +00:00
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
void set_space(Badge<AddressSpace>, AddressSpace& space) { m_space = &space; }
|
2019-08-06 09:19:16 +00:00
|
|
|
|
2021-08-21 23:37:17 +00:00
|
|
|
RecursiveSpinlock& get_lock() { return m_lock; }
|
2020-10-31 23:19:18 +00:00
|
|
|
|
2021-09-07 23:40:44 +00:00
|
|
|
// This has to be public to let the global singleton access the member pointer
|
|
|
|
IntrusiveRedBlackTreeNode<FlatPtr, PageDirectory, RawPtr<PageDirectory>> m_tree_node;
|
|
|
|
|
2019-04-03 13:13:07 +00:00
|
|
|
private:
|
2020-01-17 18:59:20 +00:00
|
|
|
PageDirectory();
|
2022-04-02 22:56:20 +00:00
|
|
|
static void register_page_directory(PageDirectory* directory);
|
|
|
|
static void deregister_page_directory(PageDirectory* directory);
|
2019-04-03 13:13:07 +00:00
|
|
|
|
2021-08-06 11:57:39 +00:00
|
|
|
AddressSpace* m_space { nullptr };
|
2021-06-25 23:00:40 +00:00
|
|
|
#if ARCH(X86_64)
|
2022-08-24 13:56:26 +00:00
|
|
|
RefPtr<PhysicalPage> m_pml4t;
|
2021-06-25 23:00:40 +00:00
|
|
|
#endif
|
2022-08-24 13:56:26 +00:00
|
|
|
RefPtr<PhysicalPage> m_directory_table;
|
2021-07-17 00:42:59 +00:00
|
|
|
#if ARCH(X86_64)
|
2022-08-24 13:56:26 +00:00
|
|
|
RefPtr<PhysicalPage> m_directory_pages[512];
|
2021-07-17 00:42:59 +00:00
|
|
|
#else
|
2022-08-24 13:56:26 +00:00
|
|
|
RefPtr<PhysicalPage> m_directory_pages[4];
|
2021-07-17 00:42:59 +00:00
|
|
|
#endif
|
2022-08-18 19:46:28 +00:00
|
|
|
RecursiveSpinlock m_lock { LockRank::None };
|
2019-04-03 13:13:07 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2022-04-02 22:56:20 +00:00
|
|
|
void activate_kernel_page_directory(PageDirectory const& pgd);
|
|
|
|
void activate_page_directory(PageDirectory const& pgd, Thread* current_thread);
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|