CSE 551 -- Programming Assignment #2
Out: Thursday April 12th, 2007
Due: Tuesday April 24th, 2007, before class
Overview
For this assignment, you will write a linux kernel module. When inserted, your module should create a new entry in the /proc filesystem called:
/proc/get_num_pagefaults
When somebody cat's that file, it should print out the number of page faults that the operating system has handled since it booted. So, for example, you ought to be able to reproduce the following session once you have your new kernel module compiled:
[root@localhost assignment2]# ls -al /proc/get_num_pagefaults
ls: /proc/get_num_pagefaults: No such file or directory
[root@localhost assignment2]# /sbin/insmod ./gribble_module.ko
[root@localhost assignment2]# ls -al /proc/get_num_pagefaults
-r--r--r-- 1 root root 37 Apr 11 21:38 /proc/get_num_pagefaults
[root@localhost assignment2]# cat /proc/get_num_pagefaults
658103
[root@localhost assignment2]# cat /proc/get_num_pagefaults
658295
[root@localhost assignment2]# cat /proc/get_num_pagefaults
658485
[root@localhost assignment2]# /sbin/rmmod gribble_module
[root@localhost assignment2]# ls -al /proc/get_num_pagefaults
ls: /proc/get_num_pagefaults: No such file or directory
[root@localhost assignment2]#
Getting started
First, you need to get yourself a development environment that will allow you
to experiment with kernel modules. For this, you'll need a machine that you
can install linux on, get root access, compile new kernels, and compile/install
new kernel modules. I strongly recommend you do your work inside a virtual
machine. So, here are some steps you can take to get going:
- Install VMware server on a machine you have access to, or find a machine
that already has some flavor of VMware installed on it. If you don't have
such a machine, talk to Steve. Note that VMware server is a free product,
and it is available for both Linux and Windows.
- Install some version of linux in a new virtual machine. I recommend you
download and install Fedora Core 5. Rather than downloading all of the
install CDs, you could download the Fedora Core 5 "virtual appliance"
from VMWare. A virtual appliance is a pre-baked virtual machine image
with software already installed in it. The virtual appliance web pages
are off of here.
- Inside that machine, download and install the latest linux
kernel source tree (version 2.6.20). You can get the
tarball from http://www.kernel.org.
- Compile and install yourself a new kernel based on the 2.6.20
source tree. If you've never compiled/installed a linux kernel, you
can find plenty of help on the Web, for example, http://linuxgazette.net/111/krishnakumar.html.
- Reboot into the new kernel to test it. Run uname -a
to verify you're in the new kernel.
Next, you need to try compiling and installing a simple "Hello World"
kernel module. For a nice tutorial on linux kernel module
programming, check out http://tldp.org/LDP/lkmpg/2.6/html/index.html.
This tutorial includes several hello world examples, as well as
information on how to insert yourself into the /proc filesystem.
Next steps
Now, you're ready to go ahead and solve the assignment. I found it
useful to break things down into a few steps. First, successfully
create yourself a kernel module that prints out a fixed string when
somebody cat's the /proc/get_num_pagefaults file. Next, figure out
how you are going to get statistics on the number of pagefaults from
the kernel.
Hint: the linux kernel already
keeps track of this statistic, and the kernel exports the appropriate
symbol that you need to access that statistic from your module.
Hint 2: if you look in the
arch/i386/mm/fault.c source code file of the linux kernel, you'll see
a function called do_page_fault, which is the first real function in
the kernel that gets called on a page fault. Inside that function,
you can see that it invokes another function called "handle_mm_fault"
to actually handle the page fault. Find the handle_mm_fault function
(it's somewhere else in the kernel source tree, in the
archtecture-independent part), and keep poking through the code path
to see if you find something useful.
Hint 3: not all functions and variables
in the kernel code are accessible to your kernel module. A symbol
has to be explicitly exported by the kernel to be accessible to your
module's code. The kernel uses the EXPORT_SYMBOL macro to export
a particular symbol, making it accessible to your module.
Hint 4: you might need to declare that
your kernel module is licensed under the GPL; some kernel symbols
are only exported to kernel modules that declare themselves to do this.
To declare it, include the following line of code in your kernel module
source, just after your includes:
MODULE_LICENSE("GPL");
What to turn in
Please email Steve and Elizabeth the following:
- a very, very short description of how you implemented your module -- i.e., how you got the information you needed about page faults from the kernel, how you hooked into the /proc filesystem. No more than a handful of sentences on each issue.
- your source code, including a Makefile for compiling your kernel module
- a transcript like the one at the top of this page, showing you putting your module through its paces