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
+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();
}