Lab 5 Copy-on-Write Fork for xv6
Your task is to implement copy-on-write fork in the xv6 kernel
The problem
Task
The solution
// Given a parent processâs page table, copy
// its memory into a childâs page table.
// Copies both the page table and the
// physical memory.
// returns 0 on success, -1 on failure.
// frees any allocated pages on failure.
int
uvmcopy(pagetable_t old, pagetable_t new, uint64 sz)
{
pte_t *pte;
uint64 pa, I;
uint flags;
for(I = 0; I < sz; I += PGSIZE){
if((pte = walk(old, I, 0)) == 0)
panic(âuvmcopy: pte should existâ);
if((*pte & PTE_V) == 0)
panic(âuvmcopy: page not presentâ);
pa = PTE2PA(*pte);
flags = PTE_FLAGS(*pte);
// Record the page is COW mapping.
flags |= PTE_RSW;
// clear PTE_W in the PTEs of both child and parent*
flags &= (~PTE_W);
// map the parentâs physical pages into the child
if(mappages(new, I, PGSIZE, (uint64)pa, flags) != 0){
//kfree(mem);
goto err;
}
// Bump the reference count*
add_ref((void*)pa);
// Remove parent page table mapping.
uvmunmap(old, I, PGSIZE, 0);
// Re-add the mapping with write bit cleared flags.
if (mappages(old, I, PGSIZE, pa, flags) != 0) {
goto err;
}
}
ĺżĺž
Last updated