syscall: implement write syscall
This commit is contained in:
parent
f6e1419961
commit
3dda839cdd
12 changed files with 176 additions and 22 deletions
15
fs/stdio_ops.c
Normal file
15
fs/stdio_ops.c
Normal 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;
|
||||
}
|
||||
18
fs/vfs.c
18
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)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
#define ENOSPC 7
|
||||
#define EEXIST 8
|
||||
#define ENOEXEC 9
|
||||
#define EFAULT 10
|
||||
|
||||
const char *strerror(int errnum);
|
||||
|
||||
|
|
|
|||
|
|
@ -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_ */
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
9
include/kernel/syscall.h
Normal file
9
include/kernel/syscall.h
Normal 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_ */
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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); */
|
||||
|
|
|
|||
52
sched/proc.c
52
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue