I. Kernel module learning
The overall framework of the kernel is very large and contains a lot of components, how to include the required components in the kernel? Select one, all of the components are compiled into the kernel, although the required components can be used, but the core is too large, it is bound to bring efficiency impact; Select Second, the component is compiled into a module, the need to load itself into the kernel, this is what we call the module, when the module is loaded into the kernel mechanism, It not only controls the kernel size, but also the kernel that is loaded and the part that is compiled into the kernel, functional meaning.
3.1, kernel load and unload
compile hello.c as module, Hello.ko,
insmod hello. Ko
can also Hello.ko modules to the kernel,
use modprobe to load modules
modprob E Hello.ko
modprobe are more powerful than install, and the modules that load modules depend on are loaded when the module is loaded modprobe.
Unload module:
rmmod Hello
implement module Hello.ko Uninstall, it is important to note that when uninstalling, write directly The name is all right, Ko doesn't need it,
You can also use Modprobe to unload the module:
modprobe-r Hello
Modprobe unload, the other modules that the Hello module depends on are also unloaded. View of the
Module:
lsmod
You can view the modules loaded by the system, in fact lsmod by reading/proc/modul Es file.
Get information about the module:
Modinfo Hello (module name)
You can obtain the module information, including the author, description and other relate parameters.
3.2. Module Module Program Structure
The components of the Linux kernel module are relatively simple and consist of a few parts:
(1) Module loading function
(2) Module unload function
(3) Module License Statement
(4) Module author and other information
3.2.1, module load function
The load function of the module is usually declared by the __INIT flag, and the general module loading function is:
Static int __init func_for_init (void) { XXXX return0;} Module_init (func_for_init);
By module_init the function, this function is the load function of the module, it can be understood as the entrance of the module, the implementation is to do some initialization work. __init is the function that tells the kernel that this function is a special function that implements the initialization of the module.
3.2.2, module unload function
The download function of the module is declared by means of the __exit identity, which is typically the following code:
Static void __exit Func_for_exit () { xxxx}module_exit (func_for_exit);
The Module_exit function specifies the Unload function of the module, and the Unload function of the module implements the exact opposite function of the module loading function.
Using __exit to modify the module unload function, is to tell the kernel, this module is compiled into the kernel, the module unload function Func_for_exit will not be compiled into the kernel, because it will not be uninstalled, why to compile it.
Declaration of 3.2.3, module
The declaration of the module can be used, Module_author, module_description, module_version, etc.
3.3, the compilation of the module
Compiling the code into a module requires writing a simple MakeFile:
// Single File hello.c // Multi-file file1.c, file2.c =/xxx/xxx/xxx/xxall: -C $ (kdir) Modulesclean: -C $ (kdir) Modules Clean -rf modules.order// when compiling a single file obj-m + = hello.o// when multi-file compilation,obj-m: = MYMODULE.O // module name, you can define the MYMODULE-OBJS: = FILE1.O file2.o
Kdir: Specifies the path to the kernel, because the compiled module requires the kernel's environment to compile,
C: Remember capital, is the jump kernel inside to compile,
Modules: Specifies that the compiled module
Obj-m: Specify compile as Module
Single file, directly to the name of the module, directly compiled can be;
When the multi-file compilation, compile the name of the module, the module name can be defined by itself, but the following line is set to the module name-OBJS: = Individual sub-Files
3, Linux kernel module learning