syscalls: start adding exit syscall, not done yet
This commit is contained in:
@@ -39,6 +39,7 @@ uint32_t* get_page_dir();
|
|||||||
void set_page_dir(uint32_t new_page_dir);
|
void set_page_dir(uint32_t new_page_dir);
|
||||||
uint32_t phys_to_virt(uint32_t phys);
|
uint32_t phys_to_virt(uint32_t phys);
|
||||||
uint32_t* create_page_dir();
|
uint32_t* create_page_dir();
|
||||||
|
void destroy_page_dir(uint32_t* page_dir);
|
||||||
|
|
||||||
//page_alloc.c
|
//page_alloc.c
|
||||||
void set_bit(uint32_t page_index);
|
void set_bit(uint32_t page_index);
|
||||||
|
|||||||
+1
-1
@@ -14,7 +14,7 @@ int printf(const char* __restrict, ...);
|
|||||||
int putchar(int);
|
int putchar(int);
|
||||||
int puts(const char*);
|
int puts(const char*);
|
||||||
|
|
||||||
//#define KATAU_DEBUG
|
#define KATAU_DEBUG
|
||||||
|
|
||||||
#ifdef KATAU_DEBUG
|
#ifdef KATAU_DEBUG
|
||||||
#define debug_log printf
|
#define debug_log printf
|
||||||
|
|||||||
+3
-1
@@ -68,10 +68,12 @@ typedef struct Process
|
|||||||
extern Process* current;
|
extern Process* current;
|
||||||
|
|
||||||
Process* task_create(uint32_t func, uint32_t user_esp, uint32_t ring, uint32_t* pagedir);
|
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 scheduler_init();
|
||||||
void schedule();
|
void schedule();
|
||||||
void scheduler_lock();
|
void scheduler_lock();
|
||||||
void scheduler_unlock();
|
void scheduler_unlock();
|
||||||
void handle_syscall(TrapFrame *tf);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -146,3 +146,27 @@ uint32_t* create_page_dir()
|
|||||||
|
|
||||||
return new_pd;
|
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;
|
return mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int sys_exit(TrapFrame *tf)
|
||||||
|
{
|
||||||
|
int error_code = tf->ebx;
|
||||||
|
|
||||||
|
task_kill(current);
|
||||||
|
return error_code;
|
||||||
|
}
|
||||||
|
|
||||||
int sys_read(TrapFrame *tf)
|
int sys_read(TrapFrame *tf)
|
||||||
{
|
{
|
||||||
if(tf->ebx < 3)
|
if(tf->ebx < 3)
|
||||||
@@ -72,7 +80,7 @@ int sys_write(TrapFrame *tf)
|
|||||||
debug_log("\n=================SYS_WRITE output to stdout=====================\n");
|
debug_log("\n=================SYS_WRITE output to stdout=====================\n");
|
||||||
print((char*)tf->ecx, tf->edx);
|
print((char*)tf->ecx, tf->edx);
|
||||||
debug_log("\n=================END OF THAT SHIT=====================\n");
|
debug_log("\n=================END OF THAT SHIT=====================\n");
|
||||||
tf->ecx++;
|
return tf->edx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -141,6 +149,12 @@ void handle_syscall(TrapFrame *tf)
|
|||||||
debug_log("ECX: %s ", tf->ecx);
|
debug_log("ECX: %s ", tf->ecx);
|
||||||
debug_log("EDX: %X\n", tf->edx);
|
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
|
if(tf->eax == 3)//read
|
||||||
{
|
{
|
||||||
int r = sys_read(tf);
|
int r = sys_read(tf);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "../include/stdio.h"
|
#include "../include/stdio.h"
|
||||||
#include "../include/paging.h"
|
#include "../include/paging.h"
|
||||||
#include "../include/string.h"
|
#include "../include/string.h"
|
||||||
|
#include "../include/liballoc.h"
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
@@ -147,3 +148,60 @@ void scheduler_init()
|
|||||||
|
|
||||||
task_create((uint32_t)idle, 0, 0, kernel_tasks_pagedir);
|
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