Hello World Module Programming
Linux kernel supports modular approach. It supports insertion and removal of code from itself at a routine. Module is loadable kernel object which we can load and unload during run time. Module is single binary image which contains the information of related subroutine, group, data, entry and exit. Device driver is also one type of module which provides the facilities to reload, debugging, and loading new driver on demand in response to hot plugging of new device. It is allowed to access hardware connected to the system. We can build monolithic kernel using module kernel and new functionality in kernel image.
Let’s understand how module would be created and work in Linux. Module is like a simple file and we can save it as C file with .c extension but it is somewhat different. It does not need to write code inside main function.
This is simple hello world module programming for understanding how module is created in Linux.
//********************************hello_module.c******************************//
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
static int __init hello_enter(void)
{
printk (KERN_INFO “Welcome In hello worldn”);
return 0;
}
static void __exit hello_exit(void)
{
printk (KERN_INFO “Goodbye, worldn”);
}
module_init (hello_enter);
module_exit (hello_exit);
//********************************hello_module.c******************************//
Here printk ( ) function behaves like printf ( ) in C language. You must be thinking if printk ( ) behaves same like printf ( ), why printk ( ) created. printk ( ) function gives the state information of kernel system. Module programming is done in kernel mode but kernel mode cannot use standard C library that’s why special printk ( ) function is used in debugging mode of kernel module programming.
How to create Make file
Makefile is compiling and rebuilding all the files with only single file. Make file needs to be rebuilt and compiled when some changes are done by programmer in file. Each application has main source file, header file and device file. We can make the executable file by compiling these files using make file. It has some rules to write command and build / rebuild the file which are managed by make program.
Let’s make the Makefile to compile and build hello_module.c file.
Create new Directory named Hello and Open the text document file and save it as Makefile in same directory.
//************************************Makefile********************************//
obj-m += hello_module.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********************************//
Write the same code as above in Makefile and save it in Hello directory.
obj-m += hello_module.o indicates the object file of hello_module.c
Build and compile Make File
Make sure you are in same directory where Makefile is stored (inside Hello directory in this tutorial) before building and compiling it. Write the following command line and press ENTER key:
$ Make
Now your hello module is loaded into kernel. This command creates a device module file and kernel object file inside the Hello directory.
Following output is displayed on screen:
make -C /lib/modules/3.16.0-30-generic/build M=/home/ashish/DD modules
make[1]: Entering directory `/usr/src/linux-headers-3.16.0-30-generic’
CC [M] /home/ashish/DD/ hello_module.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/ashish/DD/ hello_module.mod.o
LD [M] /home/ashish/DD/ hello_module.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.16.0-30-generic’
You need to enter the following command line for installing the hello module by running hello_module.ko file:
insmod hello_module.ko
This insmode command executes initial hello_enter ( ) function and installs the hello module in kernel.
Let’s see if the hello module is loaded or not. Read the message of hello module by following command and then press ENTER key:
dmesg | tail
You can read the last content of file and display the message “Welcome In hello world” on screen.
Remove the hello module by entering following command:
rmmod hello_module
This rmmod command unloads the module which is currently in run. Let’s see if the hello module is unloaded or not. Read the message of hello module by following command and then press ENTER key:
dmesg | tail
You can read the last content of file and display the message “Goodbye, world” on screen which indicates that the hello module is unloaded.
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.