syscalls: start adding exit syscall, not done yet

This commit is contained in:
2025-06-21 18:54:52 +03:00
parent bb096f826a
commit 70d6aacb91
6 changed files with 102 additions and 3 deletions
+1
View File
@@ -39,6 +39,7 @@ uint32_t* get_page_dir();
void set_page_dir(uint32_t new_page_dir);
uint32_t phys_to_virt(uint32_t phys);
uint32_t* create_page_dir();
void destroy_page_dir(uint32_t* page_dir);
//page_alloc.c
void set_bit(uint32_t page_index);
+1 -1
View File
@@ -14,7 +14,7 @@ int printf(const char* __restrict, ...);
int putchar(int);
int puts(const char*);
//#define KATAU_DEBUG
#define KATAU_DEBUG
#ifdef KATAU_DEBUG
#define debug_log printf
+3 -1
View File
@@ -68,10 +68,12 @@ typedef struct Process
extern Process* current;
Process* task_create(uint32_t func, uint32_t user_esp, uint32_t ring, uint32_t* pagedir);
void task_kill(Process* proc);
void task_exit();
void scheduler_init();
void schedule();
void scheduler_lock();
void scheduler_unlock();
void handle_syscall(TrapFrame *tf);
#endif
+24
View File
@@ -146,3 +146,27 @@ uint32_t* create_page_dir()
return new_pd;
}
void destroy_page_dir(uint32_t* page_dir) {
// Iterate through all page directory entries
for (int i = 0; i < 1024; i++) {
if (page_dir[i] & 1) { // If page table exists
uint32_t* page_table = (uint32_t*)(page_dir[i] & 0xFFFFF000);
// Free all user pages (0-767)
if (i < 768) {
for (int j = 0; j < 1024; j++) {
if (page_table[j] & 1) {
free_page((void*)(page_table[j] & 0xFFFFF000));
}
}
}
// Free the page table itself
free_page(page_table);
}
}
// Free the page directory
free_page(page_dir);
}
+15 -1
View File
@@ -46,6 +46,14 @@ const char* flags_to_mode_str(int flags) {
return mode;
}
int sys_exit(TrapFrame *tf)
{
int error_code = tf->ebx;
task_kill(current);
return error_code;
}
int sys_read(TrapFrame *tf)
{
if(tf->ebx < 3)
@@ -72,7 +80,7 @@ int sys_write(TrapFrame *tf)
debug_log("\n=================SYS_WRITE output to stdout=====================\n");
print((char*)tf->ecx, tf->edx);
debug_log("\n=================END OF THAT SHIT=====================\n");
tf->ecx++;
return tf->edx;
}
}
else
@@ -140,6 +148,12 @@ void handle_syscall(TrapFrame *tf)
debug_log("EBX: %X ", tf->ebx);
debug_log("ECX: %s ", tf->ecx);
debug_log("EDX: %X\n", tf->edx);
if(tf->eax == 1)//exit
{
int r = sys_exit(tf);
tf->eax = r;
}
if(tf->eax == 3)//read
{
+58
View File
@@ -2,6 +2,7 @@
#include "../include/stdio.h"
#include "../include/paging.h"
#include "../include/string.h"
#include "../include/liballoc.h"
#include <stddef.h>
@@ -146,4 +147,61 @@ void scheduler_init()
uint32_t* kernel_tasks_pagedir = (uint32_t*)create_page_dir();
task_create((uint32_t)idle, 0, 0, kernel_tasks_pagedir);
}
void task_exit()
{
task_kill(current);
}
void task_kill(Process* proc)
{
scheduler_lock();
// Remove process from scheduler queue
Process* prev = NULL;
Process* curr = queue;
while (curr) {
if (curr == proc) {
if (prev) {
prev->next = curr->next;
} else {
queue = curr->next;
}
break;
}
prev = curr;
curr = curr->next;
}
for (int i = 0; i < MAX_OPEN_FILES; i++) {
if (proc->file_descriptors[i] != NULL) {
fat_fclose(proc->file_descriptors[i]);
free(proc->file_descriptors[i]);
proc->file_descriptors[i] = NULL;
}
}
// Free kernel stack
if (proc->kstack) {
uint32_t kstack_phys = (uint32_t)proc->kstack - 0xC0000000;
free_page((void*)kstack_phys);
}
// Free process structure
uint32_t proc_phys = (uint32_t)proc - 0xC0000000;
free_page((void*)proc_phys);
// Destroy page directory and free user pages
destroy_page_dir(proc->pagedir);
// Update task count and current process if needed
task_count--;
if (proc == current) {
current = NULL;
schedule(); // Switch to another process
}
scheduler_unlock();
}