Sunday, March 20, 2011

Hello world Kernel Module.

Kernel module basics are explained in the previous post, and that would help you a lot if you are a beginner, I advise you to go through it first. Here is the link: Kernel module basics.
Hope you have gone through the post on kernel module basics, now it’s simple to write and even easy for me to explain.  Till 2.4 kernels the module init/initialization function is called “init_module” which is called when the module is loaded into kernel using insmod and clean-up/exit function as “cleanup_module” called when the module is unloaded from kernel using rmmod. As of Linux 2.4, you can rename the init and clean-up functions of your modules; they no longer have to be called init_module() and cleanup_module()respectively. This is done with the module_init() and  module_exit() macros. These macros are defined in linux/init.h. The only caveat is that your init and cleanup functions must be defined before calling the macros, otherwise you'll get compilation errors. 


The module_init() macro defines which function is to be called at module insertion time (if the file is compiled as a module), or at boot time: if the file is not compiled as a module the module_init() macro becomes equivalent to __initcall(), which through linker magic ensures that the function is called on boot.
The function can return a negative error number to cause module loading to fail (unfortunately, this has no effect if the module is compiled into the kernel). For modules, this is called in user context, with interrupts enabled, and the kernel lock held, so it can sleep.

This module_exit() macro defines the function to be called at module removal time (or never, in the case of the file compiled into the kernel). It will only be called if the module usage count has reached zero. This function can also sleep, but cannot fail: everything must be cleaned up by the time it returns.


Code: hello_world.c
 

#include <linux/kernel.h> /* Needed for Macros*/
#include <linux/module.h> /* Needed for all kernel modules*/
#include <linux/init.h> 
#include <linux/version.h>

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("This is a my First Test Module...!");
MODULE_AUTHOR("GVK51");


static int __init my_start_init(void){

        printk(KERN_INFO "Hello World module loaded...!\n");
        return 0;
}

static void __exit my_remove_exit(void){

        printk(KERN_INFO "Hello World module Un-loaded...!\n");  

}

module_init(my_start_init);
module_exit(my_remove_exit);

Makefile:

obj-m   :=      hello_world.o

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

clear:     

        make -C /lib/modules/$(shell uname -r)/build/ M=$(shell pwd) clean
Note: Copying the code directly into your source.c file will also copies the invisible characters and finally you will left out with stray errors, i recommend you to type the code and that even becomes a practice. In this post Loadable Kernel Module i have explained all the basic concepts of modules, module loading and unloading and utility commands.
Building module Sequence Steps:
Click to Download Code

1 comment:

  1. Hi,
    I have successfully loaded a module, but on doing modprobe -r my_module,
    I get a hang for 2 mins followed by a call trace.

    I have added some debug prints inside the exit function, but i don't any of those in the demsg. I guess the module_exit is not being hit even!

    Any suggestions on how I can cleanup my module?

    ReplyDelete