Linux boot process analysis

  
                  

The boot process refers to the entire process from turning on the computer until LINUX displays the user login screen. Analysis of the Linux boot process is also a good way to understand the core working principle of LINUX.

Start the first step - load the BIOS

When you turn on the computer, the computer will first load the BIOS information. The BIOS information is so important that the computer must find it at the very beginning. . This is because the BIOS contains information about the CPU, device startup sequence information, hard disk information, memory information, clock information, PnP characteristics, and so on. After that, the computer has a spectrum of knowing which hardware device should be read. After the BIOS hands over the control of the system to the first sector of the hard disk, Linux begins to control the system.

Start the second step--Read MBR

The first sector of the 0th track on the hard disk is called MBR, which is the Master Boot Record, which is the master boot record. It is 512 bytes, but it can store pre-boot information and partition table information. It can be divided into two parts: the first part is the boot (PRE-BOOT) area, which occupies 446 bytes; the second part is the partition table (PARTITION PABLE), which has 66 bytes, and records the partition information of the hard disk. One of the roles of the preboot area is to find the partition marked as ACTIVE and read the boot area of ​​the active partition into memory.

After the system finds the MBR of the hard disk specified by the BIOS, it will copy it to the physical memory where the 0times address is located. In fact, the content copied to the physical memory is the Boot Loader, and specific to your computer, that is lilo or grub.

Start the third step -- Boot Loader

Boot Loader is a small program that runs before the operating system kernel runs. Through this small program, we can initialize the hardware device and create a map of the memory space, so as to bring the system's hardware and software environment to a suitable state, in order to make all the preparations for the final call of the operating system kernel. Usually, BootL oade: is heavily dependent on hardware. Different architectures have different Boot Loaders.

The boot sector content of Linux is a program written in assembly language. The source code is in arch/i386/boot (the CPUs of different systems have their own boot directories), and there are 4 program files: < Br>

◎bootsect.S, the main program of the boot sector, the code after assembly does not exceed 512 bytes, that is, the size of a sector

◎setup.S, boot helper

◎edd.S, part of the auxiliary program for supporting the BIOS enhanced disk device service

◎video.S, another part of the auxiliary program for screen display during booting

The system reads the grub configuration information in the memory (usually menu.lst or grub.lst) and starts different operating systems according to this configuration information.

Start the fourth step - load the kernel

According to the path of the kernel image set by grub, the system reads the memory image and decompresses it. At this point, the screen will generally output the prompt "Uncompressing Linux". When the decompressed kernel is complete, the screen outputs <OK, booting the kernel”.

The system places the decompressed kernel in memory and calls the start_kernel() function to start a series of initialization functions and initialize various devices to complete the Linux kernel environment. At this point, the Linux kernel has been built, and Linux-based programs should work fine.

start_kenrel() is defined in init/main.c, which is similar to the main() function in a normal executable. What the system did before is only some that allows the kernel to execute at a minimum. The initialization process, the real kernel initialization process is only starting from here. The function start_kerenl() will call a series of initialization functions to complete all aspects of the kernel itself, in order to finally establish a basic Linux kernel environment.

The following operations are mainly performed in start_kernel():

(1) The current kernel version information is printed on the screen.

(2) Execute setup_arch() to set the system structure.

(3) Execute sched_init() to initialize the scheduling mechanism of the system. First, the runqueque on each available CPU is initialized; then the process 0 is initialized (its task struct and system empty M stack have been allocated in startup_32()) as the system idle process, that is, the process that occupies the CPU when the system is idle.

(4) Execute parse_early_param() and parsees_args() to parse system startup parameters.

(5) Execute trap_in itQ, first set the system interrupt vector table. The trap gate of 0-19 is used for CPU exception handling; then the system call vector is initialized; finally, cpu_init() is called to improve the initialization of the CPU, which is used to support the process scheduling mechanism, including setting the flag register, task register, and initialization program debugging. Related registers and so on.

(6) Execute rcu_init() to initialize the Read-Copy Update mutex mechanism in the system.

(7) Execute the init_IRQ() function to initialize the interrupt for the peripheral and complete the final initialization process for the IDT.

(8) Execute the init_timers(), softirq_init() and time_init() functions, respectively, the initial system timer mechanism, the soft interrupt mechanism, and the system date and time.

(9) Execute the mem_init() function to initialize the page data structure descriptor of the physical memory page to complete the creation of the physical memory management mechanism.

(10) Execute kmem_cache_init() to complete the initialization of the general slab buffer management mechanism.

(11) Execute fork_init() to calculate the number of processes (threads) that the current system's physical memory capacity can allow.

(12) Execute proc_caches_init(), bufer_init(), unnamed_dev_init(), vfs_caches_init(), signals_init() and other functions to establish a dedicated slab buffer queue for various management mechanisms.

(13) Execute the proc_root_init()Wl number to initialize the virtual file system /proc.

At the end of start_kenrel(), the kernel creates the first system kernel thread (ie, process number 1) via kenrel_thread(). This thread executes the init() function in the kernel, which is responsible for the next A phase of the start task. Finally, the cpues_idle() function is called: entering the system main loop body port will always execute the instruction in the default_idle() function, that is, the CPU's halt instruction, until the other processes in the ready queue need to be scheduled, they will switch to execute other functions. At this point, the only process in the system that has a ready state is the init process (kernel thread) created by kerne_hread(), so the kernel does not enter the default_idle() function, but instead turns to the init() function to continue the boot process.

Copyright © Windows knowledge All Rights Reserved