Linux Kernel Module Programming — Simplest Example
Let's write a simple Linux kernel hello world module, and try to understand all the basic things defined in that program.
Join the DZone community and get the full member experience.
Join For FreeToday lets talk about how to write a simple Linux kernel module. Let's write a simple hello world
module, and try to understand all the basic things defined in that program. For this guide, I assume you have a basic understanding.
- C programming language
- Makefiles
As you already know, we use C — Programming language to write Linux kernel modules. But we can not use normal C syntax for some cases in kernel module programs. All official guide can be found here. Please go through it if you are not familiar.
This is one of the basic hello-world
type Linux kernel module.
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sachith Muhandiram");
MODULE_DESCRIPTION("Simple first Linux module");
MODULE_VERSION("1.0.0");
static int __init initHelloWorld(void){
printk(KERN_INFO "Hello, this is my first kernel module \n");
return 0;
}
static void __exit exitHelloWorld(void){
printk(KERN_INFO "Exit Hello world module\n");
}
module_init(initHelloWorld);
module_exit(exitHelloWorld);
Let's get started with the header files we included for this, why, and what they do.
Makefile to generate the respective kernel module.
xxxxxxxxxx
obj-m += hello.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
Let's start with the header files we included.
linux/init.h
No wonder about its name and functionality, this header file includes all things related to __init
part. This provides you a detailed guide about how it works.
linux/module.h
This is where module_init
and module_exit
are defined. module_init
tells the kernel what is the entry point to our program and also only one module_init
call for the program we write. As the official comment says
module_init() will either be called during do_initcalls() (if * builtin) or at module insertion time (if a module)
module_exit
does the opposite, when we call rmmod module
, this will be a trigger and this calls to our __exit
function. Only one module_exit
for a program. If the driver is statically compiled into the kernel, module_exit()
has no effect. (official doc)
linux/kernel.h
This is where all the workload happening features are listed.
Macros
Above we have defined 4 macros. Those are mainly used for identifying our module. Here one most the most important macro is MODULE_LICENSE(“GPL”);
, if we do not define this, we won't be able to use any other system libraries running under GPL
license. For this simplest sample, this won't be a big issue, but when we develop a kernel driver, etc, this could be a big issue. So always remember to keep GPL
license if you need to get GPL
licensed libraries.
Also if you release your module, then you must select appropriate licensing to your module, and in that case, you would have to use static linking to GPL
licensed libraries. For more about GPL-license.
The other three macros are just basics, used to identify our module.
__init functionName(param)
This is the initialization point for the module. This feature is enabled kernel 2.2
. The compiler specially treats this function and add this at the beginning of the executable file.
Linux kernel will allocate memory for each __init
and free memory used by this after __init
function finishes for buildin drivers, for loadable modules, it keeps till we unload the module. (we use the second method). For more information please refer to this.
This and __exit
functions are also macros. init
returns 0 to the kernel if success.
__exit functionName(param)
This macro is used to free up resources hold by a module. Similar to init
functionality. This works as a cleanup for the loadable module we wrote, build-in device drivers do not need exit
function to cleanups.
module_init(functionName)
This is the entry point to our kernel module. We define our desired entry function as a parameter. This tells the kernel what our module’s functionality and setup kernel to run the module’s functionalities when the kernel needs.
This registers our functionality to kernel. After this function completes, our module does nothing till kernel requests to do something.
module_exit(functionName)
This is the opposite of module_init
. It unregisters our module functionality from the kernel. This is get called when rmmod ourModule
is called.
printk(KERNEL_LOGLEVEL,”msg”,ARG)
This is basically for logging kernel info, it has different logging levels. INFO
,WARN
,EMERG
, ALERT
, CRIT
, ERR
etc. You can see all details such as what are default numbers assigned to these macros etc from this.
Also, this makes sure our message is printed to /var/log/messages
.
Here I won't describe how Makefile
. When we run make
it creates all necessary modules, libraries for our module.
To add our module to the kernel, we use insmod ./hello.ko
(as our makefile creates outputs as hello
. This .ko
is the file that holds our module.
When this is loaded, you won't see anything at the terminal. As described earlier, it is printed to the kernel log. dmesg -w
will show our module is loaded. and it will have a record saying Hello, this is my first kernel module
To remove our module from the kernel, we use rmmod hello
, here we do not need to provide any file extension, just use our module name. This will invoke module_exit
function.
I think this is enough for today, I highly recommend you to read the programming guide book I have included in references.
This post was originally posted on my Medium page.
References:
Linux Kernel Module Programming Guide
Opinions expressed by DZone contributors are their own.
Comments