CS2200 Homework 6: Virtual Memory



Turnin Details


Goal

You are to add both software and hardware support for virtual memory to the provided simulator.

Provided Files
main.c Parses the command-line, calls the simulator
sim.c Code for your MMU goes here
sim.h Simulator header-file
os.c Operating System stuff - your page fault handler goes here
os.h OS header file
process.c Utilities for manipulating the process structures
process.h Process header file
gen_access.c A program to generate memory accesses (a helpful utility for the assignment)
Makefile A Makefile which will build both the simulator and gen_access

Testing
Testing your program with this test case will not guarantee that your program is correct, but it may help you uncover bugs in your program.

test1 A test set of 5000 memory accesses
test1.out The output when running with a finished simulator (it was run under acme and rand() was not seeded)

Part 1: Page Replacement

1. [25 points]

Create a simulator with the following command-line:
   pf-sim  <mem-access-file>

It should run start_sim() with <mem-access-file>


Translate the one-dimensional memory address parsed from the access file into a two-dimensional address consisting of a VPN and an INDEX. If the status of a page is marked "INVALID", call your pf_handler() and indicate the page fault is due to PAGE_INVALID. Your pf_handler() should print the following information:

  Invalid Page Fault
  PID: <faulting PID> 
  VPN: <faulting VPN>
  ********************************************************
You don't need to worry about out-of-bounds memory addresses.

2. [25 points]

Allocate space for invalid pages using a random replacement algorithm. If there are any free pages available, use one of the free page. If all of the physical pages are already in use, randomly evict one of the pages. If the evicted page has been marked "DIRTY", the page fault handler should write the contents of the page to disk.

On a valid page write, the MMU should mark the page as "DIRTY", and store the data into physical memory. You can safely ignore valid page reads.

Part 2: VM Hacks

3. [20 points]
Shared Memory/Permission Bits

Modify sim() and pf_handler() to support access bits. Your sim should page fault if the process does not have permission to access the page and the page fault status should be set to PERMS_INVALID. Your page fault handler should print the following information:

  Invalid Access Fault
  Pid: <faulting pid>
  VPN: <faulting VPN>
  ***********************************************************

4. [30 points]
Efficient process creation

Here's a very rough outline of how process creation works in Unix:

  fork()                 - a Unix system call which creates a new process. 
                           The new process contains a copy of the
                           forking process's memory.
 
  execl(file, args ...)  - maps <file> into memory and runs it
                           with the argv <args ...>

  So we can run a new process like this:

  pid = fork();
  if (pid > 0)
    wait(&status);
    return status;
  else if (pid == 0)
    execl("echo", "hello", "world\n");
    return EXIT_FAILURE;
  else
    return EXIT_FAILURE;

  The problem is that creating a complete copy of the parent process's
  page table is expensive.  On top of that, after making a copy
  of the address space we immediately destroy it when we map "echo"
  into memory.

  Copy-on-write paging was created to handle this problem.  Here's how
  our copy-on-write algorithm will work:

  When a new process is created, it shadows its parent's page table.  If
  the child process writes to a shared page, it creates a copy of that page
  before writing to it.  If the parent process writes to a shared
  page, all of its children must make copies of the page.
  
Modify the create_proc() function so that child process shadows the parent's page table.

Writing to a page that is marked COPY_ON_WRITE should cause a PERMS_INVALID page fault. Your pf_handler() must fix up its relatives' page tables using the above algorithm. To reduce the complexity of this assignment, you only have to deal with two generations of processes (i.e. if a process was created during the course of the program, it will not have any children of its own.) Your page fault handler should also print the following information:

  Copy-on-write Fixup
  Pid: <faulting pid>
  VPN: <faulting VPN>
  ***********************************************************

You may assume that shared memory which does not have write permssions or read permissions will not be set to COPY-ON-WRITE. However, it is possible that a page can be set to COPY-ON-WRITE and INVALID simultaneously.