syscalls: start adding exit syscall, not done yet
This commit is contained in:
@@ -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
@@ -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
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user