Process.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Demangle.h>
  27. #include <AK/RefPtr.h>
  28. #include <AK/ScopeGuard.h>
  29. #include <AK/ScopedValueRollback.h>
  30. #include <AK/StdLibExtras.h>
  31. #include <AK/StringBuilder.h>
  32. #include <AK/Time.h>
  33. #include <AK/Types.h>
  34. #include <Kernel/ACPI/Parser.h>
  35. #include <Kernel/API/Syscall.h>
  36. #include <Kernel/Arch/i386/CPU.h>
  37. #include <Kernel/Console.h>
  38. #include <Kernel/Devices/BlockDevice.h>
  39. #include <Kernel/Devices/KeyboardDevice.h>
  40. #include <Kernel/Devices/NullDevice.h>
  41. #include <Kernel/FileSystem/Custody.h>
  42. #include <Kernel/FileSystem/DevPtsFS.h>
  43. #include <Kernel/FileSystem/Ext2FileSystem.h>
  44. #include <Kernel/FileSystem/FIFO.h>
  45. #include <Kernel/FileSystem/FileDescription.h>
  46. #include <Kernel/FileSystem/InodeWatcher.h>
  47. #include <Kernel/FileSystem/Plan9FileSystem.h>
  48. #include <Kernel/FileSystem/ProcFS.h>
  49. #include <Kernel/FileSystem/TmpFS.h>
  50. #include <Kernel/FileSystem/VirtualFileSystem.h>
  51. #include <Kernel/Heap/kmalloc.h>
  52. #include <Kernel/IO.h>
  53. #include <Kernel/KBufferBuilder.h>
  54. #include <Kernel/KSyms.h>
  55. #include <Kernel/Module.h>
  56. #include <Kernel/Multiboot.h>
  57. #include <Kernel/PerformanceEventBuffer.h>
  58. #include <Kernel/Process.h>
  59. #include <Kernel/RTC.h>
  60. #include <Kernel/Scheduler.h>
  61. #include <Kernel/SharedBuffer.h>
  62. #include <Kernel/StdLib.h>
  63. #include <Kernel/TTY/MasterPTY.h>
  64. #include <Kernel/TTY/TTY.h>
  65. #include <Kernel/Thread.h>
  66. #include <Kernel/ThreadTracer.h>
  67. #include <Kernel/Time/TimeManagement.h>
  68. #include <Kernel/VM/PageDirectory.h>
  69. #include <Kernel/VM/PrivateInodeVMObject.h>
  70. #include <Kernel/VM/ProcessPagingScope.h>
  71. #include <Kernel/VM/PurgeableVMObject.h>
  72. #include <Kernel/VM/SharedInodeVMObject.h>
  73. #include <LibC/errno_numbers.h>
  74. #include <LibC/limits.h>
  75. #include <LibC/signal_numbers.h>
  76. #include <LibELF/Loader.h>
  77. #include <LibELF/Validation.h>
  78. #include <LibKeyboard/CharacterMapData.h>
  79. //#define PROCESS_DEBUG
  80. //#define DEBUG_POLL_SELECT
  81. //#define DEBUG_IO
  82. //#define SIGNAL_DEBUG
  83. namespace Kernel {
  84. static void create_signal_trampolines();
  85. RecursiveSpinLock g_processes_lock;
  86. static Atomic<pid_t> next_pid;
  87. InlineLinkedList<Process>* g_processes;
  88. String* g_hostname;
  89. Lock* g_hostname_lock;
  90. VirtualAddress g_return_to_ring3_from_signal_trampoline;
  91. HashMap<String, OwnPtr<Module>>* g_modules;
  92. pid_t Process::allocate_pid()
  93. {
  94. return next_pid.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel);
  95. }
  96. void Process::initialize()
  97. {
  98. g_modules = new HashMap<String, OwnPtr<Module>>;
  99. next_pid.store(0, AK::MemoryOrder::memory_order_release);
  100. g_processes = new InlineLinkedList<Process>;
  101. g_hostname = new String("courage");
  102. g_hostname_lock = new Lock;
  103. create_signal_trampolines();
  104. }
  105. Vector<pid_t> Process::all_pids()
  106. {
  107. Vector<pid_t> pids;
  108. ScopedSpinLock lock(g_processes_lock);
  109. pids.ensure_capacity((int)g_processes->size_slow());
  110. for (auto& process : *g_processes)
  111. pids.append(process.pid());
  112. return pids;
  113. }
  114. NonnullRefPtrVector<Process> Process::all_processes()
  115. {
  116. NonnullRefPtrVector<Process> processes;
  117. ScopedSpinLock lock(g_processes_lock);
  118. processes.ensure_capacity((int)g_processes->size_slow());
  119. for (auto& process : *g_processes)
  120. processes.append(NonnullRefPtr<Process>(process));
  121. return processes;
  122. }
  123. bool Process::in_group(gid_t gid) const
  124. {
  125. return m_gid == gid || m_extra_gids.contains(gid);
  126. }
  127. Range Process::allocate_range(VirtualAddress vaddr, size_t size, size_t alignment)
  128. {
  129. vaddr.mask(PAGE_MASK);
  130. size = PAGE_ROUND_UP(size);
  131. if (vaddr.is_null())
  132. return page_directory().range_allocator().allocate_anywhere(size, alignment);
  133. return page_directory().range_allocator().allocate_specific(vaddr, size);
  134. }
  135. Region& Process::allocate_split_region(const Region& source_region, const Range& range, size_t offset_in_vmobject)
  136. {
  137. auto& region = add_region(Region::create_user_accessible(range, source_region.vmobject(), offset_in_vmobject, source_region.name(), source_region.access()));
  138. region.set_mmap(source_region.is_mmap());
  139. region.set_stack(source_region.is_stack());
  140. size_t page_offset_in_source_region = (offset_in_vmobject - source_region.offset_in_vmobject()) / PAGE_SIZE;
  141. for (size_t i = 0; i < region.page_count(); ++i) {
  142. if (source_region.should_cow(page_offset_in_source_region + i))
  143. region.set_should_cow(i, true);
  144. }
  145. return region;
  146. }
  147. Region* Process::allocate_region(const Range& range, const String& name, int prot, bool should_commit)
  148. {
  149. ASSERT(range.is_valid());
  150. auto vmobject = AnonymousVMObject::create_with_size(range.size());
  151. auto region = Region::create_user_accessible(range, vmobject, 0, name, prot_to_region_access_flags(prot));
  152. region->map(page_directory());
  153. if (should_commit && !region->commit())
  154. return nullptr;
  155. return &add_region(move(region));
  156. }
  157. Region* Process::allocate_region(VirtualAddress vaddr, size_t size, const String& name, int prot, bool should_commit)
  158. {
  159. auto range = allocate_range(vaddr, size);
  160. if (!range.is_valid())
  161. return nullptr;
  162. return allocate_region(range, name, prot, should_commit);
  163. }
  164. Region* Process::allocate_region_with_vmobject(const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, int prot)
  165. {
  166. ASSERT(range.is_valid());
  167. size_t end_in_vmobject = offset_in_vmobject + range.size();
  168. if (end_in_vmobject <= offset_in_vmobject) {
  169. dbg() << "allocate_region_with_vmobject: Overflow (offset + size)";
  170. return nullptr;
  171. }
  172. if (offset_in_vmobject >= vmobject->size()) {
  173. dbg() << "allocate_region_with_vmobject: Attempt to allocate a region with an offset past the end of its VMObject.";
  174. return nullptr;
  175. }
  176. if (end_in_vmobject > vmobject->size()) {
  177. dbg() << "allocate_region_with_vmobject: Attempt to allocate a region with an end past the end of its VMObject.";
  178. return nullptr;
  179. }
  180. offset_in_vmobject &= PAGE_MASK;
  181. auto& region = add_region(Region::create_user_accessible(range, move(vmobject), offset_in_vmobject, name, prot_to_region_access_flags(prot)));
  182. region.map(page_directory());
  183. return &region;
  184. }
  185. Region* Process::allocate_region_with_vmobject(VirtualAddress vaddr, size_t size, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, int prot)
  186. {
  187. auto range = allocate_range(vaddr, size);
  188. if (!range.is_valid())
  189. return nullptr;
  190. return allocate_region_with_vmobject(range, move(vmobject), offset_in_vmobject, name, prot);
  191. }
  192. bool Process::deallocate_region(Region& region)
  193. {
  194. OwnPtr<Region> region_protector;
  195. ScopedSpinLock lock(m_lock);
  196. if (m_region_lookup_cache.region == &region)
  197. m_region_lookup_cache.region = nullptr;
  198. for (size_t i = 0; i < m_regions.size(); ++i) {
  199. if (&m_regions[i] == &region) {
  200. region_protector = m_regions.unstable_take(i);
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. Region* Process::find_region_from_range(const Range& range)
  207. {
  208. ScopedSpinLock lock(m_lock);
  209. if (m_region_lookup_cache.range == range && m_region_lookup_cache.region)
  210. return m_region_lookup_cache.region;
  211. size_t size = PAGE_ROUND_UP(range.size());
  212. for (auto& region : m_regions) {
  213. if (region.vaddr() == range.base() && region.size() == size) {
  214. m_region_lookup_cache.range = range;
  215. m_region_lookup_cache.region = region.make_weak_ptr();
  216. return &region;
  217. }
  218. }
  219. return nullptr;
  220. }
  221. Region* Process::find_region_containing(const Range& range)
  222. {
  223. ScopedSpinLock lock(m_lock);
  224. for (auto& region : m_regions) {
  225. if (region.contains(range))
  226. return &region;
  227. }
  228. return nullptr;
  229. }
  230. void Process::kill_threads_except_self()
  231. {
  232. InterruptDisabler disabler;
  233. if (thread_count() <= 1)
  234. return;
  235. auto current_thread = Thread::current();
  236. for_each_thread([&](Thread& thread) {
  237. if (&thread == current_thread
  238. || thread.state() == Thread::State::Dead
  239. || thread.state() == Thread::State::Dying)
  240. return IterationDecision::Continue;
  241. // At this point, we have no joiner anymore
  242. thread.m_joiner = nullptr;
  243. thread.set_should_die();
  244. if (thread.state() != Thread::State::Dead)
  245. thread.set_state(Thread::State::Dying);
  246. return IterationDecision::Continue;
  247. });
  248. big_lock().clear_waiters();
  249. }
  250. void Process::kill_all_threads()
  251. {
  252. for_each_thread([&](Thread& thread) {
  253. thread.set_should_die();
  254. return IterationDecision::Continue;
  255. });
  256. }
  257. RefPtr<Process> Process::create_user_process(Thread*& first_thread, const String& path, uid_t uid, gid_t gid, pid_t parent_pid, int& error, Vector<String>&& arguments, Vector<String>&& environment, TTY* tty)
  258. {
  259. auto parts = path.split('/');
  260. if (arguments.is_empty()) {
  261. arguments.append(parts.last());
  262. }
  263. RefPtr<Custody> cwd;
  264. RefPtr<Custody> root;
  265. {
  266. ScopedSpinLock lock(g_processes_lock);
  267. if (auto parent = Process::from_pid(parent_pid)) {
  268. cwd = parent->m_cwd;
  269. root = parent->m_root_directory;
  270. }
  271. }
  272. if (!cwd)
  273. cwd = VFS::the().root_custody();
  274. if (!root)
  275. root = VFS::the().root_custody();
  276. auto process = adopt(*new Process(first_thread, parts.take_last(), uid, gid, parent_pid, Ring3, move(cwd), nullptr, tty));
  277. process->m_fds.resize(m_max_open_file_descriptors);
  278. auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : NullDevice::the();
  279. auto description = device_to_use_as_tty.open(O_RDWR).value();
  280. process->m_fds[0].set(*description);
  281. process->m_fds[1].set(*description);
  282. process->m_fds[2].set(*description);
  283. error = process->exec(path, move(arguments), move(environment));
  284. if (error != 0) {
  285. dbg() << "Failed to exec " << path << ": " << error;
  286. delete first_thread;
  287. return {};
  288. }
  289. {
  290. ScopedSpinLock lock(g_processes_lock);
  291. g_processes->prepend(process);
  292. process->ref();
  293. }
  294. error = 0;
  295. return process;
  296. }
  297. NonnullRefPtr<Process> Process::create_kernel_process(Thread*& first_thread, String&& name, void (*e)(), u32 affinity)
  298. {
  299. auto process = adopt(*new Process(first_thread, move(name), (uid_t)0, (gid_t)0, (pid_t)0, Ring0));
  300. first_thread->tss().eip = (FlatPtr)e;
  301. if (process->pid() != 0) {
  302. ScopedSpinLock lock(g_processes_lock);
  303. g_processes->prepend(process);
  304. process->ref();
  305. }
  306. first_thread->set_affinity(affinity);
  307. first_thread->set_state(Thread::State::Runnable);
  308. return process;
  309. }
  310. Process::Process(Thread*& first_thread, const String& name, uid_t uid, gid_t gid, pid_t ppid, RingLevel ring, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, Process* fork_parent)
  311. : m_name(move(name))
  312. , m_pid(allocate_pid())
  313. , m_euid(uid)
  314. , m_egid(gid)
  315. , m_uid(uid)
  316. , m_gid(gid)
  317. , m_suid(uid)
  318. , m_sgid(gid)
  319. , m_ring(ring)
  320. , m_executable(move(executable))
  321. , m_cwd(move(cwd))
  322. , m_tty(tty)
  323. , m_ppid(ppid)
  324. {
  325. #ifdef PROCESS_DEBUG
  326. dbg() << "Created new process " << m_name << "(" << m_pid << ")";
  327. #endif
  328. m_page_directory = PageDirectory::create_for_userspace(*this, fork_parent ? &fork_parent->page_directory().range_allocator() : nullptr);
  329. #ifdef MM_DEBUG
  330. dbg() << "Process " << pid() << " ctor: PD=" << m_page_directory.ptr() << " created";
  331. #endif
  332. if (fork_parent) {
  333. // NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the only thread in the new process.
  334. first_thread = Thread::current()->clone(*this);
  335. } else {
  336. // NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
  337. first_thread = new Thread(*this);
  338. }
  339. }
  340. Process::~Process()
  341. {
  342. ASSERT(thread_count() == 0);
  343. }
  344. void Process::dump_regions()
  345. {
  346. klog() << "Process regions:";
  347. klog() << "BEGIN END SIZE ACCESS NAME";
  348. for (auto& region : m_regions) {
  349. klog() << String::format("%08x", region.vaddr().get()) << " -- " << String::format("%08x", region.vaddr().offset(region.size() - 1).get()) << " " << String::format("%08x", region.size()) << " " << (region.is_readable() ? 'R' : ' ') << (region.is_writable() ? 'W' : ' ') << (region.is_executable() ? 'X' : ' ') << (region.is_shared() ? 'S' : ' ') << (region.is_stack() ? 'T' : ' ') << (region.vmobject().is_purgeable() ? 'P' : ' ') << " " << region.name().characters();
  350. }
  351. MM.dump_kernel_regions();
  352. }
  353. void signal_trampoline_dummy(void)
  354. {
  355. // The trampoline preserves the current eax, pushes the signal code and
  356. // then calls the signal handler. We do this because, when interrupting a
  357. // blocking syscall, that syscall may return some special error code in eax;
  358. // This error code would likely be overwritten by the signal handler, so it's
  359. // neccessary to preserve it here.
  360. asm(
  361. ".intel_syntax noprefix\n"
  362. "asm_signal_trampoline:\n"
  363. "push ebp\n"
  364. "mov ebp, esp\n"
  365. "push eax\n" // we have to store eax 'cause it might be the return value from a syscall
  366. "sub esp, 4\n" // align the stack to 16 bytes
  367. "mov eax, [ebp+12]\n" // push the signal code
  368. "push eax\n"
  369. "call [ebp+8]\n" // call the signal handler
  370. "add esp, 8\n"
  371. "mov eax, %P0\n"
  372. "int 0x82\n" // sigreturn syscall
  373. "asm_signal_trampoline_end:\n"
  374. ".att_syntax" ::"i"(Syscall::SC_sigreturn));
  375. }
  376. extern "C" void asm_signal_trampoline(void);
  377. extern "C" void asm_signal_trampoline_end(void);
  378. void create_signal_trampolines()
  379. {
  380. InterruptDisabler disabler;
  381. // NOTE: We leak this region.
  382. auto* trampoline_region = MM.allocate_user_accessible_kernel_region(PAGE_SIZE, "Signal trampolines", Region::Access::Read | Region::Access::Write | Region::Access::Execute, false).leak_ptr();
  383. g_return_to_ring3_from_signal_trampoline = trampoline_region->vaddr();
  384. u8* trampoline = (u8*)asm_signal_trampoline;
  385. u8* trampoline_end = (u8*)asm_signal_trampoline_end;
  386. size_t trampoline_size = trampoline_end - trampoline;
  387. {
  388. SmapDisabler disabler;
  389. u8* code_ptr = (u8*)trampoline_region->vaddr().as_ptr();
  390. memcpy(code_ptr, trampoline, trampoline_size);
  391. }
  392. trampoline_region->set_writable(false);
  393. trampoline_region->remap();
  394. }
  395. void Process::crash(int signal, u32 eip, bool out_of_memory)
  396. {
  397. ASSERT_INTERRUPTS_DISABLED();
  398. ASSERT(!is_dead());
  399. ASSERT(Process::current() == this);
  400. if (out_of_memory) {
  401. dbg() << "\033[31;1mOut of memory\033[m, killing: " << *this;
  402. } else {
  403. if (eip >= 0xc0000000 && g_kernel_symbols_available) {
  404. auto* symbol = symbolicate_kernel_address(eip);
  405. dbg() << "\033[31;1m" << String::format("%p", eip) << " " << (symbol ? demangle(symbol->name) : "(k?)") << " +" << (symbol ? eip - symbol->address : 0) << "\033[0m\n";
  406. } else if (auto elf_bundle = this->elf_bundle()) {
  407. dbg() << "\033[31;1m" << String::format("%p", eip) << " " << elf_bundle->elf_loader->symbolicate(eip) << "\033[0m\n";
  408. } else {
  409. dbg() << "\033[31;1m" << String::format("%p", eip) << " (?)\033[0m\n";
  410. }
  411. dump_backtrace();
  412. }
  413. m_termination_signal = signal;
  414. dump_regions();
  415. ASSERT(is_ring3());
  416. die();
  417. // We can not return from here, as there is nowhere
  418. // to unwind to, so die right away.
  419. Thread::current()->die_if_needed();
  420. ASSERT_NOT_REACHED();
  421. }
  422. RefPtr<Process> Process::from_pid(pid_t pid)
  423. {
  424. ScopedSpinLock lock(g_processes_lock);
  425. for (auto& process : *g_processes) {
  426. if (process.pid() == pid)
  427. return &process;
  428. }
  429. return {};
  430. }
  431. RefPtr<FileDescription> Process::file_description(int fd) const
  432. {
  433. if (fd < 0)
  434. return nullptr;
  435. if (static_cast<size_t>(fd) < m_fds.size())
  436. return m_fds[fd].description();
  437. return nullptr;
  438. }
  439. int Process::fd_flags(int fd) const
  440. {
  441. if (fd < 0)
  442. return -1;
  443. if (static_cast<size_t>(fd) < m_fds.size())
  444. return m_fds[fd].flags();
  445. return -1;
  446. }
  447. String Process::validate_and_copy_string_from_user(const char* user_characters, size_t user_length) const
  448. {
  449. if (user_length == 0)
  450. return String::empty();
  451. if (!user_characters)
  452. return {};
  453. if (!validate_read(user_characters, user_length))
  454. return {};
  455. SmapDisabler disabler;
  456. size_t measured_length = strnlen(user_characters, user_length);
  457. return String(user_characters, measured_length);
  458. }
  459. String Process::validate_and_copy_string_from_user(const Syscall::StringArgument& string) const
  460. {
  461. return validate_and_copy_string_from_user(string.characters, string.length);
  462. }
  463. int Process::number_of_open_file_descriptors() const
  464. {
  465. int count = 0;
  466. for (auto& description : m_fds) {
  467. if (description)
  468. ++count;
  469. }
  470. return count;
  471. }
  472. int Process::alloc_fd(int first_candidate_fd)
  473. {
  474. for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) {
  475. if (!m_fds[i])
  476. return i;
  477. }
  478. return -EMFILE;
  479. }
  480. timeval kgettimeofday()
  481. {
  482. return g_timeofday;
  483. }
  484. void kgettimeofday(timeval& tv)
  485. {
  486. tv = kgettimeofday();
  487. }
  488. siginfo_t Process::reap(Process& process)
  489. {
  490. siginfo_t siginfo;
  491. memset(&siginfo, 0, sizeof(siginfo));
  492. siginfo.si_signo = SIGCHLD;
  493. siginfo.si_pid = process.pid();
  494. siginfo.si_uid = process.uid();
  495. if (process.m_termination_signal) {
  496. siginfo.si_status = process.m_termination_signal;
  497. siginfo.si_code = CLD_KILLED;
  498. } else {
  499. siginfo.si_status = process.m_termination_status;
  500. siginfo.si_code = CLD_EXITED;
  501. }
  502. ASSERT(g_processes_lock.is_locked());
  503. if (process.ppid()) {
  504. auto parent = Process::from_pid(process.ppid());
  505. if (parent) {
  506. parent->m_ticks_in_user_for_dead_children += process.m_ticks_in_user + process.m_ticks_in_user_for_dead_children;
  507. parent->m_ticks_in_kernel_for_dead_children += process.m_ticks_in_kernel + process.m_ticks_in_kernel_for_dead_children;
  508. }
  509. }
  510. #ifdef PROCESS_DEBUG
  511. dbg() << "Reaping process " << process;
  512. #endif
  513. ASSERT(process.is_dead());
  514. g_processes->remove(&process);
  515. process.unref();
  516. return siginfo;
  517. }
  518. bool Process::validate_read_from_kernel(VirtualAddress vaddr, size_t size) const
  519. {
  520. if (vaddr.is_null())
  521. return false;
  522. return MM.validate_kernel_read(*this, vaddr, size);
  523. }
  524. bool Process::validate_read(const void* address, size_t size) const
  525. {
  526. if (!size)
  527. return false;
  528. return MM.validate_user_read(*this, VirtualAddress(address), size);
  529. }
  530. bool Process::validate_write(void* address, size_t size) const
  531. {
  532. if (!size)
  533. return false;
  534. return MM.validate_user_write(*this, VirtualAddress(address), size);
  535. }
  536. Custody& Process::current_directory()
  537. {
  538. if (!m_cwd)
  539. m_cwd = VFS::the().root_custody();
  540. return *m_cwd;
  541. }
  542. KResultOr<String> Process::get_syscall_path_argument(const char* user_path, size_t path_length) const
  543. {
  544. if (path_length == 0)
  545. return KResult(-EINVAL);
  546. if (path_length > PATH_MAX)
  547. return KResult(-ENAMETOOLONG);
  548. if (!validate_read(user_path, path_length))
  549. return KResult(-EFAULT);
  550. return copy_string_from_user(user_path, path_length);
  551. }
  552. KResultOr<String> Process::get_syscall_path_argument(const Syscall::StringArgument& path) const
  553. {
  554. return get_syscall_path_argument(path.characters, path.length);
  555. }
  556. void Process::finalize()
  557. {
  558. ASSERT(Thread::current() == g_finalizer);
  559. #ifdef PROCESS_DEBUG
  560. dbg() << "Finalizing process " << *this;
  561. #endif
  562. if (m_perf_event_buffer) {
  563. auto description_or_error = VFS::the().open(String::format("perfcore.%d", m_pid), O_CREAT | O_EXCL, 0400, current_directory(), UidAndGid { m_uid, m_gid });
  564. if (!description_or_error.is_error()) {
  565. auto& description = description_or_error.value();
  566. auto json = m_perf_event_buffer->to_json(m_pid, m_executable ? m_executable->absolute_path() : "");
  567. // FIXME: Should this error path be surfaced somehow?
  568. (void)description->write(json.data(), json.size());
  569. }
  570. }
  571. m_fds.clear();
  572. m_tty = nullptr;
  573. m_executable = nullptr;
  574. m_cwd = nullptr;
  575. m_root_directory = nullptr;
  576. m_root_directory_relative_to_global_root = nullptr;
  577. disown_all_shared_buffers();
  578. {
  579. InterruptDisabler disabler;
  580. if (auto* parent_thread = Thread::from_tid(m_ppid)) {
  581. if (parent_thread->m_signal_action_data[SIGCHLD].flags & SA_NOCLDWAIT) {
  582. // NOTE: If the parent doesn't care about this process, let it go.
  583. m_ppid = 0;
  584. } else {
  585. parent_thread->send_signal(SIGCHLD, this);
  586. }
  587. }
  588. }
  589. {
  590. ScopedSpinLock lock(m_lock);
  591. m_regions.clear();
  592. }
  593. m_dead = true;
  594. }
  595. void Process::die()
  596. {
  597. // Let go of the TTY, otherwise a slave PTY may keep the master PTY from
  598. // getting an EOF when the last process using the slave PTY dies.
  599. // If the master PTY owner relies on an EOF to know when to wait() on a
  600. // slave owner, we have to allow the PTY pair to be torn down.
  601. m_tty = nullptr;
  602. kill_all_threads();
  603. }
  604. size_t Process::amount_dirty_private() const
  605. {
  606. // FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
  607. // The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
  608. // That's probably a situation that needs to be looked at in general.
  609. size_t amount = 0;
  610. ScopedSpinLock lock(m_lock);
  611. for (auto& region : m_regions) {
  612. if (!region.is_shared())
  613. amount += region.amount_dirty();
  614. }
  615. return amount;
  616. }
  617. size_t Process::amount_clean_inode() const
  618. {
  619. HashTable<const InodeVMObject*> vmobjects;
  620. {
  621. ScopedSpinLock lock(m_lock);
  622. for (auto& region : m_regions) {
  623. if (region.vmobject().is_inode())
  624. vmobjects.set(&static_cast<const InodeVMObject&>(region.vmobject()));
  625. }
  626. }
  627. size_t amount = 0;
  628. for (auto& vmobject : vmobjects)
  629. amount += vmobject->amount_clean();
  630. return amount;
  631. }
  632. size_t Process::amount_virtual() const
  633. {
  634. size_t amount = 0;
  635. ScopedSpinLock lock(m_lock);
  636. for (auto& region : m_regions) {
  637. amount += region.size();
  638. }
  639. return amount;
  640. }
  641. size_t Process::amount_resident() const
  642. {
  643. // FIXME: This will double count if multiple regions use the same physical page.
  644. size_t amount = 0;
  645. ScopedSpinLock lock(m_lock);
  646. for (auto& region : m_regions) {
  647. amount += region.amount_resident();
  648. }
  649. return amount;
  650. }
  651. size_t Process::amount_shared() const
  652. {
  653. // FIXME: This will double count if multiple regions use the same physical page.
  654. // FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
  655. // and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
  656. // so that every Region contributes +1 ref to each of its PhysicalPages.
  657. size_t amount = 0;
  658. ScopedSpinLock lock(m_lock);
  659. for (auto& region : m_regions) {
  660. amount += region.amount_shared();
  661. }
  662. return amount;
  663. }
  664. size_t Process::amount_purgeable_volatile() const
  665. {
  666. size_t amount = 0;
  667. ScopedSpinLock lock(m_lock);
  668. for (auto& region : m_regions) {
  669. if (region.vmobject().is_purgeable() && static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
  670. amount += region.amount_resident();
  671. }
  672. return amount;
  673. }
  674. size_t Process::amount_purgeable_nonvolatile() const
  675. {
  676. size_t amount = 0;
  677. ScopedSpinLock lock(m_lock);
  678. for (auto& region : m_regions) {
  679. if (region.vmobject().is_purgeable() && !static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
  680. amount += region.amount_resident();
  681. }
  682. return amount;
  683. }
  684. void Process::terminate_due_to_signal(u8 signal)
  685. {
  686. ASSERT_INTERRUPTS_DISABLED();
  687. ASSERT(signal < 32);
  688. dbg() << "Terminating " << *this << " due to signal " << signal;
  689. m_termination_status = 0;
  690. m_termination_signal = signal;
  691. die();
  692. }
  693. KResult Process::send_signal(u8 signal, Process* sender)
  694. {
  695. InterruptDisabler disabler;
  696. if (auto* thread = Thread::from_tid(m_pid)) {
  697. thread->send_signal(signal, sender);
  698. return KSuccess;
  699. }
  700. return KResult(-ESRCH);
  701. }
  702. Thread* Process::create_kernel_thread(void (*entry)(), u32 priority, const String& name, u32 affinity, bool joinable)
  703. {
  704. ASSERT((priority >= THREAD_PRIORITY_MIN) && (priority <= THREAD_PRIORITY_MAX));
  705. // FIXME: Do something with guard pages?
  706. auto* thread = new Thread(*this);
  707. thread->set_name(name);
  708. thread->set_affinity(affinity);
  709. thread->set_priority(priority);
  710. thread->set_joinable(joinable);
  711. auto& tss = thread->tss();
  712. tss.eip = (FlatPtr)entry;
  713. thread->set_state(Thread::State::Runnable);
  714. return thread;
  715. }
  716. void Process::FileDescriptionAndFlags::clear()
  717. {
  718. m_description = nullptr;
  719. m_flags = 0;
  720. }
  721. void Process::FileDescriptionAndFlags::set(NonnullRefPtr<FileDescription>&& description, u32 flags)
  722. {
  723. m_description = move(description);
  724. m_flags = flags;
  725. }
  726. KBuffer Process::backtrace() const
  727. {
  728. KBufferBuilder builder;
  729. for_each_thread([&](Thread& thread) {
  730. builder.appendf("Thread %d (%s):\n", thread.tid(), thread.name().characters());
  731. builder.append(thread.backtrace());
  732. return IterationDecision::Continue;
  733. });
  734. return builder.build();
  735. }
  736. Custody& Process::root_directory()
  737. {
  738. if (!m_root_directory)
  739. m_root_directory = VFS::the().root_custody();
  740. return *m_root_directory;
  741. }
  742. Custody& Process::root_directory_relative_to_global_root()
  743. {
  744. if (!m_root_directory_relative_to_global_root)
  745. m_root_directory_relative_to_global_root = root_directory();
  746. return *m_root_directory_relative_to_global_root;
  747. }
  748. void Process::set_root_directory(const Custody& root)
  749. {
  750. m_root_directory = root;
  751. }
  752. Region& Process::add_region(NonnullOwnPtr<Region> region)
  753. {
  754. auto* ptr = region.ptr();
  755. ScopedSpinLock lock(m_lock);
  756. m_regions.append(move(region));
  757. return *ptr;
  758. }
  759. void Process::set_tty(TTY* tty)
  760. {
  761. m_tty = tty;
  762. }
  763. OwnPtr<Process::ELFBundle> Process::elf_bundle() const
  764. {
  765. if (!m_executable)
  766. return nullptr;
  767. auto bundle = make<ELFBundle>();
  768. if (!m_executable->inode().shared_vmobject()) {
  769. return nullptr;
  770. }
  771. ASSERT(m_executable->inode().shared_vmobject());
  772. auto& vmobject = *m_executable->inode().shared_vmobject();
  773. bundle->region = MM.allocate_kernel_region_with_vmobject(const_cast<SharedInodeVMObject&>(vmobject), vmobject.size(), "ELF bundle", Region::Access::Read);
  774. if (!bundle->region)
  775. return nullptr;
  776. bundle->elf_loader = ELF::Loader::create(bundle->region->vaddr().as_ptr(), bundle->region->size());
  777. return bundle;
  778. }
  779. }