summaryrefslogtreecommitdiff
path: root/src/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/fd.c13
-rw-r--r--src/kernel/fd.h23
-rw-r--r--src/kernel/proc.c3
-rw-r--r--src/kernel/proc.h3
-rw-r--r--src/kernel/syscalls.c18
-rw-r--r--src/kernel/syscalls.h8
6 files changed, 68 insertions, 0 deletions
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)
*/