Summary: understanding computer systems in depth: Abnormal Control Flow
1. Exception Handling Mechanism in computers: processor designers (such as Division by zero, missing pages, and memory access violations) and operating system developers (such as system calls and external IO Device signals) assign a unique non-negative integer exception number for each type of exception. When the system starts, the operating system allocates and initializes a jump table that becomes an exception table, which uses the exception number K
Index, with the address of each type of exception handler as the value. When the processor detects an event (for example, when the CPU determines the event type based on the pin status), it determines the exception number K and forwards it to the corresponding processing program through the exception Table K. The abnormal table address is usually stored in the "abnormal Table Base Address Register.
2. The exception handling process is similar to a process call: the current Register status and return address are saved.
However, (1) the return address of exception handling is either the current execution instruction or the next instruction of the current execution instruction.
(2) When an exception occurs, if the kernel represents a user program for execution (for example, a system call) at this time, these statuses will be pushed to the kernel stack, instead of pushing to the user stack ).
(3) In addition, exception handlers usually run in kernel mode and have access to all system resources.
3. Exception category
(1) interrupt: the interrupt occurs asynchronously and comes from the signal of the external IO Device of the processor (different from the synchronous exception: the result of executing a command ), it is not caused by any special command. For example, the network adapter and disk Controller send a signal to a pin on the processor chip and place the exception number on the system bus to trigger the interruption. This exception number identifies the device that causes the interruption. The interrupt handler always returns the next instruction of the current instruction.
(2) trap: A synchronization exception is the result of executing a command. The most important purpose of a trap is to provide a system call interface between the user program and the kernel. The total number of traps returned to the current command
Next command.
(3) fault: the fault is caused by an error. The fault may be corrected by a common processing program. If the fault is corrected successfully, it will return to the currently executed command and re-execute it. Otherwise, the process returns the abort history to the kernel, and the faulty program is terminated. A typical fault is a page missing exception.
(4) Termination: if the result is caused by an unrecoverable well-known error, the handler will return to the abort routine in the kernel and terminate the application.
4. context switch)
The kernel maintains a context for each process. The context is the state set required by the kernel to restart a preemptible process: General registers, floating-point registers, program counters, user stacks, status registers, the kernel stack and various kernel data structures (such as tables, process tables, and file tables ). The kernel uses the scheduler to seize an execution process:
(1) Protect the context of the current process
(2) restore the context stored by a previously preemptible Process
(3) Pass the control to the newly replied process.
Context switching may occur in the following situations:
(1) When the kernel represents the user's execution of the system call. For example, if a system call is blocked by waiting for an event, the kernel can sleep the current process and switch to another process. For example, read requests for disk access.
(2) The user program calls the sleep system call to explicitly sleep the calling process.
(3) interruption may cause context switching. For example, all systems have a mechanism to generate periodic timer interruptions. A typical bit is 1 ms or 10 ms. Each time a timer is interrupted, the kernel checks whether the current process has been running for a long enough time and switches to another process.
5. UNIX Signal
A signal is a message that notifies a process that a certain type of time has occurred in the system. Each signal corresponds to a certain type of system time.
(1) underlying hardware exceptions are handled by kernel exception handlers, which are usually invisible to user processes. Signals provide a mechanism to notify user processes of these exceptions. For example, if a process tries to divide it by 0, the kernel will send a sigfpe signal.
(2) other signals correspond to high-level software events in the kernel or other users. Such as SIGINT and sigkill.