1. Module Driver description
1) The module itself is not compiled into the kernel image, which controls the size of the kernel
2) Once the module is loaded, it is exactly the same as the rest of the kernel
2. Experimental steps
1) Extract the linux-3.0.35 core from the original supply
tar XF linux-3.0. . tar. bz2
2) Create a directory dedicated to the driver module that is not in the kernel directory
mkdir S-module
3) Enter the module directory to write the Hello World driver module
$ cd S&
The contents of the Hello World driver module are as follows:
#include <linux/init.h>
#include <linux/module.h>
static int __init hello_init(void)
{
printk(KERN_ALERT "Hello World\n");
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_ALERT "Saya, Hello World !\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("Rex <[email protected]>");
MODULE_DESCRIPTION("hello world driver");
MODULE_LICENSE("GPL");
This simplest kernel module contains only kernel module load functions, unload functions, and declarations of GPL permissions and some descriptive information.
4) Linux kernel module program structure
- Module load function (functions passed in Module_init in the program)
When loaded into a kernel module via the insmod or Modprobe command, the module's load function is automatically executed by the kernel.
- Module unload function (the function passed in Module_exit in the program)
When a module is unloaded via the Rmmod command, the module's unload function is automatically executed by the kernel.
The license (LICENSE) statement describes the license permissions of the kernel module and, if not declared LICENSE, receives a kernel warning when the module is loaded
Information (kernel tainted)
Generally speaking, the module unload function is to complete the function opposite to the module loading function, as follows:
- If the module loading function registered XXX, then the module unload function should unregister xxx
- If the module loading function dynamically requests the intrinsic, then the module unload function should release the memory
- If the module loading function requests the use of hardware resources (DMA, interrupts, I/O ports, I/O memory), then the module unload function should release these hardware resources
- If the module loading function turns on the hardware, the Unload function is typically closed
5) Compile the module
By writing makefile by make to complete the compilation of the module, makefile and hello.c in the same directory, the specific content is as follows:
KDIR=/home/xxx/s-linux-3.0.35
PWD:=$(shell pwd)
# kernel modules
obj-m := hello.o
modules:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.o *.ko *.mod.c *.markesr *.order *.symvers
.PHONY:modules clean
6) Compilation of the module and problems encountered
Make in the S-module directory directly, the following error message appears:
ERROR: Kernel configuration is invalid.
include/generated/autoconf.h or include/config/auto.conf are missing.
Run ‘make oldconfig && make prepare‘ on kernel src to fix it.
Workaround: Compile the build kernel and then compile the module
First enter the kernel root directory and do the following:
s-linux-3.0.35$ make distclean
s-linux-3.0.35$ make imx6_defconfig
s-linux-3.0.35$ make uImage
Then go into the module directory and compile the module as follows:
s-module$ make
s-module$ ls
hello.c hello.mod.c hello.o modules.order
hello.ko hello.mod.o Makefile Module.symvers
7) Register the Hello driver module to the I.MX6 Development Board
The Development Board is currently mounted on an NFS file system that replicates the Hello.ko to the/root in the NFS system
Execute on the board with the following tips:
[email protected] ~$ insmod hello.ko
hello: version magic ‘3.0.35-2666-gbdde708 SMP preempt mod_unload modversions ARMv7 ‘ should be ‘3.0.35-gae4624f-dirty SMP preempt mod_unload modversions ARMv7 p2v8 ‘ insmod: can‘t insert ‘hello.ko‘: invalid module format
Error Reason: The module-driven version information does not match the kernel's version information, the board-loaded kernel is not the kernel just compiled
Workaround: Put the newly compiled uimage kernel into the TFTP root directory (the Development Board downloads the kernel via TFTP), the board executes
[email protected] ~$ reboot
The system starts and then performs the load operation:
[email protected] ~$ insmod hello.ko
Hello World
[email protected] ~$ rmmod hello.ko
Saya, Hello World !
i.mx6 Hello World Module driver experiment and related problem solving