From e672f3994bfd4f903fc7ee05be307c800ff9db91 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 15 Oct 2022 18:19:47 +0200 Subject: [PATCH] libc: Add support for fork() That doesn't mean it works. Why doesn't it work?? Oh well... --- apps/src/init.c | 12 ++++++++---- kernel/src/trace/StackTracer.cpp | 7 +++++++ libs/libc/include/luna/syscall.h | 1 + libs/libc/include/unistd.h | 3 ++- libs/libc/src/unistd.cpp | 4 +++- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/apps/src/init.c b/apps/src/init.c index f83fd1f7..29f51333 100644 --- a/apps/src/init.c +++ b/apps/src/init.c @@ -141,9 +141,13 @@ int main() fclose(new_stderr); - execv("/bin/sym", NULL); + pid_t child = fork(); + if (child == 0) + { + msleep(500); + printf("I am the child, who is my parent?\n"); + } + else { printf("My child is %ld!", child); } - perror("execv"); // If we're here, execv failed - - return 1; + return 0; } diff --git a/kernel/src/trace/StackTracer.cpp b/kernel/src/trace/StackTracer.cpp index ea305c52..39a38e7f 100644 --- a/kernel/src/trace/StackTracer.cpp +++ b/kernel/src/trace/StackTracer.cpp @@ -1,5 +1,6 @@ #include "trace/StackTracer.h" #include "memory/Memory.h" +#include "memory/VMM.h" #include "std/stdio.h" #include "trace/Resolve.h" @@ -20,6 +21,11 @@ typedef struct stackframe void StackTracer::trace() { + if (!VMM::is_using_kernel_address_space()) + { + VMM::switch_back_to_kernel_address_space(); + VMM::apply_address_space(); + } stackframe* frame = (stackframe*)m_base_pointer; while (frame && frame->instruction) { @@ -27,6 +33,7 @@ void StackTracer::trace() get_symbol_name(frame->instruction, symbol_name, sizeof(symbol_name)); printf("%lx: %s\n", frame->instruction, symbol_name); frame = frame->next; + if (VMM::get_physical((uint64_t)frame) == (uint64_t)-1) return; } } diff --git a/libs/libc/include/luna/syscall.h b/libs/libc/include/luna/syscall.h index 6d92d320..b706db8d 100644 --- a/libs/libc/include/luna/syscall.h +++ b/libs/libc/include/luna/syscall.h @@ -17,6 +17,7 @@ #define SYS_fcntl 13 #define SYS_mprotect 14 #define SYS_clock 15 +#define SYS_fork 16 #ifndef __want_syscalls #ifdef __cplusplus diff --git a/libs/libc/include/unistd.h b/libs/libc/include/unistd.h index 80add4cb..3e5a00a0 100644 --- a/libs/libc/include/unistd.h +++ b/libs/libc/include/unistd.h @@ -22,7 +22,8 @@ extern "C" int execve(const char*, char* const[], char* const[]); /* Not implemented. */ int execvp(const char*, char* const[]); - /* Not implemented. */ + + /* Creates an identical copy of the current process and returns its process ID. */ pid_t fork(void); /* Terminates the program with the status code status. */ diff --git a/libs/libc/src/unistd.cpp b/libs/libc/src/unistd.cpp index 86ac5df3..1c4577f0 100644 --- a/libs/libc/src/unistd.cpp +++ b/libs/libc/src/unistd.cpp @@ -19,9 +19,10 @@ extern "C" { NOT_IMPLEMENTED("execvp"); } + pid_t fork(void) { - NOT_IMPLEMENTED("fork"); + return syscall(SYS_fork); } long syscall(long number, ...) @@ -34,6 +35,7 @@ extern "C" { case SYS_clock: case SYS_yield: + case SYS_fork: case SYS_gettid: result = __luna_syscall0(number); break; case SYS_exit: case SYS_close: