Lab5 Virtual Memory (Due date : March 26, 2004)

Objective : You will study how Linux implements virtual memory. A general architecture-independent memory model is the basis of all Linux virtual memory implementations, though any specific implementation depends on the particular hardware platform. In this project, you need to study the source code so that you can instrument the virtual memory code to detect the rate at which the system is incurring page faults.

Background : "A page fault exception is raised when the addressed page is not present in memory, the corresponding page table entry is null or a violation of the paging protection mechanism has occurred." [ULK]. Linux handles a page fault exception with the page fault handler "do_page_fault()". This handler can be found in "arch/i386/mm/fault.c". You may also need to read related contents of Chapter 4 in LKP
"The next most important piece of the port is the virtual memory emulation. An important job of the kernel is to maintain a separate memory context for each process, making it impossible for one process to access memory belonging to another. Native kernels accomplish this by allocating physical pages and doing hardware magic to map them into the appropriate location in a process virtual memory. UML emulates this first by creating a file that is the same size as the physical memory that UML has been told it has, and then by mapping this file as a whole into an area of its virtual memory that will be treated as its "physical" memory. When a process needs memory to be allocated, pages will be allocated from this area and the corresponding pages in the file will be mmapped() into the process virtual memory.

UML must also emulate hardware faults and device interrupts. The most important fault that needs to be emulated is a page fault, which happens whenever a process does an invalid memory access. In UML this generates a SIGSEGV; the handler does the necessary checking to see if the access is valid and a new page needs to be mapped into the process (if it's invalid, the process is sent the SIGSEGV). Device interrupts are generally emulated with SIGIO on the file descriptor used to communicate with the virtual device. The timer is implemented by requesting a SIGVTALRM timer from the host kernel.
SIGSEGV, SIGIO, and SIGVTALRM are the equivalent of hardware traps. Just as processes on a physical Linux machine aren't affected by hardware traps unless the kernel converts them into Linux signals, these signals don't affect any UML processes unless the UML kernel converts them into Linux signals. UML installs its own handlers for these signals, which run in UML kernel mode." ... Jeff Dike

You may also need to read some other materials: Understanding the Linux memory management

Problem Statement
  1. Instrument the virtual memory manager
    Instrument the memory manager software so that you can determine the rate at which any particular process, or the whole system, is incurring page faults. You need to define a new page fault system call that can serve the following purposed.
  2. Reporting the performance
    Write a program that periodically calls your page fault system call to determine the current rate. Then plot the page fault frequency rate versus time. Finally, read about the vmstat command to get ideas about how to design your reporting program.

Attacking the Problem :
  1. study source codes such as "fault.c", etc.
  2. make the relative variables global;
  3. do not do the floating point calculation inside kernel.
Submitting Your Assignment :
Please submit the following items to lab5 dir on elgate:
  1. all source codes/files that you have added/modified including all related test programs and the graphs showing your test results for different parameters (e.g., different processes, different window sizes);
  2. a README that contains the follows :
  3. (New) Compiled kernel image with the new changes.


  1. please make necessary comments on your code and especially, using some special symbols (e.g.HLK) to mark the place wherever your code begins and write the symbol in your README file. (10%)
  2. (New) please make your figures output as .ps or .pdf files.