Linux kernel module and driver writing (1)

zhaozj2021-02-08  219

Linux kernel module and driver writing (1)

Author: sunmoon

The Linux kernel is a whole is a structure. Therefore, add anything to the kernel. Or delete certain functions, it is very difficult. In order to solve this problem. Introduced the kernel mechanism. Thus, you can dynamically add or delete the module.

The module is not compiled in the kernel, thereby controlling the size of the kernel. However, once the module is inserted into the kernel, he is the same as the kernel. This will be a part of the system overhead. At the same time, if the module has a problem., Maybe It will bring the crash of the system.

1.1 Implementation mechanism of module:

When startup, the module is initialized by the function void inti_modules (). Because the startup is not a module. This function often regards the kernel as a virtual module.

If you need it, you call a series of functions that start with SYS, operate the module. Such as: sys_creat_modules (), sys_inti_modules (), sys_deldte_modules (), etc..

The data of some modules will be used here, in /usr/scr/linux/include/linux/module.h, interested friends can find a look

There are two ways to join: First, manual join: such as: Insmod moduleName. The other is as needed, dynamic loading module. If you perform a command: $ mout -t msdos / dev / hdd / mnt / d. The system automatically loads the FAT module to support the MSDOS file system.

1.2 module programming

Write a module must have a certain multi-process programming basis. Because you become the program is not running with a separate program. Also, because the module needs to run in kernel mode, it will encounter within and space and users. Space data exchange issues. The general data replication function cannot complete this process. Therefore, the system has entered some special functions to complete the exchange of kernel space and user space data.

These functions are: void put _user (Type Valude, Type * U_ADDR)

Memcpy_tofs ()

Wait, interested friends can carefully look at all functions, as well as their usage. What needs to be explained. The module programming Hanoi has a big relationship. If the version is not possible, the kernel module cannot be compiled, or When running this module, there is no measurement result. Such as: system crash, etc.

Understand these later. You can try to write a kernel module. For each kernel module. Must contain two functions

INT init_module () This function starts when the core is inserted, registers a certain function function in the kernel, or uses his code instead of the content of some functions in the inner and in the middle (estimated that these functions are empty). So, internal and can be safe Uninstall. (Personal Guess)

INT CLEANUP_MODULE () When the kernel module is met, call. Clear the module from the kernel.

Like other programming tutorials, we give an example of Hello World.

/ / / * The Program Runing Under Kernel Mod and It Is A Module * / #include "linux / kerner.h" #include "llinux / module.h" / * pross the config_modversions * / # if config_modversions == 1 #define modversions #include "" Linux / modversions.h "#END if / * the init function * / int init_module () {Printk (" Hello World! / n '); Printd ("I Have Runing In a kerner mod @ !! / n "); return 1;} / * the distory function * / int cleanup_module () {Printk (" I Will Shut Down Myself in kernerl mod / n); Retutn 0;

}

Such an example is completed. We also write an example of Makefile to apply for our large program. Take the content of the Makfile file # a Makefile for a Module cc = gcc modcflags: = -wall _dmodule -d_kernel_ - Dlinux hello.o hello.c /usr/inculde?linux/Version.h cc $ (modcflags) 0C Hello.c

Echo The Module Is Complie Completely

Then you run the make command to get the Hello.o module. Run

$ INSMOD HELLO.O Hello World! I Will Shut Down Myself in Kernerl Mod $ LSMOD Hello (Unused) ... $ RMOD

I Will Shut Down Myself in Kernerl MOD

This allows your module to insert and delete.

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

New Post(0)