fork.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/FileSystem/Custody.h>
  27. #include <Kernel/FileSystem/FileDescription.h>
  28. #include <Kernel/Process.h>
  29. #include <Kernel/SharedBuffer.h>
  30. #include <Kernel/VM/Region.h>
  31. //#define FORK_DEBUG
  32. namespace Kernel {
  33. pid_t Process::sys$fork(RegisterState& regs)
  34. {
  35. REQUIRE_PROMISE(proc);
  36. RefPtr<Thread> child_first_thread;
  37. 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));
  38. if (!child_first_thread)
  39. return -ENOMEM;
  40. child->m_root_directory = m_root_directory;
  41. child->m_root_directory_relative_to_global_root = m_root_directory_relative_to_global_root;
  42. child->m_promises = m_promises;
  43. child->m_execpromises = m_execpromises;
  44. child->m_veil_state = m_veil_state;
  45. child->m_unveiled_paths = m_unveiled_paths;
  46. child->m_fds = m_fds;
  47. child->m_sid = m_sid;
  48. child->m_pg = m_pg;
  49. child->m_umask = m_umask;
  50. #ifdef FORK_DEBUG
  51. dbg() << "fork: child=" << child;
  52. #endif
  53. child->m_extra_gids = m_extra_gids;
  54. auto& child_tss = child_first_thread->m_tss;
  55. child_tss.eax = 0; // fork() returns 0 in the child :^)
  56. child_tss.ebx = regs.ebx;
  57. child_tss.ecx = regs.ecx;
  58. child_tss.edx = regs.edx;
  59. child_tss.ebp = regs.ebp;
  60. child_tss.esp = regs.userspace_esp;
  61. child_tss.esi = regs.esi;
  62. child_tss.edi = regs.edi;
  63. child_tss.eflags = regs.eflags;
  64. child_tss.eip = regs.eip;
  65. child_tss.cs = regs.cs;
  66. child_tss.ds = regs.ds;
  67. child_tss.es = regs.es;
  68. child_tss.fs = regs.fs;
  69. child_tss.gs = regs.gs;
  70. child_tss.ss = regs.userspace_ss;
  71. #ifdef FORK_DEBUG
  72. 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);
  73. #endif
  74. SharedBuffer::share_all_shared_buffers(*this, *child);
  75. {
  76. ScopedSpinLock lock(m_lock);
  77. for (auto& region : m_regions) {
  78. #ifdef FORK_DEBUG
  79. dbg() << "fork: cloning Region{" << &region << "} '" << region.name() << "' @ " << region.vaddr();
  80. #endif
  81. auto& child_region = child->add_region(region.clone());
  82. child_region.map(child->page_directory());
  83. if (&region == m_master_tls_region.unsafe_ptr())
  84. child->m_master_tls_region = child_region;
  85. }
  86. ScopedSpinLock processes_lock(g_processes_lock);
  87. g_processes->prepend(child);
  88. child->ref(); // This reference will be dropped by Process::reap
  89. }
  90. ScopedSpinLock lock(g_scheduler_lock);
  91. child_first_thread->set_affinity(Thread::current()->affinity());
  92. child_first_thread->set_state(Thread::State::Runnable);
  93. return child->pid().value();
  94. }
  95. }