Linux Device Drivers Part - 5 : Building and Compiling Kernel Moduels

In this part, we will discuss about 5 topics
  1. Linux Device Driver Example References
  2. Setting up Environment for compiling Kernel modules. 
  3. Implementing Helloworld Kernel module and Macros used in it.
  4. Creating Makefile to compile Helloworld Kernel module
  5. Compilation and Kernel object generation of Helloworld Kernel module
mrtechpathi_Title_Page_Linux_Device_Drivers_5

1. Linux Device Driver Example references

Before proceeding further, make sure you have watched my earlier videos  on "Linux Device drivers"  Part 1 to Part 4.

As mentioned in my earlier videos, most of the concepts  discussed in these sessions are  derived from famous O'Rielly Linux Device Drivers  3rd edition.

To demonstrate Kernel programming concepts, I will be using the O'Reilly  sample programs which are available on below site to download.
      http://examples.oreilly.com/linuxdrive3  

Download these files and save it to your harddrive. Compiling, loading and modifying these examples are good way to improve your understanding of how Linux Device drivers work and interact with Kernel.

All examples work with any 2.6.x Kernel. You need to install Kernel headers before Building and running your modules.

2. Setting up Environment for compiling Kernel modules. 

I am using a Virtual Linux system in Windows 7. To know how to install Linux on Windows 7 Pc, please watch my earlier video on "Installing Linux in windows 7 PC using Oracle VM Machine"


Now we will install the necessary packages required for compiling Compiling a Kernel module.

In order to install any package you should be a super user (from here on we will address it as root).

Login as root by typing "su" command in the terminal.

Enter the root password.

Its always safe and good practise to login as user and then switch to root when  required.

Type below command to install Kernel headers and other basic packages required for compiling kernel modules.

$sudo yum install vim gcc kernel-devel kernel-headers dkms make bzip2

Lets see more about the packages which we are going to install

Vim  – Popular Linux Editor
gcc  – C Compiler
Kernel-devel – are the files that are required for compiling kernel code that will run as part of the kernel, such as kernel modules.
Kernel-headers - contains files that describe the system environment and are used for compiling normal programs that run in userspace.
dkms - Dynamic kernel module support package
make - make file tool necessary for Kernel code buildsystem.
bzip2 - zipping tool

While installation kernel may ask "Is this ok Y/N". Type Y and complete the installation.

At the end, you will observe kernel source installed under /usr/src/kernels/2.6.xx

Once the installation is done, reboot your Linux Machine.

Once your virtual box is rebooted, login with user credentials.

We have completed the setting up environment.

3. Implementing Helloworld Kernel module and Macros used in it.

Now lets begin with Hello world Module

Log into your Virtual machines as a user.

Open a terminal window

Create a directory with name LDD-3 (Its your choice) anywhere preferably in your home directory (like /home/mrtechpathi)

Change directory to it

cd LDD3

Create your first helloworld module. So how do you do that ?

For this we will use vim editor. Which is very popular and used by many Linux programmers all over the world. Though novice programmers may face issues but will be wonderful editor to work with.

Now lets type hello world program. I will name it as hello.c
 #include <linux/init.h>  
 #include <linux/module.h>  
 MODULE_LICENSE("Dual BSD/GPL");  
 static int hello_init(void)  
 {  
   printk(KERN_ALERT "Hello, world\n");  
   return 0;  
 }  
 static void hello_exit(void)  
 {  
   printk(KERN_ALERT "Goodbye, cruel world\n");  
 }  
 module_init(hello_init);  
 module_exit(hello_exit);  

Instead of copy pasting this program, I would recommend to type program yourself which makes you understand each and every line of it.

Great you have coded your first Hello world kernel module.

Lets understand more about the helloworld kernel module

Kernel headers used in Module

#include <linux/init.h>
This header contains the definition of the functions used in this module.
Module_init() and Module_exit() are defined in this file.

Module_init() is a macro which defines the function to be called at module
insertion time or at system boot time.

Module_exit() is a macro which defines the function to be called at module removal time. This function is also called Celan up function.

Kernel modules must always contain these two functions, init_module and cleanup_module.

If you clearly observe in helloworld kernel module code (above) , we have used a two kernel headers and special kernel macros.
 1.MODULE_LICENsE  
 2.printk  
 3.KERN_ALERT  

 Lets see whats significance of these Kernel headers.

1. MODULE_LICENSE
This macro is used to tell the kernel that this module bears a free license; without such a declaration, the kernel complains when the module is loaded.

2. Printk function
This function is similar to printf in standard C. This is implemented to make kernel have its own print function instead of having dependency on C library.

3.KERN_ALERT
This string is the  priority of message. KERN_ALERT is the highest priority set to specify kernel to print the message immediately.We will discuss more about this macro in up coming sessions.

Coming to the functions used in this Helloworld kernel module,
There are two main functions.

hello_init and hello_exit

Function 1: hello_init()
This particular function is called when the module is loaded

Function 2: hello_exit()
This particular function is called when module is removed.

We will see more about loading and removing kernel modules in coming session

Now we have our hello.c module implemented. Its time to compile

To compile this Kernel modules, we need to create a Makefile.

I am in LDD-3 directory where my hello.c is present.

Before creating a makefile for our helloworld module, we need to know the path of kernel build directory. For this first we need to know the kernel version.

To know the kernel version used in your PC, type "uname -a"command in the terminal.
 [root@localhost LDD]# uname -a  
 Linux localhost.localdomain 2.6.32-431.20.5.el6.i686 #1 SMP Fri Jul 25 05:42:52 UTC 2014 i686 i686 i386 GNU/Linux  

This command displays the current linux version.

Make a note of this name  for example, 2.6.32-431.20.5.el6.i686.

With this information, we will try creating a makefile for our helloworld module.

4. Creating Makefile to compile Helloworld Kernel module

Open a vim editor and type these couple of lines.
 obj-m := hello.o  
 KDIR := /lib/modules/2.6.32-431.20.5.el6.i686/build  
 PWD := $(shell pwd)  
 default:  
     $(MAKE) -C $(KDIR) SUBDIRS=$(PWD)  

obj-m:=hello.o
This assignment states that there  is one module to be built from object file hello.o.

Some may wonder how this single line does whole job.

The kernel build system in Linux 2.6 Kernel is designed in
such  a way to handler the rest of the parameters.

KDIR := /lib/modules/2.6.32-431.20.5.el6.i686/build
Its Kernel build directory. The director which starts with 2.6.32 here is the one which we noted down before creating this Makefile. We got this by executing 'uname -a'

PWD := $(shell pwd)
Current directory path. It shows the kernel about the current location of our kernel module. Since we are creating makefile in the same directory, we will read current path to PWD variable.

Now the last line
default:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD)

This is the main make command which switches to your Kernel build directory and invokes the make command to compile helloworld kernel module present in the sub-directory.

-C option tells make command to switch to kernel build directory. directory.

5. Compilation and Kernel object generation of Helloworld Kernel module
Now we have Makefile and hello.c file in our LDD-3 directory.

Lets execute the program. Executing program is very simple now.

Just type ‘make’ command in the directory where your Kernel module and Makefile are present.

make will compile the helloworld program and generates the kernel object of helloworld module.

We can find kernel object hello.ko in the same directory.

In next session we will see how to load and remove this kernel module.

Hey dont forget to watch Linux Device Part -5 in youtube. I am sure you will like it and give a thumbs-up.

Comments

Popular posts from this blog

Linux Device Drivers - Part 4 : Linux Kernel Moduels (LKM) and types of LKM's

Linux Device Drivers - Part 13 : More on Device Numbers