Linux Kernel Module Part 2

Linux Kernel Module Part 2

In this post i shall exclusively talk about the insmod untility.

As mentioned before the insmod utility is one of the ways to add modules to the kernel, the other two being using modprobe and kernel using kerneld daemon.

The insmod (insert module) is present as a user space utility.It calls the init_module user space system call, which calls the kernel system call sys_init_module.The insmod utility only takes care of loading the module image to the temporary memory and making a kernel system call which further will ake care of resoving sysmbols,alocating memory, making a kernel entry ,notifying etc.

The kernel function sys_init_module when called along with parameters viz module name and user arguments performs the following operations

sys_init( )

1.check for the permissions (only the su is priviledged to add a kernel module)

2.call the load module ;
mod = load_module(mod_name,args)

3.Add the module or register the module in module_list linked list.

4.Notify threads waiting for the state change of this module

5.call the module’s module_init() function and here is where the functionality of the LKM lies.

6.change the module state to LIVE , which is visible when a cat /proc/modules is done

7.return to the caller.

Now let is have a peek into the load_module() function which is defined in /kernel/linux/module.c the load module performs the following functions.

load_module( )

1.The load module first allocates a temporary kernel memory to the .ko file present in the user spce

2.It then performs a sanity check such as bad object file, incorrect header content etc

3.Next,parsing of the ELF header to map it to certain LKM variables.The ELF is the format of any Linux Kernel module aka Extending Linkable Format.

4.Read the user arguments sent through the function call.

5.Allocate Permanent Memory to the LKM

6.Move the contents from the temporary memory to allocated permanent memory.

7.Resove the symbolic references by referiing to the kernel symbol table and perform relocations

8.Clean up the temporary memory allocated

9.Return reference address for the newly loaded module.

Thus we just went through the steps followed by a insmod to add a module dynamically.

In my next post i shall talk about modprobe utility.

Linux Kernel Modules Part 1

Linux Kernel Modules Part 1

In this post i shall talk about Linux Kernel modules (LKM)

A Linux Kernel module is a piece of code which can be dynamically added to the kernel.Thus a recompile and a reboot of a kernel can be avoided.

LKM can be thought of as a run time addition of functionality to the main kernel.LKM makes saves in terms of memory and boot time, especially in context of embedded systems where only necessary modules can be loaded i.e. minimalistic kernel and future requirements can be fulfilled with the addition of LKM.

Technically speaking LKM are object codes which are added to the kernel image dynamically. In Linux Kernel 2.6 the LKM are of extension .ko and in 2.4 they are of .o extension.

All the modules in Linux OS , regardless of the distro can be viewed by using $cat /proc/modules or also  lsmod command which internally does a cat/proc/modules

Linux provides a set of utilities known as modutils to add r remove LKM, the basic utils are

insmod : insmod adds a module to the kernel

rmmod : rmmod removes a module from the kernel

depmod : checks for dependencies among modules and updates the modules.dep file in /lib/modules/version/modules.dep

modprobe : modprobe is an intelligent utility which combines the functionality of insmod and depmod.

These modules are present as exectuable files in the /sbin folder of LINUX eg./sbin/insmod and   /sbin/rmmod

Though these modules are available as tools for LKM, a LKM has to be written as a Kernel Program.

Let us write a basic Kernel module mymodule.c

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

MODULE_LICENSE(” GPL “);
MODULE_AUTHOR(“Abhijit”);
MODULE_DESCRIPTION(“Basic Kernel Module”);

static int__init mymoduleentry(void)
{
printk(“This is the Kernel Speaking \n”);
return 0;
}

static void__exit mymoduleexit(void)
{
printk(“This is the Kernel saying Good Bye !\n”);
return;
}
module_init(mymoduleentry);
module_exit(mymoduleexit);

Every Linux Kernel Module has to compulsorily have 2 functions

1.init_module() : The init_module() is called when the module is insmod into the kernel,ie adding functionality to the kernel or replacing
2.exit_module() : The exit module is called before rmmod operation,as a cleanup operation
before exiting, this function undoes the changes made by init module.

The printk function is print kernel function which logs Kernel messages into /var./log/messages

Lets us go ahead with compiling the mymodule.c source code and observe the changes in the kernel data.

For kernel oriented programs it is always preferred to write a Makefile for compiling and other cleanup
operations.A make file for our program is shown below.

obj-m += mymodule.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

$make

This command compiles the kernel module program and creates .ko file.
The .ko fle is now ready for dynamic addition to the kernel.

linux@bourne$insmod ./mymodule.o

Add the module to the kernel and becomes an active kernel component.
So our first kernel module is ready and active.

linux@bourne$cat /proc/modules | head -1

displayes our kernel module just added,the field Live indicates the kernel module is active
as shown below.

linux@bourne$mymodule 17792 0 – Live 0xf8d27000

On verifying the kernel log files i.e. /var/log/messages we find the
statement in init function has been logged

linux@bourne$cat /var/log/messages | tail -1
Sep 19 19:06:56 linux-v3p1 kernel: This is the Kernel speaking !

Addition of the same module again displays the following error.

linux@bourne$insmod mymodule.ko
insmod: error inserting ‘mymodule.ko’: -1 File exists

Removing a Module

linux@bourne$rmmod mymodule

removes the module from the memory , i.e the exit operation is called as a clean up operation
and the module is removed from the memory.

On verifying the kernel log files i.e. /var/log/messages we find the statement in exit function has been logged

linux@bourne$cat /var/log/messages | tail -1
Sep 19 19:26:57 linux-v3p1 kernel: Unloading my module.

On checking the list of modules present in the Linux kernel we find that the entry of our model has been deleted.

linux@bourne$cat /proc/modules | head -1
ip6t_LOG 23428 7 – Live 0xf8cf6000

Finally to conclude, in this post we learned concepts of a LKM, basic utils for a LKM ,verifying LKM addition and deletions and viewing inherently present LKM’s.Next blog we will go a bit deep understanding the internals of modprobe ,insmod,rmmod and printk function.

FOSS.IN

FOSS.IN

I had been to FOSS.in last December and it was really a wonderful experience,not to forget the goodies which were part of the show.So let me provide some light on FOSS and its nitty gritties.

 FOSS.IN is the one of the worlds biggest Open Source Events. To be precise FOSS is an acronym for Forum for Open Source Software.

It’s an event held in India where developers, analysts, hackers, from all over the globe meet ,exchange ideas and contribute towards building robust and user friendly Open Source Software.

FOSS.IN had its humble beginnings in year 2001 under the name Linux Bangalore and ever since then there has been no turning back.

The last FOSS.IN event was held in Bangalore at Indian Institute of Science from Dec 4th – 8th, 2007 and was known as FOSS.IN2007.

FOSS.IN 2007 was divided into Project Days and Main Conference.

Project Days consisted Debian-Ubuntu , Fedora, Gnome, IndLinux , KDE, Mozilla OpenOffice.org , OpenSolaris .

Open Solaris had put up a very good stall and had loads of information about their new products ,and contribution to Open Solaris initiatives .SUN has dedicated nearly half a dozen of its major products to Open Source. For more information on how you can contribute to OpenSolaris Initiatives visit www.openSolaris.org .

Debian-Ubuntu , Fedora, Gnome, IndLinux , KDE, Mozilla teams provided information on their upcoming releases, localization issues, Bug Tracking and logging issues.

Main conference had very good talks on Open Source projects, Linux internals, Networking, Real Time OS etc by speakers from Yahoo, Mozilla, KDE, IndLinux, ABB, SUN ,RED HAT ,IBM etc

Other events such as BOF, Lightning Talks, and Workshops were a great hit.

The event was a grand success with all the talks running full and the stalls evincing great interest from the crowd. Lot of goodies such as caps, stickers, T Shirts, bags, pens were given away to participants in events and to the general crowd, which added more zest to the event.

One of the main attractions of the event was Hack Center, which was conducted by Sun Microsystems which involved hacking and fixing Solaris 10 Kernel code.

FOSS.IN is must participation for any LINUX or Open Source enthusiast. If it is your first time, its an eye opener, if not its always an enriching experience.

Think Freedom, Think Open Source !

Some of the useful Links where you can contribute and learn more about Open Source are.

Why do we need 2′s complement ?

Why do we need 2′s complement ?

As we all know 1′s complement is enough to represent negative numbers then why we need 2s complement ?.Well lets find out.

The problems of multiple representations of 0 and the need for the end-around carry are circumvented by a system called two’s complement.

The ones’ complement form of a negative binary number is the bitwise NOT applied to it — the complement of its positive counterpart. Like sign-and-magnitude representation, ones’ complement has two representations of 0: 00000000 (+0) and 11111111 (-0).

To add two numbers represented in this system, one does a conventional binary addition, but it is then necessary to add any resulting carry back into the resulting sum. To see why this is necessary, consider the following example showing the case of the addition of -1 (11111110) to +2 (00000010).The result isnt represented properly rather a incorrect result.

However ,the IPv4 header checksum uses ones’ complement arithmetic, here even on two’s complement machines the inconvenience of having to add back a carry is a desirable error-checking property, because “it is equally sensitive to errors in all bit positions”

I will provide more information on this shortly.Keep reading !