Cow Lab
Virtual memory provides a level of indirection: the kernel can intercept memory references by marking PTEs invalid or read-only, leading to page faults, and can change what addresses mean by modifying PTEs. There is a saying in computer systems that any systems problem can be solved with a level of indirection. The lazy allocation lab provided one example which is talked about in the last lab. This lab explores another example: copy-on write fork.
Fork’s Problem
The fork() system call in xv6 copies all of the parent process’s user-space memory into the child. If the parent is large, copying can take a long time. Worse, the work is often largely wasted; for example, a fork() followed by exec() in the child will cause the child to discard the copied memory, probably without ever using most of it. On the other hand, if both parent and child use a page, and one or both writes it, a copy is truly needed.
Implementation
The goal of copy-on-write (COW) fork() is to defer allocating and copying physical memory pages for the child until the copies are actually needed, if ever.
Cow fork will creates just a page table for the child , with PTEs for user memory pointing to the parent’s  pa. Cow fork will marks all the user PTEs in both parent and child as not writable. When either process wants to write any of these unwritable pages, will triggle a page fault .The kernel trap will handle this fault , allocates a page of physical memory for the page fault,copies the original page into the new page, and modifies the relevant PTE in the faulting process to refer to the new page, this time with the PTE marked writeable. The original pa will be not changed.
uvmcopy we will not allocate new pages , we increase the refcnt  for the pa.
Uvmcopy not allocate the new pa
| 1 | int uvmcopy(pagetable_t old, pagetable_t new, uint64 sz) | 
Kalloc Kfree increase cnt
kalloc , we maintain the refcnt for every physical page .In the initialization , the refcnt will be writedto 1,because  in the freerange ,we call kfree which decreases the refcnt for every pa.
| 1 | int refcnt[PHYSTOP / PGSIZE]; | 
increase refcnt and kfree is a combination , which is increase the refcnt of the pa , the other is decrease the refcnt of the pa .In the case when the refcnt of the pa down to zero , we really free the pa!
| 1 | void increse(uint64 pa) | 
kalloc function will allocate a pa ,  if the pa ref cnt is not valid , panic.
| 1 | void * | 
Handle function in Trap
The r_scause of page fault is 15 or 13. In usertrap , we have cowfault.
| 1 | if (r_scause() == 15) | 
cowfault function
- handle the  invalid va- more than MAXVA
- not in the page table
- not set user bit or valid bit
 
- more than 
- allocate a new pa, copy the original content to the newpa,- unmapand- mapfor this- pteentry!
- or we cook up  the ptestraightly
 
| 1 | int cowfault(pagetable_t pagetable, uint64 va) | 
One more thing ,according to the hint :  Modify copyout() to use the same scheme as page faults when it encounters a COW page.
| 1 | va0 = PGROUNDDOWN(dstva); | 
.The course lab site :MIT 6.S081 cow Lab
