mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-05-28 15:34:31 +00:00
linux: Implement vfork() syscall
This commit is contained in:
parent
0edd84c8a8
commit
8b442d14e3
@ -1495,8 +1495,89 @@ static pid_t linux_fork(SysFrm *sf)
|
|||||||
|
|
||||||
static pid_t linux_vfork(SysFrm *sf)
|
static pid_t linux_vfork(SysFrm *sf)
|
||||||
{
|
{
|
||||||
stub;
|
TCB *Thread = thisThread;
|
||||||
return -linux_ENOSYS;
|
PCB *Parent = Thread->Parent;
|
||||||
|
|
||||||
|
PCB *NewProcess =
|
||||||
|
TaskManager->CreateProcess(Parent, Parent->Name,
|
||||||
|
Parent->Security.ExecutionMode,
|
||||||
|
true);
|
||||||
|
if (unlikely(!NewProcess))
|
||||||
|
{
|
||||||
|
error("Failed to create process for vfork");
|
||||||
|
return -linux_EAGAIN;
|
||||||
|
}
|
||||||
|
|
||||||
|
NewProcess->Security.ProcessGroupID = Parent->Security.ProcessGroupID;
|
||||||
|
NewProcess->Security.SessionID = Parent->Security.SessionID;
|
||||||
|
|
||||||
|
NewProcess->PageTable = Parent->PageTable;
|
||||||
|
delete NewProcess->vma;
|
||||||
|
NewProcess->vma = Parent->vma;
|
||||||
|
delete NewProcess->ProgramBreak;
|
||||||
|
NewProcess->ProgramBreak = NewProcess->ProgramBreak;
|
||||||
|
NewProcess->FileDescriptors->Fork(Parent->FileDescriptors);
|
||||||
|
NewProcess->Executable = Parent->Executable;
|
||||||
|
NewProcess->CWD = Parent->CWD;
|
||||||
|
NewProcess->FileCreationMask = Parent->FileCreationMask;
|
||||||
|
|
||||||
|
TCB *NewThread =
|
||||||
|
TaskManager->CreateThread(NewProcess,
|
||||||
|
0,
|
||||||
|
nullptr,
|
||||||
|
nullptr,
|
||||||
|
std::vector<AuxiliaryVector>(),
|
||||||
|
Thread->Info.Architecture,
|
||||||
|
Thread->Info.Compatibility,
|
||||||
|
true);
|
||||||
|
if (!NewThread)
|
||||||
|
{
|
||||||
|
error("Failed to create thread for fork");
|
||||||
|
delete NewProcess;
|
||||||
|
return -linux_EAGAIN;
|
||||||
|
}
|
||||||
|
NewThread->Rename(Thread->Name);
|
||||||
|
|
||||||
|
TaskManager->UpdateFrame();
|
||||||
|
|
||||||
|
NewThread->FPU = Thread->FPU;
|
||||||
|
delete NewThread->Stack;
|
||||||
|
NewThread->Stack = Thread->Stack;
|
||||||
|
NewThread->Info.Architecture = Thread->Info.Architecture;
|
||||||
|
NewThread->Info.Compatibility = Thread->Info.Compatibility;
|
||||||
|
NewThread->Security.IsCritical = Thread->Security.IsCritical;
|
||||||
|
NewThread->Registers = Thread->Registers;
|
||||||
|
#if defined(a64)
|
||||||
|
NewThread->Registers.rip = (uintptr_t)__LinuxForkReturn;
|
||||||
|
/* For sysretq */
|
||||||
|
NewThread->Registers.rdi = (uintptr_t)NewProcess->PageTable;
|
||||||
|
NewThread->Registers.rcx = sf->ReturnAddress;
|
||||||
|
NewThread->Registers.r8 = sf->StackPointer;
|
||||||
|
#else
|
||||||
|
#warning "sys_fork not implemented for other platforms"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef a86
|
||||||
|
NewThread->GSBase = NewThread->ShadowGSBase;
|
||||||
|
NewThread->ShadowGSBase = Thread->ShadowGSBase;
|
||||||
|
NewThread->FSBase = Thread->FSBase;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
debug("ret addr: %#lx, stack: %#lx ip: %#lx", sf->ReturnAddress,
|
||||||
|
sf->StackPointer, (uintptr_t)__LinuxForkReturn);
|
||||||
|
debug("Forked thread \"%s\"(%d) to \"%s\"(%d)",
|
||||||
|
Thread->Name, Thread->ID,
|
||||||
|
NewThread->Name, NewThread->ID);
|
||||||
|
|
||||||
|
{
|
||||||
|
CriticalSection cs;
|
||||||
|
Thread->SetState(Tasking::Frozen);
|
||||||
|
NewProcess->Linux.vforked = true;
|
||||||
|
NewProcess->Linux.CallingThread = Thread;
|
||||||
|
NewThread->SetState(Tasking::Ready);
|
||||||
|
}
|
||||||
|
Parent->GetContext()->Yield();
|
||||||
|
return (int)NewProcess->ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
__no_sanitize("undefined") static int linux_execve(SysFrm *sf, const char *pathname,
|
__no_sanitize("undefined") static int linux_execve(SysFrm *sf, const char *pathname,
|
||||||
@ -1505,7 +1586,8 @@ __no_sanitize("undefined") static int linux_execve(SysFrm *sf, const char *pathn
|
|||||||
{
|
{
|
||||||
/* FIXME: exec doesn't follow the UNIX standard
|
/* FIXME: exec doesn't follow the UNIX standard
|
||||||
The pid, open files, etc. should be preserved */
|
The pid, open files, etc. should be preserved */
|
||||||
PCB *pcb = thisProcess;
|
TCB *tcb = thisThread;
|
||||||
|
PCB *pcb = tcb->Parent;
|
||||||
Memory::VirtualMemoryArea *vma = pcb->vma;
|
Memory::VirtualMemoryArea *vma = pcb->vma;
|
||||||
|
|
||||||
auto pPathname = vma->UserCheckAndGetAddress(pathname, PAGE_SIZE);
|
auto pPathname = vma->UserCheckAndGetAddress(pathname, PAGE_SIZE);
|
||||||
@ -1651,6 +1733,19 @@ __no_sanitize("undefined") static int linux_execve(SysFrm *sf, const char *pathn
|
|||||||
(char *const *)safeEnvp));
|
(char *const *)safeEnvp));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pcb->Linux.vforked)
|
||||||
|
{
|
||||||
|
CriticalSection cs;
|
||||||
|
|
||||||
|
pcb->Linux.CallingThread->SetState(Tasking::Ready);
|
||||||
|
pcb->Linux.vforked = false;
|
||||||
|
|
||||||
|
pcb->PageTable = KernelPageTable->Fork();
|
||||||
|
pcb->vma = new Memory::VirtualMemoryArea(pcb->PageTable);
|
||||||
|
pcb->ProgramBreak = new Memory::ProgramBreak(pcb->PageTable, pcb->vma);
|
||||||
|
// tcb->Stack = new Memory::StackGuard(true, pcb->vma);
|
||||||
|
}
|
||||||
|
|
||||||
int ret = Execute::Spawn((char *)pPathname,
|
int ret = Execute::Spawn((char *)pPathname,
|
||||||
(const char **)safeArgv,
|
(const char **)safeArgv,
|
||||||
(const char **)safeEnvp,
|
(const char **)safeEnvp,
|
||||||
@ -1682,14 +1777,18 @@ __no_sanitize("undefined") static int linux_execve(SysFrm *sf, const char *pathn
|
|||||||
static __noreturn void linux_exit(SysFrm *, int status)
|
static __noreturn void linux_exit(SysFrm *, int status)
|
||||||
{
|
{
|
||||||
TCB *t = thisThread;
|
TCB *t = thisThread;
|
||||||
|
{
|
||||||
|
CriticalSection cs;
|
||||||
|
trace("Userspace thread %s(%d) exited with code %d (%#x)",
|
||||||
|
t->Name,
|
||||||
|
t->ID, status,
|
||||||
|
status < 0 ? -status : status);
|
||||||
|
|
||||||
trace("Userspace thread %s(%d) exited with code %d (%#x)",
|
t->SetState(Tasking::Zombie);
|
||||||
t->Name,
|
t->SetExitCode(status);
|
||||||
t->ID, status,
|
if (t->Parent->Linux.vforked)
|
||||||
status < 0 ? -status : status);
|
t->Parent->Linux.CallingThread->SetState(Tasking::Ready);
|
||||||
|
}
|
||||||
t->SetState(Tasking::Zombie);
|
|
||||||
t->SetExitCode(status);
|
|
||||||
while (true)
|
while (true)
|
||||||
t->GetContext()->Yield();
|
t->GetContext()->Yield();
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user