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
|
|
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
2021-09-07 20:40:44 -03:00
|
|
|
#include <AK/IntrusiveRedBlackTree.h>
|
2019-06-21 13:58:45 -03:00
|
|
|
#include <AK/RefCounted.h>
|
2019-08-06 06:19:16 -03:00
|
|
|
#include <AK/RefPtr.h>
|
2021-07-11 06:49:16 -03:00
|
|
|
#include <Kernel/Forward.h>
|
2021-08-06 05:45:34 -03:00
|
|
|
#include <Kernel/Memory/PhysicalPage.h>
|
2021-08-06 08:54:48 -03:00
|
|
|
#include <Kernel/Memory/VirtualRangeAllocator.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
|
|
|
|
2019-06-21 10:29:31 -03:00
|
|
|
class PageDirectory : public RefCounted<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:
|
2021-09-05 10:13:20 -03:00
|
|
|
static KResultOr<NonnullRefPtr<PageDirectory>> try_create_for_userspace(VirtualRangeAllocator const* parent_range_allocator = nullptr);
|
2021-08-05 13:58:33 -03:00
|
|
|
static NonnullRefPtr<PageDirectory> must_create_kernel_page_directory();
|
2021-06-27 22:23:21 -03:00
|
|
|
static RefPtr<PageDirectory> find_by_cr3(FlatPtr);
|
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
|
|
|
|
2021-08-06 08:54:48 -03:00
|
|
|
VirtualRangeAllocator& range_allocator() { return m_range_allocator; }
|
|
|
|
|
VirtualRangeAllocator const& range_allocator() const { return m_range_allocator; }
|
2021-02-08 11:45:40 -03:00
|
|
|
|
2021-08-06 08:59:22 -03:00
|
|
|
AddressSpace* address_space() { return m_space; }
|
|
|
|
|
const AddressSpace* 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();
|
2019-04-03 10:13:07 -03:00
|
|
|
|
2021-08-06 08:57:39 -03:00
|
|
|
AddressSpace* m_space { nullptr };
|
2021-08-06 08:54:48 -03:00
|
|
|
VirtualRangeAllocator m_range_allocator;
|
2021-06-25 20:00:40 -03:00
|
|
|
#if ARCH(X86_64)
|
|
|
|
|
RefPtr<PhysicalPage> m_pml4t;
|
|
|
|
|
#endif
|
2019-12-25 07:22:16 -03:00
|
|
|
RefPtr<PhysicalPage> m_directory_table;
|
2021-07-16 21:42:59 -03:00
|
|
|
#if ARCH(X86_64)
|
|
|
|
|
RefPtr<PhysicalPage> m_directory_pages[512];
|
|
|
|
|
#else
|
2019-12-25 07:22:16 -03:00
|
|
|
RefPtr<PhysicalPage> m_directory_pages[4];
|
2021-07-16 21:42:59 -03:00
|
|
|
#endif
|
2021-08-15 08:18:43 -03:00
|
|
|
HashMap<FlatPtr, NonnullRefPtr<PhysicalPage>> m_page_tables;
|
2021-08-21 20:37:17 -03:00
|
|
|
RecursiveSpinlock m_lock;
|
2019-04-03 10:13:07 -03:00
|
|
|
};
|
2020-02-15 21:27:42 -03:00
|
|
|
|
|
|
|
|
}
|