feature (syscalls): implemented basic file syscalls (nx_fopen, nx_fclose, nx_fread, nx_fwrite)
This commit is contained in:
parent
88c46a8f38
commit
6d102f6520
|
@ -6,6 +6,7 @@
|
||||||
#include "utils/stdtypes.h"
|
#include "utils/stdtypes.h"
|
||||||
#include "utils/string.h"
|
#include "utils/string.h"
|
||||||
#include "utils/bitmap.h"
|
#include "utils/bitmap.h"
|
||||||
|
#include "proc/file_descriptor.h"
|
||||||
#include "mm/page_map.h"
|
#include "mm/page_map.h"
|
||||||
#include "drivers/elf/elf.h"
|
#include "drivers/elf/elf.h"
|
||||||
|
|
||||||
|
@ -21,22 +22,23 @@ typedef enum {
|
||||||
|
|
||||||
typedef struct process_T process_T;
|
typedef struct process_T process_T;
|
||||||
struct process_T {
|
struct process_T {
|
||||||
char name [128];
|
char name [128];
|
||||||
pid_t id;
|
pid_t id;
|
||||||
void* chunk;
|
void* chunk;
|
||||||
uint32_t chunk_id;
|
uint32_t chunk_id;
|
||||||
|
|
||||||
page_map_T* page_map;
|
page_map_T* page_map;
|
||||||
elf_executable_T* executable;
|
elf_executable_T* executable;
|
||||||
|
file_descriptor_array_T* fd_array;
|
||||||
|
|
||||||
uint32_t num_threads;
|
uint32_t num_threads;
|
||||||
void* threads;
|
void* threads;
|
||||||
bitmap_T thread_ids;
|
bitmap_T thread_ids;
|
||||||
|
|
||||||
process_T* parent;
|
process_T* parent;
|
||||||
process_T* childs;
|
process_T* childs;
|
||||||
process_T* prev;
|
process_T* prev;
|
||||||
process_T* next;
|
process_T* next;
|
||||||
};
|
};
|
||||||
|
|
||||||
void process_kernel_spawn (elf_executable_T* executable);
|
void process_kernel_spawn (elf_executable_T* executable);
|
||||||
|
|
|
@ -57,9 +57,12 @@ void kmain(boot_info_T boot_info) {
|
||||||
|
|
||||||
log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n");
|
log(LOG_INFO, "!=====[ Kernel Initialized ]=====!\n");
|
||||||
|
|
||||||
status_E status = syscall_perform(SYSCALL_FILES_OPEN, (uint64_t)"/initrd/test.txt", 0, 0, 0);
|
file_descriptor_t fd;
|
||||||
|
syscall_perform(SYSCALL_FILES_OPEN, (uint64_t)"/initrd/test.txt", &fd, 0, 0);
|
||||||
log(LOG_DEBUG, "syscall returned status: %s", g_status_code_strings[status]);
|
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");
|
// vfs_node_T* node = vfs_resolve_path(&g_root_fs, "/initrd/test.elf");
|
||||||
// void* buffer = memory_allocate(node->size);
|
// void* buffer = memory_allocate(node->size);
|
||||||
|
|
|
@ -7,8 +7,8 @@
|
||||||
#include "drivers/fs/vfs.h"
|
#include "drivers/fs/vfs.h"
|
||||||
|
|
||||||
void syscall_handle_nx_fopen(cpu_state_T* state) {
|
void syscall_handle_nx_fopen(cpu_state_T* state) {
|
||||||
// read arguments
|
|
||||||
uint64_t arg1 = state->rdi;
|
uint64_t arg1 = state->rdi;
|
||||||
|
uint64_t arg2 = state->rsi;
|
||||||
|
|
||||||
vfs_node_T *node = vfs_resolve_path(&g_root_fs, (string_t) arg1);
|
vfs_node_T *node = vfs_resolve_path(&g_root_fs, (string_t) arg1);
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
|
@ -16,7 +16,41 @@ void syscall_handle_nx_fopen(cpu_state_T* state) {
|
||||||
return;
|
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);
|
syscall_handle_nx_fopen(state);
|
||||||
break;
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -77,6 +123,7 @@ cpu_state_T* syscall_handle(cpu_state_T* state) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
|
|
|
@ -12,6 +12,7 @@ void process_kernel_spawn(elf_executable_T* executable) {
|
||||||
process->threads = NULL;
|
process->threads = NULL;
|
||||||
process->parent = NULL;
|
process->parent = NULL;
|
||||||
process->executable = executable;
|
process->executable = executable;
|
||||||
|
process->fd_array = file_descriptor_array_alloc();
|
||||||
|
|
||||||
memory_copy("kernel", process->name, 7);
|
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->thread_ids = bitmap_init(MAX_THREADS_PER_PROCESS);
|
||||||
process->parent = scheduler_get_process(parent);
|
process->parent = scheduler_get_process(parent);
|
||||||
process->executable = executable;
|
process->executable = executable;
|
||||||
|
process->fd_array = file_descriptor_array_alloc();
|
||||||
|
|
||||||
memory_copy(name, process->name, MIN(string_length(name), 127));
|
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) {
|
void process_kill(process_T* process) {
|
||||||
scheduler_kill_process(process);
|
scheduler_kill_process(process);
|
||||||
|
|
||||||
|
file_descriptor_array_destruct(process->fd_array);
|
||||||
elf_executable_destruct(process->executable);
|
elf_executable_destruct(process->executable);
|
||||||
page_map_destruct(process->page_map);
|
page_map_destruct(process->page_map);
|
||||||
bitmap_destruct(&process->thread_ids);
|
bitmap_destruct(&process->thread_ids);
|
||||||
|
|
Loading…
Reference in New Issue