From 7627ba24d279676c9954ecc8c2738660921b0504 Mon Sep 17 00:00:00 2001 From: Karel Sommer Date: Thu, 23 Jan 2025 15:39:37 +0100 Subject: [PATCH] added shutdown --- common.h | 1 + kernel.c | 8 ++++++++ shell.c | 2 ++ user.c | 4 ++++ user.h | 1 + 5 files changed, 16 insertions(+) diff --git a/common.h b/common.h index 286df8a..730030f 100644 --- a/common.h +++ b/common.h @@ -25,6 +25,7 @@ typedef uint32_t vaddr_t; #define SYS_EXIT 3 #define SYS_READFILE 4 #define SYS_WRITEFILE 5 +#define SYS_SHUTDOWN 6 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 b82c66a..0bc7cde 100644 --- a/kernel.c +++ b/kernel.c @@ -268,6 +268,10 @@ long getchar(void) { return ret.error; } +void shutdown(void) { + sbi_call(0, 0, 0, 0, 0, 0, 0, 8 /* Shutdown */); +} + __attribute__((naked)) __attribute__((aligned(4))) void kernel_entry(void) { @@ -528,6 +532,10 @@ void handle_syscall(struct trap_frame *f) { f->a0 = len; break; } + case SYS_SHUTDOWN: + printf("shuting down\n"); + shutdown(); + break; default: PANIC("unexpected syscall a3=%x\n", f->a3); } diff --git a/shell.c b/shell.c index 0bff8e2..e728c62 100644 --- a/shell.c +++ b/shell.c @@ -32,6 +32,8 @@ void main(void) { } else if (strcmp(cmdline, "writefile") == 0) writefile("hello.txt", "Hello from shell!\n", 19); + else if (strcmp(cmdline, "shutdown") == 0) + shutdown(); else printf("unknown command: %s\n", cmdline); } diff --git a/user.c b/user.c index c0854fa..6f3e048 100644 --- a/user.c +++ b/user.c @@ -32,6 +32,10 @@ int writefile(const char *filename, const char *buf, int len) { return syscall(SYS_WRITEFILE, (int) filename, (int) buf, len); } +int shutdown(void) { + return syscall(SYS_SHUTDOWN, 0, 0, 0); +} + __attribute__((noreturn)) void exit(void) { syscall(SYS_EXIT, 0, 0, 0); for (;;); diff --git a/user.h b/user.h index 8c0c859..d64bd2f 100644 --- a/user.h +++ b/user.h @@ -11,4 +11,5 @@ void putchar(char ch); int getchar(void); int readfile(const char *filename, char *buf, int len); int writefile(const char *filename, const char *buf, int len); +int shutdown(void); __attribute__((noreturn)) void exit(void);