From 5e4946e8e43f21ea39aa087929ec80b0ff44fa5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Tue, 21 Feb 2017 15:10:47 +0100 Subject: [PATCH] [USER] Fork syscall --- init/init.c | 20 ++++++++++---------- kernel/include/syscall.h | 1 + kernel/syscall/sys_proc.c | 27 +++++++++++++++++++++++++++ kernel/syscall/syscall.c | 1 + libc/syscall_num.h | 1 + libc/thread.c | 23 +++++++++++++++++++++++ 6 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 kernel/syscall/sys_proc.c diff --git a/init/init.c b/init/init.c index cd8539e..425ecc7 100644 --- a/init/init.c +++ b/init/init.c @@ -6,16 +6,16 @@ int main(int argc, char **argv) (void) argc; (void) argv; - char *a = malloc(0x100); - printf("Pointer: %p\n", a); - char *b = malloc(0x100); - printf("Pointer: %p\n", b); - char *c = malloc(0x10); - printf("Pointer: %p\n", c); - c[0] = 5; - free(a); - free(b); - free(c); + int num = 5; + + if(fork()) + { + num = 10; + printf("I am the parent! - %d\n", num); + } else { + num = 3; + printf("I am the child! - %d\n", num); + } for(;;); return 0; diff --git a/kernel/include/syscall.h b/kernel/include/syscall.h index 358d199..3eb2368 100644 --- a/kernel/include/syscall.h +++ b/kernel/include/syscall.h @@ -42,3 +42,4 @@ typedef long (*syscall_handler_t)(long num, long, long, long, long, long, long); SYSCALL_DECL(write); SYSCALL_DECL(brk); +SYSCALL_DECL(fork); diff --git a/kernel/syscall/sys_proc.c b/kernel/syscall/sys_proc.c new file mode 100644 index 0000000..adae757 --- /dev/null +++ b/kernel/syscall/sys_proc.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include + +SYSCALL_DEF(fork) +{ + SYSCALL_INIT(); + + // Copy process and memory space + process_t *p = get_current_process(); + process_t *new = process_spawn(p); + + // Copy thread + thread_t *th = new_thread(0, 1); + memcpy(&th->r, &get_current_thread()->r, sizeof(registers_t)); + + // Make new thread return 0 + th->r.rax = 0; + process_attach(new, th); + scheduler_insert(th); + + // Return new pid + return new->pid; + +} diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 491dde2..9de60da 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -58,6 +58,7 @@ void syscall_init() SYSCALL_REGISTER(debug, SYS_DEBUG); SYSCALL_REGISTER(write, SYS_WRITE); SYSCALL_REGISTER(brk, SYS_BRK); + SYSCALL_REGISTER(fork, SYS_FORK); } } diff --git a/libc/syscall_num.h b/libc/syscall_num.h index c3bf36f..01da47e 100644 --- a/libc/syscall_num.h +++ b/libc/syscall_num.h @@ -2,4 +2,5 @@ #define SYS_WRITE 0x001 #define SYS_BRK 0x002 +#define SYS_FORK 0x003 #define SYS_DEBUG 0x3FF diff --git a/libc/thread.c b/libc/thread.c index fa6ace2..7397af7 100644 --- a/libc/thread.c +++ b/libc/thread.c @@ -1,4 +1,8 @@ #include "syscalls.h" +#include "syscall_num.h" +#include +#include +long kernel_syscall(int num, ...); struct pthread *__pt; @@ -19,3 +23,22 @@ SYSCALL_DEF(set_tid_address) SYSCALL_INIT(); return 0; } + +SYSCALL_DEF(rt_sigprocmask) +{ + SYSCALL_INIT(int, how, const sigset_t *, set, sigset_t *, oldset); + + return 0; +} + +SYSCALL_DEF(fork) +{ + SYSCALL_INIT(); + return kernel_syscall(SYS_FORK); +} + +SYSCALL_DEF(gettid) +{ + SYSCALL_INIT(); + return 0; +}