diff --git a/include/paging.h b/include/paging.h index 65831f2..44c640b 100644 --- a/include/paging.h +++ b/include/paging.h @@ -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); diff --git a/include/stdio.h b/include/stdio.h index 7ee13e2..40bc973 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -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 diff --git a/include/task.h b/include/task.h index ce432ac..24ddccd 100644 --- a/include/task.h +++ b/include/task.h @@ -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 \ No newline at end of file diff --git a/src/mm/paging.c b/src/mm/paging.c index bb13dbd..80fc346 100644 --- a/src/mm/paging.c +++ b/src/mm/paging.c @@ -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); +} \ No newline at end of file diff --git a/src/tasking/syscalls.c b/src/tasking/syscalls.c index 37e20b6..3c62043 100644 --- a/src/tasking/syscalls.c +++ b/src/tasking/syscalls.c @@ -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 { diff --git a/src/tasking/task.c b/src/tasking/task.c index fe2ea4b..1967f96 100644 --- a/src/tasking/task.c +++ b/src/tasking/task.c @@ -2,6 +2,7 @@ #include "../include/stdio.h" #include "../include/paging.h" #include "../include/string.h" +#include "../include/liballoc.h" #include @@ -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(); } \ No newline at end of file