On the Structure and generation process of kernel ----- (1), kernel -----

Source: Internet
Author: User

On the Structure and generation process of kernel ----- (1), kernel -----

TodaySo many servers and workstations around us run linux, so many friends want to understand the core mechanism of linux. However, as the kernel is too large, some friends are discouraged. (I also had this experience when I was in my sophomore year. When I saw some people watching kernel, I also wanted to go in and find out, but it didn't take long before I went in, to view the uboot source code ). Today, I will show the structure of the kernel from my personal perspective (first, I declare that this is not about the structure of the Directory, because the name of the directory knows what file is put in it, if I want to talk about the directory structure, I believe many of my colleagues have learned how to launch and scold me.) and how to generate the kernel for your reference! (Here we use arm as an example.Linux kernelIt is not a sacred one. To put it simple, it is nothing more than a large source code organized by a source code tree. It is an executable file compiled by the compiler. Since it comes from a source code tree, let's go to the source code tree to find out what's going on! (Here I would like to recommend you a book that I feel better about. Its name is "Embedded. Linux. Primer". I suggest you read the original English version in Chinese.In the translated book, although some content cannot be said to be wrong, IT does not completely express the specific meaning of the author. IT seems awkward as a person in the IT field, we should also look at English.Materials !)

To understand the overall structure of the kernel, I suggest you have better knowledge about makefile, because makefile can be used for management of large projects such as kernel to control the entire compilation process conveniently. In fact, if you are very familiar with makefile, you can ignore this blog and directly read The makefile to fully understand the entire kernel source code tree. Therefore, it is better to learn about makefile first because you have mastered it.
After makefile, you don't need to trust others. If you don't need to say so, you will think so. Maybe someone else is wrong. It is better to have a method suitable for multiple places than to learn only one place, such as the class in C ++.
All objects can share one method, but the attribute values of each object are different. (This is an unreasonable analogy. Let's take a look ).
Next we enter the main directory of the kernel and we can see the following content:
Arch. config crypto firmware. gitignore ipc kernel MAINTAINERS net samples sound virt
Block COPYING Documentation fs include Kbuild lib Makefile README scripts tools
Certs CREDITS drivers. get_maintainer.ignore init Kconfig. mailmap mm REPORTING-BUGS security usr
(I am the version of the recently downloaded linux-4.4-rc4, and the home directories of other versions are basically the same, among them. Config is a configuration file generated by using make menuconfig ). Since makefile manages the entire project, the second thing we should look at is Makefile. (haha, the first thing we should look at is README. This is a good habit ), at the beginning of Makefile, we can see:
8 # To see a list of typical targets execute "make help"
9 # More info can be located in./README
Right. Check the help in Makefile:

1244 help:
1245 @ echo 'cleaning targets :'
1246 @ echo 'clean-Remove most generated files but keep the config and'
1247 @ echo 'enough build support to build external modules'
1248 @ echo 'mrproper-Remove all generated files + config + various backup files'
1249 @ echo 'didclean-mrproper + remove editor backup and patch files'
1250 @ echo''
1251 @ echo 'configuration targets :'
1252 @ $ (MAKE)-f $ (srctree)/scripts/kconfig/Makefile help
1253 @ echo''
1254 @ echo 'other generic targets :'
1255 @ echo 'all-Build all targets marked with [*]'
1256 @ echo '* vmlinux-Build the bare kernel'
1257 @ echo '* modules-Build all modules'
1258 @ echo 'leles_install-Install all modules to INSTALL_MOD_PATH (default :/)'
1259 @ echo 'firmware_install-Install all firmware to INSTALL_FW_PATH'
1260 @ echo '(default: $ (INSTALL_MOD_PATH)/lib/firmware )'
1261 @ echo 'dir/-Build all files in dir and below'
1262 @ echo 'dir/file. [oisS]-Build specified target only'

 .
.
.

After comparing multiple rows, we can see that make menuconfig is used to generate a configuration file. config that indicates project compilation. As for how this interface is generated and how to generate. config, let me talk about it: Let's look at Makefile:
527 config: scripts_basic outputmakefile FORCE
528 $ (Q) $ (MAKE) $ (build) = scripts/kconfig $ @
529
530% config: scripts_basic outputmakefile FORCE
531 $ (Q) $ (MAKE) $ (build) = scripts/kconfig $ @
Yes, it is generated through the script.../scripts/kconfig (... represents the main directory of the source tree ). You will find that there is a Kconfig in the sub-directory in the main directory, which is used for configuring menuconfig
Set the compilation information. Since this allows for indirect compilation instructions, let's look at.../Kconfig:
Mainmenu "Linux/$ ARCH $ KERNELVERSION Kernel Configuration"

Config SRCARCH
String
Option env = "SRCARCH"

Source "arch/$ SRCARCH/Kconfig"
After you see the source, go to the file.../arch/$ SRCARCH/Kconfig. It will include all the Kconfig files to be used in the source code tree. What are included? Let's look at Kconfig. There are hundreds
.

    As mentioned above, there are so many seemingly unimportant thingsIn fact, in my opinion, they are the most important, because if you are not familiar with the subject structure, the source code will be messy, let alone subsequent modifications and kernel cropping. Once you are familiar with the entire structure, you will know where to view, modify, and add code.    The above is based on makefile, kconfigTo describe how to organize the large kernel and facilitate the user to configure and compile the information. Let's talk about the generation of kernel.

Since kernel is the core part of the operating system, usually the operating system also has file systems and other components. Here we mainly talk about kernel, the file system won't be mentioned first (besides, I haven't started learning it yet, Master
If you see it, we hope to provide some suggestions for learning the file system ). okay. Let's start talking about kernel. Since kernel is so popular around the world, it is inseparable from us. android and linux operating systems of various releases
And all kinds of servers are using it, certainly has its unique function. One thing must be clear: when a computer is just powered on and started, it does not run kernel, but bootloader (usually called BIOS on a PC ),After bootloader (bios) is started, the cpu execution right is passed to the kernel. After the kernel obtains the execution right, a series of initialization operations are performed. Then, the root file system is mounted and some scripts (scripts) are executed.Such as/etc/init. d,/etc/cron * In your linux system) to start some system processes (such as user login program and Ethernet initialization ). Since the kernel is executed after bootloader (BIOS,So how does it take over the execution right of bootloader (BIOS? Go to the.../arch/arm/boot/compressed/directory, and you will see a link file. The file name is vmlinux. lds. S, which is the binary of the real kernel.The executable file vmlinux will be located here (you can check the compiled kernel). During compilation, your supervisor will output the compilation information, by viewing this information, you can know which files are being compiled into the kernel.Some:

Arch/arm/kernel/head. o
Arch/arm/kernel/init-atask.o
Init
Usr/built-in.o
Arch/arm/kernel
Arch/arm/mm
Arch/arm/common
Arch/arm/mach-ixp4xx
Arch/arm/nwfpe
Kernel
Mm
Fs
Ipc
Security
Lib/lib.
Arch/arm/lib
Lib
Drivers
Net

Next, let's talk about how the kernel takes over the execution right of the CPU, that is to say, where is the first line of code in the true sense of the kernel? Note: Since the kernel is a multi-platform kernel, so here we will talk about

The first line of code is to remove the first line of code after the initial code of a specific architecture-specific processor. The real first line of kernel code is located in.../arch/$ (SUBARCH)/kernel/head. S. Here the kernel will checkTest the effective architecture and processor, establish the initial page table entry, and enable MMU, and then truly start the kernel itself (jump... /init/main. c) Speaking of this main. c file. This file is ours.It must be nice-looking, understand it well, and use it to gradually get into the kernel, so as to deeply understand the legendary kernel. Didn't we always emphasize the first line of code in the true sense of kernel?For example, for different processors, some processors must first pass the corresponding processing before the CPU execution right is handed over to the kernel.Different architectures have different headers. S (located in... /arch/$ (SUBARCH)/boot/compressed/), such as head_32.S, head_40x.S, and head_44x.S in power pc. Through these assembler programsThis gives control to the kernel,The head. S program of the processor in this particular architecture will perform some processing on the processor, suchTo enable processor's command cache and data cache and establish an environment suitable for C code running.You can also understand this as follows... /arch/$ (SUBARCH)/kernel/head. operations before S are considered as the second bootloader. It can also verify the kernel and decompress the kernel.(This function is implemented in.../arch/$ (SUBARCH)/boot/misc. c. The function isDecompress_kernel.However, some architecture processors can direct the kernel,You do not have to perform the above operations.

This is the first blog article about kernel. Experts are welcome to make good suggestions, correct mistakes, and learn and make progress together. It is early in the morning!

 

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.