I. experiment environment
Development machine environment
Operating System: Ubuntu 9.10
Cross-compiling environment: Arm-Linux-GCC 4.2.2, installation location/usr/local/ARM/4.3.2/
6410 board kernel source code path:/work/linux-2.6.36.2-v1.05/
Target Board Environment: OK6410-A linux2.6.36
Ii. Experiment Principles
When the module is running in the kernel space, the function in the glibc library cannot be used in the module.
The module can be dynamically loaded into the kernel.
Iii. Experiment steps
Note: It is best to use the root user to perform the following experiments; otherwise, use sudo
1. Verify that the cross-compilation tool is correctly configured. Click "ok6410-a 2.6.36 5-5-" 5-5 to install the cross editor.
# Arm-Linux-gcc-V
If the preceding information is displayed, the development environment is correct. Otherwise, add the path of arm-Linux-GCC to the path. The method is as follows:
Edit the/etc/profile file and add the compiler path to the environment variable path.
# Vi/etc/profile
PATH=/usr/local/arm/4.3.2/bin:$PATHexport PATH
Enter the following command to make the configuration take effect.
# Source/etc/profile
2. Compile the module source file
# VI test. c
#include <linux/kernel.h>#include <linux/module.h>#include <linux/init.h>static int __init hello_init(void){printk(Hello world\n");return 0;}static void __exit hello_exit(void){printk(Goodbye world\n");}module_init(hello_init);module_exit(hello_exit);MODULE_LICENSE("Dual BSD/GPL");
Code explanation:
Static int _ init hello_init (void)
Static void _ exit hello_exit (void)
-Static declaration, because this function has no other meaning than a specific file-_ init mark
Indicates that this function is only used during initialization.
After the module is loaded, release the memory space occupied by the function
-_ Exit flag
This code is only used for uninstalling modules.
Printk kernel functions print information, similar to printf In the glibc library of the user space. Note that functions in the glibc library cannot be used in the kernel.
Module_init (hello_init );
Module_exit (hello_exit );
-Macro: module_init/module_exit-declares the position of the module initialization and clearing function-when the module is loaded and uninstalled, the kernel can automatically find the corresponding function.
3. Compile the MAKEFILE file
# Vi makefile
obj-m := test.oKDIR :=/work/linux-2.6.36.2-v1.05/all: make -C $(KDIR) M=$(shell pwd) modulesclean: make -C $(KDIR) M=$(shell pwd) clean
Note:
- Lines 1, 2, and 4 are written in the top line, and lines 3 and 5 are indented by tab.
- Kdir is the kernel source code path of the 6410 Development Board.
- Test. O. Because our source file is test. C, we need to write test. O here.
- The makefile and test. c files are stored in the same directory, for example,/work/lab/test01/
4. Compilation Module
Enter the make command in the Work/lab/test01/directory.
# Make
The test. Ko file is generated. This is the module File we need, and others are temporary files in the middle.
Check the test. Ko file. We can see that it is a target file based on the ARM platform.
# File test. Ko
Possible error cause
Note: When compiling a module, you need to use some dependent files in the kernel. Therefore, ensure that those files exist in the kernel source code.
If the above compilation has an error, we need to execute the following command under the kernel source code root directory (/work/linux-2.6.36.2-v1.05 /)
# Make oldconfig & make prepare & make scripts
5. Test Module
Download the previously generated module File (test. Ko) to/Lib/modules/2.6.36.2 directory (if this directory is not available, you need to manually create it ).
Load and uninstall modules on the Development Board.
(1) Loading Module
# Insmod/lib/modules/2.6.36.2/test. Ko
(2) view the module list
# Lsmod
(3) uninstall the module
# Rmmod Test
The running result is as follows:
Author: canghai hunter Source: http://blog.csdn.net/embedded_hunter reproduced please indicate the source of embedded technology exchange QQ group: 179012822
Summary
- When doing the above experiments, you must always think about the role of each step. I will try to write more details as much as possible. Do not just follow the instructions, or you will have difficulty making progress.