Linux Device driver Notes (i) Introduction to device drivers

Source: Internet
Author: User

<: The role of device drivers

From one point of view, the role of a device driver is to provide a mechanism, not a policy. When writing drivers, programmers should pay particular attention to the following basic concept: When writing kernel code to access hardware, do not impose any specific policy on the user. Because different users have different needs, the driver should handle the problem of how to make the hardware available, and how to use the hardware problem left to the upper application.

looking at a driver from another perspective, it can also be seen as an application and an actual device a software layer.

In general, the driver program design is mainly to consider the following three factors: provide users with as many options as possible, write the driver to take the time and try to keep the program simple and not error-ridden.

< two;: Kernel function partitioning

UNIX systems support multi-process concurrency, and each process requests system resources. The kernel is responsible for processing all of these requests, and the kernel functions can be divided into the following parts depending on the completion of the task:

1. Process management: Responsible for creating and ecstasy processes, and handling connections between them and the outside world. The kernel process management activity is the abstraction of multiple processes on a single or multiple CPUs.

2. Memory Management: Memory is one of the main resources of the computer, and the strategy to manage the memory is a key factor to determine the system performance.

3. File system: The kernel constructs a structured file system on unstructured hardware, and file abstraction is widely used throughout the system.

4. Device control: Almost every system operation will eventually be mapped to a physical device.

5. Network functions: Network functions must also be managed by the operating system, the system is responsible for the application and network interface between the transmission of packets, and according to the implementation of network activity control program. In addition, all routing and address resolution issues are handled by the kernel.

Loadable Modules: Linux has a good feature: the features provided by the kernel can be extended at run time. Code that can be added to the kernel at run time is called a "module." The Linux kernel supports several types of modules, including but not limited to device drivers. Each module consists of the target code, which can be used to connect the module to the running kernel using the INSMOD program, or to remove the connection using the Rmmod program.

< three;: Classification of equipment and modules

The Linux system divides the device into three basic types: a character device, a block device, and a network interface.

1. Character device: Character device drivers typically implement at least open, close, read, and write system calls. A character device can be accessed through a file system node. The only difference between these device files and normal files is that access to ordinary files can be moved back and forth, and most character devices are a data channel that can be accessed sequentially.

2. Block devices: Similar to character devices, block devices are also accessed through the file system nodes in the/dev directory. Can accommodate file systems on block devices. The difference between a block device and a character device is simply the way the kernel manages the data, which is the software interface between the kernel and the driver, which is transparent to the user. In the kernel, the block driver has a completely different interface than the character driver.

3. Network interface: The network interface is driven by the network subsystem in the kernel, responsible for sending and receiving packets, but it does not need to know how each transaction maps to the actual transmitted packets. The network driver does not need to know the information about each connection, it simply processes the packet. The communication between the kernel and the network device driver is completely different from the communication between the kernel and the character and the block driver, and the kernel calls a set of functions related to packet transport rather than read, write, and so on.

In addition to device drivers, some of the other functions in the kernel are also modular, such as file systems. A file system type determines how data is organized on a block device to represent the tree in which directories and files are formed. The file system is not a device driver because there is no actual physical device associated with the way this information is organized. Instead, the file system type is a software driver that maps the underlying data structure to a high-level data structure, determines how long the file name can be and what information is stored in the catalog item, and so on.

< four;: Security issues

1. All security checks in the system are carried out by kernel code, and if the kernel has a security vulnerability, the entire system will have a security vulnerability. When running the officially released kernel, privileged code can only be used by superuser or intruders who become superuser.

2. Driver writers should try to avoid implementing security policies in code. Security policy issues are best implemented under the control of the system administrator, at the top level of the kernel. Driver writers should also avoid introducing security flaws for their own reasons.

3. Any input from the user process can only be used after the kernel has been rigorously validated. Also be careful with uninitialized memory: any memory that is obtained from the kernel must be cleared 0 or otherwise initialized before it is supplied to the user process or device, or information disclosure may occur.

4. The software obtained from third parties should be used with care, especially when it is related to the kernel, because the source code is open and everyone can modify and recompile it.

The 5.Linux kernel can also be compiled to not support module mode, thus shutting down any module-related security vulnerabilities. It is possible to disable the transfer of kernel modules after system startup through the power mechanism.

< five;: Version number

1. First, each software package in a Linux system has its own release number, and there is often a dependency between them. Almost all distributions now have a package manager that allows the upgrade package to be allowed after verifying that the dependencies between packages are met. If you encounter any version-related issues, you can refer to the kernel source file documentation/changes to resolve.

2. For the kernel, the even numbered kernel version is a stable version for the official release, while the odd numbered version

is a snapshot of the development process.

< six;: License Terms

Linux follows the GNU General Public License (GPL), which allows anyone to republish or even sell products protected under the GPL, provided that the recipient of the product obtains the source code and has the same rights. If you want to read the original license, you can find it in the copying file in the top-level directory of the system's kernel source tree.

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.