syscalls: start implementing some basic syscalls

This commit is contained in:
2025-06-20 21:59:08 +03:00
parent a9789b7f57
commit 4ccae473ca
8 changed files with 110 additions and 12 deletions
+86
View File
@@ -0,0 +1,86 @@
#include "../include/syscalls.h"
#include <stddef.h>
#include "../include/stdio.h"
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#define EBADF 9
typedef unsigned short umode_t
int sys_write(TrapFrame *tf)
{
if(tf->ebx < 3)
{
//stdin, stdout or stderr
if(tf->ebx == STDOUT_FILENO)
{
print((char*)tf->ecx, tf->edx);
tf->ecx++;
}
}
else
{
if(current->file_descriptors[tf->ebx] == NULL)
{
return -EBADF;
}
return fat_fwrite(current->file_descriptors[tf->ebx], (void*)tf->ecx, tf->edx);
}
}
int sys_open(TrapFrame *tf)
{
const char* filename = tf->ebx;
int flags = tf->ecx;
umode_t mode = tf->edx;
int fd = -1;
for(int i = 3; i < MAX_OPEN_FILES; i++)
{
if(current->file_descriptors[i] == NULL)
{
fat_fopen(current->file_descriptors[i], filename, (char)mode);
fd = i;
}
}
return fd;
}
int sys_close(TrapFrame *tf)
{
int fd = tf->ebx;
if(current->file_descriptors[fd] != NULL)
{
return fat_fclose(current->file_descriptors[fd]);
}
return -1;
}
void handle_syscall(TrapFrame *tf)
{
debug_log("EAX: %X ", tf->eax);
debug_log("EBX: %X ", tf->ebx);
debug_log("ECX: %s ", tf->ecx);
debug_log("EDX: %X\n", tf->edx);
if(tf->eax == 4)//write
{
int r = sys_write(tf);
tf->eax = r;
}
if(tf->eax == 5)//open
{
int r = sys_open(tf);
tf->eax = r;
}
if(tf->eax == 6)//close
{
int r = sys_close(tf);
tf->eax = r;
}
}