Compare commits

...

2 Commits

Author SHA1 Message Date
7ab80014e1 More fork() work 2022-10-15 18:42:53 +02:00
e672f3994b libc: Add support for fork()
That doesn't mean it works.
Why doesn't it work??

Oh well...
2022-10-15 18:19:47 +02:00
7 changed files with 30 additions and 9 deletions

View File

@ -11,7 +11,6 @@ CFLAGS := -Wall -Wextra -Werror -Os
$(APPS_BIN)/%: $(APPS_SRC)/%.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) -o $@ $^
$(STRIP) $@
build: $(REAL_APPS)

View File

@ -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;
}

View File

@ -32,16 +32,23 @@ void sys_fork(Context* context) // FIXME: Even though both processes's address s
size_t stack_bytes = get_top_of_stack(parent->allocated_stack, TASK_PAGES_IN_STACK) - parent->regs.rsp;
child->regs.rsp -= stack_bytes;
child->regs.rsp = get_top_of_stack(child->allocated_stack, TASK_PAGES_IN_STACK) - stack_bytes;
memcpy((void*)child->regs.rsp, (void*)parent->regs.rsp, stack_bytes);
child->regs.rsp += sizeof(uintptr_t) * 2; // I don't know why this is...
child->address_space = parent->address_space.clone();
child->regs.rax = 0;
context->rax = child->id;
kinfoln("fork(): forked parent %d into child %d", parent->id, child->id);
child->state = child->Running;
kinfoln("fork(): parent RIP %lx, child RIP %lx, parent RSP %lx, child RSP %lx", parent->regs.rip, child->regs.rip,
parent->regs.rsp, child->regs.rsp);
kinfoln("fork(): forked parent %ld into child %ld", parent->id, child->id);
return;
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -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. */

View File

@ -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: