The concept and code composition of writing driver module under linux

1. Concept (why modules are needed)

1.1 After we get a kernel image, when we want to add functions, there are two ways:

        (1) One way is to add modules in the configuration options and recompile the kernel, which will be very troublesome .

        (2) Find a kernel with all functions added by default configuration, so the kernel is too large. 

1.2 Is there a mechanism so that the compiled kernel itself does not need to contain all functions, and when these functions need to be used, the corresponding codes are dynamically loaded into the kernel?
        Answer: Linux provides such a mechanism, which is called a module (Module). The module has the following characteristics:
                (1) The module itself is not compiled into the kernel image, which controls the size of the kernel.
                (2) Once the module is loaded, it is exactly like the rest of the kernel.

2. Module code composition

2.1   Module loading function (required)

        2.1.1 When the kernel module is loaded through the insmod or modprobe command, the loading function of the module will be automatically executed by the kernel to complete the relevant initialization of this module.
           (1) The modprobe command is more powerful than the insmod command. When loading a module, it will also load other modules that the module depends on. If a module loaded using the modprobe command is unloaded with "modprobe -r filename", its dependent modules will be unloaded at the same time.
            (2) Use the modinfo <module name> command to obtain module information, including the module author, module description, parameters supported by the module, and vermagic:
[root@localhost driver_study]# modinfo hello.ko
filename: full path/hello.ko
license:        Dual BSD/GPL
author: author
description: description
alias:          a simplest module
vermagic: 2.6.15.5 686 gcc-3.2 version number kernel version and module version must be the same to install
depends: dependency (whether this module depends on other modules, specified in kconfig)   
         
            (3) The Linux kernel module loading function is generally declared as an internal link with the static keyword, __init is essentially a macro definition, and there is #define __init xxxx in the kernel source code. The function of this __init is to put the function modified by him into the .init.text segment (original by default, the function is put into the .text segment). All such functions in the entire kernel will be linked by the linker into the .init.text section, so the __init modified functions of all kernel modules are actually unified together. When the kernel starts, these module installation functions in the .init.text segment will be loaded uniformly, and this segment will be released after loading to save memory. In the driver, __init appears in the kernel, but the application code does not.
         Explanation: init segment, static, dynamic way to load modules?
                Code segment: Originally, the function was placed in the .text segment. After using __init, we were placed in the init.text segment, which is defined in the kernel's init.h
                Static: When the menuconfig configuration function, add it, compile it into the kernel, and load it statically
                Dynamic: It is dynamically loaded in the form of modules, such as insmod under the rootfs command line

   (3) The module loading function must be specified in the form of module_init (function name). It returns an integer value, or 0 if the initialization is successful. When initialization fails, an error code should be returned. The kernel's error code is a negative number, defined in , in the form of ENODEV, etc.

        (4) The code is as follows:

                    1. static void __exit foo_exit(void)

                  2. {

                  3. //...

                  4. }

                  5. module_exit(foo_exit);

2.2   Module unload function (required)

(1) When a module is unloaded through the rmmod command, the unloading function of the module will be automatically executed by the kernel to complete the opposite function of the module loading function.

(2) The Linux kernel module unload function is generally declared as an internal link with the static keyword and marked with __exit. Like __init, __exit can also cause the corresponding function to automatically reclaim memory after running. Specifically, you can view the definitions of the two macros __init and __exit in the kernel code.

(3) The module unloading function must be specified in the form of module_exit (function name) and does not return any value.

The sample code is as follows:

1. static void __exit foo_exit(void)

2. {

3. //...

4. }

5. module_exit(foo_exit);


2.3   Module license statement (required)

        (1) The module license (LICENSE) statement describes the permission of the kernel module. If LICENSE is not declared, when the module is loaded, it will receive a warning that the kernel is tainted (kernel tainted), because the kernel is defined in accordance with the LICENSE standard, you write Modules must also be marked with the same LICENSE for the kernel to let you use them . In the Linux 2.6 kernel, acceptable LICENSEs include "GPL", "GPL v2", "GPL and additonal rights", "Dual BSD/GPL", "Dual MPL/GPL" and "Proprietary".

        (2)Linux2.6内核模块最常见的是声明代码如下(3.4内核用的GPL v2):

                 MODULE_LICENSE("Dual BSD/GPL");

2.4  模块vermagic信息

        (1)vermagic:       2.6.15.5 686 gcc-3.2  版本号 内核版本和模块版本必须相同才能安装,比如我们在模块的makefile中指定KERN_DIR = /root/driver/kernel时,这个内核版本与我们的模块默认的内核版本不匹配,安装会出现格式错误。

详细细节请看下面博主:

https://blog.csdn.net/bugouyonggan/article/details/9097201



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325856044&siteId=291194637