CECS Home | ANU Home | Search ANU
The Australian National University
ANU College of Engineering and Computer Science
Research School of Computer Science
Printer Friendly Version of this Document

UniSAFE

Operating Systems Implementation

Laboratory 2

This laboratory should take you less than 2 hours to complete. Make sure you ask your tutor for help if you get stuck. You are expected to print out and read through these instruction before the lab time. Also your are expected to do about 1 hour of preparation.

Learning Objectives

The main learning objectives for this lab are to:
  • be able to use printk for debugging,
  • become familiar with developing with kernel modules, and
  • understand a bit more about the /proc filesystem.

Preparation

Look at some examples of proc entries in the linix kernel. In particular take note of the parameters to the read/write routines and how they work. Your will need to write this down and explain it to your tutor at the completion of the lab.

Also read through the lab and draft the code that you need to write.

During the Lab

Step 1 - A "hello world" kernel module

At the end of the 2nd laboratory you were asked to download a "hello world" kernel module and to compile it then insert it into the kernel. The module created a new file in the /proc virtual filesystem.

If you have not yet done that exercise then do it now. Pay particular attention to the way in which the module provides the contents of the /proc/hello file when it is read using cat.

Step 2 - printk - debugging

"printk" can be very useful in debugging the kernel and modules. "printk" works in a similar way to "printf", however, the output does not go directly to a terminal. The "dmesg" command can be used to view the "printk" output.

Add a "printk" within the "hello_read_proc" routine and check that it outputs correctly.

When you type "cat /proc/hello" how many times does "hello_read_proc" get called? Why is this the case?

Step 3 - Writing to /proc/hello

The hello module always provides the same output when the file /proc/hello is viewed. That output is hard coded in the "hello_read_proc()" function.

In this lab the aim is to make the /proc/hello file writable so that when a user writes a string to the file the kernel remembers the contents of the string and returns the same string when the file is later read.

To do this you will need to do a number of things:

  • change the call to create_proc_entry() so that the permissions on the file are changed to allow writing. It is your choice whether you make the file writable by all users or only by the owner. You may find the contents of /usr/include/linux/stat.h useful.
  • initialise the function pointer proc_entry->write_proc so that it points to a function (that you must write) which handles writes to /proc/hello. You may wish to call this function hello_write_proc().
  • write the function hello_write_proc() to accept input when written to /proc/hello and store it for later use in hello_read_proc(). You will need to look at the definition of struct proc_dir_entry (perhaps using a tags search) to find out the correct parameters to this function. You may also need to use kmalloc() to allocate some storage for the string.
  • modify hello_read_proc() to return the string created in hello_write_proc()
Remember to be careful about error checking! Hints: Remember to leave room in the memory you allocate for the string terminating null character. Also in the "hello_write_proc" routine the "buffer" points to the info you need to store and "count" refers to the length of that information.

kmalloc()

kmalloc() is the kernel equivalent of the malloc() function used to allocate memory in C. kmalloc() takes two parameters, the first being the size of the allocation and the second being the priority. The priority is used to determine how hard the kernels memory management system should try to allocate the memory. You should normally set this to GFP_KERNEL when allocating memory for use by the kernel. Memory allocated using kmalloc() can be de-allocated using the function kfree().

For very large memory allocations you should use the function vmalloc(). This is an alternative to kmalloc() that returns pageable memory. If you want to use vmalloc() then you will need to include linux/vmalloc.h.

Step 4 - Test your solution.

Test your solution by redirecting different strings into /proc/hello and check that the new string is the same.

Optional Extra

Use vmalloc for allocating the memory and use the correct method to copy data from user space into the kernel.