diff options
author | sotech117 <michael_foiani@brown.edu> | 2024-05-13 09:27:24 +0000 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2024-05-13 09:27:24 +0000 |
commit | f09878f6327426631d9419d825a4e8396e3b9dc4 (patch) | |
tree | 009d1f1b1386baf6d07b3b7d9a436590ada14094 /kernel/proc/fork.c | |
parent | 0e2acbe54e5800621692c2f6e9e9590aa369e165 (diff) |
weenix
Diffstat (limited to 'kernel/proc/fork.c')
-rw-r--r-- | kernel/proc/fork.c | 75 |
1 files changed, 73 insertions, 2 deletions
diff --git a/kernel/proc/fork.c b/kernel/proc/fork.c index 28f9f9c..cbf5e30 100644 --- a/kernel/proc/fork.c +++ b/kernel/proc/fork.c @@ -57,6 +57,77 @@ static uintptr_t fork_setup_stack(const regs_t *regs, void *kstack) */ long do_fork(struct regs *regs) { - NOT_YET_IMPLEMENTED("VM: do_fork"); - return -1; + // NOT_YET_IMPLEMENTED("VM: do_fork"); + + // Create a new process + proc_t *child_proc = proc_create("child from fork"); + if (child_proc == NULL) + { + return -ENOMEM; + } + // Create a new thread + kthread_t *child_thread = kthread_clone(curthr); + if (child_thread == NULL) + { + proc_destroy(child_proc); + return -ENOMEM; + } + + // Set the child process's parent to the current process + // child_thread->kt_proc = child_proc; + // list_insert_head(&child_proc->p_threads, &child_thread->kt_plink); + + // Get the new vmmap_t for the child process + // vmmap_t *child_vmmap = vmmap_clone(curproc->p_vmmap); + // if (child_vmmap == NULL) + // { + // kthread_destroy(child_thread); + // proc_destroy(child_proc); + // return -ENOMEM; + // } + + // Set the new vmmap_t for the child process + // child_proc->p_vmmap = child_vmmap; + // // Set the vmmap to the child process + // child_thread->kt_proc = child_proc; + + // Set the working directory of the child process to the current process + // vref(curproc->p_cwd); + // child_proc->p_cwd = curproc->p_cwd; + + // Copy over each file descriptor from the parent to the child + // for (int i = 0; i < NFILES; i++) + // { + // if (curproc->p_files[i] != NULL) + // { + // fref(curproc->p_files[i]); + // child_proc->p_files[i] = curproc->p_files[i]; + // } + // } + + // Fix the values of the registers and the rest of the kthread's ctx + regs->r_rax = 0; // Set the return value to 0 for the child + child_thread->kt_ctx.c_rsp = fork_setup_stack(regs, child_thread->kt_kstack); // Set the stack pointer for the child + child_thread->kt_ctx.c_rip = (uintptr_t) userland_entry; // Set the instruction pointer to userland_entry + // child_thread->kt_ctx.c_rbp = curthr->kt_ctx.c_rbp; // Set the current thread's base pointer to the child's base pointer + child_thread->kt_ctx.c_pml4 = curproc->p_pml4; // Set the current thread's page table to the current proc's + child_thread->kt_proc = child_proc; // Set the child process to the child thread + + // Update the list + list_insert_tail(&child_proc->p_threads, &child_thread->kt_plink); + + + // Update the brks for the child process + // child_proc->p_brk = curproc->p_brk; + // child_proc->p_start_brk = curproc->p_start_brk; + + // Unmap the parent's page table and flush the TLB + pt_unmap_range(curproc->p_pml4, USER_MEM_LOW, USER_MEM_HIGH); + tlb_flush_all(); + + // Prepare the child process to be run on the CPU + sched_make_runnable(child_thread); + + // Return the child's process id to the parent + return child_proc->p_pid; } |