Llinux START Process

Source: Internet
Author: User

Partial quote Others Summary, if infringement, please tell me to delete
First, the guidance process
The kernel boot process, in turn, involves files that are
Src/arch/i386/boot/bootsect. S
Src/arch/i386/boot/setup. S
Src/arch/i386/boot/compressed/head. S
Src/arch/i386/boot/compressed/misc.c
Src/arch/i386/boot/kernel/head. S
Src/init/main.c

1. Puppet Boot Sector
The old kernel, which uses the BIOS to boot the kernel by loading the first sector of the storage media to 0x7c00 this location and handing over the execution permission mechanism. So this sector is also known as the boot sector, and now this mechanism has been deprecated, generally using other bootloader boot, popular with Lilo (Linux Loader) and grub, the bootloader by inserting code in the master boot record or partition boot record to gain CPU control, and then boot the kernel.
The code for the puppet boot sector is src/arch/i386/boot/bootsect by the file. s generated, normally will not be executed, now the function is only to output the following information
"Direct booting from floppy is no longer supported"
"Use a boot loader program instead"
"Remove disk and press any key to reboot"
and stores a small number of parameters set by the bootstrapper to the kernel.

2. Guide
The bootloader mounts the bootsect to the address 0x9000:0000 based on the bootstrap protocol, and then sets the relevant parameters in the Bootsect, which is recognized and used by the kernel. For the boot protocol, see the kernel document src/documention/i386/boot.txt description, the bootloader will also pass additional information to the kernel, in the following article

3. Detection system resources
The task of detecting the available resources of the system is by Src/arch/i386/boot/setup. S is complete, the bootloader completes the kernel image after loading, and the system control is transferred to the binary code generated by the file. Since the Bootsect is 512 bytes long, the binary code is loaded into the 0x9020:0000 location.
This part of the system is mainly to detect the system configuration information, and to store the detected information into the corresponding memory unit, set the system's global descriptor GDT and interrupt descriptor IDT, the system into the protection mode.

Source code based on the role can be divided into several modules
1. Reserved memory space
Depending on the boot protocol, a portion of the space is reserved for the kernel and the bootstrap to exchange information.
2. Resetting the hard drive controller
3. Integrity Verification
Used to determine if setup in Bzimage is fully loaded, the old bootloader default setup consumes 4 sectors 4*512 bytes, and for setups larger than 4 sectors, Setup may not be fully loaded, so the integrity verification is performed immediately after Setup executes. The Kernel Builder final stage calls the build program to generate the kernel image bzimage, which modifies the parameters in the Bootsect to record the true size of the setup when the actual size of the setup exceeds 4 sectors, so that the new bootstrapper can be fully loaded into setup.
4, the Guidance ability judgment
Determine if the bootloader is capable of properly loading the kernel image, pass the parameters passed by the bootstrap protocol, and let the kernel image know if the bootloader is capable of properly loading itself. Unlike the previous one, the kernel image developed in different forms, and the Zimage build generated a small kernel image that was loaded into the 0x100,bzimage generated as a large kernel image and loaded into 0x100000.
5. System memory Detection
Three BIOS interrupts are used once to prevent some of the motherboard from not supporting some detection interrupts
6. Detect other Information
Includes hard drive information, handles Voyager architecture, detects PS/2 mouse
7. Set the Jump Address
The jump address depends on the type of kernel image, and the bzimage generates a large kernel with a jump address of 0x100000
8. Open A20 Address line
But still can't access more than 1MB of space
9. Setting up GDT, IDT
The kernel is reset
10. Open protection mechanism
11, the control right jump

4. Unzip the kernel
This part of the code is in Src/arch/i386/boot/compressed/head. s, the setup jumps in.
After a small amount of detection to decompress the kernel code into the temporary space, and then determine whether the need to merge and move the kernel, here is the large kernel (see Appendix Kernel build process), need to move, so first the removal and merge kernel code first copy to 0x10000, and then jump to 0x10000, The code completes merging and moving the kernel to 0x100000, and finally jumps to that location.

5. Enter Kernel core initialization
This part of the code is in Src/arch/i386/boot/kernel/head. S, by Src/arch/i386/boot/compressed/head. s jumps in.
This section begins with the real kernel, establishes a preliminary page table for the system, enables paging, loads the new GDT, IDT, sets the stack pointer register%esp to the No. 0 process init_task The stack of kernel stacks is low, and the last jump is located in the file src/init/ MAIN.C System Initialization entry function Start_kernel () for system initialization work.

Second, the process of initialization
The initialization process is done by the system initialization process Init_task (also called the No. 0 process, the idle process, and finally becomes an idle process). This process completes the initialization of the system memory management subsystem, the process management subsystem, the interrupt exception subsystem, and the time measurement subsystem. Finally, a kernel thread named Init is created by the function Kernel_thread (), and then the NO. 0 process calls the Scheduler schedule (), freeing the processor to become a system idle process.
After kernel thread init obtains the processor, first completes the application processor initialization in the symmetric multiprocessor system, then mounts the system root file system, completes the system bus, the network protocol stack and so on initialization. The last Call to Execve () starts execution of the user-state program/sbin/init, at which point the kernel thread init transformation is called the user process.
The init process reads the/etc/inittab file (Debian does not have the file, all the configuration information is in the/etc/event.d/directory), and initializes it based on this file. The phrase "Si::sysinit:/etc/rc.d/rc.sysinit" in/etc/inittab (see note) indicates that the system needs to actively use the shell script "Rc.sysinit" to set up the system environment. However, the file name is not the same in each version, and you need to check the confirmation yourself.
The main initialization process for/etc/rc.d/rc.sysinit is:
(1) Get the network environment and host type. The network environment settings file "/etc/sysconfig/network" is read first to obtain a network environment such as host name and default gateway.
(2) Test and load the memory device/proc and USB device/sys. In addition to/proc, the system proactively detects if a USB device is available and actively loads the USB drive, attempting to load the USB file system.
(3) Decide whether to start SELinux.
(4) Detection of interface devices and testing of Plug and Play (PNP) parameters.
(5) Loading of user-defined modules. The user can then "/etc/sysconfig/modules/*.modules" to add a custom module, which will be loaded into the system.
(6) Load the relevant settings of the core. Another file "/etc/sysctl.conf", according to the setting value of this file configuration function.
(7) Set the system time (clock).
(8) Set the font style for the console of the terminal.
(9) Set up raid and LVM and other hard disk functions.
(10) View the test disk file system in a way.
(11) The conversion of disk quota quota.
(12) Re-loading the system disk in read mode.
(13) Start the quota function.
(14) Start the system random number device (generate random number function).
(15) Clear the temporary files during the startup process.
(16) Load the boot information into the "/VAR/LOG/DMESG" file.

After the Rc.sysinit is finished, the appropriate level of startup scripts are run according to the runlevel specified in file/etc/inittab, which controls a background service process running, and the startup script for each run level is at the/etc/rc.d/rc< runlevel;. The D directory. Finally, the script/etc/rc.d/rc.loacl for the user-defined startup execution program is executed. The script begins with a statement that clearly knows its purpose
# This script is executed *after* all and the other init scripts.
# can put your own initialization stuff in here if you don ' t
# want to does the full Sys V style init stuff.

After completing all system startup tasks, Linux will start the terminal or X-window to wait for the user to log in

Report:
First, the kernel image building process
1. Build kernel image Vmlinux
Based on the configuration in the kernel configuration file Src/.config, the system core components and options are compiled for built-in system components, resulting in a core kernel core without compression based on the link script arch/i386/kernel/vmlinux.lds src/ Vmlinux
2, compression of the Vmlinux
Use objcopy to delete vmlinux redundant information, convert to Raw binrary format, then compress using gzip, and finally use the linker LD according to the link script arch/i386/boot/compressed/ VMLINUX.SCR converts the compressed generated vmlinux.bin.gz into a linked, relocatable format file PIGGY.O.
3. Build the vmlinux image containing the extracted code
Using the LD linker to link PIGGY.O and HEAD.O and MISC.O to generate a new file Vmlinux, where MISC.O contains the zlib algorithm of the extract code of the C language implementation, HEAD.O contains the assembly code used to call the decompressor setup parameters and the heap used by the decompression process Stack. Then call the decompression code in MISC.O to unzip the compressed kernel.
4, the Vmlinux to re-delete information
Remove excess information from Vmlinux by objcopy
5. Build kernel image Bzimage
Build tool build with kernel mirroring will Bootsect (Src/arch/i386/boot/bootsect. S), Setup (Src/arch/i386/boot/setup. S), vmlinux.bin three files in a new Bzimage file

Second,/etc/inittab example:
[[Email protected] ~] #vi/etc/inittab
# Set the system to start the default Run level settings item
Id:3:initdefault:

# before starting the run-level service, use the setup file to detect and initialize the system environment:
Si::sysinit:/etc/rc.d/rc.sysinit

# 7 Script drop location paths for services that need to be started for different run levels:
10:0:WAIT:/ETC/RC.D/RC 0
11:1:WAIT:/ETC/RC.D/RC 1
12:2:WAIT:/ETC/RC.D/RC 2
13:3:WAIT:/ETC/RC.D/RC 3
14:4:WAIT:/ETC/RC.D/RC 4
15:5:WAIT:/ETC/RC.D/RC 5
16:6:WAIT:/ETC/RC.D/RC 6

# whether to run the settings item that was restarted by pressing [Ctrl]+[alt]+[del]:
Ca::ctrlatdel:/sbin/shutdown-t3-r now

# Number of native terminal launches:
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty Tty2
3:2345:respawn:/sbin/mingetty Tty3
4:2345:respawn:/sbin/mingetty Tty4
5:2345:respawn:/sbin/mingetty Tty5
6:2345:respawn:/sbin/mingetty Tty6

# Start the Script Setup project in x Window (Run Level 5) environment
X:5:once:/etc/x11/prefdm-nodaemon

Iii. Brief Introduction to modern guidance
Modern Linux mostly uses a standalone bootloader to perform kernel loading, such as the most common grub boot loader.

Grub Loading Process
Installing grub on the hard disk requires three steps to fully load into memory:
1.BIOS loads the master boot record MBR (512 bytes) to the memory 0X7C00~0X7DFF location and jumps to execute 0X7C00 instructions. These instructions set the stack (sp=0x2000), probe hard disk/floppy read mode (LBA/CHS) and save. Regardless of LBA or CHS, the code to the memory 0x7000 location is loaded in another sector (the sector location is determined during the installation process) and then copied to the memory 0x8000. Jump to 0x8000 execution.
2.0X8000~0X81FF stores the sectors that were loaded in the first step. This part of the code takes advantage of the read mode (mode) saved in the first step, as well as the stack (0x2000), and the sector position and sector number specified at installation, looping through the rest of the grub to the beginning of the memory 0x8200. At cyclic loading, for each iteration, it is loaded into the memory 0x7000 first and then copied to the correct location starting from 0x8200.
3.0x8200~x (x<0x10000) stores the instructions loaded in the second step. This part of the instruction is a compression package containing the self-extracting algorithm Lzma (the startup and decompression part is uncompressed and corresponds to startup.) S and Lzma_decode. S file). Grub is here to enter the CPU protection mode, self-extracting and releasing to 0x10000 start memory, unzip the finished copy back to the original location, and then call Grub_main, initialize the system, load the module, and enter normal or rescue mode. GRUB will load the operating system and run according to the configuration file or user input.
4. If you need to start Linux,grub will load the kernel image Vmlinuz and INITRD temporary RAM disk into memory according to the corresponding configuration file (grub.cfg or other file), and pass the relevant kernel parameter information according to the multiboot boot specification.
5. If you need to launch an operating system such as Windows, you can use two scenarios: ① uses GRUB to load the original Windows MBR or PBR into 0x7c00, reverts to real mode, jumps execution, and reboots the system. ② load Microsoft Boot manager ntldr or BOOTMGR, directly complete the startup of the Microsoft System.

The new computer already uses UEFI's PC firmware instead of the traditional BIOS, and changes the MBR disk partition type to GPT disk type for many years.
The comparison of the two disk types is as follows:
The MBR uses 64 bytes from the first sector of the disk to save the disk partition table, supporting only 4 primary partitions. For historical reasons, the partition size description in the partition table uses a 32-bit integer, so the maximum supported hard disk capacity is 2^32 sector, 2TB, now it seems that the hard disk capacity has exceeded this limit;
The first sector of the GPT partition is similar to the MBR, but there is only one primary partition, the primary partition size is the entire disk size, and the partition ID is (0xFE), which is designed to prevent GPT-aware software from being used as an unpartitioned disk. The sector 2 of the GPT disk stores GPT disk information, which defines the GPT disk signature, partition table size, partition table location, data checksum, and so on. Several sectors starting from 3 sectors are GPT partitioned tables, each partition table entry 128 bytes, which includes a partition starting sector 64-bit integer, and a partition end sector 64-bit integer, so the GPT partition supports up to 2^64 sectors, size 8ZB disks. Backward from the last sector of the disk, storing a backup of the GPT partition table.
The comparison of the two computer firmware is as follows:
The BIOS uses the assembly to write, the entire system runs in the x86 real mode, has the programming difficulty, the hardware addressing ability limited limitation.
Most of the UEFI code is written in the C language.

When the BIOS starts, the steps to load the operating system are done by loading the MBR of the disk.
At Uefi boot time, UEFI initializes the CPU and memory, etc., and for the current common Uefi_x64,uefi the processor initializes the bit 64-bit mode and the memory is in flat mode. UEFI supports partitioned tables and file systems. UEFI will load/efi/boot/bootx64.efi as the system boot file from the EFI (ESP) partition. At the same time, UEFI supports multi-system support by storing multiple operating system Boot manager load paths in the motherboard's storage.

This article is from the "No Front" blog, please be sure to keep this source http://qianyang.blog.51cto.com/7130735/1620568

Llinux START Process

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.