feature (syscalls): implemented basic file syscalls (nx_fopen, nx_fclose, nx_fread, nx_fwrite)

This commit is contained in:
antifallobst 2023-04-19 01:02:45 +02:00
parent 88c46a8f38
commit 6d102f6520
4 changed files with 73 additions and 18 deletions

View File

@ -6,6 +6,7 @@
#include "utils/stdtypes.h"
#include "utils/string.h"
#include "utils/bitmap.h"
#include "proc/file_descriptor.h"
#include "mm/page_map.h"
#include "drivers/elf/elf.h"
@ -28,6 +29,7 @@ struct process_T {
page_map_T* page_map;
elf_executable_T* executable;
file_descriptor_array_T* fd_array;
uint32_t num_threads;
void* threads;

View File

@ -57,9 +57,12 @@ void kmain(boot_info_T boot_info) {
log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n");
status_E status = syscall_perform(SYSCALL_FILES_OPEN, (uint64_t)"/initrd/test.txt", 0, 0, 0);
log(LOG_DEBUG, "syscall returned status: %s", g_status_code_strings[status]);
file_descriptor_t fd;
syscall_perform(SYSCALL_FILES_OPEN, (uint64_t)"/initrd/test.txt", &fd, 0, 0);
syscall_perform(SYSCALL_FILES_WRITE, fd, 0, "Write test", 10);
char buffer[64];
syscall_perform(SYSCALL_FILES_READ, fd, 0, (uint64_t)buffer, 64);
memory_hexdump(buffer, 64);
// vfs_node_T* node = vfs_resolve_path(&g_root_fs, "/initrd/test.elf");
// void* buffer = memory_allocate(node->size);

View File

@ -7,8 +7,8 @@
#include "drivers/fs/vfs.h"
void syscall_handle_nx_fopen(cpu_state_T* state) {
// read arguments
uint64_t arg1 = state->rdi;
uint64_t arg2 = state->rsi;
vfs_node_T *node = vfs_resolve_path(&g_root_fs, (string_t) arg1);
if (node == NULL) {
@ -16,7 +16,41 @@ void syscall_handle_nx_fopen(cpu_state_T* state) {
return;
}
log(LOG_DEBUG, "resolved file '%s' -> 0x%x", arg1, node);
*(file_descriptor_t*)(arg2) = file_descriptor_request(scheduler_get_current_process()->fd_array, node);
}
void syscall_handle_nx_fclose(cpu_state_T* state) {
uint64_t arg1 = state->rdi;
file_descriptor_free(scheduler_get_current_process()->fd_array, (file_descriptor_t)arg1);
state->rax = STATUS_SUCCESS;
}
void syscall_handle_nx_fread(cpu_state_T* state) {
uint64_t arg1 = state->rdi;
uint64_t arg2 = state->rsi;
uint64_t arg3 = state->rdx;
uint64_t arg4 = state->rcx;
vfs_node_T* node = file_descriptor_resolve(scheduler_get_current_process()->fd_array, (file_descriptor_t)arg1);
vfs_file_read(node, arg2, arg4, (uint8_t*)arg3);
state->rax = STATUS_SUCCESS;
}
void syscall_handle_nx_fwrite(cpu_state_T* state) {
uint64_t arg1 = state->rdi;
uint64_t arg2 = state->rsi;
uint64_t arg3 = state->rdx;
uint64_t arg4 = state->rcx;
vfs_node_T* node = file_descriptor_resolve(scheduler_get_current_process()->fd_array, (file_descriptor_t)arg1);
vfs_file_write(node, arg2, arg4, (uint8_t*)arg3);
state->rax = STATUS_SUCCESS;
}
@ -35,6 +69,18 @@ cpu_state_T* syscall_handle(cpu_state_T* state) {
syscall_handle_nx_fopen(state);
break;
}
case SYSCALL_FILES_CLOSE: {
syscall_handle_nx_fclose(state);
break;
}
case SYSCALL_FILES_READ: {
syscall_handle_nx_fread(state);
break;
}
case SYSCALL_FILES_WRITE: {
syscall_handle_nx_fwrite(state);
break;
}
}
break;
}
@ -77,6 +123,7 @@ cpu_state_T* syscall_handle(cpu_state_T* state) {
break;
}
}
break;
}
default: {

View File

@ -12,6 +12,7 @@ void process_kernel_spawn(elf_executable_T* executable) {
process->threads = NULL;
process->parent = NULL;
process->executable = executable;
process->fd_array = file_descriptor_array_alloc();
memory_copy("kernel", process->name, 7);
@ -29,6 +30,7 @@ pid_t process_spawn(pid_t parent, string_t name, elf_executable_T* executable, v
process->thread_ids = bitmap_init(MAX_THREADS_PER_PROCESS);
process->parent = scheduler_get_process(parent);
process->executable = executable;
process->fd_array = file_descriptor_array_alloc();
memory_copy(name, process->name, MIN(string_length(name), 127));
@ -61,6 +63,7 @@ void process_kill_pid(pid_t pid) {
void process_kill(process_T* process) {
scheduler_kill_process(process);
file_descriptor_array_destruct(process->fd_array);
elf_executable_destruct(process->executable);
page_map_destruct(process->page_map);
bitmap_destruct(&process->thread_ids);