kernel/vm.c
// 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.
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.
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;
  char *mem;
  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);
    if((mem = kalloc()) == 0)
      goto err;
    memmove(mem, (char*)pa, PGSIZE);
    if(mappages(new, i, PGSIZE, (uint64)mem, flags) != 0){
      kfree(mem);
      goto err;
    }
  }
  return 0;
 err:
  uvmunmap(new, 0, i, 1);
  return -1;
}
int uvmcopy ( pagetable_t old , pagetable_t new , uint64 sz ) { pte_t * pte ; uint64 pa , i ; uint flags ; char * mem ; for ( i = 0 ; i < sz ; i += PGSIZE ) { if ( ( pte = walk ( old , i , 0 ) ) == 0 ) panic ( " " ) ; if ( ( * pte & PTE_V ) == 0 ) panic ( " " ) ; pa = PTE2PA ( * pte ) ; flags = PTE_FLAGS ( * pte ) ; if ( ( mem = kalloc ( ) ) == 0 ) goto err ; memmove ( mem , ( char * ) pa , PGSIZE ) ; if ( mappages ( new , i , PGSIZE , ( uint64 ) mem , flags ) != 0 ) { kfree ( mem ) ; goto err ; } } return 0 ; err : uvmunmap ( new , 0 , i , 1 ) ; return -1 ; }
Given a parent process's page table, copy
its memory into a child's page table.
Given a parent process ' s page table copy its memory into a child ' s page table .