Title: Rapid prototyping of LKMs
1Rapid prototyping of LKMs
- Configuring our Linux systems for the quick
creation and use of loadable kernel modules
2Fewer keystrokes?
- If you want to run a program you wrote that
resides in your current directory, you normally
have to type the full pathname - ./myprog
- But you can adjust your logon-settings so you
wont have to specify the directory - myprog
- You just need to edit your .bash_profile
3Environment variables
- You can view your present environment using the
set command, like this - set more
- Look for the line that begins with PATH
- This line tells your command-shell (bash) which
directories it should search to find a program
whose name you entered, so you can add . (the
current directory) to PATH
4The PATH variable
- Your PATH environment-variable gets initialized
each time you login - Its value is setup by your .bash_profile
- This hidden file is your home directrory
- You can view it using the cat command
- You can also edit it (e.g., with vi) but be
very careful if you do so!
5Including . in your PATH
- If you want your command-shell to search for
programs in your current directory, you can edit
the PATH line in .bash_profile - Example change the line that says
- PATHPATHHOME/bin
- so that it looks like this
- PATHPATH.HOME/bin
6Notice HOME/bin
- You can create a new subdirectoy (named bin) in
your home directory, and use it as a place to put
your often-used programs (such as tools like
newmod and mmake) - Then, after you execute the .bash_profile
script, you can run those programs from any
directory, without having to type a full pathname
-- bash will now find them!
7A word about wizards
- We will be writing lots of modules
- We can automate the boilerplate
- We should write a wizard program
- It will save us LOTS of time!
8Our newmod example
- The newmod.cpp application creates the bare
minimum of module source-code - Its an example of a wizard tool but its not
as useful as it could be, since the code it
creates doesnt really DO anything - Tonight we learn to use a better wizard, one
that will prove very useful in studying the way
the new Linux kernel 2.6 works
9Types of files
- UNIX systems implement ordinary files for
semi-permanent storage of programs/data - But UNIX systems also implement several kinds of
special files (such as device-files and
symbolic links) which enable users to employ
familiar commands and functions (e.g., open(),
read(), write(), and close()) when working with
other kinds of objects
10Pseudo-files
- Among the various types of special files are
the so-called pseudo files - Unlike ordinary files which hold information that
is static, the pseudo-files dont store any
information at all but they produce
information that is created dynamically at the
moment when they are being read - Traditionally theyre known as /proc files
11Text in /proc files
- Usually the data produced by reading from a
/proc file consists of pure ASCII text (a few
exceptions exist, however) - This means you can view the contents of a /proc
file without having to write a special
application program just use cat! - For example
- cat /proc/version
12More /proc examples
- cat /proc/cpuinfo
- cat /proc/modules
- cat /proc/meminfo
- cat /proc/iomem
- cat /proc/devices
- cat /proc/self/maps
- Read the man-page for details man proc
13Create your own pseudo-files
- You can use our newinfo.cpp wizard to create
boilerplate code for a module that will create
a new pseudo-file when you install the module
into a running kernel - The modules payload is a function that will
get called by the operating system if an
application tries to read from that file - The get_info() function has full privileges!
14An example jiffies.c
- There is a volatile kernel variable (named
jiffies) that keeps track of elapsed time - It is used by the task-scheduler to decide when
to perform a task-switch - It increments 100 times/sec (kernel 2.4)
- Its normally inaccessible to user programs
- But we can create a /proc file that will let us
view the jiffies value at any moment
15Edit the get_info() function
- Just replace the statement
- len sprintf( buflen, s\n, modname )
- with this statement
- len sprintf( buflen, jiffies d \n,
jiffies ) - Compile and install the module, then type
- cat /proc/jiffies
16In-class exercise 1
- Try creating your own jiffies pseudo-file
- 1) use newinfo to build your boilerplate
- 2) edit one line in your modules get_info
- 3) compile your module (using mmake)
- 4) install your jiffies.ko kernel-object
- 5) use the cat command to see jiffies
17A more important example
Control Register Cr4
This Pentium register contains a collection of
flag-bits which enable or disable special
architectural extensions to the CPU (as
documented in Intels Software Developer Manual,
vol3)
movl cr4, ebx privileged instruction
Among the flag-bits in this register are two
which affect how the Pentium translatres virtual
memory-address into physical memory-addresses.
Well need to know how these are set!
18Our cr4.c module
- Weve written a module that will create a
pseudo-file (named /proc/cr4) that can let us
view the contents of Control Register 4 - cat /proc/cr4
- You can download our modules code from the class
website and try it out - You can learn what all the flag-bits mean by
reading Intels Developer Manual online
19In-class exercise 2
- Revise the source-code in the cr4.c file so
that it will display the meanings of each
flag-bit in register CR4 - For example, your /proc/cr4 file should print
output that looks like this - cr4 000006D0 PSE1 PAE0 VME0
- (but show ALL of the implemented bits)