diff --git a/include/task.h b/include/task.h index b01b03c..062872e 100644 --- a/include/task.h +++ b/include/task.h @@ -68,6 +68,8 @@ typedef struct Process } Process; 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); diff --git a/src/tasking/syscalls.c b/src/tasking/syscalls.c index acf35da..05dffb0 100644 --- a/src/tasking/syscalls.c +++ b/src/tasking/syscalls.c @@ -62,19 +62,70 @@ int sys_fork(TrapFrame *tf) { debug_log("FORKFORKFORKFORK\n"); - uint32_t* new_pd = copy_page_dir(current->pagedir) - 0xC0000000; - debug_log("got this shit done hehe\n"); + uint32_t* new_pd = copy_page_dir(current->pagedir); + 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); - while(1){} + void *p_physical = alloc_page(); + 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); + debug_log("Kernel stack copied\n"); - child->tf->eax = 0; - tf->eax = child->pid; + uint32_t tf_offset = (uint32_t)current->tf - (uint32_t)current->kstack; + 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; } @@ -168,7 +219,6 @@ int sys_close(TrapFrame *tf) void handle_syscall(TrapFrame *tf) { - asm("cli"); /* debug_log("EAX: %X ", tf->eax); debug_log("EBX: %X ", tf->ebx); @@ -182,9 +232,10 @@ void handle_syscall(TrapFrame *tf) tf->eax = r; } - if(tf->eax == 2) + if(tf->eax == 2)//fork { int r = sys_fork(tf); + debug_log("RRRRRRRR = %X\n", r); tf->eax = r; } @@ -209,5 +260,4 @@ void handle_syscall(TrapFrame *tf) int r = sys_close(tf); tf->eax = r; } - asm("sti"); } \ No newline at end of file