Detailed compilation of Linux kernel modules and drivers

Source: Internet
Author: User
Linux Kernel Modules and drivers-Linux general technology-Linux programming and kernel information. For more information, see the following. The Linux kernel is structured as a whole. Therefore, add anything to the kernel. Or it is very difficult to delete some features. To solve this problem.

Introduced the kernel mechanism. This allows you to dynamically add or delete modules in the kernel. The module is not compiled in the kernel, thus controlling the kernel size. However, once a module is inserted into the kernel, it is the same as other parts of the kernel. In this way, some system overhead will be stored. In addition, if the module has problems ., The system may crash.

   1. Implementation Mechanism of Module 1:

At startup, void inti_modules () is used to initialize the module ,. This is because many startup tasks do not have modules. 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 is used here, in/usr/scr/linux/include/linux/module. H, interested friends can find out. You can add a block manually, for example, insmod modulename. The other is to dynamically load modules as needed. For example, when you run $ mount-t msdos/dev/hdd/mnt/d. The system automatically loads the FAT module to support the MSDOS file system.

   1. 2-module programming

To write a module, you must have a certain multi-process programming Foundation. Because you become a program not run with an independent program. In addition, because the module needs to run in kernel mode, it may encounter internal and spatial and user space data exchange problems. The general data replication function cannot complete this process. Therefore, some special functions have been incorporated to exchange data between the kernel space and the user space. These functions include void put _ user (type valude, type * u_addr) memcpy_tofs () and so on. If you are interested, you can take a closer look at all the functions and their usage. It must be noted that. The kernel version of the module programming River has a great relationship. If the version is not available, the kernel module cannot be compiled or. When running this module, the untested results are displayed. For example, system crash.

After understanding this. You can 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. (I guess) int cleanup_module () is called when the kernel module is thanked. Clear the module from the kernel.

Like other programming tutorials, we provide a hello world example:
CODE:/* 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;

}

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.