mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
kernel: read syscall
This commit is contained in:
@@ -2,12 +2,13 @@ section .rodata
|
||||
extern syscall_print
|
||||
extern syscall_mmap
|
||||
extern syscall_openat
|
||||
extern syscall_read
|
||||
syscalltab:
|
||||
dq syscall_print
|
||||
dq syscall_mmap
|
||||
dq syscall_openat
|
||||
|
||||
syscallcount equ 3
|
||||
dq syscall_read
|
||||
syscallcount equ 4
|
||||
section .text
|
||||
global arch_syscall_entry
|
||||
; on entry:
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <kernel/syscalls.h>
|
||||
#include <kernel/vfs.h>
|
||||
#include <kernel/file.h>
|
||||
#include <errno.h>
|
||||
|
||||
syscallret_t syscall_read(context_t *context, int fd, void *buffer, size_t size) {
|
||||
syscallret_t ret = {
|
||||
.ret = -1
|
||||
};
|
||||
|
||||
void *kernelbuff = vmm_map(NULL, size, VMM_FLAGS_ALLOCATE, ARCH_MMU_FLAGS_READ | ARCH_MMU_FLAGS_WRITE | ARCH_MMU_FLAGS_NOEXEC, NULL);
|
||||
if (kernelbuff == NULL) {
|
||||
ret.errno = ENOMEM;
|
||||
return ret;
|
||||
}
|
||||
|
||||
file_t *file = fd_get(fd);
|
||||
|
||||
if (file == NULL || (file->flags & FILE_READ) == 0) {
|
||||
ret.errno = EBADF;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
// TODO pass flags
|
||||
size_t bytesread;
|
||||
ret.errno = vfs_read(file->vnode, kernelbuff, size, file->offset, &bytesread, 0);
|
||||
|
||||
if (ret.errno)
|
||||
goto cleanup;
|
||||
|
||||
ret.ret = bytesread;
|
||||
ret.errno = 0;
|
||||
|
||||
// TODO safe memcpy
|
||||
memcpy(buffer, kernelbuff, bytesread);
|
||||
|
||||
cleanup:
|
||||
if (file)
|
||||
fd_release(file);
|
||||
|
||||
vmm_unmap(kernelbuff, size, 0);
|
||||
return ret;
|
||||
}
|
||||
@@ -113,10 +113,10 @@ index 0000000..e5b087a
|
||||
+
|
||||
diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp
|
||||
new file mode 100644
|
||||
index 0000000..6715503
|
||||
index 0000000..80a5884
|
||||
--- /dev/null
|
||||
+++ mlibc-workdir/sysdeps/astral/generic/generic.cpp
|
||||
@@ -0,0 +1,143 @@
|
||||
@@ -0,0 +1,148 @@
|
||||
+#include <bits/ensure.h>
|
||||
+#include <mlibc/debug.hpp>
|
||||
+#include <mlibc/all-sysdeps.hpp>
|
||||
@@ -225,9 +225,14 @@ index 0000000..6715503
|
||||
+ *fd = ret;
|
||||
+ return 0;
|
||||
+ };
|
||||
+
|
||||
+ int sys_read(int fd, void *buff, size_t count, ssize_t *bytes_read) {
|
||||
+ return ENOSYS;
|
||||
+ long readc;
|
||||
+ long error = syscall(SYSCALL_READ, &readc, fd, (uint64_t)buff, count);
|
||||
+ *bytes_read = readc;
|
||||
+ return error;
|
||||
+ }
|
||||
+
|
||||
+ int sys_write(int fd, const void *buff, size_t count, ssize_t *bytes_written) {
|
||||
+ return ENOSYS;
|
||||
+ }
|
||||
@@ -279,16 +284,17 @@ index 0000000..990eca0
|
||||
+#endif
|
||||
diff --git mlibc-workdir/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h
|
||||
new file mode 100644
|
||||
index 0000000..6ef92cd
|
||||
index 0000000..17c0045
|
||||
--- /dev/null
|
||||
+++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h
|
||||
@@ -0,0 +1,26 @@
|
||||
@@ -0,0 +1,27 @@
|
||||
+#ifndef _SYSCALL_H_INCLUDE
|
||||
+#define _SYSCALL_H_INCLUDE
|
||||
+
|
||||
+#define SYSCALL_PRINT 0
|
||||
+#define SYSCALL_MMAP 1
|
||||
+#define SYSCALL_OPENAT 2
|
||||
+#define SYSCALL_READ 3
|
||||
+
|
||||
+#include <stddef.h>
|
||||
+#include <stdint.h>
|
||||
|
||||
Reference in New Issue
Block a user