vm.c
// Given a parent process's page table, create a copy
// of it for a child.
Given a parent process's page table, create a copy
of it for a child.
Given a parent process ' s page table create a copy of it for a child .
pde_t* copyuvm(pde_t *pgdir, uint sz){
  pde_t *d;
  pte_t *pte;
  uint pa, i, flags;
  char *mem;
  if((d = setupkvm()) == 0)
    return 0;
  int j = 0;
  for(i = 0; i < sz; i += PGSIZE){
    if((pte = walkpgdir(pgdir, (void *) i, 0)) == 0)
      panic("copyuvm: pte should exist");
    if (*pte & PTE_PG){
    	fixPagedOutPTE(i, d);
    	continue;
    }
    if(!(*pte & PTE_P))
      panic("copyuvm: page not present");
    pa = PTE_ADDR(*pte);
    flags = PTE_FLAGS(*pte);
    if((mem = kalloc()) == 0)
      goto bad;
    memmove(mem, (char*)p2v(pa), PGSIZE);
    j++;
    if(mappages(d, (void*)i, PGSIZE, v2p(mem), flags) < 0)
      goto bad;
  }
  return d;
bad:
  freevm(d);
  return 0;
}
pde_t * copyuvm ( pde_t * pgdir , uint sz ) { pde_t * d ; pte_t * pte ; uint pa , i , flags ; char * mem ; if ( ( d = setupkvm ( ) ) == 0 ) return 0 ; int j = 0 ; for ( i = 0 ; i < sz ; i += PGSIZE ) { if ( ( pte = walkpgdir ( pgdir , ( void * ) i , 0 ) ) == 0 ) panic ( " " ) ; if ( * pte & PTE_PG ) { fixPagedOutPTE ( i , d ) ; continue ; } if ( ! ( * pte & PTE_P ) ) panic ( " " ) ; pa = PTE_ADDR ( * pte ) ; flags = PTE_FLAGS ( * pte ) ; if ( ( mem = kalloc ( ) ) == 0 ) goto bad ; memmove ( mem , ( char * ) p2v ( pa ) , PGSIZE ) ; j ++ ; if ( mappages ( d , ( void * ) i , PGSIZE , v2p ( mem ) , flags ) < 0 ) goto bad ; } return d ; bad : freevm ( d ) ; return 0 ; }
Given a parent process's page table, create a copy
of it for a child.
Given a parent process ' s page table create a copy of it for a child .