Linux Device Drivers

Know how Linux drivers are written...

Home | Open Source | Shell Script | Linux Internal | Linux Device Drivers | About Me

So, you want to write a kernel module. You know C, you've written a few normal programs to run as processes, and now you want to get to where the real action is, to where a single wild pointer can wipe out your file system and a core dump means a reboot.

What exactly is a kernel module? Modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system. Without modules, we would have to build monolithic kernels and add new functionality directly into the kernel image. Besides having larger kernels, this has the disadvantage of requiring us to rebuild and reboot the kernel every time we want new functionality.

 

 

Hello, World (part 1): The Simplest Module

When the first caveman programmer chiseled the first program on the walls of the first cave computer, it was a program to paint the string `Hello, world' in Antelope pictures. Roman programming textbooks began with the `Salut, Mundi' program. I don't know what happens to people who break with this tradition, but I think it's safer not to find out. We'll start with a series of hello world programs that demonstrate the different aspects of the basics of writing a kernel module.

Step:1

Here's the simplest module possible.

Example:. hello-1.c

/* 
 *  hello-1.c - The simplest kernel module.
 */
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */

int init_module(void)
{
 printk(KERN_INFO "Hello world 1.\n");

 /*
  * A non 0 return means init_module failed; module can't be loaded.
  */
 return 0;
}

void cleanup_module(void)
{
 printk(KERN_INFO "Goodbye world 1.\n");
}
 

 

Kernel modules must have at least two functions: a "start" (initialization) function called init_module() which is called when the module is insmoded into the kernel, and an "end" (cleanup) function called cleanup_module() which is called just before it is rmmoded. Actually, things have changed starting with kernel 2.3.13. You can now use whatever name you like for the start and end functions of a module However, many people still use init_module() and cleanup_module() for their start and end functions.

Typically, init_module() either registers a handler for something with the kernel, or it replaces one of the kernel functions with its own code (usually code to do something and then call the original function). The cleanup_module() function is supposed to undo whatever init_module() did, so the module can be unloaded safely.

Lastly, every kernel module needs to include linux/module.h. We needed to include linux/kernel.h only for the macro expansion for the printk() log level, KERN_ALERT,

Step:2 Makefile

Now we will write the makefile which will compile our hello-1.c

obj-m += hello-1.o

all:
 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

 


Makefile for a basic kernel module save the file, now do the make

Step:3

 hostname:~/lkmpg-examples/02-HelloWorld#] make

Step:4

Use modinfo hello-*.ko to see what kind of information it is.

hostname:~/lkmpg-examples/02-HelloWorld# modinfo hello-1.ko


Step:5

Now it is time to insert your freshly-compiled module it into the kernel with

hostname:~/lkmpg-examples/02-HelloWorld#]insmod ./hello-1.ko

You are done..............

now check kernel message log to see if our hellow world is executed or not...

step:6

hostname:~/lkmpg-examples/02-HelloWorld#]dmesg

Step:7 Unload the kernel module

hostname:~/lkmpg-examples/02-HelloWorld#]]rmmod hello-1.ko

repeate the step 6 to check and varify the exit module exicuted.

Congratulations , you are now a Linux Kernel programmer

by Unknown

Who cares as long as you understood this article  :)


Enter supporting content here