diff --git a/common.h b/common.h index ce55b19..29ceb57 100644 --- a/common.h +++ b/common.h @@ -22,6 +22,7 @@ typedef uint32_t vaddr_t; #define PAGE_SIZE 4096 #define SYS_PUTCHAR 1 #define SYS_GETCHAR 2 +#define SYS_EXIT 3 void *memset(void *buf, char c, size_t n); void *memcpy(void *dst, const void *src, size_t n); diff --git a/kernel.c b/kernel.c index 5adef72..cc601ad 100644 --- a/kernel.c +++ b/kernel.c @@ -315,6 +315,11 @@ void proc_b_entry(void) { void handle_syscall(struct trap_frame *f) { switch (f->a3) { + case SYS_EXIT: + printf("process %d exited\n", current_proc->pid); + current_proc->state = PROC_EXITED; + yield(); + PANIC("unreachable"); case SYS_GETCHAR: while (1) { long ch = getchar(); diff --git a/kernel.h b/kernel.h index efe1f61..b2a96b9 100644 --- a/kernel.h +++ b/kernel.h @@ -4,6 +4,7 @@ #define PROCS_MAX 8 // Maximum ammount of processes #define PROC_UNUSED 0 // Unused processes control structure #define PROC_RUNNABLE 1 // Runnable proccess +#define PROC_EXITED 2 #define SATP_SV32 (1u << 31) #define SSTATUS_SPIE (1 << 5) #define SCAUSE_ECALL 8 diff --git a/shell.c b/shell.c index 92f74cb..1468a05 100644 --- a/shell.c +++ b/shell.c @@ -22,6 +22,8 @@ prompt: if (strcmp(cmdline, "hello") == 0) printf("Hellow :3\n"); + else if (strcmp(cmdline, "exit") == 0) + exit(); else printf("I don't know what is %s yet :(\n", cmdline); } diff --git a/user.c b/user.c index 20ea0b5..bed6ae1 100644 --- a/user.c +++ b/user.c @@ -18,7 +18,8 @@ int syscall(int sysno, int arg0, int arg1, int arg2) { } __attribute__((noreturn)) void exit(void) { - for(;;); + syscall(SYS_EXIT, 0, 0, 0); + for (;;); // just in case } void putchar(char ch) {