kernel/vm.c
int
uvmcopy(pagetable_t old, pagetable_t new, pagetable_t knew, uint64 sz)
{
  pte_t *pte;
  uint64 pa, i = 0, ki = 0;
  uint flags;
  char *mem;

  while (i < sz){
    if((pte = walk(old, i, 0)) == NULL)
      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()) == NULL)
      goto err;
    memmove(mem, (char*)pa, PGSIZE);
    if(mappages(new, i, PGSIZE, (uint64)mem, flags) != 0) {
      kfree(mem);
      goto err;
    }
    i += PGSIZE;
    if(mappages(knew, ki, PGSIZE, (uint64)mem, flags & ~PTE_U) != 0){
      goto err;
    }
    ki += PGSIZE;
  }
  return 0;

 err:
  vmunmap(knew, 0, ki / PGSIZE, 0);
  vmunmap(new, 0, i / PGSIZE, 1);
  return -1;
}
// 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, pagetable_t knew, uint64 sz)
{
  pte_t *pte;
  uint64 pa, i = 0, ki = 0;
  uint flags;
  char *mem;
  while (i < sz){
    if((pte = walk(old, i, 0)) == NULL)
      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()) == NULL)
      goto err;
    memmove(mem, (char*)pa, PGSIZE);
    if(mappages(new, i, PGSIZE, (uint64)mem, flags) != 0) {
      kfree(mem);
      goto err;
    }
    i += PGSIZE;
    if(mappages(knew, ki, PGSIZE, (uint64)mem, flags & ~PTE_U) != 0){
      goto err;
    }
    ki += PGSIZE;
  }
  return 0;
 err:
  vmunmap(knew, 0, ki / PGSIZE, 0);
  vmunmap(new, 0, i / PGSIZE, 1);
  return -1;
}
int uvmcopy ( pagetable_t old , pagetable_t new , pagetable_t knew , uint64 sz ) { pte_t * pte ; uint64 pa , i = 0 , ki = 0 ; uint flags ; char * mem ; while ( i < sz ) { if ( ( pte = walk ( old , i , 0 ) ) == NULL ) panic ( " " ) ; if ( ( * pte & PTE_V ) == 0 ) panic ( " " ) ; pa = PTE2PA ( * pte ) ; flags = PTE_FLAGS ( * pte ) ; if ( ( mem = kalloc ( ) ) == NULL ) goto err ; memmove ( mem , ( char * ) pa , PGSIZE ) ; if ( mappages ( new , i , PGSIZE , ( uint64 ) mem , flags ) != 0 ) { kfree ( mem ) ; goto err ; } i += PGSIZE ; if ( mappages ( knew , ki , PGSIZE , ( uint64 ) mem , flags & ~ PTE_U ) != 0 ) { goto err ; } ki += PGSIZE ; } return 0 ; err : vmunmap ( knew , 0 , ki / PGSIZE , 0 ) ; vmunmap ( new , 0 , i / PGSIZE , 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 .