This section describes the Linux kernel module mechanism. Refer to the typical book Linux device drivers.
① Most small-and medium-scale applications execute a single task from start to end, while the module only registers itself in advance to serve a future request, and then its initialization function ends immediately.
The module is only linked to the kernel. Therefore, the function that it can call is only the functions exported by the kernel, and there is no function library that can be linked. Therefore, the source file cannot contain common header files. The kernel module can only use functions that are part of the kernel. Most related header files are stored in the header file Declaration of the kernel source code tree (/home/SML/linux2.6.37.4/), including/Linux and include/ASM directories.
② Helloworld)
1. Create hello. C and makefile on the VM.
2. The source code of Hello. C is as follows:
# Include <Linux/module. h>
# Include <Linux/init. h>
# Include <Linux/sched. h>
# Include <Linux/kernel. h>
Module_license ("GPL ");
Static int hello_init (void)
{
Printk (kern_emerg "Hello world! \ N ");
Return 0;
}
Static void hello_exit (void)
{
Printk (kern_emerg "Crule world! \ N ");
}
Module_init (hello_init );
Module_exit (hello_exit );
3. makefile content
OBJ-M: = Hello. o
4. the kernel source code tree must exist on the Virtual Machine. My kernel is/home/SML/linux2.6.37.4, because the module we construct will eventually run on the Linux kernel 2.6.37.4, therefore, we need to compile the module under the kernel source code tree. Run make-C/home/SML/linux2.6.37.4 M = $ PWD modules.
Root @ SML-virtualbox:/home/SML/nfs_server # Make-C/home/SML/linux-2.6.37.4 M = $ PWD modules
Make: Enter the directory '/home/SML/linux-2.6.37.4'
CC [m]/home/SML/nfs_server/Hello. o
Buildingmodules, stage 2.
Modpost 1 modules
CC/home/SML/nfs_server/Hello. Mod. o
LD [m]/home/SML/nfs_server/Hello. Ko
Make: Leave the directory "/home/SML/linux-2.6.37.4"
Run the command to generate the hello. Ko module.
5. Make-C/home/SML/linux2.6.37.4 M = $ PWD modules parsing.
As mentioned above, the module depends on some symbols exported from the kernel. The main header file is declared under the include/Linux and include/ASM directories. Therefore, the compilation module must rely on the kernel source code tree. -C indicates that make switches to the specified directory. M is a variable in makefile. It is specified as the current directory, that is, the Directory of Hello. C. Modules points to the module set in the obj-m variable, which is the hello module. Because the directory of the kernel source code tree is not specified in the makefile of Hello. C,-C will tell the path of the make kernel source code tree. (This part is not very thorough, but we need to understand that kernel module compilation relies on the kernel source code tree .).
6. Module loading and unloading
Load command: insmod; uninstall command: rmmod; View module command: lsmod; View module information: modinfo;
We put the above compiled modules on the root file system, and then run the load command to see the information we want to print. For details about how to place the file on the root file system of the Development Board, see NFS implementation file sharing between the Development Board and virtual machines. For how to use the make tool, see: Make tool introduction.
③ Kernel symbol table (module stack Technology)
Insmod uses the common kernel symbol table to parse undefined symbols in the module. The common kernel symbol table contains the addresses of all global kernel items (variables and functions), which is necessary for implementing modular drivers. After a module is loaded into the kernel, any symbols it exports will become part of the kernel symbol table. New modules can use symbols exported by our own modules. In this way, we can stack new modules (module stack technology) on other modules ). Symbol export achieves resource sharing between modules. The module hierarchy of the driver can be understood as providing symbols for the hardware driver layer. The abstraction layer and hardware driver layer are equivalent to two or more modules. We can divide modules into multiple layers to shorten the development time.
Symbol export method:
Export_symbol (name );
Export_symbol_gpl (name );
④ Initialization and Shutdown
Module_init (hello_init );
Module_exit (hello_exit );
Initialization and shutdown are mandatory and are executed by the module during insmod and rmmod.
⑤ Module Parameters
The kernel allows the driver to specify parameters that can be changed when the driver module is loaded. Driver parameters include the device number and some other parameters that control the driver operation mode. For example:
Insmod hellop howmany = 10 whom = "SML"
In the module, the parameter application method includes the header file
# Include <moduleparam. h>
Format:
Module_param (name, type, s perm );
For example:
Module_param (howmany, Int, s_irugo );
S_irugo indicates that anyone can read the parameter but cannot change it;
S_irugo | s_iwusr indicates that the root user is allowed to modify this parameter. In most cases, we should not make module parameters writable.