From 6d102f65207a64c167f9b3443bd378f4694c4873 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Wed, 19 Apr 2023 01:02:45 +0200 Subject: [PATCH] feature (syscalls): implemented basic file syscalls (nx_fopen, nx_fclose, nx_fread, nx_fwrite) --- kernel/inc/proc/process.h | 28 ++++++++++--------- kernel/src/kmain.c | 9 ++++--- kernel/src/platform/syscall.c | 51 +++++++++++++++++++++++++++++++++-- kernel/src/proc/process.c | 3 +++ 4 files changed, 73 insertions(+), 18 deletions(-) diff --git a/kernel/inc/proc/process.h b/kernel/inc/proc/process.h index f204312..c93eded 100644 --- a/kernel/inc/proc/process.h +++ b/kernel/inc/proc/process.h @@ -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" @@ -21,22 +22,23 @@ typedef enum { typedef struct process_T process_T; struct process_T { - char name [128]; - pid_t id; - void* chunk; - uint32_t chunk_id; + char name [128]; + pid_t id; + void* chunk; + uint32_t chunk_id; - page_map_T* page_map; - elf_executable_T* executable; + page_map_T* page_map; + elf_executable_T* executable; + file_descriptor_array_T* fd_array; - uint32_t num_threads; - void* threads; - bitmap_T thread_ids; + uint32_t num_threads; + void* threads; + bitmap_T thread_ids; - process_T* parent; - process_T* childs; - process_T* prev; - process_T* next; + process_T* parent; + process_T* childs; + process_T* prev; + process_T* next; }; void process_kernel_spawn (elf_executable_T* executable); diff --git a/kernel/src/kmain.c b/kernel/src/kmain.c index 85b6131..76dbde3 100644 --- a/kernel/src/kmain.c +++ b/kernel/src/kmain.c @@ -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); diff --git a/kernel/src/platform/syscall.c b/kernel/src/platform/syscall.c index fdf0cde..0be97a6 100644 --- a/kernel/src/platform/syscall.c +++ b/kernel/src/platform/syscall.c @@ -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: { diff --git a/kernel/src/proc/process.c b/kernel/src/proc/process.c index 93a42ad..0bc7f2a 100644 --- a/kernel/src/proc/process.c +++ b/kernel/src/proc/process.c @@ -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);