TWiki> CS385fall14 Web>Homeworks>Homework4 (revision 2)EditAttach

Homework 4: mmap() - eager and lazy

In this homework, we learn a little bit about memory management by adding a widely used Unix feature to xv6: mmap(). While mmap can be (and is) used to allocate large chunks of memory, the more interesting use of mmap() is that of mapping a file into memory. This means, you call mmap once, with a file descriptor as argument, and the file immediately appears in virtual memory.

There are at least two main ways to do this. The first is eager: when mmap is called, have the kernel read the file as instructed, put it somewhere convenient in memory, and return a pointer to the location. The second is lazy: take note of the user's request, and return without doing anything further. When the user tries to access the memory, deal with the resulting page-fault, reading in only those pages that the user tries to read, when the user tries to read them.

Getting started

As in the past, git fetch the latest changes, then checkout the origin/hw4 branch which contains some minimal template code, and some more extensive testing code. To test your solution, rebuild and start xv6, then run hw4_tests from inside qemu, on the xv6 command line.

sys_mmap()

The system call sys_mmap() has already been created for you, only it doesn't do much yet. The majority of the homework solution should go here, or in functions called by sys_mmap().

Eager solution

In the eager solution, where the lazy argument is 0, sys_mmap() allocates and maps all the memory needed for the file contents (see allocuvm() for an example of how to do this, but for the heap), and reads all of the file contents into the newly allocated memory.

For both the eager and the lazy solution, you should map mmap() regions into addresses 0x40000000 and above - halfway between zero and the kernel area at 0x80000000. You should expect there to be more than one call to mmap(), so you can't overwrite/reuse any mmap areas you created in earlier calls. You'll want to store any state you need to keep inside struct proc.

To test your program, run the user program eager_mmap which exists in the hw4 branch from the xv6 command line.

Lazy solution

The lazy solution works quite differently. When the lazy argument is 1, sys_mmap() should not allocate or map any memory, nor does it read anything from disk. Instead, it records the request in struct proc, and returns a pointer immediately. Initially, this pointer points to an unallocated and unmapped part of the virtual address space.

Later, when the process tries to read from or write to the memory area it just =mmap=ed, a page fault occurs. You should create page fault handling code (in trap.c) that allocates the appropriate page to serve this read/write, and fills the page with the appropriate contents from the file.

Bonus points: anonymous mmap() allocations

Here, allow the user to allocate huge amounts of memory, but only actually allocate each page of memory on access. To get your extra credit, also provide a test program, and demonstrate and explain in person to Jakob or a TA.

Edit | Attach | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r2 - 2014-09-16 - 17:12:17 - Main.jakob
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF