5. Linux Kernel module development

Source: Internet
Author: User

Module drivers for Linux

    1. Next, write the simplest driver, just like the programming language of the Hello World program.
    2. The first is: HELLO.C code:

      This is the simplest driver. is to print hello information. The driver and our program language results are a little bit different. The entrance to the drive module is a function of module_init () in the penultimate row. The exit of the drive module is a function of the module_exit ().

3. Next is the Makfile file:

This is also a very simple makefile file. Obj-m is followed by our final target dependent on the file hello.o. The third line of Kdir is the path to the kernel that we compiled into. All is the target of executing make, and $ (kdir) specifies the path to the kernel, which is the path to the third row. m=$ (PWD) is the path where modules are stored. The next is the command to clear the generated file.

4.make Execution Process:

From the above implementation process, we can see the process of makefile execution.

If in a project, when there are two. c Files, write:

HELLO.C:

FUNCTION.C:

Makefile modified to:

The results of the final compilation are as follows:

Installation and uninstallation of kernel modules:

Insmod Hello.ko

To unload a kernel module:

Rmmod Hello (no need to add. ko when unloading)

View Modules:

Lsmod

Results of execution:

Note: Kernel modules can be uninstalled only when no user is available, such as: Our test is not used, and fuse has two users in use. We tried to unload the two kernel modules:

Optional information for kernel modules: module declaration, module parameters, symbolic information.

Declaration of the module:

Module_license ("Compliance Agreement")

Affirm that the module complies with the license agreement, such as: "GPL", "GPL v2" and so on.

Module_author ("author")

Affirm the author of the module

Module_description ("Function Description of the module")

Module_version ("v1.0")

Declare the version of the module

The module declaration allows the reader to know the protocol that the module adheres to, and to increase the readability of the module code.

Just a hint to increase the usefulness of readability.

Module parameters are passed:

In our application: int main (int argc,char** argv): ARGC represents the number of arguments entered on the command line, and the parameters of the input are saved in argv.

So how do we pass in the parameters in our kernel modules? :

Module parameter is a little bit like our programming language parameters, in addition to the general data type to declare variable parameters, we have to use Module_param () This macro to specify that it is a module parameter:

Module_param (name,type,perm):

Name: Names of variables

Type: The bool,int,charp of the variable.

Perm: access rights. S_irugo: Read permissions. S_IWUSR: Write permission.

For example:

Int a=33;

Char *st;

Module_param (A,int, S_irugo);

Module_param (St,charp,s_irugo);

The following is an example of execution:

We define a a=99 and then print it in 14 lines. Results of the operation:

The above is the process of execution, we can also add parameters to it at the time of execution:

The same is true for strings:

Results of the operation:

Finally, the symbol export:

Example of symbol Export:

Modify Makefile:

Modify the FUNCTION.C to:

The process of execution:

At the same time, two. Ko modules are produced.

When we went to install Hello.ko, there was this error: undefined symbol:

This is because of the extern int function () in our program, which is not present in the system. Does that have to Insmod Function.ko first!?

Although the system has already appeared function functions, but the system still cannot find. You can see that the error is still there.

This is the problem with module export: When we are going to use a variable inside a module, the function must be exported using a symbol. That is, the output of variables and functions into our system, so that the entire system can be used.

Modify the FUNCTION.C to:

Declare with Export_symbol () that my function can be used by other modules of the system. However, we should compile Function.ko before compiling the Hello.ko. Results:

This is the use of symbolic output.

Summarize the differences with the application:

Kernel Printing:

Both printf and PRINTK are printed information. But PRINTK also has level printing:

HELLO.C:

Output Result:

As a result, only Kern_emerg levels are printed on the screen. With such a print level, we can control what can be printed in those places. Of course, we can also use input to replace the level. For example, the "<0>" above is Kern_emer.

5. Linux Kernel module development

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.