Today let’s talk about how the operating system starts up.
Like the helloworld program we wrote, the operating system itself is also a program that is compiled and saved as a file on the disk.
Since ordinary programs are loaded and run by the operating system, who loads the operating system into the memory and runs it?
To know the answer to the question, we have to start with memory.
The memory can only save information if data is written after power is applied:
This information is lost when the memory is powered off:
When the computer starts up, the memory is empty and the CPU obviously cannot execute instructions from memory:
So where does the CPU execute instructions when you press the power button? The answer is from ROM or other non-volatile memory:
These storage devices store a small set of instructions that the CPU executes when the computer starts.
How does the CPU know where this program is stored? The answer is that the CPU will start executing instructions from a pre-set location. In the IA-32 architecture, the CPU will start executing this program from 0xffff0.
This small program is the familiar BIOS (Basic Input/Output System), and the more modern one is UEFI (Unified Extensible Firmware Interface).
What is the function of the BIOS program? This program is used to check the hardware to see if each piece of hardware is working properly, such as the CPU, memory, hard disk, etc. This is the so-called self-test. If you happen to have disassembled the machine and forgot to put the memory back, this small program will detect that there is no memory installed in the system and give a warning prompt.
In addition to checking the hardware, the BIOS program has an important task in the last step, which is to identify where the operating system is stored, that is, to find the so-called boot device, which is usually a disk.
After finding the boot device, it starts copying the first 512 bytes of data from the disk to the memory. These 512 bytes are the so-called MBR:
It includes:
-
A program whose purpose is to load other programs, so it is also called a boot loader.
-
Disk partition table, identifying the various partitions of the disk
At this point, the BIOS is finished executing, and the CPU starts executing the boot loader program in the MBR:
The purpose of this program is to load a larger boot loader based on the disk partition table.
This larger boot loader is more powerful and complex. If you have installed multiple operating systems, then when this larger boot loader starts executing, it will give you an option where the user can choose which operating system to start.
Here you can see that the boot loader program in the MBR loads a larger boot loader program, and therefore these two boot loaders are called the First stage boot loader and the second stage boot loader, that is, the first stage boot program and the second stage boot program respectively.
You may wonder, why do we need a multi-stage boot program? Why can't we just use a single-stage boot program to load the operating system directly?
The reason is that the 512-byte MBR boot program is too simple and can only copy a continuous section of data from the disk to the memory. Loading a large program composed of multiple files such as the operating system requires parsing the file structure, so another more powerful program, the second-stage boot program, is needed.
The second-stage boot finally identifies the location of the operating system and loads the operating system into memory.
At this point, the CPU begins to actually execute the operating system. The operating system, like the program we write, also has an initialization process. At the end of this process, a series of user-mode processes will be created. At this point, the operating system is started and can accept user input.
When you double-click the icon or enter a command in the command line, the operating system starts loading the program we wrote into the memory and running it. This process is described in the previous article "
How the program runs step by step
"
Of course, in modern systems that use UEFI, there is no need for the first-level boot loader in the MBR.
The above is the basic process of operating system startup.
Follow and reply【
1024
】 to get a large amount of Linux information
Collection of wonderful articles
Recommended articles
☞
【
Album
】
Getting started with
Linux