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
+1 -1
View File
@@ -68,7 +68,7 @@ void exec_from_file(const char* filename)
uint8_t* file;
int result = read_file("/ebalo/testfile", &file);
int result = read_file("/ebalo/aaa", &file);
debug_log("result: %X\n", result);
debug_log("buffer: %s\n", file);
+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;
}
}
-9
View File
@@ -18,7 +18,6 @@ uint32_t task_count;
extern void trapret(void);
extern void switchProcess(Process* next);
extern Process* queue;
extern Process* current;
extern uint32_t pid_counter;
extern uint32_t task_count;
@@ -147,12 +146,4 @@ void scheduler_init()
uint32_t* kernel_tasks_pagedir = (uint32_t*)create_page_dir();
task_create((uint32_t)idle, 0, 0, kernel_tasks_pagedir);
}
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);
}
+1 -1
View File
@@ -69,7 +69,7 @@ void intToString(int value, char *buffer, char format) {
buffer[i] = '\0';
}
static bool print(const char* data, size_t length) {
bool print(const char* data, size_t length) {
const unsigned char* bytes = (const unsigned char*) data;
for (size_t i = 0; i < length; i++)
if (putchar(bytes[i]) == EOF)