Linux kernel modules

Source: Internet
Author: User
Tags dmesg

    • A simple drive

The use of the module can make the Linux kernel easy to crop, according to different application needs to get a minimum kernel, while debugging kernel driver is also more convenient, such as if the debug i²c driver, if not the mode of module, then every time you modify the I²c driver will have to compile the entire kernel, for compiling debugging is extremely time consuming, using the module , a simple insmod will load the module into the kernel, if it does not feel appropriate, need to debug, only need to Rmmod to unload the module.

A simple drive module:

1#include <linux/init.h>2#include <linux/module.h>3Module_license ("Dual BSD/GPL");4 5 Static int__init Test_init (void)6 {7PRINTK (Kern_alert"Test Modules init\n");8     return 0;9 }Ten  One Static void__exit Test_exit (void) A { -PRINTK (Kern_alert"Test Modules exit\n"); - } the  -  - Module_init (test_init); -Module_exit (Test_exit);

This driver has nothing to do, print a few words, but has the most basic drivers of several elements:

    1. Driver initialization function xxx_init (void), using macro Module_init (test_init); Tell the kernel this is the initialization function entry for the module.
    2. Drive Exit Function Xxx_exot (void), use macro module_exit (test_exit); Tell the kernel this is the Exit Function entry for the module.
    3. The driver module follows the protocol module_license ("Dual BSD/GPL")

Notice that the initialization function and the Exit function are __init,__exit, in fact, it is not necessary to limit the 2 function is also possible, then the use of these 2 things to limit the difference? First of all, look at what they are 2 things:

1 #define __init        __section (. init.text) __cold notrace2#define __exit          __section (. Exit.text) __exitused __cold notrace

is a 2 macro, where __section is also a macro

1 # define __section (S) __attribute__ ((__section__ (#S)))

__ATTRIBUTE__ is a unique keyword for gcc,__attribute__ can set function properties (functionsattribute), variable properties (Variable attribute), and type properties ( Type Attribute), the more I see is in setting the Alignment property of the data structure:

1 struct S {23 short b[3]; 4 5 } __attribute__ ((aligned (8)));

Set here to. Init.text refers to putting this function into a special. Init.text segment, and seeing the assembly code may have an image. The text segment, which is the code snippet, is where the function is placed. Init.text section of the characteristics of the module is loaded, this section will be released, because it is like we write a single-chip program, the general will have an init function, it is a dead loop, driving the actual operation in the process of xxx_init function does not have any effect, so you can put Xxx_ The INIT function loads into memory and frees up space for others to use.

__exit is the memory that is consumed by the function it identifies when the Unload module is complete.

Besides __init,__exit, there are __initdata,__exitdata. The role is basically similar, I originally guessed that the data should be modified only in the initialization function and the Exit function used in the global variables, such as an array of things, but I tried to use __initdata modifier function as if the compilation does not error, temporarily do not drill down.

Module_license ("Dual BSD/GPL"), less it does not affect the compilation, but when loading the module will be warned that the module contaminated the kernel:

 [22321.826339] test:module license ' unspecified ' taints kernel.
[22321.826349] Disabling lock debugging due to kernel taint
[22321.826759] Test modules Init

    • Compile and load, uninstall this driver

 Makefile of the compilation module

1 Ifneq ($ (kernelrelease),)2Obj-m: =TEST.O BOOK.O3 Else4Kerneldir? =/lib/modules/$ (Shelluname-R)/Build5PWD: = $ (shellpwd)6 Defualt:7$ (make)-C $ (kerneldir) m=$ (PWD) modules8 endif9 Ten Clean : One         RM-RF *.o *~ core depend. *.cmd *.ko *. mod.c. tmp_versions Modules.order module.symvers A  -. Phony:clean

Load module:

1 sudo Insmod Test.ko

For the sake of diagram convenience, use a simple shell script to monitor the output of DMESG, the script is as follows:

1 #!/bin/bash23 set-o nounset                              # Treat unset variablesas an error4  5whiletrue6do7     dmesg-8      sleep19 Done

To unload a module:

1 sudo rmmod test

Uninstalling the module does not require the following. Ko.

    • Specifying and exporting parameters for a module

Parameters in the driver must be a global variable, generally add static qualification.

Declaring parameters, using macros:

1 module_param (< parameters;, < parameter type, < read and write permissions >);

Parameter type support: Byte, short, ushort, int, uint, long, ulong, charp (character pointer), bool, or Invbool (Boolean inverse)

For example:

1 Static intnum =5;2 3Module_param (NUM,int, S_irugo);4 5 Static int__init Test_init (void)6 {7PRINTK (Kern_alert"num:%d\n", num);8     return 0;9}

If at Insmod, specify the value of num:

1 insmod num=

Then output: num:10

Note that the parameter is given a default value when declaring it, because if insmod specifies a parameter value, it uses that default value.

A parameter (a global variable in the driver) how does it have read and write permissions? After loading this module, you will see a test directory, which records some information about the test module, compared to the time when the module parameter is not added. It has a parameters directory, into this directory, found that there will be a module parameter with the same name num file, cat It content is its default value of 5, and then view the permissions of this num file:

  -r--r--r--1 root root 4096 May 3 21:30 num
As I specified in the code, the original permission refers to the permissions of the file, the parameter corresponding to a file, that is, the default value of the parameter can be changed, as long as the permissions are taken, and we use Insmod num=10 method is not to modify the default value (obviously, I have specified num to be 10 successful, but I do not have write access to the NUM file, only the value of the variable is overwritten when the module is loaded.

Using Cat/proc/kallsyms | grep "\[test\]" view the symbols associated with this module Test.ko:

1 00000000 T Test_exit    [Test] 2 00000000 R __param_num    [Test] 3 00000000 R __param_str_num    [Test] 4 00000000 d num    [Test] 5 00000000 D __this_module    [Test] 6 00000000 t cleanup_module    [test]                  


You can see 2 of our expected symbols, num and test_exit, if we do not use __init to qualify Test_init, then we can see that test_init is also in it, which proves that the function of __init is to release the corresponding memory after the initialization function is called:

1 00000000t test_init [Test]2 00000000t test_exit [Test]3 00000000r __param_num [test]4 00000000r __param_str_num [test]5 00000000d num [test]6 00000000d __this_module [test]7 00000000t cleanup_module [Test]8 00000000T init_module [Test]

Using Module_param means that the outside can pass a value in, if this value is visible externally, we obviously cannot use the method of removing the static limit, but using export_symbol (num); This macro refers to the global variable num in the module is visible, it can not only be used for variables can also work on the function.

Module A:

1#include <linux/init.h>2#include <linux/module.h>3Module_license ("Dual BSD/GPL");4 5 Static intnum =5;6Module_param (NUM,int, S_irugo);7 8 Static int__init Test_init (void)9 {TenPRINTK (Kern_alert"Test Modules init\n"); One     return 0; A } -  - Static voidTest_saymyname (void) the { -PRINTK (Kern_alert"Call me\n"); - } - Static void__exit Test_exit (void) + { -PRINTK (Kern_alert"Test Modules exit\n"); + } A  at  - Module_init (test_init); - Module_exit (test_exit); -Export_symbol (Test_saymyname);

Module B calls the test_saymyname of module A

1#include <linux/init.h>2#include <linux/module.h>3Module_license ("Dual BSD/GPL");4 5 extern voidTest_saymyname (void);6 7 Static int__init Book_init (void)8 {9 test_saymyname ();Ten     return 0; One } A  - Static void__exit Book_exit (void) - { thePRINTK (Kern_alert"Book modules exit\n"); - } -  - Module_init (book_init); +Module_exit (Book_exit);

respectively Insmod Test.ko and Insmod Book.ko, and then unload the module, if the first uninstall Test.ko will find the hint Test.ko inside the function is Book.ko call, can not uninstall, only uninstall Book.ko first line.

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.