CS 0449 – Project 4: /dev/pi
Due: Sunday, April 3, 2011 at
11:59pm
Penalty-free Extension: Tuesday, April 5,
2011 at 11:59pm
Standard UNIX and Linux systems come with a few special files like /dev/zero, which returns nothing but zeros when it is read, and /dev/random, which returns random bytes. In this project, you will write a device driver to create a new device, /dev/pi which returns the digits of everyone’s favorite irrational number: p.
For this project we will need to create three parts: the device driver, the special file /dev/pi, and a test program to convince ourselves it works. The good news is that I have provided a function that will generate the first N digits of pi for you. Your job is then to make a device driver, and a simple program named pi_digits that takes two command-line arguments of which digits of pi to return (start to end, zero indexed):
./pi_digits 0 3
3141
./pi_digits 4 7
5926
Our device driver will be a character device (although it could be a block device since we can access individual bytes of pi by address) that will implement a read function and return the appropriate substring of pi back to the user.
KDIR := /lib/modules/$(shell uname -r)/build
to
KDIR := /u/SysLab/shared/linux-2.6.23.1
make ARCH=i386
scp USERNAME@thot.cs.pitt.edu:/u/SysLab/USERNAME/hello_dev/hello_dev.ko .
insmod hello_dev.ko
cd /sys/class/misc/hello
cat dev
cd /dev/
mknod hello c 10 63
cat /dev/hello
rm /dev/hello
rmmod hello_dev.ko
The code for the example we went through comes from:
Read that while going through the source to get an idea of what the Module is doing. Start with the third section entitled “Hello, World! Using /dev/hello_world” and read up until the author starts describing udev rules; we are not using udev under QEMU.
When you have an idea of what is going on, make a new directory under your /u/SysLab/USERNAME directory called pi_driver:
mkdir pi_driver
Copy and rename the hello_dev.c from the example, and copy over the Makefile. Edit the Makefile to build your new file. Change all the references of “hello” to “pi_driver”.
The pi generating function can be copied from the /u/SysLab/shared directory:
cp /u/SysLab/shared/pi.h .
Your job is to make a read function that returns the appropriate substring of pi back (starting at *ppos and stretching out count digits. Notice that the pi.h code will give you all the digits from 3141 until some maximum you specify. You’ll need to make this work.
To build any changes you have made, on thot in your pi_driver directory, simply:
make ARCH=i386
If you want to force a rebuild of everything you may want to remove the object files first:
rm *.o
From QEMU, you will need to download the driver that you just built. Use scp to download the driver to a home directory (/root/ if root):
scp USERNAME@thot.cs.pitt.edu:/u/SysLab/USERNAME/pi_driver/pi_driver.ko .
As root (either by logging in or via su):
insmod pi_driver.ko
Like in the example, we’ll need to determine the MAJOR and MINOR numbers for this particular driver.
cd /sys/class/misc/pi_driver
cat dev
and use those numbers to make the /dev/pi file:
cd /dev
mknod pi c MAJOR MINOR
As root (either by logging in or via su):
rmmod pi_driver.ko
Then you can remove the /dev/pi file:
rm /dev/pi
Since thot is a 64-bit machine and QEMU emulates a 32-bit machine, we should build with the –m32 flag:
gcc –m32 –o pi_digit pi_digit.c
We cannot run our pi_digit program on thot.cs.pitt.edu because its kernel does not have the device driver loaded. However, we can test the program under QEMU once we have installed the new driver. We first need to download pi_digit using scp as we did for the driver. However, we can just run it from our home directory without any installation necessary.
One of the major contributions the university provides for the AFS filesystem is nightly backups. However, the /u/SysLab/ partition is not part of AFS space. Thus, any files you modify under your personal directory in /u/SysLab/ are not backed up. If there is a catastrophic disk failure, all of your work will be irrecoverably lost. As such, it is my recommendation that you:
Backup all the files you change under /u/SysLab to your ~/private/ directory frequently!
Loss of work not backed up is not grounds for an extension. You have been warned.
You need to submit:
Make a tar.gz file named USERNAME-project4.tar.gz
Copy it to ~jrmst106/submit/449 by the deadline for credit.