Programming Linux Module Modules

Source: Internet
Author: User

Reprinted from: Http://blog.csdn.net/eroswang/archive/2008/09/13/2924875.aspx

Summary
Linux kernel module programming information is a bit complicated, some too simple, some too complex, I try to use the form of notes to readers to show how to process the Linux module programming, trying to be concise, this article is also as I memo information, so some places too simplistic is inevitable. Originally the purpose of this article is to let users know it, as to why or please refer to the corresponding information, in fact, the best information is Linux Kernel source.

Scope of application:

Linux Kernel >= 2.6.0

Introduction to Linux Modules
First of all, this module is different from the microkernel module,microkernel module is a daemon process, working in user space, the Linux module is just a kernel of the target code, the kernel by performing a run-time connection, To integrate it into the kernel, so that the Linux module mechanism does not change the Linux kernel to monolithic OS essence, its module is also working in kernel mode, enjoy all the privileges of the kernel.

As for why to introduce the Linux Kernle Module (hereinafter referred to as LKM), I would like to have at least a few points:

The need for modular programming reduces development and maintenance costs.
The flexibility of the system makes it possible to modify some kernel functions without recompiling the kernel and rebooting the system.
Reduces the complexity of kernel programming and lowers the threshold for entry.

Related macros and header files
Lkm need to include the head file:<linux/kernel.h> <linux/module.h>

You need to define the following macros: __kernel__, MODULE

An example of a simple kernel module

1/*file:hello.c*/
2
3#ifndef __kernel__
4#define __kernel__
5#endif
6#ifndef MODULE
7#define MODULE
8#endif
9
10#include <linux/module.h>
11#include <linux/kernel.h>
12
13static int __init hello_init (void)
14{
PRINTK (Kern_alert "Hello, My lkm.\n");
return 0;
17}
18
19static void __exit hello_exit (void)
{
PRINTK (Kern_alert "Bye, My lkm.\n");
22}
23
24module_init (Hello_init);
25module_exit (hello_exit);

It's a simple answer, isn't it? This LKM function is also very simple, that is, when loading it through insmod, he prints hello, my LKM. By Rmmod Uninstall it when he prints bye, my LKM. A basic kernel module typically contains two functions, one of which is the initialization function ( For example hello_init here), one is the Unload function (Hello_exit), of course, there can be no function, just provide some variables. However, initialization functions and unload functions must appear in pairs. and the INIT function returns a value greater than or equal to zero when the operation succeeds, and nonzero if the operation fails. Macros Module_init and module_exit are used to register initialization functions and unload functions.

Compilation of LKM
An example of makefile is shown below

1obj-m: = hello.o
2KERNELBUILD: =/lib/modules/' Uname-r '/build
3default:
4 make-c $ (kernelbuild) m=$ (shell pwd) modules
5clean:
6 rm-rf *.o. *.cmd *.ko *.mod.c. tmp_versions

If there are other modules under this directory, just add them after hello.o.

Obj-m: = hello.o mod.o execute make in the directory where the module is located, and then we can get the module (HELLO.KO) We want after the success.

If a module has many source files, such as Hello, which is connected by hello1.c hello2.c, you need to add the following line to the makefile

HELLO-OBJS: = hello1.o hello2.olkm Loading
Linux provides users with the Modutils to manipulate modules. This toolset mainly includes:

Insmod Install module Rmmod Remove module modprobe more advanced load and delete modules, you can solve the dependencies between modules Lsmod lists the modules that have been loaded and their information modinfo used to query the module information, such as author, copyright ... Try to load the module with the command insmod Hello.ko, rmmod delete the module and see what happens. You may not see any output, is there an error occurred? No, execute command tail/var/log/message Oh, did you see it?

Feb 00:07:35 Gentux Hello, my LKM. Feb 00:07:38 Gentux Bye, my LKM. Module other Information
Common information often includes: Author, description, copyright, etc., for which LKM provides us with the following macros:

Module_author ("AUTHOR"); Module_description ("the DESCRIPTION"); Module_license ("GPL"); Module_supported_device ("Dev"); Devices supported by the device driver. The more commonly used free license are "GPL", "GPL v2", "GPL and additional rights", "Dual BSD/GPL", "Dual MPL/GPL".

Module parameters
User-Space Applications can accept user parameters, lkm can do it, but the way is a little different. The related macros are:

Module_parm (Var, type); Module_parm_desc (Var, "The description of the Var"); The type of the module parameter (that is, type in Module_parm) has several:

b byte (unsigned char)
H Short
I int
L Long
s string (char*)
These parameters should preferably have a default value, and if some of the necessary parameters are not set the user can reject the load of the module by returning a negative value from the INIT function specified in Module_init. LKM also supports modules of the array type, if the type symbol is preceded by a number n to indicate an array of maximum n, the numbers separated by "-" represent the minimum and maximum array lengths.

Example: Module_parm (Var, "4i"); Module_parm (Var, "2-6i") with a maximum length of 4 for the shaped array; The minimum length is 2, the maximum length of 6 of the shaping array how to pass in the parameters Insmod, in fact, man a bit can be, but now the man is a little too simple, so here to illustrate:

Insmod variable=value[,value2 ...] Where value can be enclosed in quotation marks or not. However, there can be no space between the "=" and no space in value.

Export of module symbols
Unlike user-space applications, the purpose of introducing a module is often to provide some routine to the kernel to accomplish certain functions, with few modules being exported, and Linux provides the following macros to the user:

Export_symbol (VAR); Output SYMBOL VAREXPORT_SYMBOL_GPL (VAR); The output symbol copyright is a dependency between the GPL modules
Sometimes two modules may have dependencies between, to load the module A, depends on the module B, at this time Insmod is powerless, can only use Modprobe to load the module and its dependent modules, otherwise it can only be manually loaded one by one.

Modprobe reads the/LIB/MODULES/VERSION/MODULES.DEP generated by depmod-a to get a list of the modules it relies on (and possibly a module tree), and then calls Insmod to load sequentially.

Problems with namespaces
For global symbol that does not need export, it is best to modify it with static, limiting its scope to this file to prevent contamination of the kernel's namespace.
For some of the symbol export from the kernel or other modules, it is best to use extern to decorate it to show that it is not in this file.
Where the errno variable may be used, because the kernel does not export this symbol, can only be defined by the user, such as: int errno;
A more complex example of a module

1/**//*file:hello.c*/
2#ifndef __kernel__
3#define __kernel__
4#endif
5#ifndef MODULE
6#define MODULE
7#endif
8#include <linux/module.h>
9#include <linux/kernel.h>
10module_author ("Xiaosuo <[email protected]>);
11module_license (GPL);
12module_description ("This module is a example.");
13static int int_var = 0;
14static const char *str_var = "Default";
15static int int_array[6];
17module_parm (Int_var, "I");
18module_parm_desc (Int_var, "A integer variable");
19module_parm (Str_var, "s");
20module_parm_desc (Str_var, "A string variable");
21module_parm (Int_array, "2-6i");
22module_parm_desc (Int_array, "A integer array");
24static int __init hello_init (void)
059
int i;
PRINTK (Kern_alert "Hello, My lkm.\n");
PRINTK (kern_alert "Int_var%d.\n", Int_var);
PRINTK (kern_alert "Str_var%s.\n", Str_var);
for (i = 0; i < 6; i + +)
{
PRINTK ("int_array[%d] =%d\n", I, int_array[i]);
}
return 0;
35}
37static void __exit hello_exit (void)
38{
PRINTK (Kern_alert "Bye, My lkm.\n");
40}
42module_init (Hello_init);
43module_exit (Hello_exit);

Programming Linux Module Modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.