This article briefly describes how the system and computer hardware work together from the moment we press the power-on key to the system that can be logged in. Not only as a summary of their own knowledge, but also to Linux novice users to do a introductory introduction, Master please skip.
In general, Linux startup can be divided into three stages: the BIOS phase, the system boot phase, the system startup phase.
First, the BIOS phase
The BIOS (basic input and output system) is a program that is cured in the computer ROM. This is before the installation of the computer has been cured well.
the computer has some knowledge of the students should know that the computer is by the CPU, memory, hard disk, display and so on plus an iron box package up, and the computer is run by the CPU instructions to execute and direct the other hardware to work together to complete the normal operation of the computer. When we press the computer button, the computer's power components will give the CPU and other hardware to power, and then the CPU began to execute the first instruction, and the address of this first instruction is the address of the BIOS. That is, the BIOS is the first program that runs on a computer, before the operating system (LINUX,WINDOWS,OSX, etc.). Again this "address", the CPU's instruction address is identified by the CPU two registers (CS:IP), and the CPU hardware logic is designed to power up after CS is all F, the IP is full 0, so this "address" is 0xffff0 (calculation method: CSx16 + IP)
The main function of the BIOS is to detect whether the basic hardware of the computer meets the operational requirements, and to initialize the hardware status, interrupt vector table, interrupt service program, etc. The first sector (MBR) of the bootable media (which is currently a hard disk) is loaded into memory based on the hardware information obtained, and the control is transferred to the program, which is the second phase of the operation.
Second, the system guidance stage
MBR is a 512-byte-size mapping, details can be referenced (images from the inside of the Linux boot process):
Bootloader: Boot Program
Partition table: Hard disk partition information
Magic number:mbr identification, the normal value is: 0xaa55
In fact, the bootloader here is not all of the boot program, but only a small part of the bootloader executable (because the MBR space is too small to fit the entire bootstrapper) we call the first phase of the bootloader, the main task is to load the rest of the boot program, called the second-stage boot program. The main function of the second stage bootloader is to load the operating system and transfer control to the kernel. The common boot program has lilo,grub and so on.
Third, the system operation start stage
Linux kernel code is now very large, compiled well after generally still have about 5 m, so all are compressed with bzimage way. The sequence of early code execution can be seen in the Linux boot Process insider and Linux boot process overview.
This article was written with the kernel already in version 3.17, in the new version:
1, the kernel code entry is arch\x86\boot\header. S, this assembly code is converted to protected mode (32-bit) by calling the main function in MAIN.C in the same directory as the actual mode (16-bit).
2,protected_mode_jump (Boot_params.hdr.code32_start, (u32) &boot_params + (DS () << 4)); The call to the function jumps to the the startup_32 label in the X86/BOOT/COMPRESSED/HEAD_32.S is executed. This code calls the Decompress_kernel function to extract the kernel (bzimage) image file. A detailed jump process can be consulted: Linux kernel boot process
3, and jump to the X86/kernel/head_32.s in the startup_32 label place to execute. This code calls the I386_start_kernel function, and I386_start_kernel calls the main function Start_kernel of the Linux kernel.
Note: The 64-bit has the corresponding HEAD_64.S assembly code. The process is similar.
4, to compare Linux to an application, Start_kernel is the main function of Linux. The preceding code is just a preparation for running Linux. In this function we are beginning to get familiar with the initialization of the kernel modules, such as lock mechanism, process scheduling, file system and so on. Last Call Kernel_thread (Kernel_init, NULL, CLONE_FS | Clone_sighand) to execute the first process "init" process under the Linux system.
The initialization of the 5,linux application layer is done by Init calling the startup script, and finally invoking a login process to display our login interface. The most common init here is sysvinit, Upstart, Systemd.
Linux system Boot