Some basic knowledge of modules

zhaozj2021-02-16  91

From: http://www.linuxfans.org/nuke/modules.php? Name = News & File = Article & Sid = 2299 & Mode = & Order = 0 & THOLD = 0

For the Linux kernel, the module is available. This feature must be successful when compiling the kernel, and select the appropriate option to succeed. As far as I know, the kernel of all issued systems is customized as preset options.

We can easily design new modules for the system, and have internal core load execution without re-compiling the kernel, nor does it need to restart the system.

When a module loads the kernel, it is a part of the kernel, so:

This module can use all functions in the kernel, and you can access all parameters and data structures. This module is executed with the highest level of the processor. As far as I386's architecture is the 0th layer of the ring (Ring Level 0); so, this module can do a variety of access actions for I / O, and perform a general program unable to execute Directive. The code segment and data segment of the module are directly mapped to physical memory, that is, this module is not possible to do "paning" action. So, during the execution of the module, it is impossible to generate a paging error.

As we have seen, the dynamic load module has some real-time system features: dynamic load modules, avoid time delay caused by paging errors, and dynamically load modules, you can access all hardware resources.

The module can be written in "C language". Here is an example (to execute the command mentioned below, it is best to log in to the system as SU, ROOT):

EXAMPLE1.C

#define module

#include

#include

Static int out = 1;

INT init_module (void) {

Printk ("OUTPUT =% DN", OUTPUT;

Return 0;

}

Void cleanup_module (void) {

Printk ("ADI, BYE, CHAO, OVUAR, N");

}

The following lines of parameters are compiled with example1.c:

# gcc -i / usr / src / linux / include / linux -o2 -wall -d__kernel__ -c example1.c

Option-C is required to stop after generating a target file, do not do connection actions. The final result is a target file, example1.o.

The kernel lacks the standard output function, so we cannot use the Printf () function, and the printk () function provided in the kernel is replaced. Printk () and printf () have almost no two, the only difference is Printk () to send the result of the output to the ring buffer of the kernel. This buffer is where all information sets are concentrated, just like the information seen when booting, can be found in this ring buffer. At any time, we can use the DMSEG command to view the contents of the ring buffer, or inspect / proc / kmsg this file.

Note that there is no main () function in this module, but there is an init_module () function without any parameters. Cleanup_module () is a function that must call before the last release module. Insmod is used to load modules and then perform modules. # insmod example1.o

Now we have installed the Example1 module, and also execute the init_module () function of Example1. To see the results, use the following command:

# dmesg | TAIL -1

OUTPUT = 1

Command LSMOD lists all modules currently loaded into the core:

# lsmod

Module Pages Used by:

EXAMPLE1 1 0

SB 6 1

UART401 2 [SB] 1

Sound 16 [SB UART401] 0 (AutoClean)

Finally, we use rmmod to release modules:

# rmmod esample1

# Dmesg | TAIL-2

OUTPUT = 1

Adi, BYE, CHAO, Orvua,

Dmesg shows the function cleanup_module () has been executed.

Now we just don't know how to pass the parameters to the module. This method is surprisingly simple, as long as the value gives the parameters, then pass the parameters to the module by INSMOD. E.g:

# insmod ejemplo1.o Output = 4

# Dmesg | TAIL-3

OUTPUT = 1

ADS, BYE, CHAO, Orvua,

OUTPUT = 4

转载请注明原文地址:https://www.9cbs.com/read-13399.html

New Post(0)