PageDirectory.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Memory.h>
  7. #include <AK/Singleton.h>
  8. #include <Kernel/Process.h>
  9. #include <Kernel/Random.h>
  10. #include <Kernel/Sections.h>
  11. #include <Kernel/VM/MemoryManager.h>
  12. #include <Kernel/VM/PageDirectory.h>
  13. namespace Kernel {
  14. static AK::Singleton<HashMap<FlatPtr, PageDirectory*>> s_cr3_map;
  15. static HashMap<FlatPtr, PageDirectory*>& cr3_map()
  16. {
  17. VERIFY_INTERRUPTS_DISABLED();
  18. return *s_cr3_map;
  19. }
  20. RefPtr<PageDirectory> PageDirectory::find_by_cr3(FlatPtr cr3)
  21. {
  22. ScopedSpinLock lock(s_mm_lock);
  23. return cr3_map().get(cr3).value_or({});
  24. }
  25. #if ARCH(X86_64)
  26. extern "C" PageDirectoryEntry boot_pml4t[1024];
  27. #endif
  28. extern "C" PageDirectoryEntry* boot_pdpt[4];
  29. extern "C" PageDirectoryEntry boot_pd0[1024];
  30. extern "C" PageDirectoryEntry boot_pd3[1024];
  31. UNMAP_AFTER_INIT PageDirectory::PageDirectory()
  32. {
  33. m_range_allocator.initialize_with_range(VirtualAddress(0xc2000000), 0x2f000000);
  34. m_identity_range_allocator.initialize_with_range(VirtualAddress(FlatPtr(0x00000000)), 0x00200000);
  35. // Adopt the page tables already set up by boot.S
  36. #if ARCH(X86_64)
  37. PhysicalAddress boot_pml4t_paddr(virtual_to_low_physical((FlatPtr)boot_pml4t));
  38. dmesgln("MM: boot_pml4t @ {}", boot_pml4t_paddr);
  39. m_pml4t = PhysicalPage::create(boot_pml4t_paddr, true, false);
  40. #endif
  41. PhysicalAddress boot_pdpt_paddr(virtual_to_low_physical((FlatPtr)boot_pdpt));
  42. PhysicalAddress boot_pd0_paddr(virtual_to_low_physical((FlatPtr)boot_pd0));
  43. PhysicalAddress boot_pd3_paddr(virtual_to_low_physical((FlatPtr)boot_pd3));
  44. dmesgln("MM: boot_pdpt @ {}", boot_pdpt_paddr);
  45. dmesgln("MM: boot_pd0 @ {}", boot_pd0_paddr);
  46. dmesgln("MM: boot_pd3 @ {}", boot_pd3_paddr);
  47. m_directory_table = PhysicalPage::create(boot_pdpt_paddr, true, false);
  48. m_directory_pages[0] = PhysicalPage::create(boot_pd0_paddr, true, false);
  49. m_directory_pages[3] = PhysicalPage::create(boot_pd3_paddr, true, false);
  50. }
  51. PageDirectory::PageDirectory(const RangeAllocator* parent_range_allocator)
  52. {
  53. constexpr FlatPtr userspace_range_base = 0x00800000;
  54. constexpr FlatPtr userspace_range_ceiling = 0xbe000000;
  55. ScopedSpinLock lock(s_mm_lock);
  56. if (parent_range_allocator) {
  57. m_range_allocator.initialize_from_parent(*parent_range_allocator);
  58. } else {
  59. size_t random_offset = (get_fast_random<u8>() % 32 * MiB) & PAGE_MASK;
  60. u32 base = userspace_range_base + random_offset;
  61. m_range_allocator.initialize_with_range(VirtualAddress(base), userspace_range_ceiling - base);
  62. }
  63. // Set up a userspace page directory
  64. #if ARCH(X86_64)
  65. m_pml4t = MM.allocate_user_physical_page();
  66. if (!m_pml4t)
  67. return;
  68. #endif
  69. m_directory_table = MM.allocate_user_physical_page();
  70. if (!m_directory_table)
  71. return;
  72. m_directory_pages[0] = MM.allocate_user_physical_page();
  73. if (!m_directory_pages[0])
  74. return;
  75. m_directory_pages[1] = MM.allocate_user_physical_page();
  76. if (!m_directory_pages[1])
  77. return;
  78. m_directory_pages[2] = MM.allocate_user_physical_page();
  79. if (!m_directory_pages[2])
  80. return;
  81. // Share the top 1 GiB of kernel-only mappings (>=3GiB or >=0xc0000000)
  82. m_directory_pages[3] = MM.kernel_page_directory().m_directory_pages[3];
  83. #if ARCH(X86_64)
  84. {
  85. auto& table = *(PageDirectoryPointerTable*)MM.quickmap_page(*m_pml4t);
  86. table.raw[0] = (FlatPtr)m_directory_table->paddr().as_ptr() | 3;
  87. MM.unquickmap_page();
  88. }
  89. #endif
  90. {
  91. auto& table = *(PageDirectoryPointerTable*)MM.quickmap_page(*m_directory_table);
  92. #if ARCH(I386)
  93. table.raw[0] = (FlatPtr)m_directory_pages[0]->paddr().as_ptr() | 1;
  94. table.raw[1] = (FlatPtr)m_directory_pages[1]->paddr().as_ptr() | 1;
  95. table.raw[2] = (FlatPtr)m_directory_pages[2]->paddr().as_ptr() | 1;
  96. table.raw[3] = (FlatPtr)m_directory_pages[3]->paddr().as_ptr() | 1;
  97. #else
  98. table.raw[0] = (FlatPtr)m_directory_pages[0]->paddr().as_ptr() | 3;
  99. table.raw[1] = (FlatPtr)m_directory_pages[1]->paddr().as_ptr() | 3;
  100. table.raw[2] = (FlatPtr)m_directory_pages[2]->paddr().as_ptr() | 3;
  101. table.raw[3] = (FlatPtr)m_directory_pages[3]->paddr().as_ptr() | 3;
  102. #endif
  103. // 2 ** MAXPHYADDR - 1
  104. // Where MAXPHYADDR = physical_address_bit_width
  105. u64 max_physical_address = (1ULL << Processor::current().physical_address_bit_width()) - 1;
  106. // bit 63 = no execute
  107. // bit 7 = page size
  108. // bit 5 = accessed
  109. // bit 4 = cache disable
  110. // bit 3 = write through
  111. // bit 2 = user/supervisor
  112. // bit 1 = read/write
  113. // bit 0 = present
  114. constexpr u64 pdpte_bit_flags = 0x80000000000000BF;
  115. // This is to notify us of bugs where we're:
  116. // 1. Going over what the processor is capable of.
  117. // 2. Writing into the reserved bits (51:MAXPHYADDR), where doing so throws a GPF
  118. // when writing out the PDPT pointer to CR3.
  119. // The reason we're not checking the page directory's physical address directly is because
  120. // we're checking for sign extension when putting it into a PDPTE. See issue #4584.
  121. VERIFY((table.raw[0] & ~pdpte_bit_flags) <= max_physical_address);
  122. VERIFY((table.raw[1] & ~pdpte_bit_flags) <= max_physical_address);
  123. VERIFY((table.raw[2] & ~pdpte_bit_flags) <= max_physical_address);
  124. VERIFY((table.raw[3] & ~pdpte_bit_flags) <= max_physical_address);
  125. MM.unquickmap_page();
  126. }
  127. // Clone bottom 2 MiB of mappings from kernel_page_directory
  128. PageDirectoryEntry buffer;
  129. auto* kernel_pd = MM.quickmap_pd(MM.kernel_page_directory(), 0);
  130. memcpy(&buffer, kernel_pd, sizeof(PageDirectoryEntry));
  131. auto* new_pd = MM.quickmap_pd(*this, 0);
  132. memcpy(new_pd, &buffer, sizeof(PageDirectoryEntry));
  133. // If we got here, we successfully created it. Set m_space now
  134. m_valid = true;
  135. cr3_map().set(cr3(), this);
  136. }
  137. PageDirectory::~PageDirectory()
  138. {
  139. ScopedSpinLock lock(s_mm_lock);
  140. if (m_space)
  141. cr3_map().remove(cr3());
  142. }
  143. }