Introduction to Linux kernel modules

Source: Internet
Author: User

I. Summary

This article mainly introduces the concepts of Linux kernel modules and the simple module development process. Mainly from the module development of common directives, kernel module program structure, module usage count and module compilation and other aspects of the kernel module is introduced. In the Linux system development process, the development of modules in the form of the importance of self-evident, and in the embedded device driver development in the form of modules released, but also greatly improve the flexibility of the use of equipment-users only need to get the relevant driver module, and then inserted into the user's kernel, The flexibility to use your device.

Two. Article outline

1. Summary

2. Article outline

3. Overview

4. Common Directives for module development

5. Kernel Module Program Structure

6. Module Usage Count

7. Compiling the module

8. Use the module to bypass the GPL

9. Summary

Three. Overview

The overall architecture of the Linux kernel is already huge and contains many components, and for our engineers, there are two ways to include the required functionality into the kernel.

One: All functions are compiled into the Linux kernel.

Second: Compile the required functions into modules and add them dynamically when needed.

The pros and cons of both of these approaches:

The first type:

Pros: No version incompatibility issues, no strict version checking required

Cons: The resulting kernel will be large; To add new functionality to an existing kernel, compile the entire kernel

The second type:

Advantage: The module itself does not compile into the kernel, thus controlling the size of the kernel; Once the module is loaded, it will be exactly the same as the rest.

Disadvantage: The kernel may be incompatible with the module version, causing the kernel to crash, resulting in low memory utilization.

Four. Common directives for module development

The following directives are commonly used during the development of kernel modules.

1) Insmod: Insert the module into the kernel, using the method: #insmod Xxx.ko

2) Rmmod: Remove the module from the kernel using the method: #rmmod Xxx.ko

3) Lsmod: The list shows all kernel modules that can be used in conjunction with grep directives. How to use: #lsmod | grep XXX

4) The Modprobe:modprobe can load the specified individual modules or load a group of dependent modules. Modprobe will decide which modules to load based on the dependencies that the depmod generates. If an error occurs during onboarding, the entire set of modules is unloaded at modprobe. Dependencies are obtained by reading/LIB/MODULES/2.6.XX/MODULES.DEP. And the file was established through Depmod.

5) Modinfo: View module information. How to use: #modinfo Xxx.ko

6) Tree–a: View the entire tree structure of the current directory. How to use: #tree-A

Five. Kernel Module program Structure

1) Module load function (typically required)

The function is executed when the module is loaded with the Insmod or Modprobe command. Completes initialization of the module.

The module load function of the Linux kernel is usually declared with the __init identity, and the module load function must be specified in the form of module_init (function name). The function returns an integer value, if successful, returns 0, the error code is returned when the initialization fails, and the error code in the Linux kernel is negative, as defined in <linux/errno.h>.

In Linux, identify __ The init function is placed in the. Init.text section at the time of connection, and a function pointer is kept in the. Initcall.init, and the kernel invokes the initialization function based on these pointers when initialized, releasing the init segments (including the first two) after initialization is complete.

Code Listing:

1 static int __init xxx_init (void)

2

3 {

4

5 return 0;

6}

7

8

9

Ten Moudle_init (Xxx_init);

2) module unload function (typically required)

The function is executed when the module is unloaded with the Rmmod or Modprobe command. Complete the opposite work with the load.

The Unload function of the module and the module loading function implement the opposite function, mainly including

1. If the module loading function is registered with XXX, the module unload function unregisters xxx

2. If the module load function allocates memory dynamically, the module unload function frees the memory

3. If the module loading function requests hardware resources, the module unload function frees the hardware resources

4. If the module loading function turns on the hardware resource, the module unload function must close these resources

Code Listing:

1 static void __exit xxx_exit (void)

2

3 {

4

5}

6

7

8

9 Moudle_exit (Xxx_exit);

3) Module License Statement (required)

If you do not declare, you will receive a warning that the kernel is contaminated when the module is loaded, and you should generally follow the GPL agreement.

Code Listing:

1 module_license ("GPL");

4) module parameters (optional)

The value that the module passes to the module when it is loaded should itself be a global variable inside the module.

Sample program BOOK.C

1 #include <linux/init.h>

2

3 #include <linux/module.h>

4

5

6

7 static Char *bookname = "good book.";

8

9 static int booknumber = 100;

10

11

12

static int __init book_init (void)

14

15 {

16

PRINTK (kern_info "book name is%s\n", bookname);

18

PRINTK (kern_info "book Number is%d\n", booknumber);

20

return 0;

22

23}

24

25

26

static void __exit book_exit (void)

28

29 {

30

PRINTK (kern_info "book Module exit.\n");

32

33}

34

35

36

Panax Notoginseng Module_init (book_init);

38

Module_exit (Book_exit);

40

Module_param (BookName, Charp, S_irugo);

42

Module_param (Booknumber, Int., S_irugo);

44

45

46

Module_license ("GPL");

When inserting modules into the kernel, you can use the following methods, and you can see in the kernel log that the variables already have values after the module is loaded.

5) module export symbol (optional)

Use the module to export symbols to make it easier for other modules to rely on the module, and to use variables and functions in the module.

In the kernel of Linux2.6, the/proc/kallsyms file corresponds to the symbol table, which records the memory address of the symbol and symbol. For modules, use the following macro to export symbols.

1 export_symbol (symbol name);

Or

1 export_gpl_symbol (symbol name);

6) module information (optional)

The module information refers to the module's author information and so on.

Six. Module usage count

The Linux kernel provides mod_inc_use_count and Mod_dec_use_count macros to manage module usage counts. However, for kernel modules, it is not common to manage usage counts on their own.

Seven. Compiling the module

Place the following makefile file in the BOOK.C sibling directory, and then compile with the #make command or the #make all command to generate the Book.ko module file.

The corresponding makefile:

1 Ifneq ($ (kernelrelease),)

2

3 Mymodule_objs: = BOOK.O

4

5 Obj-m: = BOOK.O

6

7 Else

8

9 pwd: = $ (Shell PWD)

10

Kver? = $ (Shell uname-r)

12

Kdir: =/usr/src/linux-headers-2.6.38-8-generic

14

15

16

All:

18

$ (make)-C $ (Kdir) m=$ (PWD)

20

Clean:

22

RM-RF *.mod.c *.mod.o *.ko *.o *.tmp_versions *.order *symvers

24

endif

Eight. Use the module to bypass

If the function is not compiled into a module, then can not bypass the GPL, compiled into a module after the company released products will only need to publish the module, and do not need to publish the source code. For Linux systems to support modules, the following work is required:

1. Kernel compile time Select "Can load module", embedded products generally do not need to unload the module, you can not select "Can unload module"

2. Place our KO files in the file system

3. Linux system implements Insmod, Rmmod and other tools

4. The module can be loaded manually using Insmod, or the/etc/init.d/rcs file can be modified to load the module when the system is started.

Nine. Summary

This paper mainly introduces the concept of kernel module and basic programming method, the kernel module mainly consists of a series of declarations, such as loading and unloading function functions. It can be passed in parameters, or it can export symbols for use by other modules.

Introduction to Linux kernel 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.