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

@ -1,4 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "kernel/lock.h"
#include <kernel/common.h>
#include <kernel/mem.h>
#include <kernel/slab.h>
@ -243,6 +244,41 @@ void proc_munmap(struct proc *proc, uintptr_t vaddr)
spinlock_release(&proc->page_map_lock);
}
paddr_t proc_clone_mmap(struct proc *in, struct proc *out)
{
/* allocate a new page table */
out->cr3 = (uintptr_t)buddy_alloc(0x1000);
if (!out->cr3) {
kprintf(LOG_ERROR "proc_clone_mmap: failed to allocate new page table\n");
return 0;
}
memcpy((void *)out->cr3, (void *)(in->cr3 | hhdm_start), 0x1000);
out->cr3 &= ~hhdm_start;
/* copy all mappings */
spinlock_acquire(&in->page_map_lock);
spinlock_acquire(&out->page_map_lock);
struct rbnode *node = rbt_minimum(in->page_map.root);
while (node) {
uintptr_t vaddr = node->key;
paddr_t paddr = node->value;
size_t len = node->value2;
uint64_t attr = PAGE_PRESENT | PAGE_RW;
proc_mmap(out, paddr, vaddr, len, attr);
node = rbt_successor(node);
}
spinlock_release(&in->page_map_lock);
spinlock_release(&out->page_map_lock);
return out->cr3;
}
void page_init(paddr_t kpaddr, uintptr_t kvaddr, size_t kernel_size, struct mem_region *regions, size_t num_regions)
{
page_slab = slab_create(0x1000, 4 * MB, SLAB_PAGE_ALIGN);