Pages

Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Thursday, December 24, 2015

Library Function Vs System Calls

Standard functions can be classified into two major categories:

1. Library function calls
2. System function calls

Library Functions
System Calls
They are part of standard C library and executes in user space. Eg: strcmp(), malloc()
These functions change the execution mode of the program from user mode to kernel mode. Eg: open(), read()
Library functions may or may not make system calls.
They are called by library functions and executed by kernel.
Library functions are portable i.e. an application using library functions will run on every system.
An application relying on system call may not run on every system as system call interface may vary from system to system.
Library functions can be debugged using a debugger.
System calls cannot be debugged as they are executed by kernel.


How does system calls switch modes ?

Traditionally, the mechanism of raising an interrupt of ‘int $0x80′ to kernel was used. After trapping the interrupt, kernel processes it and changes the execution mode from user to kernel mode. Today, the systenter/sysexit instructions are used for switching the execution mode.



Monday, November 9, 2015

insmod: ERROR: could not insert module hello.ko: Invalid parameters

I encountered this problem when I tried passing variables from command line using module_param for the first time.

This problem has a simple solution. The solution is that the assignment operator used to assign the value should not have any spaces before or after the operator.

I am using the following code :

#include <linux/module.h>   /* Needed by all modules */

#include <linux/kernel.h>   /* Needed for KERN_INFO */
#include <linux/moduleparam.h>

static int myvar = 1;
module_param(myvar, int , 0);
MODULE_PARM_DESC(myvar, "An integer");

static int __init init_hello(void)
{
        printk(KERN_INFO "Hello world 1.\n");
        printk(KERN_INFO "myvar is an integer: %d \n", myvar);
        return 0;
}

static void __exit cleanup_hello(void)
{
        printk(KERN_INFO "Goodbye world 1.\n");
}
module_init(init_hello);
module_exit(cleanup_hello);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sandesh Shrestha");

After I compile using make, I used the following command to insert the module:

ubuntu@sandesh:~/cpractice[00:44]$  sudo insmod hello.ko myvar=10

Please note that there are no spaces before and after the assignment operator.

Friday, June 26, 2015

Linux Kernel Module - Creating, Inserting and Removing Kernel Object

Consider the following C program stored in the file simple_module.c


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

int simple_module_init(void){
  printk(KERN_ALERT "Inside the init function");
  return 0;
}

void simple_module_exit(void){
  printk(KERN_ALERT "Inside the exit function");
}

module_init(simple_module_init);
module_exit(simple_module_exit);
The above example is the simplest example of a kernel module. It has an init and an exit function. The init function is called when the module is initialized and the exit function is called when the module is removed.

To run this program we create a Makefile which is as follows:

obj-m := simple_module.o

To compile this function we run the following command,

"make -C /lib/modules/$(uname -r)/build M=$PWD modules", where C gives the directory of the build and M means the module, PWD means the reference files are in the present directory.

When this command is run, it creates a simple_module.ko file, ko = kernel object.

To load this module into the kernel, the command is :

sudo insmod simple_module.ko

We can see if the module is loaded using the following command:

lsmod

To remove the module from the kernel, we use the following command:

sudo rmmod simple_module

The log messages can be seen in /var/log/syslog or /var/log/messages.

Use the following command to see the tail of the file and follow the changes in file

tail -f /var/log/syslog