Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Wednesday, January 29, 2014

Printing the Linux kernel version in your kernel module

The kernel version that a kernel module is built with can differ from the kernel version it is running on. The version of the kernel that the module is running on is shown in the first log and the version of the kernel is compiled with is shown in the second log in the version_init function:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/utsname.h>
#include <generated/utsrelease.h>

static int __init version_init(void)
{
        int i = 0;

        pr_alert("Kernel module running of version %s\n", utsname()->release);
        pr_alert("Kernel module compiled with version %s\n", UTS_RELEASE);
        return 0;
}

static void __exit version_exit(void)
{
        pr_alert("Over and out!\n");
}

module_init(version_init);
module_exit(version_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("zeuzoix");
MODULE_DESCRIPTION("Kernel Module Version Example Module");

Sunday, February 3, 2013

Kernel Module Makefiles

You've got your kernel module code ready and you want to build it. The following is a simple kernel module makefile which will help you do this:


1
2
3
4
5
6
7
obj-m += hello-1.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

Note: The above makefile will fail if used directly please replace the spaces at lines 4 and 7 with TABS or please download the following from here makefile1.

In this explanation there are two makefiles which I will be referring to. The first is the one shown above and the second is the main kernel makefile which is located in the kernel source directory.

When you compile the kernel the make process assembles a list of kernel drirvers and modules that are to be compiled as loadable kernel modules defined by obj-m.

In our example above we indicate that we want to create a kernel module out of the hello-1.o object file which is eventually built from our hello-1.c source code.

There are two targets for the makefile i.e. all and clean. They invoke the 'modules' and 'clean' targets of the main kernel makefile

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
  
Let's understand the option switches.

The -C option switches the make command to the kernel source directory. Basically this is the directory of the source code used to build the kernel image with the main kernel makefile. Here $(shell uname -r) is a convenient way of specifying the name of the live kernel being run when the make command is executed. 


The M option tells the main kernel makefile to go to the present working directory and read the makefile present there. This is a way of telling the main kernel makefile to read the the makefile shown above. This enables the main kernel makefile to add the hello-1.o object to its list of modules to be built.

Here $(PWD) is a convenient way of specifying the present working directory.

More information at :http://www.tldp.org/LDP/lkmpg/2.6/html/x181.html.

Tuesday, January 22, 2013

Linux Kernel Modules

A unique feature of the Linux Kernel is its ability to allow injection of code while it is running. This piece of code is called a kernel module

The traditional approach employed by operating system designers was to add the code to the source code of the OS and build the OS as a single binary that can be booted by the boot-loader.

The kernel module can be inserted and removed at any time during the execution of the kernel giving Linux Kernel developers the flexibility to test their modules before making them a part of the mainline Kernel source code.

The process of building a stand-alone kernel module or integrating the kernel module with the source code of the Linux Kernel is simplified through the makefile of the kernel module. The source code of the kernel module remains the same irrespective of how it is built

Information on kernel module programming can be obtained from:
http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html

Saturday, January 19, 2013

An operating system called Linux

An operating system is a software program which provides 5 management functions:
  1. CPU
  2. Memory
  3. Networking
  4. Storage
  5. Device Input/Output

Linux if one were to draw a block diagram can be represented as:





In this diagram the core operating system is the Linux Kernel. The management functions which map to the generic definition of an operating system are:

  1. Process management
  2. Memory management
  3. Network
  4. File Systems
  5. Device Drivers

Applications viz. Firefox, vim, LibreOffice, Bash, etc .run in the User memory space and are not part of the Linux Kernel memory space. An application interfaces with the Linux Kernel through the system call interface.