The purpose of this lecture is to clarify the following question ：
What happened when we pressed the power button ？
well , This seems to be a problem that many people especially want to understand , Sometimes I wonder , Why a seemingly simple question , I can't find an answer to a direct question ？
Good question , I don't know why , But I guess it's because ：
- firstly , There are too many people who don't understand , They don't really know what happened , So I can only vaguely and roughly say something in the textbook .
- second , The person who knows the answer must be Daniel , Daniel either doesn't answer this question , Or you won't simply answer this question . And me , I think it's just between the two , Now I want to share what I know , So you find the answer here .
I think the answer to this question when you look for it is , Most of what we found were descriptions like this ：
BIOS according to “ Startup sequence ”, Transfer control to the top storage device ： 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 load the operating system into memory , Then you can see the classic startup interface , This boot process is completed .
It's a fantastic description , Why BIOS Dominating all this ？ How to call it according to Startup sequence ？ How could this partition be load It's in memory , Why tell Where's the computer operating system ？ I can't stand such magical descriptions , I have to make it clear .
First learn one thing , There must be one In front of Knowledge , We think of it as known , I can't start with atoms and molecules . So what is the pre knowledge of learning the computer startup process ？ I ask you to know the following ：
- Memory is where data is stored , Give an address signal , Memory can return the data corresponding to the address .
- CPU The way of working is to take instructions out of memory , And implement .
- CPU Which address from memory to get the instruction from , It is determined by the value in a register , This value goes on and on +1 operation , Or by a jump instruction to specify its value .
Okay , All you need to know is these three things In front of knowledge , You can explain the startup process of the computer professionally .
One 、 Why BIOS The dominant ？
It's said that when it's turned on ,BIOS I started running my own program , And hardware self check , Load the boot area again . I won't accept it , Why is it executed after startup BIOS The procedure in ？ Why it's not in memory ？ Why it's not in the hard disk ？
well , Don't doubt the pre knowledge ,CPU How it works , It is to constantly take instructions from memory and execute , Then why is it execution BIOS What about the program in ？ This has to be said Memory mapping 了 .
Two 、 Memory mapping
CPU The width of the address bus determines the amount of memory space that can be accessed . such as 16 Bit CPU Address bus width is 20 position , The address range is 1M.32 Bit CPU Address bus width is 32 position , The address range is 4G. You can calculate what we have now 64 Address range of bit machine .
But , There's so much memory available , It doesn't mean that it's all for memory , In other words, addressing objects is not only memory , There are also some peripherals that need to be accessed through the address bus , How to access these peripherals ？ It's to mark out areas in the address range , This is for video memory , That's for the hard disk controller , wait .
Say so , In fact, it does not conform to our pre knowledge , So there can be a less correct way of understanding , That is, this location in memory is video memory , That's the hard disk controller . We read or write in the corresponding position , It is equivalent to reading or writing in the corresponding position of peripheral devices such as video memory , It's like the storage area of these peripherals , By mapping It's the same as a certain area in memory . So we don't have to worry about the peripherals , The focus is still a simple memory . That's what's called Memory mapping .
Great , Now we can explain it with simple knowledge , We keep pushing down .
3、 ... and 、 Memory distribution in real mode
Just said that the memory is divided into a piece of area for various peripherals , Then the question comes naturally , Which area , Which peripherals were allocated to ？ If it's a rule , It should be better to have a watch . Well, that's right , There are , It's the memory distribution in real mode , The author drew a picture of it ：
Oh, I'm a little angel , It shows the proportions , If you can find something more intuitive on the Internet, please leave me a message . Explain after the real pattern , Now the simple understanding is that when the computer just turned on, only 1M Of memory available .
We see , Memory is partitioned by various peripherals , That is, it is mapped in memory .BIOS More malicious , Not only is its space mapped to memory 0xC0000 - 0xFFFFF Location , The program inside also takes up some of the initial areas , For example, write the interrupt vector table at the beginning of memory , What a first come, first served .
Four 、 How can we start from BIOS The program in is running
Okay , Now we know that BIOS The information in is mapped to memory 0xC0000 - 0xFFFFF Location , One of the most critical systems BIOS It's mapped to 0xF0000 - 0xFFFFF Location . If I say ,CPU Starting up is executing the code of this area , Then barrabarra started up as soon as he had a meal of operation , You're going to spray me , Why is this the execution , Why don't you start from scratch ？
This naturally leads to a conjecture , We need another pre knowledge , Namely CPU From which location in memory is taken out and executed ？ yes PC The address value in the register .BIOS The entry address of the program, which is the starting address, is 0xFFFF0（ That's what they wrote ）, That is to say, when the power button is pressed , There must be a magical power , take pc The value in the register becomes 0xFFFF0, then CPU He started to run nonstop . you 're right , The next sentence , Maybe it's the answer you've been looking for for a long time , Please be prepared ：
The moment you turn it on ,CPU Of PC Registers are forced to be initialized to 0xFFFF0. If you want to be more specific ,CPU The segment base register cs Initialize to 0xF000, Will offset address register IP Initialize to 0xFFF0, According to the final address calculation rule in real mode , Move segment base left 4 position , Add the offset address , Get the final physical address, which is abstract PC The register address is 0xFFFF0.
When I was learning this piece of knowledge , See this sentence to let my heart backlog for a long time to solve the doubts , What a simple and crude truth . I also had a long sigh of relief writing here , Because the rest of the process , It's almost just a daily account .
As for how to force initialization , I think we have crossed the boundary of pre knowledge , Moreover, the hardware implementation of each manufacturer is not necessarily the same , There are many ways , It's also very simple. . It doesn't make much sense to discuss .
5、 ... and 、BIOS What program is written in it
Okay , We now know BIOS Mapped to a location in memory , And turn it on for a second CPU Force your own pc Register is initialized to BIOS Program entry address , So let's start here CPU He ran forward without stopping . The next question seems to come out naturally , That's it BIOS What's in the program ？
hold BIOS It's not appropriate to post all the binary information in the program , Let's analyze some of the main . Let's guess first , Look at the entrance address 0xFFFF0, Explain that the program is executed from here . In real mode, the lower boundary of memory is 0xFFFFF, That is to say, only 16 Bytes of space to write code , What is this enough for ？ If you do, you should be able to guess , The entry address may be a jump command , Jump into a larger space to perform your own tasks . Yeah, that's it. ,0xFFFF0 Machine instructions stored at , Translation into assembly language is ：
jmp far f000:e05b
Jump to a physical address 0xfe05b Start execution （ Recall the address calculation in real mode ）.
Address 0xfe05b Start at , That is BIOS The code that really works , This code will detect some peripheral information , And initialize the hardware , Create an interrupt vector table and fill in the interrupt routine . Don't expand the part here , It's just a dead program , And it doesn't help to understand the startup process , Let's look at the highlights later , That is to say BIOS The last job of ： Load boot area .
6、 ... and 、0x7c00 What is it
The real thing is to be more serious , I will never let load This magic word appears here , We'll break it up now .
In fact, the word is not magical , Loading in the computer world means , Put on a device （ For example, hard disk ） The process of copying a program to memory . The process of loading the boot area , Which translates as ,BIOS The program copies the contents of the boot area to an area in memory . Okay , The problem comes out naturally again , Where is the boot zone ？ Where in the memory was copied ？ so what ？ Let's answer one by one .
What is the boot zone ？ Even if you don't know , You should also be able to guess , It must be an area of some kind , So people call it the startup area , So what are the characteristics ？ First, no hurry. , I don't know if you've set it up BIOS The experience of the startup sequence , Usually there are U Disk start 、 Hard disk boot 、 Floppy disk boot 、 CD start and so on ,BIOS In order , Read these startup disks in 0 disc 0 Avenue 1 The contents of the sector .
As for the division of disk formats , This article will not explain , In short, for memory , We can get the data of the address by giving a numerical address , And for disks , We need to give the head 、 cylinder 、 Only the three information of the sector can locate the data of a certain position , It's just a way to describe a location .
And then say , this 0 disc 0 Avenue 1 The contents of the sector are 512 Bytes , If the last two bytes are 0x55 and 0xaa, that BIOS You think it's a starter zone . If not , Then continue to search for the next device in sequence 0 disc 0 Avenue 1 The contents of the sector . If it turns out that none of them meet the requirements , That directly reported an error without starting area .
BIOS What after finding this startup area ？ Oh , As mentioned earlier, it's loading , That's what 512 Bytes of content , A bit is all copied to memory 0x7c00 This position . How to copy it ？ Orders, of course . What are the instructions ？ I can only simply say that there are in and out Of , Used to copy data from peripherals to memory , Or copy the data in memory to the peripheral , With these two commands , And the way that peripherals provide us with reading , You can do that .
The contents of the boot area have been BIOS The program is copied to memory 0x7c00 This position , so what ？ It's not hard to guess , The content of the boot area is our own code , After copying here , We're going to do it , Then our program takes over the next process ,BIOS That's the end of our mission . So after copying , And then it's a jump command ！ you 're right , It's just like this ,PC The value of the register changes to 0x7c00, The instruction starts from here .
Why ？ I don't know if you noticed , We seem to unconsciously translate the previous magic language into Chinese , At the beginning we said ：
BIOS Transfer control to the top storage device .
So what does this sentence mean ？ Namely BIOS Put the... In the boot zone 512 Bytes copied to memory 0x7c00 Location , And use a jump command to pc The value of the register points to 0x7c00. You see , It's not that there are not many words , Let's make it clear , A simple .
Oh , by the way , Now there seems to be only one problem left , Why is it necessary to 0x7c00 Well ？ Good question , Of course, the answer is simple , That's somebody else BIOS That's how the development team is set , It's not easy to change after , Otherwise, it is not compatible with . Why is it not easy to change ？ Let's look at a simple bootstrap 512 Byte code .（ Code excerpt from 《30 Self made operating system 》）
; hello-os ; TAB=4 ORG 0x7c00 ; Program loaded into memory 0x7c00 This position ; Procedural subject entry: MOV AX,0 ; Initialization register MOV SS,AX MOV SP,0x7c00 MOV DS,AX ; The segment register is initialized to 0 MOV ES,AX MOV SI,msg putloop: MOV AL,[SI] ADD SI,1 CMP AL,0 ; If you encounter 0 At the end of the , Just jump out of the loop and no longer print new characters JE fin MOV AH,0x0e ; Specify the text MOV BX,15 ; Assign colors INT 0x10 ; call BIOS Display character function JMP putloop fin: HLT JMP fin msg: DB 0x0a,0x0a ; Line break 、 Line break DB "hello-os" DB 0x0a ; Line break DB 0 ;0 ending RESB 0x7dfe-$ ; fill 0 To 512 byte DB 0x55, 0xaa ; Bootable device identification
Let's look at the first line ：
This number is the loading location of the boot area , This line of assembly code simply means to add all the following addresses 0x7c00. It is because BIOS Load the code for the boot area here , So there's an offset , So all the people who write the startup code need to write one such code at the beginning , Otherwise, it's all in series .
And because of all the things that write to the operating system , The first line of assembly code in the boot area has this number written dead , that BIOS It's hard to change the number originally set by developers , Otherwise, it has to contact the developers of each operating system one by one , Well, I'll change my address , You change with it . It takes a lot of effort to push another team to change the code in the company , Think about how much manpower it takes to push this forward . Besides, even if we change , The previous code is not compatible , This can't be scolded to death by people .
Look at the last line ：
DB 0x55, 0xaa
This also confirms what we said earlier 512 The last two bytes of a byte have to be 0x55 0xaa,BIOS It's a startup area , To load it , That's it .
Looking back 0x7c00 This value , It is actually a fixed value , But there are still people who ask , It must have its rationality . Actually , My explanation can only be said that it is stipulated by others , The later generations explain the rationality for them , It doesn't mean that people must have thought like this at the beginning , Just like we do Chinese reading comprehension questions .
first BIOS The development team is IBM PC 5150 BIOS, The first operating system that was thought to be at that time was DOS 1.0 operating system ,BIOS The team is supposed to serve it . But the operating system hasn't come out yet ,BIOS The team assumed that the minimum memory required for its operating system was 32 KB.BIOS I hope to load the boot code as far back as possible , Compare like this “ Security ”, Not too early to be covered by other programs . But if you just leave 512 The byte feels too hung again , There are also some stack space to reserve , That extends to 1 KB Well . such 32 KB At the end is 0x8000, subtract 1KB(0x400) , Exactly equal to 0x7c00. wow , It's so accurate , It can be a way of explaining .
7、 ... and 、 What does the code in the boot area write
Actually, it's written here , My article should be stopped at once , Because the original problem has been solved ,CPU We've started running from where we expected to be , Everything is difficult at the beginning , The rest , That is, the operating system can play as it wants .
But I don't think it's tasty enough , It seems that there are still some problems in your mind . Let's say this question ：
What does the code in the boot area write ？ Just 512 Bytes are all OS content ？
This is a good question ,512 A byte really doesn't do much , Today's operating systems have to press M Let's do it in units ,512 One byte is not enough , What 's going on ？
In fact, we can guess according to the previous thinking ,BIOS With very little code 512 Bytes of boot area content loaded into memory , And jump over and start executing . Then follow this routine , this 512 Bytes of boot code , Is it possible to store more operating system programs on disk , Load to a location in memory , And then jump to it ？
you 're right , This is the routine . therefore BIOS Responsible for loading the boot area , The boot area is responsible for loading the real operating system kernel , This is a tacit understanding ？
Because the disk used for the boot disk is made by the manufacturer who writes the operating system , Commonly known as making startup disk , So he must know which sector of the disk is the core code of the operating system , So the boot area takes this sector , And many sectors after that （ It depends on how big the operating system is ） All read into memory , Then jump to where the program starts . Where to jump to ？ This is not like 0x7c00 This number is so classic , Different operating systems are certainly different , There's no need to specify it , Anyway, the people who write the operating system will order one for themselves , Just don't cover the area used by other critical equipment .
8、 ... and 、 What does the operating system kernel write
OK, now after several rounds of jumping , Finally jump to the kernel code , Let's review ：
- Press the power button ,CPU take PC The value of the register is forced to be initialized to 0xffff0, This position is BIOS Program entry address （ a jump ）
- The entry address is a jump instruction , Jump to 0xfe05b Location , Start execution （ Two hop ）
- After doing some hardware testing , The last step is to load the contents of the boot area into memory 0x7c00, And jump here （ Three jumps ）
- Boot code is mainly to load the operating system kernel , And jump to the loading location （ Four jumps ）
After four consecutive jumps , Finally came to the world of operating systems , The rest , It can be said that the principle of the whole operating system course , piecewise 、 Pagination 、 Create interrupt 、 Device drivers 、 memory management 、 Process management 、 file system 、 User interface and so on .
You may have heard more or less of these rankings in operating system courses , If you study hard, you must know the general principle , But I've read it from beginning to end linux Kernel source code for the hard core dog , These concepts are not just boring concepts in books , It's about every line of code in the operating system , Some show the author's incomparable wisdom , Some let me see the author because of the hardware settings have to make the submission , I suggest you find time to read , Share your thoughts with me. Ha ha .
Nine 、 Reference material
Okay , This time I really want to end , Believe that if you really read the full text , The start-up process of the computer , It can be said that we have a more concrete understanding of . If you want to dig into the details , That is to know everything in the whole process , That's going to take a lot of work .
Beginners recommend two books , You can read in sequence , May you go into the pit ：
- 《30 Self made operating system 》
- 《 Operating system reality restore 》