2. Basic operation of the operating system

2.1 Starting the operating system

Some concepts:
DISK: store OS
BIOS: basic I/O processing system
Bootloader: load OS, load the code and data of the operating system from the hard disk into the memory
POST (power-on self-test): find the graphics card and execute the
specific memory address of the BIOS :Such as CS:IP = 0xf000:fff0
CS: segment register; IP: instruction register

2.1.1 Q

Why can't the application directly access peripherals (requires the operating system)?
When the computer is running, the kernel is a trusted third party (malicious application).
Only the kernel can execute privileged instructions.
In order to facilitate the application

2.2 Interrupts, exceptions, and system calls of the operating system

2.2.1 Definition

System calls (from applications)
applications actively send service requests to the operating system (such as switch/read and write files, send network packets)
abnormal (from bad applications)
illegal instructions or other bad processing states (such as: Memory errors, malicious programs, unmet resources required by applications)
Interrupts (from peripherals)
timers and network interrupts from different hardware devices

2.2.2 Comparison

2.2.2.1 Different sources

Interrupt: Peripheral
Abnormal: Unexpected behavior of the
application System call: The application requests operation to provide services

2.2.2.2 Different processing time

Interrupt: Asynchronous
Exception: Synchronous
System call: Asynchronous or synchronous

2.2.2.3 Different responses

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

2.2.3 Interrupt and exception handling

(1) Interrupt/exception handling mechanism
Interrupts are peripheral events, and exceptions are CPU events;
interrupts/exceptions force the CPU to access some functions accessed by interrupts and exception services.

(2) Interrupt handling mechanism

Hardware: Set interrupt flag (CPU initialization)

Set the internal/external event interrupt flag;
the ID of the interrupt event (interrupt vector address accessed by the program)

Software (operating system):
save the current processing state,
interrupt service program processing,
clear the interrupt flag,
restore the previously saved processing state

(3) Exception handling mechanism
Exception number
save the scene
Exception handling: kill the abnormal program; re-execute the exception instruction to
restore the scene

2.2.4 System call

A command will trigger a system call

Program access is mainly through high-level API interfaces rather than direct system calls.

Normally, there is a sequence number associated with each system call, and the system call interface maintains the index of the table according to these sequence numbers.
The system call interface calls the expected system call in the kernel mode and returns the status of the system call and any other return values.
The user does not need to know how the system call is implemented, but only needs to obtain the API and understand what the new system will operate as a return result. Most of the details of the operating system interface are hidden in the API and managed by the library supported by the running program.

User mode: The state of the privilege level executed by the CPU during the execution of the application program (very low, unable to access special machine instructions and IO).

Kernel mode: The state of the privilege level executed by the CPU during the execution of the application program (high, the operating system can execute any instruction of the CPU).

The system call involves the conversion of privilege level from user mode to kernel mode. The application and operating system have their own stacks. These two changes are more expensive than function calls, but are safer and more reliable. (The program call is to call and return parameters in a stack space).

2.2.5 Overhead across operating system boundaries

The execution time exceeds the program call

The overhead includes:
the initialization overhead of
establishing the mapping relationship between interrupt/exception/system call numbers and corresponding service routines; establishing the kernel stack (the operating system and the application stack are not the same);
verifying parameters (the operating system will check the data);
kernel state mapping To the user mode address space, update the page mapping permissions (memory copy overhead);
kernel mode independent address space TLB.

Guess you like

Origin blog.csdn.net/weixin_51864831/article/details/111823279