Original article title: How Computers Boot Up
Address: http://duartes.org/gustavo/blog/
[Note: I have a limited level, so I have to pick some excellent articles from foreign experts for translation. I will review it myself, and I will share it with you.]
The previous article introduced the motherboard and memory ing of Intel computers, thus setting a scenario for the system boot phase. Booting is a complex, skillful, and interesting process involving multiple stages. Lists the summary of this process:
Overview
After you press the power key of your computer (do not press it now !), The machine starts to run. Once the motherboard is powered on, It initializes its firmware (firmware)-the chipset and other fragmented things-and tries to start the CPU. If something goes wrong at this time (for example, the CPU is broken or is not installed at all), it is very likely that the computer does not have any movement, except that the fan is switching. Some boards may sound when a CPU failure or failure occurs, but in my experience, most machines will be frozen. Some USB or other devices may also cause the machine to become frozen during startup. One possible solution for computers that previously worked normally and suddenly experienced such symptoms is to remove all unnecessary devices. You can also disconnect only one device at a time to find out which one is the culprit.
If everything is normal, the CPU starts to run. In a multi-processor or multi-core processor system, a CPU is dynamically assigned as a bootstrap processor (BSP) to execute all BIOS and kernel initialization code. The remaining processors, now called application processor (AP), remain shut down until the kernel explicitly activates them. Although Intel CPU has been developing for many years, it has maintained full backward compatibility, so the performance of modern CPU can be exactly the same as that of Intel 1978 in 8086. In fact, when the CPU is powered on, it does this. In this basic power-on process, the processor works in real mode, and the paging function is invalid. At this time the system environment, like the old MS-DOS, only 1 MB memory can be addressable, any code can read and write any address of memory, there is no concept of protection or privilege level.
After the CPU is powered on, most registers have well-defined initial values, including the instruction pointer register (EIP), which records the memory address of the next instruction to be executed by the CPU. Although the Intel CPU can only address 1 MB of memory at this time, with a strange technique, a hidden base address (actually an offset) will be added to the EIP, the result points to the address 0xFFFFFFF0 of the first command to be executed (16 bytes long, at the end of the 4 GB memory, much higher than 1 MB ). This special address is called the reset vector, which is also the standard of modern Intel CPU.
The motherboard ensures that the command at the Reset vector is a jump and the memory ing address at the entry point of the BIOS execution. This jump will remove the hidden base address when powering on. Thanks to the memory ing function provided by the chipset, the memory address stores the real content required for CPU initialization. All these contents are mapped from the flash memory containing the BIOS, And the RAM module only has random junk data. The following example lists the memory areas:
Important memory areas during boot
Then, the CPU starts to execute BIOS code to initialize some hardware on the machine. Then the BIOS starts the power-on self-check process (POST) to detect various components in the computer. If you cannot find an available video card, the POST will fail, causing the BIOS to go down and sending a beep message (because the prompt information cannot be output on the screen at this time ). If the video card is normal, the computer looks like it is actually running: display a manufacturer's custom trademark, start memory self-check, and the angels give a loud horn. Some other POST failures, such as the absence of a keyboard, can cause downtime and Display Error information on the screen. In fact, POST is the detection and initialization, but also the resources of all PCI devices-interrupt, memory range, I/O port. The modern BIOS follows the Advanced Configuration and Power Interface (ACPI) Protocol to create data tables used to describe devices. These tables will be used by the operating system kernel in the future.
After the POST, the BIOS is ready to boot the operating system, which must exist somewhere: hard disk, optical drive, and floppy disk. You can customize the actual sequence of BIOS boot device search. If no suitable boot device is found, the BIOS displays an Error message and stops working. For example, "Non-System Disk or Disk Error" does not cause a System Disk or drive failure. A bad hard disk may cause this problem. Fortunately, in this article, the BIOS successfully found a drive that can be properly booted.
Now, the BIOS will read the first sector (0 sector) of the hard disk, containing 512 bytes. The data is called the Master Boot Record (MBR ). Generally, it contains two extremely important parts: one is the boot program related to the operating system that begins with MBR, and the other is the disk partition table that follows it. The BIOS does not care about these things at all: it simply loads the MBR content to the memory address 0x7C00 and jumps to this place to start execution, no matter what the code in the MBR is.
Master Boot Record
This special code in MBR may be a Windows boot loader, a Linux boot loader (such as LILO or GRUB), or even a virus. In contrast, a partitioned table is standardized: it is a 64-byte block and contains 4 16-byte records, describes how a disk is split (so you can install multiple operating systems on a disk or have multiple independent volumes ). Traditionally, Microsoft's MBR code looks at the partition table, finds a (unique) active partition, and loads the boot sector of that partition ), and execute the code.Boot SectorIs the first sector of a partition, rather than the first sector of the entire disk. If something goes wrong at this time, you may receive the following error message: "Invalid Partition Table" is Invalid Partition Table or the "Missing Operating System" Operating System is Missing. This informationNoFrom the BIOS, but given by the MBR program loaded from the disk. Therefore, this information depends on the MBR content.
With the passage of time, the boot loading process has become increasingly complex and more flexible. Linux boot loader Lilo and GRUB can process many types of operating systems, file systems, and boot configuration information. Their MBR Code no longer needs to follow the aforementioned "pilot from the active partition" method. However, in terms of functions, this process is roughly as follows:
1. MBR itself contains the first-stage boot loader. GRUB is called Stage 1.
2. Because MBR is small, the code is only used to load another sector containing additional boot code from the disk. This sector may be the boot sector of a partition, but it may also be a hard-coded sector location in the MBR.
3. MBR is used with the code loaded in step 1 to read a file, which contains the pilot program required for the next stage. This is the "Phase 2" Bootstrap in GRUB, and C: \ NTLDR in Windows Server. If Step 2 fails, you will receive an error message in Windows, for example, "NTLDR is missing" NTLDR is missing. Stage 2 code further reads a boot configuration file (for example, GRUB. conf in grub and boot. ini in Windows ). Then, you can either display some boot options to the user or directly guide the system.
4. At this time, the boot loader needs to start the operating system core. It must have enough information about the file system to read the kernel from the boot partition. In Linux, this means to read a file named "vmlinuz-2.6.22-14-server" containing a kernel image, load it into memory and jump to execute the kernel boot code. In Windows Server 2003, a kernel startup code is separated from the kernel image itself and is actually embedded into NTLDR. After initialization, NTDLR loads the kernel image from the "c: \ Windows \ System32 \ ntoskrnl.exe" file, just as GRUB did, and jumps to the kernel entry point for execution.
There is also a complicated area to mention (which is also the reason why I say guidance is skillful ). Even if the Linux kernel image is compressed, it cannot be inserted into the KB available RAM in the real-world mode. My vanilla Ubuntu kernel is compressed to 1.7 MB. However, the boot loader must run in real mode to call the BIOS code to read the disk. Therefore, the kernel cannot be used at this time. The solution is to use a highly respected "virtual mode ". It is not a real processor running mode (I hope Intel engineers will allow me to do this), but a special technique. The program constantly switches between the real mode and the protection mode, so that the access to memory larger than 1 MB can also use BIOS. If you read the GRUB source code, you will find that all the switches are done (check the programs in the stage2/directory and call the real_to_prot and prot_to_real functions ). At the end of this tricky process, the loader finally made every effort to plug the entire kernel into the memory, but after that, the processor remained running in real mode.
So far, we have arrived at the time from "boot loading" to "Early kernel initialization", as shown in the first figure. After the system completes the warm-up, the kernel is opened and the system starts to run. The next article will take you step by step into the Linux kernel initialization process. You can also refer to the Linux Cross reference resources. I can't do the same for Windows, but I will point out the points.