fork.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2018-2023, 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/Devices/TTY/TTY.h>
  9. #include <Kernel/FileSystem/Custody.h>
  10. #include <Kernel/Memory/Region.h>
  11. #include <Kernel/Tasks/PerformanceManager.h>
  12. #include <Kernel/Tasks/Process.h>
  13. #include <Kernel/Tasks/Scheduler.h>
  14. namespace Kernel {
  15. ErrorOr<FlatPtr> Process::sys$fork(RegisterState& regs)
  16. {
  17. VERIFY_NO_PROCESS_BIG_LOCK(this);
  18. TRY(require_promise(Pledge::proc));
  19. auto credentials = this->credentials();
  20. auto child_and_first_thread = TRY(Process::create_with_forked_name(credentials->uid(), credentials->gid(), pid(), m_is_kernel_process, current_directory(), executable(), 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. with_protected_data([&](auto& my_protected_data) {
  88. child->with_mutable_protected_data([&](auto& child_protected_data) {
  89. child_protected_data.promises = my_protected_data.promises;
  90. child_protected_data.execpromises = my_protected_data.execpromises;
  91. child_protected_data.has_promises = my_protected_data.has_promises;
  92. child_protected_data.has_execpromises = my_protected_data.has_execpromises;
  93. child_protected_data.credentials = my_protected_data.credentials;
  94. child_protected_data.umask = my_protected_data.umask;
  95. child_protected_data.signal_trampoline = my_protected_data.signal_trampoline;
  96. child_protected_data.dumpable = my_protected_data.dumpable;
  97. child_protected_data.process_group = my_protected_data.process_group;
  98. });
  99. });
  100. dbgln_if(FORK_DEBUG, "fork: child={}", child);
  101. // A child created via fork(2) inherits a copy of its parent's signal mask
  102. child_first_thread->update_signal_mask(Thread::current()->signal_mask());
  103. // A child process created via fork(2) inherits a copy of its parent's alternate signal stack settings.
  104. child_first_thread->m_alternative_signal_stack = Thread::current()->m_alternative_signal_stack;
  105. auto& child_regs = child_first_thread->m_regs;
  106. #if ARCH(X86_64)
  107. child_regs.rax = 0; // fork() returns 0 in the child :^)
  108. child_regs.rbx = regs.rbx;
  109. child_regs.rcx = regs.rcx;
  110. child_regs.rdx = regs.rdx;
  111. child_regs.rbp = regs.rbp;
  112. child_regs.rsp = regs.userspace_rsp;
  113. child_regs.rsi = regs.rsi;
  114. child_regs.rdi = regs.rdi;
  115. child_regs.r8 = regs.r8;
  116. child_regs.r9 = regs.r9;
  117. child_regs.r10 = regs.r10;
  118. child_regs.r11 = regs.r11;
  119. child_regs.r12 = regs.r12;
  120. child_regs.r13 = regs.r13;
  121. child_regs.r14 = regs.r14;
  122. child_regs.r15 = regs.r15;
  123. child_regs.rflags = regs.rflags;
  124. child_regs.rip = regs.rip;
  125. child_regs.cs = regs.cs;
  126. dbgln_if(FORK_DEBUG, "fork: child will begin executing at {:#04x}:{:p} with stack {:p}, kstack {:p}",
  127. child_regs.cs, child_regs.rip, child_regs.rsp, child_regs.rsp0);
  128. #elif ARCH(AARCH64)
  129. child_regs.x[0] = 0; // fork() returns 0 in the child :^)
  130. for (size_t i = 1; i < array_size(child_regs.x); ++i)
  131. child_regs.x[i] = regs.x[i];
  132. child_regs.spsr_el1 = regs.spsr_el1;
  133. child_regs.elr_el1 = regs.elr_el1;
  134. child_regs.sp_el0 = regs.sp_el0;
  135. #elif ARCH(RISCV64)
  136. (void)child_regs;
  137. (void)regs;
  138. TODO_RISCV64();
  139. #else
  140. # error Unknown architecture
  141. #endif
  142. TRY(address_space().with([&](auto& parent_space) {
  143. return child->address_space().with([&](auto& child_space) -> ErrorOr<void> {
  144. child_space->set_enforces_syscall_regions(parent_space->enforces_syscall_regions());
  145. for (auto& region : parent_space->region_tree().regions()) {
  146. dbgln_if(FORK_DEBUG, "fork: cloning Region '{}' @ {}", region.name(), region.vaddr());
  147. auto region_clone = TRY(region.try_clone());
  148. TRY(region_clone->map(child_space->page_directory(), Memory::ShouldFlushTLB::No));
  149. TRY(child_space->region_tree().place_specifically(*region_clone, region.range()));
  150. auto* child_region = region_clone.leak_ptr();
  151. if (&region == m_master_tls_region.unsafe_ptr()) {
  152. child->m_master_tls_region = TRY(child_region->try_make_weak_ptr());
  153. child->m_master_tls_size = m_master_tls_size;
  154. child->m_master_tls_alignment = m_master_tls_alignment;
  155. }
  156. }
  157. return {};
  158. });
  159. }));
  160. thread_finalizer_guard.disarm();
  161. remove_from_jail_process_list.disarm();
  162. Process::register_new(*child);
  163. PerformanceManager::add_process_created_event(*child);
  164. SpinlockLocker lock(g_scheduler_lock);
  165. child_first_thread->set_affinity(Thread::current()->affinity());
  166. child_first_thread->set_state(Thread::State::Runnable);
  167. auto child_pid = child->pid().value();
  168. return child_pid;
  169. }
  170. }