There are a lot of things about the kernel driver on the network, but the online things are still a bit general. After reading it, I forget it. I still need to write it down or write it on the book, it is better to build a concept by yourself. I read the things in this book and write down what I think is good. I have used them from time to time and strengthened my memory.
1. Concepts of Kernel ModulesThis section describes the differences between the kernel module and applications. Although there is a lot of content, it is useful. 1. the kernel module is some code that can be loaded and executed by the operating system kernel as needed, and can be detached when not needed. This is a good function. It extends the kernel function of the operating system, but does not need to restart the system. It is a dynamic loading technology. Features: dynamic loading, loading at any time, unloading at any time, extended functions 2. Loading of kernel modules: Just register yourself with the Linux kernel in advance for future requests. That is to say, it tells the kernel that it has new features, instead of using (executing) immediately, and the application starts to execute after loading. 3. No external function libraries can be used to compile the code of the kernel module. Only functions exported by the kernel can be used. Applications are used to using external library functions and link the program with the library functions during compilation. For example, compare printf () and printk () 4. The kernel module code runs in the kernel space, while the application is in the user space. The running of applications forms new processes, but kernel modules generally do not. Each time an application executes a system call, the Linux execution mode switches from user space to kernel space.
2. kernel module frameworkAt least two entry points
* Module Loading Function
* Module uninstallation FunctionThe module_init () and module_exit () macros are usually used to define the loading and uninstalling functions of the declared module, which are defined in linux2.6.x/include/Linux/init. h. Content: # define module_init (x) _ initcall (x) // executed during kernel startup or module loading # define module_exit (x) _ exitcall (X) // execute * When the module is uninstalled. Each module can have only one module_init and one module_exit. The source code of a simple kernel module. the C format is as follows: Kernel # include <Linux/module. h> // All kernel modules must contain this header file # include <Linux/kernel. h> // This # include <Linux/init. h> // some initialization functions such as module_init () module_license ("GPL"); // module license Declaration
Static int hello_init (void) {} static void hello_exit (void) {} module_init (hello_init); module_exit (hell0_exit); these two functions are just an internal declaration, or an internal loading method. When it comes to low or internal modules, the kernel does not load modules. To really load modules, run the command insmod to uninstall the modules and use rmmod. --------------------------------------------------------------------------------
3. Compile and load kernel modules of the 2.6 SeriesHere we will talk about kernel module compilation, 2. In version 6, kbuild is introduced to unify the compilation of the external kernel module and the compilation of the kernel source code tree. The Directory of the kernel source code tree contains two files: kconfig (config. In version 2.4) and makefile. The kconfig distributed to each directory constitutes a distributed Kernel configuration database. Each kconfig describes the Kernel configuration menus related to the directory source files. When you configure make menuconfig (or xconfig) in the kernel, read the menu from kconfig and save it to the Kernel configuration file of. config. When the kernel is compiled, the main makefile calls this. config, and the user's choice is known. * As described above, kconfig corresponds to the Kernel configuration menu. If you want to add a new driver to the kernel source code, you can modify kconfig to select the driver. If you want to compile the driver, you need to modify the makefile.
There are two types of files to be modified when adding a new driver (note not only two files)
* Kconfig
* MakefileTo know how to modify these two types of files, you must know the syntax structure of the two types of files.
3.1 kconfigEach menu has a keyword identifier, and the most common is the config Syntax: configsymbol is a new marked menu item, options is the attributes and options under the new menu item. The options section includes:
1. Type Definition:Each config menu item must have a type definition, bool boolean type, tristate three states: built-in, module, Remove string, Hex hexadecimal, integer, such as config hello_modulebool "Hello test module" bool type can only be selected or not selected, the menu items of the tristate type include multiple options for compiling to the kernel module. If you select to compile to the kernel module. generate a config_hello_module = m configuration in config. If you select built-in, the configuration will be directly compiled into the kernel. generate a config_hello_module = y configuration in config.
2. Dependency-defined depends on or requiresWhether the menu appears depends on another definition of config hello_modulebool "Hello test module" depends on arch_pxa. This example shows that the menu item hello_module is only valid for the XScale processor.
3. Definition of helpJust add the help keyword help or --- help ---
3.2 kernel makefile
In the linux2.6.x/documentation/kbuild directory, you can learn more about Kernel makefile. The 2.6 kernel makefile consists of five components:
Makefile at the top layer
The current configuration file of the. config kernel, which is part of the fixed-layer makefile during compilation.
ARCH/$ (ARCH)/makefile related to the architecture
S/makefile. * some general makefile rules
The kbuild makefile directory contains about 500 files. during compilation, the source code is compiled into a module or a kernel based on the macro definition passed by the upper makefile and other compilation rules.
The makefile on the top layer reads the content of the. config file, and is responsible for building the kernel and module. Arch makefile provides supplementary information about the architecture. The makefile in the S directory contains all the definitions and rules required to build the kernel based on the kbuild makefile. (Where. the Config content is the result of the kconfig file configuration when making menuconfig. As mentioned above) for developers and users of most kernel modules or device drivers, the kbuild makefile files based on the kbuild architecture under the directories of different layers are most often exposed. The main parts are:
1. Target definition,The target definition is used to define the content to be compiled as a module and the content to be compiled and linked to the kernel. The simplest is only one line, such
OBJ-y + = Foo. o
It indicates that Foo. o needs to be compiled by the foo. C or Foo. s file and linked to the kernel, while obj-M indicates that the file should be compiled as a module. Objects in the obj-x format except y and M will not be compiled. Since it can be compiled into modules or into the kernel, a more common practice is to determine the file Compilation Method Based on the config _ variable of the. config file, such:
OBJ-$ (config_hello_module) + = Hello. O, which has been described in 7.2.3.1.
In addition to objects in the obj-format, there are also targets such as Lib-y library and hostprogs-y Host Program, but they are basically applied in specific directories and scenarios.
2. Multi-ObjectiveA kernel module is compiled from multiple source files, which are different from makefile. Define the component file of the module by adding the-objs suffix or-y suffix. For example:
OBJ-$ (config_ext2_fs) + = ext2.o
Ext2-y: = balloc. O bitmap. o
Ext2-$ (config_ext2_fs_xattr) + = xattr. o
The module name is ext2. The ext2.o to ext2.ko files are generated by the final links of the target files balloc. O and bitmap. O. Whether the files include xattr. O depends on the configuration of the Kernel configuration file. If the value of config_ext2_fs is Y, the ext2.o generated during this process will be linked to the built-in.o and eventually linked to the kernel. Note that the directory where the kbuild makefile is located should not contain source files with the same module name, such as ext2.c/ext2.s, or written as-objs:
OBJ-$ (config_isdn) + = ISDN. o
ISDN-objs: = isdn_net_lib.o isdn_v110.o isdn_common.o
3. Directory IterationOBJ-$ (config_ext2_fs) + = ext2/
If the config_ext2_fs value is Y or M, kbuild will include the ext2 directory in the target of the next iteration, but its role is limited to this, the files under the ext2 directory are determined by whether to compile the files as modules, link them to the kernel, or by the contents of the makefile under the ext2 directory.4. Different module compilation methodsWhen compiling a module, you can place the module in the code tree and compile your module using make modules. You can also place the module-related file directories outside the code tree, use the following command to compile the module:
Make-C path/to/kernel/src M = $ PWD modules
-C: Specifies the root directory of the kernel source code. $ PWD or 'pwd' is the environment variable of the current directory and tells kbuild to return to the current directory to execute the build operation.5. Module InstallationWhen you need to install the module to a non-default location, you can use install_mod_path to specify a prefix, such:
Make install_mod_path =/Foo modules_install
The module will be installed in the/Foo/lib/modules directory.
Note 2. 6 and 2. The compiling result of Version 4 is different. The kernel module is in version 2. 6 is the. Ko suffix, replacing 2. 4. The. O suffix to differentiate the kernel module from the common target file. (Better) [from: http://www.eefocus.com/manu20/blog/08-07/152597_cfc44.html#articletop]