Process.cpp 28 KB

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