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