fork.cpp 4.7 KB

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