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) 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.kofilename: full path/hello.kolicense: Dual BSD/GPLauthor: authordescription: descriptionalias: a simplest modulevermagic: 2.6.15.5 686 gcc-3.2 version number kernel version and module version must be the same to installdepends: dependency (whether this module depends on other modules, specified in kconfig)
(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信息
详细细节请看下面博主: