I. module functions
1. Differences between kernel module programming and Linux application development:
(1) In kernel module programming, there is no main function (2) in kernel module programming, we use printk to print, without printf
2. program structure of the kernel module Program
(1) module Loading Function (required): The function automatically called by the system when the module is installed. It is specified by the module_init (XXX) Macro.
(2) module uninstallation function (required): The function automatically called by the system when the module is detached. It is specified by the module_exit (XXX) Macro.
3. Module Compilation: use makefile to compile
(1) The kernel module is composed of a source file and the makefile Writing Method
Fixed writing format
Ifneq ($ (kernelrelease),) obj-M: = hello. O // this line will be changed, obj-M will not change, and will become hello. oelsekdir: =/lib/modules/2.6.18-53. e15/build // This line changes to all: Make-C $ (kdir) M = $ (PWD) Modules // make-C $ (kdir, go to the $ (kdir) directory and use its makefile to compile clean: Rm-f *. ko *. O *. mod. O *. mod. C *. symversendif
(2) The kernel module consists of multiple source files. The makefile compilation method is as follows:
Ifneq ($ (kernelrelease),) obj-M: = mymodule. O // the target file name generated in this line needs to be changed to mymodule-objs: = Main. o add. oelsekdir: =/lib/modules/2.6.18-53. e15/build // This line changes to all: Make-C $ (kdir) M = $ (PWD) Modules // make-C $ (kdir, go to the $ (kdir) directory and use its makefile to compile clean: Rm-f *. ko *. O *. mod. O *. mod. C *. symversendif
(3) The module is compiled into the target module by two suffixes:. O and. Ko.
Difference: The. Ko file is a kernel object file. The difference with. O is that it has more sections, such as. modinfo. In the 2.4 kernel, The. o file is generated. The 2.6 kernel provides some extensions for kernel module management and generates the. Ko file.
4. Module installation and uninstallation:
(1) insmod loading Module
(2) rmmod uninstall Module
(3) view lsmod
(4) modprobe loading. The difference between modprobe and insmod is that modprobe will be based on the file/lib/modules/<$ version>/modules. dep is used to view the modules to be loaded. It depends on other modules. If yes, modprobe will first find these modules and load them to the kernel.
5. Optional module information:
(1) license statement: module_license
Used to inform the kernel that the kernel module has a license. Valid licenses include: "GPL" gplv2"
(2) Author's statement: module_author
(3) module Description: module_description
(4) module version: module_version
(5) module alias: module_alias
(6) module parameters: module_param
= Use macro module_param to specify the module parameters. The module parameters are used to pass parameters to the module when loading the module.
= Module_param (name, type, Perm );
= Name is the name of the module parameter, type is the type of this parameter, and perm is the access permission of the module parameter
= Type common values: bool -- Boolean, int -- integer, CHARP -- string
= Perm common value: s_irugo -- any user has read permission for this parameter in/sys/Module
S_iwusr -- allows the root user to modify this parameter in/sys/module.
= Eg: int A = 3; // define the variable in step 1
Module_param (A, Int, s_irugo); // The second step declares the variable as a module parameter.
6. kernel symbol Export
To allow functions or variables implemented in a module to be used in other modules, you must first export functions or variables.
(1) Use the macro export_symbol (XXX) to specify the function or variable to be exported.