Linux Kernel Analysis Method

Source: Internet
Author: User
Linux Kernel Analysis Method-general Linux technology-Linux programming and kernel information. For details, refer to the following section. I want to analyze the Linux source code Linuxer. I think this article may be helpful to you. I am also analyzing the memory management part. Welcome to the discussion and make common progress. [Although it is less powerful, we hope to implement a line of code in the Linux kernel as soon as possible.]

--------------------------------------------------------------------------------

One of the biggest benefits of Linux is its open source code. At the same time, the Open Core source code attracts countless computer enthusiasts and programmers. They interpret and analyze the core source code of Linux as their greatest interests, modify the Linux source code and transform the Linux system as the biggest goal of computer technology.

The Linux kernel source code is very attractive, especially when you understand a problem that you haven't understood for a long time; or the kernel that you have modified can be compiled smoothly, when everything runs normally. That sense of accomplishment was born! In addition to the enthusiastic pursuit of technology, the analysis of the kernel is also fascinating in the return of this daunting labor, this is also the main reason why it has many followers:

First, you can learn a lot about the underlying knowledge of computers, such as system guidance and interrupt mechanisms provided by hardware. Others, such as the implementation mechanism of virtual storage, multi-task mechanism, system protection mechanism, and so on, all of which cannot be understood by the source code.

At the same time, you will also experience the weight and role of the overall design in the software design from the overall structure of the operating system, as well as some macro design methods and skills: the Linux Kernel provides a platform for upper-layer applications that is not related to specific hardware. At the same time, it also divides the code into components related to the architecture and hardware, and the portable part. For example, although Linux is not a microkernel, it processes most of the device drivers into a relatively independent kernel module, which reduces the overhead of kernel running, enhances the module independence of kernel code.

In addition, you can analyze the kernel source code to find out how to solve a specific problem: as shown in the following figure, Linux uses the Botoom_half mechanism to speed up the handling of system interruptions.

The most important thing is that, in the source code analysis process, you will be more professional at 1.1 points. A professional programmer always puts the clarity, compatibility, and portability of the Code in a very important position. They always enhance the clarity and readability of the Code by defining a large number of macros without increasing the length of the compiled code and the efficiency of code running. They are always coding at the same time, it takes into account future code maintenance and upgrade. Even after analyzing 1% of the code, you will deeply understand what kind of code is written by a professional programmer and what kind of code is written by a hobbyist. This cannot be realized by anyone who has not really analyzed the standard code.

However, due to the lengthy kernel code and the complexity of the kernel architecture, it is also difficult to analyze the kernel, which requires perseverance. In the absence of guidance and communication, this is especially true. Only when the method is correct can we get twice the result with half the effort. Based on this consideration, the author hopes that this article will give you some reference and inspiration.

As I have analyzed the kernel based on version 2.2.5, the following analysis is based on version 2.2.5 OF THE i386 single processor. All source files are relative to the directory/usr/src/linux.

Method 1: Start from

To analyze the Linux kernel source code, you must first find the location of each module, that is, you must understand the file organization form of the source code. Although this is not difficult for experienced experts, it is necessary for many beginners who are interested in source code analysis but have little contact with others.

1. the Linux core source code is usually installed in/usr/src/linux, and it has a very simple numbering Convention: Any even number of cores (two numbers are even numbers, for example, 2.0.30) is the core of a stable release, and any odd core (for example, 2.1.42) is the core of development.

2. The files of the core source program are organized in a tree structure. The directory/usr/src/linux contains the following directories and files at the top of the source program tree:

◆ COPYING: GPL copyright statement. It has the obligation to use GPL for published programs formed by source code changes with GPL copyrights or programs generated using GPL tools, such as open source code;

◆ CREDITS: honor list. Information of some people who have made great contributions to Linux;

◆ MAINTAINERS: maintenance personnel list, responsible for all parts of the current version of the kernel;

◆ Makefile: The first Makefile. It is used to organize the modules of the kernel, record the relationship and dependency between modules, and is used during compilation; carefully reading The Makefile files under each subdirectory is helpful for figuring out the relationship and dependency between each file;

◆ ReadMe: A Brief Introduction to the core and its compilation and configuration methods;

◆ Rules. make: common Rules used by Makefilemake;

◆ REPORTING-BUGS: Report BUGS;

● Arch/: the arch sub-directory contains all core Code related to the architecture. Each of its subdirectories represents a supported architecture. For example, i386 is a subdirectory about intel cpu and its compatible architecture. Generally, PCs are based on this directory;

● Include/: The include sub-Directory includes most of the header files required for compiling the core. Platform-independent header files are in the include/linux subdirectory, intel cpu-related header files are in the include/asm-i386 subdirectory, And the include/scsi directory is the header file directory of the scsi device;

● Init/: This directory contains the core initialization code (Note: it is not the System Boot Code) and contains two files main. c and Version. c. This is one of the good starting points for studying how the core works.

● Mm/: This Directory includes all memory management code independent of the cpu architecture, such as page-based storage management memory allocation and release; the Memory Management Code related to the architecture is located at arch/*/mm/, for example, arch/i386/mm/Fault. c;

● Kernel/: the main core code. The files in this directory implement the Kernel functions of most linux systems. The most important file is sched. c; similarly, the architecture-related code is in arch/*/kernel;

● Drivers/: Place all the device Drivers in the system. Each driver occupies a sub-directory, for example, block device Drivers under/block, such as ide. c ). If you want to check how all devices that may contain a file system are initialized, you can check device_setup () in drivers/block/genhd. c (). It not only initializes the hard disk, but also the network, because the network is required when installing the nfs file system;

● Documentation/: The document directory, without kernel code, is just a set of useful documents. Unfortunately, they are all in English and should be useful;

● Fs/: All File System Code and various types of file operation code. Each subdirectory of Fs supports one file system, such as fat and ext2;

● Ipc/: This directory contains the code for inter-process communication between core processes;

● Lib/: place the core library code;

● Net/: core network-related code;

● Modules/: module File directory, which is an empty directory used to store the module target file generated during compilation;

● Scripts/: description file and script for core configuration;

Generally, each sub-directory contains a Makefile and a Readme file. Read these two files carefully to understand the kernel source code.

There are several good entry points for the Linux kernel source code analysis: one is system boot and initialization, that is, from machine power-up to system core running; the other is system calling, system Call is the function interface provided by the user program or operation call core. For those who are familiar with hardware, it may be easier to start with system guidance and start from system calls, it may be more suitable for those who have C programming experience in dos, Uinx, and Linux.

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.