From 3dda839cddaca826db60b8a13d70df525a367d43 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 22 Nov 2023 23:27:39 -0500 Subject: [PATCH] syscall: implement write syscall --- fs/stdio_ops.c | 15 ++++++++++++ fs/vfs.c | 18 ++++++++++++++ include/errno.h | 1 + include/fs/vfs.h | 6 ++++- include/kernel/proc.h | 5 ++++ include/kernel/syscall.h | 9 +++++++ init/cmd.c | 1 - lib/errno.c | 1 + sched/entry.S | 31 +++++++++++++++++++++--- sched/proc.c | 52 +++++++++++++++++++++++++++++++--------- sched/syscall.c | 42 +++++++++++++++++++++++++++++++- usr.bin/test1/start.S | 17 +++++++++---- 12 files changed, 176 insertions(+), 22 deletions(-) create mode 100644 fs/stdio_ops.c create mode 100644 include/kernel/syscall.h diff --git a/fs/stdio_ops.c b/fs/stdio_ops.c new file mode 100644 index 0000000..2172972 --- /dev/null +++ b/fs/stdio_ops.c @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include + +#include + +int stdout_write(struct file_descriptor *fdesc, void *buf, size_t len) +{ + int i; + for (i = 0; i < len; i++) { + console_write(((char *)buf)[i]); + } + return i; +} diff --git a/fs/vfs.c b/fs/vfs.c index f49ba53..562ffd4 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -16,12 +16,21 @@ typedef struct fs *(*vfs_init_t)(struct block_device *); int write(struct file_descriptor *fdesc, void *buf, size_t count) { + if (fdesc->mode == FD_TYPE_CHARDEV) { + if (fdesc->buf_write) + return fdesc->buf_write(fdesc, buf, count); + return -EBADF; + } + if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR) return -EISDIR; if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG) return -EBADF; + if (fdesc->flags & O_RDONLY) + return -EBADF; + int ret = fdesc->fs->ops->write(fdesc->fs, &fdesc->vnode, buf, fdesc->pos, count); if (ret < 0) @@ -34,12 +43,21 @@ int write(struct file_descriptor *fdesc, void *buf, size_t count) int read(struct file_descriptor *fdesc, void *buf, size_t count) { + if (fdesc->mode == FD_TYPE_CHARDEV) { + if (fdesc->buf_read) + return fdesc->buf_read(fdesc, buf, count); + return -EBADF; + } + if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR) return -EISDIR; if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG) return -EBADF; + if (fdesc->flags & O_WRONLY) + return -EBADF; + int ret = fdesc->fs->ops->read(fdesc->fs, &fdesc->vnode, buf, fdesc->pos, count); if (ret < 0) diff --git a/include/errno.h b/include/errno.h index 91fd873..e4ce924 100644 --- a/include/errno.h +++ b/include/errno.h @@ -11,6 +11,7 @@ #define ENOSPC 7 #define EEXIST 8 #define ENOEXEC 9 +#define EFAULT 10 const char *strerror(int errnum); diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 8433db2..32d0458 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -80,7 +80,9 @@ struct file_descriptor { uint32_t mode; char *buf; - size_t buf_size; + size_t buf_len; + int (*buf_write)(struct file_descriptor *fdesc, void *buf, size_t count); + int (*buf_read)(struct file_descriptor *fdesc, void *buf, size_t count); struct fs *fs; }; @@ -126,4 +128,6 @@ char *dirname(char *path); struct file_descriptor *fd_special(); void fd_special_free(struct file_descriptor *fd); +int stdout_write(struct file_descriptor *fdesc, void *buf, size_t len); + #endif /* _VFS_H_ */ diff --git a/include/kernel/proc.h b/include/kernel/proc.h index a4caa1f..1efd492 100644 --- a/include/kernel/proc.h +++ b/include/kernel/proc.h @@ -11,6 +11,10 @@ #define PROC_STOPPED 1 #define PROC_BLOCKED 2 +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 + typedef int64_t pid_t; struct procregs { @@ -44,6 +48,7 @@ struct proc { uint64_t pid; uint64_t ppid; uint64_t cr3; + spinlock_t lock; bool is_kernel; uint8_t state; diff --git a/include/kernel/syscall.h b/include/kernel/syscall.h new file mode 100644 index 0000000..ad05008 --- /dev/null +++ b/include/kernel/syscall.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _SYSCALL_H_ +#define _SYSCALL_H_ + +#define SYS_READ 0 +#define SYS_WRITE 1 +#define SYS_EXIT 60 + +#endif /* _SYSCALL_H_ */ diff --git a/init/cmd.c b/init/cmd.c index 769de70..57225cc 100644 --- a/init/cmd.c +++ b/init/cmd.c @@ -299,7 +299,6 @@ int kcmd_exec(int argc, char **argv) return 1; } - proc->state = PROC_RUNNING; void _return_to_user(struct procregs * regs, paddr_t cr3); proc_set_current(pid); _return_to_user(&proc->regs, proc->cr3); diff --git a/lib/errno.c b/lib/errno.c index 23edbe8..8d4c136 100644 --- a/lib/errno.c +++ b/lib/errno.c @@ -13,6 +13,7 @@ static const char *errno_str[] = { [ENOSPC] = "No space left on device", [EEXIST] = "File exists", [ENOEXEC] = "Exec format error", + [EFAULT] = "Bad address", }; inline const char *strerror(int errnum) diff --git a/sched/entry.S b/sched/entry.S index 7bc5c40..864b8fe 100644 --- a/sched/entry.S +++ b/sched/entry.S @@ -196,11 +196,36 @@ irq 0x0F .global gate_syscall gate_syscall: cli - save_context - movq 0(%rax), %rdi /* syscall number */ - movq 40(%rax), %rsi /* arguments */ + pushq %rbx + pushq %rbp + pushq %r12 + pushq %r13 + pushq %r14 + pushq %r15 + pushq %rax + pushq %rdi + pushq %rsi + pushq %rdx + pushq %rcx + pushq %r8 + + popq %r9 + popq %r8 + popq %rcx + popq %rdx + popq %rsi + popq %rdi + call syscall + + popq %r15 + popq %r14 + popq %r13 + popq %r12 + popq %rbp + popq %rbx + iretq /* void return_from_irq(struct procregs *regs, paddr_t cr3); */ diff --git a/sched/proc.c b/sched/proc.c index 9e16e00..bf84898 100644 --- a/sched/proc.c +++ b/sched/proc.c @@ -62,6 +62,7 @@ void proc_term(pid_t pid) if (proc_node) { struct proc *proc = (void *)proc_node->value; + spinlock_acquire(&proc->lock); rbt_delete(proc_tree, proc_node); @@ -72,6 +73,9 @@ void proc_term(pid_t pid) map_node = next; } + rbt_destroy(&proc->page_map); + rbt_destroy(&proc->fd_map); + slab_free(proc_slab, proc); } else { kprintf(LOG_ERROR "proc: proc_term: proc %u not found\n", pid); @@ -85,15 +89,31 @@ void schedule() struct proc *proc = proc_find(proc_current[id]); - if (proc) { - _return_to_user(&proc->regs, proc->cr3); - } else { - kprintf(LOG_ERROR "proc: schedule: proc_current[%u] is NULL\n", id); - panic(); + if (proc && proc->state == PROC_RUNNING) + proc->state = PROC_STOPPED; + + while (1) { + if (proc) { + spinlock_acquire(&proc->lock); + + if (proc->state == PROC_STOPPED) { + proc->state = PROC_RUNNING; + spinlock_release(&proc->lock); + _return_to_user(&proc->regs, proc->cr3); + } else if (proc->state == PROC_BLOCKED) { + spinlock_release(&proc->lock); + return; + } else { + spinlock_release(&proc->lock); + } + } else { + kprintf(LOG_ERROR "proc: schedule: proc_current[%u] is NULL\n", id); + panic(); + } } } -static int proc_insert_charfd(struct proc *proc, int fdno, int flags) +static struct file_descriptor *proc_insert_charfd(struct proc *proc, int fdno, int flags) { struct file_descriptor *fd = fd_special(); fd->vnode = (struct vnode){ 0 }; @@ -102,13 +122,16 @@ static int proc_insert_charfd(struct proc *proc, int fdno, int flags) fd->flags = flags; fd->mode = FD_TYPE_CHARDEV; fd->buf = NULL; - fd->buf_size = 0; + fd->buf_len = 0; fd->fs = NULL; + fd->buf_write = NULL; + fd->buf_read = NULL; + struct rbnode *fd_node = rbt_insert(&proc->fd_map, fdno); fd_node->value = (uintptr_t)fd; - return fdno; + return fd; } struct proc *proc_create() @@ -116,6 +139,8 @@ struct proc *proc_create() struct proc *proc = slab_alloc(proc_slab); memset(proc, 0, sizeof(struct proc)); + spinlock_acquire(&proc->lock); + proc->pid = rbt_next_key(proc_tree); proc->is_kernel = false; @@ -123,9 +148,14 @@ struct proc *proc_create() proc_node->value = (uintptr_t)proc; /* insert stdin, stdout, stderr */ - proc_insert_charfd(proc, 0, O_RDONLY); /* stdin */ - proc_insert_charfd(proc, 1, O_WRONLY); /* stdout */ - proc_insert_charfd(proc, 2, O_WRONLY); /* stderr */ + struct file_descriptor *fd; + fd = proc_insert_charfd(proc, STDIN_FILENO, O_RDONLY); + fd = proc_insert_charfd(proc, STDOUT_FILENO, O_WRONLY); + fd->buf_write = stdout_write; + fd = proc_insert_charfd(proc, STDERR_FILENO, O_WRONLY); + fd->buf_write = stdout_write; + + spinlock_release(&proc->lock); return proc; } diff --git a/sched/syscall.c b/sched/syscall.c index 37a149b..2c1e809 100644 --- a/sched/syscall.c +++ b/sched/syscall.c @@ -3,8 +3,9 @@ #include #include #include +#include -#define SYS_EXIT 60 +#include static struct rbtree *syscall_tree; @@ -22,6 +23,44 @@ void syscall(uint64_t syscall_no, uint64_t arg1, uint64_t arg2, uint64_t arg3, u syscall(arg1, arg2, arg3, arg4, arg5); } +int sys_read(int fd, void *buf, size_t count) +{ + if ((uintptr_t)buf >= hhdm_start) + return -EFAULT; + + struct proc *proc = proc_find(getpid()); + if (proc == NULL) + return -1; + + struct rbnode *fdesc_node = rbt_search(&proc->fd_map, fd); + + if (fdesc_node == NULL) + return -EBADF; + + struct file_descriptor *fdesc = (void *)fdesc_node->value; + + return read(fdesc, buf, count); +} + +int sys_write(int fd, void *buf, size_t count) +{ + if ((uintptr_t)buf >= hhdm_start) + return -EFAULT; + + struct proc *proc = proc_find(getpid()); + if (proc == NULL) + return -1; + + struct rbnode *fdesc_node = rbt_search(&proc->fd_map, fd); + + if (fdesc_node == NULL) + return -EBADF; + + struct file_descriptor *fdesc = (void *)fdesc_node->value; + + return write(fdesc, buf, count); +} + void sys_exit(int status) { struct proc *proc = proc_find(getpid()); @@ -47,5 +86,6 @@ static void syscall_insert(uint64_t syscall_no, syscall_t syscall) void syscall_init() { syscall_tree = kzalloc(sizeof(struct rbtree), ALLOC_KERN); + syscall_insert(SYS_WRITE, (syscall_t)sys_write); syscall_insert(SYS_EXIT, (syscall_t)sys_exit); } diff --git a/usr.bin/test1/start.S b/usr.bin/test1/start.S index 209dbc8..6c312af 100644 --- a/usr.bin/test1/start.S +++ b/usr.bin/test1/start.S @@ -5,18 +5,25 @@ * Test the basic functionality of the kernel loader */ +sys_write = 1 sys_exit = 60 .global _start _start: - movq test_var, %rdi - movq $sys_exit, %rax + movl $sys_write, %eax + movl $1, %edi + movq $test_string, %rsi + movl $test_string_len, %edx + int $0x80 + + movl $sys_exit, %eax + movl $0xDEADC0DE, %edi int $0x80 _no_exit: movq $0, %rbx jmp _no_exit .data -test_var: - .long 0xfeedbeef - .long 0xdeadbeef +test_string: + .ascii "hello, world\n\n" +test_string_len = . - test_string