kernel – hello world

To compile up this module you will need to have the linux header files, which should be installed but if not you just need to either use apt-get, rpm, pacman etc to install the linux header files.

With kubuntu I use the aptitude command to install applications and updates etc, but to get the linux-headers I used

aptitude install linux-headers

that is normally linked to the latest version of the linux kernel that you are using.

The main part of the program is outputting a potential parameter being passed (passing parameters to a nvidia kernel module here) and also saying hello world, kinder two for the price of one as such tutorial.

As in c language there is the printf, but in the kernel there is a printk which is the same similar thing but more for outputting errors/messages to the user.

So for example

printk(KERN_INFO "hi there\n");

the KERN_INFO is a kernel information marco that sends the details to a information level of output, e.g. /var/log/messages or you could use dmesg

here is the code on how to pull in parameters from a kernel module being loaded, the parameter_int is my parameter to be checked against and the S_IRUSR is the access rights (IR = read permission, IW = write permission, USR = user, GRP = group, OTH = others)

The module_param takes 3 parameters, the first is the variable to place the value into, second is the variable type and the third is the access rights, with the MODULE_PARM_DESC is the description of the parameter to pass, in this case it is just a integer value.

static int parameter_int = 0;
module_param(parameter_int, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(parameter_int, "An integer value");

Within a normal c/c++ program there is a main function where the program is run from, but in the kernel space since the main program is already running you can define what functions to call when the module is inserted and also removed,


the module_init parameter is the function to call when the module is loaded and the module_exit parameter is the function to call when module is removed from the kernel space.

Here is the full code, if you save this as helloworld_parameter.c

/* hello world with passing some parameters in the kernel module */
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
/* need to setup a setup a static variable to hold the parameter value and
   set it to a default value is none is passed */
static int parameter_int = 0;
/* the linux/stat.h has the S_IRUSR definitions etc.. */
/* S_IRUSR = read permission, owner
   S_IWUSR = write permission, owner
   S_IRGRP = read permission, group
   S_IROTH = read permission, others
module_param(parameter_int, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(parameter_int, "An integer value");
static int __init hello_init(void)
	printk(KERN_INFO "Hello world\n\n");
	printk(KERN_INFO "my parameter int value is : %d\n", parameter_int);
	return 0;
static void __exit hello_exit(void)
	printk(KERN_INFO "Goodbye from hello world parameter\n");

since we are compiling a kernel module we need to link to the loaded kernel modules, here is a Makefile to compile up the program, so save this as “Makefile”

obj-m += helloworld_parameter.o
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

the first is the obj-m += which means compile a object module and you could have more than one file to compile up so use the += to add more files to it, the -C means change directory for the build environment for the kernel space, the M is a parameter passed to the build environment to use this current directory for where the source files are, and the modules means to create kernel modules e.g. filename.ko.

once you have run the


there should be a file called helloworld_parameter.ko, to find out details about your new module you can use the modinfo

modinfo helloworld_parameter.ko
filename:       helloworld_parameter.ko
author:         genux
license:        GPL
srcversion:     A81F18D40DA3C5FAB1C71FF
vermagic:       2.6.31-17-generic SMP mod_unload modversions
parm:           parameter_int:An interger value (int)

and the parm: is the important part here, it is what the parameter is called to pass a value to for example to watch the module being inserted if you open up two consoles and on one put

tail -f /var/log/messages

in the second console do

insmod helloworld_parameter.ko
rmmod helloworld_parameter.ko
insmod helloworld_parameter.ko parameter_int=3
rmmod helloworld_parameter.ko

and the output should be in the first console

Hello world
my parameter int value is : 0
Goodbye from hello world parameter
Hello world
my parameter int value is : 3
Goodbye from hello world parameter

hope that helps with kernel modules, I am planning on doing a kernel module for a custom built USB device.

Leave a Reply

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