Region.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/EnumBits.h>
  8. #include <AK/IntrusiveList.h>
  9. #include <AK/Weakable.h>
  10. #include <Kernel/Arch/x86/PageFault.h>
  11. #include <Kernel/Forward.h>
  12. #include <Kernel/Heap/SlabAllocator.h>
  13. #include <Kernel/KString.h>
  14. #include <Kernel/Memory/PageFaultResponse.h>
  15. #include <Kernel/Memory/VirtualRangeAllocator.h>
  16. #include <Kernel/Sections.h>
  17. #include <Kernel/UnixTypes.h>
  18. namespace Kernel::Memory {
  19. enum class ShouldFlushTLB {
  20. No,
  21. Yes,
  22. };
  23. class Region final
  24. : public Weakable<Region> {
  25. friend class MemoryManager;
  26. MAKE_SLAB_ALLOCATED(Region)
  27. public:
  28. enum Access : u8 {
  29. None = 0,
  30. Read = 1,
  31. Write = 2,
  32. Execute = 4,
  33. HasBeenReadable = 16,
  34. HasBeenWritable = 32,
  35. HasBeenExecutable = 64,
  36. ReadOnly = Read,
  37. ReadWrite = Read | Write,
  38. ReadWriteExecute = Read | Write | Execute,
  39. };
  40. enum class Cacheable {
  41. No = 0,
  42. Yes,
  43. };
  44. static KResultOr<NonnullOwnPtr<Region>> try_create_user_accessible(VirtualRange const&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable, bool shared);
  45. static KResultOr<NonnullOwnPtr<Region>> try_create_kernel_only(VirtualRange const&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable = Cacheable::Yes);
  46. ~Region();
  47. [[nodiscard]] VirtualRange const& range() const { return m_range; }
  48. [[nodiscard]] VirtualAddress vaddr() const { return m_range.base(); }
  49. [[nodiscard]] size_t size() const { return m_range.size(); }
  50. [[nodiscard]] bool is_readable() const { return m_access & Access::Read; }
  51. [[nodiscard]] bool is_writable() const { return m_access & Access::Write; }
  52. [[nodiscard]] bool is_executable() const { return m_access & Access::Execute; }
  53. [[nodiscard]] bool has_been_readable() const { return m_access & Access::HasBeenReadable; }
  54. [[nodiscard]] bool has_been_writable() const { return m_access & Access::HasBeenWritable; }
  55. [[nodiscard]] bool has_been_executable() const { return m_access & Access::HasBeenExecutable; }
  56. [[nodiscard]] bool is_cacheable() const { return m_cacheable; }
  57. [[nodiscard]] StringView name() const { return m_name ? m_name->view() : StringView {}; }
  58. [[nodiscard]] OwnPtr<KString> take_name() { return move(m_name); }
  59. [[nodiscard]] Region::Access access() const { return static_cast<Region::Access>(m_access); }
  60. void set_name(OwnPtr<KString> name) { m_name = move(name); }
  61. [[nodiscard]] VMObject const& vmobject() const { return *m_vmobject; }
  62. [[nodiscard]] VMObject& vmobject() { return *m_vmobject; }
  63. void set_vmobject(NonnullRefPtr<VMObject>&&);
  64. [[nodiscard]] bool is_shared() const { return m_shared; }
  65. void set_shared(bool shared) { m_shared = shared; }
  66. [[nodiscard]] bool is_stack() const { return m_stack; }
  67. void set_stack(bool stack) { m_stack = stack; }
  68. [[nodiscard]] bool is_mmap() const { return m_mmap; }
  69. void set_mmap(bool mmap) { m_mmap = mmap; }
  70. [[nodiscard]] bool is_user() const { return !is_kernel(); }
  71. [[nodiscard]] bool is_kernel() const { return vaddr().get() < 0x00800000 || vaddr().get() >= kernel_mapping_base; }
  72. PageFaultResponse handle_fault(PageFault const&);
  73. KResultOr<NonnullOwnPtr<Region>> try_clone();
  74. [[nodiscard]] bool contains(VirtualAddress vaddr) const
  75. {
  76. return m_range.contains(vaddr);
  77. }
  78. [[nodiscard]] bool contains(VirtualRange const& range) const
  79. {
  80. return m_range.contains(range);
  81. }
  82. [[nodiscard]] unsigned page_index_from_address(VirtualAddress vaddr) const
  83. {
  84. return (vaddr - m_range.base()).get() / PAGE_SIZE;
  85. }
  86. [[nodiscard]] VirtualAddress vaddr_from_page_index(size_t page_index) const
  87. {
  88. return vaddr().offset(page_index * PAGE_SIZE);
  89. }
  90. [[nodiscard]] bool translate_vmobject_page(size_t& index) const
  91. {
  92. auto first_index = first_page_index();
  93. if (index < first_index) {
  94. index = first_index;
  95. return false;
  96. }
  97. index -= first_index;
  98. auto total_page_count = this->page_count();
  99. if (index >= total_page_count) {
  100. index = first_index + total_page_count - 1;
  101. return false;
  102. }
  103. return true;
  104. }
  105. [[nodiscard]] ALWAYS_INLINE size_t translate_to_vmobject_page(size_t page_index) const
  106. {
  107. return first_page_index() + page_index;
  108. }
  109. [[nodiscard]] size_t first_page_index() const
  110. {
  111. return m_offset_in_vmobject / PAGE_SIZE;
  112. }
  113. [[nodiscard]] size_t page_count() const
  114. {
  115. return size() / PAGE_SIZE;
  116. }
  117. PhysicalPage const* physical_page(size_t index) const;
  118. RefPtr<PhysicalPage>& physical_page_slot(size_t index);
  119. [[nodiscard]] size_t offset_in_vmobject() const
  120. {
  121. return m_offset_in_vmobject;
  122. }
  123. [[nodiscard]] size_t offset_in_vmobject_from_vaddr(VirtualAddress vaddr) const
  124. {
  125. return m_offset_in_vmobject + vaddr.get() - this->vaddr().get();
  126. }
  127. [[nodiscard]] size_t amount_resident() const;
  128. [[nodiscard]] size_t amount_shared() const;
  129. [[nodiscard]] size_t amount_dirty() const;
  130. [[nodiscard]] bool should_cow(size_t page_index) const;
  131. void set_should_cow(size_t page_index, bool);
  132. [[nodiscard]] size_t cow_pages() const;
  133. void set_readable(bool b) { set_access_bit(Access::Read, b); }
  134. void set_writable(bool b) { set_access_bit(Access::Write, b); }
  135. void set_executable(bool b) { set_access_bit(Access::Execute, b); }
  136. void set_page_directory(PageDirectory&);
  137. [[nodiscard]] bool map(PageDirectory&, ShouldFlushTLB = ShouldFlushTLB::Yes);
  138. enum class ShouldDeallocateVirtualRange {
  139. No,
  140. Yes,
  141. };
  142. void unmap(ShouldDeallocateVirtualRange = ShouldDeallocateVirtualRange::Yes);
  143. void remap();
  144. [[nodiscard]] bool is_syscall_region() const { return m_syscall_region; }
  145. void set_syscall_region(bool b) { m_syscall_region = b; }
  146. private:
  147. Region(VirtualRange const&, NonnullRefPtr<VMObject>, size_t offset_in_vmobject, OwnPtr<KString>, Region::Access access, Cacheable, bool shared);
  148. [[nodiscard]] bool remap_vmobject_page(size_t page_index, bool with_flush = true);
  149. [[nodiscard]] bool do_remap_vmobject_page(size_t page_index, bool with_flush = true);
  150. void set_access_bit(Access access, bool b)
  151. {
  152. if (b)
  153. m_access |= access | (access << 4);
  154. else
  155. m_access &= ~access;
  156. }
  157. [[nodiscard]] PageFaultResponse handle_cow_fault(size_t page_index);
  158. [[nodiscard]] PageFaultResponse handle_inode_fault(size_t page_index);
  159. [[nodiscard]] PageFaultResponse handle_zero_fault(size_t page_index);
  160. [[nodiscard]] bool map_individual_page_impl(size_t page_index);
  161. RefPtr<PageDirectory> m_page_directory;
  162. VirtualRange m_range;
  163. size_t m_offset_in_vmobject { 0 };
  164. NonnullRefPtr<VMObject> m_vmobject;
  165. OwnPtr<KString> m_name;
  166. u8 m_access { Region::None };
  167. bool m_shared : 1 { false };
  168. bool m_cacheable : 1 { false };
  169. bool m_stack : 1 { false };
  170. bool m_mmap : 1 { false };
  171. bool m_syscall_region : 1 { false };
  172. IntrusiveListNode<Region> m_memory_manager_list_node;
  173. IntrusiveListNode<Region> m_vmobject_list_node;
  174. public:
  175. using ListInMemoryManager = IntrusiveList<Region, RawPtr<Region>, &Region::m_memory_manager_list_node>;
  176. using ListInVMObject = IntrusiveList<Region, RawPtr<Region>, &Region::m_vmobject_list_node>;
  177. };
  178. AK_ENUM_BITWISE_OPERATORS(Region::Access)
  179. inline Region::Access prot_to_region_access_flags(int prot)
  180. {
  181. Region::Access access = Region::Access::None;
  182. if (prot & PROT_READ)
  183. access |= Region::Access::Read;
  184. if (prot & PROT_WRITE)
  185. access |= Region::Access::Write;
  186. if (prot & PROT_EXEC)
  187. access |= Region::Access::Execute;
  188. return access;
  189. }
  190. inline int region_access_flags_to_prot(Region::Access access)
  191. {
  192. int prot = 0;
  193. if (access & Region::Access::Read)
  194. prot |= PROT_READ;
  195. if (access & Region::Access::Write)
  196. prot |= PROT_WRITE;
  197. if (access & Region::Access::Execute)
  198. prot |= PROT_EXEC;
  199. return prot;
  200. }
  201. }