Linux Programming Tutorial - 1
Linux Kernel Module Programming - 1
Hello Friends,
I like Linux Kernel Programming, so I thought to start basic tutorial regarding Linux Kernel module programming. So it will help me and other beginners also.
Let’s start from beginning....
Kernel is the core component of Operating system which is a bridge between application and hardware. Communication between hardware and Software is shown below.
Hello Friends,
I like Linux Kernel Programming, so I thought to start basic tutorial regarding Linux Kernel module programming. So it will help me and other beginners also.
Let’s start from beginning....
- What is Kernel?
Kernel is the core component of Operating system which is a bridge between application and hardware. Communication between hardware and Software is shown below.
-------------------------------------------------------------------------------------------------------------------------------
- Basic functionalities of Kernel
1. Kernel’s Primary function is to manage the computer’s resource and allow other programs to run and use these resources.
2.The Kernel is responsible for deciding which memory each process can use and also determine that what to do when not enough memory is available.
3.Kernel allocate request from application to perform I/O operation to appropriate device.
4.Process management, Memory management and System Calls.
-------------------------------------------------------------------------------------------------------------------------------
- User Space and Kernel Space
Kernel runs in Kernel space and normal programs run in user space. Kernel space is accessed by user processes only through the System calls. System calls are request by kernel like I/O or Process creation.
Whatever normally we do in Linux system like writing text in editor, making graphical program,..Etc is in User space.
Calling interrupts I/O device communication..Etc comes in Kernel space.
Main reason for separating these two spaces is improve performance and making stable system. Otherwise user data and Kernel data will be disturb with each other causing fault or errors.
-------------------------------------------------------------------------------------------------------------------------------
- My Starting experience
Initially when I try to run Hello world Linux Kernel Program, it gives Error to me.
So after Updating my OS Kernel it runs okay. So if give error update your Kernel.
You can check version of your Kernel by uname –r command in terminal.
Initially my kernel in Fedora 15 KDE was 2.6.38.6-26.rc1.fc15.i686 and after kernel Updation it becomes 2.6.42.3-2.fc15.i686 . [ yum install kernel , then restart PC]
Hello world of Kernel module Programming
Need two files
- #1 Hello.c (Our Program code written)
- #2 Makefile (Program compilation command written)
Save bellow file as hello.c
--------------------------------------------------CODE STARTS HERE------------------------------------------------------
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
-----------------------------------------------CODE ENDS------------------------------------------------------------------
Kernel module contain main two functions init and cleanup module.
Init_module()
Its init of kernel module. It registers a handler for something with kernel or it replaces any of kernel function code.
Cleanup_module()
Safely unload of module. It does reveres of Init_module.
Printk()
Use for kernel log information or warning.
We can give any name to init_module() and cleanup_module(). That we will cover in next section.
Makefile
- Save bellow commands as Makefile name.
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
In Root>lib>modules our kernel 2.6.42.3-2.fc15.i686 folder will be there.
Now we have two files hello.c and Makefile.
Compilation:
1. Go to Linux terminal
2. write make command to run Makefile command contents.
It will give bellow message.And code compilation will be over.
Type: make -C /lib/modules/2.6.42.3-2.fc15.i686/build M=/home/Kernel/hello modules
-------------------------------------------------------------------------------------------------------------------------------
make[1]: Entering directory `/usr/src/kernels/2.6.42.3-2.fc15.i686'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/kernels/2.6.42.3-2.fc15.i686'
-------------------------------------------------------------------------------------------------------------------------------
3. bellow files will be generates.
-------------------------------------------------------------------------------------------------------------------------------
hello.mod.c , (C code, system generated structure)
hello.ko, (old one was .o file, in new kernel module is coming in .ko file)
modules.order, (give path of our .o file, ex: Kernel//home//folder//hello.o)
hello.ko, (Kernel module extension)
hello.mod.o, (object file of mod.c)
Module.symvers
-------------------------------------------------------------------------------------------------------------------------------
Now to see module information by write command modinfo hello.ko
It will give bellow informations.
Filename: hello.ko
Depends:
Vermagic: 2.6.42.3-2.fc15.i686 SMP mod_unload 686
Insert-remove Module:
1) insmod hello.ko
Insert module into kernel.
2) lsmod
It will show all moduls of kernel. Now you will able to see hello module listed in it.
3) rmmod hello.ko
It will remove module from kernel space.
4) dmesg
It will display message which we have written.
Log file entry will done in /var/log/messages
Few common text editors:
The text editors here have a basic support to their language, example it uses colours to respresent a type, int in blue, numbers in brown, these give a better way to read and write code.
Tool\Editor:-
SciTe Editor(Linux)
Here right side we can write our program code and Left side we can type our command which we normally run in Terminal. Try it if you like.Also you can see the nice colours.
===============================================================================================
Troubleshooting:
1) If you are getting errors ,try the following,
Common Error: "Removing 'hello':Device or Resource busy"
2)If you still face an error ,post @ our Facebook Group or @ the fourms.
3)If you find a bug which you are sure, please report at the support.
=====================================================================================
Written by Pratik Mota
Go to the Linux Section
Tutorial Table
Linux Kernel Module Programming -1 (You are here)
Linux Kernel Module Programming -2 (Comming Soon)
===============================================================================================
Troubleshooting:
1) If you are getting errors ,try the following,
Common Error: "Removing 'hello':Device or Resource busy"
- Try an older version of Linux Kernel(Example: 2.6)
- Make sure you have written or copied the code correctly
2)If you still face an error ,post @ our Facebook Group or @ the fourms.
3)If you find a bug which you are sure, please report at the support.
=====================================================================================
Written by Pratik Mota
Go to the Linux Section
Tutorial Table
Linux Kernel Module Programming -1 (You are here)
Linux Kernel Module Programming -2 (Comming Soon)