syscall: implement write syscall

This commit is contained in:
Michael Shaw 2023-11-22 23:27:39 -05:00
commit 3dda839cdd
12 changed files with 176 additions and 22 deletions

15
fs/stdio_ops.c Normal file
View file

@ -0,0 +1,15 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/slab.h>
#include <dev/console.h>
#include <fs/vfs.h>
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;
}

View file

@ -16,12 +16,21 @@ typedef struct fs *(*vfs_init_t)(struct block_device *);
int write(struct file_descriptor *fdesc, void *buf, size_t count) 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) if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR)
return -EISDIR; return -EISDIR;
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG) if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG)
return -EBADF; return -EBADF;
if (fdesc->flags & O_RDONLY)
return -EBADF;
int ret = fdesc->fs->ops->write(fdesc->fs, &fdesc->vnode, buf, fdesc->pos, count); int ret = fdesc->fs->ops->write(fdesc->fs, &fdesc->vnode, buf, fdesc->pos, count);
if (ret < 0) 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) 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) if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR)
return -EISDIR; return -EISDIR;
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG) if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG)
return -EBADF; return -EBADF;
if (fdesc->flags & O_WRONLY)
return -EBADF;
int ret = fdesc->fs->ops->read(fdesc->fs, &fdesc->vnode, buf, fdesc->pos, count); int ret = fdesc->fs->ops->read(fdesc->fs, &fdesc->vnode, buf, fdesc->pos, count);
if (ret < 0) if (ret < 0)

View file

@ -11,6 +11,7 @@
#define ENOSPC 7 #define ENOSPC 7
#define EEXIST 8 #define EEXIST 8
#define ENOEXEC 9 #define ENOEXEC 9
#define EFAULT 10
const char *strerror(int errnum); const char *strerror(int errnum);

View file

@ -80,7 +80,9 @@ struct file_descriptor {
uint32_t mode; uint32_t mode;
char *buf; 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; struct fs *fs;
}; };
@ -126,4 +128,6 @@ char *dirname(char *path);
struct file_descriptor *fd_special(); struct file_descriptor *fd_special();
void fd_special_free(struct file_descriptor *fd); void fd_special_free(struct file_descriptor *fd);
int stdout_write(struct file_descriptor *fdesc, void *buf, size_t len);
#endif /* _VFS_H_ */ #endif /* _VFS_H_ */

View file

@ -11,6 +11,10 @@
#define PROC_STOPPED 1 #define PROC_STOPPED 1
#define PROC_BLOCKED 2 #define PROC_BLOCKED 2
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
typedef int64_t pid_t; typedef int64_t pid_t;
struct procregs { struct procregs {
@ -44,6 +48,7 @@ struct proc {
uint64_t pid; uint64_t pid;
uint64_t ppid; uint64_t ppid;
uint64_t cr3; uint64_t cr3;
spinlock_t lock;
bool is_kernel; bool is_kernel;
uint8_t state; uint8_t state;

9
include/kernel/syscall.h Normal file
View file

@ -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_ */

View file

@ -299,7 +299,6 @@ int kcmd_exec(int argc, char **argv)
return 1; return 1;
} }
proc->state = PROC_RUNNING;
void _return_to_user(struct procregs * regs, paddr_t cr3); void _return_to_user(struct procregs * regs, paddr_t cr3);
proc_set_current(pid); proc_set_current(pid);
_return_to_user(&proc->regs, proc->cr3); _return_to_user(&proc->regs, proc->cr3);

View file

@ -13,6 +13,7 @@ static const char *errno_str[] = {
[ENOSPC] = "No space left on device", [ENOSPC] = "No space left on device",
[EEXIST] = "File exists", [EEXIST] = "File exists",
[ENOEXEC] = "Exec format error", [ENOEXEC] = "Exec format error",
[EFAULT] = "Bad address",
}; };
inline const char *strerror(int errnum) inline const char *strerror(int errnum)

View file

@ -196,11 +196,36 @@ irq 0x0F
.global gate_syscall .global gate_syscall
gate_syscall: gate_syscall:
cli cli
save_context pushq %rbx
movq 0(%rax), %rdi /* syscall number */ pushq %rbp
movq 40(%rax), %rsi /* arguments */ 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 call syscall
popq %r15
popq %r14
popq %r13
popq %r12
popq %rbp
popq %rbx
iretq iretq
/* void return_from_irq(struct procregs *regs, paddr_t cr3); */ /* void return_from_irq(struct procregs *regs, paddr_t cr3); */

View file

@ -62,6 +62,7 @@ void proc_term(pid_t pid)
if (proc_node) { if (proc_node) {
struct proc *proc = (void *)proc_node->value; struct proc *proc = (void *)proc_node->value;
spinlock_acquire(&proc->lock);
rbt_delete(proc_tree, proc_node); rbt_delete(proc_tree, proc_node);
@ -72,6 +73,9 @@ void proc_term(pid_t pid)
map_node = next; map_node = next;
} }
rbt_destroy(&proc->page_map);
rbt_destroy(&proc->fd_map);
slab_free(proc_slab, proc); slab_free(proc_slab, proc);
} else { } else {
kprintf(LOG_ERROR "proc: proc_term: proc %u not found\n", pid); 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]); struct proc *proc = proc_find(proc_current[id]);
if (proc) { if (proc && proc->state == PROC_RUNNING)
_return_to_user(&proc->regs, proc->cr3); proc->state = PROC_STOPPED;
} else {
kprintf(LOG_ERROR "proc: schedule: proc_current[%u] is NULL\n", id); while (1) {
panic(); 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(); struct file_descriptor *fd = fd_special();
fd->vnode = (struct vnode){ 0 }; 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->flags = flags;
fd->mode = FD_TYPE_CHARDEV; fd->mode = FD_TYPE_CHARDEV;
fd->buf = NULL; fd->buf = NULL;
fd->buf_size = 0; fd->buf_len = 0;
fd->fs = NULL; fd->fs = NULL;
fd->buf_write = NULL;
fd->buf_read = NULL;
struct rbnode *fd_node = rbt_insert(&proc->fd_map, fdno); struct rbnode *fd_node = rbt_insert(&proc->fd_map, fdno);
fd_node->value = (uintptr_t)fd; fd_node->value = (uintptr_t)fd;
return fdno; return fd;
} }
struct proc *proc_create() struct proc *proc_create()
@ -116,6 +139,8 @@ struct proc *proc_create()
struct proc *proc = slab_alloc(proc_slab); struct proc *proc = slab_alloc(proc_slab);
memset(proc, 0, sizeof(struct proc)); memset(proc, 0, sizeof(struct proc));
spinlock_acquire(&proc->lock);
proc->pid = rbt_next_key(proc_tree); proc->pid = rbt_next_key(proc_tree);
proc->is_kernel = false; proc->is_kernel = false;
@ -123,9 +148,14 @@ struct proc *proc_create()
proc_node->value = (uintptr_t)proc; proc_node->value = (uintptr_t)proc;
/* insert stdin, stdout, stderr */ /* insert stdin, stdout, stderr */
proc_insert_charfd(proc, 0, O_RDONLY); /* stdin */ struct file_descriptor *fd;
proc_insert_charfd(proc, 1, O_WRONLY); /* stdout */ fd = proc_insert_charfd(proc, STDIN_FILENO, O_RDONLY);
proc_insert_charfd(proc, 2, O_WRONLY); /* stderr */ 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; return proc;
} }

View file

@ -3,8 +3,9 @@
#include <kernel/proc.h> #include <kernel/proc.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <kernel/rbtree.h> #include <kernel/rbtree.h>
#include <kernel/syscall.h>
#define SYS_EXIT 60 #include <fs/vfs.h>
static struct rbtree *syscall_tree; 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); 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) void sys_exit(int status)
{ {
struct proc *proc = proc_find(getpid()); struct proc *proc = proc_find(getpid());
@ -47,5 +86,6 @@ static void syscall_insert(uint64_t syscall_no, syscall_t syscall)
void syscall_init() void syscall_init()
{ {
syscall_tree = kzalloc(sizeof(struct rbtree), ALLOC_KERN); syscall_tree = kzalloc(sizeof(struct rbtree), ALLOC_KERN);
syscall_insert(SYS_WRITE, (syscall_t)sys_write);
syscall_insert(SYS_EXIT, (syscall_t)sys_exit); syscall_insert(SYS_EXIT, (syscall_t)sys_exit);
} }

View file

@ -5,18 +5,25 @@
* Test the basic functionality of the kernel loader * Test the basic functionality of the kernel loader
*/ */
sys_write = 1
sys_exit = 60 sys_exit = 60
.global _start .global _start
_start: _start:
movq test_var, %rdi movl $sys_write, %eax
movq $sys_exit, %rax movl $1, %edi
movq $test_string, %rsi
movl $test_string_len, %edx
int $0x80
movl $sys_exit, %eax
movl $0xDEADC0DE, %edi
int $0x80 int $0x80
_no_exit: _no_exit:
movq $0, %rbx movq $0, %rbx
jmp _no_exit jmp _no_exit
.data .data
test_var: test_string:
.long 0xfeedbeef .ascii "hello, world\n\n"
.long 0xdeadbeef test_string_len = . - test_string