[Driver] linux Device Driver & #183; getting started

Source: Internet
Author: User

The Driver is also known as the Device Driver.

In the modern computer architecture, the operating system does not directly deal with hardware, but communicates with hardware through drivers.

 

On the same computer, although the devices are the same, the drivers vary greatly due to different operating systems. However, no matter what is similar, it can be summarized into the following three points:

    This is the most basic function of the driver. The initialization identifies the device through the bus, accesses the device register, configures the device's local port as needed, and sets the interrupt.

  • The Device Driver provides the operating system with a common software interface for the device. For example, the hard drive provides the operating system with interfaces such as reading/writing disk blocks and addressing, no matter which brand the hard drive provides the same interface to the operating system.

  • The processing capability of modern computers is getting stronger and stronger. The operating system has a virtual device driver that can simulate operations on real devices. For example, the virtual Printer Driver provides the printer interface to the operating system, printing can still be performed without a printer.

 

The Linux kernel module is

Kernel modules can be used to expand kernel functions. kernel modules are usually used for device drivers and file systems. If you do not have a kernel module, you need to add features to the kernel to modify the code, recompile the kernel, and install the new kernel. This is not only tedious but also error-tolerant and not easy to debug.

 

The kernel module is an application, but different from common applications, the difference is:

    The kernel module runs in the kernel space and can access almost all the hardware and software resources of the system. The general application runs in the user space and the accessible resources are limited. This is also the main difference between kernel modules and common applications. Because the kernel module can obtain the same permissions as the operating system kernel, you should pay special attention to programming. A small error that may be seen in the user space will cause the system to crash.

  • Common applications have clearly defined functions to accomplish a specific purpose. The kernel module serves other kernel modules and applications and generally provides general functions.

  • The kernel module can only call functions provided by the kernel. Access to other functions may cause running exceptions. Normal applications may call functions other than themselves, and they will run as long as they can be correctly connected.

     

 

Program concurrency.

Due to the Code reentrant feature, you must consider that the data structure is not damaged by other threads in a multi-threaded environment, and the shared data should be protected by locking. A common error for driver programmers is that it is difficult to debug a piece of code that does not contain concurrency, and data is damaged.

Applications use virtual memory, which has a huge address space and can allocate large blocks of memory in the application. The memory available for the kernel module is very small, which may be as small as a memory page (4096 bytes ). Pay attention to the memory allocation and usage when writing the kernel module code.

Therefore, a kernel module includes at least two functions: loading and detaching. In linux 2.6 series kernels, The module_init () macro can call the initialization function of the kernel module when loading the kernel module. module_exit () the macro can call the unmount function of the kernel module when uninstalling the kernel module.

Static int _ init init_func (void); // initialize the static void _ exit exit_func (void) function; // clear the Function

The names of these two functions can be defined by the user, but the specified return value and parameter format must be used.

 

 

The kmod module communicates with the user-mode kmodule module to obtain information about the kernel module.

Both the insmod and modprobe commands can load a kernel module.

    When the command loads the kernel module, it does not check whether the symbols of the kernel module have been defined in the kernel.

  • Check not only the kernel module symbol table, but also the module dependency.

In addition, the Linux kernel can notify the user-mode modprobe to load modules through the kmod mechanism when a module needs to be loaded.

 

Generally, the kernel output symbol is stored in the first module structure in the kernel module list. The insmod command loads the kernel module into the virtual memory, and uses the kernel output symbol table to modify the resource address of the kernel functions not parsed In the loaded module.

Because the kernel module is in the kernel state, address conversion is required to access user State resources. After applying for a space, insmod copies the kernel module to the new space, adds the module to the end of the kernel module list, and sets the module flag as UNINTIALIZED, indicating that the module has not been referenced.

 

 

When a kernel module is referenced by other modules, its reference counter will increase by 1. when uninstalling a module, You need to determine whether the counter value referenced by the module is 0. If it is 0, You Can unmount the module. Otherwise, you can only reduce the module count by 1.

The super user can use the rmmod command to uninstall the specified module.

In addition, the kernel kmod mechanism regularly checks the reference counter of each module. If the reference counter value of a module is 0, kmod uninstalls the module.

 

Or the most classic "Hello World! "For example.

/* Kernel module: ModuleHelloWorld. c */# include <linux/init. h> # include <linux/module. h> # include <linux/kernel. h> MODULE_LICENSE ("GPL"); MODULE_AUTHOR ("Mystety");/* init function */static int _ init hello_init (void) {printk (KERN_ALERT "(init) hello, World! \ N "); return 0;}/* exit function */static void _ exit hello_exit (void) {printk (KERN_ALERT" (exit) Bye-bye, Mystery! \ N ");} module_init (hello_init); module_exit (hello_exit );

 

To compile the kernel module, you need to create a Makefile to use the kernel header file, because the kernel module has a strong dependency on the kernel version.

I use Ubuntu as the ghost system,

sudo apt-get install linux-source

After kernel is installed, it is in the same directory as ModuleHelloWorld. c.

ifneq ($(KERNELRELEASE),)    obj-m := ModuleHelloWorld.oelse    KERNELDIR := /lib/modules/$(shell uname -r)/build    PWD := $(shell pwd)default:    $(MAKE) -C $(KERNELDIR) M=$(PWD) modulesendif

 

Program

By setting the KERNELDIR and PWD environment variables, and then compiling the current file through the kernel script to generate the kernel module File.

After the external Makefile is created, enter""Press enter to compile the kernel module.

 

After compile is complete, the ModuleHelloWorld. ko kernel module is generated,

The output information of the hello_init () function is displayed during the loading process.

After kernel is loaded successfully, you can use

When the module is uninstalled, the kernel will call the kernel's unmount function to output the content of the hello_exit () function.

After the module is uninstalled, use it. If there is no output, it indicates that the HelloWorld kernel module has been uninstalled successfully.

lsmod | grep ModuleHelloWorld

 

Drivers often need to provide one or more parameters during loading. The internal module provides the ability to set parameters.

PassOdule_param ()A macro can set a parameter for the kernel module.

Definition:

Where ,;

# Include <linux/init. h> # include <linux/module. h> # include <linux/kernel. h> MODULE_LICENSE ("GPL"); MODULE_AUTHOR ("Mystety"); static int initValue = 0; // module parameter initValue = <int value> static char * initName = NULL; // module parameter initName = <char *> module_param (initValue, int, S_IRUGO); module_param (initName, charp, S_IRUGO ); /* init function */static int _ init hello_init (void) {printk (KERN_ALERT "initValue = % d init Name = % s \ n ", initValue, initName); // print the parameter value printk (KERN_ALERT" (init) Hello, World! \ N "); return 0;}/* exit function */static void _ exit hello_exit (void) {printk (KERN_ALERT" (exit) Bye-bye, Mystery! \ N ");} module_init (hello_init); module_exit (hello_exit );

In the original code, recompile and load the module with parameters.

The output result shows that the kernel module parameters are correctly passed to the program.

 

Driving is not as difficult as it is,

 

 

This article from the "Cheng Peng Zhiyuan" blog, please be sure to keep this source http://infohacker.blog.51cto.com/6751239/1218461

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.