Detailed process from CPU power up to loading OS kernel (Tsinghua University ucore-lab1 Summary One)

Source: Internet
Author: User

With the recent study of Tsinghua's OS class, first use "the language" to highly abstract describe my own understanding. When the CPU is powered on the system and we press the POWER switch, we start to initialize his register, mainly CS and EIP (this article is based on the x86 architecture), and then find a BIOS (Basic Input Output System) in the ROM, load into RAM and start executing him, After completing the self-test and initialization of the device, he loads the primary boot sector of the first device into memory based on his own internal "which device should I boot the loader" table, which means that the control of the system is transferred to the program, and then he quotes the bootloader correctly, That is what we often call the "boot program", the boot program will read into the memory of the I/O area of the hard disk (OS location) sector information, get the operating system kernel elf file, loaded into memory, and then transfer control to the OS, it's done.

(a) Those things about the BIOS

  1. Where does the address of our first instruction come from?

The CPU first initializes the registers after power-up, primarily CF registers and EIP registers, because they combine to form the linear address of the first instruction.

The address fffffff0h is beyond the 1-mbyte addressable range of the processor and in real-address mode. The processor is initialized to this starting address as follows. The CS register has both parts:the visible segment selector part and the hidden base address part. In real-address mode, the base address was normally formed by shifting the 16-bit segment selector value 4 bits to the left To produce a 20-bit base address. However, during a hardware reset, the segment selector in the CS register are loaded with f000h and the base address is LOA Ded with ffff0000h. The starting address is thus formed by adding the base address to the value in the EIP register (which is, FFFF0000 + fff0h = fffffff0h).

"Reference IA-32 Intel Architecture software Developer ' s Manual Volume 3:system Programming Guide Section 9.1.4"

Because 386 of the segment base address is not stored directly in the segment register like 8086/8088 and then left four bits formed, but by depositing the segment selector in the segment register (also called segment selector, segment selector), Then find the GDT or the LDT to get the base address and segment bounds of the segment and attributes, and so on-to avoid each memory access to the above process, in the internal processor specifically for each segment configured a 76-bit high-speed buffer register, called the Segment Descriptor cache Register, These registers are transparent to the programmer (invisible). The base address provided by the first CS register is then obtained from here.

2, what does the BIOS do?

The first instruction is a jump execution, which jumps to the first address of the BIOS to execute, and the system transfers control to the BIOS. After the BIOS first performs the hardware self-test and initial initialization, it selects a boot device (such as a floppy disk, hard disk, CD-ROM, etc.) and reads the first sector of the device (i.e. the primary boot sector or boot sector) to a specific address 0x7c00 in memory, and then the CPU control is transferred to that address to continue execution. Now that the BIOS initialization is done, further work is given to bootloader.

Load bootloader:

Bootloader's Chinese translation is called the "boot program", his hardware sector (512B) is called "Boot Sector" (boot sector), here is a simple description of the hard disk sector structure.

The hard disk is composed of a number of 512bytes sectors, which are further divided into multiple "partitions", each partition has a sector that is physically contiguous, and the first sector of each partition becomes the starting sector or boot sector. The main boot sector (boot sector) is located on the hard disk's 0 head 0 cylinder 1 sector, consisting of a master boot record and a partition table (disk Partition table) . The purpose of the master boot record is to check whether the partition is correct based on the partition table and determine which partition is the boot partition, and then load the boot partition's starting sector into memory at the end of the program.

  

The last two bytes of each starting sector (including the main boot sector) are "0X55AA" as a flag for its end.

(a) the things about bootloader

  1. What did bootloader do?

    • Switch to protected mode, enabling the staging mechanism
    • Read Disk elf Execute file format ucore OS to memory
    • displaying string information
    • Handing control over to the Ucore operating system

  2. Real mode and protection mode

A blog post to understand everything: http://www.cnblogs.com/immortal-worm/p/5867418.html

 3, about A20gate

This concept is presented here only from a higher level of abstraction. (A20gate is the 20th address line of the CPU from low to high 0 counting start number)

First in real mode, we know that only 20 of the CPU address line can be used for addressing, that is, theoretically our largest addressable range is 2^20 that is 1111 1111 1111 1111 1111=1m space, the 21st bit is 1, the back is all 0. But in fact we all know that x86 is using the "segment Register *4+EIP Register" to get a linear address. It is obvious that the maximum addressing space for this is 0xffff*4 + 0xFFFF = 0x10ffef that is 1 0000 1111 1111 1110 1111--This value is greater than the theoretical 1M, then in real mode, we have to go beyond the 1M address, introduced a kind of called Wrap-around Technology, that is, for the excess part of the 1M bit mode to take the remainder, so that there is no cross-border situation. It is not difficult at this point to find out that a20gate is off in real mode because it is the 21st address line, and the real mode only has 20 open.

So now bootloader to convert the real mode to protected mode, the first thing to do is to open the a20gate, here is related to the keyboard and the 8042 chip interaction, in the understanding of the specific open mode, to first reserve a bit of x86 IO port related knowledge.

Basic knowledge of IO port: http://www.cnblogs.com/immortal-worm/p/5867690.html

Most PCs use a keyboard controller (8042 chip) to handle a20gate.

Theoretically, the way to open the a20gate is by setting the 2nd-bit of the 8042-chip output port (64H), but in fact, when you write to the 8042-chip output port, in the keyboard buffer, there may be other data that has not yet been processed, so you must first process the data.

The process is as follows:

1. no interruption;

2. Wait until 8042 inputbuffer is empty;

3. Send the Disable keyboard operation command to 8042Input buffer;

4. Wait until 8042 inputbuffer is empty;

5. Send read 8042 outputport command;

6. Wait until 8042 OutputBuffer has the data;

7. Read 8042 OutputBuffer, and save the resulting bytes;

8. Wait until 8042 inputbuffer is empty;

9. Send the Write 8042Output port command to 8042 Input buffer;

10. Wait until 8042 inputbuffer is empty;

11. Place the 2nd position 1 (OR 2) of the bytes obtained from 8042 Outputport and write to 8042 Input buffer;

12. Wait until 8042 inputbuffer is empty;

13. Send Allow keyboard operation command to 8042Input buffer;

14. Turn on interrupts.

4. Configuring the GDT Table

  The detailed description of the GDT table is given in the previous protection mode, and now we simply load the GDT position into the DGTR register with the LGDT instruction.

5. Enable Protection mode

After 386, the CPU of the x86 architecture sets the control register, where the CR0 D0 bit determines the mode in which the CPU accesses the memory. After you have opened the A20gate and configured the GDT table, place it D0 position 1.

   6 , read the ELF-formatted operating system from the hard disk

  After the CPU enters the protection mode, it is necessary to start reading the ELF-formatted OS kernel from the corresponding location of the hard disk, which is a two-step process that reads the elf file from the hard disk and then enters the kernel of the entire OS from the header of the elf file.

Detailed process from CPU power up to loading OS kernel (Tsinghua University ucore-lab1 Summary One)

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.