Compiling process of Linux kernel modules and drivers

Source: Internet
Author: User
Tags define function erro
The compiling process of Linux kernel modules and drivers-general Linux technology-Linux programming and kernel information. The following is a detailed description. The Linux kernel is an integral structure, so it is very difficult to add anything to or delete some features to the kernel. The kernel mechanism is introduced to solve this problem. This allows you to dynamically add or delete modules in the kernel.

The module is not compiled in the kernel, so the kernel size is controlled. However, once the module is inserted into the kernel, it will be the same as other parts of the kernel. In this way, part of the system overhead will be stored. At the same time, if a module encounters a problem, the system may crash.

Module implementation mechanism:

At startup, the void inti_modules () function initializes the module, because there are usually no modules during startup. This function often treats the kernel as a virtual module.

If required by the system, call a series of functions starting with sys to operate the module. For example:

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

The data structure of some modules will be used here, in/usr/scr/Linux/include/Linux/module. h. If you are interested, you can find two ways to add the block: manually add the block, for example, insmod modulename. the other is to dynamically load the module as needed: for example, 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.

Module programming

To write a module, you must have a certain degree of multi-process programming Foundation, Because you become a program is not run by an independent program. In addition, because the module needs to run in kernel mode, it will encounter internal and spatial data exchange problems and the general data replication function cannot complete this process. Therefore, the system has introduced some special functions for kernel space and user space data exchange/

These functions are: void put _ user (type valude, type * u_addr)

Memcpy_tofs ()

If you are interested, you can take a closer look at all the functions and their usage. It should be noted that the version of the module programming river kernel has a great relationship. If the version is not available, the kernel module cannot be compiled, or when this module is running, the untested results may appear. For example, system crash.

After understanding this, you can try to write the kernel module. For each kernel module, there must be two functions: int init_module (). This function is started when the kernel is inserted and some function is registered in the kernel, or use his code to replace the content of some internal and middle functions (it is estimated that these functions are empty ). Therefore, internal and security can be uninstalled.

Int cleanup_module () is called when the kernel module is thanked. It is cleared from the kernel.

Like other programming tutorials, we provide a hello world example.

/* 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 mod @!! \ N ");

Return 1;

}

/* The distory function */

Int cleanup_module ()

{

Printk ("I will shut down myself in kernerl mod/n )";

Retutn 0;

}

This example is complete. We also write a makefile example to make it suitable for our applications with heavy program. Here is the content of the makfile.

# 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 run the make command to get the hello. o module and run

$ Insmod hello. o

Hello world!

I will shut down myself in kernerl mod

$ Lsmod

Hello (unused)

....

$ Remmod

I will shut down myself in kernerl mod


In this way, your module can be inserted and deleted at will.

Most drivers in Linux are written in the form of modules. The source code of these drivers can be modified to the kernel, compiled into the module situation, and dynamically loaded as needed.

A typical driver can be divided into the following parts:

1. register the device

When the system starts or the module is loaded, you must register the device to the corresponding device array and return the device's main driver number. For example, for a fast device, call refister_blkdec () add the device to the array blkdev, obtain the device number, and use the device number to index the array. For a character-driven device, use module_register_chrdev () to obtain the Driver Number of the device, and use this device number for all calls to the device.

2. Define Function

For each driver function, there are some function functions closely related to this device. For the most commonly used Block devices or character devices, such as open () read () write () ioctrol. When the system community uses these calls, it will automatically use specific modules in the driver function to implement specific operations. For a specific device, the function called by the above system is certain.

For example, in a block driver, when the system tries to read the device (that is, when reading () is called), The block_read () function in the driver will be run.

When a new device is opened, the device_open () function of the device driver is called.

3. Uninstall the module

When this device is not used, you can detach it from/proc to cancel the special file of the device, which can be implemented by specific functions.

Next we will list the framework of a character device driver 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 will 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 the module !! \ N ");

Return 1;

}

Return 0;

}
Related Article

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.