Current location - Quotes Website - Personality signature - Where is the linux kernel module signature public key?
Where is the linux kernel module signature public key?
This paper mainly introduces the related concepts of Linux kernel module and the simple module development process. This paper mainly introduces the kernel module from the common instructions in module development, the structure of kernel module program, module usage counting and module compilation. In the process of Linux system development, the importance of developing in the form of modules is self-evident, while in the development of embedded device drivers, releasing drivers in the form of modules greatly improves the flexibility of device use-users only need to get the relevant driver modules and insert them into the user's kernel, so they can use your devices flexibly.

Two. Outline of the article

1.

2. Outline of the article

3. Overview

4. General description of module development

5. Kernel module program structure

6. Module usage count

7. Compilation of modules

8. Use the module to bypass the GPL

9. Summary

Three. general survey

The whole structure of Linux kernel is already huge, including many components. For us engineers, there are two ways to incorporate the required functions into the kernel.

One: Compile all functions into the Linux kernel.

Second, compile the required functions into modules and add them dynamically when necessary.

Advantages and disadvantages of the above two methods:

The first kind:

Advantages: there will be no version incompatibility, and strict version checking is not required.

Disadvantages: the generated kernel will be very large; To add new features to an existing kernel, you need to compile the entire kernel.

The second type:

Advantages: the module itself is not compiled into the kernel, thus controlling the size of the kernel; Once the module is loaded, it will be exactly the same as other parts.

Disadvantages: There may be incompatibility between kernel and module versions, leading to kernel crash; Will lead to low memory utilization.

4. Instructions commonly used in module development

The following instructions are commonly used in kernel module development.

1) insmod: Insert the module into the kernel. Usage: # INSMODXXX.ko

2) rmmod: delete the module from the kernel, using method: # rmmodxxx.ko

3) lsmod: list shows all kernel modules, which can be used in combination with grep instruction. Usage: #lsmod | grep XXX

4) modprobe: modprobe can load a specified single module or a set of dependent modules. Modprobe will decide which modules to load according to the dependencies generated by depmod. If there is an error during the loading process, the whole module will be uninstalled in modprobe. The dependency is obtained by reading/lib/modules/2.6.xx/modules.dep, which was created by depmod.

5) modinfo: View module information. Usage: # modinfxxx.ko

6) Tree–a: View the entire tree structure of the current directory. Usage: #tree -a

Verb (abbreviation of verb) The program structure of kernel module.

1) module loading function (generally required)

This function is performed when the module is loaded using the insmod or modprobe command. Complete the initialization of module.

The module loading function of Linux kernel is generally declared by __init, and it must be specified in the form of module_init (function name). This function returns an integer value, 0 if the execution is successful, and an error code if the initialization fails. The error code in the Linux kernel is negative, and

In Linux, the function identifying __init is placed in the init.text section when connecting, and a copy of function pointers is saved in initcall.init when initializing. The kernel will call the initialization function according to these pointers, and release these init sections (including the first two) after initialization.

Code list:

1 static int _ _ init XXX _ init(void)

2

3 {

four

5 returns 0;

6 }

seven

eight

nine

10 moudle _ init(XXX _ init);

2) Module unloading function (generally required)

This function is performed when the module is uninstalled by using the rmmod or modprobe command. Complete the opposite operation of loading.

The unloading function of the module and the loading function of the module realize opposite functions, mainly including

If the module loading function registers XXX, the module unloading function cancels XXX.

If the module loading function dynamically allocates memory, the module unloading function releases memory.

If the module loading function applies for hardware resources, the module unloading function releases these hardware resources.

If the module loading function opens hardware resources, the module unloading function must close these resources.

Code list:

1 static void __exit XXX_exit(void)

2

3 {

four

5 }

six

seven

eight

9 moudle _ exit(XXX _ exit);

3) Module license statement (required)

If it is not declared, the module will receive a warning that the kernel is polluted when loading, and generally it should follow the GPL protocol.

Code list:

1 MODULE _ LICENSE(“GPL”);

4) Module parameters (optional)

The value passed to the module at load time should itself be a global variable in the module.

Sample program manual

1 # include & lt; Linux/init . h & gt;

2

3 # contains < linux/module.h >;

four

five

six

7 Static char *bookName = "Good book." ;

eight

9 Static int ISBN =100;

10

1 1

12

13 static int _ _ init book _ init(void)

14

15 {

16

17 printk(KERN_INFO "book name %s\n", book name);

18

19 printk(KERN_INFO "ISBN %d\n", Book number.

20

2 1 returns 0;

22

23 }

24

25

26

27 static void _ exit book _ exit (void)

28

29 {

30

3 1 printk(KERN_INFO "book module exits. \ n ");

32

33 }

34

35

36

37 module _ initialization (book _ init);

38

39 module _ exit (book _ exit);

40

4 1 module_param(bookName,charp,S _ I rugo);

Forty two.

43 module_param(bookNumber,int,S _ I rugo);

Forty-four

45

46

47 Module _ License ("GPL");

When inserting a module into the kernel, you can use the following methods. In the kernel log, you can see that the variables have values after the module is loaded.

5) Module export symbol (optional)

Using modules to export symbols is convenient for other modules to rely on modules and use variables and functions in modules.

In the Linux2.6 kernel, the /proc/kallsyms file corresponds to the symbol table, which records symbols and their corresponding memory addresses. For modules, the following macros can be used to export symbols.

1 EXPORT_SYMBOL (symbol name);

or

1 EXPORT_GPL_SYMBOL (symbol name);

6) Module information (optional)

Module information refers to the author information of the module, etc.

Count of intransitive verb module usage

The Linux kernel provides MOD_INC_USE_COUNT and MOD_DEC_USE_COUNT macros to manage the module usage count. However, for kernel modules, usage counting is generally not managed by themselves.

Seven. Compilation of modules

Put the following Makefile in the directory at the same level as book.c, and then compile it with #make command or #make all command to generate the book.ko module file.

Corresponding Makefile:

1 ifneq ($(KERNELRELEASE),)

2

3 mymodule_objs := book.o

four

5 obj-m := book.o

six

7 others

eight

9 PWD := $ (Shell PWD)

10

1 1 KVER? = $(shell uname -r)

12

13kdir: =/usr/src/Linux-headers-2.6.38-8-General.

14

15

16

17 all:

18

19 $(MAKE)- Canadian dollar (KDIR) M=$(PWD)

20

2 1 cleaning:

22

23 rm -rf *.mod.c *.mod.o *。 ko *。 o *。 tmp_versions *。 Order * Symmetry

24

25 endif

Eight. Use module bypass

If the function is not compiled into a module, it is impossible to bypass the GPL. After compiling into a module, the company only needs to publish the module, not the source code. In order to make Linux system support modules, the following work needs to be done:

Select Loadable Module when compiling the kernel. Embedded products generally do not need to uninstall modules, so "uninstallable modules" can be omitted.

Put our ko file in the file system.

Linux system implements tools such as insmod and rmmod.

When using, you can use insmod to load the module manually, or you can modify the /etc/init.d/rcS file to make the module load when the system starts.