The kernel module is an external plug-in provided by the Linux kernel. It is called the dynamic loadable kernel module (lkm. The Linux Kernel provides a module mechanism because it is a single kernel ). The biggest advantage of a single kernel is high efficiency, because all the content is integrated, but its disadvantage is the poor scalability and maintainability. The module mechanism aims to make up for this defect.
I. What is a module?
A module is a program with independent functions. It can be compiled but cannot run independently. It is linked to the kernel at runtime and runs in the kernel space as part of the kernel, which is different from the process running in the user space. A module is usually composed of a group of functions and data structures to implement a file system, a driver, or other kernel upper-layer functions.
Comparison between applications and Kernel Modules
To better understand the kernel module, table 1 shows the comparison between applications and kernel module programs.
Table 1 Comparison between applications and kernel module programs
C language application kernel module Program
Use libc library kernel functions
Run space user space kernel space
Running permission, common user, Super User
Entry function main () module_init ()
Exit function exit () module_exit ()
Compile gcc-C makefile
Connect to GCC insmod
Run insmod directly
Debug GDB kdbug, KDB, kgdb, etc.
From table 1, we can see that the kernel module program cannot call functions in the libc library. It runs in the kernel space and can only be run by Super Users. In addition, the module program must use the module_init () and module-exit () functions to tell the kernel "I am coming" and "I am going ".
2. Compile a simple module
Both modules and kernels run in the kernel space. In a sense, module programming is kernel programming. Due to each change in the kernel version, some function names also change accordingly. Therefore, module programming is closely related to the kernel version. The following is an example of ubuntu 9.10 kernel 2.6.31-14-generic.
1. program example
Hello. c
#include<linux/module.h>#include<linux/kernel.h>#include<linux/init.h>MODULE_LICENSE("GPL");static int __init lkp_init(void){ printk(KERN_ALERT "Hello World!\n"); return 0;}static void __exit lkp_cleanup(void){ printk(KERN_ALERT "Bye World!\n");}module_init(lkp_init);module_exit(lkp_cleanup);MODULE_AUTHOR("heyutao");MODULE_DESCRIPTION("hello");
Description
All modules must use the header file module. H, which must be included.
The header file kernel. h contains common kernel functions.
The header file init. h contains macro _ init and _ exit, which allow you to release the memory occupied by the kernel.
Lkp_init is the initialization function of the module. It must contain such content as the code to be compiled and the initialized data structure.
The printk () function is defined by the kernel and similar to the printf () function in the C library. It outputs the information to be printed to the terminal or system log.
Lkp_cleanup is the exit and cleanup function of the module. Here we can do all the cleanup work related to terminating the driver.
Module_init () and cleanup_exit () are the two most basic and necessary functions in module programming.
Module_init () is the entry point for driver initialization. Cleanup_exit () cancels all functions provided by the module.
2. Compile the MAKEFILE file and put it in the same directory as hello. C.
obj-m := hello.oKERNELBUILD :=/lib/modules/$(shell uname -r)/builddefault: make -C $(KERNELBUILD) M=$(shell pwd) modulesclean: rm -rf *.o *.ko *.mod.c .*.cmd *.markers *.order *.symvers .tmp_versions
(Note the required tab in makefile)
Kernelbuild: =/lib/modules/$ (shell uname-R)/build is the makefile path required for compiling the kernel module, which is
/Lib/modules/2.6.31-14-generic/build
Make-C $ (kernelbuild) M = $ (shell PWD) Modules compile the kernel module. -C transfers the working directory to kernelbuild, calls makefile under the directory, and passes the value of the Parameter m to this makefile: $ (shell PWD) modules.
3. Compilation Module
# Sudo make (call the first command default)
At this time, there will be hello. KO in the folder where hello. C is located. This is the kernel module we need.
# Sudo make clean
Clear the compilation garbage. Hello. Ko will also be cleared.
4. Insert a module to make it work. Note that the permission must be root.
# Sudo insmod./Hello. Ko
We can use dmesg to see the generated kernel information, hello World!
If "hello from Hello world" is not output, because if you run it on a character terminal instead of a terminal simulator, in the terminal simulator, kernel messages are output to the log file/var/log/Kern. log.
# Sudo rmmod./Hello
Use dmesg to view bye world!
Modutils is a software package for managing kernel modules. You can obtain the modutils (modutils-x.y.z.tar.gz) Source Code wherever you obtain the kernel source code, and then select the highest level patch. x. y. Z is equal to or less than the current kernel version. After installation, there will be utilities such as insomod, rmmod, ksyms, lsmod, and modprobe in the/sbin directory. Of course, when we load the Linux kernel, The modutils has been loaded.
1. insmod command
Call the insmod program to insert the module to the kernel as the target code. During insertion, insmod automatically calls the init_module () function to run. Note: Only super users can use this command in the following format:
# Insmod [path] modulename. Ko
2. rmmod command
Call the rmmod program to remove the module that has been inserted into the kernel from the kernel. rmmod runs the cleanup_module () function automatically. The command format is as follows:
# Rmmod [path] modulename. Ko
3. lsmod command
When you call the lsmod program, information about the modules in use in the current system is displayed. In fact, the function of this program is to read the information in/proc/modules in the/proc file system. The command format is:
# Lsmod
4. ksyms command
Ksyms is used to display information about Kernel symbols and module symbol tables. Similar to lsmod, lsmod is used to read another file/proc/kallsyms in the/proc file system.
This is the simplest module compilation process in 2.6.xx.