2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
2019-06-21 18:58:45 +02:00
|
|
|
#include <AK/RefCounted.h>
|
2019-08-06 11:19:16 +02:00
|
|
|
#include <AK/RefPtr.h>
|
2021-07-11 11:49:16 +02:00
|
|
|
#include <Kernel/Forward.h>
|
2021-08-06 10:45:34 +02:00
|
|
|
#include <Kernel/Memory/PhysicalPage.h>
|
2021-08-06 13:54:48 +02:00
|
|
|
#include <Kernel/Memory/VirtualRangeAllocator.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-08-06 13:49:36 +02:00
|
|
|
namespace Kernel::Memory {
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2019-06-21 15:29:31 +02:00
|
|
|
class PageDirectory : public RefCounted<PageDirectory> {
|
2019-04-03 15:13:07 +02:00
|
|
|
friend class MemoryManager;
|
2019-05-28 11:53:16 +02:00
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
public:
|
2021-09-05 15:13:20 +02:00
|
|
|
static KResultOr<NonnullRefPtr<PageDirectory>> try_create_for_userspace(VirtualRangeAllocator const* parent_range_allocator = nullptr);
|
2021-08-05 18:58:33 +02:00
|
|
|
static NonnullRefPtr<PageDirectory> must_create_kernel_page_directory();
|
2021-06-28 03:23:21 +02:00
|
|
|
static RefPtr<PageDirectory> find_by_cr3(FlatPtr);
|
2019-08-06 11:19:16 +02:00
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
~PageDirectory();
|
|
|
|
|
|
2021-07-07 19:50:05 -06:00
|
|
|
void allocate_kernel_directory();
|
|
|
|
|
|
2021-06-28 03:23:21 +02:00
|
|
|
FlatPtr cr3() const
|
2021-06-26 01:00:40 +02:00
|
|
|
{
|
|
|
|
|
#if ARCH(X86_64)
|
|
|
|
|
return m_pml4t->paddr().get();
|
|
|
|
|
#else
|
|
|
|
|
return m_directory_table->paddr().get();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-08-06 13:54:48 +02:00
|
|
|
VirtualRangeAllocator& range_allocator() { return m_range_allocator; }
|
|
|
|
|
VirtualRangeAllocator const& range_allocator() const { return m_range_allocator; }
|
2021-02-08 15:45:40 +01:00
|
|
|
|
2021-08-06 13:59:22 +02:00
|
|
|
AddressSpace* address_space() { return m_space; }
|
|
|
|
|
const AddressSpace* address_space() const { return m_space; }
|
2021-02-08 15:45:40 +01:00
|
|
|
|
2021-08-06 13:57:39 +02:00
|
|
|
void set_space(Badge<AddressSpace>, AddressSpace& space) { m_space = &space; }
|
2019-08-06 11:19:16 +02:00
|
|
|
|
2021-08-22 01:37:17 +02:00
|
|
|
RecursiveSpinlock& get_lock() { return m_lock; }
|
2020-10-31 17:19:18 -06:00
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
private:
|
2020-01-17 19:59:20 +01:00
|
|
|
PageDirectory();
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2021-08-06 13:57:39 +02:00
|
|
|
AddressSpace* m_space { nullptr };
|
2021-08-06 13:54:48 +02:00
|
|
|
VirtualRangeAllocator m_range_allocator;
|
2021-06-26 01:00:40 +02:00
|
|
|
#if ARCH(X86_64)
|
|
|
|
|
RefPtr<PhysicalPage> m_pml4t;
|
|
|
|
|
#endif
|
2019-12-25 11:22:16 +01:00
|
|
|
RefPtr<PhysicalPage> m_directory_table;
|
2021-07-17 02:42:59 +02:00
|
|
|
#if ARCH(X86_64)
|
|
|
|
|
RefPtr<PhysicalPage> m_directory_pages[512];
|
|
|
|
|
#else
|
2019-12-25 11:22:16 +01:00
|
|
|
RefPtr<PhysicalPage> m_directory_pages[4];
|
2021-07-17 02:42:59 +02:00
|
|
|
#endif
|
2021-08-15 13:18:43 +02:00
|
|
|
HashMap<FlatPtr, NonnullRefPtr<PhysicalPage>> m_page_tables;
|
2021-08-22 01:37:17 +02:00
|
|
|
RecursiveSpinlock m_lock;
|
2019-04-03 15:13:07 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
|
}
|