Developing and understanding your first Linux kernel module

first

Lets start by writing a very basic kernel module:

1. write a module which prints some strings in console (/var/log/messages)
2. Makefile which compile this module
3. load the module and see the messages
4. unload the module.

#include <linux/module.h>

/* Just a simple to understand modules and makefile */

int __init mymodule_init (void)
{
printk(KERN_INFO "Module Loaded..\n");
return 0;
}

void __exit mymodule_exit(void)
{
printk(KERN_INFO "Module Exited\n");

}

module_init(mymodule_init);
module_exit(mymodule_exit);

MODULE_AUTHOR("Soorej P");
MODULE_DESCRIPTION("Sample Module");
MODULE_LICENSE("GPLv3");

There you go.. a very simple kernel module. Lets name it firstmod.c. Lets understand the code:

The kernel modules do not have main() function to start with. When the module is loaded (or when kernel is booted if compiled as built-in in kernel), an initialize function is called which is declared as:

int <init function name>( void);

On successful initialization the function should return 0 and on failure -ve value.

similar function for unloading the module is:

void <exit function name> (void);

The function names can be of your choice. But, the kernel will know these are the init/exit functions only when you register them with module_init(<init function name>) and module_exit(<exit function name>)

the __init and __exit are macros to let the kernel know that these functions are used only on init or unload time. This will optimize the use and will release the memory after the initialization since it wont be used again (or won’t allocate till unload function is called in case of exit).

The macros starting with MODULE_ identifies the module. We can set the author name, description etc. If MODULE_LICENSE is not GPL(ed), the module will warn you that the module license taints the kernel. Most of the exported symbols by other kernel modules are accessible only if licensed under GPL. Possible license values are available in /usr/lib/modules/<kernel version>/build/include/linux/module.h

Makefile:

Now to go ahead and compile, lets make a Makefile. We can also compile from command line but make file is much useful and a must have when you go ahead.

Create a file with name Makefile with following content in the same directory where the sample code firstmod.c is:

obj-m += firstmod.o

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

Please note that you should use <tab> instead of white spaces in starting of lines. Here all and clean are targets and obj-m is used since we are building our code as a module not for built-in kernel for which obj-y will be used.

now use make command will build the module and you can find your module firstmod.ko. During build, the make will create intermediate files and other binary files which will be present in the same folder. To remove all these files and keep only the source use the command make clean

first_compile

once the command is successfully built, use insmod to load and rmmod to unload the module.

insmod

Makefile: building kernel module from multiple source files

Lets split the source file to two

sec.c:

#include <linux/module.h>
void print_value(int val)
{
printk(KERN_INFO "Module Loaded.. %d\n",val);
}

firstmod.c:

#include <linux/module.h>

/* Just a simple to understand modules and makefile */
extern void print_value(int);
int __init mymodule_init (void)
{
print_value(10);
return 0;
}

void __exit mymodule_exit(void)
{
printk(KERN_INFO "Module Exited\n");

}

module_init(mymodule_init);
module_exit(mymodule_exit);

MODULE_AUTHOR("Soorej P");
MODULE_DESCRIPTION("Sample Module");
MODULE_LICENSE("GPLv3");

Now function print_value is defined in sec.c but used in firstmod.c

We can compile this by modifying the Makefile as follows:

obj-m += hellomod.o

hellomod-y += sec.o firstmod.o

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

After compiling the module with make, a loadable module will be available as hellomod.ko which combines sec.o and firstmod.o

 

 

We need your valuable your feedback and comments for improving this web site. Also, please follow us with your favorite social media (links below)

One comment

  • You share interesting things here. I think that
    your website can go viral easily, but you must give it initial boost and
    i know how to do it, just type in google – mundillo traffic increase

Leave a Reply

Your email address will not be published. Required fields are marked *

17 + sixteen =