feature (syscalls): implemented syscalls (nx_fdelete, nx_flist)

This commit is contained in:
antifallobst 2023-04-20 16:26:20 +02:00
parent 2fa608ac41
commit e3d3faf8e0
2 changed files with 80 additions and 25 deletions

View File

@ -57,11 +57,9 @@ void kmain(boot_info_T boot_info) {
log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n");
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);
uint32_t needed_mem = 0;
syscall_perform(SYSCALL_FILES_LIST, (uint64_t)"/initrd/", 0, buffer, &needed_mem);
memory_hexdump(buffer, 64);
// vfs_node_T* node = vfs_resolve_path(&g_root_fs, "/initrd/test.elf");

View File

@ -3,52 +3,103 @@
#include "platform/syscall.h"
#include "utils/logger.h"
#include "utils/panic.h"
#include "utils/memory.h"
#include "proc/scheduler.h"
#include "drivers/fs/vfs.h"
void syscall_handle_nx_fopen(cpu_state_T* state) {
uint64_t arg1 = state->rdi;
uint64_t arg2 = state->rsi;
string_t path = (string_t)state->rdi; // arg1
uint64_t len = state->rsi; // arg2
file_descriptor_t* fd_ptr = (file_descriptor_t*)state->rdx; // arg3
vfs_node_T *node = vfs_resolve_path(&g_root_fs, (string_t) arg1);
vfs_node_T *node = vfs_resolve_path(&g_root_fs, path);
if (node == NULL) {
state->rax = STATUS_RESOURCE_NOT_AVAILABLE;
return;
}
*(file_descriptor_t*)(arg2) = file_descriptor_request(scheduler_get_current_process()->fd_array, node);
*fd_ptr = 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_t fd = (file_descriptor_t)state->rdi; // arg1
file_descriptor_free(scheduler_get_current_process()->fd_array, (file_descriptor_t)arg1);
file_descriptor_free(scheduler_get_current_process()->fd_array, fd);
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;
file_descriptor_t fd = (file_descriptor_t)state->rdi; // arg1
uint64_t offset = state->rsi; // arg2
uint8_t* mem = (uint8_t*)state->rdx; // arg3
uint64_t n = state->rcx; // arg4
vfs_node_T* node = file_descriptor_resolve(scheduler_get_current_process()->fd_array, (file_descriptor_t)arg1);
vfs_node_T* node = file_descriptor_resolve(scheduler_get_current_process()->fd_array, fd);
vfs_file_read(node, arg2, arg4, (uint8_t*)arg3);
vfs_file_read(node, offset, n, mem);
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;
file_descriptor_t fd = (file_descriptor_t)state->rdi; // arg1
uint64_t offset = state->rsi; // arg2
uint8_t* mem = (uint8_t*)state->rdx; // arg3
uint64_t n = state->rcx; // arg4
vfs_node_T* node = file_descriptor_resolve(scheduler_get_current_process()->fd_array, (file_descriptor_t)arg1);
vfs_node_T* node = file_descriptor_resolve(scheduler_get_current_process()->fd_array, fd);
vfs_file_write(node, arg2, arg4, (uint8_t*)arg3);
vfs_file_write(node, offset, n, mem);
state->rax = STATUS_SUCCESS;
}
void syscall_handle_nx_fdelete(cpu_state_T* state) {
string_t path = (string_t)state->rdi; // arg1
string_t len = (string_t)state->rsi; // arg2
vfs_node_T* node = vfs_resolve_path(&g_root_fs, path);
if (node == NULL) {
state->rax = STATUS_RESOURCE_NOT_AVAILABLE;
return;
}
vfs_node_delete(node);
state->rax = STATUS_SUCCESS;
}
void syscall_handle_nx_flist(cpu_state_T* state) {
string_t path = (string_t)state->rdi; // arg1
uint64_t len = state->rsi; // arg2
uint8_t* mem = (uint8_t*)state->rdx; // arg3
uint32_t* needed_mem = (uint32_t*)state->rcx; // arg4
vfs_node_T* node = vfs_resolve_path(&g_root_fs, path);
if (node == NULL) {
state->rax = STATUS_RESOURCE_NOT_AVAILABLE;
return;
}
uint32_t position = 0;
vfs_node_T* child = node->childs;
while (child != NULL) {
uint32_t strlen = string_length(child->name);
if (mem != NULL) {
memory_copy(child->name, &mem[position], strlen);
mem[position + strlen] = '\0';
}
position += strlen + 1;
child = child->next;
}
if (mem == NULL) {
*needed_mem = position;
}
state->rax = STATUS_SUCCESS;
}
@ -57,8 +108,6 @@ void syscall_handle_nx_fwrite(cpu_state_T* state) {
cpu_state_T* syscall_handle(cpu_state_T* state) {
cpu_state_T* return_state = state;
syscall_group_E group_id = (state->rax & 0xFF00) >> 8;
// read arguments
uint64_t arg1 = state->rdi;
switch (group_id) {
@ -81,6 +130,14 @@ cpu_state_T* syscall_handle(cpu_state_T* state) {
syscall_handle_nx_fwrite(state);
break;
}
case SYSCALL_FILES_DELETE: {
syscall_handle_nx_fdelete(state);
break;
}
case SYSCALL_FILES_LIST: {
syscall_handle_nx_flist(state);
break;
}
}
break;
}