Http://www.cnblogs.com/yuuyuu/p/5119891.html
ZC: Doubt, the last module Kernel_mod call module Kernel_fun function Fun, is successful OK. But how does the module Kernel_mod know that it calls the kernel_fun fun function of the module? If there is another module Kernel_fun01 it also exported the fun function, at this time the module Kernel_mod call fun, which module is called the fun function?
(ZC: Test, two modules have the same export function, when loading the module [Insmod] will error: "Insmod:error inserting ' Kernel_fun01.ko ':-1 Invalid module format", and then yongming "DMESG |tail-n 10" can see a clearer message: "Kernel_fun01:exports duplicate symbol fun (owned by Kernel_fun)". Visible kernel modules cannot export functions with the same name ...)
ZC: module Kernel_mod call function Fun, is the module is located in the path to find the function? The path where the module is located cannot be found, continue to find the path inside paths?? Or are all modules under the same special path??
ZC: Command "Insmod???" Installed kernel modules, are located in the same place? That means the same path? Does the kernel module have a relative path to say?
ZC: can modules of different paths export functions with the same function name?
ZC: A: This test: Kernel_fun.ko placed in "/home/driverz/" and has been installed, will Kernel_fun01.ko put in "/home/" Install the Times wrong "insmod:error inserting" Kernel_fun01.ko ':-1 Invalid module format "," DMESG |tail-n 10 "display" Kernel_fun01:exports duplicate symbol fun (owned by Ke Rnel_fun) "
I. Preface
Let's take a look at the Linux kernel programming from 3 small examples. As follows:
1. The Hello World of kernel programming
2. Module parameter passing
3. Inter-module function calls
Two. Preparatory work
First, install the Linux header file on your Linux system, the Debian series:
1 $:sudo apt-get Install linux-headers-' uname-r '
After installation, in your/lib/modules/directory, there is a directory that corresponds to the version number of the header file you just installed. There is also a build folder underneath the header folder, where the makefile file is used for compiling kernel modules. , this is on top of my machine:
Note: The installation of the header file version must be the same as your system version, or you write your own module can not be inserted into the native kernel. If you apt-get install the header file, there is no corresponding header file, or your source inside the unstable version of the source, there is still no corresponding header file, you can go here to search for the required Deb package to install. or download the kernel source corresponding to the machine to build the environment.
Three. The Hello world of kernel programming
Let's take a look at the entry and exit of the kernel module files first. It consists of 2 macros to register the entrances and exits, respectively:
1 module_init (x); 2 module_exit (x);
These 2 macros are include/linux/module.h in the header file directory. The x inside the macro represents the entry and exit functions registered to the kernel. The popular point is the function that is called when module initialization and module unload.
The form of the initialization function: int my_init (void);
form of Exit Function: void My_exit (void);
There are other macros:
Module_license (_license): The license for the module.
Module_author (_author): The author of the module.
Module_version (_version): module version
Module_description (_description): Description of the module.
Some of them are not examples.
Now, let me look at the simplest example of Hello World:
File name: kernel_hello.c
1 #include <linux/init.h> 2 #include <linux/module.h> 3 #include <linux/kernel.h> 4 5/* The following 4 macros are licensed Certificate, author, module description, module version */6 Module_license ("Dual BSD/GPL"); 7 Module_author ("Yuuyuu"); 8 module_description ("Kernel MODULE hello"); 9 Module_version ("1.0"); 10 11/* Ingress function */12 static int hello_init (void) ( kern_alert "Hello_init () start\n" ); 0;17}18 19/* Exit function */20 static void Hello_exit (void) {PRINTK (Kern_alert hello_exit () start\n 23}24 25/* Registered to the kernel */26 module_init (hello_init); Module_exit (Hello_exit);
The above PRINTK () function when the kernel implements its own output function, Kern_alert when the level of output information!
Then write a very simple makefile file:
1 Kernal_dir =/lib/modules/$ (Shell uname-r)/build2 PWD: = $ (Shell PWD) 3 4 obj-m: = Kernel_hello.o5 6 default:7 $ (MA KE)-C $ (Kernal_dir) m=$ (PWD) modules
Kernal_dir: The directory where you just installed the header file
PWD: Represents the directory in which you are currently writing module files.
OBJ-M: The dependent target file for the module. In addition, the kernel of the makefile file: Obj-m representative to compile the target file into a module, OBJ-Y representative compiled to the kernel, OJB-N representatives do not compile.
C: When you perform make, switch the working directory to the parameter directory specified after-C, which is the build directory under the header file directory.
M: This m is a variable inside the kernel makefile. When acting back to the current directory to continue reading makefile, here is read the build directory after the makefile and then go back to our directory, read the makefile we have just written.
The final modules is to represent the compilation module.
Now let's compile and execute make under the directory where we write makefile, and we'll see that the module file is generated Kernel_hello.ko
View module information: sudo modinfo Kernel_hello.ko
You can see the module information that just a few macros have inserted.
Now we do 4 actions at a time: Insert the module, view the module that the kernel has plugged in, unload the module, and view the DMESG information:
As you can see, the module prints the information inside the function at initialization and exit.
Four. module parameter passing
The parameter passing of the module is also a macro, in the header file directory of the include/linux/moduleparam.h:
1 Module_param (name, type, perm)
Name: The variable name in the module and the parameter name that the user can specify.
Type:byte,short,ushot,int,uint,long,ulong,charp,bool these
Perm: Permissions control for the module. Same as the Linux file permissions control.
File name: KERNEL_HELLO_PARAM.C
1 #include <linux/init.h> 2 #include <linux/module.h> 3 #include <linux/kernel.h> 4 5/* The following 4 macros are licensed Certificate, author, module description, module version */6 Module_license ("Dual BSD/GPL"); 7 Module_author ("Yuuyuu"); 8 module_description ("Kernel MODULE hello"); 9 Module_version ("1.0"), ten static char *msg;12 Module_param (msg, charp, 0644); 13 14/* Ingress function */15 static int hello_init ( void) PRINTK (Kern_alert "Hello_init () start\n"), printk (Kern_alert "%s\n", msg), return 0;21 }22 23/* Exit Function */24 static void Hello_exit (void): { printk (kern_alert "hello_exit () start\n"); 27}28 29/* Register to the kernel */ Module_init (Hello_init); Module_exit (Hello_exit);
The 11,12,18 line is added to the previous file. Note the charp of line 12th, which is the character pointer of the kernel.
After compiling, the parameter is inserted, DMESG View information:
The inserted parameter msg follows the module.
Five. Inter-module function calls
The functions of the module are exported to the symbol table for use by other functions, and macros are required:
1 Export_symbol (SYM)
The macro is inside the include/linux/export.h.
Now that we have a function call between modules, we're going to write 2 modules.
File One: Kernel_fun.h
1 #ifndef kernel_fun_h2 #define KERNEL_FUN_H3 4 void Fun (void), 5 6 #endif
File two, module file to export: KERNEL_FUN.C
1 #include <linux/init.h> 2 #include <linux/module.h> 3 #include <linux/kernel.h> 4 #include <linux/ Export.h> 5 6 #include "kernel_fun.h" 7 8/* The following 4 macros are licenses, author, module description, module version */9 Module_license ("Dual BSD/GPL"); Odule_author ("Yuuyuu"), Module_description ("Kernel MODULE hello"), Module_version ("1.0"); 13 14/* Entry function */15 static int fun_init (void)-{ printk (kern_alert "Fun_init () start\n"), return 0;20}21 $ void fun () INTK (Kern_alert "Fun () is called\n"); 25}26 27/* Exit function */28 static void Fun_exit (void) printk (Kern_alert "fun_ex It () start\n "), 31}32 33/* Registered to the kernel */34 module_init (fun_init); Module_exit (Fun_exit); 36 37/* Export symbol Table */38 Export_symbol (FU n);
The last line is to export to the symbol table.
File three, the function to invoke module file two: KERNEL_MOD.C
1 #include <linux/init.h> 2 #include <linux/module.h> 3 #include <linux/kernel.h> 4 #include <linux/ Export.h> 5 6 #include "kernel_fun.h" 7 8/* The following 4 macros are licenses, author, module description, module version */9 Module_license ("Dual BSD/GPL"); Odule_author ("Yuuyuu"), Module_description ("Kernel MODULE hello"), Module_version ("1.0"); 13 14/* Entry function */15 static int mod_init (void)-{ printk (kern_alert "Mod_init () start\n"); */* Call Fun */20 fun (); return 0 ; 22}23 24/* Exit function */25 static void Mod_exit (void): { printk (kern_alert "mod_exit () start\n"); 28}29 30/* Register to Kernel */ Module_init (Mod_init); Module_exit (Mod_exit);
The 20th line is the function that calls the other module.
Here to compile 2 modules, corresponding to the makefile file:
1 Kernal_dir =/lib/modules/$ (Shell uname-r)/build2 PWD: = $ (Shell PWD) 3 4 obj-m: = KERNEL_MOD.O kernel_fun.o5 6 default : 7 $ (make)-C $ (Kernal_dir) m=$ (PWD) modules
After compiling these 2 modules, we will now verify. Note that because Kernel_mod relies on kernel_fun, I want to insert the Kernel_fun module first.
Uninstalling the module, we must first uninstall Kernel_mod, the reason is the same as above.
Insert Kernel_fun, view its symbol table, and then insert Kernel_mod to see DMESG:
You can see that Kernel_fun's fun () was called by Kernle_mod.
"Turn" on Linux kernel programming, parameter passing and inter-module function calls