Process.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * Copyright (c) 2018-2021, 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/Debug.h>
  27. #include <AK/Demangle.h>
  28. #include <AK/QuickSort.h>
  29. #include <AK/StdLibExtras.h>
  30. #include <AK/StringBuilder.h>
  31. #include <AK/Time.h>
  32. #include <AK/Types.h>
  33. #include <Kernel/API/Syscall.h>
  34. #include <Kernel/Arch/i386/CPU.h>
  35. #include <Kernel/CoreDump.h>
  36. #include <Kernel/Devices/NullDevice.h>
  37. #include <Kernel/FileSystem/Custody.h>
  38. #include <Kernel/FileSystem/FileDescription.h>
  39. #include <Kernel/FileSystem/VirtualFileSystem.h>
  40. #include <Kernel/Heap/kmalloc.h>
  41. #include <Kernel/KBufferBuilder.h>
  42. #include <Kernel/KSyms.h>
  43. #include <Kernel/Module.h>
  44. #include <Kernel/PerformanceEventBuffer.h>
  45. #include <Kernel/Process.h>
  46. #include <Kernel/RTC.h>
  47. #include <Kernel/SharedBuffer.h>
  48. #include <Kernel/StdLib.h>
  49. #include <Kernel/TTY/TTY.h>
  50. #include <Kernel/Thread.h>
  51. #include <Kernel/VM/PageDirectory.h>
  52. #include <Kernel/VM/PrivateInodeVMObject.h>
  53. #include <Kernel/VM/ProcessPagingScope.h>
  54. #include <Kernel/VM/SharedInodeVMObject.h>
  55. #include <LibC/errno_numbers.h>
  56. #include <LibC/limits.h>
  57. //#define DEBUG_IO
  58. //#define DEBUG_POLL_SELECT
  59. //#define PROCESS_DEBUG
  60. //#define SIGNAL_DEBUG
  61. namespace Kernel {
  62. static void create_signal_trampolines();
  63. RecursiveSpinLock g_processes_lock;
  64. static Atomic<pid_t> next_pid;
  65. InlineLinkedList<Process>* g_processes;
  66. String* g_hostname;
  67. Lock* g_hostname_lock;
  68. VirtualAddress g_return_to_ring3_from_signal_trampoline;
  69. HashMap<String, OwnPtr<Module>>* g_modules;
  70. ProcessID Process::allocate_pid()
  71. {
  72. // Overflow is UB, and negative PIDs wreck havoc.
  73. // TODO: Handle PID overflow
  74. // For example: Use an Atomic<u32>, mask the most significant bit,
  75. // retry if PID is already taken as a PID, taken as a TID,
  76. // takes as a PGID, taken as a SID, or zero.
  77. return next_pid.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel);
  78. }
  79. void Process::initialize()
  80. {
  81. g_modules = new HashMap<String, OwnPtr<Module>>;
  82. next_pid.store(0, AK::MemoryOrder::memory_order_release);
  83. g_processes = new InlineLinkedList<Process>;
  84. g_process_groups = new InlineLinkedList<ProcessGroup>;
  85. g_hostname = new String("courage");
  86. g_hostname_lock = new Lock;
  87. create_signal_trampolines();
  88. }
  89. Vector<ProcessID> Process::all_pids()
  90. {
  91. Vector<ProcessID> pids;
  92. ScopedSpinLock lock(g_processes_lock);
  93. pids.ensure_capacity((int)g_processes->size_slow());
  94. for (auto& process : *g_processes)
  95. pids.append(process.pid());
  96. return pids;
  97. }
  98. NonnullRefPtrVector<Process> Process::all_processes()
  99. {
  100. NonnullRefPtrVector<Process> processes;
  101. ScopedSpinLock lock(g_processes_lock);
  102. processes.ensure_capacity((int)g_processes->size_slow());
  103. for (auto& process : *g_processes)
  104. processes.append(NonnullRefPtr<Process>(process));
  105. return processes;
  106. }
  107. bool Process::in_group(gid_t gid) const
  108. {
  109. return m_gid == gid || m_extra_gids.contains_slow(gid);
  110. }
  111. Range Process::allocate_range(VirtualAddress vaddr, size_t size, size_t alignment)
  112. {
  113. vaddr.mask(PAGE_MASK);
  114. size = PAGE_ROUND_UP(size);
  115. if (vaddr.is_null())
  116. return page_directory().range_allocator().allocate_anywhere(size, alignment);
  117. return page_directory().range_allocator().allocate_specific(vaddr, size);
  118. }
  119. Region& Process::allocate_split_region(const Region& source_region, const Range& range, size_t offset_in_vmobject)
  120. {
  121. auto& region = add_region(Region::create_user_accessible(this, range, source_region.vmobject(), offset_in_vmobject, source_region.name(), source_region.access()));
  122. region.set_mmap(source_region.is_mmap());
  123. region.set_stack(source_region.is_stack());
  124. size_t page_offset_in_source_region = (offset_in_vmobject - source_region.offset_in_vmobject()) / PAGE_SIZE;
  125. for (size_t i = 0; i < region.page_count(); ++i) {
  126. if (source_region.should_cow(page_offset_in_source_region + i))
  127. region.set_should_cow(i, true);
  128. }
  129. return region;
  130. }
  131. KResultOr<Region*> Process::allocate_region(const Range& range, const String& name, int prot, AllocationStrategy strategy)
  132. {
  133. ASSERT(range.is_valid());
  134. auto vmobject = AnonymousVMObject::create_with_size(range.size(), strategy);
  135. if (!vmobject)
  136. return KResult(-ENOMEM);
  137. auto region = Region::create_user_accessible(this, range, vmobject.release_nonnull(), 0, name, prot_to_region_access_flags(prot));
  138. if (!region->map(page_directory()))
  139. return KResult(-ENOMEM);
  140. return &add_region(move(region));
  141. }
  142. KResultOr<Region*> Process::allocate_region(VirtualAddress vaddr, size_t size, const String& name, int prot, AllocationStrategy strategy)
  143. {
  144. auto range = allocate_range(vaddr, size);
  145. if (!range.is_valid())
  146. return KResult(-ENOMEM);
  147. return allocate_region(range, name, prot, strategy);
  148. }
  149. KResultOr<Region*> Process::allocate_region_with_vmobject(const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, int prot, bool shared)
  150. {
  151. ASSERT(range.is_valid());
  152. size_t end_in_vmobject = offset_in_vmobject + range.size();
  153. if (end_in_vmobject <= offset_in_vmobject) {
  154. dbgln("allocate_region_with_vmobject: Overflow (offset + size)");
  155. return KResult(-EINVAL);
  156. }
  157. if (offset_in_vmobject >= vmobject->size()) {
  158. dbgln("allocate_region_with_vmobject: Attempt to allocate a region with an offset past the end of its VMObject.");
  159. return KResult(-EINVAL);
  160. }
  161. if (end_in_vmobject > vmobject->size()) {
  162. dbgln("allocate_region_with_vmobject: Attempt to allocate a region with an end past the end of its VMObject.");
  163. return KResult(-EINVAL);
  164. }
  165. offset_in_vmobject &= PAGE_MASK;
  166. auto& region = add_region(Region::create_user_accessible(this, range, move(vmobject), offset_in_vmobject, name, prot_to_region_access_flags(prot), true, shared));
  167. if (!region.map(page_directory())) {
  168. // FIXME: What is an appropriate error code here, really?
  169. return KResult(-ENOMEM);
  170. }
  171. return &region;
  172. }
  173. KResultOr<Region*> Process::allocate_region_with_vmobject(VirtualAddress vaddr, size_t size, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, const String& name, int prot, bool shared)
  174. {
  175. auto range = allocate_range(vaddr, size);
  176. if (!range.is_valid())
  177. return KResult(-ENOMEM);
  178. return allocate_region_with_vmobject(range, move(vmobject), offset_in_vmobject, name, prot, shared);
  179. }
  180. bool Process::deallocate_region(Region& region)
  181. {
  182. OwnPtr<Region> region_protector;
  183. ScopedSpinLock lock(m_lock);
  184. if (m_region_lookup_cache.region.unsafe_ptr() == &region)
  185. m_region_lookup_cache.region = nullptr;
  186. for (size_t i = 0; i < m_regions.size(); ++i) {
  187. if (&m_regions[i] == &region) {
  188. region_protector = m_regions.unstable_take(i);
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. Region* Process::find_region_from_range(const Range& range)
  195. {
  196. ScopedSpinLock lock(m_lock);
  197. if (m_region_lookup_cache.range == range && m_region_lookup_cache.region)
  198. return m_region_lookup_cache.region.unsafe_ptr();
  199. size_t size = PAGE_ROUND_UP(range.size());
  200. for (auto& region : m_regions) {
  201. if (region.vaddr() == range.base() && region.size() == size) {
  202. m_region_lookup_cache.range = range;
  203. m_region_lookup_cache.region = region;
  204. return &region;
  205. }
  206. }
  207. return nullptr;
  208. }
  209. Region* Process::find_region_containing(const Range& range)
  210. {
  211. ScopedSpinLock lock(m_lock);
  212. for (auto& region : m_regions) {
  213. if (region.contains(range))
  214. return &region;
  215. }
  216. return nullptr;
  217. }
  218. void Process::kill_threads_except_self()
  219. {
  220. InterruptDisabler disabler;
  221. if (thread_count() <= 1)
  222. return;
  223. auto current_thread = Thread::current();
  224. for_each_thread([&](Thread& thread) {
  225. if (&thread == current_thread
  226. || thread.state() == Thread::State::Dead
  227. || thread.state() == Thread::State::Dying)
  228. return IterationDecision::Continue;
  229. // We need to detach this thread in case it hasn't been joined
  230. thread.detach();
  231. thread.set_should_die();
  232. return IterationDecision::Continue;
  233. });
  234. big_lock().clear_waiters();
  235. }
  236. void Process::kill_all_threads()
  237. {
  238. for_each_thread([&](Thread& thread) {
  239. // We need to detach this thread in case it hasn't been joined
  240. thread.detach();
  241. thread.set_should_die();
  242. return IterationDecision::Continue;
  243. });
  244. }
  245. RefPtr<Process> Process::create_user_process(RefPtr<Thread>& first_thread, const String& path, uid_t uid, gid_t gid, ProcessID parent_pid, int& error, Vector<String>&& arguments, Vector<String>&& environment, TTY* tty)
  246. {
  247. auto parts = path.split('/');
  248. if (arguments.is_empty()) {
  249. arguments.append(parts.last());
  250. }
  251. RefPtr<Custody> cwd;
  252. RefPtr<Custody> root;
  253. {
  254. ScopedSpinLock lock(g_processes_lock);
  255. if (auto parent = Process::from_pid(parent_pid)) {
  256. cwd = parent->m_cwd;
  257. root = parent->m_root_directory;
  258. }
  259. }
  260. if (!cwd)
  261. cwd = VFS::the().root_custody();
  262. if (!root)
  263. root = VFS::the().root_custody();
  264. auto process = adopt(*new Process(first_thread, parts.take_last(), uid, gid, parent_pid, false, move(cwd), nullptr, tty));
  265. if (!first_thread)
  266. return {};
  267. process->m_fds.resize(m_max_open_file_descriptors);
  268. auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : NullDevice::the();
  269. auto description = device_to_use_as_tty.open(O_RDWR).value();
  270. process->m_fds[0].set(*description);
  271. process->m_fds[1].set(*description);
  272. process->m_fds[2].set(*description);
  273. error = process->exec(path, move(arguments), move(environment));
  274. if (error != 0) {
  275. dbgln("Failed to exec {}: {}", path, error);
  276. first_thread = nullptr;
  277. return {};
  278. }
  279. {
  280. ScopedSpinLock lock(g_processes_lock);
  281. g_processes->prepend(process);
  282. process->ref();
  283. }
  284. error = 0;
  285. return process;
  286. }
  287. RefPtr<Process> Process::create_kernel_process(RefPtr<Thread>& first_thread, String&& name, void (*entry)(void*), void* entry_data, u32 affinity)
  288. {
  289. auto process = adopt(*new Process(first_thread, move(name), (uid_t)0, (gid_t)0, ProcessID(0), true));
  290. if (!first_thread)
  291. return {};
  292. first_thread->tss().eip = (FlatPtr)entry;
  293. first_thread->tss().esp = FlatPtr(entry_data); // entry function argument is expected to be in tss.esp
  294. if (process->pid() != 0) {
  295. ScopedSpinLock lock(g_processes_lock);
  296. g_processes->prepend(process);
  297. process->ref();
  298. }
  299. ScopedSpinLock lock(g_scheduler_lock);
  300. first_thread->set_affinity(affinity);
  301. first_thread->set_state(Thread::State::Runnable);
  302. return process;
  303. }
  304. Process::Process(RefPtr<Thread>& first_thread, const String& name, uid_t uid, gid_t gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, Process* fork_parent)
  305. : m_name(move(name))
  306. , m_pid(allocate_pid())
  307. , m_euid(uid)
  308. , m_egid(gid)
  309. , m_uid(uid)
  310. , m_gid(gid)
  311. , m_suid(uid)
  312. , m_sgid(gid)
  313. , m_is_kernel_process(is_kernel_process)
  314. , m_executable(move(executable))
  315. , m_cwd(move(cwd))
  316. , m_tty(tty)
  317. , m_ppid(ppid)
  318. , m_wait_block_condition(*this)
  319. {
  320. dbgln<debug_process>("Created new process {}({})", m_name, m_pid.value());
  321. m_page_directory = PageDirectory::create_for_userspace(*this, fork_parent ? &fork_parent->page_directory().range_allocator() : nullptr);
  322. if (fork_parent) {
  323. // NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the only thread in the new process.
  324. first_thread = Thread::current()->clone(*this);
  325. } else {
  326. // NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
  327. first_thread = adopt(*new Thread(*this));
  328. first_thread->detach();
  329. }
  330. if (first_thread && !first_thread->was_created()) {
  331. // We couldn't entirely create or clone this thread, abort
  332. first_thread = nullptr;
  333. }
  334. }
  335. Process::~Process()
  336. {
  337. ASSERT(thread_count() == 0); // all threads should have been finalized
  338. ASSERT(!m_alarm_timer);
  339. {
  340. ScopedSpinLock processses_lock(g_processes_lock);
  341. if (prev() || next())
  342. g_processes->remove(this);
  343. }
  344. }
  345. void Process::dump_regions()
  346. {
  347. klog() << "Process regions:";
  348. klog() << "BEGIN END SIZE ACCESS NAME";
  349. ScopedSpinLock lock(m_lock);
  350. Vector<Region*> sorted_regions;
  351. sorted_regions.ensure_capacity(m_regions.size());
  352. for (auto& region : m_regions)
  353. sorted_regions.append(&region);
  354. quick_sort(sorted_regions, [](auto& a, auto& b) {
  355. return a->vaddr() < b->vaddr();
  356. });
  357. for (auto& sorted_region : sorted_regions) {
  358. auto& region = *sorted_region;
  359. klog() << String::format("%08x", region.vaddr().get()) << " -- " << String::format("%08x", region.vaddr().offset(region.size() - 1).get()) << " " << String::format("%08zx", 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_anonymous() ? 'A' : ' ') << " " << region.name().characters();
  360. }
  361. MM.dump_kernel_regions();
  362. }
  363. // Make sure the compiler doesn't "optimize away" this function:
  364. extern void signal_trampoline_dummy();
  365. void signal_trampoline_dummy()
  366. {
  367. // The trampoline preserves the current eax, pushes the signal code and
  368. // then calls the signal handler. We do this because, when interrupting a
  369. // blocking syscall, that syscall may return some special error code in eax;
  370. // This error code would likely be overwritten by the signal handler, so it's
  371. // necessary to preserve it here.
  372. asm(
  373. ".intel_syntax noprefix\n"
  374. "asm_signal_trampoline:\n"
  375. "push ebp\n"
  376. "mov ebp, esp\n"
  377. "push eax\n" // we have to store eax 'cause it might be the return value from a syscall
  378. "sub esp, 4\n" // align the stack to 16 bytes
  379. "mov eax, [ebp+12]\n" // push the signal code
  380. "push eax\n"
  381. "call [ebp+8]\n" // call the signal handler
  382. "add esp, 8\n"
  383. "mov eax, %P0\n"
  384. "int 0x82\n" // sigreturn syscall
  385. "asm_signal_trampoline_end:\n"
  386. ".att_syntax" ::"i"(Syscall::SC_sigreturn));
  387. }
  388. extern "C" void asm_signal_trampoline(void);
  389. extern "C" void asm_signal_trampoline_end(void);
  390. void create_signal_trampolines()
  391. {
  392. InterruptDisabler disabler;
  393. // NOTE: We leak this region.
  394. 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();
  395. g_return_to_ring3_from_signal_trampoline = trampoline_region->vaddr();
  396. u8* trampoline = (u8*)asm_signal_trampoline;
  397. u8* trampoline_end = (u8*)asm_signal_trampoline_end;
  398. size_t trampoline_size = trampoline_end - trampoline;
  399. {
  400. SmapDisabler disabler;
  401. u8* code_ptr = (u8*)trampoline_region->vaddr().as_ptr();
  402. memcpy(code_ptr, trampoline, trampoline_size);
  403. }
  404. trampoline_region->set_writable(false);
  405. trampoline_region->remap();
  406. }
  407. void Process::crash(int signal, u32 eip, bool out_of_memory)
  408. {
  409. ASSERT_INTERRUPTS_DISABLED();
  410. ASSERT(!is_dead());
  411. ASSERT(Process::current() == this);
  412. if (out_of_memory) {
  413. dbgln("\033[31;1mOut of memory\033[m, killing: {}", *this);
  414. } else {
  415. if (eip >= 0xc0000000 && g_kernel_symbols_available) {
  416. auto* symbol = symbolicate_kernel_address(eip);
  417. dbgln("\033[31;1m{:p} {} +{}\033[0m\n", eip, (symbol ? demangle(symbol->name) : "(k?)"), (symbol ? eip - symbol->address : 0));
  418. } else {
  419. dbgln("\033[31;1m{:p} (?)\033[0m\n", eip);
  420. }
  421. dump_backtrace();
  422. }
  423. m_termination_signal = signal;
  424. set_dump_core(!out_of_memory);
  425. dump_regions();
  426. ASSERT(is_user_process());
  427. die();
  428. // We can not return from here, as there is nowhere
  429. // to unwind to, so die right away.
  430. Thread::current()->die_if_needed();
  431. ASSERT_NOT_REACHED();
  432. }
  433. RefPtr<Process> Process::from_pid(ProcessID pid)
  434. {
  435. ScopedSpinLock lock(g_processes_lock);
  436. for (auto& process : *g_processes) {
  437. process.pid();
  438. if (process.pid() == pid)
  439. return &process;
  440. }
  441. return {};
  442. }
  443. RefPtr<FileDescription> Process::file_description(int fd) const
  444. {
  445. if (fd < 0)
  446. return nullptr;
  447. if (static_cast<size_t>(fd) < m_fds.size())
  448. return m_fds[fd].description();
  449. return nullptr;
  450. }
  451. int Process::fd_flags(int fd) const
  452. {
  453. if (fd < 0)
  454. return -1;
  455. if (static_cast<size_t>(fd) < m_fds.size())
  456. return m_fds[fd].flags();
  457. return -1;
  458. }
  459. int Process::number_of_open_file_descriptors() const
  460. {
  461. int count = 0;
  462. for (auto& description : m_fds) {
  463. if (description)
  464. ++count;
  465. }
  466. return count;
  467. }
  468. int Process::alloc_fd(int first_candidate_fd)
  469. {
  470. for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) {
  471. if (!m_fds[i])
  472. return i;
  473. }
  474. return -EMFILE;
  475. }
  476. timeval kgettimeofday()
  477. {
  478. return TimeManagement::now_as_timeval();
  479. }
  480. void kgettimeofday(timeval& tv)
  481. {
  482. tv = kgettimeofday();
  483. }
  484. siginfo_t Process::wait_info()
  485. {
  486. siginfo_t siginfo;
  487. memset(&siginfo, 0, sizeof(siginfo));
  488. siginfo.si_signo = SIGCHLD;
  489. siginfo.si_pid = pid().value();
  490. siginfo.si_uid = uid();
  491. if (m_termination_signal) {
  492. siginfo.si_status = m_termination_signal;
  493. siginfo.si_code = CLD_KILLED;
  494. } else {
  495. siginfo.si_status = m_termination_status;
  496. siginfo.si_code = CLD_EXITED;
  497. }
  498. return siginfo;
  499. }
  500. Custody& Process::current_directory()
  501. {
  502. if (!m_cwd)
  503. m_cwd = VFS::the().root_custody();
  504. return *m_cwd;
  505. }
  506. KResultOr<String> Process::get_syscall_path_argument(const char* user_path, size_t path_length) const
  507. {
  508. if (path_length == 0)
  509. return KResult(-EINVAL);
  510. if (path_length > PATH_MAX)
  511. return KResult(-ENAMETOOLONG);
  512. auto copied_string = copy_string_from_user(user_path, path_length);
  513. if (copied_string.is_null())
  514. return KResult(-EFAULT);
  515. return copied_string;
  516. }
  517. KResultOr<String> Process::get_syscall_path_argument(const Syscall::StringArgument& path) const
  518. {
  519. return get_syscall_path_argument(path.characters, path.length);
  520. }
  521. bool Process::dump_core()
  522. {
  523. ASSERT(is_dumpable());
  524. ASSERT(should_core_dump());
  525. dbgln("Generating coredump for pid: {}", m_pid.value());
  526. auto coredump_path = String::formatted("/tmp/coredump/{}_{}_{}", name(), m_pid.value(), RTC::now());
  527. auto coredump = CoreDump::create(*this, coredump_path);
  528. if (!coredump)
  529. return false;
  530. return !coredump->write().is_error();
  531. }
  532. bool Process::dump_perfcore()
  533. {
  534. ASSERT(is_dumpable());
  535. ASSERT(m_perf_event_buffer);
  536. dbgln("Generating perfcore for pid: {}", m_pid.value());
  537. auto description_or_error = VFS::the().open(String::formatted("perfcore.{}", m_pid.value()), O_CREAT | O_EXCL, 0400, current_directory(), UidAndGid { m_uid, m_gid });
  538. if (description_or_error.is_error())
  539. return false;
  540. auto& description = description_or_error.value();
  541. auto json = m_perf_event_buffer->to_json(m_pid, m_executable ? m_executable->absolute_path() : "");
  542. if (!json)
  543. return false;
  544. auto json_buffer = UserOrKernelBuffer::for_kernel_buffer(json->data());
  545. return !description->write(json_buffer, json->size()).is_error();
  546. }
  547. void Process::finalize()
  548. {
  549. ASSERT(Thread::current() == g_finalizer);
  550. dbgln<debug_process>("Finalizing process {}", *this);
  551. if (is_dumpable()) {
  552. if (m_should_dump_core)
  553. dump_core();
  554. if (m_perf_event_buffer)
  555. dump_perfcore();
  556. }
  557. m_threads_for_coredump.clear();
  558. if (m_alarm_timer)
  559. TimerQueue::the().cancel_timer(m_alarm_timer.release_nonnull());
  560. m_fds.clear();
  561. m_tty = nullptr;
  562. m_executable = nullptr;
  563. m_cwd = nullptr;
  564. m_root_directory = nullptr;
  565. m_root_directory_relative_to_global_root = nullptr;
  566. m_arguments.clear();
  567. m_environment.clear();
  568. m_dead = true;
  569. disown_all_shared_buffers();
  570. {
  571. // FIXME: PID/TID BUG
  572. if (auto parent_thread = Thread::from_tid(m_ppid.value())) {
  573. if (!(parent_thread->m_signal_action_data[SIGCHLD].flags & SA_NOCLDWAIT))
  574. parent_thread->send_signal(SIGCHLD, this);
  575. }
  576. }
  577. {
  578. ScopedSpinLock processses_lock(g_processes_lock);
  579. if (!!ppid()) {
  580. if (auto parent = Process::from_pid(ppid())) {
  581. parent->m_ticks_in_user_for_dead_children += m_ticks_in_user + m_ticks_in_user_for_dead_children;
  582. parent->m_ticks_in_kernel_for_dead_children += m_ticks_in_kernel + m_ticks_in_kernel_for_dead_children;
  583. }
  584. }
  585. }
  586. unblock_waiters(Thread::WaitBlocker::UnblockFlags::Terminated);
  587. {
  588. ScopedSpinLock lock(m_lock);
  589. m_regions.clear();
  590. }
  591. ASSERT(ref_count() > 0);
  592. // WaitBlockCondition::finalize will be in charge of dropping the last
  593. // reference if there are still waiters around, or whenever the last
  594. // waitable states are consumed. Unless there is no parent around
  595. // anymore, in which case we'll just drop it right away.
  596. m_wait_block_condition.finalize();
  597. }
  598. void Process::disowned_by_waiter(Process& process)
  599. {
  600. m_wait_block_condition.disowned_by_waiter(process);
  601. }
  602. void Process::unblock_waiters(Thread::WaitBlocker::UnblockFlags flags, u8 signal)
  603. {
  604. if (auto parent = Process::from_pid(ppid()))
  605. parent->m_wait_block_condition.unblock(*this, flags, signal);
  606. }
  607. void Process::die()
  608. {
  609. // Let go of the TTY, otherwise a slave PTY may keep the master PTY from
  610. // getting an EOF when the last process using the slave PTY dies.
  611. // If the master PTY owner relies on an EOF to know when to wait() on a
  612. // slave owner, we have to allow the PTY pair to be torn down.
  613. m_tty = nullptr;
  614. for_each_thread([&](auto& thread) {
  615. m_threads_for_coredump.append(&thread);
  616. return IterationDecision::Continue;
  617. });
  618. kill_all_threads();
  619. }
  620. size_t Process::amount_dirty_private() const
  621. {
  622. // FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
  623. // The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
  624. // That's probably a situation that needs to be looked at in general.
  625. size_t amount = 0;
  626. ScopedSpinLock lock(m_lock);
  627. for (auto& region : m_regions) {
  628. if (!region.is_shared())
  629. amount += region.amount_dirty();
  630. }
  631. return amount;
  632. }
  633. size_t Process::amount_clean_inode() const
  634. {
  635. HashTable<const InodeVMObject*> vmobjects;
  636. {
  637. ScopedSpinLock lock(m_lock);
  638. for (auto& region : m_regions) {
  639. if (region.vmobject().is_inode())
  640. vmobjects.set(&static_cast<const InodeVMObject&>(region.vmobject()));
  641. }
  642. }
  643. size_t amount = 0;
  644. for (auto& vmobject : vmobjects)
  645. amount += vmobject->amount_clean();
  646. return amount;
  647. }
  648. size_t Process::amount_virtual() const
  649. {
  650. size_t amount = 0;
  651. ScopedSpinLock lock(m_lock);
  652. for (auto& region : m_regions) {
  653. amount += region.size();
  654. }
  655. return amount;
  656. }
  657. size_t Process::amount_resident() const
  658. {
  659. // FIXME: This will double count if multiple regions use the same physical page.
  660. size_t amount = 0;
  661. ScopedSpinLock lock(m_lock);
  662. for (auto& region : m_regions) {
  663. amount += region.amount_resident();
  664. }
  665. return amount;
  666. }
  667. size_t Process::amount_shared() const
  668. {
  669. // FIXME: This will double count if multiple regions use the same physical page.
  670. // FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
  671. // and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
  672. // so that every Region contributes +1 ref to each of its PhysicalPages.
  673. size_t amount = 0;
  674. ScopedSpinLock lock(m_lock);
  675. for (auto& region : m_regions) {
  676. amount += region.amount_shared();
  677. }
  678. return amount;
  679. }
  680. size_t Process::amount_purgeable_volatile() const
  681. {
  682. size_t amount = 0;
  683. ScopedSpinLock lock(m_lock);
  684. for (auto& region : m_regions) {
  685. if (region.vmobject().is_anonymous() && static_cast<const AnonymousVMObject&>(region.vmobject()).is_any_volatile())
  686. amount += region.amount_resident();
  687. }
  688. return amount;
  689. }
  690. size_t Process::amount_purgeable_nonvolatile() const
  691. {
  692. size_t amount = 0;
  693. ScopedSpinLock lock(m_lock);
  694. for (auto& region : m_regions) {
  695. if (region.vmobject().is_anonymous() && !static_cast<const AnonymousVMObject&>(region.vmobject()).is_any_volatile())
  696. amount += region.amount_resident();
  697. }
  698. return amount;
  699. }
  700. void Process::terminate_due_to_signal(u8 signal)
  701. {
  702. ASSERT_INTERRUPTS_DISABLED();
  703. ASSERT(signal < 32);
  704. ASSERT(Process::current() == this);
  705. dbgln("Terminating {} due to signal {}", *this, signal);
  706. m_termination_status = 0;
  707. m_termination_signal = signal;
  708. die();
  709. }
  710. KResult Process::send_signal(u8 signal, Process* sender)
  711. {
  712. // Try to send it to the "obvious" main thread:
  713. auto receiver_thread = Thread::from_tid(m_pid.value());
  714. // If the main thread has died, there may still be other threads:
  715. if (!receiver_thread) {
  716. // The first one should be good enough.
  717. // Neither kill(2) nor kill(3) specify any selection precedure.
  718. for_each_thread([&receiver_thread](Thread& thread) -> IterationDecision {
  719. receiver_thread = &thread;
  720. return IterationDecision::Break;
  721. });
  722. }
  723. if (receiver_thread) {
  724. receiver_thread->send_signal(signal, sender);
  725. return KSuccess;
  726. }
  727. return KResult(-ESRCH);
  728. }
  729. RefPtr<Thread> Process::create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, const String& name, u32 affinity, bool joinable)
  730. {
  731. ASSERT((priority >= THREAD_PRIORITY_MIN) && (priority <= THREAD_PRIORITY_MAX));
  732. // FIXME: Do something with guard pages?
  733. auto thread = adopt(*new Thread(*this));
  734. if (!thread->was_created()) {
  735. // Could not fully create this thread
  736. return {};
  737. }
  738. thread->set_name(name);
  739. thread->set_affinity(affinity);
  740. thread->set_priority(priority);
  741. if (!joinable)
  742. thread->detach();
  743. auto& tss = thread->tss();
  744. tss.eip = (FlatPtr)entry;
  745. tss.esp = FlatPtr(entry_data); // entry function argument is expected to be in tss.esp
  746. ScopedSpinLock lock(g_scheduler_lock);
  747. thread->set_state(Thread::State::Runnable);
  748. return thread;
  749. }
  750. void Process::FileDescriptionAndFlags::clear()
  751. {
  752. m_description = nullptr;
  753. m_flags = 0;
  754. }
  755. void Process::FileDescriptionAndFlags::set(NonnullRefPtr<FileDescription>&& description, u32 flags)
  756. {
  757. m_description = move(description);
  758. m_flags = flags;
  759. }
  760. OwnPtr<KBuffer> Process::backtrace() const
  761. {
  762. KBufferBuilder builder;
  763. for_each_thread([&](Thread& thread) {
  764. builder.appendf("Thread %d (%s):\n", thread.tid().value(), thread.name().characters());
  765. builder.append(thread.backtrace());
  766. return IterationDecision::Continue;
  767. });
  768. return builder.build();
  769. }
  770. Custody& Process::root_directory()
  771. {
  772. if (!m_root_directory)
  773. m_root_directory = VFS::the().root_custody();
  774. return *m_root_directory;
  775. }
  776. Custody& Process::root_directory_relative_to_global_root()
  777. {
  778. if (!m_root_directory_relative_to_global_root)
  779. m_root_directory_relative_to_global_root = root_directory();
  780. return *m_root_directory_relative_to_global_root;
  781. }
  782. void Process::set_root_directory(const Custody& root)
  783. {
  784. m_root_directory = root;
  785. }
  786. Region& Process::add_region(NonnullOwnPtr<Region> region)
  787. {
  788. auto* ptr = region.ptr();
  789. ScopedSpinLock lock(m_lock);
  790. m_regions.append(move(region));
  791. return *ptr;
  792. }
  793. void Process::set_tty(TTY* tty)
  794. {
  795. m_tty = tty;
  796. }
  797. void Process::start_tracing_from(ProcessID tracer)
  798. {
  799. m_tracer = ThreadTracer::create(tracer);
  800. }
  801. void Process::stop_tracing()
  802. {
  803. m_tracer = nullptr;
  804. }
  805. void Process::tracer_trap(Thread& thread, const RegisterState& regs)
  806. {
  807. ASSERT(m_tracer.ptr());
  808. m_tracer->set_regs(regs);
  809. thread.send_urgent_signal_to_self(SIGTRAP);
  810. }
  811. PerformanceEventBuffer& Process::ensure_perf_events()
  812. {
  813. if (!m_perf_event_buffer)
  814. m_perf_event_buffer = make<PerformanceEventBuffer>();
  815. return *m_perf_event_buffer;
  816. }
  817. }