Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why can't you read the Linux kernel driver source code?

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

To learn embedded Linux driver development, the core skill is to be able to write Linux kernel drivers, in-depth understanding of the Linux kernel. The basis for doing this is that you need to understand the Linux kernel source code, understand its basic framework and specific implementation, and understand the use of its kernel API, and then you can write high-quality kernel drivers according to your own needs.

It is easier said than done. When many newcomers, or even developers who have worked for 1-2 years, first come into contact with the Linux kernel, they may look confused when they see the kernel code: it is obviously C language, but they just can't understand what it means. Besides understanding the basic functions of the whole function according to the function name, function parameters, function return value and comments, once you analyze its details, you will find that it is impossible to move a step further. Every line of code seems to be unfathomable, as if it contains a lot of energy, so you turn the book, Baidu, Google, step by step forward.

This is a common learning method for many beginners, including me. When I first came into contact with the Linux kernel driver, I saw a lot of deja vu, but very unfamiliar kernel code after careful scrutiny. My heart was broken and my heart was empty: dear, what is this? why can't I understand it? is there something wrong with my IQ? Am I the only one who can't understand it? Even after work, I have asked my colleagues with a lot of work experience to ask for some experience and methods of reading the kernel. You will find that many of them have not studied the kernel in depth. In addition to being familiar with the modules they are responsible for (register configuration, data flow), they have little time and energy to pay attention to other modules, even the framework of this module, of course. Most of this is the relationship between energy and time, the workload requirements require you to quickly complete the task through a variety of API. Some colleagues will even say that looking at the Linux kernel is like looking at flowers through fog. It is better to be hazy. You can't take a closer look. The closer you look, the more you don't understand.

However, if you want to write high-quality programs, you must have a deep understanding of the Linux kernel and modules, otherwise, you will always stay on the periphery, sewing and mending. According to my various learning experience and experience summary, Linux kernel is not indestructible, invincible, mastered the correct learning methods and knowledge base, we can also roam in the kernel code, appreciate the Linux kernel C language in a variety of wonderful applications and powerful skills, a variety of complex Linux kernel framework, subsystems can also be instructed, confident.

What knowledge reserves and skills are needed before learning Linux drivers and analyzing Linux kernel source code?

1) C language Foundation + data structure

The Linux kernel, literally, is made up of structures, function pointers, linked lists, and queues. So before you enter the Linux kernel, your C language foundation must be solid: what function pointers, pointer functions, array pointers, pointer arrays, and various pointers as function parameters, return values, etc., because these are widely used in the Linux kernel. These are the basics, and now I'm confused, and I'm even more dizzy when I look at the core. About C pointer advanced learning, you can follow the video tutorial: C language embedded Linux advanced programming

In addition, the data structure is also to be mastered. Linked lists and queues are widely used in the Linux kernel, so they must be mastered. Like some other nonlinear data structures, such as trees, binary trees, red-black trees, etc., developers who do underlying drivers have little contact with them, so they don't have to learn them first, and it's not too late to make up for them when they need them.

2) grammatical extension of C language

In the process of reading the Linux kernel code, have you ever felt that some of the code looks "weird" and is different from the normal C language? It seems to be C language. After careful analysis, I find that I can't understand it again.

These "weird C code" you look like are actually the GCC compiler's extension syntax to the standard C language: such as statement expressions, local tags, _ _ attribute__ attribute declarations, variable parameter macros, etc. The syntax of these GCC extensions is widely used in the Linux kernel and driver source code, especially some settings related to the underlying startup and compilation links. If you do not master the use of these extended C language syntax, you may encounter a lot of obstacles in the process of reading the Linux kernel source code, or drivers, causing all kinds of interference to our understanding of the code.

So before you read the Linux source code, it is recommended to learn the common extension syntax of GNU C to standard C: Wang Litao's talent class or video course: C language embedded Linux advanced programming issue 5: C standard and GNU C extension

After learning this, remove the grammatical barriers of reading the Linux source code, and then you can choose a small module that you are interested in: first leave this module to the user's API, learn to program, and then slowly study its internal kernel implementation. From the bottom to the upper level, get through the governor's second pulse, and then analyze the kernel, other complex systems, it will be easier to get started.

3) object-oriented thought in Linux kernel

With the above foundation, we analyze a small Linux kernel module, there is no problem. When you encounter a large complex subsystem, such as USB subsystem, memory management, MTD, file system, etc., the structure is embedded with multi-layer structures, various device, bus, driver, various layers, isn't it a little dizzy? There is a feeling that the blind touch the elephant and get lost in the forest, unable to grasp the "overall situation." At this time, we can not use the process-oriented thinking of C language, the design of Linux kernel actually uses a lot of object-oriented thinking. Therefore, we should learn to use object-oriented thinking to analyze the Linux kernel and analyze the reuse of each module, so it is very convenient to build the framework and level of the system in our mind. Then use process-oriented thinking to analyze the specific functional implementation, specific details, spend a little more time and energy, I believe you will have a different harvest. With regard to the data structures and object-oriented ideas commonly used in the Linux kernel, you can pay attention to the tutorial: C language embedded Linux Advanced programming issue 7: data structures and data encapsulation in the Linux kernel

Embedded AC QQ Group: 475504428

Official account of Wechat: home-learning tribe

Sina Weibo: @ House-learning tribe

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report