4.98 seconds after pressing the power button

This article only explains the following question:

What happened in the few seconds after we pressed the power button?

This seems to be a question that a lot of people want to understand, but what makes me wonder: Why can't I find an answer to a question that seems so simple?

Most of the descriptions I found are like this:

According to the "boot sequence", the BIOS transfers control to the storage device that ranks first: the hard disk. Then look for the partition of the master boot record in the hard disk. This partition tells the computer where the operating system is and loads the operating system into the memory. Then you can see the classic boot interface, and the boot process is complete.

This description is simply magical, why is the BIOS dominating all this? How to call it in the order of activation ? This partition is loaded into the memory, and then how do you tell the computer where the operating system is? I can't stand such a magical description, I have to make it clear.

To learn something first, you must have a pre- knowledge. We treat it as a known one. I can’t start with the principle of forming molecules from atoms.

What is the pre-knowledge for learning the computer startup process? I expect you to know the following:

  1. The memory is a place where data is stored. Given an address signal, the memory can return the data corresponding to the address.

  2. The way the CPU works is to constantly fetch instructions from memory and execute them.

  3. Which address of the memory the CPU fetches from is determined by the value in a register, this value will continue to perform +1 operation, or a jump instruction specifies its value.

Well, you only need to know these three pre- knowledges, and you can professionally explain the computer's startup process.

1. Why is the BIOS dominated?

It is said that after booting, the BIOS will start to run its own program, perform hardware self-check, and load the boot area. I am dissatisfied. Why is the program in BIOS executed after booting? Why is it not in memory? Why is it not in the hard drive?

Okay, don't doubt the pre-knowledge. The working method of the CPU is to constantly fetch instructions from the memory and execute them. Then why is it said to execute the programs in the BIOS? This has to talk about memory mapping .

Two, memory mapping

The width of the CPU address bus determines the size of the accessible memory space . For example, the width of the 16-bit CPU address bus is 20 bits, and the address range is 1M. The width of the 32-bit CPU address bus is 32 bits, and the address range is 4G. You can calculate the address range of our current 64-bit machine.

However, the accessible memory space is so large that it does not mean that all of it is used for memory. In other words, not only memory is the addressable object, but also some peripherals must be accessed through the address bus. How to access these What about peripherals?

One method is memory mapping . The simple understanding is to map the video memory, hard disk controller, etc. to the memory. We read or write in the corresponding location, which is equivalent to reading or writing in the corresponding location of the video memory and other peripherals. Write, so that peripherals and memory are unified.

The same is true for the BIOS, it will also be mapped to the memory.

Great, now it can be explained with simple pre-knowledge, we continue to push down.

3. Memory distribution in real mode

Just said that the memory is divided into areas for various peripherals, so the question naturally arises, which area is allocated to which peripheral? If it is a rule, it should be better to have a table. Yes, it is true. It is the memory distribution in real mode. I drew a picture for it:

Oops, I am really a little angel. I have shown the proportions. If you can find something more intuitive than mine on the Internet, please leave me a message.

I will explain after the real mode. The simple understanding now is that only 1M of memory is available when the computer is just turned on.

We see that the memory is divided up by various peripherals, that is, mapped in the memory.

The BIOS is more ruthless. Not only its space is mapped to the memory 0xC0000-0xFFFFF, but the program inside it also occupies some areas at the beginning. For example, the interrupt vector table is written at the beginning of the memory. This is really so-called first come first served.

4. How to start execution from the program in BIOS

Okay, now we know that the information in the BIOS is mapped to the memory location 0xC0000-0xFFFFF, and the most critical system BIOS is mapped to the location 0xF0000-0xFFFFF.

If I say now that the CPU boot is to execute the code in this area, and then Barabara will boot up after a short operation, you must spray me, why did you stop here? Why not start from the beginning?

This naturally has a conjecture, we need to use another pre-knowledge, which is where the CPU fetches execution from the memory and executes it?

Is the address value in the PC register .

The entry address of the BIOS program, which is the start address, is 0xFFFF0 (that's what people write), that is, when the power-on button is pressed, there must be a magical power to change the value in the pc register to 0xFFFF0, and then the CPU starts to go non-stop Ran up. Yes, the next sentence may be the answer you have been looking for for a long time, please be prepared:

At the moment you turn on the computer, the PC register of the CPU is forced to initialize to 0xFFFF0 .

(To be more specific, the CPU initializes the segment base address register cs to 0xF000 and the offset address register IP to 0xFFF0. According to the final address calculation rules in real mode, the segment base address is shifted 4 bits to the left, plus the offset Address, the final physical address is obtained, that is, the PC register address abstracted out is 0xFFFF0.)

When I was studying this piece of knowledge, it was only when I saw this sentence that the doubts that had been accumulated in my heart for a long time were resolved. What a simple and rude truth. At this point, I also breathed a sigh of relief, because the rest of the process is almost just like a running account.

As for how to force initialization, I think it has crossed the boundary of pre-knowledge. Moreover, the hardware implementation of various manufacturers is not necessarily the same. There are many ways and it is very simple. It doesn't make much sense to discuss it.

5. What program is written in the BIOS?

Well, we now know that the CPU forcibly initializes its pc register to the entry address of the BIOS program at the moment of booting. From here on, the CPU ran forward without stopping.

Then the next question seems to be asked very naturally, that is, what is written in the BIOS program?

It is also inappropriate to paste all the binary information in the BIOS program. We analyze some of the main ones. Let's first guess, you see that the entry address is 0xFFFF0, indicating that the program is executed from here. The lower boundary of the memory in real mode is 0xFFFFF, which means that there are only 16 bytes left to write code. What is enough?

If you are interested, you should be able to guess that the entry address may be a jump instruction to jump to a larger space to perform your own task. That's right, the machine instruction stored at 0xFFFF0, translated into assembly language is:

jmp far f000:e05b

It means to jump to the physical address 0xfe05b and start execution (recall the address calculation method in real mode mentioned earlier).

Starting at address 0xfe05b, it is the code that the BIOS really works. This code will detect some peripheral information, initialize the hardware, establish an interrupt vector table and fill in the interrupt routine. Don't expand the part here. This is just a hard-coded program, and it is not helpful to understand the boot process. Let's look at the next wonderful part, which is the last task of the BIOS: loading the boot area .

Six, what is 0x7c00

The place to be more real is to be more real. I will never let the words that load this kind of magic appear here. Let's break it down now.

In fact, the term is not magical. Loading in the computer field refers to the process of copying a program on a device (such as a hard disk) into the memory . The translation process of loading the boot area is that the BIOS program copies the contents of the boot area to a certain area in the memory .

Okay, the problem naturally arises again. Where is the startup zone? Where is the memory copied to? and then? We will answer one by one.

What is the boot zone? Even if you don't know it, you should be able to guess that it must be an area that meets a certain characteristic, so people call it a startup area. What characteristics should it meet? Don’t worry, I don’t know if you have any experience in setting the BIOS boot sequence. Usually there are U disk boot, hard disk boot, floppy disk boot, CD boot and so on. The BIOS will read these boot disks in order, which is located in the 0 disk 0 track. 1 The contents of the sector .

As for the division of the disk format, this article will not explain it. In short, for the memory, we can get the data at that address by giving a numeric address. For the disk, we need to give the head, cylinder, and sector. The data that can locate a certain location with only one piece of information is just a way of describing the location.

Then, the content of this 0 disk 0 track 1 sector has a total of 512 bytes. If the last two bytes are 0x55 and 0xaa respectively, then the BIOS will consider it to be a boot area . If it is not, then proceed to the next device in order to find the content located in the 0 disk 0 track 1 sector. If you find that none of them meets the conditions at the end, it will directly report an error of no start area.

What does the BIOS do after finding this boot area? Oh, I mentioned earlier that it is loading, which means copying the content of these 512 bytes, one bit is not enough, to the memory location 0x7c00 . How is it copied? Of course it's instructions. Which instructions? Here I can only simply say that there are in and out in the instruction set, which are used to copy the data in the peripheral to the memory, or copy the data in the memory to the peripheral. Use these two instructions and the peripheral to provide us This can be done by the way of reading.

After the copy is completed, our program should take over the rest of the process, and the mission of the BIOS is over!

But before it ends, it must jump to the address 0x7c00, and the instruction starts to execute from here.

Huh? I don’t know if you noticed that we seem to have translated a previous magic language into adult words again without knowing it. At the beginning we said:

The BIOS transfers control to the storage device that ranks first.

So what does this sentence mean? That is, the BIOS copies the 512 bytes of the boot area to the 0x7c00 location of the memory, and uses a jump instruction to point the value of the pc register to 0x7c00 . You see, there are only a few more words, so let's make the question clear and simple.

Oh, by the way, there seems to be one question left now, why does it have to be 0x7c00? Good question. Of course, the answer is very simple. That's how the BIOS development team made it, and it won't be easy to change afterwards, otherwise it's not compatible. Why is it not easy to change? Let's look at a simple 512-byte code in the boot area. (The code is excerpted from "30 days of self-made operating system")

; hello-os
; TAB=4

  ORG  0x7c00   ;程序加载到内存的 0x7c00 这个位置

;程序主体

entry:
  MOV  AX,0   ;初始化寄存器
  MOV  SS,AX
  MOV  SP,0x7c00
  MOV  DS,AX   ;段寄存器初始化为 0
  MOV  ES,AX
  MOV  SI,msg
putloop:
  MOV  AL,[SI]
  ADD  SI,1
  CMP  AL,0   ;如果遇到 0 结尾的,就跳出循环不再打印新字符
  JE  fin
  MOV  AH,0x0e   ;指定文字
  MOV  BX,15   ;指定颜色
  INT  0x10   ;调用 BIOS 显示字符函数
  JMP  putloop
fin:
  HLT
  JMP  fin
msg:
  DB  0x0a,0x0a  ;换行、换行
  DB  "hello-os"
  DB  0x0a   ;换行
  DB  0    ;0 结尾

  RESB 0x7dfe-$   ;填充0到512字节
  DB 0x55, 0xaa   ;可启动设备标识

We look at the first line:

ORG  0x7c00

This number is the loading location of the boot area just mentioned. This line of assembly code simply means adding 0x7c00 to all the addresses below. Just because the BIOS loads the boot area code here, there is an offset, so everyone who writes the boot area code needs to write such a code at the beginning, otherwise it will all be serialized.

And just because all those who write the operating system, the first line of assembly code in the boot area is dead with this number, then the number originally set by the BIOS developer is not easy to change, otherwise it has to contact the developers of each operating system one by one. , I said, I’ll change this address, and you will change it accordingly. Pushing another team to change a code in the company has to be a lot of trouble, think about how much manpower such a push takes. Moreover, even if it is changed, the previous code is not compatible anymore. This should not be scolded by people.

Look at the last line again:

DB 0x55, 0xaa

This also verifies that the last two bytes of the 512 bytes we said before must be 0x55 0xaa, the BIOS will consider it to be a boot area, and then load it, and nothing more.

Looking back at the value 0x7c00, it is actually a regulated value, but someone will still ask, it must have its rationality. In fact, my explanation can only say that people set this value, and later people explain this rationality for them. It does not mean that people must have thought this way at the beginning, just like we are doing Chinese reading comprehension problems.

The first BIOS development team was the IBM PC 5150 BIOS. The first operating system considered at the time was the DOS 1.0 operating system, and the BIOS team assumed that it served it.

But the operating system has not yet come out, and the BIOS team assumes that the minimum memory required for its operating system is 32 KB. The BIOS hopes that the boot area code loaded by itself is as far back as possible, so that it is "safe" and will not be overwritten by other programs prematurely. But if only 512 bytes are left, it feels too overhanging, and there is still some stack space that needs to be reserved, so expand it to 1 KB. So the end of 32 KB is 0x8000, minus 1KB (0x400), which is exactly equal to 0x7c00. Wow, too precise, this can be an explanation.

Seven, what is written in the code in the startup area

In fact, at this point, I should stop this article abruptly, because the original problem has been solved, and the CPU has started to run non-stop from our expected position. Everything is difficult at the beginning, and the rest is the operation. The system can play whatever it wants.

But I don’t think it tastes enough, and there seem to be some questions lingering in your mind. For example, this question:

What is written in the code in the launch area? Is 512 bytes the entire operating system content?

This is a good question. 512 bytes really can't do anything. Now the operating system has to be counted in M ​​units. 512 bytes are far from enough. What is going on?

In fact, we can guess from the previous thinking that the BIOS loads the 512-byte boot area content into the memory with very little code, and jumps over to start execution. According to this routine, can the 512-byte boot area code be able to load more operating system programs stored in the disk to a certain location in the memory and then jump to it?

That's right, this is the routine. So the BIOS is responsible for loading the boot area, and the boot area is responsible for loading the real operating system kernel . Does this work in harmony?

Since the disk used for the boot disk is made by the manufacturer who writes the operating system, commonly known as the boot disk, he must also know which sector of the disk the core code of the operating system is stored in, so this sector is stored in the boot area, and After that, many and many sectors (depending on how big the operating system is) are read into the memory, and then jump to the beginning of the beginning of the program. Where do you jump to? This is not as classic as the number 0x7c00. Different operating systems are definitely different, and there is no need to pre-determine it. Anyway, the person who writes the operating system can set one for himself. Don’t cover the area used by other key devices. Great.

8. What did the operating system kernel write?

Okay, now after several rounds of jumping and jumping, finally jumping to the kernel code, let's review it together:

  1. Press the power-on button, and the CPU will forcibly initialize the value of the PC register to 0xffff0, which is the entry address of the BIOS program (one jump)

  2. The entry address is a jump instruction, jump to 0xfe05b position, start execution (two jumps)

  3. After performing some hardware testing, the last step is to load the contents of the boot area into memory 0x7c00 and jump to it (three jumps)

  4. The boot area code is mainly to load the operating system kernel and jump to the loading point (four jumps)

After these four consecutive jumps, I finally came to the world of operating systems. The rest of the content can be said to be the principles described in the entire operating system course, such as segmentation, paging, establishment of interrupts, device drivers, memory management, and process management. , File system, user mode interface, etc.

You may have heard more or less of these terms in operating system courses. If you learn it well, you must know the general principles. But for a hardcore dog like the author who has studied the Linux kernel source code from beginning to end, These concepts are not just boring concepts in books, but flexible and flexible. On every line of code in the operating system, some show the author's incomparable wisdom, and some let me see the author's surrender due to hardware settings.

If this article brings up your curiosity about the operating system, I suggest you also find time to read it, join me in the pit, you will find that the door of a new world opens to you

Nine, reference materials

Well, this time I really want to end it. I believe that if you really read the full text, the computer startup process can be said to have a more concrete understanding. If you want to go into the details, that is, to understand every point of the whole process, then you have to work hard.

Beginners recommend two books, which can be read in order. I wish you a pit:

  • "30 days self-made operating system"

  • "Operating System Truth Restoration"

Finish

Low concurrency programming, very decadent on Monday, hardcore on Thursday

Guess you like

Origin blog.csdn.net/coderising/article/details/113488638