Lab: mmap

In this lab you will use mmap on Linux to demand-page a very large table and add memory-mapped files to xv6.

Using mmap on Linux

This assignment will make you more familiar with how to manage virtual memory in user programs using the Unix system call interface. You can do this assignment on any operating system that supports the Unix API (a Linux Athena machine, your laptop with Linux or MacOS, etc.).

Download the mmap homework assignment and look it over. The program maintains a very large table of square root values in virtual memory. However, the table is too large to fit in physical RAM. Instead, the square root values should be computed on demand in response to page faults that occur in the table's address range. Your job is to implement the demand faulting mechanism using a signal handler and UNIX memory mapping system calls. To stay within the physical RAM limit, we suggest using the simple strategy of unmapping the last page whenever a new page is faulted in.

To compile mmap.c, you need a C compiler, such as gcc. On Athena, you can type:

$ add gnu
Once you have gcc, you can compile mmap.c as follows:
$ gcc mmap.c -lm -o mmap
Which produces a mmap file, which you can run:
$ ./mmap
page_size is 4096
Validating square root table contents...
oops got SIGSEGV at 0x7f6bf7fd7f18

When the process accesses the square root table, the mapping does not exist and the kernel passes control to the signal handler code in handle_sigsegv(). Modify the code in handle_sigsegv() to map in a page at the faulting address, unmap a previous page to stay within the physical memory limit, and initialize the new page with the correct square root values. Use the function calculate_sqrts() to compute the values. The program includes test logic that verifies if the contents of the square root table are correct. When you have completed your task successfully, the process will print “All tests passed!”.

You may find that the man pages for mmap() and munmap() are helpful references.

$ man mmap
$ man munmap

Implement memory-mapped files in xv6

In this assignment you will implement memory-mapped files in xv6. The test program mmaptest tells you what should work.

Here are some hints about how you might go about this assignment:

Run usertests to make sure you didn't break anything.

Optional challenges: