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
+4 -1
View File
@@ -3,7 +3,7 @@ OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o \
bin/utils.o bin/string.o bin/stdio.o bin/disk.o bin/pic.o bin/pit.o \
bin/keyboard.o bin/paging.o bin/page_alloc.o bin/page_tables.o \
bin/fat.o bin/task.o bin/switch.o bin/sys_exit.o bin/gdt.o bin/isr-asm.o \
bin/kheap.o bin/liballoc.o bin/exec_from_file.o
bin/kheap.o bin/liballoc.o bin/exec_from_file.o bin/syscalls.o
# Define the compiler and assembler
#CC = i686-elf-gcc -D__is_katauos
@@ -137,6 +137,9 @@ bin/liballoc.o: src/mm/liballoc.c
bin/exec_from_file.o: src/tasking/exec_from_file.c
$(CC) $(CFLAGS) -c $< -o $@
bin/syscalls.o: src/tasking/syscalls.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -rf bin/
rm -f $(OUTPUT)
+4
View File
@@ -1,11 +1,15 @@
#ifndef STDIO_H
#define STDIO_H
#include <stddef.h>
#include <stdbool.h>
//TODO: these should be contained in newlib i guess
//but until i implement it let it be here yo
#define EOF (-1)
#define INT_MAX 2147483647
bool print(const char* data, size_t length);
int printf(const char* __restrict, ...);
int putchar(int);
int puts(const char*);
+7
View File
@@ -0,0 +1,7 @@
#ifndef SYSCALLS_H
#define SYSCALLS_H
#include "../include/task.h"
void handle_syscall(TrapFrame *tf);
#endif
+7
View File
@@ -2,6 +2,7 @@
#define TASK_H
#include <stdint.h>
#include "../include/fat.h"
// Сегменты для ядра и пользователя
#define SEG_KCODE 0x08 // Сегмент кода ядра
@@ -14,6 +15,8 @@
// Флаги процессора
#define FL_IF 0x202 // Разрешить прерывания
#define MAX_OPEN_FILES 32
typedef enum TaskState
{
Ready,
@@ -58,8 +61,12 @@ typedef struct Process
TrapFrame* tf; // Trap frame (только для ring 3)
uint32_t ring; // Уровень привилегий (0 или 3)
struct Process* next; // Следующий процесс
file_t* file_descriptors[MAX_OPEN_FILES];
} Process;
extern Process* current;
Process* task_create(uint32_t func, uint32_t user_esp, uint32_t ring, uint32_t* pagedir);
void scheduler_init();
void schedule();
+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)