Process.cpp 29 KB

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