One of the many neat tricks an O/S can play with page table hardware is lazy allocation of heap memory. Xv6 applications ask the kernel for heap memory using the sbrk() system call. In the kernel we've given you, sbrk() allocates physical memory and maps it into the process's virtual address space. There are programs that allocate memory but never use it, for example to implement large sparse arrays. Sophisticated kernels delay allocation of each page of memory until the application tries to use that page—as signaled by a page fault. You'll add this lazy allocation feature to xv6 in this exercise.
Try to guess what the result of this modification will be: what will break?
Make this modification, boot xv6, and type echo hi to the shell. You should see something like this:
init: starting sh $ echo hi pid 3 sh: trap 14 err 6 on cpu 0 eip 0x12f1 addr 0x4004--kill proc $The "pid 3 sh: trap..." message is from the kernel trap handler in trap.c; it has caught a page fault (trap 14, or T_PGFLT), which the xv6 kernel does not know how to handle. Make sure you understand why this page fault occurs. The "addr 0x4004" indicates that the virtual address that caused the page fault is 0x4004.
Hint: look at the cprintf arguments to see how to find the virtual address that caused the page fault.
Hint: steal code from allocuvm() in vm.c, which is what sbrk() calls (via growproc()).
Hint: the faulting virtual address can be found in the %cr2 register which can be read with rcr2().
Hint: use PGROUNDDOWN(va) to round the faulting virtual address down to a page boundary.
Hint: break or return in order to avoid the cprintf and the myproc()->killed = 1.
Hint: you'll need to call mappages(). In order to do this you'll need to delete the static in the declaration of mappages() in vm.c, and you'll need to declare mappages() in trap.c. Add this declaration to trap.c before any call to mappages():
int mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm);
Hint: you can check whether a fault is a page fault by checking if tf->trapno is equal to T_PGFLT in trap().
If all goes well, your lazy allocation code should result in echo hi working. You should get at least one page fault (and thus lazy allocation) in the shell, and perhaps two.
By the way, this is not a fully correct implementation. See the challenges below for a list of problems we're aware of.
Optional challenges: