mmap.cpp 24 KB

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