mmap.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <Kernel/Arch/SafeMem.h>
  8. #include <Kernel/Arch/SmapDisabler.h>
  9. #include <Kernel/Arch/x86/MSR.h>
  10. #include <Kernel/FileSystem/OpenFileDescription.h>
  11. #include <Kernel/Memory/AnonymousVMObject.h>
  12. #include <Kernel/Memory/MemoryManager.h>
  13. #include <Kernel/Memory/PageDirectory.h>
  14. #include <Kernel/Memory/PrivateInodeVMObject.h>
  15. #include <Kernel/Memory/Region.h>
  16. #include <Kernel/Memory/SharedInodeVMObject.h>
  17. #include <Kernel/PerformanceEventBuffer.h>
  18. #include <Kernel/PerformanceManager.h>
  19. #include <Kernel/Process.h>
  20. #include <LibC/limits.h>
  21. #include <LibELF/Validation.h>
  22. namespace Kernel {
  23. static bool should_make_executable_exception_for_dynamic_loader(bool make_readable, bool make_writable, bool make_executable, Memory::Region const& region)
  24. {
  25. // Normally we don't allow W -> X transitions, but we have to make an exception
  26. // for the dynamic loader, which needs to do this after performing text relocations.
  27. // FIXME: Investigate whether we could get rid of all text relocations entirely.
  28. // The exception is only made if all the following criteria is fulfilled:
  29. // The region must be RW
  30. if (!(region.is_readable() && region.is_writable() && !region.is_executable()))
  31. return false;
  32. // The region wants to become RX
  33. if (!(make_readable && !make_writable && make_executable))
  34. return false;
  35. // The region is backed by a file
  36. if (!region.vmobject().is_inode())
  37. return false;
  38. // The file mapping is private, not shared (no relocations in a shared mapping!)
  39. if (!region.vmobject().is_private_inode())
  40. return false;
  41. auto const& inode_vm = static_cast<Memory::InodeVMObject const&>(region.vmobject());
  42. auto const& inode = inode_vm.inode();
  43. ElfW(Ehdr) header;
  44. auto buffer = UserOrKernelBuffer::for_kernel_buffer((u8*)&header);
  45. auto result = inode.read_bytes(0, sizeof(header), buffer, nullptr);
  46. if (result.is_error() || result.value() != sizeof(header))
  47. return false;
  48. // The file is a valid ELF binary
  49. if (!ELF::validate_elf_header(header, inode.size()))
  50. return false;
  51. // The file is an ELF shared object
  52. if (header.e_type != ET_DYN)
  53. return false;
  54. // FIXME: Are there any additional checks/validations we could do here?
  55. return true;
  56. }
  57. ErrorOr<void> Process::validate_mmap_prot(int prot, bool map_stack, bool map_anonymous, Memory::Region const* region) const
  58. {
  59. bool make_readable = prot & PROT_READ;
  60. bool make_writable = prot & PROT_WRITE;
  61. bool make_executable = prot & PROT_EXEC;
  62. if (map_anonymous && make_executable && !(executable()->mount_flags() & MS_AXALLOWED))
  63. return EINVAL;
  64. if (map_stack && make_executable)
  65. return EINVAL;
  66. if (executable()->mount_flags() & MS_WXALLOWED)
  67. return {};
  68. if (make_writable && make_executable)
  69. return EINVAL;
  70. if (region) {
  71. if (make_writable && region->has_been_executable())
  72. return EINVAL;
  73. if (make_executable && region->has_been_writable()) {
  74. if (should_make_executable_exception_for_dynamic_loader(make_readable, make_writable, make_executable, *region)) {
  75. return {};
  76. } else {
  77. return EINVAL;
  78. };
  79. }
  80. }
  81. return {};
  82. }
  83. ErrorOr<void> Process::validate_inode_mmap_prot(int prot, Inode const& inode, bool map_shared) const
  84. {
  85. auto metadata = inode.metadata();
  86. if ((prot & PROT_READ) && !metadata.may_read(*this))
  87. return EACCES;
  88. if (map_shared) {
  89. // FIXME: What about readonly filesystem mounts? We cannot make a
  90. // decision here without knowing the mount flags, so we would need to
  91. // keep a Custody or something from mmap time.
  92. if ((prot & PROT_WRITE) && !metadata.may_write(*this))
  93. return EACCES;
  94. if (auto shared_vmobject = inode.shared_vmobject()) {
  95. if ((prot & PROT_EXEC) && shared_vmobject->writable_mappings())
  96. return EACCES;
  97. if ((prot & PROT_WRITE) && shared_vmobject->executable_mappings())
  98. return EACCES;
  99. }
  100. }
  101. return {};
  102. }
  103. ErrorOr<FlatPtr> Process::sys$mmap(Userspace<Syscall::SC_mmap_params const*> user_params)
  104. {
  105. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  106. TRY(require_promise(Pledge::stdio));
  107. auto params = TRY(copy_typed_from_user(user_params));
  108. auto addr = (FlatPtr)params.addr;
  109. auto size = params.size;
  110. auto alignment = params.alignment ? params.alignment : PAGE_SIZE;
  111. auto prot = params.prot;
  112. auto flags = params.flags;
  113. auto fd = params.fd;
  114. auto offset = params.offset;
  115. if (prot & PROT_EXEC) {
  116. TRY(require_promise(Pledge::prot_exec));
  117. }
  118. if (prot & MAP_FIXED || prot & MAP_FIXED_NOREPLACE) {
  119. TRY(require_promise(Pledge::map_fixed));
  120. }
  121. if (alignment & ~PAGE_MASK)
  122. return EINVAL;
  123. size_t rounded_size = TRY(Memory::page_round_up(size));
  124. if (!Memory::is_user_range(VirtualAddress(addr), rounded_size))
  125. return EFAULT;
  126. OwnPtr<KString> name;
  127. if (params.name.characters) {
  128. if (params.name.length > PATH_MAX)
  129. return ENAMETOOLONG;
  130. name = TRY(try_copy_kstring_from_user(params.name));
  131. }
  132. if (size == 0)
  133. return EINVAL;
  134. if ((FlatPtr)addr & ~PAGE_MASK)
  135. return EINVAL;
  136. bool map_shared = flags & MAP_SHARED;
  137. bool map_anonymous = flags & MAP_ANONYMOUS;
  138. bool map_private = flags & MAP_PRIVATE;
  139. bool map_stack = flags & MAP_STACK;
  140. bool map_fixed = flags & MAP_FIXED;
  141. bool map_noreserve = flags & MAP_NORESERVE;
  142. bool map_randomized = flags & MAP_RANDOMIZED;
  143. bool map_fixed_noreplace = flags & MAP_FIXED_NOREPLACE;
  144. if (map_shared && map_private)
  145. return EINVAL;
  146. if (!map_shared && !map_private)
  147. return EINVAL;
  148. if ((map_fixed || map_fixed_noreplace) && map_randomized)
  149. return EINVAL;
  150. TRY(validate_mmap_prot(prot, map_stack, map_anonymous));
  151. if (map_stack && (!map_private || !map_anonymous))
  152. return EINVAL;
  153. Memory::Region* region = nullptr;
  154. // If MAP_FIXED is specified, existing mappings that intersect the requested range are removed.
  155. if (map_fixed)
  156. TRY(address_space().unmap_mmap_range(VirtualAddress(addr), size));
  157. Memory::VirtualRange requested_range { VirtualAddress { addr }, rounded_size };
  158. if (addr && !(map_fixed || map_fixed_noreplace)) {
  159. // If there's an address but MAP_FIXED wasn't specified, the address is just a hint.
  160. requested_range = { {}, rounded_size };
  161. }
  162. if (map_anonymous) {
  163. auto strategy = map_noreserve ? AllocationStrategy::None : AllocationStrategy::Reserve;
  164. RefPtr<Memory::AnonymousVMObject> vmobject;
  165. if (flags & MAP_PURGEABLE) {
  166. vmobject = TRY(Memory::AnonymousVMObject::try_create_purgeable_with_size(rounded_size, strategy));
  167. } else {
  168. vmobject = TRY(Memory::AnonymousVMObject::try_create_with_size(rounded_size, strategy));
  169. }
  170. region = TRY(address_space().allocate_region_with_vmobject(map_randomized ? Memory::RandomizeVirtualAddress::Yes : Memory::RandomizeVirtualAddress::No, requested_range.base(), requested_range.size(), alignment, vmobject.release_nonnull(), 0, {}, prot, map_shared));
  171. } else {
  172. if (offset < 0)
  173. return EINVAL;
  174. if (static_cast<size_t>(offset) & ~PAGE_MASK)
  175. return EINVAL;
  176. auto description = TRY(open_file_description(fd));
  177. if (description->is_directory())
  178. return ENODEV;
  179. // Require read access even when read protection is not requested.
  180. if (!description->is_readable())
  181. return EACCES;
  182. if (map_shared) {
  183. if ((prot & PROT_WRITE) && !description->is_writable())
  184. return EACCES;
  185. }
  186. if (description->inode())
  187. TRY(validate_inode_mmap_prot(prot, *description->inode(), map_shared));
  188. region = TRY(description->mmap(*this, requested_range, static_cast<u64>(offset), prot, map_shared));
  189. }
  190. if (!region)
  191. return ENOMEM;
  192. region->set_mmap(true);
  193. if (map_shared)
  194. region->set_shared(true);
  195. if (map_stack)
  196. region->set_stack(true);
  197. if (name)
  198. region->set_name(move(name));
  199. PerformanceManager::add_mmap_perf_event(*this, *region);
  200. return region->vaddr().get();
  201. }
  202. ErrorOr<FlatPtr> Process::sys$mprotect(Userspace<void*> addr, size_t size, int prot)
  203. {
  204. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  205. TRY(require_promise(Pledge::stdio));
  206. if (prot & PROT_EXEC) {
  207. TRY(require_promise(Pledge::prot_exec));
  208. }
  209. auto range_to_mprotect = TRY(Memory::expand_range_to_page_boundaries(addr.ptr(), size));
  210. if (!range_to_mprotect.size())
  211. return EINVAL;
  212. if (!is_user_range(range_to_mprotect))
  213. return EFAULT;
  214. if (auto* whole_region = address_space().find_region_from_range(range_to_mprotect)) {
  215. if (!whole_region->is_mmap())
  216. return EPERM;
  217. TRY(validate_mmap_prot(prot, whole_region->is_stack(), whole_region->vmobject().is_anonymous(), whole_region));
  218. if (whole_region->access() == Memory::prot_to_region_access_flags(prot))
  219. return 0;
  220. if (whole_region->vmobject().is_inode())
  221. TRY(validate_inode_mmap_prot(prot, static_cast<Memory::InodeVMObject const&>(whole_region->vmobject()).inode(), whole_region->is_shared()));
  222. whole_region->set_readable(prot & PROT_READ);
  223. whole_region->set_writable(prot & PROT_WRITE);
  224. whole_region->set_executable(prot & PROT_EXEC);
  225. whole_region->remap();
  226. return 0;
  227. }
  228. // Check if we can carve out the desired range from an existing region
  229. if (auto* old_region = address_space().find_region_containing(range_to_mprotect)) {
  230. if (!old_region->is_mmap())
  231. return EPERM;
  232. TRY(validate_mmap_prot(prot, old_region->is_stack(), old_region->vmobject().is_anonymous(), old_region));
  233. if (old_region->access() == Memory::prot_to_region_access_flags(prot))
  234. return 0;
  235. if (old_region->vmobject().is_inode())
  236. TRY(validate_inode_mmap_prot(prot, static_cast<Memory::InodeVMObject const&>(old_region->vmobject()).inode(), old_region->is_shared()));
  237. // Remove the old region from our regions tree, since were going to add another region
  238. // with the exact same start address.
  239. auto region = address_space().take_region(*old_region);
  240. region->unmap();
  241. // This vector is the region(s) adjacent to our range.
  242. // We need to allocate a new region for the range we wanted to change permission bits on.
  243. auto adjacent_regions = TRY(address_space().try_split_region_around_range(*region, range_to_mprotect));
  244. size_t new_range_offset_in_vmobject = region->offset_in_vmobject() + (range_to_mprotect.base().get() - region->range().base().get());
  245. auto* new_region = TRY(address_space().try_allocate_split_region(*region, range_to_mprotect, new_range_offset_in_vmobject));
  246. new_region->set_readable(prot & PROT_READ);
  247. new_region->set_writable(prot & PROT_WRITE);
  248. new_region->set_executable(prot & PROT_EXEC);
  249. // Map the new regions using our page directory (they were just allocated and don't have one).
  250. for (auto* adjacent_region : adjacent_regions) {
  251. TRY(adjacent_region->map(address_space().page_directory()));
  252. }
  253. TRY(new_region->map(address_space().page_directory()));
  254. return 0;
  255. }
  256. if (auto const& regions = TRY(address_space().find_regions_intersecting(range_to_mprotect)); regions.size()) {
  257. size_t full_size_found = 0;
  258. // Check that all intersecting regions are compatible.
  259. for (auto const* region : regions) {
  260. if (!region->is_mmap())
  261. return EPERM;
  262. TRY(validate_mmap_prot(prot, region->is_stack(), region->vmobject().is_anonymous(), region));
  263. if (region->vmobject().is_inode())
  264. TRY(validate_inode_mmap_prot(prot, static_cast<Memory::InodeVMObject const&>(region->vmobject()).inode(), region->is_shared()));
  265. full_size_found += region->range().intersect(range_to_mprotect).size();
  266. }
  267. if (full_size_found != range_to_mprotect.size())
  268. return ENOMEM;
  269. // Finally, iterate over each region, either updating its access flags if the range covers it wholly,
  270. // or carving out a new subregion with the appropriate access flags set.
  271. for (auto* old_region : regions) {
  272. if (old_region->access() == Memory::prot_to_region_access_flags(prot))
  273. continue;
  274. auto const intersection_to_mprotect = range_to_mprotect.intersect(old_region->range());
  275. // If the region is completely covered by range, simply update the access flags
  276. if (intersection_to_mprotect == old_region->range()) {
  277. old_region->set_readable(prot & PROT_READ);
  278. old_region->set_writable(prot & PROT_WRITE);
  279. old_region->set_executable(prot & PROT_EXEC);
  280. old_region->remap();
  281. continue;
  282. }
  283. // Remove the old region from our regions tree, since were going to add another region
  284. // with the exact same start address.
  285. auto region = address_space().take_region(*old_region);
  286. region->unmap();
  287. // This vector is the region(s) adjacent to our range.
  288. // We need to allocate a new region for the range we wanted to change permission bits on.
  289. auto adjacent_regions = TRY(address_space().try_split_region_around_range(*old_region, intersection_to_mprotect));
  290. // Since the range is not contained in a single region, it can only partially cover its starting and ending region,
  291. // therefore carving out a chunk from the region will always produce a single extra region, and not two.
  292. VERIFY(adjacent_regions.size() == 1);
  293. size_t new_range_offset_in_vmobject = old_region->offset_in_vmobject() + (intersection_to_mprotect.base().get() - old_region->range().base().get());
  294. auto* new_region = TRY(address_space().try_allocate_split_region(*region, intersection_to_mprotect, new_range_offset_in_vmobject));
  295. new_region->set_readable(prot & PROT_READ);
  296. new_region->set_writable(prot & PROT_WRITE);
  297. new_region->set_executable(prot & PROT_EXEC);
  298. // Map the new region using our page directory (they were just allocated and don't have one) if any.
  299. if (adjacent_regions.size())
  300. TRY(adjacent_regions[0]->map(address_space().page_directory()));
  301. TRY(new_region->map(address_space().page_directory()));
  302. }
  303. return 0;
  304. }
  305. return EINVAL;
  306. }
  307. ErrorOr<FlatPtr> Process::sys$madvise(Userspace<void*> address, size_t size, int advice)
  308. {
  309. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  310. TRY(require_promise(Pledge::stdio));
  311. auto range_to_madvise = TRY(Memory::expand_range_to_page_boundaries(address.ptr(), size));
  312. if (!range_to_madvise.size())
  313. return EINVAL;
  314. if (!is_user_range(range_to_madvise))
  315. return EFAULT;
  316. auto* region = address_space().find_region_from_range(range_to_madvise);
  317. if (!region)
  318. return EINVAL;
  319. if (!region->is_mmap())
  320. return EPERM;
  321. if (advice == MADV_SET_VOLATILE || advice == MADV_SET_NONVOLATILE) {
  322. if (!region->vmobject().is_anonymous())
  323. return EINVAL;
  324. auto& vmobject = static_cast<Memory::AnonymousVMObject&>(region->vmobject());
  325. if (!vmobject.is_purgeable())
  326. return EINVAL;
  327. bool was_purged = false;
  328. TRY(vmobject.set_volatile(advice == MADV_SET_VOLATILE, was_purged));
  329. return was_purged ? 1 : 0;
  330. }
  331. return EINVAL;
  332. }
  333. ErrorOr<FlatPtr> Process::sys$set_mmap_name(Userspace<Syscall::SC_set_mmap_name_params const*> user_params)
  334. {
  335. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  336. TRY(require_promise(Pledge::stdio));
  337. auto params = TRY(copy_typed_from_user(user_params));
  338. if (params.name.length > PATH_MAX)
  339. return ENAMETOOLONG;
  340. auto name = TRY(try_copy_kstring_from_user(params.name));
  341. auto range = TRY(Memory::expand_range_to_page_boundaries((FlatPtr)params.addr, params.size));
  342. auto* region = address_space().find_region_from_range(range);
  343. if (!region)
  344. return EINVAL;
  345. if (!region->is_mmap())
  346. return EPERM;
  347. region->set_name(move(name));
  348. PerformanceManager::add_mmap_perf_event(*this, *region);
  349. return 0;
  350. }
  351. ErrorOr<FlatPtr> Process::sys$munmap(Userspace<void*> addr, size_t size)
  352. {
  353. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  354. TRY(require_promise(Pledge::stdio));
  355. TRY(address_space().unmap_mmap_range(addr.vaddr(), size));
  356. return 0;
  357. }
  358. ErrorOr<FlatPtr> Process::sys$mremap(Userspace<Syscall::SC_mremap_params const*> user_params)
  359. {
  360. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  361. TRY(require_promise(Pledge::stdio));
  362. auto params = TRY(copy_typed_from_user(user_params));
  363. auto old_range = TRY(Memory::expand_range_to_page_boundaries((FlatPtr)params.old_address, params.old_size));
  364. auto* old_region = address_space().find_region_from_range(old_range);
  365. if (!old_region)
  366. return EINVAL;
  367. if (!old_region->is_mmap())
  368. return EPERM;
  369. if (old_region->vmobject().is_shared_inode() && params.flags & MAP_PRIVATE && !(params.flags & (MAP_ANONYMOUS | MAP_NORESERVE))) {
  370. auto range = old_region->range();
  371. auto old_prot = region_access_flags_to_prot(old_region->access());
  372. auto old_offset = old_region->offset_in_vmobject();
  373. NonnullRefPtr inode = static_cast<Memory::SharedInodeVMObject&>(old_region->vmobject()).inode();
  374. auto new_vmobject = TRY(Memory::PrivateInodeVMObject::try_create_with_inode(inode));
  375. auto old_name = old_region->take_name();
  376. old_region->unmap();
  377. address_space().deallocate_region(*old_region);
  378. auto* new_region = TRY(address_space().allocate_region_with_vmobject(range, move(new_vmobject), old_offset, old_name->view(), old_prot, false));
  379. new_region->set_mmap(true);
  380. return new_region->vaddr().get();
  381. }
  382. dbgln("sys$mremap: Unimplemented remap request (flags={})", params.flags);
  383. return ENOTIMPL;
  384. }
  385. ErrorOr<FlatPtr> Process::sys$allocate_tls(Userspace<char const*> initial_data, size_t size)
  386. {
  387. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  388. TRY(require_promise(Pledge::stdio));
  389. if (!size || size % PAGE_SIZE != 0)
  390. return EINVAL;
  391. if (!m_master_tls_region.is_null())
  392. return EEXIST;
  393. if (thread_count() != 1)
  394. return EFAULT;
  395. Thread* main_thread = nullptr;
  396. bool multiple_threads = false;
  397. for_each_thread([&main_thread, &multiple_threads](auto& thread) {
  398. if (main_thread)
  399. multiple_threads = true;
  400. main_thread = &thread;
  401. return IterationDecision::Break;
  402. });
  403. VERIFY(main_thread);
  404. if (multiple_threads)
  405. return EINVAL;
  406. auto* region = TRY(address_space().allocate_region(Memory::RandomizeVirtualAddress::Yes, {}, size, PAGE_SIZE, "Master TLS"sv, PROT_READ | PROT_WRITE));
  407. m_master_tls_region = TRY(region->try_make_weak_ptr());
  408. m_master_tls_size = size;
  409. m_master_tls_alignment = PAGE_SIZE;
  410. {
  411. Kernel::SmapDisabler disabler;
  412. void* fault_at;
  413. if (!Kernel::safe_memcpy((char*)m_master_tls_region.unsafe_ptr()->vaddr().as_ptr(), (char*)initial_data.ptr(), size, fault_at))
  414. return EFAULT;
  415. }
  416. TRY(main_thread->make_thread_specific_region({}));
  417. #if ARCH(I386)
  418. auto& tls_descriptor = Processor::current().get_gdt_entry(GDT_SELECTOR_TLS);
  419. tls_descriptor.set_base(main_thread->thread_specific_data());
  420. tls_descriptor.set_limit(main_thread->thread_specific_region_size());
  421. #else
  422. MSR fs_base_msr(MSR_FS_BASE);
  423. fs_base_msr.set(main_thread->thread_specific_data().get());
  424. #endif
  425. return m_master_tls_region.unsafe_ptr()->vaddr().get();
  426. }
  427. ErrorOr<FlatPtr> Process::sys$msyscall(Userspace<void*> address)
  428. {
  429. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  430. if (address_space().enforces_syscall_regions())
  431. return EPERM;
  432. if (!address) {
  433. address_space().set_enforces_syscall_regions(true);
  434. return 0;
  435. }
  436. if (!Memory::is_user_address(address.vaddr()))
  437. return EFAULT;
  438. auto* region = address_space().find_region_containing(Memory::VirtualRange { address.vaddr(), 1 });
  439. if (!region)
  440. return EINVAL;
  441. if (!region->is_mmap())
  442. return EINVAL;
  443. region->set_syscall_region(true);
  444. return 0;
  445. }
  446. ErrorOr<FlatPtr> Process::sys$msync(Userspace<void*> address, size_t size, int flags)
  447. {
  448. if ((flags & (MS_SYNC | MS_ASYNC | MS_INVALIDATE)) != flags)
  449. return EINVAL;
  450. bool is_async = (flags & MS_ASYNC) == MS_ASYNC;
  451. bool is_sync = (flags & MS_SYNC) == MS_SYNC;
  452. if (is_sync == is_async)
  453. return EINVAL;
  454. if (address.ptr() % PAGE_SIZE != 0)
  455. return EINVAL;
  456. // Note: This is not specified
  457. auto rounded_size = TRY(Memory::page_round_up(size));
  458. auto regions = TRY(address_space().find_regions_intersecting(Memory::VirtualRange { address.vaddr(), rounded_size }));
  459. // All regions from address upto address+size shall be mapped
  460. if (regions.is_empty())
  461. return ENOMEM;
  462. size_t total_intersection_size = 0;
  463. Memory::VirtualRange range_to_sync { address.vaddr(), rounded_size };
  464. for (auto const* region : regions) {
  465. // Region was not mapped
  466. if (!region->is_mmap())
  467. return ENOMEM;
  468. total_intersection_size += region->range().intersect(range_to_sync).size();
  469. }
  470. // Part of the indicated range was not mapped
  471. if (total_intersection_size != size)
  472. return ENOMEM;
  473. for (auto* region : regions) {
  474. auto& vmobject = region->vmobject();
  475. if (!vmobject.is_shared_inode())
  476. continue;
  477. off_t offset = region->offset_in_vmobject() + address.ptr() - region->range().base().get();
  478. auto& inode_vmobject = static_cast<Memory::SharedInodeVMObject&>(vmobject);
  479. // FIXME: If multiple regions belong to the same vmobject we might want to coalesce these writes
  480. // FIXME: Handle MS_ASYNC
  481. TRY(inode_vmobject.sync(offset / PAGE_SIZE, rounded_size / PAGE_SIZE));
  482. // FIXME: Handle MS_INVALIDATE
  483. }
  484. return 0;
  485. }
  486. }