Skip to content

Commit 1e20dd2

Browse files
gunnarbeutnerawesomekling
authored andcommitted
Kernel: Add PML4T support for the PageDirectory class
1 parent f5cd366 commit 1e20dd2

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Kernel/VM/PageDirectory.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ RefPtr<PageDirectory> PageDirectory::find_by_cr3(u32 cr3)
2828
return cr3_map().get(cr3).value_or({});
2929
}
3030

31+
#if ARCH(X86_64)
32+
extern "C" PageDirectoryEntry boot_pml4t[1024];
33+
#endif
3134
extern "C" PageDirectoryEntry* boot_pdpt[4];
3235
extern "C" PageDirectoryEntry boot_pd0[1024];
3336
extern "C" PageDirectoryEntry boot_pd3[1024];
@@ -38,6 +41,11 @@ UNMAP_AFTER_INIT PageDirectory::PageDirectory()
3841
m_identity_range_allocator.initialize_with_range(VirtualAddress(FlatPtr(0x00000000)), 0x00200000);
3942

4043
// Adopt the page tables already set up by boot.S
44+
#if ARCH(X86_64)
45+
PhysicalAddress boot_pml4t_paddr(virtual_to_low_physical((FlatPtr)boot_pml4t));
46+
dmesgln("MM: boot_pml4t @ {}", boot_pml4t_paddr);
47+
m_pml4t = PhysicalPage::create(boot_pml4t_paddr, true, false);
48+
#endif
4149
PhysicalAddress boot_pdpt_paddr(virtual_to_low_physical((FlatPtr)boot_pdpt));
4250
PhysicalAddress boot_pd0_paddr(virtual_to_low_physical((FlatPtr)boot_pd0));
4351
PhysicalAddress boot_pd3_paddr(virtual_to_low_physical((FlatPtr)boot_pd3));
@@ -64,6 +72,11 @@ PageDirectory::PageDirectory(const RangeAllocator* parent_range_allocator)
6472
}
6573

6674
// Set up a userspace page directory
75+
#if ARCH(X86_64)
76+
m_pml4t = MM.allocate_user_physical_page();
77+
if (!m_pml4t)
78+
return;
79+
#endif
6780
m_directory_table = MM.allocate_user_physical_page();
6881
if (!m_directory_table)
6982
return;
@@ -79,12 +92,27 @@ PageDirectory::PageDirectory(const RangeAllocator* parent_range_allocator)
7992
// Share the top 1 GiB of kernel-only mappings (>=3GiB or >=0xc0000000)
8093
m_directory_pages[3] = MM.kernel_page_directory().m_directory_pages[3];
8194

95+
#if ARCH(X86_64)
96+
{
97+
auto& table = *(PageDirectoryPointerTable*)MM.quickmap_page(*m_pml4t);
98+
table.raw[0] = (FlatPtr)m_directory_table->paddr().as_ptr() | 3;
99+
MM.unquickmap_page();
100+
}
101+
#endif
102+
82103
{
83104
auto& table = *(PageDirectoryPointerTable*)MM.quickmap_page(*m_directory_table);
105+
#if ARCH(I386)
84106
table.raw[0] = (FlatPtr)m_directory_pages[0]->paddr().as_ptr() | 1;
85107
table.raw[1] = (FlatPtr)m_directory_pages[1]->paddr().as_ptr() | 1;
86108
table.raw[2] = (FlatPtr)m_directory_pages[2]->paddr().as_ptr() | 1;
87109
table.raw[3] = (FlatPtr)m_directory_pages[3]->paddr().as_ptr() | 1;
110+
#else
111+
table.raw[0] = (FlatPtr)m_directory_pages[0]->paddr().as_ptr() | 3;
112+
table.raw[1] = (FlatPtr)m_directory_pages[1]->paddr().as_ptr() | 3;
113+
table.raw[2] = (FlatPtr)m_directory_pages[2]->paddr().as_ptr() | 3;
114+
table.raw[3] = (FlatPtr)m_directory_pages[3]->paddr().as_ptr() | 3;
115+
#endif
88116

89117
// 2 ** MAXPHYADDR - 1
90118
// Where MAXPHYADDR = physical_address_bit_width

Kernel/VM/PageDirectory.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ class PageDirectory : public RefCounted<PageDirectory> {
3232

3333
~PageDirectory();
3434

35-
u32 cr3() const { return m_directory_table->paddr().get(); }
35+
u32 cr3() const
36+
{
37+
#if ARCH(X86_64)
38+
return m_pml4t->paddr().get();
39+
#else
40+
return m_directory_table->paddr().get();
41+
#endif
42+
}
3643

3744
RangeAllocator& range_allocator() { return m_range_allocator; }
3845
const RangeAllocator& range_allocator() const { return m_range_allocator; }
@@ -55,6 +62,9 @@ class PageDirectory : public RefCounted<PageDirectory> {
5562
Space* m_space { nullptr };
5663
RangeAllocator m_range_allocator;
5764
RangeAllocator m_identity_range_allocator;
65+
#if ARCH(X86_64)
66+
RefPtr<PhysicalPage> m_pml4t;
67+
#endif
5868
RefPtr<PhysicalPage> m_directory_table;
5969
RefPtr<PhysicalPage> m_directory_pages[4];
6070
HashMap<u32, RefPtr<PhysicalPage>> m_page_tables;

0 commit comments

Comments
 (0)