mmap.cpp 25 KB

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