MemoryManager.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/HashTable.h>
  28. #include <AK/NonnullRefPtrVector.h>
  29. #include <AK/String.h>
  30. #include <Kernel/Arch/i386/CPU.h>
  31. #include <Kernel/Forward.h>
  32. #include <Kernel/VM/PhysicalPage.h>
  33. #include <Kernel/VM/Region.h>
  34. #include <Kernel/VM/VMObject.h>
  35. namespace Kernel {
  36. #define PAGE_ROUND_UP(x) ((((u32)(x)) + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)))
  37. template<typename T>
  38. inline T* low_physical_to_virtual(T* physical)
  39. {
  40. return (T*)(((u8*)physical) + 0xc0000000);
  41. }
  42. inline u32 low_physical_to_virtual(u32 physical)
  43. {
  44. return physical + 0xc0000000;
  45. }
  46. template<typename T>
  47. inline T* virtual_to_low_physical(T* physical)
  48. {
  49. return (T*)(((u8*)physical) - 0xc0000000);
  50. }
  51. inline u32 virtual_to_low_physical(u32 physical)
  52. {
  53. return physical - 0xc0000000;
  54. }
  55. class KBuffer;
  56. class SynthFSInode;
  57. #define MM Kernel::MemoryManager::the()
  58. class MemoryManager {
  59. AK_MAKE_ETERNAL
  60. friend class PageDirectory;
  61. friend class PhysicalPage;
  62. friend class PhysicalRegion;
  63. friend class Region;
  64. friend class VMObject;
  65. friend Optional<KBuffer> procfs$mm(InodeIdentifier);
  66. friend Optional<KBuffer> procfs$memstat(InodeIdentifier);
  67. public:
  68. static MemoryManager& the();
  69. static void initialize();
  70. PageFaultResponse handle_page_fault(const PageFault&);
  71. void enter_process_paging_scope(Process&);
  72. bool validate_user_stack(const Process&, VirtualAddress) const;
  73. bool validate_user_read(const Process&, VirtualAddress, size_t) const;
  74. bool validate_user_write(const Process&, VirtualAddress, size_t) const;
  75. bool validate_kernel_read(const Process&, VirtualAddress, size_t) const;
  76. bool can_read_without_faulting(const Process&, VirtualAddress, size_t) const;
  77. enum class ShouldZeroFill {
  78. No,
  79. Yes
  80. };
  81. RefPtr<PhysicalPage> allocate_user_physical_page(ShouldZeroFill = ShouldZeroFill::Yes);
  82. RefPtr<PhysicalPage> allocate_supervisor_physical_page();
  83. Vector<RefPtr<PhysicalPage>> allocate_contiguous_supervisor_physical_pages(size_t size);
  84. void deallocate_user_physical_page(PhysicalPage&&);
  85. void deallocate_supervisor_physical_page(PhysicalPage&&);
  86. OwnPtr<Region> allocate_contiguous_kernel_region(size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  87. OwnPtr<Region> allocate_kernel_region(size_t, const StringView& name, u8 access, bool user_accessible = false, bool should_commit = true, bool cacheable = true);
  88. OwnPtr<Region> allocate_kernel_region(PhysicalAddress, size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  89. OwnPtr<Region> allocate_kernel_region_with_vmobject(VMObject&, size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  90. OwnPtr<Region> allocate_kernel_region_with_vmobject(const Range&, VMObject&, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  91. OwnPtr<Region> allocate_user_accessible_kernel_region(size_t, const StringView& name, u8 access, bool cacheable = true);
  92. unsigned user_physical_pages() const { return m_user_physical_pages; }
  93. unsigned user_physical_pages_used() const { return m_user_physical_pages_used; }
  94. unsigned super_physical_pages() const { return m_super_physical_pages; }
  95. unsigned super_physical_pages_used() const { return m_super_physical_pages_used; }
  96. template<typename Callback>
  97. static void for_each_vmobject(Callback callback)
  98. {
  99. for (auto& vmobject : MM.m_vmobjects) {
  100. if (callback(vmobject) == IterationDecision::Break)
  101. break;
  102. }
  103. }
  104. static Region* region_from_vaddr(Process&, VirtualAddress);
  105. static const Region* region_from_vaddr(const Process&, VirtualAddress);
  106. void dump_kernel_regions();
  107. PhysicalPage& shared_zero_page() { return *m_shared_zero_page; }
  108. private:
  109. MemoryManager();
  110. ~MemoryManager();
  111. enum class AccessSpace { Kernel,
  112. User };
  113. enum class AccessType { Read,
  114. Write };
  115. template<AccessSpace, AccessType>
  116. bool validate_range(const Process&, VirtualAddress, size_t) const;
  117. void register_vmobject(VMObject&);
  118. void unregister_vmobject(VMObject&);
  119. void register_region(Region&);
  120. void unregister_region(Region&);
  121. void detect_cpu_features();
  122. void setup_low_identity_mapping();
  123. void setup_low_pseudo_identity_mapping();
  124. void protect_kernel_image();
  125. void parse_memory_map();
  126. void flush_entire_tlb();
  127. void flush_tlb(VirtualAddress);
  128. static Region* user_region_from_vaddr(Process&, VirtualAddress);
  129. static Region* kernel_region_from_vaddr(VirtualAddress);
  130. static Region* region_from_vaddr(VirtualAddress);
  131. RefPtr<PhysicalPage> find_free_user_physical_page();
  132. u8* quickmap_page(PhysicalPage&);
  133. void unquickmap_page();
  134. PageDirectoryEntry* quickmap_pd(PageDirectory&, size_t pdpt_index);
  135. PageTableEntry* quickmap_pt(PhysicalAddress);
  136. PageDirectory& kernel_page_directory() { return *m_kernel_page_directory; }
  137. const PageTableEntry* pte(const PageDirectory&, VirtualAddress);
  138. PageTableEntry& ensure_pte(PageDirectory&, VirtualAddress);
  139. RefPtr<PageDirectory> m_kernel_page_directory;
  140. RefPtr<PhysicalPage> m_low_page_table;
  141. RefPtr<PhysicalPage> m_shared_zero_page;
  142. unsigned m_user_physical_pages { 0 };
  143. unsigned m_user_physical_pages_used { 0 };
  144. unsigned m_super_physical_pages { 0 };
  145. unsigned m_super_physical_pages_used { 0 };
  146. NonnullRefPtrVector<PhysicalRegion> m_user_physical_regions;
  147. NonnullRefPtrVector<PhysicalRegion> m_super_physical_regions;
  148. InlineLinkedList<Region> m_user_regions;
  149. InlineLinkedList<Region> m_kernel_regions;
  150. InlineLinkedList<VMObject> m_vmobjects;
  151. bool m_quickmap_in_use { false };
  152. RefPtr<PhysicalPage> m_low_pseudo_identity_mapping_pages[4];
  153. };
  154. template<typename Callback>
  155. void VMObject::for_each_region(Callback callback)
  156. {
  157. // FIXME: Figure out a better data structure so we don't have to walk every single region every time an inode changes.
  158. // Perhaps VMObject could have a Vector<Region*> with all of his mappers?
  159. for (auto& region : MM.m_user_regions) {
  160. if (&region.vmobject() == this)
  161. callback(region);
  162. }
  163. for (auto& region : MM.m_kernel_regions) {
  164. if (&region.vmobject() == this)
  165. callback(region);
  166. }
  167. }
  168. inline bool is_user_address(VirtualAddress vaddr)
  169. {
  170. return vaddr.get() < 0xc0000000;
  171. }
  172. inline bool is_user_range(VirtualAddress vaddr, size_t size)
  173. {
  174. if (vaddr.offset(size) < vaddr)
  175. return false;
  176. return is_user_address(vaddr) && is_user_address(vaddr.offset(size));
  177. }
  178. inline bool PhysicalPage::is_shared_zero_page() const
  179. {
  180. return this == &MM.shared_zero_page();
  181. }
  182. }