fork.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Debug.h>
  7. #include <Kernel/FileSystem/Custody.h>
  8. #include <Kernel/FileSystem/FileDescription.h>
  9. #include <Kernel/Panic.h>
  10. #include <Kernel/PerformanceManager.h>
  11. #include <Kernel/Process.h>
  12. #include <Kernel/VM/Region.h>
  13. namespace Kernel {
  14. KResultOr<pid_t> Process::sys$fork(RegisterState& regs)
  15. {
  16. REQUIRE_PROMISE(proc);
  17. RefPtr<Thread> child_first_thread;
  18. auto child = Process::create(child_first_thread, m_name, uid(), gid(), pid(), m_is_kernel_process, m_cwd, m_executable, m_tty, this);
  19. if (!child || !child_first_thread)
  20. return ENOMEM;
  21. child->m_root_directory = m_root_directory;
  22. child->m_root_directory_relative_to_global_root = m_root_directory_relative_to_global_root;
  23. child->m_veil_state = m_veil_state;
  24. child->m_unveiled_paths = m_unveiled_paths.deep_copy();
  25. child->m_fds = m_fds;
  26. child->m_pg = m_pg;
  27. {
  28. ProtectedDataMutationScope scope { *child };
  29. child->m_promises = m_promises;
  30. child->m_execpromises = m_execpromises;
  31. child->m_has_promises = m_has_promises;
  32. child->m_has_execpromises = m_has_execpromises;
  33. child->m_sid = m_sid;
  34. child->m_extra_gids = m_extra_gids;
  35. child->m_umask = m_umask;
  36. child->m_signal_trampoline = m_signal_trampoline;
  37. child->m_dumpable = m_dumpable;
  38. }
  39. dbgln_if(FORK_DEBUG, "fork: child={}", child);
  40. child->space().set_enforces_syscall_regions(space().enforces_syscall_regions());
  41. #if ARCH(I386)
  42. auto& child_tss = child_first_thread->m_tss;
  43. child_tss.eax = 0; // fork() returns 0 in the child :^)
  44. child_tss.ebx = regs.ebx;
  45. child_tss.ecx = regs.ecx;
  46. child_tss.edx = regs.edx;
  47. child_tss.ebp = regs.ebp;
  48. child_tss.esp = regs.userspace_esp;
  49. child_tss.esi = regs.esi;
  50. child_tss.edi = regs.edi;
  51. child_tss.eflags = regs.eflags;
  52. child_tss.eip = regs.eip;
  53. child_tss.cs = regs.cs;
  54. child_tss.ds = regs.ds;
  55. child_tss.es = regs.es;
  56. child_tss.fs = regs.fs;
  57. child_tss.gs = regs.gs;
  58. child_tss.ss = regs.userspace_ss;
  59. dbgln_if(FORK_DEBUG, "fork: child will begin executing at {:04x}:{:08x} with stack {:04x}:{:08x}, kstack {:04x}:{:08x}", child_tss.cs, child_tss.eip, child_tss.ss, child_tss.esp, child_tss.ss0, child_tss.esp0);
  60. #else
  61. (void)regs;
  62. PANIC("Process::sys$fork() not implemented.");
  63. #endif
  64. {
  65. ScopedSpinLock lock(space().get_lock());
  66. for (auto& region : space().regions()) {
  67. dbgln_if(FORK_DEBUG, "fork: cloning Region({}) '{}' @ {}", region, region->name(), region->vaddr());
  68. auto region_clone = region->clone(*child);
  69. if (!region_clone) {
  70. dbgln("fork: Cannot clone region, insufficient memory");
  71. // TODO: tear down new process?
  72. return ENOMEM;
  73. }
  74. auto& child_region = child->space().add_region(region_clone.release_nonnull());
  75. child_region.map(child->space().page_directory(), ShouldFlushTLB::No);
  76. if (region == m_master_tls_region.unsafe_ptr())
  77. child->m_master_tls_region = child_region;
  78. }
  79. ScopedSpinLock processes_lock(g_processes_lock);
  80. g_processes->prepend(*child);
  81. }
  82. PerformanceManager::add_process_created_event(*child);
  83. ScopedSpinLock lock(g_scheduler_lock);
  84. child_first_thread->set_affinity(Thread::current()->affinity());
  85. child_first_thread->set_state(Thread::State::Runnable);
  86. auto child_pid = child->pid().value();
  87. // We need to leak one reference so we don't destroy the Process,
  88. // which will be dropped by Process::reap
  89. (void)child.leak_ref();
  90. return child_pid;
  91. }
  92. }