kernel: fork syscall

This commit is contained in:
Matheus
2023-05-16 19:42:18 -03:00
parent 0f95ff18f0
commit 0afbff3881
3 changed files with 80 additions and 6 deletions
+5 -2
View File
@@ -10,6 +10,7 @@ extern syscall_write
extern syscall_getpid
extern syscall_fstat
extern syscall_fstatat
extern syscall_fork
syscalltab:
dq syscall_print
dq syscall_mmap
@@ -22,7 +23,8 @@ dq syscall_write
dq syscall_getpid
dq syscall_fstat
dq syscall_fstatat
syscallcount equ 11
dq syscall_fork
syscallcount equ 12
section .text
global arch_syscall_entry
; on entry:
@@ -49,7 +51,7 @@ arch_syscall_entry:
; push context
push qword 0x1b ; user SS
push qword rcx
push qword rcx ; user RSP
add qword [rsp], 8 ; correct the old stack pointer to before the return address push
push r11 ; rflags is stored in r11
push qword 0x23 ; user CS
@@ -89,6 +91,7 @@ arch_syscall_entry:
mov rdx, rsi
mov rsi, rdi
mov rdi, rsp
add rdi, 8 ; context pointer is after r9 argument
sti
cmp rax, syscallcount
+63
View File
@@ -0,0 +1,63 @@
#include <kernel/syscalls.h>
#include <kernel/scheduler.h>
#include <errno.h>
#include <kernel/file.h>
#include <kernel/interrupt.h>
#include <logging.h>
syscallret_t syscall_fork(context_t *ctx) {
syscallret_t ret = {
.ret = -1,
.errno = 0
};
proc_t *proc = _cpu()->thread->proc;
proc_t *nproc = sched_newproc();
if (nproc == NULL) {
ret.errno = ENOMEM;
goto cleanup;
}
thread_t *nthread = sched_newthread((void *)CTX_IP(ctx), 16 * PAGE_SIZE, 1, nproc, (void *)CTX_SP(ctx));
if (nthread == NULL) {
ret.errno = ENOMEM;
goto cleanup;
}
nthread->vmmctx = vmm_fork(_cpu()->thread->vmmctx);
if (nthread->vmmctx == NULL) {
ret.errno = ENOMEM;
goto cleanup;
}
ret.errno = fd_clone(nproc);
if (ret.errno)
goto cleanup;
bool intstate = interrupt_set(false);
spinlock_acquire(&proc->lock);
nproc->parent = proc;
nproc->sibling = proc->child;
proc->child = nproc;
spinlock_release(&proc->lock);
interrupt_set(intstate);
nproc->umask = _cpu()->thread->proc->umask;
nproc->root = sched_getroot();
nproc->cwd = sched_getcwd();
ARCH_CONTEXT_THREADSAVE(nthread, ctx);
CTX_RET(&nthread->context) = 0;
CTX_ERRNO(&nthread->context) = 0;
ret.ret = nproc->pid;
sched_queue(nthread);
cleanup:
return ret;
}