fork.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/Process.h>
  10. #include <Kernel/VM/Region.h>
  11. namespace Kernel {
  12. KResultOr<pid_t> Process::sys$fork(RegisterState& regs)
  13. {
  14. REQUIRE_PROMISE(proc);
  15. RefPtr<Thread> child_first_thread;
  16. auto child = adopt_ref(*new Process(child_first_thread, m_name, uid(), gid(), pid(), m_is_kernel_process, m_cwd, m_executable, m_tty, this));
  17. if (!child_first_thread)
  18. return ENOMEM;
  19. child->m_root_directory = m_root_directory;
  20. child->m_root_directory_relative_to_global_root = m_root_directory_relative_to_global_root;
  21. child->m_veil_state = m_veil_state;
  22. child->m_unveiled_paths = m_unveiled_paths.deep_copy();
  23. child->m_fds = m_fds;
  24. child->m_pg = m_pg;
  25. {
  26. ProtectedDataMutationScope scope { *child };
  27. child->m_promises = m_promises;
  28. child->m_execpromises = m_execpromises;
  29. child->m_has_promises = m_has_promises;
  30. child->m_has_execpromises = m_has_execpromises;
  31. child->m_sid = m_sid;
  32. child->m_extra_gids = m_extra_gids;
  33. child->m_umask = m_umask;
  34. child->m_signal_trampoline = m_signal_trampoline;
  35. child->m_dumpable = m_dumpable;
  36. }
  37. dbgln_if(FORK_DEBUG, "fork: child={}", child);
  38. child->space().set_enforces_syscall_regions(space().enforces_syscall_regions());
  39. auto& child_tss = child_first_thread->m_tss;
  40. child_tss.eax = 0; // fork() returns 0 in the child :^)
  41. child_tss.ebx = regs.ebx;
  42. child_tss.ecx = regs.ecx;
  43. child_tss.edx = regs.edx;
  44. child_tss.ebp = regs.ebp;
  45. child_tss.esp = regs.userspace_esp;
  46. child_tss.esi = regs.esi;
  47. child_tss.edi = regs.edi;
  48. child_tss.eflags = regs.eflags;
  49. child_tss.eip = regs.eip;
  50. child_tss.cs = regs.cs;
  51. child_tss.ds = regs.ds;
  52. child_tss.es = regs.es;
  53. child_tss.fs = regs.fs;
  54. child_tss.gs = regs.gs;
  55. child_tss.ss = regs.userspace_ss;
  56. 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);
  57. {
  58. ScopedSpinLock lock(space().get_lock());
  59. for (auto& region : space().regions()) {
  60. dbgln_if(FORK_DEBUG, "fork: cloning Region({}) '{}' @ {}", region, region->name(), region->vaddr());
  61. auto region_clone = region->clone(*child);
  62. if (!region_clone) {
  63. dbgln("fork: Cannot clone region, insufficient memory");
  64. // TODO: tear down new process?
  65. return ENOMEM;
  66. }
  67. auto& child_region = child->space().add_region(region_clone.release_nonnull());
  68. child_region.map(child->space().page_directory(), ShouldFlushTLB::No);
  69. if (region == m_master_tls_region.unsafe_ptr())
  70. child->m_master_tls_region = child_region;
  71. }
  72. ScopedSpinLock processes_lock(g_processes_lock);
  73. g_processes->prepend(child);
  74. }
  75. ScopedSpinLock lock(g_scheduler_lock);
  76. child_first_thread->set_affinity(Thread::current()->affinity());
  77. child_first_thread->set_state(Thread::State::Runnable);
  78. auto child_pid = child->pid().value();
  79. // We need to leak one reference so we don't destroy the Process,
  80. // which will be dropped by Process::reap
  81. (void)child.leak_ref();
  82. return child_pid;
  83. }
  84. }