Homework: xv6 lazy page allocation
xv6 user program using the sbrk () system call request to the kernel stack memory
Part One: Eliminate allocation from sbrk()
int
sys_sbrk(void)
{
int addr;
int n;
if(argint(0, &n) < 0)
return -1;
addr = myproc()->sz;
myproc()->sz += n;
// if(growproc(n) < 0)
// return -1;
return addr;
}
run:
init: starting sh
$ echo hi
pid 3 sh: trap 14 err 6 on cpu 1 eip 0x1168 addr 0x4004--kill proc
$
Part Two: Lazy allocation
First vm.c
in the mappages
static function declaration removed, then trap.c
the trap
added function:
case T_PGFLT: {
char* mem = kalloc();
if (mem != 0) {
memset(mem, 0, PGSIZE);
extern int mappages(pde_t * pgdir, void* va, uint size, uint pa, int perm);
if (mappages(myproc()->pgdir, (void*)PGROUNDDOWN(rcr2()), PGSIZE, V2P(mem), PTE_W | PTE_U) < 0) {
kfree(mem);
}
} else {
cprintf("trap(): kalloc out of memory\n");
}
break;
}
Now echo hi
normal