Linux kernel modules and drivers for authoring _unix Linux

Source: Internet
Author: User
Tags erro

The Linux kernel is a whole structure, so it is difficult to add anything to the kernel, or to remove certain features. In order to solve this problem, the kernel mechanism is introduced. This allows you to dynamically add or remove modules from the kernel.


The module is not compiled into the kernel and thus controls the size of the kernel. However, once the module is inserted into the kernel, it is the same as the rest of the kernel. This would have been a part of the system overhead. Also, if there is a problem with the module, it may cause a system crash.

The implementation mechanism of the module:

At startup, the module is initialized by the function void Inti_modules (), because the startup thing often doesn't have a module. This function tends to treat the kernel itself as a virtual module.

If required by the system, a series of functions that begin with SYS are invoked to manipulate the module. Such as:

Sys_creat_modules (), Sys_inti_modules (),
Sys_deldte_modules () and so on.

Here will use some of the data structure of the module, in the/usr/scr/linux/include/linux/module.h, interested friends can find a look at the addition of a block there are two ways: first, manually joined: such as: Insmod ModuleName. The other is to load the module dynamically as needed: If you execute the command:

$mount-T msdos/dev/hdd/mnt/d, the system automatically loads the Fat module to support the Msdos file system.

1. Module programming

To write a module, you have to have a certain number of process programming basics, because you become a program that is not run as a stand-alone program. In addition, because the module needs to run in kernel mode, it encounters problems with space and user-space data exchange. General data replication functions cannot complete this process. So the system has entered some special functions to complete the exchange of kernel space and user spatial data.

These functions are: void put _user (type Valude,type *u_addr)

Memcpy_tofs ()

And so on, interested friends can take a closer look at all the functions and their usage. What needs to be explained is that. Module Programming River Kernel version has a great relationship. If the version is not working, the kernel module cannot be compiled, or. When this module is run, an unpredictable result occurs. Such as: System crashes and so on.

After you understand this, you can try to write the kernel module. For each kernel module, there must be two functions int init_module () This function starts when inserting the kernel, registers a certain function in the kernel, or replaces the contents of some functions within and with his code (estimates that these functions are empty). Therefore, the inside and can be safely unloaded.

int Cleanup_module () called when the kernel module is Chezay. Clears the module from the kernel.

As with other programming tutorials, we give an example of Hello World

/*HELLO.C a module programm*/

/* The program runing under Kernel mod and it is a module*/

#include "linux/kernerl.h"

#include "llinux/module.h"

* * Pross the config_modversions*/

#if config_modversions==1

#define Modversions

#include "Linux/modversions.h"

#end If

/* The init function*/

int Init_module ()

{

PRINTK ("Hello World!\n");

Printd ("I have runing in a Kerner");

return 1;

}

/* The Distory function*/

int Cleanup_module ()

{

PRINTK ("I'll shut down myself in Kernerl mod/n)";

RETUTN 0;

}

Such an example is done. We also write a makefile example to fit our application in large program heavy applications. This is the content of the Makfile file.

# A Makefile for a module

cc=gcc

modcflags:=-wall _dmodule-d_kernel_-dlinux

HELLO.O Hello.c/usr/inculde? Linux/version.h

CC $ (modcflags) 0c hello.c

echo the module is Complie completely

Then you run make command to get hello.o this module and run

$insmod hello.o

Hello world!

I'll shut down myself in Kernerl MoD

$lsmod

Hello (unused)

....

$remmod

I'll shut down myself in Kernerl MoD

This allows your module to be inserted and deleted at will.

Most of the drivers in Linux are written in the form of modules that can be modified into the kernel, and can be compiled into modular situations, dynamically loaded when needed.

A typical driver can be divided into so many parts:

1. Registering devices

At the start of the system, or when the module is loaded, the device must be registered to the appropriate device array and returned to the device's main drive number, for example: to the quick device, call Refister_blkdec () to add the device to the array blkdev, and obtain the device number. and use these device numbers to index this array. For character-driven devices, use Module_register_chrdev () to get the driver number of the device, and then all calls to the device are implemented with this device number.

2. Defining functional functions

For each driver function, there are functional functions that are closely related to the device, and the most commonly used block or character devices have operations such as the Open () read () write () Ioctrol (). When these calls are used by the system society, the specific modules in the driver function are automatically used to implement the specific operation. For a particular device, the corresponding function on the above system call is certain.

For example: in a block-driven device. When the system attempts to read the device (that is, call Read ()), it runs the Block_read () function in the driver.

The Device_open () function of this device driver is invoked when a new device is opened.

3. Chezay Module


When you do not use this device, you can uninstall it, mainly from/proc to cancel the device's special files, can be implemented with specific functions.

Below we enumerate a character device driver framework. To illustrate this process.

/* A module of a character device * *

/* Some include files*/

#include "param.h"

#include "User.h"

#include "Tty.h"

#include "Dir.h"

#include "Fs.h"

/* The Include files modules need*/

#include "linux/kernel.h"

#include "linux/module.h"

#if config_modbersions==1

Degine modbersions

#include "Linux.modversions.h"

#endif

#difine devicename Mydevice

/* The init funcion*/

int Init_module ()

{

int Tag=module_register_chrdev (0,mydevice,&fops);

if (tag<0)

{

PRINTK ("The device init is erro!\n");

return 1;

}

return 0;

}

/*the funcion which the device would be used * *

int Device_open ()

{

.......

}

int Device_read ()

{

.......

}

int Device_write ()

{

.......

}

int Device_ioctl ()

{

.......

}

......

/* The Deltter function of this module*/

int Cleanup_module ()

{

int Re=module_unregister_chrdev (tag,mydevice);

if (re<0)

{

PRINTK ("Erro unregister" Module!!) \ n ");

return 1;

}

return 0;

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.