2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2021-06-23 19:54:41 +00:00
|
|
|
#include <Kernel/Panic.h>
|
2021-05-07 08:38:50 +00:00
|
|
|
#include <Kernel/PerformanceManager.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/VM/Region.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$fork(RegisterState& regs)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(proc);
|
2020-09-27 14:53:35 +00:00
|
|
|
RefPtr<Thread> child_first_thread;
|
2021-05-14 11:55:43 +00:00
|
|
|
auto child = Process::create(child_first_thread, m_name, uid(), gid(), pid(), m_is_kernel_process, m_cwd, m_executable, m_tty, this);
|
|
|
|
if (!child || !child_first_thread)
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENOMEM;
|
2020-07-30 21:38:15 +00:00
|
|
|
child->m_root_directory = m_root_directory;
|
|
|
|
child->m_root_directory_relative_to_global_root = m_root_directory_relative_to_global_root;
|
|
|
|
child->m_veil_state = m_veil_state;
|
2020-12-26 10:24:34 +00:00
|
|
|
child->m_unveiled_paths = m_unveiled_paths.deep_copy();
|
2020-07-30 21:38:15 +00:00
|
|
|
child->m_fds = m_fds;
|
2021-03-11 12:13:05 +00:00
|
|
|
child->m_pg = m_pg;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-03-10 18:59:46 +00:00
|
|
|
{
|
2021-03-11 12:23:23 +00:00
|
|
|
ProtectedDataMutationScope scope { *child };
|
2021-03-11 12:13:05 +00:00
|
|
|
child->m_promises = m_promises;
|
|
|
|
child->m_execpromises = m_execpromises;
|
|
|
|
child->m_has_promises = m_has_promises;
|
|
|
|
child->m_has_execpromises = m_has_execpromises;
|
|
|
|
child->m_sid = m_sid;
|
|
|
|
child->m_extra_gids = m_extra_gids;
|
2021-03-11 12:23:23 +00:00
|
|
|
child->m_umask = m_umask;
|
2021-03-11 12:28:50 +00:00
|
|
|
child->m_signal_trampoline = m_signal_trampoline;
|
2021-03-11 13:34:53 +00:00
|
|
|
child->m_dumpable = m_dumpable;
|
2021-03-10 18:59:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(FORK_DEBUG, "fork: child={}", child);
|
2021-02-08 14:45:40 +00:00
|
|
|
child->space().set_enforces_syscall_regions(space().enforces_syscall_regions());
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-06-23 19:54:41 +00:00
|
|
|
#if ARCH(I386)
|
2021-06-26 17:57:16 +00:00
|
|
|
auto& child_regs = child_first_thread->m_regs;
|
|
|
|
child_regs.eax = 0; // fork() returns 0 in the child :^)
|
|
|
|
child_regs.ebx = regs.ebx;
|
|
|
|
child_regs.ecx = regs.ecx;
|
|
|
|
child_regs.edx = regs.edx;
|
|
|
|
child_regs.ebp = regs.ebp;
|
|
|
|
child_regs.esp = regs.userspace_esp;
|
|
|
|
child_regs.esi = regs.esi;
|
|
|
|
child_regs.edi = regs.edi;
|
|
|
|
child_regs.eflags = regs.eflags;
|
|
|
|
child_regs.eip = regs.eip;
|
|
|
|
child_regs.cs = regs.cs;
|
|
|
|
child_regs.ds = regs.ds;
|
|
|
|
child_regs.es = regs.es;
|
|
|
|
child_regs.fs = regs.fs;
|
|
|
|
child_regs.gs = regs.gs;
|
|
|
|
child_regs.ss = regs.userspace_ss;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-06-26 17:57:16 +00:00
|
|
|
dbgln_if(FORK_DEBUG, "fork: child will begin executing at {:04x}:{:08x} with stack {:04x}:{:08x}, kstack {:04x}:{:08x}",
|
|
|
|
child_regs.cs, child_regs.eip, child_regs.ss, child_regs.esp, child_regs.ss0, child_regs.esp0);
|
2021-06-23 19:54:41 +00:00
|
|
|
#else
|
2021-06-29 00:56:07 +00:00
|
|
|
auto& child_regs = child_first_thread->m_regs;
|
|
|
|
child_regs.rax = 0; // fork() returns 0 in the child :^)
|
|
|
|
child_regs.rbx = regs.rbx;
|
|
|
|
child_regs.rcx = regs.rcx;
|
|
|
|
child_regs.rdx = regs.rdx;
|
|
|
|
child_regs.rbp = regs.rbp;
|
|
|
|
child_regs.rsp = regs.userspace_rsp;
|
|
|
|
child_regs.rsi = regs.rsi;
|
|
|
|
child_regs.rdi = regs.rdi;
|
|
|
|
child_regs.r8 = regs.r8;
|
|
|
|
child_regs.r9 = regs.r9;
|
|
|
|
child_regs.r10 = regs.r10;
|
|
|
|
child_regs.r11 = regs.r11;
|
|
|
|
child_regs.r12 = regs.r12;
|
|
|
|
child_regs.r13 = regs.r13;
|
|
|
|
child_regs.r14 = regs.r14;
|
|
|
|
child_regs.r15 = regs.r15;
|
|
|
|
child_regs.rflags = regs.rflags;
|
|
|
|
child_regs.rip = regs.rip;
|
|
|
|
child_regs.cs = regs.cs;
|
|
|
|
|
|
|
|
dbgln_if(FORK_DEBUG, "fork: child will begin executing at {:04x}:{:16x} with stack {:08x}, kstack {:08x}",
|
|
|
|
child_regs.cs, child_regs.rip, child_regs.rsp, child_regs.rsp0);
|
2021-06-23 19:54:41 +00:00
|
|
|
#endif
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-10-26 02:22:59 +00:00
|
|
|
{
|
2021-02-08 14:45:40 +00:00
|
|
|
ScopedSpinLock lock(space().get_lock());
|
|
|
|
for (auto& region : space().regions()) {
|
2021-04-06 23:20:29 +00:00
|
|
|
dbgln_if(FORK_DEBUG, "fork: cloning Region({}) '{}' @ {}", region, region->name(), region->vaddr());
|
|
|
|
auto region_clone = region->clone(*child);
|
2020-09-05 03:12:25 +00:00
|
|
|
if (!region_clone) {
|
2021-01-09 17:51:44 +00:00
|
|
|
dbgln("fork: Cannot clone region, insufficient memory");
|
2020-09-05 03:12:25 +00:00
|
|
|
// TODO: tear down new process?
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENOMEM;
|
2020-09-05 03:12:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 14:45:40 +00:00
|
|
|
auto& child_region = child->space().add_region(region_clone.release_nonnull());
|
2021-03-03 21:45:18 +00:00
|
|
|
child_region.map(child->space().page_directory(), ShouldFlushTLB::No);
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-04-06 23:20:29 +00:00
|
|
|
if (region == m_master_tls_region.unsafe_ptr())
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 22:26:13 +00:00
|
|
|
child->m_master_tls_region = child_region;
|
2020-10-26 02:22:59 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
|
Kernel: Introduce the new ProcFS design
The new ProcFS design consists of two main parts:
1. The representative ProcFS class, which is derived from the FS class.
The ProcFS and its inodes are much more lean - merely 3 classes to
represent the common type of inodes - regular files, symbolic links and
directories. They're backed by a ProcFSExposedComponent object, which
is responsible for the functional operation behind the scenes.
2. The backend of the ProcFS - the ProcFSComponentsRegistrar class
and all derived classes from the ProcFSExposedComponent class. These
together form the entire backend and handle all the functions you can
expect from the ProcFS.
The ProcFSExposedComponent derived classes split to 3 types in the
manner of lifetime in the kernel:
1. Persistent objects - this category includes all basic objects, like
the root folder, /proc/bus folder, main blob files in the root folders,
etc. These objects are persistent and cannot die ever.
2. Semi-persistent objects - this category includes all PID folders,
and subdirectories to the PID folders. It also includes exposed objects
like the unveil JSON'ed blob. These object are persistent as long as the
the responsible process they represent is still alive.
3. Dynamic objects - this category includes files in the subdirectories
of a PID folder, like /proc/PID/fd/* or /proc/PID/stacks/*. Essentially,
these objects are always created dynamically and when no longer in need
after being used, they're deallocated.
Nevertheless, the new allocated backend objects and inodes try to use
the same InodeIndex if possible - this might change only when a thread
dies and a new thread is born with a new thread stack, or when a file
descriptor is closed and a new one within the same file descriptor
number is opened. This is needed to actually be able to do something
useful with these objects.
The new design assures that many ProcFS instances can be used at once,
with one backend for usage for all instances.
2021-06-12 01:23:58 +00:00
|
|
|
Process::register_new(*child);
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 08:38:50 +00:00
|
|
|
PerformanceManager::add_process_created_event(*child);
|
2021-04-25 21:42:36 +00:00
|
|
|
|
2020-10-26 02:22:59 +00:00
|
|
|
ScopedSpinLock lock(g_scheduler_lock);
|
2020-09-07 14:31:00 +00:00
|
|
|
child_first_thread->set_affinity(Thread::current()->affinity());
|
|
|
|
child_first_thread->set_state(Thread::State::Runnable);
|
2020-09-05 21:52:14 +00:00
|
|
|
|
|
|
|
auto child_pid = child->pid().value();
|
|
|
|
// We need to leak one reference so we don't destroy the Process,
|
|
|
|
// which will be dropped by Process::reap
|
|
|
|
(void)child.leak_ref();
|
|
|
|
return child_pid;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|