Operating system-start, interrupt, exception, system call [1]

Overview of computer architecture

The structure of the computer can be simplified as shown below. The memory in the figure is divided into ROM (read only memory) and RAM (random access memory). The system initialization code is read from the ROM and executed.

Insert picture description here

BIOS (English: Basic Input/Output System), also known as ROM BIOS, System BIOS, and PC BIOS, is the firmware that runs hardware initialization during the power-on boot phase and provides runtime services for the operating system. BIOS first appeared in 1975 with the introduction of the CP/M operating system. The BIOS is pre-installed on the motherboard of the personal computer and is the first software loaded when the personal computer starts.

Now, the role of BIOS is to initialize and test hardware components, and load boot programs from mass storage devices (such as hard disks), and the boot program loads the operating system; when the operating system is loaded, the BIOS provides hardware for the operating system through the system management mode abstract. In the DOS era, BIOS provides a hardware abstraction layer for keyboards, displays, and other I/O devices for the DOS operating system.

Many BIOS programs can only run on specific computer models or specific motherboard models. In the early years, the BIOS was stored on the ROM chip; now the BIOS is mostly stored on the flash memory chip, which facilitates the update of the BIOS.

BIOS -Wikipedia

When the computer is powered on, it will execute the BIOS part.
Insert picture description here

Insert picture description here
Taking X86 as an example, the first step after BIOS startup is to cs:ipstart execution from a specific address (fixed memory address ), and then complete a series of tasks.

It is agreed that when the power is turned on, the CPU executes the code from the address in the above figure after the initialization is completed. Note that the size of this part of the BIOS is about 1MB, because the address space is only 20 bits at this time, so 220bit or 1MB can be used.

Insert picture description here

The functions that the BIOS needs to provide are as shown in the figure above. Basic input and output are used to read/write data from the disk, read input from the keyboard, and display output on the monitor. Examples of system settings include which disk to boot from, or whether to boot from the network.

Finally, load the program and operating system content according to the configuration. The specific process is:

Insert picture description here

The reason why the kernel image of the system cannot be read directly from the BIOS is that the file system on the disk must be determined (there are many file systems on the market) before it can be read. We directly pre-arranged that without knowing the type of file system, we can directly read the first piece of data, and then identify the file system on the disk based on these data, and finally read the kernel image of the operating system on the disk and load it into the memory .

Finally, mention the functions and limitations provided by the BIOS:

Insert picture description here

System startup process

Insert picture description here

Look for the master boot record to determine which file system to read the loader from, because there may be more than one partition, and different partitions may not use the same file system. After determining the master boot sector, you can determine which active partition to read the program.

Insert picture description here
Insert picture description here

The self-check is to confirm that several key hardware are working properly. System detection is mainly to determine whether there is a system, for example, before starting the system (WinToGo) from the U disk, it will first check whether there is a system in your U disk. Finally, the first sector will be read from the designated floppy disk, hard disk or optical drive.

After reading in, it is necessary to read the master boot record:

Insert picture description here

After reading, it will jump to the boot sector of the active partition:

Insert picture description here

The JMP part is platform-related, and different platforms are different. The startup code determines where the program is stored, it can be changed, and the location of the program can also be changed.

Insert picture description here

The format of the startup configuration file is determined by the system.

Insert picture description here

The BIOS already has a corresponding standard. When writing code, you need to write it according to the standard (so that you don't need to implement a different BIOS on each platform). MBR is the earliest. The master boot record can only describe up to 4 partitions, each occupying 16 bytes, and the total length is 512 bytes. However, computers often use more than 4 partitions, so GPT (Globally Unique Identifier) Partition table), so that you can not only describe the limit of 4 partitions. PXE is the standard for network startup. UEFI also provides authentication of the disk signature. If the signature is incorrect, it will refuse to continue reading the contents of the disk.

Insert picture description here

Comparison of interrupts, exceptions and system calls

In order to provide services to the program, while not allowing the program to perform specific operations (security issues). Regarding the problem in the figure: When the peripheral is connected to the computer, in order for the system to respond appropriately to the input of the peripheral, an interrupt is needed (polling is too resource-intensive). Corresponding measures are also needed to deal with this unexpected situation when the program fails. The interface is provided through system calls, so that no security problems can arise on the premise of providing services. The difference is that system calls will have portability issues, because different systems have different calling functions, and the speeds are also different. Usually system calls are faster than function calls, and there are other things, you can see here.

The difference between library function (procedure) call and system call

Insert picture description here

It can be seen that the communication between the program and the kernel basically revolves around interrupts, exceptions, and system calls.

System call (system call)
A service request issued by an application to the operating system

Exception (exception)
illegal instruction or other reasons cause the current instruction execution failure (such as: memory error) processing request


Hardware interrupt processing requests from hardware devices

The difference between interrupt, exception, and system call:

1. Source

  • Interrupt: Peripheral
  • Exception: unexpected behavior of the application
  • System call: application request operation to provide service

2. Response method

  • Interrupt: Asynchronous
  • Exception: sync
  • System call: asynchronous or synchronous

3. Processing mechanism

  • Interruption: continuous, transparent to the user application
  • Exception: Kill or re-execute unexpected application instructions
  • System call: wait and continue

1. Interrupt handling mechanism:

Insert picture description here

It must be enabled, otherwise the interrupt cannot be used (the reason why the interrupt function is set is because sometimes the system has to perform some operations that must be completed at one time, and it cannot respond to the interrupt at this time, so the interrupt function needs to be temporarily turned off) .

Insert picture description here

Insert picture description here

2. System call

The system call is the interface for the operating system to provide services.

Insert picture description here

The picture above is an example of a C program.

Insert picture description here
Insert picture description here
Implementation of system calls

Insert picture description here
When a program calls a system call, it first enters the system kernel through an interrupt, and then goes to the system call table. At this time, the number of the system call coming in through the interrupt will be used to check the corresponding system call implementation in the system call table. After the result is obtained Go back to the program.

Insert picture description here

The stack used by the two calls is different.

Insert picture description here

The overhead of the system call is shown in the figure above.

System call example

Insert picture description here
The left side of the above figure is the program to be implemented (example), and the red one on the right is the system call used.

Guess you like

Origin blog.csdn.net/qq_44721831/article/details/108700340