diff options
author | dzwdz | 2021-08-24 16:25:50 +0200 |
---|---|---|
committer | dzwdz | 2021-08-24 16:25:50 +0200 |
commit | 83fa1f34ea744da7d26997179a0dd6af751dd8b0 (patch) | |
tree | 0794348a02acebf36ecab9ad665bc0f0f75fbfee /src | |
parent | 2df1c7370bf596aebbe06f7333a65a6f4d6ec15a (diff) |
add some scaffolding for file descriptors
Diffstat (limited to 'src')
-rw-r--r-- | src/init/syscalls.c | 12 | ||||
-rw-r--r-- | src/kernel/fd.c | 13 | ||||
-rw-r--r-- | src/kernel/fd.h | 23 | ||||
-rw-r--r-- | src/kernel/proc.c | 3 | ||||
-rw-r--r-- | src/kernel/proc.h | 3 | ||||
-rw-r--r-- | src/kernel/syscalls.c | 18 | ||||
-rw-r--r-- | src/kernel/syscalls.h | 8 |
7 files changed, 80 insertions, 0 deletions
diff --git a/src/init/syscalls.c b/src/init/syscalls.c index 0be7917..4f207db 100644 --- a/src/init/syscalls.c +++ b/src/init/syscalls.c @@ -24,6 +24,18 @@ int _syscall_mount(const char *path, int len, fd_t fd) { return _syscall(_SYSCALL_MOUNT, (int)path, len, fd); } +int _syscall_fd_read(fd_t fd, char *buf, int len) { + return _syscall(_SYSCALL_FD_READ, fd, (int)buf, len); +} + +int _syscall_fd_write(fd_t fd, char *buf, int len) { + return _syscall(_SYSCALL_FD_WRITE, fd, (int)buf, len); +} + +int _syscall_fd_close(fd_t fd) { + return _syscall(_SYSCALL_FD_CLOSE, fd, 0, 0); +} + int _syscall_debuglog(const char *msg, size_t len) { return _syscall(_SYSCALL_DEBUGLOG, (int)msg, len, 0); } diff --git a/src/kernel/fd.c b/src/kernel/fd.c new file mode 100644 index 0000000..54f1c72 --- /dev/null +++ b/src/kernel/fd.c @@ -0,0 +1,13 @@ +#include <kernel/fd.h> +#include <kernel/panic.h> +#include <kernel/proc.h> + +int fdop_dispatch(enum fdop fdop, fd_t fd, void *ptr, size_t len) { + if (fd < 0 || fd >= FD_MAX) return -1; + switch(process_current->fds[fd].type) { + case FD_EMPTY: + return -1; + default: + panic(); + } +} diff --git a/src/kernel/fd.h b/src/kernel/fd.h new file mode 100644 index 0000000..4976c7d --- /dev/null +++ b/src/kernel/fd.h @@ -0,0 +1,23 @@ +#pragma once +#include <stddef.h> + +#define FD_MAX 16 + +typedef int fd_t; // TODO duplicated in syscalls.h + +enum fd_type { + FD_EMPTY, +}; + +struct fd { + enum fd_type type; +}; + + +enum fdop { // describes the operations which can be done on file descriptors + FDOP_READ, + FDOP_WRITE, + FDOP_CLOSE, +}; + +int fdop_dispatch(enum fdop fdop, fd_t fd, void *ptr, size_t len); diff --git a/src/kernel/proc.c b/src/kernel/proc.c index f826d8a..4c278e6 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -21,6 +21,9 @@ struct process *process_seed() { process_first = proc; + for (int i = 0; i < FD_MAX; i++) + proc->fds[i].type = FD_EMPTY; + // map the stack to the last page in memory pagedir_map(proc->pages, (void*)~PAGE_MASK, page_alloc(1), true, true); proc->stack_top = (void*) (proc->regs.esp = ~0xF); diff --git a/src/kernel/proc.h b/src/kernel/proc.h index ffc342a..0a1a42c 100644 --- a/src/kernel/proc.h +++ b/src/kernel/proc.h @@ -1,5 +1,6 @@ #pragma once #include <kernel/arch/generic.h> +#include <kernel/fd.h> #include <kernel/vfs/mount.h> enum process_state { @@ -28,6 +29,8 @@ struct process { size_t saved_len; struct vfs_mount *mount; + + struct fd fds[FD_MAX]; }; extern struct process *process_first; diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index 8ee90eb..4da5261 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -130,6 +130,18 @@ fail: return -1; } +int _syscall_fd_read(fd_t fd, char *buf, int len) { + return fdop_dispatch(FDOP_READ, fd, buf, len); +} + +int _syscall_fd_write(fd_t fd, char *buf, int len) { + return fdop_dispatch(FDOP_WRITE, fd, buf, len); +} + +int _syscall_fd_close(fd_t fd) { + return fdop_dispatch(FDOP_CLOSE, fd, 0, 0); +} + int _syscall_debuglog(const char *msg, size_t len) { struct virt_iter iter; virt_iter_new(&iter, (void*)msg, len, process_current->pages, true, false); @@ -150,6 +162,12 @@ int syscall_handler(int num, int a, int b, int c) { return _syscall_fs_open((void*)a, b); case _SYSCALL_MOUNT: return _syscall_mount((void*)a, b, c); + case _SYSCALL_FD_READ: + return _syscall_fd_read(a, (void*)b, c); + case _SYSCALL_FD_WRITE: + return _syscall_fd_write(a, (void*)b, c); + case _SYSCALL_FD_CLOSE: + return _syscall_fd_close(a); case _SYSCALL_DEBUGLOG: return _syscall_debuglog((void*)a, b); default: diff --git a/src/kernel/syscalls.h b/src/kernel/syscalls.h index f3e8dc0..d8ca5c3 100644 --- a/src/kernel/syscalls.h +++ b/src/kernel/syscalls.h @@ -14,6 +14,10 @@ enum { _SYSCALL_FS_OPEN, _SYSCALL_MOUNT, + _SYSCALL_FD_READ, + _SYSCALL_FD_WRITE, + _SYSCALL_FD_CLOSE, + _SYSCALL_DEBUGLOG }; @@ -36,6 +40,10 @@ int _syscall_fork(); fd_t _syscall_fs_open(const char *path, int len); int _syscall_mount(const char *path, int len, fd_t fd); +int _syscall_fd_read(fd_t fd, char *buf, int len); +int _syscall_fd_write(fd_t fd, char *buf, int len); +int _syscall_fd_close(fd_t fd); + /** Prints a message to the debug console. * @return the amount of bytes written (can be less than len) */ |