feat(kernel/syscalls): implement sys_fork()

Signed-off-by: EnderIce2 <enderice2@protonmail.com>
This commit is contained in:
2025-03-07 01:31:50 +00:00
parent 2c02da7eaf
commit 1ff62e22bf
2 changed files with 264 additions and 114 deletions

View File

@ -175,120 +175,14 @@ static off_t sys_seek(SysFrm *Frame, int fd, off_t offset, int whence)
return fdt->usr_lseek(fd, offset, whence);
}
static __noreturn void sys_exit(SysFrm *Frame, int status)
{
TCB *t = thisThread;
{
CriticalSection cs;
trace("Userspace thread %s(%d) exited with code %d (%#x)",
t->Name,
t->ID, status,
status < 0 ? -status : status);
t->SetState(Tasking::Zombie);
t->SetExitCode(status);
}
while (true)
t->GetContext()->Yield();
__builtin_unreachable();
}
static pid_t sys_fork(SysFrm *Frame) { return -ENOSYS; }
static int sys_execve(SysFrm *Frame, const char *pathname, char *const argv[], char *const envp[]) { return -ENOSYS; }
static pid_t sys_getpid(SysFrm *Frame) { return -ENOSYS; }
static pid_t sys_getppid(SysFrm *Frame) { return -ENOSYS; }
static pid_t sys_waitpid(pid_t pid, int *wstatus, int options) { return -ENOSYS; }
static int sys_kill(SysFrm *Frame, pid_t pid, int sig)
{
PCB *pcb = thisProcess->GetContext()->GetProcessByID(pid);
if (!pcb)
return -ESRCH;
/* TODO: Check permissions */
if (sig == 0)
return 0;
if (pid == 0)
{
bool found = false;
foreach (auto proc in pcb->GetContext()->GetProcessList())
{
if (proc->Security.ProcessGroupID == thisProcess->Security.ProcessGroupID)
{
debug("Sending signal %d to %s(%d)", sig, proc->Name, proc->ID);
proc->SendSignal(sig);
found = true;
}
}
if (!found)
return -ESRCH;
return 0;
}
if (pid == -1)
{
fixme("Sending signal %d to all processes except init", sig);
return -ENOSYS;
}
if (pid < -1)
{
fixme("Sending signal %d to process group %d", sig, pid);
return -ENOSYS;
}
return pcb->SendSignal(sig);
}
static int sys_prctl(SysFrm *Frame, prctl_options_t option, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4)
{
PCB *pcb = thisProcess;
Memory::VirtualMemoryArea *vma = pcb->vma;
switch (option)
{
case __SYS_GET_GS:
{
auto arg = vma->UserCheckAndGetAddress((void *)arg1);
if (arg == nullptr)
return -EFAULT;
#if defined(__amd64__) || defined(__i386__)
*r_cst(uintptr_t *, arg) = CPU::x86::rdmsr(CPU::x86::MSRID::MSR_GS_BASE);
#endif
return 0;
}
case __SYS_SET_GS:
{
#if defined(__amd64__) || defined(__i386__)
CPU::x86::wrmsr(CPU::x86::MSRID::MSR_GS_BASE, arg1);
#endif
return 0;
}
case __SYS_GET_FS:
{
auto arg = vma->UserCheckAndGetAddress((void *)arg1);
if (arg == nullptr)
return -EFAULT;
#if defined(__amd64__) || defined(__i386__)
*r_cst(uintptr_t *, arg) = CPU::x86::rdmsr(CPU::x86::MSRID::MSR_FS_BASE);
#endif
return 0;
}
case __SYS_SET_FS:
{
#if defined(__amd64__) || defined(__i386__)
CPU::x86::wrmsr(CPU::x86::MSRID::MSR_FS_BASE, arg1);
#endif
return 0;
}
default:
return -EINVAL;
}
}
__noreturn void sys_exit(SysFrm *Frame, int status);
pid_t sys_fork(SysFrm *Frame);
int sys_execve(SysFrm *Frame, const char *pathname, char *const argv[], char *const envp[]);
pid_t sys_getpid(SysFrm *Frame);
pid_t sys_getppid(SysFrm *Frame);
pid_t sys_waitpid(pid_t pid, int *wstatus, int options);
int sys_kill(SysFrm *Frame, pid_t pid, int sig);
int sys_prctl(SysFrm *Frame, prctl_options_t option, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4);
int sys_brk(SysFrm *Frame, void *end_data);
void *sys_mmap(SysFrm *Frame, void *addr, size_t length, int prot, int flags, int fd, off_t offset);