proc: add proc_fork and accompanying syscall

Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
Michael Shaw 2024-06-18 11:14:15 -04:00
commit e7133a473f
8 changed files with 115 additions and 0 deletions

View file

@ -118,6 +118,7 @@ void proc_term(pid_t pid)
node = next;
}
buddy_free((void *)((paddr_t)proc->cr3 | hhdm_start));
rbt_destroy(&proc->page_map);
rbt_destroy(&proc->fd_map);
@ -272,6 +273,48 @@ struct proc *proc_create()
return proc;
}
struct proc *proc_fork(struct proc *parent)
{
struct proc *proc = proc_create();
proc_clone_mmap(parent, proc);
/* copy file descriptors */
struct rbnode *node = rbt_minimum(parent->fd_map.root);
while (node) {
struct file_descriptor *fdesc = (void *)node->value;
struct file *file = fdesc->file;
struct file_descriptor *new_fdesc = slab_alloc(fd_slab);
new_fdesc->fd = fdesc->fd;
new_fdesc->file = file;
new_fdesc->pos = fdesc->pos;
new_fdesc->flags = fdesc->flags;
struct rbnode *new_node = rbt_insert(&proc->fd_map, fdesc->fd);
new_node->value = (uintptr_t)new_fdesc;
node = rbt_successor(node);
}
/* copy parent */
proc->parent = parent;
/* copy children */
node = rbt_minimum(parent->children.root);
while (node) {
struct proc *child = (void *)node->value;
struct proc *new_child = proc_fork(child);
struct rbnode *new_node = rbt_insert(&proc->children, child->pid);
new_node->value = (uintptr_t)new_child;
node = rbt_successor(node);
}
return proc;
}
struct proc *proc_get(pid_t pid)
{
struct rbnode *proc_node = rbt_search(proc_tree, pid);

View file

@ -160,6 +160,21 @@ void sys_exit(int status)
schedule();
}
pid_t sys_fork()
{
struct proc *proc = proc_find(getpid());
if (proc == NULL)
return -1;
struct proc *new_proc = proc_fork(proc);
if (new_proc == NULL)
return -1;
sys_set_return(new_proc, 0);
return new_proc->pid;
}
static void syscall_insert(uint64_t syscall_no, syscall_t syscall)
{
syscall_table[syscall_no] = syscall;
@ -173,5 +188,6 @@ void syscall_init()
syscall_insert(SYS_READ, (syscall_t)sys_read);
syscall_insert(SYS_WRITE, (syscall_t)sys_write);
syscall_insert(SYS_OPEN, (syscall_t)sys_open);
syscall_insert(SYS_FORK, (syscall_t)sys_fork);
syscall_insert(SYS_EXIT, (syscall_t)sys_exit);
}