MemoryManager.h 10 KB

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