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

View file

@ -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); */

View file

@ -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;
}

View file

@ -3,8 +3,9 @@
#include <kernel/proc.h>
#include <kernel/slab.h>
#include <kernel/rbtree.h>
#include <kernel/syscall.h>
#define SYS_EXIT 60
#include <fs/vfs.h>
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);
}