Individual Lab 4 - Virtual Memory
CS3410 Spring 2015
Due in class during your lab section
We give you the source code to the MIPS simulator that we have been using in previous assignments. Modify the simulator to simulate virtual memory, including a shared memory system call with copy-on-write semantics.
The original simulator code did not implement virtual memory: for each load and store, the simulator simply used the address to index into a large array (allocated using malloc). We have modified the simulator in a few simple ways to get you started. In particular, we have implemented the simulation for the hardware part of the the virtual memory system, but not the software part:
- The simulator already supports physical memory using the existing code.
- The simulator now calls create_address_space() before loading a program to create a new set of page tables for the program.
- The simulator stores a (physical) pointer to these page tables in Register 4 on Coprocessor 0, i.e. c0r4, which is the standard MIPS Context register, also known as the Page Table Base Register (PTBR) on x86.
- The simulator now calls map_page() to create virtual mappings for the (virtual) memory segments of the program being loaded.
- For loads and stores, the simulator now traverses the page tables stored at c0r4 to translate virtual addresses to physical addresses before it accesses the existing physical memory.
- Simple stub code for the shared memory system call has been added so that the simulator calls map_shared_page() as needed to create copy-on-write virtual mappings. Implementing copy-on-write mappings is optional.
What is left for you to do is implement the simulation of what would normally be done by the operating system:
- Implement the create_address_space() function to create a virtual address space.
- Implement the map_page() function to create virtual mappings.
- (Optional) Implement the map_shared_page() function call to support shared, copy-on-write virtual memory (do after map_page).
- (Optional) Implement the pfault() function call to support process killing and shared, copy-on-write virtual memory (if map_shared_page is implemented).
We provide a header file that describes precisely what each function should do.
What to submit
Submit your well-commented vmem.c file and your vsimulate binary. You should not have to change any other files. Not including comments, it is feasible to complete this lab in about 50 lines of code.
Overview of source files
All of the source files for the simulator are available in the course directory in the CSUG Lab. The top level directory contains the simulator source code and a Makefile for compiling it. You should copy the files to your own directory to work on them:
$ cp -r /courses/cs3410/lab4 ~/lab4 $ cd ~/lab4
If you will be using the VM instead of CSUG then follow the instructions here.
The mips subdirectory contains an example MIPS program that uses shared memory (you can also write your own MIPS test programs if needed), and a Makefile for compiling it.
The most relevant files for you are:
- mem.h Contains detailed descriptions and prototypes for the functions you must implement, along with some prototypes for functions that you will likely need to call (e.g. to allocate physical pages).
- vmem.c You write this file. It should contain all of your implementation. The simulator will not compile without it.
- mips/hello.c This compiles into a MIPS executable. If your virtual memory code is working correctly, you should at least be able to run this program with no errors.
The _mem_ref() function is used by the simulator to traverse the page tables on each memory access to a virtual address; it is implemented in mem.c. You may find it helpful to at least look at this function to be sure you understand what it is doing. Otherwise, most of the simulator code is not terribly interesting. In rough order of interest, are:
- pmem.c Simulates physical memory as a big array of bytes.
- syscalls.c Simulates system calls.
- mem.c Helper routines for managing memory.
- readelf.c Parses and loads MIPS executable binaries.
- run.c Simulates the the CPU datapath.
- main.c Main entry point and command line processing.
- sim.h Prototypes and definitions.
- debug.c Interactive debugger.
- disasm.c Disassembler (used when debugging).
- readline.c Keyboard input (used when debugging).
- readline.h Keyboard input (used when debugging).
Compiling the Code
To compile the simulator (do this each time you edit vmem.c or anything in the lab4/ dir) from the lab4/ directory:
To compile the test program (only do this once) from the lab4/ directory:
$ cd mips/ $ make $ cd ..
To run the test program:
We have slightly upgraded the simulator's built-in interactive debugger (the "-d" option) so that it can work with simulated virtual memory. The "info memory" command now prints out information about both simulated physical and virtual memory. And the "x" command to examine memory has been replaced with two commands: "vx" for examining simulated virtual memory; and "px" for examining simulated physical memory.
The simulator itself runs on x86 linux platforms, and so is compiled with regular gcc instead of mipsel-linux-gcc. If you need to debug the simulator, try the gdb interactive debugger. You should find that it has an interface much like the simulator's built-in MIPS debugger:
$ gdb vsimulate GNU gdb 6.8-debian Copyright (C) 2008 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i486-linux-gnu"... (gdb) break map_page Breakpoint 1 at 0x8050c0d: file vmem.c, line 25. (gdb) run mips/hello Starting program: /home/hw342/hw4/vsimulate mips/hello [Thread debugging using libthread_db enabled] [New Thread 0xb7ea16c0 (LWP 6124)] [Switching to Thread 0xb7ea16c0 (LWP 6124)] Breakpoint 1, map_page (context=290816, vaddr=4194304, writeable=0, executable=0) at vmem.c:25 (gdb)