MemoryManager.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/SpinLock.h>
  33. #include <Kernel/VM/PhysicalPage.h>
  34. #include <Kernel/VM/Region.h>
  35. #include <Kernel/VM/VMObject.h>
  36. namespace Kernel {
  37. #define PAGE_ROUND_UP(x) ((((u32)(x)) + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1)))
  38. template<typename T>
  39. inline T* low_physical_to_virtual(T* physical)
  40. {
  41. return (T*)(((u8*)physical) + 0xc0000000);
  42. }
  43. inline u32 low_physical_to_virtual(u32 physical)
  44. {
  45. return physical + 0xc0000000;
  46. }
  47. template<typename T>
  48. inline T* virtual_to_low_physical(T* physical)
  49. {
  50. return (T*)(((u8*)physical) - 0xc0000000);
  51. }
  52. inline u32 virtual_to_low_physical(u32 physical)
  53. {
  54. return physical - 0xc0000000;
  55. }
  56. class KBuffer;
  57. class SynthFSInode;
  58. #define MM Kernel::MemoryManager::the()
  59. struct MemoryManagerData {
  60. SpinLock<u8> m_quickmap_in_use;
  61. u32 m_quickmap_prev_flags;
  62. };
  63. extern RecursiveSpinLock s_mm_lock;
  64. class MemoryManager {
  65. AK_MAKE_ETERNAL
  66. friend class PageDirectory;
  67. friend class PhysicalPage;
  68. friend class PhysicalRegion;
  69. friend class Region;
  70. friend class VMObject;
  71. friend Optional<KBuffer> procfs$mm(InodeIdentifier);
  72. friend Optional<KBuffer> procfs$memstat(InodeIdentifier);
  73. public:
  74. static MemoryManager& the();
  75. static void initialize(u32 cpu);
  76. static inline MemoryManagerData& get_data()
  77. {
  78. return Processor::current().get_mm_data();
  79. }
  80. PageFaultResponse handle_page_fault(const PageFault&);
  81. void enter_process_paging_scope(Process&);
  82. bool validate_user_stack(const Process&, VirtualAddress) const;
  83. bool validate_user_read(const Process&, VirtualAddress, size_t) const;
  84. bool validate_user_write(const Process&, VirtualAddress, size_t) const;
  85. bool validate_kernel_read(const Process&, VirtualAddress, size_t) const;
  86. bool can_read_without_faulting(const Process&, VirtualAddress, size_t) const;
  87. enum class ShouldZeroFill {
  88. No,
  89. Yes
  90. };
  91. RefPtr<PhysicalPage> allocate_user_physical_page(ShouldZeroFill = ShouldZeroFill::Yes);
  92. RefPtr<PhysicalPage> allocate_supervisor_physical_page();
  93. NonnullRefPtrVector<PhysicalPage> allocate_contiguous_supervisor_physical_pages(size_t size);
  94. void deallocate_user_physical_page(PhysicalPage&&);
  95. void deallocate_supervisor_physical_page(PhysicalPage&&);
  96. OwnPtr<Region> allocate_contiguous_kernel_region(size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  97. OwnPtr<Region> allocate_kernel_region(size_t, const StringView& name, u8 access, bool user_accessible = false, bool should_commit = true, bool cacheable = true);
  98. OwnPtr<Region> allocate_kernel_region(PhysicalAddress, size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  99. OwnPtr<Region> allocate_kernel_region_identity(PhysicalAddress, size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  100. OwnPtr<Region> allocate_kernel_region_with_vmobject(VMObject&, size_t, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  101. OwnPtr<Region> allocate_kernel_region_with_vmobject(const Range&, VMObject&, const StringView& name, u8 access, bool user_accessible = false, bool cacheable = true);
  102. OwnPtr<Region> allocate_user_accessible_kernel_region(size_t, const StringView& name, u8 access, bool cacheable = true);
  103. unsigned user_physical_pages() const { return m_user_physical_pages; }
  104. unsigned user_physical_pages_used() const { return m_user_physical_pages_used; }
  105. unsigned super_physical_pages() const { return m_super_physical_pages; }
  106. unsigned super_physical_pages_used() const { return m_super_physical_pages_used; }
  107. template<typename Callback>
  108. static void for_each_vmobject(Callback callback)
  109. {
  110. for (auto& vmobject : MM.m_vmobjects) {
  111. if (callback(vmobject) == IterationDecision::Break)
  112. break;
  113. }
  114. }
  115. template<typename T, typename Callback>
  116. static void for_each_vmobject_of_type(Callback callback)
  117. {
  118. for (auto& vmobject : MM.m_vmobjects) {
  119. if (!is<T>(vmobject))
  120. continue;
  121. if (callback(static_cast<T&>(vmobject)) == IterationDecision::Break)
  122. break;
  123. }
  124. }
  125. static Region* find_region_from_vaddr(Process&, VirtualAddress);
  126. static const Region* find_region_from_vaddr(const Process&, VirtualAddress);
  127. void dump_kernel_regions();
  128. PhysicalPage& shared_zero_page() { return *m_shared_zero_page; }
  129. PageDirectory& kernel_page_directory() { return *m_kernel_page_directory; }
  130. private:
  131. MemoryManager();
  132. ~MemoryManager();
  133. enum class AccessSpace { Kernel,
  134. User };
  135. enum class AccessType { Read,
  136. Write };
  137. template<AccessSpace, AccessType>
  138. bool validate_range(const Process&, VirtualAddress, size_t) const;
  139. void register_vmobject(VMObject&);
  140. void unregister_vmobject(VMObject&);
  141. void register_region(Region&);
  142. void unregister_region(Region&);
  143. void detect_cpu_features();
  144. void protect_kernel_image();
  145. void parse_memory_map();
  146. static void flush_tlb_local(VirtualAddress, size_t page_count = 1);
  147. static void flush_tlb(VirtualAddress, size_t page_count = 1);
  148. static Region* user_region_from_vaddr(Process&, VirtualAddress);
  149. static Region* kernel_region_from_vaddr(VirtualAddress);
  150. static Region* find_region_from_vaddr(VirtualAddress);
  151. RefPtr<PhysicalPage> find_free_user_physical_page();
  152. u8* quickmap_page(PhysicalPage&);
  153. void unquickmap_page();
  154. PageDirectoryEntry* quickmap_pd(PageDirectory&, size_t pdpt_index);
  155. PageTableEntry* quickmap_pt(PhysicalAddress);
  156. const PageTableEntry* pte(const PageDirectory&, VirtualAddress);
  157. PageTableEntry& ensure_pte(PageDirectory&, VirtualAddress);
  158. RefPtr<PageDirectory> m_kernel_page_directory;
  159. RefPtr<PhysicalPage> m_low_page_table;
  160. RefPtr<PhysicalPage> m_shared_zero_page;
  161. unsigned m_user_physical_pages { 0 };
  162. unsigned m_user_physical_pages_used { 0 };
  163. unsigned m_super_physical_pages { 0 };
  164. unsigned m_super_physical_pages_used { 0 };
  165. NonnullRefPtrVector<PhysicalRegion> m_user_physical_regions;
  166. NonnullRefPtrVector<PhysicalRegion> m_super_physical_regions;
  167. InlineLinkedList<Region> m_user_regions;
  168. InlineLinkedList<Region> m_kernel_regions;
  169. InlineLinkedList<VMObject> m_vmobjects;
  170. RefPtr<PhysicalPage> m_low_pseudo_identity_mapping_pages[4];
  171. };
  172. template<typename Callback>
  173. void VMObject::for_each_region(Callback callback)
  174. {
  175. // FIXME: Figure out a better data structure so we don't have to walk every single region every time an inode changes.
  176. // Perhaps VMObject could have a Vector<Region*> with all of his mappers?
  177. for (auto& region : MM.m_user_regions) {
  178. if (&region.vmobject() == this)
  179. callback(region);
  180. }
  181. for (auto& region : MM.m_kernel_regions) {
  182. if (&region.vmobject() == this)
  183. callback(region);
  184. }
  185. }
  186. inline bool is_user_address(VirtualAddress vaddr)
  187. {
  188. return vaddr.get() < 0xc0000000;
  189. }
  190. inline bool is_user_range(VirtualAddress vaddr, size_t size)
  191. {
  192. if (vaddr.offset(size) < vaddr)
  193. return false;
  194. return is_user_address(vaddr) && is_user_address(vaddr.offset(size));
  195. }
  196. inline bool PhysicalPage::is_shared_zero_page() const
  197. {
  198. return this == &MM.shared_zero_page();
  199. }
  200. }