Region.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. #include <AK/Memory.h>
  27. #include <AK/StringView.h>
  28. #include <Kernel/Debug.h>
  29. #include <Kernel/FileSystem/Inode.h>
  30. #include <Kernel/Process.h>
  31. #include <Kernel/Thread.h>
  32. #include <Kernel/VM/AnonymousVMObject.h>
  33. #include <Kernel/VM/MemoryManager.h>
  34. #include <Kernel/VM/PageDirectory.h>
  35. #include <Kernel/VM/Region.h>
  36. #include <Kernel/VM/SharedInodeVMObject.h>
  37. namespace Kernel {
  38. Region::Region(const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, u8 access, bool cacheable, bool kernel, bool shared)
  39. : PurgeablePageRanges(vmobject)
  40. , m_range(range)
  41. , m_offset_in_vmobject(offset_in_vmobject)
  42. , m_vmobject(move(vmobject))
  43. , m_name(name)
  44. , m_access(access)
  45. , m_shared(shared)
  46. , m_cacheable(cacheable)
  47. , m_kernel(kernel)
  48. {
  49. m_vmobject->ref_region();
  50. register_purgeable_page_ranges();
  51. MM.register_region(*this);
  52. }
  53. Region::~Region()
  54. {
  55. m_vmobject->unref_region();
  56. unregister_purgeable_page_ranges();
  57. // Make sure we disable interrupts so we don't get interrupted between unmapping and unregistering.
  58. // Unmapping the region will give the VM back to the RangeAllocator, so an interrupt handler would
  59. // find the address<->region mappings in an invalid state there.
  60. ScopedSpinLock lock(s_mm_lock);
  61. if (m_page_directory) {
  62. unmap(ShouldDeallocateVirtualMemoryRange::Yes);
  63. ASSERT(!m_page_directory);
  64. }
  65. MM.unregister_region(*this);
  66. }
  67. void Region::register_purgeable_page_ranges()
  68. {
  69. if (m_vmobject->is_anonymous()) {
  70. auto& vmobject = static_cast<AnonymousVMObject&>(*m_vmobject);
  71. vmobject.register_purgeable_page_ranges(*this);
  72. }
  73. }
  74. void Region::unregister_purgeable_page_ranges()
  75. {
  76. if (m_vmobject->is_anonymous()) {
  77. auto& vmobject = static_cast<AnonymousVMObject&>(*m_vmobject);
  78. vmobject.unregister_purgeable_page_ranges(*this);
  79. }
  80. }
  81. OwnPtr<Region> Region::clone(Process& new_owner)
  82. {
  83. ASSERT(Process::current());
  84. ScopedSpinLock lock(s_mm_lock);
  85. if (m_shared) {
  86. ASSERT(!m_stack);
  87. if (vmobject().is_inode())
  88. ASSERT(vmobject().is_shared_inode());
  89. // Create a new region backed by the same VMObject.
  90. auto region = Region::create_user_accessible(
  91. &new_owner, m_range, m_vmobject, m_offset_in_vmobject, m_name, m_access, m_cacheable, m_shared);
  92. if (m_vmobject->is_anonymous())
  93. region->copy_purgeable_page_ranges(*this);
  94. region->set_mmap(m_mmap);
  95. region->set_shared(m_shared);
  96. return region;
  97. }
  98. if (vmobject().is_inode())
  99. ASSERT(vmobject().is_private_inode());
  100. auto vmobject_clone = vmobject().clone();
  101. if (!vmobject_clone)
  102. return {};
  103. // Set up a COW region. The parent (this) region becomes COW as well!
  104. remap();
  105. auto clone_region = Region::create_user_accessible(
  106. &new_owner, m_range, vmobject_clone.release_nonnull(), m_offset_in_vmobject, m_name, m_access, m_cacheable, m_shared);
  107. if (m_vmobject->is_anonymous())
  108. clone_region->copy_purgeable_page_ranges(*this);
  109. if (m_stack) {
  110. ASSERT(is_readable());
  111. ASSERT(is_writable());
  112. ASSERT(vmobject().is_anonymous());
  113. clone_region->set_stack(true);
  114. }
  115. clone_region->set_mmap(m_mmap);
  116. return clone_region;
  117. }
  118. void Region::set_vmobject(NonnullRefPtr<VMObject>&& obj)
  119. {
  120. if (m_vmobject.ptr() == obj.ptr())
  121. return;
  122. unregister_purgeable_page_ranges();
  123. m_vmobject->unref_region();
  124. m_vmobject = move(obj);
  125. m_vmobject->ref_region();
  126. register_purgeable_page_ranges();
  127. }
  128. bool Region::is_volatile(VirtualAddress vaddr, size_t size) const
  129. {
  130. if (!m_vmobject->is_anonymous())
  131. return false;
  132. auto offset_in_vmobject = vaddr.get() - (this->vaddr().get() - m_offset_in_vmobject);
  133. size_t first_page_index = PAGE_ROUND_DOWN(offset_in_vmobject) / PAGE_SIZE;
  134. size_t last_page_index = PAGE_ROUND_UP(offset_in_vmobject + size) / PAGE_SIZE;
  135. return is_volatile_range({ first_page_index, last_page_index - first_page_index });
  136. }
  137. auto Region::set_volatile(VirtualAddress vaddr, size_t size, bool is_volatile, bool& was_purged) -> SetVolatileError
  138. {
  139. was_purged = false;
  140. if (!m_vmobject->is_anonymous())
  141. return SetVolatileError::NotPurgeable;
  142. auto offset_in_vmobject = vaddr.get() - (this->vaddr().get() - m_offset_in_vmobject);
  143. if (is_volatile) {
  144. // If marking pages as volatile, be prudent by not marking
  145. // partial pages volatile to prevent potentially non-volatile
  146. // data to be discarded. So rund up the first page and round
  147. // down the last page.
  148. size_t first_page_index = PAGE_ROUND_UP(offset_in_vmobject) / PAGE_SIZE;
  149. size_t last_page_index = PAGE_ROUND_DOWN(offset_in_vmobject + size) / PAGE_SIZE;
  150. if (first_page_index != last_page_index)
  151. add_volatile_range({ first_page_index, last_page_index - first_page_index });
  152. } else {
  153. // If marking pages as non-volatile, round down the first page
  154. // and round up the last page to make sure the beginning and
  155. // end of the range doesn't inadvertedly get discarded.
  156. size_t first_page_index = PAGE_ROUND_DOWN(offset_in_vmobject) / PAGE_SIZE;
  157. size_t last_page_index = PAGE_ROUND_UP(offset_in_vmobject + size) / PAGE_SIZE;
  158. switch (remove_volatile_range({ first_page_index, last_page_index - first_page_index }, was_purged)) {
  159. case PurgeablePageRanges::RemoveVolatileError::Success:
  160. case PurgeablePageRanges::RemoveVolatileError::SuccessNoChange:
  161. break;
  162. case PurgeablePageRanges::RemoveVolatileError::OutOfMemory:
  163. return SetVolatileError::OutOfMemory;
  164. }
  165. }
  166. return SetVolatileError::Success;
  167. }
  168. size_t Region::cow_pages() const
  169. {
  170. if (!vmobject().is_anonymous())
  171. return 0;
  172. return static_cast<const AnonymousVMObject&>(vmobject()).cow_pages();
  173. }
  174. size_t Region::amount_dirty() const
  175. {
  176. if (!vmobject().is_inode())
  177. return amount_resident();
  178. return static_cast<const InodeVMObject&>(vmobject()).amount_dirty();
  179. }
  180. size_t Region::amount_resident() const
  181. {
  182. size_t bytes = 0;
  183. for (size_t i = 0; i < page_count(); ++i) {
  184. auto* page = physical_page(i);
  185. if (page && !page->is_shared_zero_page() && !page->is_lazy_committed_page())
  186. bytes += PAGE_SIZE;
  187. }
  188. return bytes;
  189. }
  190. size_t Region::amount_shared() const
  191. {
  192. size_t bytes = 0;
  193. for (size_t i = 0; i < page_count(); ++i) {
  194. auto* page = physical_page(i);
  195. if (page && page->ref_count() > 1 && !page->is_shared_zero_page() && !page->is_lazy_committed_page())
  196. bytes += PAGE_SIZE;
  197. }
  198. return bytes;
  199. }
  200. NonnullOwnPtr<Region> Region::create_user_accessible(Process* owner, const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const StringView& name, u8 access, bool cacheable, bool shared)
  201. {
  202. auto region = make<Region>(range, move(vmobject), offset_in_vmobject, name, access, cacheable, false, shared);
  203. if (owner)
  204. region->m_owner = owner->make_weak_ptr();
  205. region->m_user_accessible = true;
  206. return region;
  207. }
  208. NonnullOwnPtr<Region> Region::create_kernel_only(const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const StringView& name, u8 access, bool cacheable)
  209. {
  210. auto region = make<Region>(range, move(vmobject), offset_in_vmobject, name, access, cacheable, true, false);
  211. region->m_user_accessible = false;
  212. return region;
  213. }
  214. bool Region::should_cow(size_t page_index) const
  215. {
  216. if (!vmobject().is_anonymous())
  217. return false;
  218. return static_cast<const AnonymousVMObject&>(vmobject()).should_cow(first_page_index() + page_index, m_shared);
  219. }
  220. void Region::set_should_cow(size_t page_index, bool cow)
  221. {
  222. ASSERT(!m_shared);
  223. if (vmobject().is_anonymous())
  224. static_cast<AnonymousVMObject&>(vmobject()).set_should_cow(first_page_index() + page_index, cow);
  225. }
  226. bool Region::map_individual_page_impl(size_t page_index)
  227. {
  228. ASSERT(m_page_directory->get_lock().own_lock());
  229. auto page_vaddr = vaddr_from_page_index(page_index);
  230. auto* pte = MM.ensure_pte(*m_page_directory, page_vaddr);
  231. if (!pte)
  232. return false;
  233. auto* page = physical_page(page_index);
  234. if (!page || (!is_readable() && !is_writable())) {
  235. pte->clear();
  236. } else {
  237. pte->set_cache_disabled(!m_cacheable);
  238. pte->set_physical_page_base(page->paddr().get());
  239. pte->set_present(true);
  240. if (page->is_shared_zero_page() || page->is_lazy_committed_page() || should_cow(page_index))
  241. pte->set_writable(false);
  242. else
  243. pte->set_writable(is_writable());
  244. if (Processor::current().has_feature(CPUFeature::NX))
  245. pte->set_execute_disabled(!is_executable());
  246. pte->set_user_allowed(is_user_accessible());
  247. }
  248. return true;
  249. }
  250. bool Region::do_remap_vmobject_page_range(size_t page_index, size_t page_count)
  251. {
  252. bool success = true;
  253. ASSERT(s_mm_lock.own_lock());
  254. ASSERT(m_page_directory);
  255. if (!translate_vmobject_page_range(page_index, page_count))
  256. return success; // not an error, region doesn't map this page range
  257. ScopedSpinLock page_lock(m_page_directory->get_lock());
  258. size_t index = page_index;
  259. while (index < page_index + page_count) {
  260. if (!map_individual_page_impl(index)) {
  261. success = false;
  262. break;
  263. }
  264. index++;
  265. }
  266. if (index > page_index)
  267. MM.flush_tlb(m_page_directory, vaddr_from_page_index(page_index), index - page_index);
  268. return success;
  269. }
  270. bool Region::remap_vmobject_page_range(size_t page_index, size_t page_count)
  271. {
  272. bool success = true;
  273. ScopedSpinLock lock(s_mm_lock);
  274. auto& vmobject = this->vmobject();
  275. if (vmobject.is_shared_by_multiple_regions()) {
  276. vmobject.for_each_region([&](auto& region) {
  277. if (!region.do_remap_vmobject_page_range(page_index, page_count))
  278. success = false;
  279. });
  280. } else {
  281. if (!do_remap_vmobject_page_range(page_index, page_count))
  282. success = false;
  283. }
  284. return success;
  285. }
  286. bool Region::do_remap_vmobject_page(size_t page_index, bool with_flush)
  287. {
  288. ScopedSpinLock lock(s_mm_lock);
  289. ASSERT(m_page_directory);
  290. if (!translate_vmobject_page(page_index))
  291. return true; // not an error, region doesn't map this page
  292. ScopedSpinLock page_lock(m_page_directory->get_lock());
  293. ASSERT(physical_page(page_index));
  294. bool success = map_individual_page_impl(page_index);
  295. if (with_flush)
  296. MM.flush_tlb(m_page_directory, vaddr_from_page_index(page_index));
  297. return success;
  298. }
  299. bool Region::remap_vmobject_page(size_t page_index, bool with_flush)
  300. {
  301. bool success = true;
  302. ScopedSpinLock lock(s_mm_lock);
  303. auto& vmobject = this->vmobject();
  304. if (vmobject.is_shared_by_multiple_regions()) {
  305. vmobject.for_each_region([&](auto& region) {
  306. if (!region.do_remap_vmobject_page(page_index, with_flush))
  307. success = false;
  308. });
  309. } else {
  310. if (!do_remap_vmobject_page(page_index, with_flush))
  311. success = false;
  312. }
  313. return success;
  314. }
  315. void Region::unmap(ShouldDeallocateVirtualMemoryRange deallocate_range)
  316. {
  317. ScopedSpinLock lock(s_mm_lock);
  318. if (!m_page_directory)
  319. return;
  320. ScopedSpinLock page_lock(m_page_directory->get_lock());
  321. size_t count = page_count();
  322. for (size_t i = 0; i < count; ++i) {
  323. auto vaddr = vaddr_from_page_index(i);
  324. MM.release_pte(*m_page_directory, vaddr, i == count - 1);
  325. }
  326. MM.flush_tlb(m_page_directory, vaddr(), page_count());
  327. if (deallocate_range == ShouldDeallocateVirtualMemoryRange::Yes) {
  328. if (m_page_directory->range_allocator().contains(range()))
  329. m_page_directory->range_allocator().deallocate(range());
  330. else
  331. m_page_directory->identity_range_allocator().deallocate(range());
  332. }
  333. m_page_directory = nullptr;
  334. }
  335. void Region::set_page_directory(PageDirectory& page_directory)
  336. {
  337. ASSERT(!m_page_directory || m_page_directory == &page_directory);
  338. ASSERT(s_mm_lock.own_lock());
  339. m_page_directory = page_directory;
  340. }
  341. bool Region::map(PageDirectory& page_directory)
  342. {
  343. ScopedSpinLock lock(s_mm_lock);
  344. ScopedSpinLock page_lock(page_directory.get_lock());
  345. // FIXME: Find a better place for this sanity check(?)
  346. if (is_user_accessible() && !is_shared()) {
  347. ASSERT(!vmobject().is_shared_inode());
  348. }
  349. set_page_directory(page_directory);
  350. size_t page_index = 0;
  351. while (page_index < page_count()) {
  352. if (!map_individual_page_impl(page_index))
  353. break;
  354. ++page_index;
  355. }
  356. if (page_index > 0) {
  357. MM.flush_tlb(m_page_directory, vaddr(), page_index);
  358. return page_index == page_count();
  359. }
  360. return false;
  361. }
  362. void Region::remap()
  363. {
  364. ASSERT(m_page_directory);
  365. map(*m_page_directory);
  366. }
  367. PageFaultResponse Region::handle_fault(const PageFault& fault)
  368. {
  369. ScopedSpinLock lock(s_mm_lock);
  370. auto page_index_in_region = page_index_from_address(fault.vaddr());
  371. if (fault.type() == PageFault::Type::PageNotPresent) {
  372. if (fault.is_read() && !is_readable()) {
  373. dbgln("NP(non-readable) fault in Region({})[{}]", this, page_index_in_region);
  374. return PageFaultResponse::ShouldCrash;
  375. }
  376. if (fault.is_write() && !is_writable()) {
  377. dbgln("NP(non-writable) write fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
  378. return PageFaultResponse::ShouldCrash;
  379. }
  380. if (vmobject().is_inode()) {
  381. dbgln<PAGE_FAULT_DEBUG>("NP(inode) fault in Region({})[{}]", this, page_index_in_region);
  382. return handle_inode_fault(page_index_in_region);
  383. }
  384. auto& page_slot = physical_page_slot(page_index_in_region);
  385. if (page_slot->is_lazy_committed_page()) {
  386. auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
  387. page_slot = static_cast<AnonymousVMObject&>(*m_vmobject).allocate_committed_page(page_index_in_vmobject);
  388. remap_vmobject_page(page_index_in_vmobject);
  389. return PageFaultResponse::Continue;
  390. }
  391. #ifdef MAP_SHARED_ZERO_PAGE_LAZILY
  392. if (fault.is_read()) {
  393. page_slot = MM.shared_zero_page();
  394. remap_vmobject_page(translate_to_vmobject_page(page_index_in_region));
  395. return PageFaultResponse::Continue;
  396. }
  397. return handle_zero_fault(page_index_in_region);
  398. #else
  399. dbgln("BUG! Unexpected NP fault at {}", fault.vaddr());
  400. return PageFaultResponse::ShouldCrash;
  401. #endif
  402. }
  403. ASSERT(fault.type() == PageFault::Type::ProtectionViolation);
  404. if (fault.access() == PageFault::Access::Write && is_writable() && should_cow(page_index_in_region)) {
  405. dbgln<PAGE_FAULT_DEBUG>("PV(cow) fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
  406. auto* phys_page = physical_page(page_index_in_region);
  407. if (phys_page->is_shared_zero_page() || phys_page->is_lazy_committed_page()) {
  408. dbgln<PAGE_FAULT_DEBUG>("NP(zero) fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
  409. return handle_zero_fault(page_index_in_region);
  410. }
  411. return handle_cow_fault(page_index_in_region);
  412. }
  413. dbgln("PV(error) fault in Region({})[{}] at {}", this, page_index_in_region, fault.vaddr());
  414. return PageFaultResponse::ShouldCrash;
  415. }
  416. PageFaultResponse Region::handle_zero_fault(size_t page_index_in_region)
  417. {
  418. ASSERT_INTERRUPTS_DISABLED();
  419. ASSERT(vmobject().is_anonymous());
  420. LOCKER(vmobject().m_paging_lock);
  421. auto& page_slot = physical_page_slot(page_index_in_region);
  422. auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
  423. if (!page_slot.is_null() && !page_slot->is_shared_zero_page() && !page_slot->is_lazy_committed_page()) {
  424. #if PAGE_FAULT_DEBUG
  425. dbgln("MM: zero_page() but page already present. Fine with me!");
  426. #endif
  427. if (!remap_vmobject_page(page_index_in_vmobject))
  428. return PageFaultResponse::OutOfMemory;
  429. return PageFaultResponse::Continue;
  430. }
  431. auto current_thread = Thread::current();
  432. if (current_thread != nullptr)
  433. current_thread->did_zero_fault();
  434. if (page_slot->is_lazy_committed_page()) {
  435. page_slot = static_cast<AnonymousVMObject&>(*m_vmobject).allocate_committed_page(page_index_in_vmobject);
  436. dbgln<PAGE_FAULT_DEBUG>(" >> ALLOCATED COMMITTED {}", page_slot->paddr());
  437. } else {
  438. page_slot = MM.allocate_user_physical_page(MemoryManager::ShouldZeroFill::Yes);
  439. if (page_slot.is_null()) {
  440. klog() << "MM: handle_zero_fault was unable to allocate a physical page";
  441. return PageFaultResponse::OutOfMemory;
  442. }
  443. dbgln<PAGE_FAULT_DEBUG>(" >> ALLOCATED {}", page_slot->paddr());
  444. }
  445. if (!remap_vmobject_page(page_index_in_vmobject)) {
  446. klog() << "MM: handle_zero_fault was unable to allocate a page table to map " << page_slot;
  447. return PageFaultResponse::OutOfMemory;
  448. }
  449. return PageFaultResponse::Continue;
  450. }
  451. PageFaultResponse Region::handle_cow_fault(size_t page_index_in_region)
  452. {
  453. ASSERT_INTERRUPTS_DISABLED();
  454. auto current_thread = Thread::current();
  455. if (current_thread)
  456. current_thread->did_cow_fault();
  457. if (!vmobject().is_anonymous())
  458. return PageFaultResponse::ShouldCrash;
  459. auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
  460. auto response = reinterpret_cast<AnonymousVMObject&>(vmobject()).handle_cow_fault(page_index_in_vmobject, vaddr().offset(page_index_in_region * PAGE_SIZE));
  461. if (!remap_vmobject_page(page_index_in_vmobject))
  462. return PageFaultResponse::OutOfMemory;
  463. return response;
  464. }
  465. PageFaultResponse Region::handle_inode_fault(size_t page_index_in_region)
  466. {
  467. ASSERT_INTERRUPTS_DISABLED();
  468. ASSERT(vmobject().is_inode());
  469. LOCKER(vmobject().m_paging_lock);
  470. ASSERT_INTERRUPTS_DISABLED();
  471. auto& inode_vmobject = static_cast<InodeVMObject&>(vmobject());
  472. auto page_index_in_vmobject = translate_to_vmobject_page(page_index_in_region);
  473. auto& vmobject_physical_page_entry = inode_vmobject.physical_pages()[page_index_in_vmobject];
  474. dbgln<PAGE_FAULT_DEBUG>("Inode fault in {} page index: {}", name(), page_index_in_region);
  475. if (!vmobject_physical_page_entry.is_null()) {
  476. dbgln<PAGE_FAULT_DEBUG>("MM: page_in_from_inode() but page already present. Fine with me!");
  477. if (!remap_vmobject_page(page_index_in_vmobject))
  478. return PageFaultResponse::OutOfMemory;
  479. return PageFaultResponse::Continue;
  480. }
  481. auto current_thread = Thread::current();
  482. if (current_thread)
  483. current_thread->did_inode_fault();
  484. u8 page_buffer[PAGE_SIZE];
  485. auto& inode = inode_vmobject.inode();
  486. auto buffer = UserOrKernelBuffer::for_kernel_buffer(page_buffer);
  487. auto nread = inode.read_bytes(page_index_in_vmobject * PAGE_SIZE, PAGE_SIZE, buffer, nullptr);
  488. if (nread < 0) {
  489. klog() << "MM: handle_inode_fault had error (" << nread << ") while reading!";
  490. return PageFaultResponse::ShouldCrash;
  491. }
  492. if (nread < PAGE_SIZE) {
  493. // If we read less than a page, zero out the rest to avoid leaking uninitialized data.
  494. memset(page_buffer + nread, 0, PAGE_SIZE - nread);
  495. }
  496. vmobject_physical_page_entry = MM.allocate_user_physical_page(MemoryManager::ShouldZeroFill::No);
  497. if (vmobject_physical_page_entry.is_null()) {
  498. klog() << "MM: handle_inode_fault was unable to allocate a physical page";
  499. return PageFaultResponse::OutOfMemory;
  500. }
  501. u8* dest_ptr = MM.quickmap_page(*vmobject_physical_page_entry);
  502. {
  503. void* fault_at;
  504. if (!safe_memcpy(dest_ptr, page_buffer, PAGE_SIZE, fault_at)) {
  505. if ((u8*)fault_at >= dest_ptr && (u8*)fault_at <= dest_ptr + PAGE_SIZE)
  506. dbgln(" >> inode fault: error copying data to {}/{}, failed at {}",
  507. vmobject_physical_page_entry->paddr(),
  508. VirtualAddress(dest_ptr),
  509. VirtualAddress(fault_at));
  510. else
  511. ASSERT_NOT_REACHED();
  512. }
  513. }
  514. MM.unquickmap_page();
  515. remap_vmobject_page(page_index_in_vmobject);
  516. return PageFaultResponse::Continue;
  517. }
  518. RefPtr<Process> Region::get_owner()
  519. {
  520. return m_owner.strong_ref();
  521. }
  522. }