Kernel modular programming

catalogue

Introduction to the concept of kernel module

Dynamic modular programming framework

Module loading function

Module unloading function

Open source protocol and declaration of module

Module compilation Makefile template

Programming steps of single module

1 write the kernel module file test c

2. Write Makefile

3 compile and generate test ko

Introduction to the concept of kernel module

The overall structure of Linux kernel has been very large, including many components. For our engineers, there are two ways to include the required functions into the kernel.
One: compile all functions into the Linux kernel (static loading module).
Second: compile the required functions into modules and add them dynamically when needed (dynamically load modules).
Analysis of advantages and disadvantages of the above two methods:
The first: advantages: there will be no version incompatibility, and strict version inspection is not required. Disadvantages: the generated kernel will be large; To add new functionality to an existing kernel, compile the entire kernel
Second: advantages: the module itself is not compiled into the kernel, thus controlling the size of the kernel; Once the module is loaded, it will be exactly the same as other parts. Disadvantages: there may be incompatibility between kernel and module versions, resulting in kernel crash; This will result in low memory utilization.

Use the common commands of the module in the super terminal MobaXterm

#insmod xxx.ko / / add modules to the kernel
#rmmod xxx.ko / / uninstall the module from the kernel
#lsmod / / view the modules installed in the kernel
#modprobe / / load the specified individual module or a group of dependent modules. modprobe will be based on depmod
//The resulting dependencies determine which modules to load. If an error occurs during loading, the whole will be unloaded in modprobe
//Module of the group. Dependencies are obtained by reading / lib / modules / 2.6 xx/modules. Dep got it. And the document is passed
//Established by depmod.
#modinfo //View module information. Usage: #modinfo XXX ko
#Tree – a / / view the entire tree structure of the current directory

Dynamic modular programming framework

Module loading function

static int __init XXX_init(void)//Load function
{
    //Register device hello
	return 0;
}
moudle_init(XXX_init); // When #insmod or #modprobe is called, it can be regarded as the entry of the module

When #insmod XXX Ko will call module_ XXX modified by init_ Init function to complete some initialization operations (xxx)
The module loading function of Linux kernel is generally used__ Init identification declaration is used to tell the compiler that related functions or variables are only used for initialization, and this memory will be released after initialization. XXX_ The return value of init function is an integer number. If the execution is successful, it will return 0. If the initialization fails, it will return the error code. The error code in Linux kernel is negative, which is in < Linux / errno h> Defined in

Module unloading function

static void __exit XXX_exit(void)
{ }
moudle_exit(XXX_exit); // Called when executing #rmmod

This function is executed when the module is unloaded with the rmmod or modprobe command. Complete the reverse of loading. The module unloading function and module loading function realize the opposite functions, mainly including:
1) If the module loading function registers XXX, the module unloading function unregisters XXX
2) If the module loading function dynamically allocates memory, the module unloading function releases the memory
3) If the module loading function applies for hardware resources, the module unloading function releases these hardware resources
4) If the module loading function opens hardware resources, the module unloading function must close these resources

Open source protocol and declaration of module

MODULE_LICENSE("GPL");

Introduction to GPL agreement: the full name is GNU General Public License. GPL guarantees the rights of all developers (developers have copyrights and patents), and provides users with sufficient rights to copy, distribute and modify:
1) Users are free to copy the software anywhere
2) Users can freely distribute source code or binary files
3) Users can make money, but they must provide customers with the GNU GPL license agreement of the software, so that they can know that they can get the software for free from other channels and the reason for your charge
4) The user is free to modify the software. If the user wants to add or delete a function, it's OK. If you want to use part of the code in other projects, it's OK. The only requirement is that the project using this code must also use GPL protocol

Non mandatory module declaration

MODULE_AUTHOR       // Statement author
MODULE_DESCRIPTION  // A brief description of the module
MODULE_VERSION      // Declare the version of the module
MODULE_ALIAS        // Alias of the module
MODULE_DEVICE_TABLE // Tell the user space the devices supported by this module

Module compilation Makefile template

# Makefile 2.6 template
#When the target of make is all, - C $(KDIR) indicates to jump to the kernel source code directory and read the Makefile there;
#obj-m means that the file hello O compile as a "module", which will not be compiled into the kernel, but will generate an independent "hello.ko" file;
#M=$(PWD) indicates to return to the current directory and continue reading in and executing the current Makefile.
obj-m += hello.o
#When using KDIR, you should modify it to the top-level directory of your own kernel
KDIR:= /3linux/linux-3.5
all:
	make -C $(KDIR) M=$(PWD) modules
#-The function of option C is to transfer the current working directory to the location you specify.
#The "M =" option is used to compile an external module based on a kernel,
#You need to add "M=dir" in the make modules command,
#The program will automatically find the module source code in the dir directory you specify, compile it and generate KO files.
clean:
	rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.markers *.order
#Use the make command to compile the module. Because the makefile file cannot correctly handle path names with spaces,
#Make sure there are no spaces in the path
#This command is an extension of the make modules command. The function of the - C option is to transfer the current working directory to the specified directory, that is, the (KDIR) directory. The program finds the module source code in the (shellpwd) current directory, compiles it and generates it ko file

Programming steps of single module

1 write the kernel module file test c

#include<linux/kernel.h>
#include<linux/module.h>
static int __init test_init(void)  //Load function
{
    printk("this is test file!\n");
	return 0;
}
static void __exit test_exit(void) //Unload function
{
    printk("exit!\n");
}
module_init(test_init); //Declare load function
module_exit(test_exit); //Declare unload function
MODULE_LISENSE("GPL"); //Declare open source agreement

2. Write Makefile

obj-m += test.o
KDIR:=/root/tiny4412/linux-3.5/

all:
	make -C $(KDIR) M=$(PWD) modules
clean:
	rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.markers *.order

3 compile and generate test ko

Enter the command make in the virtual machine terminal to generate the module file test ko. Test Ko is copied to the root file system and executed in the terminal of the development board

insmod test.ko //Load test module
lsmod          //View currently mounted modules
rmmod test.ko  //Uninstall test module

Keywords: Linux Operation & Maintenance server

Added by Morbius on Sat, 29 Jan 2022 15:01:36 +0200