sysproc.c
int
sys_lazymm(char* addr) {
  cprintf("requested lazy-mmaped address (0x%x)\n",(uint) addr);

  int found = 0;
  int i;
  struct mmap_job *job; // check if the address lies within a promised lazy-mmap region
  for (i=0; i < proc->mmjobs_count; ++i) {
    job = &proc->mmjobs[i];
    if (addr >= job->beg && addr < job->end) {
      found = 1;
      break;
    }
  }
  if (found == 0)
    return -1; // nope, the addr isn't referring to mmaped file. probably process misbehaved

  struct file *f = job->f;
  pde_t* pgdir = proc->pgdir;

  uint a = PGROUNDDOWN((uint) addr); 
  char * mem = kalloc();
  if (mem == 0) { // todo: handle
    cprintf("lazy mmap out of memory\n");
    // deallocuvm(pgdir, newsz, oldsz); wroooong. need a custom one. 
    return -1;
  }

  memset(mem, 0, PGSIZE); // clear it with zeros
  mappages(pgdir, (char*)a, PGSIZE, v2p(mem), PTE_W|PTE_U);
  // cprintf("v2p of mem %d\n", v2p((void*)mem));
  // cprintf("v2p of a %d\n", v2p((void*)a));
  // cprintf("allocated %d\n\n",newsz-oldsz+PGSIZE);
  // cprintf("file offset = %d\n",  (uint) ((char*)a - job->beg));
  readi(f->ip, (char*)a, (char*)a - job->beg, PGSIZE); // read from file PAGESIZE bytes into our newly allocated page. if we replace 'a' with 'mem' this still works!! why?
  // switchuvm(proc);

  return 1; // cool
}
// check if addr is referring to (lazily) mmaped file,  
// if so, actually load the file (or a chunk of it)
// and add this addr (or the page it's in) to page table.
check if addr is referring to (lazily) mmaped file,
if so, actually load the file (or a chunk of it)
and add this addr (or the page it's in) to page table.
check if addr is referring to ( lazily ) mmaped file if so actually load the file ( or a chunk of it ) and add this addr ( or the page it ' s in ) to page table .
int
sys_lazymm(char* addr) {
  cprintf("requested lazy-mmaped address (0x%x)\n",(uint) addr);
  int found = 0;
  int i;
  struct mmap_job *job; 
  for (i=0; i < proc->mmjobs_count; ++i) {
    job = &proc->mmjobs[i];
    if (addr >= job->beg && addr < job->end) {
      found = 1;
      break;
    }
  }
  if (found == 0)
    return -1; 
  struct file *f = job->f;
  pde_t* pgdir = proc->pgdir;
  uint a = PGROUNDDOWN((uint) addr); 
  char * mem = kalloc();
  if (mem == 0) { 
    cprintf("lazy mmap out of memory\n");
    return -1;
  }
  memset(mem, 0, PGSIZE); 
  mappages(pgdir, (char*)a, PGSIZE, v2p(mem), PTE_W|PTE_U);
  readi(f->ip, (char*)a, (char*)a - job->beg, PGSIZE); 
  return 1; 
}
int sys_lazymm ( char * addr ) { cprintf ( " \n " , ( uint ) addr ) ; int found = 0 ; int i ; struct mmap_job * job ; for ( i = 0 ; i < proc -> mmjobs_count ; ++ i ) { job = & proc -> mmjobs [ i ] ; if ( addr >= job -> beg && addr < job -> end ) { found = 1 ; break ; } } if ( found == 0 ) return -1 ; struct file * f = job -> f ; pde_t * pgdir = proc -> pgdir ; uint a = PGROUNDDOWN ( ( uint ) addr ) ; char * mem = kalloc ( ) ; if ( mem == 0 ) { cprintf ( " \n " ) ; return -1 ; } memset ( mem , 0 , PGSIZE ) ; mappages ( pgdir , ( char * ) a , PGSIZE , v2p ( mem ) , PTE_W | PTE_U ) ; readi ( f -> ip , ( char * ) a , ( char * ) a - job -> beg , PGSIZE ) ; return 1 ; }
check if addr is referring to (lazily) mmaped file,
if so, actually load the file (or a chunk of it)
and add this addr (or the page it's in) to page table.
check if addr is referring to ( lazily ) mmaped file if so actually load the file ( or a chunk of it ) and add this addr ( or the page it ' s in ) to page table .
// check if the address lies within a promised lazy-mmap region // nope, the addr isn't referring to mmaped file. probably process misbehaved // todo: handle // deallocuvm(pgdir, newsz, oldsz); wroooong. need a custom one.  // clear it with zeros // cprintf("v2p of mem %d\n", v2p((void*)mem)); // cprintf("v2p of a %d\n", v2p((void*)a)); // cprintf("allocated %d\n\n",newsz-oldsz+PGSIZE); // cprintf("file offset = %d\n",  (uint) ((char*)a - job->beg)); // read from file PAGESIZE bytes into our newly allocated page. if we replace 'a' with 'mem' this still works!! why? // switchuvm(proc); // cool