fork.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. 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, m_uid, m_gid, m_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_promises = m_promises;
  42. child->m_execpromises = m_execpromises;
  43. child->m_has_promises = m_has_promises;
  44. child->m_has_execpromises = m_has_execpromises;
  45. child->m_veil_state = m_veil_state;
  46. child->m_unveiled_paths = m_unveiled_paths.deep_copy();
  47. child->m_fds = m_fds;
  48. child->m_sid = m_sid;
  49. child->m_pg = m_pg;
  50. child->m_umask = m_umask;
  51. dbgln<FORK_DEBUG>("fork: child={}", child);
  52. child->m_extra_gids = m_extra_gids;
  53. auto& child_tss = child_first_thread->m_tss;
  54. child_tss.eax = 0; // fork() returns 0 in the child :^)
  55. child_tss.ebx = regs.ebx;
  56. child_tss.ecx = regs.ecx;
  57. child_tss.edx = regs.edx;
  58. child_tss.ebp = regs.ebp;
  59. child_tss.esp = regs.userspace_esp;
  60. child_tss.esi = regs.esi;
  61. child_tss.edi = regs.edi;
  62. child_tss.eflags = regs.eflags;
  63. child_tss.eip = regs.eip;
  64. child_tss.cs = regs.cs;
  65. child_tss.ds = regs.ds;
  66. child_tss.es = regs.es;
  67. child_tss.fs = regs.fs;
  68. child_tss.gs = regs.gs;
  69. child_tss.ss = regs.userspace_ss;
  70. #if FORK_DEBUG
  71. dbgln("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);
  72. #endif
  73. {
  74. ScopedSpinLock lock(m_lock);
  75. for (auto& region : m_regions) {
  76. dbgln<FORK_DEBUG>("fork: cloning Region({}) '{}' @ {}", &region, region.name(), region.vaddr());
  77. auto region_clone = region.clone(*child);
  78. if (!region_clone) {
  79. dbgln("fork: Cannot clone region, insufficient memory");
  80. // TODO: tear down new process?
  81. return -ENOMEM;
  82. }
  83. auto& child_region = child->add_region(region_clone.release_nonnull());
  84. child_region.map(child->page_directory());
  85. if (&region == m_master_tls_region.unsafe_ptr())
  86. child->m_master_tls_region = child_region;
  87. }
  88. ScopedSpinLock processes_lock(g_processes_lock);
  89. g_processes->prepend(child);
  90. }
  91. ScopedSpinLock lock(g_scheduler_lock);
  92. child_first_thread->set_affinity(Thread::current()->affinity());
  93. child_first_thread->set_state(Thread::State::Runnable);
  94. auto child_pid = child->pid().value();
  95. // We need to leak one reference so we don't destroy the Process,
  96. // which will be dropped by Process::reap
  97. (void)child.leak_ref();
  98. return child_pid;
  99. }
  100. }