That year, I learned how to practice Linux kernel-initialization of subsystems: Taking the PCI subsystem as an example.

Source: Internet
Author: User


These articles are the notes for reading the "Cultivation of Linux kernel" and some additional knowledge and insights. If you are not clear about these articles, please refer to the "Cultivation of Linux kernel" and other materials. If there is anything wrong with this article, please point out, thank you.

Reprinted please indicate the source: http://blog.csdn.net/muge0913/article/details/7252115


According to the distribution of kconfig, the PCI code should be distributed in two places: Drivers/PCI and arch/i386/PCI. Both sides of the Taiwan Strait belong to one China, both drivers/PCI and arch/i386/PCI belong to only one PCI subsystem. In line with the One-China Principle, we need to make a comprehensive investigation and analysis of the code located in two places, so these far break through the five-digit code. Looking left and right, looking at the vertical view is so gloomy, however, what people say is that it is also the whole PCI subsystem, just like Sister Furong and Yang erchelaom walking on the T-station, it looks terrible but also has a great connotation, can anyone tell me what they say with just a few words?

Now let's look at the code in these two regions in a comprehensive manner. Based on the previous content, we can speculate that there should be an entry such as subsys_initcall for subsystems such as USB and PCI, we need to find it first. Zhu deyong said in "about going to work" that it takes the first half of life to find the entrance and the last half to find the exit. It can be seen that the search portal is very important for us to look at the kernel code in our life. Of course, the search for the subsys_initcall portal does not take that long. Why do I say "column" to the bottom of the list? Is there a lot more? You guessed it. There are a lot of entrances to the PCI side, and they are organized into series, not just subsys_initcall, arch_initcall, postcore_initcall, and so on.


Memory location of file function entry
ARCH/i386/PCI/ACPI. c pci_acpi_init subsys_initcall. initcall4.init
ARCH/i386/PCI/common. c pcibios_init subsys_initcall. initcall4.init
ARCH/i386/PCI/i386.c pcibios_assign_resources fs_initcall. initcall5.init
ARCH/i386/PCI/legacy. c pci_legacy_init subsys_initcall. initcall4.init
Drivers/PCI/pci-acpi.c acpi_pci_init arch_initcall. initcall3.init
Drivers/PCI/pci-driver.c pci_driver_init postcore_initcall. initcall2.init
Drivers/PCI/pci-sysfs.c pci_sysfs_init late_initcall. initcall7.init
Drivers/PCI. c pci_init device_initcall. initcall6.init
Drivers/PCI/probe. c pcibus_class_init postcore_initcall. initcall2.init
Drivers/PCI/proc. c pci_proc_init _ initcall. initcall6.init
ARCH/i386/PCI/init. c pci_access_init arch_initcall. initcall3.init




Let's take a look at the various xxx_initcall statements that are listed in the mouth and disagree with each other. I really don't know where to start. I should say the word "KESO: all pain points come from choice. Happiness means no choice. Just like the USB sub-system, a single subsys_initcall is simple, and you have no choice but to know how to proceed. However, you may be confused for a while, but don't really get involved. You need to know "how many things are always in a hurry; heaven and earth are turning around and time is pressing. 10 thousand years is too long to compete for the evening. The four seas are surging and the five continents are surging. You can see all the entries ." Let's see all the entrances on the eve of the day.

We already know that the call to these xxx_initcall functions must follow a certain sequence. call again in initcall1.init. in initcall2.init, it is obvious that the columns in the table should be called first. the pcibus_class_init and pci_driver_init functions in the initcall2.init subsection. Now the problem arises. Which of the following functions in the same subsection, such as pcibus_class_init and pci_driver_init, will be called first? Of course, you can say that the address at the front side will be called first. This is a big truth, because the implementation of the do_initcils function is a for loop back and forth on the address of. initcall. init. But how do you know which function is in the same subsection and which is in the front and back?

Let's not talk about anything more. Let's take a look at a section in the GCC using the GNU Compiler Collection:


The linker searches and processes libraries and object files in the order they are specified. thus, 'foo. o-LZ bar. O 'searches library 'Z' after file 'foo. O 'But before' bar. O '.


After reading this passage, I hope to hear you say, "I am enlightened! I hope you can see the drivers/PCI/MAKEFILE file and view the two lines at the bottom.


5 obj-y + = access. O bus. O probe. o remove. o pci. O quirks. O/
6 pci-driver.o search. O pci-sysfs.o Rom. O setup-res.o


If probe. O is in front of PCI-driver. O, the pcibus_class_init function in probe. C will also be called before the pci_driver_init function in PCI-driver. C. Again

To show you a sentence, in section 3.2 of documents/kbuild/makefile.txt:

The order of files in $ (obj-Y) is significant.


The pcibus_class_init and pci_driver_init functions in the same directory can be determined by the link sequence specified by makefile in the directory. the acpi_pci_init function and the pci_access_init function in the initcall3.init subsection cannot use this method.

Acpi_pci_init is in drivers/PCI/pci-acpi.c file, while pci_access_init is in arch/i386/PCI/init. in the c file, they are not in the same directory at all, so the technique of determining the order of pcibus_class_init and pci_driver_init on the front side is not applicable. What method can we find their order? Let's see how Wang Ran said: "Yesterday was May Day, but the vast majority of people in China are not working. Obviously, the best way to celebrate one thing is not to do it. For example, the best way to celebrate the World Cup is not to participate in the World Cup-the Chinese team has always done so. For another example, the best way to celebrate Valentine's Day is not to find a lover-as a result, many Chinese men make the day before Valentine's Day (February 13) Valentine's Day ." According to him, the best way to identify the order between the two functions is not to care about their order. You can click a point at random to start, however, as a post 80 who clearly understands their responsibilities and missions, I decided to explore their order.

In fact, this problem can be transformed into the makefile under ARCH/i386/PCI and the makefile under Drivers/PCI, that is, how the kernel is built, that is, the problem of kbuild.

There are three main types of makefiles in the kernel: the first is the makefile in the root directory. Although there is only one, it is far above other makefiles, it defines all the variables and objectives unrelated to the architecture. The second is arch/*/makefile. If you see Arch, you will know that it is related to a specific architecture, it is contained in the makefile under the root directory, providing specific information about the architecture for kbuild, and it contains makefiles of subdirectories at various levels under ARCH; the third type is the makefile that is hidden in various subdirectories, such as drivers.

While the kbuild kernel is first executed from the root directory makefile to obtain the variables and dependencies unrelated to the architecture, it also obtains system-specific variables and other information from arch/*/makefile to extend the variables provided by makefile in the root directory. At this time, kbuild has all the variables and targets required to build the kernel. Then, make enters each subdirectory and passes some variables to the makefile in the subdirectory, the subdirectory makefile determines which source files to compile Based on the configuration information, so as to build a list of files to be compiled.

Then, there is a long way to go. How long will it take for you to compile the kernel? But the answer to the previous question has come to the fore. Obviously, makefile under ARCH/i386/PCI is located in front of makefile under Drivers/PCI, that is, pci_access_init is located in front of acpi_pci_init.

With these hidden rules, we can obtain the execution sequence of initialization functions when studying a subsystem, and use Comrade Han Feng's attitude towards the diary for in-depth analysis.


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.