From 02c0816e413926a8269ff5ab91c56a28ef861e5f Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Sun, 18 Jan 2015 15:08:12 -0800 Subject: [PATCH] Adding wait system call --- bin/shell/shell.c | 25 +++++++++++++++++++++++-- include/syscall.h | 3 ++- lib/libc/syscall.c | 6 ++++++ sys/include/syscall.h | 7 ++++--- sys/kern/syscall.c | 19 ++++++++++++++----- 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/bin/shell/shell.c b/bin/shell/shell.c index f113315..f50698a 100644 --- a/bin/shell/shell.c +++ b/bin/shell/shell.c @@ -19,6 +19,7 @@ main(int argc, const char *argv[]) char buf[256]; printf("System Shell\n"); + while (1) { fputs("Shell> ", stdout); fgets(buf, sizeof(buf), stdin); @@ -31,12 +32,13 @@ void Cmd_Help(int argc, const char *argv[]) { printf("cat Print file\n"); + printf("bkpt Trigger a kernel breakpoint\n"); + printf("date Print current date and time\n"); printf("echo Echo arguments\n"); printf("exit Exit shell\n"); printf("help Display the list of commands\n"); printf("ls List files in a directory\n"); - printf("date Print current date and time\n"); - printf("bkpt Trigger a kernel breakpoint\n"); + printf("spawn Spawn a process\n"); } void @@ -124,6 +126,23 @@ Cmd_Hexdump(int argc, const char *argv[]) { } +void +Cmd_Spawn(int argc, const char *argv[]) +{ + int fd; + uint64_t status; + uintptr_t offset = 0; + + if (argc != 2) { + fputs("Requires an argument\n", stdout); + return; + } + + status = OSSpawn(argv[1]); + // XXX: check errors + OSWait(status); +} + void DispatchCommand(char *buf) { @@ -168,6 +187,8 @@ DispatchCommand(char *buf) exit(0); } else if (strcmp(argv[0], "ls") == 0) { Cmd_List(argc, (const char **)argv); + } else if (strcmp(argv[0], "spawn") == 0) { + Cmd_Spawn(argc, (const char **)argv); } else if (strcmp(argv[0], "#") == 0) { // Ignore comments } else if (buf[0] != '\0') { diff --git a/include/syscall.h b/include/syscall.h index 4421aec..720bc73 100644 --- a/include/syscall.h +++ b/include/syscall.h @@ -5,9 +5,10 @@ #include uint64_t OSTime(); -void OSExit(int status); uint64_t OSGetPID(); +void OSExit(int status); uint64_t OSSpawn(const char *path); +uint64_t OSWait(uint64_t pid); // Memory void *OSMemMap(void *addr, uint64_t len, int flags); diff --git a/lib/libc/syscall.c b/lib/libc/syscall.c index bd153c4..761bdd1 100644 --- a/lib/libc/syscall.c +++ b/lib/libc/syscall.c @@ -31,6 +31,12 @@ OSSpawn(const char *path) return syscall(SYSCALL_SPAWN, path); } +uint64_t +OSWait(uint64_t pid) +{ + return syscall(SYSCALL_WAIT, pid); +} + void * OSMemMap(void *addr, uint64_t len, int flags) { diff --git a/sys/include/syscall.h b/sys/include/syscall.h index 005f3c5..26f3ddd 100644 --- a/sys/include/syscall.h +++ b/sys/include/syscall.h @@ -4,9 +4,10 @@ #define SYSCALL_NULL 0x00 #define SYSCALL_TIME 0x01 -#define SYSCALL_EXIT 0x02 -#define SYSCALL_SPAWN 0x03 -#define SYSCALL_GETPID 0x04 +#define SYSCALL_GETPID 0x02 +#define SYSCALL_EXIT 0x03 +#define SYSCALL_SPAWN 0x04 +#define SYSCALL_WAIT 0x05 // Memory #define SYSCALL_MMAP 0x08 diff --git a/sys/kern/syscall.c b/sys/kern/syscall.c index d8649b7..aabdf9b 100644 --- a/sys/kern/syscall.c +++ b/sys/kern/syscall.c @@ -24,6 +24,13 @@ Syscall_Time() return KTime_GetEpochNS(); } +uint64_t +Syscall_GetPID() +{ + Thread *cur = Thread_Current(); + return cur->tid; +} + void Syscall_Exit(uint64_t status) { @@ -101,10 +108,10 @@ Syscall_Spawn(uint64_t user_path) } uint64_t -Syscall_GetPID() +Syscall_Wait(uint64_t pid) { - Thread *cur = Thread_Current(); - return cur->tid; + NOT_IMPLEMENTED(); + return 0; } uint64_t @@ -379,13 +386,15 @@ Syscall_Entry(uint64_t syscall, uint64_t a1, uint64_t a2, return 0; case SYSCALL_TIME: return Syscall_Time(); + case SYSCALL_GETPID: + return Syscall_GetPID(); case SYSCALL_EXIT: Syscall_Exit(a1); return 0; // To eliminate warning case SYSCALL_SPAWN: return Syscall_Spawn(a1); - case SYSCALL_GETPID: - return Syscall_GetPID(); + case SYSCALL_WAIT: + return Syscall_Wait(a1); case SYSCALL_MMAP: return Syscall_MMap(a1, a2, a3); case SYSCALL_MUNMAP: