The process from powering on the computer to executing the main function of the Operating System

Source: Internet
Author: User
In general, the operating system has gone through the following three major steps before the main function is executed after the operating system is powered on and started. 1. Start BIOS. In real mode, load the interrupt vector and interrupt service program. 2. Load the operating system kernel and prepare for the protection mode.At this time, the operating system loads three pieces of code: boot program bootsect, kernel code setup, and kernel code system module. 3. switch from the real mode to the 32-bit protection mode.This process requires a lot of reconstruction work and continues to work on the execution of the main function of the operating system. In details, it mainly includes opening the 32-bit addressing space, enabling the protection mode, establishing the relevant mechanism for interrupting the protection mode and supporting the protection mode, and establishing the memory paging mechanism.
Glossary:Real-time mode: 20-bit memory address space, which allows direct software access to bios and peripheral hardware. There is no hardware-supported paging mechanism or Real-Time Multitasking concept. Starting from 80286, all 80 x CPUs are in real mode. Interrupt vector table: memory address used to record the interrupt service program corresponding to all interrupt numbers in real mode. Service interruption program: Used to indicate what to do after the interruption occurs. SS: Stack base register, SP: Stack top pointer, both constitute the position of the stack in the memory. Stack pushing is a high-Address to the ground. Root file system device: Linux 0.11 uses the minux OS file system management method. A root file must exist in the system and other file systems are mounted on it. Therefore, Linux 0.11 requires two data parts during startup, namely the system kernel image and the root file system. Gdt (Global Descriptor Table): A unique array of segment registers in the system. It works with programs to address segments in protection mode. It can be understood as the total directory table of all processes, which stores the LDT address and TSS address of each task to complete the addressing of each segment in the process, on-site protection and recovery. IDT (middle-stage Descriptor Table): stores the endpoint addresses of all interrupted services in protected mode, similar to the interrupt vector table in real mode. GDTR and idtr: these are the base address registers Cr0 registers of the descriptor table above respectively. Port 32 is the control register, which stores the system control mark. 0th bits indicates PE. When set to 1, the CPU works in protection mode, and when set to 0, it is in real mode. Set the segment register command: this group of commands transfers a "low character" of the memory unit to the specified 16 registers of the command, and then transmits the "high character" to the corresponding segment register. Command Format: lDs/LES/LFS/LGS/lss mem, Reg
Note: I learned the Linux 0.11 kernel. Currently, some kernel versions are 3.6. Although they are much worse, they are suitable for learning. Note: The BIOS at the beginning is irrelevant to the operating system.

1. Start bios and prepare the interrupt vector table and interrupt service program in real mode.After the computer is started, the CPU logic circuit is designed to run only programs in the memory, and cannot directly run the operating system in the floppy disk or hard disk, it must be loaded into the memory (RAM. How the BIOS is started:The hardware logic of the CPU is designed to forcibly set the Cs value to 0xf000 and the IP address to 0xfff0 during power-on. In this way, CS: IP points to 0xffff0, which is the entry address of the BIOS program. The BIOS loads the interrupt vector table and interrupt service programs in the memory.The BIOS program is solidified into a very small ROM chip on the computer motherboard. Now Cs: the IP address has been directed to the 0xffff0 position, which means that the BIOS starts to start. With the execution of the BIOS program, the screen will display the video card information, memory information ...... Indicates that the BIOS program is detecting the video card, memory ...... During this period, there is a task that is vital to the startup of the operating system, that is, the BIOS establishes an in-memory disconnection meter and interrupt the service program. The BIOS program uses 1 kb of memory space (0x00000 ~ 0x003ff) Build the interrupt vector table, and build the BIOS data zone (0x00400 ~ 0x004ff), and some interrupt service programs corresponding to the interrupt vector table about 8 KB are loaded after about 57kb (0x0e05b. The interrupt vector table has 256 interrupt vectors, each of which occupies 4 bytes, two of which are Cs values and two of which are IP values. Each interrupt vector points to a specific interrupt service program.
2. Load the operating system kernel and prepare for the protection modeNow, the computer needs to load the OS kernel code in three batches. Boot Program bootsect, kernel code setup, kernel code system module Load bootsectFirst, an int 0x19 interrupt is sent to the CPU so that the CPU runs the int 0x19 interrupt service program, this service interruption program is used to load the program in the first sector of the floppy disk to the specified memory location. To sum up, find the floppy disk and load the first sector. The content of the first sector is bootsect, and the first sector is called the start sector. So far, the first batch of code bootsect has been loaded into the memory. The following task is to execute bootsect to load the second and third batches of code to the memory. Load Part 2 Program SetupBootsect first plans the memory, including the number of sections of the setup program to be loaded, the locations to which the Boot Sector is loaded by BIOS, and so on. In short, the memory distribution after planning is convenient for later use. Bootsect then copies its own code. Copy From memory 0x07c00 to memory 0x90000. At this time, the OS began to arrange memory according to its own needs. Finally, bootsect loads setup to the memory. The loading process must be completed by using the interrupt service program pointed to by the int 0x13 interrupt vector provided by the BIOS. This program loads the Program Setup. s to the setupseg (0x90200) of the memory for the four sectors starting from the second sector of the floppy disk. Load the third kernel code-system moduleThe third batch of code is still loaded using the int 0x13 interrupt vector. The number of slice loaded this time is 240. To make the user feel that it is not a machine fault, Linus designs a line of string on the screen: "loading system... "confirm the root device number after loading.
3. Switch from real-time mode to 32-bit protection modeSo far, the three parts of the Code have been loaded, and the change has started into the protection mode. Disconnect and move the system to the memory address starting position 0x00000The disconnection is to avoid responding to external interruptions. the interruption will not be opened until the interrupted service system in the protection mode is constructed. Review: 0x00000 originally stores the BIOS-level BIOS data zone of the interrupt vector table established by the BIOS. This copy will overwrite the original region. The advantage is: a) Abolish the BIOS interrupt vector table. B) memory space recycling c) Let the kernel code occupy the initial advantage of the memory physical address Set the interrupt description table and Global Descriptor TableSet and initialize the two tables Enable A20 for 32-bit addressingA20 is 21st IP address lines, and the first 20 (A0 ~ A19), the protection mode will enable A20 ~ A31, marking 32-bit addressing. Prepare to execute head. s in Protected Modeto establish the interrupt mechanism in protection mode, the setup program re-program the Programmable Interrupt Controller 8259a and remap the interrupt number. Then run the code:... mov ax, #0x0001! Protected Mode bit lmsw ax jmpi 0, 8! JMP offset 0 of Segment 8 (CS )... 8 indicates 1000, the last two digits indicate the kernel privilege level, and 11 indicates the user privilege level. The third digit 0 indicates gdt, and 1 indicates LDT; 1 indicates selecting 1st items in the table (starting from 0), which is used to determine the segment base address and segment limit length of the code segment (CS.

Head. s start executionHead. the loading method of S is different from the previous bootsect and setup. The general process is: first set head. S is compiled into the target code, and the kernel program compiled in C language is compiled into the target code, and then linked to the system module. That is, there are both kernel programs and head programs in the memory. The two are close to each other. In addition to calling main, the head program mainly uses the program's own code to create a memory paging mechanism in the memory space of the program. The head program presses the l6 label and the main function entry address on the stack, and the top of the stack is the main function address, so that the head program can directly execute the main function through the RET command after execution. Normally, the main function should not exit, but if the main function exits abnormally, it will return the l6 mark for continued execution to prevent system crash. After these stack operations are completed, the head executes setup_paging: to create a paging mechanism. You must first place the page Directory table and four page tables at the starting position of the physical memory. The four page tables map all pages from 0x0000 to 0xfff000 respectively. The size of one page is the memory distribution configured by the 4 kb head program.

So far, the process ends before the main function is executed.


Reference: Linux kernel design art

The process from powering on the computer to executing the main function of the Operating System

Related Article

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.