syscalls: close to being done with fork syscall

This commit is contained in:
2025-07-07 22:22:10 +03:00
parent 7f861a8b92
commit c2293b8996
2 changed files with 63 additions and 11 deletions
+2
View File
@@ -68,6 +68,8 @@ typedef struct Process
} Process; } Process;
extern Process* current; extern Process* current;
extern uint32_t pid_counter;
extern Process* queue;
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);
+61 -11
View File
@@ -62,19 +62,70 @@ int sys_fork(TrapFrame *tf)
{ {
debug_log("FORKFORKFORKFORK\n"); debug_log("FORKFORKFORKFORK\n");
uint32_t* new_pd = copy_page_dir(current->pagedir) - 0xC0000000; uint32_t* new_pd = copy_page_dir(current->pagedir);
debug_log("got this shit done hehe\n"); if (!new_pd) {
debug_log("Failed to copy page directory\n");
return -1;
}
debug_log("new_pd: %X\n", new_pd);
set_page_dir((uint32_t)new_pd - 0xC0000000); void *p_physical = alloc_page();
while(1){} if (!p_physical) {
destroy_page_dir(new_pd);
return -1;
}
Process* child = (Process*)((uint32_t)p_physical + 0xC0000000);
map_page(p_physical, (void*)child, PAGE_PRESENT | PAGE_RW);
memset(child, 0, sizeof(Process));
Process* child = task_create(0, 0, 3, new_pd); child->pid = ++pid_counter;
child->state = Ready;
child->ring = current->ring;
child->pagedir = new_pd;
void *kstack_physical = alloc_page();
if (!kstack_physical) {
destroy_page_dir(new_pd);
free_page(p_physical);
debug_log("Failed to allocate kernel stack\n");
return -1;
}
child->kstack = (char*)((uint32_t)kstack_physical + 0xC0000000);
map_page(kstack_physical, child->kstack, PAGE_PRESENT | PAGE_RW);
debug_log("child->kstack: %X\n", child->kstack);
memcpy(child, current, sizeof(Process));
memcpy(child->kstack, current->kstack, KSTACKSIZE); memcpy(child->kstack, current->kstack, KSTACKSIZE);
debug_log("Kernel stack copied\n");
child->tf->eax = 0; uint32_t tf_offset = (uint32_t)current->tf - (uint32_t)current->kstack;
tf->eax = child->pid; child->tf = (TrapFrame*)((uint32_t)child->kstack + tf_offset);
debug_log("child->tf: %X\n", child->tf);
if (current->context) {
uint32_t context_offset = (uint32_t)current->context - (uint32_t)current->kstack;
child->context = (Context*)((uint32_t)child->kstack + context_offset);
debug_log("child->context: %X\n", child->context);
}
*child->tf = *tf;
child->tf->eax = 0; // Child returns 0
debug_log("Parent returns PID: %X, Child returns 0\n", child->pid);
for (int i = 0; i < MAX_OPEN_FILES; i++) {
child->file_descriptors[i] = current->file_descriptors[i];
}
debug_log("File descriptors copied\n");
if (!queue) {
queue = child;
current = queue;
} else {
Process* curr = queue;
while (curr->next)
curr = curr->next;
curr->next = child;
}
debug_log("Child added to queue\n");
return child->pid; return child->pid;
} }
@@ -168,7 +219,6 @@ int sys_close(TrapFrame *tf)
void handle_syscall(TrapFrame *tf) void handle_syscall(TrapFrame *tf)
{ {
asm("cli");
/* /*
debug_log("EAX: %X ", tf->eax); debug_log("EAX: %X ", tf->eax);
debug_log("EBX: %X ", tf->ebx); debug_log("EBX: %X ", tf->ebx);
@@ -182,9 +232,10 @@ void handle_syscall(TrapFrame *tf)
tf->eax = r; tf->eax = r;
} }
if(tf->eax == 2) if(tf->eax == 2)//fork
{ {
int r = sys_fork(tf); int r = sys_fork(tf);
debug_log("RRRRRRRR = %X\n", r);
tf->eax = r; tf->eax = r;
} }
@@ -209,5 +260,4 @@ void handle_syscall(TrapFrame *tf)
int r = sys_close(tf); int r = sys_close(tf);
tf->eax = r; tf->eax = r;
} }
asm("sti");
} }