ANU College of Engineering and Computer Science
Research School of Computer Science
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 ObjectivesThe main learning objectives for this lab are to:
PreparationLook 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 moduleAt 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/helloThe 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:
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 ExtraUse vmalloc for allocating the memory and use the correct method to copy data from user space into the kernel.