MemoryManager.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. NonnullRefPtrVector<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_identity(PhysicalAddress, size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  90. OwnPtr<Region> allocate_kernel_region_with_vmobject(VMObject&, size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  91. OwnPtr<Region> allocate_kernel_region_with_vmobject(const Range&, VMObject&, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  92. OwnPtr<Region> allocate_user_accessible_kernel_region(size_t, const StringView& name, u8 access, bool cacheable = true);
  93. unsigned user_physical_pages() const { return m_user_physical_pages; }
  94. unsigned user_physical_pages_used() const { return m_user_physical_pages_used; }
  95. unsigned super_physical_pages() const { return m_super_physical_pages; }
  96. unsigned super_physical_pages_used() const { return m_super_physical_pages_used; }
  97. template<typename Callback>
  98. static void for_each_vmobject(Callback callback)
  99. {
  100. for (auto& vmobject : MM.m_vmobjects) {
  101. if (callback(vmobject) == IterationDecision::Break)
  102. break;
  103. }
  104. }
  105. template<typename T, typename Callback>
  106. static void for_each_vmobject_of_type(Callback callback)
  107. {
  108. for (auto& vmobject : MM.m_vmobjects) {
  109. if (!is<T>(vmobject))
  110. continue;
  111. if (callback(static_cast<T&>(vmobject)) == IterationDecision::Break)
  112. break;
  113. }
  114. }
  115. static Region* region_from_vaddr(Process&, VirtualAddress);
  116. static const Region* region_from_vaddr(const Process&, VirtualAddress);
  117. void dump_kernel_regions();
  118. PhysicalPage& shared_zero_page() { return *m_shared_zero_page; }
  119. PageDirectory& kernel_page_directory() { return *m_kernel_page_directory; }
  120. private:
  121. MemoryManager();
  122. ~MemoryManager();
  123. enum class AccessSpace { Kernel,
  124. User };
  125. enum class AccessType { Read,
  126. Write };
  127. template<AccessSpace, AccessType>
  128. bool validate_range(const Process&, VirtualAddress, size_t) const;
  129. void register_vmobject(VMObject&);
  130. void unregister_vmobject(VMObject&);
  131. void register_region(Region&);
  132. void unregister_region(Region&);
  133. void detect_cpu_features();
  134. void protect_kernel_image();
  135. void parse_memory_map();
  136. void flush_entire_tlb();
  137. void flush_tlb(VirtualAddress);
  138. static Region* user_region_from_vaddr(Process&, VirtualAddress);
  139. static Region* kernel_region_from_vaddr(VirtualAddress);
  140. static Region* region_from_vaddr(VirtualAddress);
  141. RefPtr<PhysicalPage> find_free_user_physical_page();
  142. u8* quickmap_page(PhysicalPage&);
  143. void unquickmap_page();
  144. PageDirectoryEntry* quickmap_pd(PageDirectory&, size_t pdpt_index);
  145. PageTableEntry* quickmap_pt(PhysicalAddress);
  146. const PageTableEntry* pte(const PageDirectory&, VirtualAddress);
  147. PageTableEntry& ensure_pte(PageDirectory&, VirtualAddress);
  148. RefPtr<PageDirectory> m_kernel_page_directory;
  149. RefPtr<PhysicalPage> m_low_page_table;
  150. RefPtr<PhysicalPage> m_shared_zero_page;
  151. unsigned m_user_physical_pages { 0 };
  152. unsigned m_user_physical_pages_used { 0 };
  153. unsigned m_super_physical_pages { 0 };
  154. unsigned m_super_physical_pages_used { 0 };
  155. NonnullRefPtrVector<PhysicalRegion> m_user_physical_regions;
  156. NonnullRefPtrVector<PhysicalRegion> m_super_physical_regions;
  157. InlineLinkedList<Region> m_user_regions;
  158. InlineLinkedList<Region> m_kernel_regions;
  159. InlineLinkedList<VMObject> m_vmobjects;
  160. bool m_quickmap_in_use { false };
  161. RefPtr<PhysicalPage> m_low_pseudo_identity_mapping_pages[4];
  162. };
  163. template<typename Callback>
  164. void VMObject::for_each_region(Callback callback)
  165. {
  166. // FIXME: Figure out a better data structure so we don't have to walk every single region every time an inode changes.
  167. // Perhaps VMObject could have a Vector<Region*> with all of his mappers?
  168. for (auto& region : MM.m_user_regions) {
  169. if (&region.vmobject() == this)
  170. callback(region);
  171. }
  172. for (auto& region : MM.m_kernel_regions) {
  173. if (&region.vmobject() == this)
  174. callback(region);
  175. }
  176. }
  177. inline bool is_user_address(VirtualAddress vaddr)
  178. {
  179. return vaddr.get() < 0xc0000000;
  180. }
  181. inline bool is_user_range(VirtualAddress vaddr, size_t size)
  182. {
  183. if (vaddr.offset(size) < vaddr)
  184. return false;
  185. return is_user_address(vaddr) && is_user_address(vaddr.offset(size));
  186. }
  187. inline bool PhysicalPage::is_shared_zero_page() const
  188. {
  189. return this == &MM.shared_zero_page();
  190. }
  191. }