Experiment two kernel modules compiled

This article contains a file to the teacher and do their actual experimental procedures shots.

Compile the kernel module

The experiment:
1, the principle understanding module
2, to write the code module
3, compiling module
4, the load module
5, the test module
6, unloading module

Experimental Procedure

1, Principle
Linux module is a collection of functions and data types can be compiled as a standalone program. The reason why the module provides the mechanism, because Linux itself is a single core. Since the single-core everything integrated, high efficiency, scalability and maintainability relatively poor module mechanism can compensate for this deficiency.
Linux module can be loaded by static or dynamic approach to the kernel space, a static load refers to load the kernel boot process; refers to dynamic loading at any time during the loading operation of the kernel.
When a module is loaded into the kernel, it becomes part of the kernel code. When the module is loaded into the system, the system to modify the kernel symbol table, the new symbol and to add a resource module is loaded into the kernel symbol table, so that the communication between modules.
2, to write the code module
 module constructor:
performing initialization function that will be invoked when the instruction insmod or modprobe loadable module. The function prototype must be module_init (), the function pointer in parentheses
 module destructor:
a function call instruction rmmod when unloaded. The function prototype is module_exit ()
² Module license statement:
function prototype is MODULE_LICENSE (), tells the license to use the program's kernel, otherwise it will prompt when loading the kernel module pollution. Usually write GPL.
 module parameters (optional)
 module exported symbols (optional)
 module of the information statement (optional)

Header module.h, you must include this file;
header kernel.h, contains common kernel functions;
header file that contains macros init.h _init and _exit, allowing the release of the kernel memory occupied.

Write a simple code to the kernel output to a text.

On my machine

Code is very simple, which includes the construction, and permits destructor mentioned above.
3, compile the module
next write Makefile.

The Makefile to be useful shorthand for the semester before.
The first line of printname into your own writing .c file name.
LINUX_KERNEL_PATH behind the third row of the kernel source package to write the address of your own kernel version, I upgraded the kernel, the version is not the same.
Explain the make command:
make -C $ (LINUX_KERNEL_PATH) indicates jump to the kernel source directory read where the Makefile
M = $ (current_path) show returns to the current directory to continue the current Makefile.

On my machine

When such execution after make:

On my machine

Generate a lot of documents:

on my machine

4, the load module
the sudo the insmod printname.ko
. 5, the test module
dmesg kernel information see

On my machine

6, uninstall module
sudo rmmod printname

Then look at the kernel messages with dmesg, you will see written on module_exit () in the output.

On my machine

7, the output of the current process information to achieve the functions
implemented earlier function is too simple, then writing complex code for a functional module to achieve more useful features.
Teacher's advice is to do a display process of the code, do not pass a long time has been compiled, and finally concluded that the new kernel is not compatible with the old kernel function. Grub configuration changes, restart, select the old version of the kernel in the grub boot menu.

Now a successful switch back to the original kernel.
Then write code. Internet to find the code can read it to understand, but also to follow talked about before format.

In accordance with the norms have said before, first to modify the behavior of the new file name, and the third line references the kernel code to be replaced older versions of the kernel code.
After the make, sudo insmod module2.ko, after watching the kernel information dmesg.
On my machine

To achieve the functionality of the code, the validation is successful.
8, to achieve the reading process list of functions
on the basis of a code, the code changes.

You can see, I used a macro called for_each_process (), which is defined as follows:

In fact, a for loop, starting from the first PCB (called the init_task), down the next read pointer in a circle.
Modify Makefile, make, insmod, output in the following figure:

on my machine
appeared the following error:

is for_each_process () function issues
through access to information, to the current search for_each_process include / linux / sched.h file in the kernel directory ( ), found no really function definitions, but this function is found in the include / linux / sched / signal.h file, the file new9302.c introduced into the file, as shown in FIG:

after successfully make:

after sudo insmod new9302.ko, then after dmesg, the output obtained:

9. Conclusion
compile the module with the normal C language there are some differences, of innclude C language files are stored in / usr / include, the include files and modules used in / usr / src / kernel code / include in. Due to differences in the header file, it will produce some unexpected errors. Also even make through, but also wrong wrong parameter class may appear at the time of insmod. Want to make more powerful with the module, you also need to try, learn. **

Guess you like

Origin www.cnblogs.com/eosmomo/p/11775825.html