Friday 17 February 2017

Linux Kernel Module



For  LDD Project Click on this Link
https://github.com/0xsachin/LDD/


Loadable Kernel Module using C

- kernel module are considered as object files which contains code which Extends the feature of running kernel.
- kernel module are generally used to Support new hardware , to support new file system to add new system call.
- Advantages of kernel module is that It gets loaded memory of kernel whenever required and it gets removed from memory when its used gets completed.
- To load and Unload kernel modules their is no need to reboot our kernel.
- The Concept of kernel module is used in almost every Linux Distro. Including MAC OS.
- To write the kernel module we have to use Basic C programming concept and some commands.
- Types of kernel Module-  1. Device Driver
                                               2. File system Driver
                                               3. Network Driver
                                               4. TTY (teli Type Terminals)

- Advantages of kernel Module-

1. Easy to Insert and Remove from memory.
2. We can Insert it without rebooting the kernel.
3 We can easily Remove it from memory when its used gets completed.

- Disadvantages of kernel Module-

1. Due to loading and unloading of kernel module their is fragmentation in kernel memory.


- Technology Details about Kernel module-  

1. The source code of kernel module is written in file having extension  .c
2. Kernel module gets compiled by writing the command into make file.
3. After successful compilation of kernel module its kernel object file (.ko) is created.
4. After creating .ko file we can insert it into the running kernel.

- Example of Kernel Module-  

//  Module1.c

#include<linux/module.h>     // This header file is required by all kernel modules
#include<linux/kernel.h>      // This header file is required for KERN_INFO

// This function gets called automattically when module gets loaded by insmod

int init_module( void )
{
       printk(KERN_INFO "module loaded" );
       return 0;
}


// This function gets called automatically when module gets removed from memory by rmmod

void cleanup_module( void )
{
       printk(KERN_INFO "Module removed" );
}

// All the information which is printed by printk function is available in /var/log/syslog file.

- After writing the above code kernel module we have to write the make file which is used to compile   the kernel module program.
- Make file is consider as file which contains the command which are executed by make Utility.

// Make File

obj-m +=module1.o
all:
       make -C /lib/modules/$(shell uname -r)/build M=$(PWD)modules


- The above file should be created in a same directory where the program of kernel module is placed.
- Above make file contains one command which is used to compile the kernel module.
- Above command travels the path from linux file system and execute the make file which is written     by kernel.
- After Writing Make file we have to execute make file.
   open terminal and go to directory where make file and module is placed.

- if make file successfully executed then module1.ko file gets created
- module1.ko is an considers as kernel object which gets inserted into running kernel.

- Insert the kernel module into kernel-

For insertion insmod command is used.

     $sudo insmod module1.ko

after this command we are enter in password of super user.

- if we want check whether loaded kernel module in to the kernel we have to use lsmod command.
                   $lsmod
- if our kernel module is inserted successfully inside that list we get name as module1.
- Inside the kernel module we write the printk( ) which is responsible to write the data into kernel        log file 
- kernel log file is present at path    "/var/log/syslog".
- we can open system log file using below command
       $ cat /var/log/syslog

-if we want to remove kernel module then we have to use the command rmmod.
       $ sudo rmmod module1

- to check wheather our module is removed from kernel or not we have to again call the command       lsmod

-modinfo command is used to display the information about the module.
        $ modinfo -0 module1.ko

-Description about the kernel module-

1. For the kernel module we required 2 major files who's contents are used inside kernel module as
     a. module.h          b. kernel.h

2. Every kernel module should contains 2 predefined functions which gets called implicitly automatically when module gets loaded into memory and modules gets removed from memory.

3. init_module( ) is a function which gets called when we call insmod command invoked.
    generally this function should contains the code which is required to allocate resources of kernel  module.

4.cleanup_module( ) is a function which gets called when rmmod command gets invoked.

5. inside the above programe their is a printk( ) function which is required to write the data into kernel log file.



/////////////////////////////////   More Examples of Kernel Module ///////////////////////////////////////////

module 2:   rename the init_module( ) and  cleanup_moduke( )

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>    // This header file is required macroes

static int __init fun(void)    //here fun is a entry point function which is called by insmod
{
printk(KERN_INFO " Inserting module\n");
return 0;
}

// This function gets called automatically when module gets removed from memory by rmmod

static void __exit gun(void)   //here gun( ) which gets called when rmmod command invoked
{
printk(KERN_INFO "Removing module 2\n");
}

module_init(fun);    // Register our init function
module_exit(gun);   // Register our cleanup function

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module 3:   Register  driver Author ,description,  Licence

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
// Macro for name of author
#define DRIVER_AUTHOR "Sachin_Gaikwad"

// Macro for module description
#define DRIVER_DESC   "my device driver demo"

static int __init fun(void)   //Entry function
{
printk(KERN_INFO " loading module\n");
return 0;
}

static void __exit gun(void)    //Exit function
{
printk(KERN_INFO "Removing module\n");
}

module_init(fun);
module_exit(gun);

MODULE_LICENSE("GPL");                            // Register name of licance
MODULE_AUTHOR(DRIVER_AUTHOR);      // Register name of author
MODULE_DESCRIPTION(DRIVER_DESC);   // Provide module description

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

module 4: Accept parameter in kernel module

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/moduleparam.h>  // This header file is used to get information about the parameters
#include <linux/stat.h>


#define DRIVER_AUTHOR "Sachin_Gaikwad"
#define DRIVER_DESC   " Demo module with arguments"

int myint = 21;

module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(myint, "Integer variable");      // parameter description  

/*
This macro is used to to register information about the input argument
First parameter is name of variable
Second parameter is its type
Third parameter is its permission
*/

static int __init fun(void)
{
printk(KERN_INFO "Integer Value is:  %d\n", myint);
return 0;
}

static void __exit gun(void)
{
printk(KERN_INFO "Removing module\n");
}

module_init(fun);
module_exit(gun);

MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);

// we can insert our module as sudo insmod module.ko myint=10

//////////////////////////////////// Array parameter ///////////////////////////////////////////

module 5: Accept Array parameter in kernel module

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/moduleparam.h>  // This header file is used to get information about the parameters



#define DRIVER_AUTHOR "Sachin_Gaikwad"
#define DRIVER_DESC   " Demo module with Array arguments"

int myint[3];

module_param_array(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(myint, "Integer array variable");      // parameter description  

static int __init fun(void)
{
printk(KERN_INFO "Integer array Value is:  %d\t%d\t%d\t", myint[0],myint[1],myint[2]);

return 0;
}

static void __exit gun(void)
{
printk(KERN_INFO "Removing module\n");
}

module_init(fun);
module_exit(gun);

MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);

// we can insert our module as sudo insmod module.ko myint=10,20,30
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Here The Small Project of Linux Kernel Character Device Driver
Just Click on Link

https://github.com/0xsachin/LDD/


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////