Operating system boot sequence

Overview

Here, taking the x86 processor as an example, the  machine will execute the first instruction
when it starts up . This command will execute the bios and give control to the bios. bios completes the hardware quality inspection, then reads the bootloader from the hard disk into the memory, executes the bootloader , and passes the control to the bootloader  . The bootloader is responsible for enabling the protected mode, establishing the segment mechanism and loading the operating system , and then giving the control to the operating system

boot sequence

first instruction

The first instruction of the execution machine is to jump to the bios for execution, so you need to know where the bios is? How is the cpu addressed?

where is bios

The bios is solidified in the memory EPROM, and it will not be lost (non-volatile) when the power is turned off, so the address of the biod is fixed , because after the cpu is powered on for the first time, the registers will have default initial values . So the bios address = the default value of the addressing register is fine. 
Addressing is done by the segment register and the instruction pointer. The bios address is represented by CS:IP=16*CS+IP (code segment register: instruction pointer). 
The default value of CS is FFFF0000H, and the default value of IP is 0000FFF0H. 
CS:IP=FFFFFFF0H

addressing mechanism

Addressing is done by the segment register and the instruction pointer. Because the memory is very large, and the memory operated within a period of time has spatial locality, the memory can be divided into sections and located by segment registers, and the specific code and data 
segments are located in this section of memory by the instruction pointer. Registers: 
- CS - code segment, code segment register 
- DS - data segment, data segment register 
- ES - extra segment, additional segment register 
- SS - stack segment, stack register

The instruction register IP (instruction pointer) is the program counter PC that we are familiar with. 
An address is represented by a segment register: instruction pointer=16*segment register+instruction pointer
Since at this stage, the machine is still in real mode , and real mode is 20-bit addressing, so it is not enough to use segment register (16 bits) alone. of. 
Because real mode has only 20-bit addressing, the maximum callable space is only 1M

bios

BIOS is responsible for hardware self-check and initialization and loading the bootloader into the memory 
to ensure that the hard disk, memory... will not go wrong in the follow-up work. 
Then load the bootloader to 0x7c00 in memory , and then jump to 0x7c00 for execution.

bootloader

The bootloader needs to enable the protected mode (real mode -> protected mode), establish the segment mechanism and load the operating system

bootloader structure

The first sector of the bootloader in the hard disk is also called the main boot sector, and a sector is 512 bytes, so the bootloader is also 512 bytes. 
Among them, 400+ bytes are the startup code , which is responsible for completing the work that the bootloader needs to complete, and the remaining bytes are recording the hard disk partition table. 
bootloader structure

Create segment mechanism

When there is no segment mechanism, CS:IP gets the real physical address, and the segment mechanism is for more flexible segmentation. 
The segment mechanism is to do a layer of mapping in the middle. CS first locates the corresponding segment descriptor of the GDT (global description table), and then finds the starting address of the segment from the segment descriptor, and forms the physical address with the IP.

Enable protected mode

Set the 0th bit of the CR0 register (control 0 register control register) to 1, so that the protection mode is turned on

View the original text: http://blog.zswlib.com/2016/11/15/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%90%AF% E5%8A%A8%E9%A1%BA%E5%BA%8F/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326400610&siteId=291194637