Kernel module programming-Lesson 1

Source: Internet
Author: User

Module programming is kernel programming. Therefore, in addition to kernel-related knowledge, you also need to understand module-related knowledge.

1. Comparison between applications and Kernel Modules
To better understand the kernel module, table 1 shows the comparison between applications and kernel module programs.
Table 1 Comparison between applications and kernel module programs

  C language application Kernel module Program
Use Functions Libc library Kernel functions
Runtime Space User space Kernel space
Run permission Common User Superuser
Entry Function Main () Module_init ()
Exit Function Exit () Module_exit ()
Compile Gcc-C Makefile
Connection Gcc Insmod
Run Run directly Insmod
Debugging GDB Kdbug, KDB, kgdb, etc.

From table 1, we can see that the kernel module program cannot call functions in the libc library. It runs in the kernel space and can only be run by Super Users. In addition, the module program must use the module_init () and module-exit () functions to tell the kernel "I am coming" and "I am going ".

2. kernel symbol table (if the following 2nd ~ 4. Difficulties in understanding. You can skip this step)

As mentioned above, the Linux kernel is an overall structure, like a ball, and the module is a plug-in inserted into the kernel. Although the kernel is not a installable module, Linux regards the kernel as a "mother" module for convenience. Then, how do we interact between modules? A common method is to share variables and functions. But not every variable and function in the module can be shared. The kernel only places the main variables and functions in each module in a specific section. These variables and functions are collectively referred toSymbol. To which symbols can be shared? Linux Kernel has its own rules. For the special kernel parent module, the "Remove" symbol is defined in kernel/ksyms. C. For example, the "Remove" symbol of the process management subsystem is defined as follows:

/* Process Management */

Export_symbol (do_mmap_pgoff );

Export_symbol (do_munmap );

Export_symbol (do_brk );

Export_symbol (exit_mm );

...

Export_symbol (Schedule );

Export_symbol (jiffies );

Export_symbol (xtime );

...

You may be familiar with these variables and functions. The macro defines export_symbol () as "Remove symbol ". Why is it "Remove? Because these symbols are originally symbols inside the kernel, they are put in a public place through this macro so that other modules loaded into the kernel can reference them.

In fact, it is not enough to know the names of these symbols. It is necessary to know their addresses in the kernel address space. Therefore, the kernel defines the following structure to describe the module symbol:

Struct module_symbol

{

Unsigned long value;/* the address of the symbol in the kernel address space */

Const char * Name;/* symbol name */

};

We can read the "Remove" symbols of all kernel modules from the/proc/ksyms file, which forms a kernel symbol table. The format is as follows:

Memory Address symbol name [module]

In module programming, you can retrieve the corresponding address from the file according to the symbol name, and then directly access the address to obtain kernel data. The third column "module" refers to the module name where the symbol is located. This column is empty for the symbols removed from the kernel parent module.

After the module is loaded, the kernel symbols output by the module can be viewed through/proc/ksyms and/proc/kallsyms in the 2.4 kernel.

3. Module dependency

As mentioned above, the kernel symbol table records the symbols accessible to all modules and their corresponding addresses. When a new module is loaded into the kernel, some symbols it declares will be registered in this table, and these symbols may be referenced by other modules, this leads to the module dependency problem.

When one module A references the symbol removed from another Module B, we can say that Module B is referenced by module A, or that module A depends on Module B. To connect module A, you must first connect Module B. The dependency between modules is calledModule dependency.

4. Module Reference Counter

To ensure that the module is safely uninstalled, each module has a reference counter. The counter is incremented when the operations involved in the execution module are involved, and this counter is decreased when the operation ends. In addition, when Module B is referenced by module A, the reference count of Module B increases progressively, the reference ends and the counter is decreasing. When can I Uninstall this module? Of course, only when the counter value is 0, for example, when a file system is still installed on the system, it cannot be detached. When the file system is no longer used, the reference counter is 0, so it can be detached.

Iv. Module Compilation

GCC is the most important software development tool in Linux. GCC is the gnu c and C ++ compilers. However, in a large development project, there are usually dozens or hundreds of source files. If you manually type the GCC command to compile each time, it will be very inconvenient. Therefore, people usually use the make tool to automatically complete compilation. Using this automatic compilation can greatly simplify the development work and avoid unnecessary re-compilation. These tasks include: if only a few source files are modified, only the source files are recompiled. If a header file is modified, all the source files containing the header file are recompiled.

1. Make

In fact, the make tool uses a file called makefile to complete and automatically maintain the compilation. Makefile needs to be compiled according to a certain syntax, which describes how to compile each source file, generate an executable file, and define the dependency between the source files. Below2.6 makefile template of the kernel module (see makefile writing)

 

# Makefile2.6
OBJ-M + = hellomod. O # generate the target file of the hellomod Module
Current_path: = $ (shell PWD) # current path of the module
Linux_kernel: = $ (shell uname-R) # current version of the Linux kernel source code
Linux_kernel_path: =/usr/src/Linux-headers-$ (linux_kernel) # absolute path of the Linux kernel source code
ALL:
Make-C $ (linux_kernel_path) M = $ (current_path) Modules # compile the module
Clean:
Make-C $ (linux_kernel_path) M = $ (current_path) clean # clean

Note: before each command (for example, before the make command), type A tab (generated by pressing the tab key)

With makefile, executing the make command will automatically form the relevant suffixes as. O and. Ko files.
At this point, the module has been compiled, and it should be inserted into the kernel:
For example: $ insmod hellomod. Ko

Of course, the module can be inserted only as a system member.

After successful insertion, you can run the dmesg command to check whether the output of the last few lines of the screen is the content output in your program: Hello, world! From the kernel space...

 
When the module is no longer needed, you can run the rmmod command, for example

$ Rmmod hellomod

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.