From 4ccae473caceede2054a2d2c58d5406af0300a6e Mon Sep 17 00:00:00 2001 From: Ruslan Isaev Date: Fri, 20 Jun 2025 21:59:08 +0300 Subject: [PATCH] syscalls: start implementing some basic syscalls --- Makefile | 5 ++- include/stdio.h | 4 ++ include/syscalls.h | 7 +++ include/task.h | 7 +++ src/tasking/exec_from_file.c | 2 +- src/tasking/syscalls.c | 86 ++++++++++++++++++++++++++++++++++++ src/tasking/task.c | 9 ---- src/utils/stdio.c | 2 +- 8 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 include/syscalls.h create mode 100644 src/tasking/syscalls.c diff --git a/Makefile b/Makefile index 2c337a7..85a8344 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/include/stdio.h b/include/stdio.h index 0f762d2..7ee13e2 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -1,11 +1,15 @@ #ifndef STDIO_H #define STDIO_H +#include +#include + //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*); diff --git a/include/syscalls.h b/include/syscalls.h new file mode 100644 index 0000000..9f6987e --- /dev/null +++ b/include/syscalls.h @@ -0,0 +1,7 @@ +#ifndef SYSCALLS_H +#define SYSCALLS_H +#include "../include/task.h" + +void handle_syscall(TrapFrame *tf); + +#endif \ No newline at end of file diff --git a/include/task.h b/include/task.h index a05d8af..ce432ac 100644 --- a/include/task.h +++ b/include/task.h @@ -2,6 +2,7 @@ #define TASK_H #include +#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(); diff --git a/src/tasking/exec_from_file.c b/src/tasking/exec_from_file.c index 855a97c..052f718 100644 --- a/src/tasking/exec_from_file.c +++ b/src/tasking/exec_from_file.c @@ -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); diff --git a/src/tasking/syscalls.c b/src/tasking/syscalls.c new file mode 100644 index 0000000..dfbe921 --- /dev/null +++ b/src/tasking/syscalls.c @@ -0,0 +1,86 @@ +#include "../include/syscalls.h" +#include +#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; + } +} \ No newline at end of file diff --git a/src/tasking/task.c b/src/tasking/task.c index ca63be7..fe2ea4b 100644 --- a/src/tasking/task.c +++ b/src/tasking/task.c @@ -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); } \ No newline at end of file diff --git a/src/utils/stdio.c b/src/utils/stdio.c index d93be35..5e039cb 100644 --- a/src/utils/stdio.c +++ b/src/utils/stdio.c @@ -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)