The role of interrupts

Publisher:丹青妙手Latest update time:2018-11-27 Source: eefocusKeywords:Interrupt Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

All things rely on interrupts to solve problems. This is especially true for computers, which rely on clock generators and artificially set a task every few clock cycles. Regardless of whether it is completed or not, once the specified time is up, it will be forced to "interrupt" to do the next task... In this way, until all tasks have been done (but not necessarily completed), then the first unfinished task will be done, and the cycle will repeat over and over again. This is the so-called "multitasking".
Otherwise, what if a CPU can type and see the screen at the same time? This is a CPU core, constantly detecting the keyboard and the screen...

There are also soft interrupts. For specific details, please refer to the principles of microcomputers.


Interrupt system


Interrupt devices and interrupt handlers are collectively referred to as interrupt systems.

Interrupt systems are an important part of computers. Real-time control, automatic fault handling, and data transmission between computers and peripheral devices often use interrupt systems. The application of interrupt systems has greatly improved computer efficiency.

Different computers have different hardware structures and software instructions, so the interrupt systems are also different. The computer's interrupt system can enhance the CPU's ability to handle multi-task events. The interrupt mechanism is one of the infrastructures in modern computer systems. It plays the role of a communication network in the system to coordinate the system's response and processing to various external events. Interrupts are a necessary condition for implementing multi-programming. Interrupts are a reaction of the CPU to an event that occurs in the system. The event that causes an interrupt is called an interrupt source. The request made by the interrupt source to the CPU for processing is called an interrupt request. The pause point of the interrupted program when an interrupt occurs is called a breakpoint. The process by which the CPU pauses the current program and responds to the interrupt request is called an interrupt response. The program that handles the interrupt source is called an interrupt handler. The CPU executing the relevant interrupt handler is called interrupt processing. The process of returning to the breakpoint is called an interrupt return. The implementation of the interrupt is completed by software and hardware. The hardware part is called a hardware device, and the software part is called a software handler.

Functions of the interrupt system

1) Realize interrupt response and interrupt return
When the CPU receives an interrupt request, it can decide whether to respond to the interrupt according to the specific situation. If the CPU has no more urgent or important work, it will respond to the interrupt request after executing the current instruction. The CPU interrupt response process is as follows: First, the PC value at the breakpoint (that is, the address of the next instruction to be executed) is pushed into the stack for preservation. This is called protecting the breakpoint and is automatically executed by the hardware. Then, the relevant register content and flag bit status are pushed into the stack for preservation. This is called protecting the scene and is programmed by the user. After protecting the breakpoint and the scene, the interrupt service program can be executed. After execution, the CPU returns to the main program from the interrupt service program. The interrupt return process is as follows: First, restore the original reserved register content and flag bit status. This is called restoring the scene and is programmed by the user. Then, add the return instruction RETI. The function of the RETI instruction is to restore the PC value and make the CPU return to the breakpoint. This is called restoring the breakpoint. After restoring the scene and breakpoint, the CPU will continue to execute the original main program, and the interrupt response process ends here.

2) Implement priority queuing
Usually, there are multiple interrupt sources in the system. When multiple interrupt sources send interrupt requests at the same time, the computer is required to determine which interrupt is more urgent so that it can respond first. To this end, the computer specifies a priority level for each interrupt source, which is called priority. In this way, when multiple interrupt sources send interrupt requests at the same time, the interrupt with a higher priority can be responded to first, and only after the interrupt with a higher priority is processed can the interrupt with a lower priority be responded to. The process in which the computer responds to interrupt sources one by one according to their priority is called priority queuing. This process can be implemented through hardware circuits or through software queries.

3) Implement interrupt nesting
When the CPU responds to an interrupt, if an interrupt source with a higher priority sends an interrupt request, the CPU can interrupt the ongoing interrupt service program and retain the breakpoint of this program (similar to subroutine nesting), respond to high-level interrupts, and continue the interrupt service program after the high-level interrupt processing is completed. This process is called interrupt nesting. If the priority level of the interrupt source that sends a new interrupt request is the same as or lower than that of the interrupt source being processed, the CPU will not respond to this interrupt request until the interrupt service program being processed is executed.

 

Interrupts, exceptions, and system calls

An interrupt is a reaction of the CPU to an event occurring in the system. The CPU suspends the executing program, preserves the scene, and automatically switches to execute the corresponding processing program. After processing the event, it returns to the breakpoint to continue executing the "interrupted" program.

Interrupts can be divided into three categories. The first category is caused by the outside of the CPU, called interrupts, such as I/O interrupts, clock interrupts, console interrupts, etc. The second category is the process caused by internal events from the CPU or events in program execution, called exceptions, such as processes caused by CPU failures (power supply voltage lower than 105V or frequency outside 47-63Hz), program failures (illegal opcodes, address out of bounds, floating point overflow, etc.).

The third type of process is caused by the use of system calls in the program to request system services, which is called a "trap". The first two types are usually called interrupts, which are often unintentional and passive, while traps are intentional and active.

       1. Interrupt processing

        Interrupt processing is generally divided into two steps: interrupt response and interrupt processing. Interrupt response is implemented by hardware, and interrupt processing is mainly implemented by software.

        (1) Interrupt response

        The entire processing process of the interrupt request is implemented by a set of interrupt mechanisms formed by combining hardware and software. When an interrupt occurs, the CPU pauses the execution of the current program and turns to processing the interrupt. This process of hardware responding to the interrupt request is called interrupt response. Generally speaking, the interrupt response sequence performs the following three steps:

◆Terminate the execution of the current program;

◆Save the breakpoint information of the original program (mainly the contents of the program counter PC and the program status register PS);

◆Take out the interrupt vector from the interrupt controller and transfer it to the corresponding handler.

Usually, after executing an instruction, the CPU immediately checks whether there is an interrupt request, and if so, it responds immediately.

The system responds when interrupts occur, whether they come from hardware (such as from a clock or external device), programmatic interrupts (the execution of instructions causes "software interrupts"), or from unexpected events (such as accessing a page that is not in memory).

If the current CPU execution priority is lower than the interrupt priority, then it will terminate the execution of the next instruction of the current program, accept the interrupt, and increase the execution level of the processor (generally the same as the interrupt priority) so that when the CPU handles the current interrupt, it can shield other interrupts of the same level or lower level, and then save the breakpoint scene information and jump to the entry of the corresponding interrupt handler through the obtained interrupt vector.

(2) Interrupt processing

The CPU obtains the interrupt vector from the interrupt controller, and then finds the corresponding table entry from the interrupt vector table IDT according to the specific interrupt vector, which should be an interrupt gate. Therefore, the CPU reaches the entrance of the general service program of the channel according to the setting of the interrupt gate.

The order in which the core handles interrupts is mainly completed by the following actions:

◆Save the contents of the registers of the running process and put them into the new frame of the core stack.

◆Determine the "source of the interrupt" or check the occurrence of the interrupt, identify the type of interrupt (such as clock interrupt or disk interrupt) and the device number of the interrupt (such as which disk caused the interrupt). When the system receives an interrupt, it gets an interrupt number from the machine, which is the displacement to retrieve the interrupt vector table. The interrupt vector varies from machine to machine, but usually includes the entry address of the corresponding interrupt handler and the status word of the processor when the interrupt is processed.

◆The core calls the interrupt handler to process the interrupt.

◆Interrupt processing is completed and returns. After the interrupt handler is executed, the core executes a specific instruction sequence related to the machine, restores the register content at the time of the interrupt and pops the execution core stack, and the process returns to the user state. If the rescheduling flag is set, the process is scheduled when the process returns to the user state.

2. System calls

In Unix/Linux systems, system calls appear in C programs like ordinary C function calls. However, ordinary function call sequences cannot change the state of a process from user mode to kernel mode, while system calls can do so.

The C language compiler uses a predetermined function library (generally called the C library), which contains the names of various system calls. The functions in the C library use a dedicated instruction to change the running state of the process to the kernel state. The Linux system call is implemented through the interrupt instruction "INT0x80".

Each system call has a unique number, called the system call number. All system calls are centrally managed in the system call entry table.

The system call entry table is an array of function pointers. By finding the corresponding function pointer in the array with the system call number as the subscript, we can determine which system call the user is using. The number of system calls in different systems is different. Currently, there are 221 system calls defined in the Linux system.

In addition, there are some remaining items in the system call table for users to add.

When the CPU executes the interrupt instruction "INT0x80", the hardware responds in a series of ways, the actions are the same as the above interrupt response. The CPU passes through the trap door and enters the system space from the user space. Accordingly, the context of the process is switched from the user stack to the system stack.

Then run the kernel function system_call(). First, further save the contents of each register; then call syscall_trace(), use the system call number as the index to search the system call entry table sys_call_table, find the corresponding function; then turn to execute the function to complete the specific service.

After executing the service program, the core checks whether an error occurs and handles it accordingly. If the process receives a signal, it handles the signal accordingly. Finally, the process returns from the system space to the user space.

The above two lectures briefly introduced the main data structures and corresponding algorithms of the Linux kernel. The Linux kernel contains a lot of content, and here is just a little bit of it, in order to play a role of "starting a discussion".


Signal interruption and system call restart


#include


Keywords:Interrupt Reference address:The role of interrupts

Previous article:Principle of MCU key debounce
Next article:Notes on interrupt handling functions

Recommended ReadingLatest update time:2024-11-16 12:01

PIC32MZ tutorial -- Timer Interrupt
  An interrupt is an internal or external event that requires quick attention from the controller. The PIC32MZ architecture provides a rich interrupt system that can manage up to 190 sources of interrupts. Each interrupt source can have a unique piece of code, called the Interrupt Service Routine (ISR) directly associ
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号