Home  >  Article  >  Operation and Maintenance  >  what is interrupt in linux

what is interrupt in linux

青灯夜游
青灯夜游Original
2023-04-20 16:34:492481browse

In Linux, an interrupt is a mechanism by which the hardware sends a signal to the CPU when needed, and the CPU temporarily stops its ongoing work to handle the hardware request. When the hardware is busy, the CPU is likely to do a lot of useless work (each poll is skipped and not processed); therefore, in order to improve the performance of the collaborative work between the CPU and peripheral hardware (hard disk, keyboard, mouse, etc.), introduce The interrupt mechanism.

what is interrupt in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

1. Interrupt definition

Interrupt is when the hardware sends a signal to the CPU when needed, and the CPU temporarily stops the ongoing work to process it. A mechanism for hardware requests.

Specifically:
Interrupt refers to the CPU temporarily stopping the running program due to internal or external events or events pre-arranged by the program during normal operation of the CPU, and switches to the internal or external event. Or go to the program of the pre-arranged event service, and then return to continue running the temporarily interrupted program after the service is completed.

1.1 Background (why interrupts are needed)

1. Without interrupts, collaboration between the CPU and peripheral devices The only way to work/communicate is by polling: the CPU regularly checks the hardware status and processes it when it needs to be processed, otherwise it skips it.
2. The speed of the processor is not in the same order of magnitude as the peripheral hardware devices, so a mechanism is provided for the hardware to send signals to the kernel when needed.

Disadvantages of polling/Introduction of interrupt mechanism:
When the hardware is busy, the CPU is likely to do a lot of useless work (each polling is skipped and not processed).
Therefore, in order to improve the performance of collaborative work between the CPU and peripheral hardware (hard disk, keyboard, mouse, etc.), an interrupt mechanism is introduced.

1.2 Interrupts and signals

中断: 硬件/进程发,内核收
信号:内核发,进程收,或者进程发进程收

##1.3 Interrupt handling Process (refer to CSAPP books)

中断是异步发生的,是来自处理器外部的I/O设备的信号的结果

1. 硬件中断不是由任何一条专门的指令造成的,从这个意义上来说它是异步的
2. 硬件中断的异常处理程序常常被称为中断处理程序(interrupt handler)
The following figure outlines the processing of an interrupt.

Figure 8.5

what is interrupt in linux

I/O devices, such as network adapters, disk controllers, and timer chips, signal an exception to a pin on the processor chip. The exception number is placed on the system bus to trigger an interrupt. This exception number identifies the device that caused the interrupt.

Before the current instruction completes execution, the processor notices that the voltage on the interrupt pin has become high, reads the exception number from the system bus, and then calls the appropriate interrupt handler. When the handler returns, it returns control to the next instruction (that is, the instruction that would be after the current instruction in the control flow if no interrupt had occurred). The result is that program execution continues as if the interruption had not occurred.

剩下的异常类型(陷阱、故障和终止)是同步发生的,是执行当前指令的结果
我们把这类指令叫做故障指令(faulting instruction)

1.4 The essence of interrupt and processing mechanism/process

The essence of interrupt is a special electrical signal

what is interrupt in linux

Processing process:

Interrupts are generated by hardware devices and sent directly to the input pins of the interrupt controller (simple electronic chip). The interrupt controller uses multiplexing The technology uses multiple interrupt pipelines to communicate with the processor through only one pipeline connected to the processor. Once the processor detects this signal, it interrupts its current work and processes the interrupt. When hardware devices generate interrupts, they do not consider synchronization with the processor's clock, that is, interrupts can be generated at any time, so the kernel may be interrupted at any time by new interrupts.

2. Interrupt types (categories)

Linux is usually divided into external interrupts (also called hardware interrupts) and internal interrupts (also called abnormal).

2.1 Synchronous interrupt (exception/internal interrupt)

Synchronous interrupt (exception/internal interrupt): Synchronous interrupt Generated by the CPU itself, also known as internal interrupt or exception

2.1.1 Synchronous interrupt example: page missing interrupt

The CPU is executing a When executing the instruction, if it is found that the page he wants to access (the page of the virtual address) is not in the physical memory, then the execution of the instruction will be stopped and a page non-existence exception will be generated.

An executable file may be large and placed in On the disk, only a part of it is read into the memory at a time (cpu locality principle).
When it wants to access the remaining content, a page fault interrupt will occur. At this time, it will be swapped in from the disk.

2.2 Asynchronous interrupt (interrupt/external interrupt)

Asynchronous interrupt (interrupt/external interrupt): Asynchronous interrupt is generated by an external hardware device , also known as external interrupt or interrupt

2.2.1 异步中断举例:网卡的工作原理

当网卡接受到数据包时,通知内核,触发中断,所谓的上半部就是,及时读取数据包到内存,防止因为延迟导致丢失,这是很急迫的工作。
读到内存后,对这些数据的处理不再紧迫,此时内核可以去执行中断前运行的程序,而对网络数据包的处理则交给下半部处理。

2.3 中断与异常的区别

异常与中断不同,中断是由硬件引起的;
异常则发生在编程失误而导致错误指令,或者在执行期间出现特殊情况必须要靠内核来处理的时候(比如缺页)。它在产生时必须考虑与处理器时钟同步,因此异常也称同步中断。

3、中断请求实现:上下半部机制

3.1 背景

中断处理程序运行需要快速执行(因为不可阻塞),同时要能完成尽可能多的工作,这里存在矛盾。

因此把中断处理切分为两个部分,上半部分(top half)接收到一个中断后立即执行,但是只做有严格时限的工作,例如对接收到的中断进行应答或复位硬件。能够被允许稍后完成的工作会推迟到下半部分(bottom half)去,此后在合适的时机下半部分会被中断执行,Linux提供了实现下半部分的各种机制。

优点:这种设计可以使系统处于中断屏蔽状态的时间尽可能的短,以此来提高系统的响应能力。

3.2 上半部:

中断处理程序是上半部——接受中断,他就立即开始执行,但只有做严格时限的工作。
上半部简单快速,执行时禁止一些或者全部中断。
工作内容:处理紧急功能,取寄存器状态。

3.3 下半部:

能够被允许稍后完成的工作会推迟到下半部去,此后,在合适的时机,下半部执行
工作内容:完成中断事件绝大多数任务。
下半部稍后执行,而且执行期间可以响应所有的中断。
下半部的实现有软中断实现, tasklet 实现和工作队列实现。

3.4 上下半部划分原则

1) 如果一个任务对时间非常敏感,将其放在中断处理程序中执行;
2) 如果一个任务和硬件有关,将其放在中断处理程序中执行;
3) 如果一个任务要保证不被其他中断打断,将其放在中断处理程序中执行;
4) 其他所有任务,考虑放置在下半部执行

3.5 举例子: 用网卡来解释一下这两半。

当网卡接受到数据包时,通知内核,触发中断,所谓的上半部就是,及时读取数据包到内存,防止因为延迟导致丢失,这是很急迫的工作。
读到内存后,对这些数据的处理不再紧迫,此时内核可以去执行中断前运行的程序,而对网络数据包的处理则交给下半部处理。

4、中断号

中断对应着一个中断号,内核通过这个中断号查找相应的中断服务程序。

每个中断都通过一个唯一的数字标志,这样操作系统才能够给不同的中断提供对应的中断处理程序。
这些中断值即中断请求线,例如IRQ 0是时钟中断、IRQ 1是键盘中断。对于连接在PCI总线上的设备而言,中断请求线是动态分配的。

5、中断上下文

中断服务程序不在进程上下文中执行,而是在一个与所有进程都无关的、专门的中断上下文中运行,以此保证中断服务程序能够在第一时间响应和处理中断请求,然后快速地退出。

处理器在任何指定时间点上的活动必然属于以下三种情况之一:

运行于用户空间,执行用户进程;
运行于内核空间,处于进程上下文,代表某个特定的进程执行;
			 (CPU空闲时,内核执行空进程)
运行于内核空间,处于中断上下文,与任何进程无关,处理某个特定的中断;

相关推荐:《Linux视频教程

The above is the detailed content of what is interrupt in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn