Linux Driver Development-module building

Source: Internet
Author: User

1. What the module means

Linux is built in a modular way, allowing the kernel to dynamically insert or remove code from it at runtime, which is combined with the package extension function, data, module entry function, and module Exit function.

In a separate binary image, the so-called loadable kernel module is available.

The module can be the basic kernel image as small as possible, while the new features can be easily debugged, but also can be hot-swappable (follow-up learning how to implement the hot plug function of the device, no need to delve into), and the core subsystem of the kernel is not the same, module files need to have entry points and exit points.

The difference between a module and an application:

A. Modules and library functions are similar, a module usually contains several functions and data, each function provides a specific service, decide what functions to complete is the application thing, the module is only used to provide services to the application. That is, the relationship between strategy and mechanism. Modules provide a mechanism by which applications implement policies based on the mechanisms provided by the module.

B. The registration function of the module simply registers the module with the kernel, and how the function in the module is called is the application thing.

C. Application exit is possible regardless of the release of the resource and other cleanup work (usually we will still handle it), but the module's exit function must carefully undo everything that the initialization function has done.

D. The module is linked to the kernel, which can only invoke functions that are exported by the kernel (how to export kernel functions later), and the header files used are header files in the include/directory in the kernel source code, such as:

#include <linux/init.h> ======>include/linux/init.h

E. Module does not link with any libraries

F. Module programming must carefully consider concurrency issues, which must usually be reentrant.

2. Compile, load and unload the module.

Please refer to Linux kernel stability for a very detailed description.

Documentation/kbuild/modules.txt

Because the document is in English, let me briefly describe it.

A. Run the Makefile compilation module first, and simply analyze the compile parameters of make

Make-c changes the directory to the directory where the kernel source code is located, with the top-level makefile file of the kernel

m= ' pwd ' requires the top level makefile to return to the module's source code directory before constructing the modules target.

Modules only want to obj-m the module set in the variable, that is, to tell the compilation system, this make is to compile a kernel module.

Commands for kernel reprint and uninstallation

Insmod
46 Install the module's code and data into the system's running kernel via Insmod. Once the installation is successful, the module can access the common symbols of the kernel (including functions and variables). Insmod and LD are similar, but Insmod does not
The module's disk files are modified, and only the copies in memory are modified.
Rmmod
With Rmmod uninstalling the module, be aware that only root privileges can install and unload modules. If the kernel believes that the module is still in use, it may not be uninstalled. You need to select Loadable module s Upport-->module unloading when compiling the kernel, otherwise the kernel does not support module uninstallation.
Lsmod

The LSMOD program lists the modules currently loaded into the kernel, as well as other information, such as whether the other modules are using a feature module. Lsmod obtains this information by reading the/proc/modules.

The basic knowledge of the above people have a general impression, the following example analysis, some of the origin of program learning is Hello World, then we also start from Hello World, create a simple Hello World module.

First build the module compiled makefile file

#Makefileobj-m = Hello.okern =/share/arm/linux-3.2                               #基于源代码树的模块编译, the source tree must be compiled before the module can be compiled # KERN =/lib/modules/' Uname-r '/build/                   # #如果你在x86平台的linux系统, can directly compile modules based on x86 platform all:        make-c $ (KERN) m= ' pwd ' Modulesclean:        make-c $ (KERN) m= ' pwd ' modules clean        rm-rf modules.order
Look at the first module code HELLOWORLD.C

#include <linux/init.h> #include <linux/module.h>//module must have two header files Module_license ("GPL");      The GPL agreement requires Module_author ("MJ");   Option       describes the author information module_description ("Just for Test");   Option  describes the module's static __init int hello_init (void) {    printk (Kern_emerg, "Hello kernel!\n");    PRINTK similar application of printf, use slightly different, can Baidu a bit. }static __exit void Hello_exit (void) {   printk (Kern_emerg, "Bye, Cruel world!\n");} Module_init (Hello_init); Module_exit (Hello_exit);
Usually PRINTK can not output directly to the terminal, need to look at the configuration of the terminal and your PRINTK printer. But it will be output to the log file, you can use the DMESG command to view the log message.

PRINTK ()
A. Introduction
PRINTK is a very useful way of information output in the kernel, unlike printf, where PRINTK can be appended with different log levels, which can be categorized according to the severity of the message. Such as:
PRINTK (kern_debug "Here I am:%s:%i\n", __file__, __line__);

B. Log level
PRINTK () defined in/KERNEL/PRINTK.C, the message level and the prototype declaration are all in <linux/kernel.h>. The lack of provincial level is specified in PRINTK.C.
Supported log levels:
Kern_emerg: Emergency situation
Kern_alert: Errors that require immediate attention
Kern_crit: Critical condition
Kern_err: Error
Kern_warning: Warning
Kern_notice: note
Kern_info: Informal News
Kern_debug: Debug information (redundant information)


C.PRINTK Output to User space
Two daemons syslogd and KLOGD are used to process log information. Information that is smaller than the console level is output to the console, and information that is larger than the console level is not output, but is written in/var/log/messages and/proc/kmsg. Can be viewed through DMESG.

To modify the console level:
$>echo 8 >/PROC/SYS/KERNEL/PRINTK
Or
$>klogd-c 8
/* need to first close klogd, use ps-c klogd to isolate the PID of the process, then kill)

The PRINTK function writes the message to a loop buffer of length __log_buf_len bytes, and then wakes any sleep in the syslog system call or is reading the/proc/kmsg process. When the loop buffer is full, the new content is filled in around > back to the beginning. PRINTK () can be called in interrupts.

When the PRINTK () call frequency is too high, the output can also be restricted by Printk_ratelimit ().


(7) Initialization and removal of modules
<linux/init.h> contains information related to initialization and cleanup of modules.

Initialization
A.module_init
initialization function for registering a module
Module_init (Hello_init);

B.__init
The initialization function used to decorate the module, indicating that the function is used only during initialization of the module. When the initialization is complete, the memory that this function occupies is freed.
static int __init hello_init (void)
{
...
}

C.__initdata
The initialization data structure used to modify the module.
int __initdata array[100];


Clear
A.module_exit
The purge function for registering the module
Module_exit (Hello_exit);

B.__exit
static void __exit hello_exit (void)
{
...
The purge function has no return value, and the module modified by __exit can only be used for module unloading (the compiler puts the function in a special elf segment), and other times the purge function is wrong.


In this paper, the basic introduction of Linux modules, so that we have a basic understanding of the Linux module.

The next article will explain the other features of the module


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.