The mmap and munmap system calls allow UNIX programs to exert detailed control over their address spaces. They can be used to share memory among processes, to map files into process address spaces, and as part of user-level page fault schemes such as the garbage-collection algorithms discussed in lecture. In this lab you’ll add mmap and munmap to xv6, focusing on memory-mapped files.
You should implement enough mmap and munmap functionality to make the mmaptest test program work. If mmaptest doesn’t use a mmap feature, you don’t need to implement that feature.
Here are some hints:
Start by adding _mmaptest to UPROGS, and mmap and munmap system calls, in order to get user/mmaptest.c to compile.
Fill in the page table lazily, in response to page faults. That is, mmap should not allocate physical memory or read the file. Instead, do that in page fault handling code in (or called by) usertrap, as in the lazy page allocation lab. The reason to be lazy is to ensure that mmap of a large file is fast, and that mmap of a file larger than physical memory is possible.
Keep track of what mmap has mapped for each process. Define a structure corresponding to the VMA (virtual memory area) described in Lecture 15, recording the address, length, permissions, file, etc. for a virtual memory range created by mmap.
Since the xv6 kernel doesn’t have a memory allocator in the kernel, it’s OK to declare a fixed-size array of VMAs and allocate from that array as needed. A size of 16 should be sufficient.
Implement mmap: find an unused region in the process’s address space in which to map the file, and add a VMA to the process’s table of mapped regions. The VMA should contain a pointer to a struct file for the file being mapped; mmap should increase the file’s reference count so that the structure doesn’t disappear when the file is closed (hint: see filedup). Run mmaptest: the first mmap should succeed, but the first access to the mmap-ed memory will cause a page fault and kill mmaptest.
Add code to cause a page-fault in a mmap-ed region to allocate a page of physical memory, read 4096 bytes of the relevant file into that page, and map it into the user address space. Read the file with readi, which takes an offset argument at which to read in the file (but you will have to lock/unlock the inode passed to readi). Don’t forget to set the permissions correctly on the page. Run mmaptest; it should get to the first munmap. Implement munmap: find the VMA for the address range and unmap the specified pages (hint: use uvmunmap). If munmap removes all pages of a previous mmap, it should decrement the reference count of the corresponding struct file. If an unmapped page has been modified and the file is mapped MAP_SHARED, write the page back to the file. Look at filewrite for inspiration.
Ideally your implementation would only write back MAP_SHARED pages that the program actually modified. The dirty bit (D) in the RISC-V PTE indicates whether a page has been written. However, mmaptest does not check that non-dirty pages are not written back; thus you can get away with writing pages back without looking at D bits. Modify exit to unmap the process’s mapped regions as if munmap had been called. Run mmaptest; mmap_test should pass, but probably not fork_test.
Modify fork to ensure that the child has the same mapped regions as the parent. Don’t forget to increment the reference count for a VMA’s struct file. In the page fault handler of the child, it is OK to allocate a new physical page instead of sharing a page with the parent. The latter would be cooler, but it would require more implementation work. Run mmaptest; it should pass both mmap_test and fork_test.
for(a = va; a < va + nbytes; a += PGSIZE){ if((pte = walk(pagetable, a, 0)) == 0) continue; // 这行代码的作用是确保要取消映射的页表项是一个叶子节点,而不是中间节点或其他不应该被取消映射的节点。 if(PTE_FLAGS(*pte) == PTE_V) panic("sys_munmap: not a leaf"); if(*pte & PTE_V){ uint64 pa = PTE2PA(*pte); // 脏且共享,写回 if((*pte & PTE_D) && (v->flags & MAP_SHARED)) { begin_op(); ilock(v->f->ip); uint64 aoff = a - v->vastart; // offset relative to the start of memory range if(aoff < 0) { // if the first page is not a full 4k page writei(v->f->ip, 0, pa + (-aoff), v->offset, PGSIZE + aoff); } elseif(aoff + PGSIZE > v->sz){ // if the last page is not a full 4k page writei(v->f->ip, 0, pa, v->offset + aoff, v->sz - aoff); } else { // full 4k pages writei(v->f->ip, 0, pa, v->offset + aoff, PGSIZE); } iunlock(v->f->ip); end_op(); } kfree((void*)pa); *pte = 0; } } }
// free a proc structure and the data hanging from it, // including user pages. // p->lock must be held. staticvoid freeproc(struct proc *p) { if(p->trapframe) kfree((void*)p->trapframe); p->trapframe = 0;
for(int i = 0; i < NVMA; i++) { structvma *v = &p->vmas[i]; vmaunmap(p->pagetable, v->vastart, v->sz, v); }
// Create a new process, copying the parent. // Sets up child kernel stack to return as if from fork() system call. int fork(void) { ... // copy vmas created by mmap. // actual memory page as well as pte will not be copied over. for(i = 0; i < NVMA; i++) { structvma *v = &p->vmas[i]; if(v->valid) { np->vmas[i] = *v; filedup(v->f); } } ... return pid; }
== Test running mmaptest == $ make qemu-gdb (6.6s) == Test mmaptest: mmap f == mmaptest: mmap f: OK == Test mmaptest: mmap private == mmaptest: mmap private: OK == Test mmaptest: mmap read-only == mmaptest: mmap read-only: OK == Test mmaptest: mmap read/write == mmaptest: mmap read/write: OK == Test mmaptest: mmap dirty == mmaptest: mmap dirty: OK == Test mmaptest: not-mapped unmap == mmaptest: not-mapped unmap: OK == Test mmaptest: two files == mmaptest: two files: OK == Test mmaptest: fork_test == mmaptest: fork_test: OK == Test usertests == $ make qemu-gdb usertests: OK (102.5s) == Test time == time: OK Score: 140/140