Linux Kernel module for Driver Programming

Source: Internet
Author: User
Article Title: Linux kernel module for Driver Programming. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Linux device drivers are part of the kernel. A Linux kernel module can be compiled and loaded in two ways:

(1) directly compile the program into the Linux kernel and load it along with the Linux Startup;

(2) compile it into a module that can be loaded and deleted and use insmod to load (modprobe and insmod commands are similar, but dependent on the relevant configuration files). rmmod deletes the module. This method controls the kernel size. Once a module is inserted into the kernel, it is the same as other parts of the kernel.

The following is an example of a kernel module:

# Include // Header files required by all modules
# Include // Init & exit macros
MODULE_LICENSE ("GPL ");
Static int _ init hello_init (void)
{
Printk ("Hello module init \ n ");
Return 0;
}

Static void _ exit hello_exit (void)
{
Printk ("Hello module exit \ n ");
}

Module_init (hello_init );
Module_exit (hello_exit );

After analyzing the above program, it is found that a Linux kernel module must contain module initialization and module uninstallation functions. The former runs during insmod and the latter runs during rmmod. The initialization and uninstallation functions must be defined before the macros module_init and module_exit are used. Otherwise, a compilation error occurs.

MODULE_LICENSE ("GPL") in the program is used to declare the module license.
    
If you want to compile the above program into a module loaded and deleted during runtime, the compilation command is:

Gcc-D__KERNEL _-DMODULE-DLINUX-I/usr/local/src/linux2.4/include-c-o hello. o hello. c

It can be seen that the gcc command-d1_kernel _-DMODULE-DLINUX parameter is required for compiling the Linux kernel module. -The I option follows the Include directory path in the Linux kernel source code.

The following command loads the hello module:

Insmod./hello. o

The following command completes the opposite process:

Rmmod hello

To directly compile the file into the Linux kernel, copy the source code file to the corresponding path of the Linux kernel source code and modify the Makefile.
We need to add some basic knowledge about Linux kernel programming:

   Memory

In Linux kernel mode, we cannot use user-mode malloc () and free () functions to apply for and release memory. During kernel programming, the most common memory application and release functions are kmalloc () and kfree () declared in the include/linux/kernel. h file. Their prototype is:

Void * kmalloc (unsigned int len, int priority );
Void kfree (void * _ ptr );

The priority parameter of kmalloc is usually set to GFP_KERNEL. If you apply for memory in the interrupted service program, you must use the GFP_ATOMIC parameter because using the GFP_KERNEL parameter may cause sleep, it cannot be used in non-process context (sleep is not allowed during interruption ).

Because the kernel mode and user mode use different memory definitions, the two cannot directly access the memory of the other party. In Linux, users and kernel-state memory interaction functions should be used (these functions are declared in include/asm/uaccess. h ):

Unsigned long copy_from_user (void * to, const void * from, unsigned long n );
Unsigned long copy_to_user (void * to, void * from, unsigned long len );

The copy_from_user and copy_to_user functions return the number of bytes that cannot be copied. Therefore, if the copy is successful, the return value is 0.

The put_user and get_user defined in include/asm/uaccess. h are used for single-Value Interaction (such as char, int, and long) between the kernel space and the user space ).

Here we only show the details about memory management in the kernel and more details about Linux memory management, we will give a more in-depth introduction to memory and I/O operations in section 9th.

   Output
 
In kernel programming, we cannot use the printf () function in the user State C library function to output information, but can only use printk (). However, the printk () function in the kernel is not designed to communicate with users. It is actually a Log Mechanism of the kernel, which is used to record the log information or give a warning.

Each printk has a priority. The kernel has a total of eight priorities, which have corresponding macro definitions. If no priority is specified, the kernel selects the default priority DEFAULT_MESSAGE_LOGLEVEL. If the priority number is smaller than the int console_loglevel variable, the message is printed to the console. If the syslogd and klogd daemon are running, messages will be appended to the/var/log/messages file regardless of whether they are output to the console. Klogd only processes kernel messages. syslogd processes messages from other systems, such as applications.

   Module Parameters

In the 2.4 kernel, the macro MODULE_Parm (var, type) defined in include/linux/module. h is used to pass command line parameters to the module. Var is the variable name that accepts the parameter value. type is a string in the following format: [min [-max] {B, h, I, l, s }. Min and max are used to indicate the number range of array elements that can be input when the parameter is of the array type; B: byte; h: short; I: int; l: long; s: string.

When loading the kernel module, you can pass some parameters to the module:

Insmod modname var = value

If no parameter is specified, var uses the default value defined in the module.

Related Article

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.