Linux Kernel analysis experience

Source: Internet
Author: User
Article Title: Linux kernel analysis experience. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
The Linux kernel has basically been analyzed, and the kernel of the later version has also read a bit. I have some tips to share with you.
Click it! Here I am not going to talk about specific technical aspects, but about how to read the kernel.
.
Some time ago, I mainly looked at the kernel of version 0.01. Linux0.01 is the oldest completion of Linux's "lin" Linus
In a Linux version, the kernel is only 512 KB after compilation. Although the sparrow is small, the kernel is dirty, and 0.01 includes
Provides complete functions for disk boot, file system, and console management, and provides many standard user interfaces,
There are several parts, including kernel, boot, fs, init, and mm, with no network.
Why choose Linux0.01? When you see 0.01, you must shake your head: Oh, what time is it?
Is it necessary to see it? I didn't have many ideas when I chose 0.01, but the Tm-linux team just started to choose
I chose 0.01, so I started to read it. Now I think it is easier for beginners to read Linux0.01.
Get started. There may be the following benefits:
1) 0.01 of the Code is small. Many of you have a desire to become a Linux master and have carried back several brick books,
How fast is Linux? coder is a hacker and often gives up without long time!
  
2) 0.01 of the code is simple and streamlined (this is of course relative to subsequent versions ). Actually completed 0.01
Is the initial requirements of an operating system, including startup, process scheduling, and memory management.
Hardware integration, when you look at the kernel of a higher version, you are often not exposed to this hardware knowledge.
Confused.
3) from the perspective of lower versions, we can better see the source power of technological progress. For example, if the 0.01 kernel is small, its startup code can only be placed
In one slice, the later versions of the kernel are large and cannot be placed in one slice. Therefore, the core loader is compressed.
Law was born. Another example is memory management. 0.01 of memory management is relatively simple. The application for release of memory directly goes through the application before use,
Release after use, but considering the complexity of functions in later versions, how to solve the possible "external fragments" problem and how to solve the problem of insufficient memory, so the "partner algorithm" adopted for later versions"
, Slab technology, and page Daemon have a better understanding.
I said that the above is not about advertisement for 0.01, but a reminder of the possible problems encountered in reading the kernel:
For example, the complexity of layers is difficult to clarify, and many codes do not know the primary and secondary components, which are required by the operating system, and which are used to improve the system
Performance.
Look at the purpose of the kernel. This is a question that everyone who looks at the kernel needs to ask themselves.
I think the most important thing for most people who first look at the kernel is to solve the problem from operating system principles to a practical operation.
Work
The system transition is simply how the operating system runs. Everyone has learned the operating system principles,
How is an operating system started? What is the so-called protection mode? process scheduling and switching
In the end, the specific operations of a file system may be vague. In Linux
These questions can be clearly answered.
Of course, an operating system with powerful vitality will never be able to run, and she uses many advanced
The technology gives her excellent performance, and it will be very useful for future engineering design, programming, and system development in Unix-like systems.
More help.
In view of the powerful technical strength of Linux, many fields will encounter technologies related to its adoption, such as the embedded field,
Network interfaces. After understanding the kernel, you can certainly get twice the result with half the effort in future development.
The purpose of reading the kernel is relatively simple. I just want to see how Linux runs and why multiple processes can
To avoid interference.
The tool I used to read the code is source insight. Unfortunately, it cannot automatically identify the Assembly. s file, while the 0.01 Assembly File
A large number of parts. Oh, I 've talked so much about it and haven't entered the topic: My Experience
Generally, it is recommended that you start reading from the boot kernel.
Some seem the most mysterious and curious! But I started reading from the memory part, because
The translation from an address to a physical address is always vague. Reading from the places you are interested in will be easier to stick to later. In terms of the kernel reading method, I suggest "Horizontal first and vertical ".
  
In terms of the kernel reading method, I suggest "Horizontal first and vertical ".
The benefit of Souce insight is that you can click a function to automatically display the definition of the function, which is more convenient.
Jump to view. For people who are reading the kernel at the beginning, I do not recommend starting with this method. Because function layer-by-layer embedding
When you get down to the bottom layer, you have forgotten the original function called. We recommend that you just skip one layer to see the annotation of the function called.
Or simply look at the function code guess function and make some marks.
I personally think that the data structure and algorithms are important for operations. algorithms are embodied in programs, while
You need to view the relevant header files and related files. So at the beginning, I used file 1.
File Reading, that is, the so-called "horizontal", because the arrangement in a file is always some operations on a specific data structure
You have more opportunities to understand the functions of each major data structure, including the meanings of each subitem in the structure. Of course
Do not forget to write comments (although the current understanding may not be accurate ).
After reading all the files of a module, such as the FS module, the so-called "vertical" is adopted. You can choose one
To understand the organic relationship between data structures.
. For example, the fs module involves the following data structures: super block, node index block, data index block, and high-speed buffer block,
File. After understanding each related file separately, you can start with do_execve to view the input file parameters of a file.
Count to the last step to understand the interface between VFS and the specific fs.
After reading each major module, I will write a summary (written summary) to see what I have done for this model.
Block. Because reading code is often more meticulous, you may focus on reading the Code itself and ignore the functions of the module.
Ability and organization form. I think this is more important. The so-called "cool grass to repeat" may have to read the code.
In this way, we can go up to a higher level and truly understand the subtlety of its method and structure.
Finally, let's talk about the basics of Linux kernel.
1) Intel386 hardware knowledge, such as various registers, TSS, segment description tables (IDT, GDT, LDT), segment page Conversion
Mechanism (later versions of Linux support other hardware systems, but are familiar with intelx86)
2) Assembly commands, especially AT&T syntax and its migration-type assembly, I first saw such code:
  
_ Asm _ ("std; repne; scasw \ n \ t"
"Jne 1f \ n \ t" "movw $1, 2 (% edi) \ n \ t"
  
"Movw $1, 2 (% edi) \ n \ t"
"Sall $12, % ecx \ n \ t"
"Movl % ecx, % edx \ n \ t"
"Addl % 2, % edx \ n \ t"
"Movl $1024, % ecx \ n \ t"
"Leal 4092 (% edx), % edi \ n \ t"
"Rep; stosl \ n \ t"
"Movl % edx, % eax \ n"
"1 :"
: "= A" (_ res)
: "0" (0), "I" (LOW_MEM), "c" (PAGING_PAGES ),
"D" (mem_map + PAGING_PAGES-1)
: "Di", "cx", "dx ");
  
Such an assembly statement is swollen, but after learning about its format, you will get used to it.
3) the basic knowledge of the operating system, general understanding of the operating system, everyone has it!
4) one or several Linux kernel reference books are always much better. After all, many data structures do not need to be used by themselves.
I'm reading 0. 01 it seems very different. In many cases, you can only guess, but it is still useful. For Linux
There are already a lot of Nuclear books, so you will know when you go to dulle.
5) perseverance + perseverance: This is the most important point. Initial interests are often swept away by the difficulties encountered.
It is really difficult to stick to other things. At the beginning, no one in the Tm-linux Group participated in Kernel Analysis.
Dozens, but only a few people stick to it (this is only 0.01)
6) I have a lot of communication with others. After all, my personal abilities are limited. through communication, I can improve my knowledge.
Communication, although it may take some time to write, because the written content can be viewed later, and the written content can be handed in with others
Stream always hopes to be correct and make fewer mistakes. Xi'an Jiao Tong University has done a good job in this regard and organized the Linux kernel forum,
There is a theme every time at a meeting. Someone will give a lecture and then everyone will discuss it with each other.
Do you have any of these? Congratulations, because by reading the kernel, you can increase your knowledge and experience in this area.
.
Reading the kernel is not the ultimate goal, either for your own development in the embedded system, or Linux's "Easy to use", or to achieve
Your own operating system, that is "when the sword is out of the sheath "!
It is estimated that there are quite a few people reading the Linux kernel, but there are only a few people talking about it. It is very important that I want to talk about it.
People have never asked questions, but they can increase understanding and raise awareness through communication. Willing and encouraging
Although I am not a Daniel (nor a calf J), I am willing to use bbs, Email,
Or other communication methods.
I hope that some of my insights will inspire you and more Linux cool people will emerge in the future!
  

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.