LINUX kernel module and the preparation of the driver

xiaoxiao2021-03-05  28

This article is a good writing, original is not me, but some mistakes in the original text have been modified. It is helpful for me to drive beginners.

The Linux kernel is a whole structure. Therefore, add anything to the kernel. Or delete some functions, it is very difficult. In order to solve this problem. Introduced the kernel mechanism. Thus, you can dynamically add or delete the module. Module is not Compiled in the kernel, thereby controlled the size of the kernel. However, once the module is inserted into the core, he is the same as the kernel. This will be a part of the system overhead. At the same time, if the module has problems., May bring The crash of the system. 1.1 module implementation mechanism: When startup, the module is initialized by the function void inti_modules (). Because the startup does not have a module. This function often regards the kernel itself as a virtual module. If the system needs, Call a series of functions that start with SYS, operate to modules. Such as: sys_creat_modules (), sys_intte_modules (), SYS_DELDTE_MODULES (), etc. Here, some modules are used in the structure, in / usr / scr / linux / include /Linux/module.h, interested friends can find two ways to add a picture: First, manual join: such as: Insmod modulename. The other is as needed, dynamic loading module. Such as you Command: $ mount -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 change The program is not run in a separate program. In addition, because the module needs to run in kernel mode, there will be problems in internal and space and user space data exchange. 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 spatial data. These functions are: void put_user (type value, type * u_addr), memcpy_tofs (), etc., interested friends can carefully Take a look at all functions and their usage. What you need is. The module programming Hanoi core has a great relationship. If the version is different, you may cause the kernel module to be able to compile, or when you run this module, you can't Results. Such as: system crash, etc. After you understand these. You can try to write kernel modules. For each kernel module. Must contain two functions: int init_module () This function is started when the core is inserted. Register a certain function function in the kernel, or use his code instead of some of the contents of some functions (estimated that these functions are empty So, therefore, and can be securely uninstalled. (Personal Guess) INT CLEANUP_MODULE () When the kernel module is called. Clear the module from the kernel. Like other program design tutorials, we give a Hello World Example / / / * The program in the program / / / * 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 kener 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 complete. We also write a Makefile example to apply the application of 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 a Hello.o. Run $ Insmod Hello. o Hello World! I Have Runing in a kener mod @ !! $ rmmod i will shut down myself in kernerl MOD So your module can insert and delete. Most drivers in Linux are in the form of modules. Written. These driver source codes can be modified to the kernel, or they can compile them into module situations. Dynamically load it when needed. A typical driver can be divided into such parts: 1. Register device The system is initiated, or when the module is loaded, the device must be registered to the corresponding device array, and return to the main drive number of the device, for example, calling REFISTER_BLKDEC () to the fast device to add the device to the array BLKDEV. And get the device Number. And use these device numbers to index this array. For characters drive devices, you want to use module_register_chrdev () to get the driver number of the apparatus. Then all calls to this device use this device number to implement 2, define Function Functions For each drive function. There are some functional functions that are closely related to this device. That most commonly used block devices or character devices have, there is such as Open () read () write () octrol () This kind of operation. When the system sends these calls. The automatic use of a specific module will be automatically used. For a specific device. The function of the above system calls is certain. Such as: In the block drive device. When the system attempts to read this device (ie, when calling read ()), the function in the driver is run. When the new device is opened, the device driver is called Device_Open () () this Function. 3, when the unloading module is not in this device, To uninstall him. Mainly from / proc to cancel this device special file. You can implement a specific function. Let's include a framework of a character device driver. To illustrate this process. / * A module of a character device * / / * Some include Files * / #include "param.h" #include "user.h" #include "tty.h" #include "dir.h" #include "fs.h" / * the include files modules need * / #include "linux / kernel.h" #include "linux / module.h" #if config_modbs == 1 Degine modbsion #include "linux.modversions.h" #ENDIF #DIFINE DEVICENAMEDEVICE / * The init function * / int INT_MODULE () {int Tag = module_register_chrdev (0, MyDevice, & FOPS); if (tag <0) {Printk ("The Device Init IS Erro! / N"); Return 1;} Return 0;

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

New Post(0)