Thursday, 21 March 2013

Anatomy of Linux loadable kernel modules


for details check out the  link,
http://www.ibm.com/developerworks/linux/library/l-lkm/index.html?ca=dgr-lnxw07LinuxLKM
The Linux kernel is what's known as a monolithic kernel, which means that the majority of the operating system functionality is called the kernel and runs in a privileged mode. This differs from a micro-kernel, which runs only basic functionality as the kernel (inter-process communication [IPC], scheduling, basic input/output [I/O], memory management) and pushes other functionality outside the privileged space (drivers, network stack, file systems). You'd think that Linux is then a very static kernel, but in fact it's quite the opposite. Linux can be dynamically altered at run time through the use of Linux kernel modules (LKMs).
Linux is not the only monolithic kernel that can be dynamically altered (and it wasn't the first). You'll find loadable module support in Berkeley Software Distribution (BSD) variants, Sun Solaris, in older kernels such as OpenVMS, and other popular operating systems such as Microsoft® Windows® and Apple Mac OS X.Dynamically alterable means that you can load new functionality into the kernel, unload functionality from the kernel, and even add new LKMs that use other LKMs. The advantage to LKMs is that you can minimize the memory footprint for a kernel, loading only those elements that are needed (which can be an important feature in embedded systems).
An LKM has some fundamental differences from elements that compile directly into the kernel and also typical programs. A typical program has a main, where an LKM has a module entry and exit function (in version 2.6, you can name these functions anything you wish). The entry function is called when the module is inserted into the kernel, and the exit function called when it's removed. Because the entry and exit functions are user-defined, amodule_init and module_exit macro exist to define which functions these are. An LKM also includes a required and optional set of module macros. These define the license of the module, the module's author, a description of the module, and more. Figure 1 provides view of a very simple LKM.

Figure 1. Source view of a simple LKM 
Source view of a simple LKM 
The version 2.6 Linux kernel provides a new (simpler) method for building LKMs. When built, you can use the typical user tools for managing modules (though the internals have changed): the standard insmod (installing an LKM), rmmod (removing an LKM),modprobe (wrapper for insmod and rmmod), depmod (to create module dependencies), and modinfo (to find the values for module macros). For more information on building LKMs for the version 2.6 kernel, check out Resources.
An LKM is nothing more than a special Executable and Linkable Format (ELF) object file. Typically, object files are linked to resolve their symbols and result in an executable. But because an LKM can't resolve its symbols until it's loaded into the kernel, the LKM remains an ELF object. You can use standard object tools on LKMs (which for version 2.6 have the suffix .ko, for kernel object). For example, if you used the objdump utility on an LKM, you'd find several familiar sections, such as .text (instructions),.data (initialized data), and .bss (Block Started Symbol, or uninitialized data).
You'll also find additional sections in a module to support its dynamic nature. The .init.text section contains the module_initcode, and the .exit.text contains the module_exit code (see Figure 2). The .modinfo section contains the various macro text indicating module license, author, description, and so on.

Figure 2. An example of an LKM with various ELF sections 
Example of an LKM with various ELF sections 
So, with that introduction to the basics of LKMs, let's dig in to see how modules get into the kernel and are managed internally.
The process of module loading begins in user space with insmod (insert module). The insmod command defines the module to load and invokes the init_module user-space system call to begin the loading process. The insmod command for the version 2.6 kernel has become extremely simple (70 lines of code) based on a change to do more work in the kernel. Rather thaninsmod doing any of the symbol resolution that's necessary (working with kerneld), the insmod command simply copies the module binary into the kernel through the init_module function, where the kernel takes care of the rest.
The init_module function works through the system call layer and into the kernel to a kernel function called sys_init_module(see Figure 3). This is the main function for module loading, making use of numerous other functions to do the difficult work. Similarly, the rmmod command results in a system call for delete_module, which eventually finds its way into the kernel with a call to sys_delete_module to remove the module from the kernel.

Figure 3. Primary commands and functions involved in module loading and unloading 
Primary commands and functions involved in module loading and unloading 
During module load and unload, the module subsystem maintains a simple set of state variables to indicate the operation of a module. If the module is being loaded, then the state is MODULE_STATE_COMING. If the module has been loaded and is available, it is MODULE_STATE_LIVE. Otherwise, if the module is being unloaded, then the state is MODULE_STATE_GOING.
Let's now look at the internal functions for module loading (see Figure 4). When the kernel function sys_init_module is called, it begins with a permissions check to see whether the caller can actually perform this operation (through the capable function). Then, the load_module function is called, which takes care of the mechanical work to bring the module into the kernel and perform the necessary plumbing (I review this shortly). The load_module function returns a module reference that refers to the newly loaded module. This module is loaded onto a doubly linked list of all modules in the system, and any threads currently waiting for module state change are notified through the notifier list. Finally, the module's init() function is called, and the module's state is updated to indicate that it is loaded and live.

Figure 4. The internal (simplified) module loading process 
The internal (simplified) module loading process 
The internal details of module loading are ELF module parsing and manipulation. The load_module function (which resides in ./linux/kernel/module.c) begins by allocating a block of temporary memory to hold the entire ELF module. The ELF module is then read from user space into the temporary memory using copy_from_user. As an ELF object, this file has a very specific structure that can be easily parsed and validated.
The next step is to perform a set of sanity checks on the loaded image (is it a valid ELF file? is it defined for the current architecture? and so on). When these sanity checks are passed, the ELF image is parsed and a set of convenience variables are created for each section header to simplify their access later. Because the ELF objects are based at offset 0 (until relocation), the convenience variables include the relative offset into the temporary memory block. During the process of creating the convenience variables, the ELF section headers are also validated to ensure that a valid module is being loaded.
Any optional module arguments are loaded from user space into another allocated block of kernel memory (step 4), and the module state is updated to indicate that it's being loaded (MODULE_STATE_COMING). If per-CPU data is needed (as determined in the section header checks), a per-CPU block is allocated.
In the prior steps, the module sections are loaded into kernel (temporary) memory, and you also know which are persistent and which can be removed. The next step (7) is to allocate the final location for the module in memory and move the necessary sections (indicated in the ELF headers by SHF_ALLOC, or the sections that occupy memory during execution). Another allocation is then performed of the size needed for the required sections of the module. Each section in the temporary ELF block is iterated, and those that need to be around for execution are copied into the new block. This is followed by some additional housekeeping. Symbol resolution also occurs, which can resolve to symbols that are resident in the kernel (compiled into the kernel image) or symbols that are transient (exported from other modules).
The new module is then iterated for each remaining section and relocations performed. This step is architecture dependent and therefore relies on helper functions defined for that architecture (./linux/arch/<arch>/kernel/module.c). Finally, the instruction cache is flushed (because the temporary .text sections were used), a bit more housekeeping is performed (free temporary module memory, setup the sysfs), and the module is finally returned to load_module.
Unloading the module is essentially a mirror of the load process, except that several sanity checks must occur to ensure safe removal of the module. Unloading a module begins in user space with the invocation of the rmmod (remove module) command. Inside the rmmod command, a system call is made to delete_module, which eventually results in a call to sys_delete_moduleinside the kernel (recall from Figure 3). Figure 5 illustrates the basic operation of the module removal process.

Figure 5. The internal (simplified) module unloading process 
The internal (simplified) module unloading process 
When the kernel function sys_delete_module is invoked (with the name of the module to be removed, passed in as the argument), the first step is to ensure that the caller has permissions. Next, a list is checked to see whether any other modules depend on this module. There exists a list called modules_which_use_me that contains an element per dependent module. If this list is empty, no module dependencies exist and the module is a candidate for removal (otherwise, an error is returned). The next test is to see if the module is loaded. Nothing prohibits a user calling rmmod on a module that's currently being installed, so this check ensures that the module is live. After a few more housekeeping checks, the penultimate step is to call the module's exit function (provided within the module itself). Finally, the free_module function is called.
When free_module is called, the module has been found to be safely removable. No dependencies exist now for the module, and the process of cleaning up the kernel can begin for this module. This process begins by removing the module from the various lists that it was placed on during installation (sysfs, module list, and so on). Next, an architecture-specific cleanup routine is invoked (which can be found in ./linux/arch/<arch>/kernel/module.c). You then iterate the modules that depended on you and remove this module from their lists. Finally, with the cleanup complete—from the kernel's perspective—the various memory that was allocated for the module is freed, including the argument memory, per-CPU memory, and the module ELF memory (core and init).
In many applications, the need for dynamic loading of modules is important, but when loaded, it's not necessary for the modules to be unloaded. This allows the kernel to be dynamic at startup (load modules based on the devices that are found) but not dynamic throughout operation. If it's not required to unload a module after it's loaded, you can make several optimizations to reduce the amount of code needed for module management. You can "unset" the kernel configuration optionCONFIG_MODULE_UNLOAD to remove a considerable amount of kernel functionality related to module unloads.

Friday, 8 March 2013

Difference Between Semaphores and Mutex


Difference Between Semaphores and Mutex




  1. A semaphore can be a Mutex but a Mutex can never be semaphore. This simply means that a binary semaphore can be used as Mutex, but a Mutex can never exhibit the functionality of semaphore.
  2. Both semaphores and Mutex (at least the on latest kernel) are non-recursive in nature.
  3. No one owns semaphores, whereas Mutex are owned and the owner is held responsible for them. This is an important distinction from a debugging perspective.
  4. In case the of Mutex, the thread that owns the Mutex is responsible for freeing it. However, in the case of semaphores, this condition is not required. Any other thread can signal to free the semaphore by using the sem_post() function.
  5. A Mutex, by definition, is used to serialize access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A semaphore, by definition, restricts the number of simultaneous users of a shared resource up to a maximum number
  6. Another difference that would matter to developers is that semaphores are system-wide and remain in the form of files on the filesystem, unless otherwise cleaned up. Mutex are process-wide and get cleaned up automatically when a process exits.
  7. The nature of semaphores makes it possible to use them in synchronizing related and unrelated process, as well as between threads. Mutex can be used only in synchronizing between threads and at most between related processes (the pthread implementation of the latest kernel comes with a feature that allows Mutex to be used between related process).
  8. According to the kernel documentation, Mutex are lighter when compared to semaphores. What this means is that a program with semaphore usage has a higher memory footprint when compared to a program having Mutex.
  9. From a usage perspective, Mutex has simpler semantics when compared to semaphores.

check out the link for example. http://www.geeksforgeeks.org/mutex-vs-semaphore/

Sunday, 3 March 2013

Bitwise operations in c



Bitwise operations in c

Function to get no of set bits in binary representation of passed binary no. *
#include<stdio.h>
/* Function to get no of set bits in binary representation of passed binary no. */
int countSetBits(long n)
{
        unsigned int num_zeroes = 0;
        for(size_t i = 0; i < 8 * sizeof n; ++i, n >>= 1)
        {
                 if ((n & 1) == 1)
                         ++num_zeroes;
          }
         return num_zeroes;
}

int countResetBits(long n)
    {
    unsigned int num_zeroes = 0;
    for(size_t i = 0; i < 8 * sizeof n; ++i, n >>= 1)
        {
            if ((n & 1) == 0)
            ++num_zeroes;
    }
    return num_zeroes;
    }

/* Program to test function countSetBits */
int main()
{
    long i = 5;
    printf("%d", countResetBits(i));
    printf("\n%d", countSetBits(i));
    getchar();
}


//Decimal to Binary using Bitwise AND operator

void binary(unsigned int num)
{
unsigned int mask=32768;   //mask = [1000 0000 0000 0000]
printf("Binary Equivalent : ");

while(mask > 0)
   {
   if((num & mask) == 0 )
         printf("0");
   else
         printf("1");
  mask = mask >> 1 ;  // Right Shift
   }
}
Output :
Enter Decimal Number : 10
Binary Eqivalent : 0000000000001010


Write a c program to multiply given number by 4

#include <stdio.h>
void main()
{
long number, tempnum;
printf("Enter an integer\n");
scanf("%ld",&number);
tempnum = number;
number = number << 2;   /*left shift by two bits*/
printf("%ld x 4 = %ld\n", tempnum,number);
printf("Enter an integer\n");
scanf("%ld",&number);
tempnum = number;
number = number << 2;   /*left shift by two bits*/
printf("%ld x 4 = %ld\n", tempnum,number);
}

/*------------------------------
Output
Enter an integer
15
15 x 4 = 60

RUN2
Enter an integer
262
262 x 4 = 1048
---------------------------------*/

C program to check odd or even using bitwise operator
#include<stdio.h>
main()
{
   int n;
   printf("Enter an integer\n");
   scanf("%d",&n);

   if ( n & 1 == 1 )
      printf("Odd\n");
   else
      printf("Even\n");
    return 0;
}

Write a ‘C’ program to swap two numbers using bitwise operator

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\n enter the elements\n");
scanf("%d%d",&x,&y);
printf("\n before swaping x=%d,y=%d",x,y);
x=x^y;
y=y^x;
x=x^y;
printf("\n after swaping x=%d y=%d",x,y);
getch();
}

Multiplication of two numbers using BITWISE operators
//How will you multiply two numbers using BITWISE operators
#include<stdio.h>
main()
{
   int a,b,result;
   printf("nEnter the numbers to be multiplied :");
   scanf("%d%d",&a,&b);
   result=0;
   while(b != 0)               // Iterate the loop till b==0
   {
      if (b&01)               // Logical ANDing of the value of b with 01
      result=result+a; // Update the result with the new value of a.
      a<<=1;              // Left shifting the value contained in 'a' by 1.
      b>>=1;             // Right shifting the value contained in 'b' by 1.
   }
   printf("nResult:%d",result);
}

Bitwise operation for division
int main(void) {
int L, leftShift, i, changeMask;

leftShift = 0; /* added or subtracted value will be 2^leftShift */
i = 25; /* value we are adding to or subtracting from */
changeMask = 1 << leftShift;

for (L = leftShift; L < INT_BIT; L++) {
i ^= changeMask;
if ( /* ! */ (i & changeMask)) { /* comment in or out "!" for
addition or subtraction */
break;
}
changeMask <<= 1;
}

printf("%i", i);

return 0;
}

Program in C to manipulate bits,bitmanip.c
//========================
//Program in C to manipulate bits,bitmanip.c
//========================
#include<stdio.h>
int main()
{
int n,b;
printf("\n Enter a number and bit to manipulate: \n");
scanf("%d%d",&n,&b);
printf("You entered %d and %d",n,b);
//Code to Set bth bit in number n
n=n|(1<<b);
//to toggle //n=n^(1<<b)
//to display bth bit// n&(1<<b)
//to clear bth bit // n& ~(1<<b)
printf("\n Entered Number after setting %d th bit is %d \n :",b,n);
return 0;
}

Program in C to set nth bit, bitset.c
//========================
//Program in C to set nth bit, bitset.c
//========================
#include<stdio.h>
int showbits(int nn)
{
unsigned int m;
m=1<<(sizeof(nn)*8-1);
        while(m > 0)
        {
                if(nn & m)
                {
                printf("1");
                }
                else
                {
                printf("0");
                }
        m>>=1;
        }
}
int main()
{
int number,bits;
printf("Enter a nomber and bits to set:\n");
scanf("%d%d",&number,&bits);
printf("\n You Entered: %d and %d  \n",number,bits);
showbits(number);
number=number^(1<<(bits-1));
printf("\n Number now is: %d \n",number);
showbits(number);
printf("\n");
return 0;
}

Program in C to check bit at nth position, bitcheckatn.c
//========================
//Program in C to check bit at nth position, bitcheckatn.c
//========================
#include<stdio.h>
int showbits(int nn)
{
unsigned int m;
m=1<<(sizeof(nn)*8-1);
        while(m > 0)
        {
                if(nn & m)
                {
                printf("1");
                }
                else
                {
                printf("0");
                }
        m>>=1;
        }
}
int main()
{
int number,bits;
printf("Enter a nomber and bits to see qt bit th position:\n");
scanf("%d%d",&number,&bits);
printf("\n You Entered: %d and %d  \n",number,bits);
showbits(number);
number=number&(1<<(bits));
printf("\n Now number is :%d and \n bit at %d  position is: \n",number,bits);
showbits(number);
printf("\n");
return 0;
}
How to Swap two numbers using bitwise operator?
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\n enter the elements\n");
scanf("%d%d",&x,&y);
printf("\n before swaping x=%d,y=%d",x,y);
x=x^y;
y=y^x;
x=x^y;
printf("\n after swaping x=%d y=%d",x,y);
getch();
}
Entered Character is digit or uppercase alphabet or lowercase alphabet
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("enter the character\n");
scanf("%c",&ch);
if(ch>='0'&& ch<='9')
printf("\nIt is adigit");
if(ch>='A'&&ch<='Z')
printf("\n It is alphabet");
if(ch>='a'&&ch<='z')
printf("\nit is alphabet");
getch();
}

Sum of digit of a number using recursion function
#include<stdio.h>
#include<conio.h>
void main()
{
int sumdig(int);
int n,sum,d,f,rev=0;
clrscr();
printf("enter the number");
scanf("%d",&n);
sum=sumdig(n);
printf("%d",sum);
getch();
}

int sumdig(int n)
{
int sum,d,r=0;
if(n==0)
return(0);
else
sum=n%10+sumdig(n/10);
while(sum!=0)
{
d=sum%10;
r=r+d;
sum=sum/10;
}
return(r);
}

Write a ‘C’ program to convert given decimal number into binary number
#include
#include
void main()
{
int n,j,a[50],i=0;
clrscr();
printf("\n enter the value :-");
scanf("%d",&n);
while(n!=0)
{
a[i]=n%2;
i++;
n=n/2;
}
printf("\n binary conversion\n");
for(j=i-1;j>=0;j--)
printf("%d",a[j]);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int n,tmp,d,rev=0;
clrscr();
printf("enter the number=");
scanf("%d",&n);
tmp=n;
while(n!=0)
{
d=n%10;
rev=(rev*10)+d;
n=n/10;
}
if(tmp==rev)
printf("enter number is palindrom");
else
printf("enter number is not palindrom");
getch();
}