Thursday, June 21, 2012

First Kernel Programming

In this blog i use 'kernal' instead of 'kernel' intentionally to identify my files.

In computing, the kernel is the main component of most computer operating systems; it is a bridge between applications and the actual data processing done at the hardware level. The kernel's responsibilities include managing the system's resources (the communication between hardware and software components). Usually as a basic component of an operating system, a kernel can provide the lowest-level abstraction layer for the resources (especially processors and I/O devices) that application software must control to perform its function. It typically makes these facilities available to application processes through inter-process communication mechanisms and system calls. (source : Wikipedia)

There are numerous materials available on linux kernel.

In general we insert and remove the module from a kernel in order to avoid recompiling the whole kernel from the scratch. Here in this blog we will get to know how to write a first linux kernel programming module from the basic. 



  Step 1 : Creating a folder kernalprogramming

                     
    Step 2 : Create a file called firstkernal.c



















Step 3 : Write the first kernel program

 #include <linux/init.h>
#include <linux/module.h>

// To start the module

static int hello_init(void) {
    printk(KERN_ALERT "TEST : Hello world. this is jithu");  
    return 0;
}

// To end the module

static void hello_exit(void) {
    printk(KERN_ALERT "TEST : Hello world. good bye");  
}

// To call the start and end modules

module_init(hello_init);
module_exit(hello_exit);































In the above program, linux/init.h and linux/module.h are the header files located in /usr/src/

This location is important to provide in the Makefile which will be discussed soon.

Next hello_init is a function which prints "TEST : Hello world. this is jithu"
 
Kernel programs do not call conventional c libraries such as "printf" "scanf" etc.. 

So we are using "printk" which will output into the screen

Another function is hello_exit, is a function which prints "TEST : Hello world. good bye"

module_init(hello_init); 

This line is the starting point. The parameter is the function and the function is called when we run/ insert the program into the kernel. (like a main in conventional programming)

module_exit(hello_exit);

This line is the ending point. The parameter is the function and the function is called when we run/ remove the program from the kernel. 

Step 4 : Creating a Makefile


#obj-m += filename.o

obj-m += firstkernal.o

#linux header files
#we can check using the command 'uname -r'

KDIR = /usr/src/linux-headers-3.2.0-23-generic

all:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

clean:
    rm -rf *.o *.ko *.mod.* *.symvers *.order



first line says obj-m += firstkernal.o
It means build driver calling firstkernal.o
obj = object  m = module 

KDIR is the directory where the linux headers are located

we can find the version using the command uname -r


Step 5 : Executing a make file

Just type 'make'


compiled files are created



Step 6 : Inserting the module into the kernel

sudo insmod firstkernal.ko 
and type
dmesg  : 

dmesg (for "display message" or "driver message" ) is a command on most Linux and Unix based operating systems that prints the message buffer of the kernel. 



 The output is 

Here I already I ran 3 times. The screen shot is for 4th time running "TEST : Hello world. this is jithu" is the output for the inserting a module


 Step 7 and final step : Removing the module from the kernel

sudo rmmod firstkernal.ko 


and type dmesg


 "TEST : Hello world. good bye" is the output for the removing a module 


That is the end of first basic kernel programming to print hello !! 

-----------------------XXXXXXXXXXXXX---------------------XXXXXXXXXXXXXXX---------------

2 comments:

  1. Glad that you are into 'coding' now :) :)
    I remember doing some of it as a part of advanced OS course.....
    For open source best thing is availability of resources online, best being discussion forums ..
    Waiting to see more from jithu ..... Try implementing some hi-fi driver on your own :)

    ReplyDelete
  2. hey jithuu, kallakara !! implement a file system just to learn. All the best ! :)

    ReplyDelete