"Linux kernel Design and implementation" Chapter 17th reading notes

Source: Internet
Author: User

17th Chapter Equipment and modules

One or four core components

    • Device Type : The classification used in all Unix systems in order to unify the operation of a common device.
    • Module : A mechanism for loading and unloading target codes on demand in the Linux kernel.
    • Kernel objects : The kernel data structures support simple object-oriented operations, and also support the maintenance of parent-child relationships between objects.
    • Sysfs : Represents a file system for the device tree in the system.

Second, the type of equipment

Linux systems, the device is divided into the following three types of

    • Block Devices -- addressable , addressable in blocks, with different block sizes depending on the device; Support Relocation (seeking) operation, that is, random access to data block device example has a hard disk, Blu-ray dispatch, but also like Flash storage devices; accessed through special files called "Block device Nodes".
    • The character device -- is not addressable, only provides streaming access to the data; examples include keyboards, mice, printers, and most pseudo-devices. accessed through a special file called a "character device node." unlike block devices, applications interact with character devices by directly accessing device nodes.
    • Network Equipment -- network devices break the Unix "Everything is a file" design principle, it is not accessed through the device node, but through the socket API such a special interface to access.

Third, the module

1. Concept of the module:

The Linux kernel is modular, allowing the kernel to dynamically insert or remove code from it at runtime. the code (including related subroutines, data, function population, and function exits) is combined in a separate binary image, known as a loadable kernel module, or simply as a module .

2. Benefits of support modules:

The basic kernel image can be as small as possible, because optional features and drivers can be provided in the form of modules. The module allows us to easily remove and reload kernel code, as well as facilitate debugging efforts. And when hot-swappable new devices, you can load new drivers with commands.

3. Calling Module_init () is actually not a real function call, but a macro call, and its only argument is the initialization function of the module. All initialization functions of the module must conform to the following form:int my _ init (void);

4. Building the module:

The first step in the build process is to decide where to manage the module's source code.
How to build:

    • placed in the kernel code tree, Add the module source code to the kernel source tree, as a patch or eventually merge your code into the formal kernel code tree.
    • Placed outside the kernel code.

5. Install the module:

to install the compiled module into the appropriate directory, Make modules Install, Typically, you need to run with root privileges.

6. Generate Module dependencies:

  Generate kernel dependency information, the root user can run the command

7. Load module:

    • The simplest way to load a module is through the Insmod command-- Insmod Module.ko
    • The unload module uses the Rmmod command, which is shipped as root-- Rmmod Module
    • via Modprobe plug-in module, need to run as root-- modprobe module [module parameters]

The parameter module specifies the modules that need to be loaded, and the subsequent parameters pass into the kernel when the module is loaded.

8. Module Parameters:

All macros need to contain a header file.

9. Export the symbol table:

in the kernel, exporting kernel functions requires the use of special instructions: Export_ SYMBOL () and EXPORTSYMBOLGPL ()

Four, equipment model

1. Unified Device Model:

The device model provides an independent mechanism to represent the device and describe its topology in the system.

2.kobject:

The core part of the device model is kobject, which is represented from the struct kobject struct, defined in the header file .

3.ktype:

The existence of Ktype is to describe the universal characteristics of a family of kobject.

4.kset:

Kset is a collection of Kobject objects. Think of it as a container that places all relevant kobject objects, such as "All Block devices" in the same location.

Relationship between 5.kobject, Ktype and Kset:

Kobject, represented by struct koject. Kobject introduced basic object props such as reference counting, parent-child relationships, and object names, and provided them in a uniform way.

6. Management and operation of Kobject:

The first step in using KOBJCET needs to be declared and initialized first. Kobject is initialized by the function Ko Ect_init, which is defined in the file :

void Kobjectinit (struct kobject kobj, struct kobjtype ktype);

The first parameter is the Kobject object that needs to be initialized, and the kobject must be emptied before the initialization function is called.
This work is often done at the top of the kobject where the upper body is initialized.
If the kobject is not emptied, then only the call to Memset () is required.
7. Reference count:

Increasing the reference count is called getting a reference to the object, and reducing the reference count is called releasing the object's reference.
When the reference count falls to 0 o'clock, the object can be revoked, and the associated memory is freed.


Wu, Sysfs

  The Sysfs file system is a virtual file system in memory, It gives us a view of the Kobject object hierarchy . helps users to observe the topology of various devices in a system in a simple file system.

Add and remove Kobject in 1.SYSFS:

Functions are defined in the file lib/kobject.c, and are declared in the header file .

2. Add files to the SYSFS:

(1) Default properties: The default collection of files is provided through the Ktype field in Kobject and Kset. So all kobject with the same type have the same default set of files in their corresponding SYSFS directories.

(2) Create a new attribute: Create a symbolic connection in SYSFS: int Sysfscreatelink (struct kobject kobj, struct kobject target, char name);

(3) Deleting a new attribute-Deleting an attribute is done through the function Sysfsremove file (): void Sysfsremovefile (struct kobject kobj, const struct attribute attr); the symbolic connection created by Sysfs_ creat_ link () can be deleted by: void Sysfsremovelink (struct kobject kobj, char name);

3. Kernel Event Layer:

(1) Kernel event is passed netlink to user space by kernel space. NetLink a multicast socket for transmitting network information.

(2) Send signal to user space in kernel code using function Kobject uevent ():

int kobject_uevent (struct kobject *kobj,enum kobject_ action action);

    • The first parameter specifies the Koject object that sends the signal. The actual kernel event will contain the Koject map to the SYSFS path.
    • The second parameter specifies the "action" or "verb" that describes the signal.

"Linux kernel Design and implementation" Chapter 17th reading notes

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.