Process.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. if (thread.state() != Thread::State::Dead)
  250. thread.set_state(Thread::State::Dying);
  251. return IterationDecision::Continue;
  252. });
  253. big_lock().clear_waiters();
  254. }
  255. void Process::kill_all_threads()
  256. {
  257. for_each_thread([&](Thread& thread) {
  258. thread.set_should_die();
  259. return IterationDecision::Continue;
  260. });
  261. }
  262. 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)
  263. {
  264. auto parts = path.split('/');
  265. if (arguments.is_empty()) {
  266. arguments.append(parts.last());
  267. }
  268. RefPtr<Custody> cwd;
  269. RefPtr<Custody> root;
  270. {
  271. ScopedSpinLock lock(g_processes_lock);
  272. if (auto parent = Process::from_pid(parent_pid)) {
  273. cwd = parent->m_cwd;
  274. root = parent->m_root_directory;
  275. }
  276. }
  277. if (!cwd)
  278. cwd = VFS::the().root_custody();
  279. if (!root)
  280. root = VFS::the().root_custody();
  281. auto process = adopt(*new Process(first_thread, parts.take_last(), uid, gid, parent_pid, Ring3, move(cwd), nullptr, tty));
  282. process->m_fds.resize(m_max_open_file_descriptors);
  283. auto& device_to_use_as_tty = tty ? (CharacterDevice&)*tty : NullDevice::the();
  284. auto description = device_to_use_as_tty.open(O_RDWR).value();
  285. process->m_fds[0].set(*description);
  286. process->m_fds[1].set(*description);
  287. process->m_fds[2].set(*description);
  288. error = process->exec(path, move(arguments), move(environment));
  289. if (error != 0) {
  290. dbg() << "Failed to exec " << path << ": " << error;
  291. delete first_thread;
  292. return {};
  293. }
  294. {
  295. ScopedSpinLock lock(g_processes_lock);
  296. g_processes->prepend(process);
  297. process->ref();
  298. }
  299. error = 0;
  300. return process;
  301. }
  302. NonnullRefPtr<Process> Process::create_kernel_process(Thread*& first_thread, String&& name, void (*e)(), u32 affinity)
  303. {
  304. auto process = adopt(*new Process(first_thread, move(name), (uid_t)0, (gid_t)0, ProcessID(0), Ring0));
  305. first_thread->tss().eip = (FlatPtr)e;
  306. if (process->pid() != 0) {
  307. ScopedSpinLock lock(g_processes_lock);
  308. g_processes->prepend(process);
  309. process->ref();
  310. }
  311. first_thread->set_affinity(affinity);
  312. first_thread->set_state(Thread::State::Runnable);
  313. return process;
  314. }
  315. 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)
  316. : m_name(move(name))
  317. , m_pid(allocate_pid())
  318. , m_euid(uid)
  319. , m_egid(gid)
  320. , m_uid(uid)
  321. , m_gid(gid)
  322. , m_suid(uid)
  323. , m_sgid(gid)
  324. , m_ring(ring)
  325. , m_executable(move(executable))
  326. , m_cwd(move(cwd))
  327. , m_tty(tty)
  328. , m_ppid(ppid)
  329. {
  330. #ifdef PROCESS_DEBUG
  331. dbg() << "Created new process " << m_name << "(" << m_pid << ")";
  332. #endif
  333. m_page_directory = PageDirectory::create_for_userspace(*this, fork_parent ? &fork_parent->page_directory().range_allocator() : nullptr);
  334. #ifdef MM_DEBUG
  335. dbg() << "Process " << pid() << " ctor: PD=" << m_page_directory.ptr() << " created";
  336. #endif
  337. if (fork_parent) {
  338. // NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the only thread in the new process.
  339. first_thread = Thread::current()->clone(*this);
  340. } else {
  341. // NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
  342. first_thread = new Thread(*this);
  343. }
  344. }
  345. Process::~Process()
  346. {
  347. ASSERT(thread_count() == 0);
  348. }
  349. void Process::dump_regions()
  350. {
  351. klog() << "Process regions:";
  352. klog() << "BEGIN END SIZE ACCESS NAME";
  353. for (auto& region : m_regions) {
  354. 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();
  355. }
  356. MM.dump_kernel_regions();
  357. }
  358. void signal_trampoline_dummy(void)
  359. {
  360. // The trampoline preserves the current eax, pushes the signal code and
  361. // then calls the signal handler. We do this because, when interrupting a
  362. // blocking syscall, that syscall may return some special error code in eax;
  363. // This error code would likely be overwritten by the signal handler, so it's
  364. // neccessary to preserve it here.
  365. asm(
  366. ".intel_syntax noprefix\n"
  367. "asm_signal_trampoline:\n"
  368. "push ebp\n"
  369. "mov ebp, esp\n"
  370. "push eax\n" // we have to store eax 'cause it might be the return value from a syscall
  371. "sub esp, 4\n" // align the stack to 16 bytes
  372. "mov eax, [ebp+12]\n" // push the signal code
  373. "push eax\n"
  374. "call [ebp+8]\n" // call the signal handler
  375. "add esp, 8\n"
  376. "mov eax, %P0\n"
  377. "int 0x82\n" // sigreturn syscall
  378. "asm_signal_trampoline_end:\n"
  379. ".att_syntax" ::"i"(Syscall::SC_sigreturn));
  380. }
  381. extern "C" void asm_signal_trampoline(void);
  382. extern "C" void asm_signal_trampoline_end(void);
  383. void create_signal_trampolines()
  384. {
  385. InterruptDisabler disabler;
  386. // NOTE: We leak this region.
  387. 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();
  388. g_return_to_ring3_from_signal_trampoline = trampoline_region->vaddr();
  389. u8* trampoline = (u8*)asm_signal_trampoline;
  390. u8* trampoline_end = (u8*)asm_signal_trampoline_end;
  391. size_t trampoline_size = trampoline_end - trampoline;
  392. {
  393. SmapDisabler disabler;
  394. u8* code_ptr = (u8*)trampoline_region->vaddr().as_ptr();
  395. memcpy(code_ptr, trampoline, trampoline_size);
  396. }
  397. trampoline_region->set_writable(false);
  398. trampoline_region->remap();
  399. }
  400. void Process::crash(int signal, u32 eip, bool out_of_memory)
  401. {
  402. ASSERT_INTERRUPTS_DISABLED();
  403. ASSERT(!is_dead());
  404. ASSERT(Process::current() == this);
  405. if (out_of_memory) {
  406. dbg() << "\033[31;1mOut of memory\033[m, killing: " << *this;
  407. } else {
  408. if (eip >= 0xc0000000 && g_kernel_symbols_available) {
  409. auto* symbol = symbolicate_kernel_address(eip);
  410. dbg() << "\033[31;1m" << String::format("%p", eip) << " " << (symbol ? demangle(symbol->name) : "(k?)") << " +" << (symbol ? eip - symbol->address : 0) << "\033[0m\n";
  411. } else if (auto elf_bundle = this->elf_bundle()) {
  412. dbg() << "\033[31;1m" << String::format("%p", eip) << " " << elf_bundle->elf_loader->symbolicate(eip) << "\033[0m\n";
  413. } else {
  414. dbg() << "\033[31;1m" << String::format("%p", eip) << " (?)\033[0m\n";
  415. }
  416. dump_backtrace();
  417. }
  418. m_termination_signal = signal;
  419. dump_regions();
  420. ASSERT(is_ring3());
  421. die();
  422. // We can not return from here, as there is nowhere
  423. // to unwind to, so die right away.
  424. Thread::current()->die_if_needed();
  425. ASSERT_NOT_REACHED();
  426. }
  427. RefPtr<Process> Process::from_pid(ProcessID pid)
  428. {
  429. ScopedSpinLock lock(g_processes_lock);
  430. for (auto& process : *g_processes) {
  431. process.pid();
  432. if (process.pid() == pid)
  433. return &process;
  434. }
  435. return {};
  436. }
  437. RefPtr<FileDescription> Process::file_description(int fd) const
  438. {
  439. if (fd < 0)
  440. return nullptr;
  441. if (static_cast<size_t>(fd) < m_fds.size())
  442. return m_fds[fd].description();
  443. return nullptr;
  444. }
  445. int Process::fd_flags(int fd) const
  446. {
  447. if (fd < 0)
  448. return -1;
  449. if (static_cast<size_t>(fd) < m_fds.size())
  450. return m_fds[fd].flags();
  451. return -1;
  452. }
  453. String Process::validate_and_copy_string_from_user(const char* user_characters, size_t user_length) const
  454. {
  455. if (user_length == 0)
  456. return String::empty();
  457. if (!user_characters)
  458. return {};
  459. if (!validate_read(user_characters, user_length))
  460. return {};
  461. SmapDisabler disabler;
  462. size_t measured_length = strnlen(user_characters, user_length);
  463. return String(user_characters, measured_length);
  464. }
  465. String Process::validate_and_copy_string_from_user(const Syscall::StringArgument& string) const
  466. {
  467. return validate_and_copy_string_from_user(string.characters, string.length);
  468. }
  469. int Process::number_of_open_file_descriptors() const
  470. {
  471. int count = 0;
  472. for (auto& description : m_fds) {
  473. if (description)
  474. ++count;
  475. }
  476. return count;
  477. }
  478. int Process::alloc_fd(int first_candidate_fd)
  479. {
  480. for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) {
  481. if (!m_fds[i])
  482. return i;
  483. }
  484. return -EMFILE;
  485. }
  486. timeval kgettimeofday()
  487. {
  488. return g_timeofday;
  489. }
  490. void kgettimeofday(timeval& tv)
  491. {
  492. tv = kgettimeofday();
  493. }
  494. siginfo_t Process::reap(Process& process)
  495. {
  496. siginfo_t siginfo;
  497. memset(&siginfo, 0, sizeof(siginfo));
  498. siginfo.si_signo = SIGCHLD;
  499. siginfo.si_pid = process.pid().value();
  500. siginfo.si_uid = process.uid();
  501. if (process.m_termination_signal) {
  502. siginfo.si_status = process.m_termination_signal;
  503. siginfo.si_code = CLD_KILLED;
  504. } else {
  505. siginfo.si_status = process.m_termination_status;
  506. siginfo.si_code = CLD_EXITED;
  507. }
  508. ASSERT(g_processes_lock.is_locked());
  509. if (!!process.ppid()) {
  510. auto parent = Process::from_pid(process.ppid());
  511. if (parent) {
  512. parent->m_ticks_in_user_for_dead_children += process.m_ticks_in_user + process.m_ticks_in_user_for_dead_children;
  513. parent->m_ticks_in_kernel_for_dead_children += process.m_ticks_in_kernel + process.m_ticks_in_kernel_for_dead_children;
  514. }
  515. }
  516. #ifdef PROCESS_DEBUG
  517. dbg() << "Reaping process " << process;
  518. #endif
  519. ASSERT(process.is_dead());
  520. g_processes->remove(&process);
  521. process.unref();
  522. return siginfo;
  523. }
  524. bool Process::validate_read_from_kernel(VirtualAddress vaddr, size_t size) const
  525. {
  526. if (vaddr.is_null())
  527. return false;
  528. return MM.validate_kernel_read(*this, vaddr, size);
  529. }
  530. bool Process::validate_read(const void* address, size_t size) const
  531. {
  532. if (!size)
  533. return false;
  534. return MM.validate_user_read(*this, VirtualAddress(address), size);
  535. }
  536. bool Process::validate_write(void* address, size_t size) const
  537. {
  538. if (!size)
  539. return false;
  540. return MM.validate_user_write(*this, VirtualAddress(address), size);
  541. }
  542. Custody& Process::current_directory()
  543. {
  544. if (!m_cwd)
  545. m_cwd = VFS::the().root_custody();
  546. return *m_cwd;
  547. }
  548. KResultOr<String> Process::get_syscall_path_argument(const char* user_path, size_t path_length) const
  549. {
  550. if (path_length == 0)
  551. return KResult(-EINVAL);
  552. if (path_length > PATH_MAX)
  553. return KResult(-ENAMETOOLONG);
  554. if (!validate_read(user_path, path_length))
  555. return KResult(-EFAULT);
  556. return copy_string_from_user(user_path, path_length);
  557. }
  558. KResultOr<String> Process::get_syscall_path_argument(const Syscall::StringArgument& path) const
  559. {
  560. return get_syscall_path_argument(path.characters, path.length);
  561. }
  562. void Process::finalize()
  563. {
  564. ASSERT(Thread::current() == g_finalizer);
  565. #ifdef PROCESS_DEBUG
  566. dbg() << "Finalizing process " << *this;
  567. #endif
  568. if (m_perf_event_buffer) {
  569. 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 });
  570. if (!description_or_error.is_error()) {
  571. auto& description = description_or_error.value();
  572. auto json = m_perf_event_buffer->to_json(m_pid, m_executable ? m_executable->absolute_path() : "");
  573. // FIXME: Should this error path be surfaced somehow?
  574. (void)description->write(json.data(), json.size());
  575. }
  576. }
  577. m_fds.clear();
  578. m_tty = nullptr;
  579. m_executable = nullptr;
  580. m_cwd = nullptr;
  581. m_root_directory = nullptr;
  582. m_root_directory_relative_to_global_root = nullptr;
  583. disown_all_shared_buffers();
  584. {
  585. InterruptDisabler disabler;
  586. // FIXME: PID/TID BUG
  587. if (auto* parent_thread = Thread::from_tid(m_ppid.value())) {
  588. if (parent_thread->m_signal_action_data[SIGCHLD].flags & SA_NOCLDWAIT) {
  589. // NOTE: If the parent doesn't care about this process, let it go.
  590. m_ppid = 0;
  591. } else {
  592. parent_thread->send_signal(SIGCHLD, this);
  593. }
  594. }
  595. }
  596. {
  597. ScopedSpinLock lock(m_lock);
  598. m_regions.clear();
  599. }
  600. m_dead = true;
  601. }
  602. void Process::die()
  603. {
  604. // Let go of the TTY, otherwise a slave PTY may keep the master PTY from
  605. // getting an EOF when the last process using the slave PTY dies.
  606. // If the master PTY owner relies on an EOF to know when to wait() on a
  607. // slave owner, we have to allow the PTY pair to be torn down.
  608. m_tty = nullptr;
  609. kill_all_threads();
  610. }
  611. size_t Process::amount_dirty_private() const
  612. {
  613. // FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
  614. // The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
  615. // That's probably a situation that needs to be looked at in general.
  616. size_t amount = 0;
  617. ScopedSpinLock lock(m_lock);
  618. for (auto& region : m_regions) {
  619. if (!region.is_shared())
  620. amount += region.amount_dirty();
  621. }
  622. return amount;
  623. }
  624. size_t Process::amount_clean_inode() const
  625. {
  626. HashTable<const InodeVMObject*> vmobjects;
  627. {
  628. ScopedSpinLock lock(m_lock);
  629. for (auto& region : m_regions) {
  630. if (region.vmobject().is_inode())
  631. vmobjects.set(&static_cast<const InodeVMObject&>(region.vmobject()));
  632. }
  633. }
  634. size_t amount = 0;
  635. for (auto& vmobject : vmobjects)
  636. amount += vmobject->amount_clean();
  637. return amount;
  638. }
  639. size_t Process::amount_virtual() const
  640. {
  641. size_t amount = 0;
  642. ScopedSpinLock lock(m_lock);
  643. for (auto& region : m_regions) {
  644. amount += region.size();
  645. }
  646. return amount;
  647. }
  648. size_t Process::amount_resident() const
  649. {
  650. // FIXME: This will double count if multiple regions use the same physical page.
  651. size_t amount = 0;
  652. ScopedSpinLock lock(m_lock);
  653. for (auto& region : m_regions) {
  654. amount += region.amount_resident();
  655. }
  656. return amount;
  657. }
  658. size_t Process::amount_shared() const
  659. {
  660. // FIXME: This will double count if multiple regions use the same physical page.
  661. // FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
  662. // and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
  663. // so that every Region contributes +1 ref to each of its PhysicalPages.
  664. size_t amount = 0;
  665. ScopedSpinLock lock(m_lock);
  666. for (auto& region : m_regions) {
  667. amount += region.amount_shared();
  668. }
  669. return amount;
  670. }
  671. size_t Process::amount_purgeable_volatile() const
  672. {
  673. size_t amount = 0;
  674. ScopedSpinLock lock(m_lock);
  675. for (auto& region : m_regions) {
  676. if (region.vmobject().is_purgeable() && static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
  677. amount += region.amount_resident();
  678. }
  679. return amount;
  680. }
  681. size_t Process::amount_purgeable_nonvolatile() const
  682. {
  683. size_t amount = 0;
  684. ScopedSpinLock lock(m_lock);
  685. for (auto& region : m_regions) {
  686. if (region.vmobject().is_purgeable() && !static_cast<const PurgeableVMObject&>(region.vmobject()).is_volatile())
  687. amount += region.amount_resident();
  688. }
  689. return amount;
  690. }
  691. void Process::terminate_due_to_signal(u8 signal)
  692. {
  693. ASSERT_INTERRUPTS_DISABLED();
  694. ASSERT(signal < 32);
  695. dbg() << "Terminating " << *this << " due to signal " << signal;
  696. m_termination_status = 0;
  697. m_termination_signal = signal;
  698. die();
  699. }
  700. KResult Process::send_signal(u8 signal, Process* sender)
  701. {
  702. InterruptDisabler disabler;
  703. // FIXME: PID/TID BUG
  704. if (auto* thread = Thread::from_tid(m_pid.value())) {
  705. thread->send_signal(signal, sender);
  706. return KSuccess;
  707. }
  708. return KResult(-ESRCH);
  709. }
  710. Thread* Process::create_kernel_thread(void (*entry)(), u32 priority, const String& name, u32 affinity, bool joinable)
  711. {
  712. ASSERT((priority >= THREAD_PRIORITY_MIN) && (priority <= THREAD_PRIORITY_MAX));
  713. // FIXME: Do something with guard pages?
  714. auto* thread = new Thread(*this);
  715. thread->set_name(name);
  716. thread->set_affinity(affinity);
  717. thread->set_priority(priority);
  718. thread->set_joinable(joinable);
  719. auto& tss = thread->tss();
  720. tss.eip = (FlatPtr)entry;
  721. thread->set_state(Thread::State::Runnable);
  722. return thread;
  723. }
  724. void Process::FileDescriptionAndFlags::clear()
  725. {
  726. m_description = nullptr;
  727. m_flags = 0;
  728. }
  729. void Process::FileDescriptionAndFlags::set(NonnullRefPtr<FileDescription>&& description, u32 flags)
  730. {
  731. m_description = move(description);
  732. m_flags = flags;
  733. }
  734. KBuffer Process::backtrace() const
  735. {
  736. KBufferBuilder builder;
  737. for_each_thread([&](Thread& thread) {
  738. builder.appendf("Thread %d (%s):\n", thread.tid().value(), thread.name().characters());
  739. builder.append(thread.backtrace());
  740. return IterationDecision::Continue;
  741. });
  742. return builder.build();
  743. }
  744. Custody& Process::root_directory()
  745. {
  746. if (!m_root_directory)
  747. m_root_directory = VFS::the().root_custody();
  748. return *m_root_directory;
  749. }
  750. Custody& Process::root_directory_relative_to_global_root()
  751. {
  752. if (!m_root_directory_relative_to_global_root)
  753. m_root_directory_relative_to_global_root = root_directory();
  754. return *m_root_directory_relative_to_global_root;
  755. }
  756. void Process::set_root_directory(const Custody& root)
  757. {
  758. m_root_directory = root;
  759. }
  760. Region& Process::add_region(NonnullOwnPtr<Region> region)
  761. {
  762. auto* ptr = region.ptr();
  763. ScopedSpinLock lock(m_lock);
  764. m_regions.append(move(region));
  765. return *ptr;
  766. }
  767. void Process::set_tty(TTY* tty)
  768. {
  769. m_tty = tty;
  770. }
  771. OwnPtr<Process::ELFBundle> Process::elf_bundle() const
  772. {
  773. if (!m_executable)
  774. return nullptr;
  775. auto bundle = make<ELFBundle>();
  776. if (!m_executable->inode().shared_vmobject()) {
  777. return nullptr;
  778. }
  779. ASSERT(m_executable->inode().shared_vmobject());
  780. auto& vmobject = *m_executable->inode().shared_vmobject();
  781. bundle->region = MM.allocate_kernel_region_with_vmobject(const_cast<SharedInodeVMObject&>(vmobject), vmobject.size(), "ELF bundle", Region::Access::Read);
  782. if (!bundle->region)
  783. return nullptr;
  784. bundle->elf_loader = ELF::Loader::create(bundle->region->vaddr().as_ptr(), bundle->region->size());
  785. return bundle;
  786. }
  787. }