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/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. auto credentials = this->credentials();
  90. if ((prot & PROT_READ) && !readable_description)
  91. return EACCES;
  92. if (map_shared) {
  93. // FIXME: What about readonly filesystem mounts? We cannot make a
  94. // decision here without knowing the mount flags, so we would need to
  95. // keep a Custody or something from mmap time.
  96. if ((prot & PROT_WRITE) && !description_writable)
  97. return EACCES;
  98. }
  99. return {};
  100. }
  101. ErrorOr<FlatPtr> Process::sys$mmap(Userspace<Syscall::SC_mmap_params const*> user_params)
  102. {
  103. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  104. TRY(require_promise(Pledge::stdio));
  105. auto params = TRY(copy_typed_from_user(user_params));
  106. auto addr = (FlatPtr)params.addr;
  107. auto size = params.size;
  108. auto alignment = params.alignment ? params.alignment : PAGE_SIZE;
  109. auto prot = params.prot;
  110. auto flags = params.flags;
  111. auto fd = params.fd;
  112. auto offset = params.offset;
  113. if (prot & PROT_EXEC) {
  114. TRY(require_promise(Pledge::prot_exec));
  115. }
  116. if (prot & MAP_FIXED || prot & MAP_FIXED_NOREPLACE) {
  117. TRY(require_promise(Pledge::map_fixed));
  118. }
  119. if (alignment & ~PAGE_MASK)
  120. return EINVAL;
  121. size_t rounded_size = TRY(Memory::page_round_up(size));
  122. if (!Memory::is_user_range(VirtualAddress(addr), rounded_size))
  123. return EFAULT;
  124. OwnPtr<KString> name;
  125. if (params.name.characters) {
  126. if (params.name.length > PATH_MAX)
  127. return ENAMETOOLONG;
  128. name = TRY(try_copy_kstring_from_user(params.name));
  129. }
  130. if (size == 0)
  131. return EINVAL;
  132. if ((FlatPtr)addr & ~PAGE_MASK)
  133. return EINVAL;
  134. bool map_shared = flags & MAP_SHARED;
  135. bool map_anonymous = flags & MAP_ANONYMOUS;
  136. bool map_private = flags & MAP_PRIVATE;
  137. bool map_stack = flags & MAP_STACK;
  138. bool map_fixed = flags & MAP_FIXED;
  139. bool map_noreserve = flags & MAP_NORESERVE;
  140. bool map_randomized = flags & MAP_RANDOMIZED;
  141. bool map_fixed_noreplace = flags & MAP_FIXED_NOREPLACE;
  142. if (map_shared && map_private)
  143. return EINVAL;
  144. if (!map_shared && !map_private)
  145. return EINVAL;
  146. if ((map_fixed || map_fixed_noreplace) && map_randomized)
  147. return EINVAL;
  148. TRY(validate_mmap_prot(prot, map_stack, map_anonymous));
  149. if (map_stack && (!map_private || !map_anonymous))
  150. return EINVAL;
  151. Memory::VirtualRange requested_range { VirtualAddress { addr }, rounded_size };
  152. if (addr && !(map_fixed || map_fixed_noreplace)) {
  153. // If there's an address but MAP_FIXED wasn't specified, the address is just a hint.
  154. requested_range = { {}, rounded_size };
  155. }
  156. Memory::Region* region = nullptr;
  157. RefPtr<OpenFileDescription> description;
  158. LockRefPtr<Memory::VMObject> vmobject;
  159. u64 used_offset = 0;
  160. if (map_anonymous) {
  161. auto strategy = map_noreserve ? AllocationStrategy::None : AllocationStrategy::Reserve;
  162. if (flags & MAP_PURGEABLE) {
  163. vmobject = TRY(Memory::AnonymousVMObject::try_create_purgeable_with_size(rounded_size, strategy));
  164. } else {
  165. vmobject = TRY(Memory::AnonymousVMObject::try_create_with_size(rounded_size, strategy));
  166. }
  167. } else {
  168. if (offset < 0)
  169. return EINVAL;
  170. used_offset = static_cast<u64>(offset);
  171. if (static_cast<size_t>(offset) & ~PAGE_MASK)
  172. return EINVAL;
  173. description = TRY(open_file_description(fd));
  174. if (description->is_directory())
  175. return ENODEV;
  176. // Require read access even when read protection is not requested.
  177. if (!description->is_readable())
  178. return EACCES;
  179. if (map_shared) {
  180. if ((prot & PROT_WRITE) && !description->is_writable())
  181. return EACCES;
  182. }
  183. if (description->inode())
  184. TRY(validate_inode_mmap_prot(prot, description->is_readable(), description->is_writable(), map_shared));
  185. vmobject = TRY(description->vmobject_for_mmap(*this, requested_range, used_offset, map_shared));
  186. }
  187. return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
  188. // If MAP_FIXED is specified, existing mappings that intersect the requested range are removed.
  189. if (map_fixed)
  190. TRY(space->unmap_mmap_range(VirtualAddress(addr), size));
  191. region = TRY(space->allocate_region_with_vmobject(
  192. map_randomized ? Memory::RandomizeVirtualAddress::Yes : Memory::RandomizeVirtualAddress::No,
  193. requested_range.base(),
  194. requested_range.size(),
  195. alignment,
  196. vmobject.release_nonnull(),
  197. used_offset,
  198. {},
  199. prot,
  200. map_shared));
  201. if (!region)
  202. return ENOMEM;
  203. if (description)
  204. region->set_mmap(true, description->is_readable(), description->is_writable());
  205. else
  206. region->set_mmap(true, false, false);
  207. if (map_shared)
  208. region->set_shared(true);
  209. if (map_stack)
  210. region->set_stack(true);
  211. if (name)
  212. region->set_name(move(name));
  213. PerformanceManager::add_mmap_perf_event(*this, *region);
  214. return region->vaddr().get();
  215. });
  216. }
  217. ErrorOr<FlatPtr> Process::sys$mprotect(Userspace<void*> addr, size_t size, int prot)
  218. {
  219. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  220. TRY(require_promise(Pledge::stdio));
  221. if (prot & PROT_EXEC) {
  222. TRY(require_promise(Pledge::prot_exec));
  223. }
  224. auto range_to_mprotect = TRY(Memory::expand_range_to_page_boundaries(addr.ptr(), size));
  225. if (!range_to_mprotect.size())
  226. return EINVAL;
  227. if (!is_user_range(range_to_mprotect))
  228. return EFAULT;
  229. return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
  230. if (auto* whole_region = space->find_region_from_range(range_to_mprotect)) {
  231. if (!whole_region->is_mmap())
  232. return EPERM;
  233. if (whole_region->is_immutable())
  234. return EPERM;
  235. TRY(validate_mmap_prot(prot, whole_region->is_stack(), whole_region->vmobject().is_anonymous(), whole_region));
  236. if (whole_region->access() == Memory::prot_to_region_access_flags(prot))
  237. return 0;
  238. if (whole_region->vmobject().is_inode())
  239. TRY(validate_inode_mmap_prot(prot, whole_region->mmapped_from_readable(), whole_region->mmapped_from_writable(), whole_region->is_shared()));
  240. whole_region->set_readable(prot & PROT_READ);
  241. whole_region->set_writable(prot & PROT_WRITE);
  242. whole_region->set_executable(prot & PROT_EXEC);
  243. whole_region->remap();
  244. return 0;
  245. }
  246. // Check if we can carve out the desired range from an existing region
  247. if (auto* old_region = space->find_region_containing(range_to_mprotect)) {
  248. if (!old_region->is_mmap())
  249. return EPERM;
  250. if (old_region->is_immutable())
  251. return EPERM;
  252. TRY(validate_mmap_prot(prot, old_region->is_stack(), old_region->vmobject().is_anonymous(), old_region));
  253. if (old_region->access() == Memory::prot_to_region_access_flags(prot))
  254. return 0;
  255. if (old_region->vmobject().is_inode())
  256. TRY(validate_inode_mmap_prot(prot, old_region->mmapped_from_readable(), old_region->mmapped_from_writable(), old_region->is_shared()));
  257. // Remove the old region from our regions tree, since were going to add another region
  258. // with the exact same start address.
  259. auto region = space->take_region(*old_region);
  260. region->unmap();
  261. // This vector is the region(s) adjacent to our range.
  262. // We need to allocate a new region for the range we wanted to change permission bits on.
  263. auto adjacent_regions = TRY(space->try_split_region_around_range(*region, range_to_mprotect));
  264. size_t new_range_offset_in_vmobject = region->offset_in_vmobject() + (range_to_mprotect.base().get() - region->range().base().get());
  265. auto* new_region = TRY(space->try_allocate_split_region(*region, range_to_mprotect, new_range_offset_in_vmobject));
  266. new_region->set_readable(prot & PROT_READ);
  267. new_region->set_writable(prot & PROT_WRITE);
  268. new_region->set_executable(prot & PROT_EXEC);
  269. // Map the new regions using our page directory (they were just allocated and don't have one).
  270. for (auto* adjacent_region : adjacent_regions) {
  271. TRY(adjacent_region->map(space->page_directory()));
  272. }
  273. TRY(new_region->map(space->page_directory()));
  274. return 0;
  275. }
  276. if (auto const& regions = TRY(space->find_regions_intersecting(range_to_mprotect)); regions.size()) {
  277. size_t full_size_found = 0;
  278. // Check that all intersecting regions are compatible.
  279. for (auto const* region : regions) {
  280. if (!region->is_mmap())
  281. return EPERM;
  282. if (region->is_immutable())
  283. return EPERM;
  284. TRY(validate_mmap_prot(prot, region->is_stack(), region->vmobject().is_anonymous(), region));
  285. if (region->vmobject().is_inode())
  286. TRY(validate_inode_mmap_prot(prot, region->mmapped_from_readable(), region->mmapped_from_writable(), region->is_shared()));
  287. full_size_found += region->range().intersect(range_to_mprotect).size();
  288. }
  289. if (full_size_found != range_to_mprotect.size())
  290. return ENOMEM;
  291. // Finally, iterate over each region, either updating its access flags if the range covers it wholly,
  292. // or carving out a new subregion with the appropriate access flags set.
  293. for (auto* old_region : regions) {
  294. if (old_region->access() == Memory::prot_to_region_access_flags(prot))
  295. continue;
  296. auto const intersection_to_mprotect = range_to_mprotect.intersect(old_region->range());
  297. // If the region is completely covered by range, simply update the access flags
  298. if (intersection_to_mprotect == old_region->range()) {
  299. old_region->set_readable(prot & PROT_READ);
  300. old_region->set_writable(prot & PROT_WRITE);
  301. old_region->set_executable(prot & PROT_EXEC);
  302. old_region->remap();
  303. continue;
  304. }
  305. // Remove the old region from our regions tree, since were going to add another region
  306. // with the exact same start address.
  307. auto region = space->take_region(*old_region);
  308. region->unmap();
  309. // This vector is the region(s) adjacent to our range.
  310. // We need to allocate a new region for the range we wanted to change permission bits on.
  311. auto adjacent_regions = TRY(space->try_split_region_around_range(*old_region, intersection_to_mprotect));
  312. // Since the range is not contained in a single region, it can only partially cover its starting and ending region,
  313. // therefore carving out a chunk from the region will always produce a single extra region, and not two.
  314. VERIFY(adjacent_regions.size() == 1);
  315. size_t new_range_offset_in_vmobject = old_region->offset_in_vmobject() + (intersection_to_mprotect.base().get() - old_region->range().base().get());
  316. auto* new_region = TRY(space->try_allocate_split_region(*region, intersection_to_mprotect, new_range_offset_in_vmobject));
  317. new_region->set_readable(prot & PROT_READ);
  318. new_region->set_writable(prot & PROT_WRITE);
  319. new_region->set_executable(prot & PROT_EXEC);
  320. // Map the new region using our page directory (they were just allocated and don't have one) if any.
  321. if (adjacent_regions.size())
  322. TRY(adjacent_regions[0]->map(space->page_directory()));
  323. TRY(new_region->map(space->page_directory()));
  324. }
  325. return 0;
  326. }
  327. return EINVAL;
  328. });
  329. }
  330. ErrorOr<FlatPtr> Process::sys$madvise(Userspace<void*> address, size_t size, int advice)
  331. {
  332. VERIFY_NO_PROCESS_BIG_LOCK(this);
  333. TRY(require_promise(Pledge::stdio));
  334. auto range_to_madvise = TRY(Memory::expand_range_to_page_boundaries(address.ptr(), size));
  335. if (!range_to_madvise.size())
  336. return EINVAL;
  337. if (!is_user_range(range_to_madvise))
  338. return EFAULT;
  339. return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
  340. auto* region = space->find_region_from_range(range_to_madvise);
  341. if (!region)
  342. return EINVAL;
  343. if (!region->is_mmap())
  344. return EPERM;
  345. if (region->is_immutable())
  346. return EPERM;
  347. if (advice == MADV_SET_VOLATILE || advice == MADV_SET_NONVOLATILE) {
  348. if (!region->vmobject().is_anonymous())
  349. return EINVAL;
  350. auto& vmobject = static_cast<Memory::AnonymousVMObject&>(region->vmobject());
  351. if (!vmobject.is_purgeable())
  352. return EINVAL;
  353. bool was_purged = false;
  354. TRY(vmobject.set_volatile(advice == MADV_SET_VOLATILE, was_purged));
  355. return was_purged ? 1 : 0;
  356. }
  357. return EINVAL;
  358. });
  359. }
  360. ErrorOr<FlatPtr> Process::sys$set_mmap_name(Userspace<Syscall::SC_set_mmap_name_params const*> user_params)
  361. {
  362. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  363. TRY(require_promise(Pledge::stdio));
  364. auto params = TRY(copy_typed_from_user(user_params));
  365. if (params.name.length > PATH_MAX)
  366. return ENAMETOOLONG;
  367. auto name = TRY(try_copy_kstring_from_user(params.name));
  368. auto range = TRY(Memory::expand_range_to_page_boundaries((FlatPtr)params.addr, params.size));
  369. return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
  370. auto* region = space->find_region_from_range(range);
  371. if (!region)
  372. return EINVAL;
  373. if (!region->is_mmap())
  374. return EPERM;
  375. if (region->is_immutable())
  376. return EPERM;
  377. region->set_name(move(name));
  378. PerformanceManager::add_mmap_perf_event(*this, *region);
  379. return 0;
  380. });
  381. }
  382. ErrorOr<FlatPtr> Process::sys$munmap(Userspace<void*> addr, size_t size)
  383. {
  384. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  385. TRY(require_promise(Pledge::stdio));
  386. TRY(address_space().with([&](auto& space) {
  387. return space->unmap_mmap_range(addr.vaddr(), size);
  388. }));
  389. return 0;
  390. }
  391. ErrorOr<FlatPtr> Process::sys$mremap(Userspace<Syscall::SC_mremap_params const*> user_params)
  392. {
  393. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  394. TRY(require_promise(Pledge::stdio));
  395. auto params = TRY(copy_typed_from_user(user_params));
  396. auto old_range = TRY(Memory::expand_range_to_page_boundaries((FlatPtr)params.old_address, params.old_size));
  397. return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
  398. auto* old_region = space->find_region_from_range(old_range);
  399. if (!old_region)
  400. return EINVAL;
  401. if (!old_region->is_mmap())
  402. return EPERM;
  403. if (old_region->is_immutable())
  404. return EPERM;
  405. if (old_region->vmobject().is_shared_inode() && params.flags & MAP_PRIVATE && !(params.flags & (MAP_ANONYMOUS | MAP_NORESERVE))) {
  406. auto range = old_region->range();
  407. auto old_prot = region_access_flags_to_prot(old_region->access());
  408. auto old_offset = old_region->offset_in_vmobject();
  409. NonnullLockRefPtr inode = static_cast<Memory::SharedInodeVMObject&>(old_region->vmobject()).inode();
  410. auto new_vmobject = TRY(Memory::PrivateInodeVMObject::try_create_with_inode(inode));
  411. auto old_name = old_region->take_name();
  412. bool old_region_was_mmapped_from_readable = old_region->mmapped_from_readable();
  413. bool old_region_was_mmapped_from_writable = old_region->mmapped_from_writable();
  414. old_region->unmap();
  415. space->deallocate_region(*old_region);
  416. auto* new_region = TRY(space->allocate_region_with_vmobject(range, move(new_vmobject), old_offset, old_name->view(), old_prot, false));
  417. new_region->set_mmap(true, old_region_was_mmapped_from_readable, old_region_was_mmapped_from_writable);
  418. return new_region->vaddr().get();
  419. }
  420. dbgln("sys$mremap: Unimplemented remap request (flags={})", params.flags);
  421. return ENOTIMPL;
  422. });
  423. }
  424. ErrorOr<FlatPtr> Process::sys$allocate_tls(Userspace<char const*> initial_data, size_t size)
  425. {
  426. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  427. TRY(require_promise(Pledge::stdio));
  428. if (!size || size % PAGE_SIZE != 0)
  429. return EINVAL;
  430. if (!m_master_tls_region.is_null())
  431. return EEXIST;
  432. if (thread_count() != 1)
  433. return EFAULT;
  434. Thread* main_thread = nullptr;
  435. bool multiple_threads = false;
  436. for_each_thread([&main_thread, &multiple_threads](auto& thread) {
  437. if (main_thread)
  438. multiple_threads = true;
  439. main_thread = &thread;
  440. return IterationDecision::Break;
  441. });
  442. VERIFY(main_thread);
  443. if (multiple_threads)
  444. return EINVAL;
  445. return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
  446. auto* region = TRY(space->allocate_region(Memory::RandomizeVirtualAddress::Yes, {}, size, PAGE_SIZE, "Master TLS"sv, PROT_READ | PROT_WRITE));
  447. m_master_tls_region = TRY(region->try_make_weak_ptr());
  448. m_master_tls_size = size;
  449. m_master_tls_alignment = PAGE_SIZE;
  450. {
  451. Kernel::SmapDisabler disabler;
  452. void* fault_at;
  453. if (!Kernel::safe_memcpy((char*)m_master_tls_region.unsafe_ptr()->vaddr().as_ptr(), (char*)initial_data.ptr(), size, fault_at))
  454. return EFAULT;
  455. }
  456. TRY(main_thread->make_thread_specific_region({}));
  457. Processor::set_thread_specific_data(main_thread->thread_specific_data());
  458. return m_master_tls_region.unsafe_ptr()->vaddr().get();
  459. });
  460. }
  461. ErrorOr<FlatPtr> Process::sys$annotate_mapping(Userspace<void*> address, int flags)
  462. {
  463. VERIFY_NO_PROCESS_BIG_LOCK(this);
  464. if (flags == to_underlying(VirtualMemoryRangeFlags::None))
  465. return EINVAL;
  466. if (!address)
  467. return EINVAL;
  468. if (!Memory::is_user_address(address.vaddr()))
  469. return EFAULT;
  470. return address_space().with([&](auto& space) -> ErrorOr<FlatPtr> {
  471. if (space->enforces_syscall_regions() && (flags & to_underlying(VirtualMemoryRangeFlags::SyscallCode)))
  472. return EPERM;
  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. }