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

The core skills of learning embedded Linux driver development are to be able to write Linux kernel drivers and to have a deep understanding of the Linux kernel. The foundation of this step is that you need to understand the Linux kernel source code, understand its basic framework and specific implementation, and understand how to use its kernel API, and then you can write high-quality kernel drivers according to your own needs.

It's easier said than done. Many newcomers, even developers who have been working for 1-2 years, are confused when they first come into contact with the Linux kernel, not to mention writing them. They may be confused when looking at the kernel code: it is clearly C language, but they just can't understand it. What does it mean? In addition to understanding the basic functions of the entire function according to the function name, function parameters, function return value and comments, once you analyze its details, you will find that it is difficult to move, and each line of code seems to be unfathomable, as if it contains extremely Great energy, so you flip through books, Baidu, Google, and move forward step by step.

This is a learning method commonly used by many beginners, including me. When I first came into contact with the Linux kernel driver, I saw a lot of familiar kernel code, but it was very unfamiliar to me. My heart was broken and my heart was empty: darling, What is this, why can't I understand it, do I have a problem with my IQ? Am I the only one who doesn't understand? Even after work, I have asked a lot of colleagues with work experience. I want to ask some experience and methods about reading the kernel. You will find that many of them have not studied the kernel in depth, except that they are familiar with the modules they are responsible for (register configuration, Data flow), there is very 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, and the workload requirements require you to quickly pass through each module. kind of API to complete the task. Some colleagues may even say that looking at the Linux kernel is like looking at flowers through fog.

However, in order 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 and make up for it. According to my various learning experiences and summaries of experience, the Linux kernel is not indestructible or indestructible. If we have mastered the correct learning methods and knowledge base, we can also roam in the kernel code and appreciate the various aspects of the C language in the Linux kernel. Wonderful application and powerful skills, can also give pointers to various complex frameworks and subsystems of the Linux kernel, and have a good mind.

What knowledge reserves and skills do you need before learning Linux drivers and analyzing Linux kernel source code?

1) C language foundation + data structure

The Linux kernel, it is no exaggeration to say, is composed of various structures, function pointers, linked lists, and queues. So before entering 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. must be clear, because the Linux kernel Use these a lot. These are all basics, and now I am confused, and looking at the kernel is even more dizzy. For advanced learning of C pointers, you can pay attention to the video tutorial: C language embedded Linux advanced programming

In addition, data structures are also to be mastered. Linked lists and queues are widely used in the Linux kernel, so they must be mastered. Like some other non-linear data structures, such as trees, binary trees, red-black trees, etc., for the developers who are the underlying drivers, they have little contact with them.

2) Syntax extension of C language

In the process of reading the Linux kernel code, do you feel that some code looks "weird" and is not the same as the general C language? It seems to be the C language. After careful analysis, I found that I couldn't understand it again.

These "weird C language code" you look like are actually the extended syntax of the GCC compiler to the standard C language: such as statement expressions, local labels, __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 low-level startup, compilation and linking. If you do not master the use of these extended C language syntax, you may encounter many obstacles in the process of reading the Linux kernel source code or driver, causing various disturbances to our understanding of the code.

Therefore, before planning to read the Linux source code, it is recommended to learn the common extension syntax of GNU C to standard C: Wang Litao's master class or video tutorial: C Language Embedded Linux Advanced Programming Issue 5: C Standard and GNU C Extensions

After learning this, the grammatical obstacle of reading Linux source code has been removed. Next, you can choose a small module that you are interested in: first leave this module to the user to play with the API, learn to program, and then slowly study the internal implementation of the kernel. From the bottom to the top, open up the two veins of Ren and Du, and then analyze other complex systems in the core, which is also easy to get started.

3) Object-oriented ideas in the Linux kernel

有了上面的基础,我们分析一个小的Linux内核模块,是没有问题的。当遇到一个大的复杂子系统,比如说USB子系统、内存管理、MTD、文件系统等,结构体里面嵌套多层结构体,各种device、bus、driver、各种层,是不是有点绕晕了?有种盲人摸象、在森林里迷路的感觉,把握不了“全局”。这时候,我们就不能使用C语言的面向过程思维了,Linux内核的设计其实大量使用了面向对象思想。因此,我们要学会用面向对象的思维去分析Linux内核,分析各个模块的复用,这样就很方便的在脑海中搭建出系统的框架和层次了。然后再使用面向过程思维去分析具体的功能实现、具体细节,多花点时间和精力,相信你会有不一样的收获的。关于Linux内核常用的数据结构和面向对象思想,可以关注教程:C语言嵌入式Linux高级编程第7期:Linux内核中的数据结构与数据封装

嵌入式交流QQ群:475504428

微信公众号:宅学部落

qrcode_for_gh_e21956862bc4_430.jpg

新浪微博:@宅学部落


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325028682&siteId=291194637