This article was reproduced from: http://blog.csdn.net/coding__madman/article/details/51298180
1. What is a kernel module
The kernel module has the following two features: 1. The module itself is not compiled into a kernel file (zimage or bzimage) and can be dynamically installed or uninstalled during kernel operation, as required.
2. Why kernel modules are required
Cause: The overall structure of the Linux kernel is very large, it contains a lot of components, how to use these components, Method 1: All the components are compiled key kernel, namely: Zimage or bzimage, but this will cause a problem: too much memory. Then the kernel module is born, can not be compiled into the kernel but can be dynamically added to the running kernel!
3. How to use kernel modules
1> Mounting Module Insmod Filename.ko
2> Unload module Rmmod filename
3> View Module Lsmod
Here is a simple example:
Still the same way HelloWorld the program (but there is no main function here)
[HTML]View PlainCopy
- #include <linux/init.h>
- #include <linux/module.h>
- static int Hello_init ()
- {
- PRINTK (kern_warning "Hello world!\n");//The preceding macro represents the level of printing
- return 0;
- }
- static void Hello_exit ()
- {
- PRINTK (kern_warning "Hello exit!\n");
- }
- Module_init (hello_init);//Use a macro to specify that the load function is called when the import module is loaded
- Module_exit (Hello_exit);
Three elements: Load module, unload module, header file
Makefile file
[HTML]View PlainCopy
- Obj-m : = helloworld.o
- Kdir : =/home/kernel/linux-ok6410//Development Board run the kernel source path, because the module finally is to run on the Development Board, so the compilation of this module is to rely on the board running kernel source code, (Here I Development Board with the kernel source code is this path)
- All
- Make-c $ (Kdir) m=$ (PWD) modules cross_compile=arm-linux- arch=arm
- Clean
- Rm-f *.o *.ko *.order *.symvers
Here Kdir is the path of our kernel source code:
Then make to compile the module file, which can see that the Helloworld.ko file was generated
Combined with the previous courses here can be seen through the serial terminal synchronization (see the effect of the relevant command execution)
Optional options for kernel modules:
1. Module declaration
2. Module parameters
3. Symbol output
Linux kernel Module Development Basics "Go"