- Kernel state(Kernel Mode): Run operating system programs.
- User State(User Mode): Run the user program.
- User state Kernel state: interrupt / exception / trap mechanism.
- Kernel State User State: Set the program status word PSW.
- Privilege directivesInstructions that can only be used by the operating system and not by user programs.
- Non-privileged directivesUser programs can use instructions.
A central processor
- The processor is composed ofArithmetic unit、ControllerA series ofregisteras well asCachingComposition.
Two types of registers
- User Visible RegisterHigh-level language compiler allocates and uses it through optimization algorithm to reduce the number of memory accesses.
- Control and Status RegisterUsed to control the operation of the processor, usually by operating system code.
Control and Status Register
- Used to control the operation of the processor.
- It can be accessed and modified at a certain level of privilege.
Common Control and Status Registers
- Program counter(PC: Program Counter): Record the address of the instruction to be taken out.
- Instruction register(IR: Instruction Register): Record the instructions recently taken out.
- Program status word(PSW: Program Status Word): Record the running state of the processor, such as conditional code, mode, control bit. Set up a special bit in PSW, according to the running program.Permission to use resources and instructionsSetting different CPU states.
Interruption and exception mechanism
It can be said that the operating system is made up of“Interrupt drive” Or“event driven” Yes.
- Handle interruption requests from equipment in time.
- It enables the operating system to capture service requests from user programs.
- Prevent destructive activities during the execution of user programs.
- CPU happens to the systemAn eventA response.
- The CPU suspends executing programs.Preserve the sceneAutomatically switch back to executing the corresponding eventprocessing programWhen the processing is completed, the breakpoint is returned.Continue implementationInterrupted procedures.
- interruptExternal events, which are not expected by the running program.
- abnormalTriggered by an executing instruction.
- Hardware work（Interrupt/exception response）：captureInterrupt/exception requests from interrupt sources in a certain wayresponseTo control the processorHandSpecific handlers.
- Work of software（Interrupt/exception handler）：DistinguishInterrupt/exception type and complete the correspondingProcessing operations。
Interrupt vectorA memory unit for storageInterrupt Handler Entry AddressAnd what the program needs to runProcessor status word。 The execution process transfers control to the corresponding interrupt handler through the interrupt vector table according to the interrupt number/exception type.
Interrupt response diagram
Interrupt Processing Procedure
- When designing the operating system, the corresponding processing program is programmed for each kind of interrupt/exception event, and the interrupt vector table is set up.
If the system responds to interruption during operation, the interruption of hardware components will result in CPUcontrol powerTransfer to interrupt handler:
- PreservationRelevant register information.
- AnalysisSpecific reasons for interruption/anomaly.
- implementThe corresponding processing function.
- recoveryOn the spot, return to the procedure interrupted by the incident.
- Users can call when programmingOperating System Functions。
- The operating system isProcess running in user modeandHardware equipmentThe set of interfaces provided for interaction is set upBetween applications and hardware devicesAn interface layer. It can be said to be an interface left by the operating system to the user program.
- Each operating system provides hundreds of system calls (process control, process communication, file usage, directory operation, device management, information maintenance, etc.).
The Relation and Difference between System Call and Library Function
- What the system call provides to the user isDirect and pure high-level servicesIf we want to be more humane and have functions that are more suitable for specific situations, then we need to define them by our users themselves, so we derive library functions, which call part of the system.PackingGet up. For example, when we want to print a sentence in C language, if we don’t use the library function printf, then we need to call putc () and write () and other system functions.
- System call is for the convenience of using the interface of operating system, while library function is for the convenience of programming.
- In terms of portability, different operating systemssystem callGenerally different,Poor transplantabilityIn all ANSI C compiler versions, C library functions are the same.
When the CPU executes a special trapped instruction:
- Hardware protection site
- Transfer control to the user by looking up the interrupt vector tableSystem Call General Entry Program
System Call General Entry Program
- Preservation site
- Save parameters in the kernel stack
- Transfer control to the corresponding system call processing routine or kernel function by looking up the system call table
- Execute system call routines
Restore the scene and return to the user program