fork.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Idan Horowitz <idan.horowitz@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <Kernel/Debug.h>
  8. #include <Kernel/FileSystem/Custody.h>
  9. #include <Kernel/Memory/Region.h>
  10. #include <Kernel/PerformanceManager.h>
  11. #include <Kernel/Process.h>
  12. #include <Kernel/Scheduler.h>
  13. namespace Kernel {
  14. ErrorOr<FlatPtr> Process::sys$fork(RegisterState& regs)
  15. {
  16. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
  17. TRY(require_promise(Pledge::proc));
  18. auto child_name = TRY(name().with([](auto& name) { return name->try_clone(); }));
  19. auto credentials = this->credentials();
  20. auto child_and_first_thread = TRY(Process::create(move(child_name), credentials->uid(), credentials->gid(), pid(), m_is_kernel_process, current_directory(), executable(), m_tty, this));
  21. auto& child = child_and_first_thread.process;
  22. auto& child_first_thread = child_and_first_thread.first_thread;
  23. ArmedScopeGuard thread_finalizer_guard = [&child_first_thread]() {
  24. SpinlockLocker lock(g_scheduler_lock);
  25. child_first_thread->detach();
  26. child_first_thread->set_state(Thread::State::Dying);
  27. };
  28. // NOTE: All user processes have a leaked ref on them. It's balanced by Thread::WaitBlockerSet::finalize().
  29. child->ref();
  30. TRY(m_unveil_data.with([&](auto& parent_unveil_data) -> ErrorOr<void> {
  31. return child->m_unveil_data.with([&](auto& child_unveil_data) -> ErrorOr<void> {
  32. child_unveil_data.state = parent_unveil_data.state;
  33. child_unveil_data.paths = TRY(parent_unveil_data.paths.deep_copy());
  34. return {};
  35. });
  36. }));
  37. TRY(m_exec_unveil_data.with([&](auto& parent_exec_unveil_data) -> ErrorOr<void> {
  38. return child->m_exec_unveil_data.with([&](auto& child_exec_unveil_data) -> ErrorOr<void> {
  39. child_exec_unveil_data.state = parent_exec_unveil_data.state;
  40. child_exec_unveil_data.paths = TRY(parent_exec_unveil_data.paths.deep_copy());
  41. return {};
  42. });
  43. }));
  44. // Note: We take the spinlock of Process::all_instances list because we need
  45. // to ensure that when we take the jail spinlock of two processes that we don't
  46. // run into a deadlock situation because both processes compete over each other Jail's
  47. // spinlock. Such pattern of taking 3 spinlocks in the same order happens in
  48. // Process::for_each* methods.
  49. TRY(Process::all_instances().with([&](auto const&) -> ErrorOr<void> {
  50. TRY(m_attached_jail.with([&](auto& parent_jail) -> ErrorOr<void> {
  51. return child->m_attached_jail.with([&](auto& child_jail) -> ErrorOr<void> {
  52. child_jail = parent_jail;
  53. if (child_jail) {
  54. child_jail->attach_count().with([&](auto& attach_count) {
  55. attach_count++;
  56. });
  57. }
  58. return {};
  59. });
  60. }));
  61. return {};
  62. }));
  63. ArmedScopeGuard remove_from_jail_process_list = [&]() {
  64. m_jail_process_list.with([&](auto& list_ptr) {
  65. if (list_ptr) {
  66. list_ptr->attached_processes().with([&](auto& list) {
  67. list.remove(*child);
  68. });
  69. }
  70. });
  71. };
  72. m_jail_process_list.with([&](auto& list_ptr) {
  73. if (list_ptr) {
  74. child->m_jail_process_list.with([&](auto& child_list_ptr) {
  75. child_list_ptr = list_ptr;
  76. });
  77. list_ptr->attached_processes().with([&](auto& list) {
  78. list.append(child);
  79. });
  80. }
  81. });
  82. TRY(child->m_fds.with_exclusive([&](auto& child_fds) {
  83. return m_fds.with_exclusive([&](auto& parent_fds) {
  84. return child_fds.try_clone(parent_fds);
  85. });
  86. }));
  87. child->m_pg = m_pg;
  88. with_protected_data([&](auto& my_protected_data) {
  89. child->with_mutable_protected_data([&](auto& child_protected_data) {
  90. child_protected_data.promises = my_protected_data.promises.load();
  91. child_protected_data.execpromises = my_protected_data.execpromises.load();
  92. child_protected_data.has_promises = my_protected_data.has_promises.load();
  93. child_protected_data.has_execpromises = my_protected_data.has_execpromises.load();
  94. child_protected_data.sid = my_protected_data.sid;
  95. child_protected_data.credentials = my_protected_data.credentials;
  96. child_protected_data.umask = my_protected_data.umask;
  97. child_protected_data.signal_trampoline = my_protected_data.signal_trampoline;
  98. child_protected_data.dumpable = my_protected_data.dumpable;
  99. });
  100. });
  101. dbgln_if(FORK_DEBUG, "fork: child={}", child);
  102. // A child created via fork(2) inherits a copy of its parent's signal mask
  103. child_first_thread->update_signal_mask(Thread::current()->signal_mask());
  104. // A child process created via fork(2) inherits a copy of its parent's alternate signal stack settings.
  105. child_first_thread->m_alternative_signal_stack = Thread::current()->m_alternative_signal_stack;
  106. child_first_thread->m_alternative_signal_stack_size = Thread::current()->m_alternative_signal_stack_size;
  107. auto& child_regs = child_first_thread->m_regs;
  108. #if ARCH(X86_64)
  109. child_regs.rax = 0; // fork() returns 0 in the child :^)
  110. child_regs.rbx = regs.rbx;
  111. child_regs.rcx = regs.rcx;
  112. child_regs.rdx = regs.rdx;
  113. child_regs.rbp = regs.rbp;
  114. child_regs.rsp = regs.userspace_rsp;
  115. child_regs.rsi = regs.rsi;
  116. child_regs.rdi = regs.rdi;
  117. child_regs.r8 = regs.r8;
  118. child_regs.r9 = regs.r9;
  119. child_regs.r10 = regs.r10;
  120. child_regs.r11 = regs.r11;
  121. child_regs.r12 = regs.r12;
  122. child_regs.r13 = regs.r13;
  123. child_regs.r14 = regs.r14;
  124. child_regs.r15 = regs.r15;
  125. child_regs.rflags = regs.rflags;
  126. child_regs.rip = regs.rip;
  127. child_regs.cs = regs.cs;
  128. dbgln_if(FORK_DEBUG, "fork: child will begin executing at {:#04x}:{:p} with stack {:p}, kstack {:p}",
  129. child_regs.cs, child_regs.rip, child_regs.rsp, child_regs.rsp0);
  130. #elif ARCH(AARCH64)
  131. child_regs.x[0] = 0; // fork() returns 0 in the child :^)
  132. for (size_t i = 1; i < array_size(child_regs.x); ++i)
  133. child_regs.x[i] = regs.x[i];
  134. child_regs.spsr_el1 = regs.spsr_el1;
  135. child_regs.elr_el1 = regs.elr_el1;
  136. child_regs.sp_el0 = regs.sp_el0;
  137. #else
  138. # error Unknown architecture
  139. #endif
  140. TRY(address_space().with([&](auto& parent_space) {
  141. return child->address_space().with([&](auto& child_space) -> ErrorOr<void> {
  142. child_space->set_enforces_syscall_regions(parent_space->enforces_syscall_regions());
  143. for (auto& region : parent_space->region_tree().regions()) {
  144. dbgln_if(FORK_DEBUG, "fork: cloning Region '{}' @ {}", region.name(), region.vaddr());
  145. auto region_clone = TRY(region.try_clone());
  146. TRY(region_clone->map(child_space->page_directory(), Memory::ShouldFlushTLB::No));
  147. TRY(child_space->region_tree().place_specifically(*region_clone, region.range()));
  148. auto* child_region = region_clone.leak_ptr();
  149. if (&region == m_master_tls_region.unsafe_ptr())
  150. child->m_master_tls_region = TRY(child_region->try_make_weak_ptr());
  151. }
  152. return {};
  153. });
  154. }));
  155. thread_finalizer_guard.disarm();
  156. remove_from_jail_process_list.disarm();
  157. Process::register_new(*child);
  158. PerformanceManager::add_process_created_event(*child);
  159. SpinlockLocker lock(g_scheduler_lock);
  160. child_first_thread->set_affinity(Thread::current()->affinity());
  161. child_first_thread->set_state(Thread::State::Runnable);
  162. auto child_pid = child->pid().value();
  163. return child_pid;
  164. }
  165. }