MIT6.S081 Lab5
Lab 5 Lazy Allocation
In this lab, we gonna implement lazy allocation
which means delay the true memory allocation till we actually needs it.
lazy sbrk()
To start with, we should modify the sbrk() from eager allocation to lazy one.
But we should note that the parameter n
can be arbitary number, either + or -
so once the number is positve do the lazy allocation, but if the number is negative, we should do the deallocation immediately.
Lazy Allocation
The time to truely allocate a page, is when kernel receives a No.13 or 15
exception, which tells the kernel a non-PTE_V page is being accessed. If the page is lazy allocated but not been truely allocated yet, we should do the allocation and return to the same instruction in the user mode.
We should check the boundary case, that if
- va $\in$ [0, p->sz) (Exclusively contain! Important)
- va $\notin$ guard page range
Then the virtual address is valid to allocate a new physical page for it. In this part, we could refer to the implementation of uvmalloc
1 | void allocate_page(uint64 va){ |
Note that in the original implementation of xv6, we do the eager allocation, which means whenever we visit a virtual address, the physical page is mapped. So there are many panic
check in the uvmunmap, walk, uvmcopy...
should be canceled, because now many ptes are lazy
allocated but not been mapped yet. So just leave to usertrap()
to decide whether page should be accessed.
Last, in the sbrkarg
test, it tests the copyin and copyout functions.
Because in the kernel mode, kernel simulates the dereference process by walkaddr
, it won’t cause a exception. So we should manually modify the function and add a allocation to it.
1 | va0 = PGROUNDDOWN(dstva); |
Some hints
- When encounter a bug, check the backtrace in the gdb.
- The way to decide whether a
va
is in the guard page, I useif(va < PGROUNDDOWN(p->trapframe->sp))
.- I saw some other’s blog says could use
r_sp()
, but it just return the kernel stack pointer…
- I saw some other’s blog says could use
- Don’t forget to add codes on copyin, copyout and copyinstr.