# 中断

中断是发给处理器的一种信号，表示发生了需要处理器关注的事件，使处理器暂停当前正在运行的程序，执行一个称为[中断处理程序](https://www.edgepedia.org/zh/interrupt-handler)（或中断服务例程）的特殊软件例程，然后恢复被中断的程序。中断在程序执行过程中随机发生，以响应来自硬件的信号；软件也可以有意生成中断，例如在 x86 上执行 INT n 指令。相关术语“异常”涵盖处理器在执行指令时检测到的错误状况，例如除零、缺页和机器检查故障。<sup>[1](https://cdrdv2-public.intel.com/835754/253668-sdm-vol-3a.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| x86 向量空间 | 向量号 0 至 255；0 至 31 保留给体系结构定义的异常和中断，32 至 255 由用户定义，通常分配给外部 I/O 设备<sup>[1](https://cdrdv2-public.intel.com/835754/253668-sdm-vol-3a.pdf)</sup> |
| 分派机制 | 向量号用作中断描述符表（IDT）的索引，该表提供处理程序的入口点<sup>[1](https://cdrdv2-public.intel.com/835754/253668-sdm-vol-3a.pdf)</sup> |
| 门类型 | 中断门在进入时清除 EFLAGS 的 IF 标志，以阻止进一步中断；陷阱门保持 IF 不变<sup>[2](https://cdrdv2-public.intel.com/868137/325462-089-sdm-vol-1-2abcd-3abcd-4.pdf)</sup> |
| Arm 公司（Arm）GIC 中断类型 | 共享外设中断（SPI）、私有外设中断（PPI）、软件生成中断（SGI）和局部特定外设中断（LPI，GICv3 引入）<sup>[3](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/GICv3_v4_overview.pdf?revision=65f91645-cd52-4795-952b-f01095ff5ef8)</sup> |
| Cortex-M 延迟 | 在零等待状态存储器下，从中断请求生效到执行处理程序第一条指令为 12 个周期（Cortex-M3 和 M4）、15 个周期（M0+）和 16 个周期（M0）<sup>[4](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/beginner-guide-on-interrupt-latency-and-interrupt-latency-of-the-arm-cortex-m-processors)</sup> |
| MSI 容量 | MSI 每设备最多支持 32 个中断（为 2 的幂，连续分配）；MSI-X 支持 1 至 2048 个独立配置的中断<sup>[5](https://docs.kernel.org/5.19/PCI/msi-howto.html)</sup> |
| 实时 Linux（Linux） | PREEMPT_RT 强制采用线程化中断：IRQ 上下文中的主处理程序唤醒一个线程化处理程序，后者以 SCHED_FIFO 优先级 50 在进程上下文中运行<sup>[6](https://docs.kernel.org/core-api/real-time/theory.html)</sup> |

## 中断与异常

x86 体系结构按来源区分这两类事件。中断表示处理器外部的事件，例如请求服务外设，并在不可预测的时刻到达。异常发生在处理器执行指令时检测到错误状况之时，包括保护违规、缺页和内部机器故障；机器检查体系结构还允许在检测到内部硬件错误和总线错误时产生[机器检查异常](https://www.edgepedia.org/zh/machine-check-exception)。<sup>[1](https://cdrdv2-public.intel.com/835754/253668-sdm-vol-3a.pdf)</sup> 在两种情况下，正在运行的例程或任务都会在处理程序执行期间被挂起，随后恢复执行，程序连续性不会丢失，除非无法从异常中恢复，或者该中断终止了程序。<sup>[1](https://cdrdv2-public.intel.com/835754/253668-sdm-vol-3a.pdf)</sup>

**向量与 IDT。** 每个体系结构定义的异常和每种中断条件都被分配一个唯一标识号，即向量号，处理器将其用作中断描述符表（IDT）的索引。IDT 条目提供处理程序的入口点。在英特尔（Intel）的 Intel 64 和 IA-32 处理器上，IA-32 体系结构定义了 18 个预定义中断和异常以及 224 个与 IDT 条目关联的用户定义中断；向量 0 至 8、10 至 14 和 16 至 19 是预定义向量，向量 32 至 255 用于软件定义中断，即软件中断或可屏蔽硬件中断。<sup>[1](https://cdrdv2-public.intel.com/835754/253668-sdm-vol-3a.pdf)</sup><sup> • </sup><sup>[2](https://cdrdv2-public.intel.com/868137/325462-089-sdm-vol-1-2abcd-3abcd-4.pdf)</sup> 预定义示例包括向量 0 用于除法错误（#DE，来自 DIV 和 IDIV）、向量 3 用于断点（#BP，来自 INT3）、向量 6 用于无效操作码（#UD），以及向量 2 用于不可屏蔽中断（NMI）。<sup>[2](https://cdrdv2-public.intel.com/868137/325462-089-sdm-vol-1-2abcd-3abcd-4.pdf)</sup>

**门类型。** IDT 条目可以是中断门、陷阱门或任务门。中断门与陷阱门之间的唯一区别是 IF 标志的处理方式：通过中断门时，处理器清除 IF，使其他中断无法干扰处理程序，随后的 IRET 会恢复它；通过陷阱门时，IF 不变。任务门会引发完整的任务切换，自动保存被中断任务的整个上下文，并可提供全新的特权级 0 栈，但保存的机器状态量使其比中断门更慢，并增加中断延迟。<sup>[2](https://cdrdv2-public.intel.com/868137/325462-089-sdm-vol-1-2abcd-3abcd-4.pdf)</sup>

## 中断控制器

由于处理器的中断输入数量有限，系统使用中断控制器来收集、确定优先级并路由中断源。

**x86 APIC。** 在 Intel 64 和 IA-32 系统上，本地 APIC 接收中断，将其记录在中断请求寄存器（IRR）中，并服务优先级最高的待处理中断；处理程序必须在 IRET 指令之前写入中断结束（EOI）寄存器以表示完成，随后 APIC 清除服务中寄存器中的最高优先级位并分派下一个中断。对于电平触发中断，本地 APIC 还会向 I/O APIC 发送 EOI 消息。<sup>[7](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.pdf)</sup> 本地 APIC 的中断命令寄存器（ICR）允许软件向其他处理器发送处理器间中断（IPI）、转发未服务的中断、中断自身，或传递启动 IPI（SIPI）等特殊 IPI。<sup>[8](https://cdrdv2-public.intel.com/868136/252046-081-sdm-change-document.pdf)</sup> 在 SMP 主板上，IO-APIC 作为一种增强型中断控制器，将硬件中断路由到多个 CPU 或 CPU 组，高端服务器使用多个 IO-APIC 进一步分散 IRQ 负载。<sup>[9](https://docs.kernel.org/6.6/arch/x86/i386/IO-APIC.html)</sup>

**Arm GIC。** Arm 的通用中断控制器（GIC）管理四种中断源类型，由 INTID 号标识：SGI 占用 INTID 0 至 15，通常用于处理器间通信；PPI 占用 16 至 31，对单个核心私有，通用定时器即为一例；SPI 占用 32 至 1019，可传递给任何连接的核心；LPI 在 GICv3 中首次引入，占用 INTID 8192 及以上，上限由实现定义。<sup>[3](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/GICv3_v4_overview.pdf?revision=65f91645-cd52-4795-952b-f01095ff5ef8)</sup> 每个处理单元（PE）都有一个优先级掩码寄存器（ICC_PMR_EL1），用于设置中断被转发给该 PE 所必须达到的最低优先级。当 PE 确认一个中断时，其运行优先级变为该中断的优先级，并在 PE 写入中断结束寄存器时恢复为原值；较高优先级中断可以抢占较低优先级中断，所需的优先级差通过二进制点寄存器（ICC_BPRn_EL1）控制。<sup>[10](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/GICv3_v4_overview.pdf?revision=93ada6c8-22f2-44a8-9fcc-395b870e490f)</sup> 三种物理中断类型（IRQ、FIQ 和异步错误类别）可以使用 SCR_EL3 和 HCR_EL2 独立路由到异常级别 EL1、EL2 或 EL3，路由到低于当前执行异常级别的异常会被隐式屏蔽，直到 PE 到达所路由的级别。<sup>[11](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Exception%20model.pdf)</sup>

**Cortex-M NVIC。** 在微控制器中，Arm Cortex-M 系列的嵌套向量中断控制器（NVIC）支持外设中断请求以及内部异常，例如 SysTick（一个常被操作系统使用的 24 位定时器）和不可屏蔽中断输入。NMI 和 HardFault 系统异常具有固定优先级。<sup>[4](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/beginner-guide-on-interrupt-latency-and-interrupt-latency-of-the-arm-cortex-m-processors)</sup>

**RISC-V。** 在 RISC-V 特权体系结构下，一个 hart 分配编号 0 至 15 的主要中断标识：机器级和监管级的外部中断分别携带编号 11 和 9，这两个级别的定时器中断为 7 和 5，软件中断为 3 和 1。诸如 APLIC 或 IMSIC 之类的外部中断控制器为每个特权级别下的外部中断提供次级标识，以区分设备或原因。<sup>[12](https://docs.riscv.org/reference/hardware/aia/_attachments/riscv-interrupts.pdf)</sup>

## 中断延迟

[中断延迟](https://www.edgepedia.org/zh/interrupt-latency)是从中断请求生效到执行处理程序第一条指令的时间。对于具有零等待状态存储器的 Cortex-M 处理器，Cortex-M3 和 Cortex-M4 为 12 个周期，Cortex-M0+ 为 15 个周期，Cortex-M0 为 16 个周期；在 M3/M4 上，进入序列会在 12 个周期的延迟内压入寄存器 R0 至 R3 和 R12，因此处理程序可以写成普通的 C 子例程。<sup>[4](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/beginner-guide-on-interrupt-latency-and-interrupt-latency-of-the-arm-cortex-m-processors)</sup> 延迟数字不包括软件开销，也不说明抖动——即中断到达时 CPU 正在做什么所造成的响应时间变化；在 8051 等体系结构上，进入处理程序必须等到多周期指令结束才能开始。Cortex-M 处理器通过放弃并重启大多数多周期指令，以及在 PSR 中保存被中断的多重加载/存储的状态以便之后恢复，来限制抖动。<sup>[4](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/beginner-guide-on-interrupt-latency-and-interrupt-latency-of-the-arm-cortex-m-processors)</sup>

## 消息信号中断

传统 PCI 设备通过物理引脚发出中断信号，这些引脚常由多个设备共享。相比之下，消息信号中断（MSI）通过设备向专用地址发送的内存写入来工作，促使 CPU 接收中断；在 PCIe 设备上，信号路径使用 MSI-X，它可以将每个中断定向到特定 CPU。<sup>[5](https://docs.kernel.org/5.19/PCI/msi-howto.html)</sup><sup> • </sup><sup>[13](https://docs.kernel.org/networking/scaling.html)</sup> MSI 能力最初在 PCI 2.2 中规定，并在 PCI 3.0 中增强，允许单独屏蔽每个中断；MSI-X 随 PCI 3.0 引入，支持每设备更多中断且可独立配置。<sup>[5](https://docs.kernel.org/5.19/PCI/msi-howto.html)</sup> 由此带来两个实际优势：按定义，MSI 是独占的中断向量，因此处理程序无需验证是哪个设备引起了中断；MSI 避免了 DMA/IRQ 竞态条件，因为当 MSI 传递时，发往主机内存的 DMA 保证对主机 CPU 可见，使驱动程序可以省略用于刷新 DMA 流的 MMIO 读取。<sup>[14](https://docs.kernel.org/PCI/pci.html)</sup> 在 Linux 中，驱动程序调用 pci_alloc_irq_vectors() 分配向量，调用 request_irq() 注册处理程序；如果设备既不支持 MSI-X 也不支持 MSI，则回退到单个传统 IRQ 向量。<sup>[5](https://docs.kernel.org/5.19/PCI/msi-howto.html)</sup>

## 操作系统中的中断处理

**Linux 通用 IRQ 层。** Linux 内核的通用中断处理层为设备驱动程序提供了完整的中断处理抽象，因此驱动程序通过通用 API 函数请求、启用、禁用和释放中断，而无需了解中断硬件细节。最初的实现使用一个 __do_IRQ() 超级处理程序，能够处理每一种中断逻辑；拉塞尔·金（Russell King）在 Linux 2.5/2.6 的 ARM 中断实现中识别出电平、边沿和简单处理程序类型，该实现增加了快速 EOI 和每 CPU 类型，拆分高层 IRQ 处理以针对每种中断类型优化流程。<sup>[15](https://docs.kernel.org/6.12/core-api/genericirq.html)</sup> 该层提供默认流处理程序，例如 handle_level_irq，它屏蔽并确认芯片、运行处理程序，然后解除屏蔽；handle_edge_irq，它在处理期间有中断到达时循环；以及 handle_fasteoi_irq，它仅在服务完成后发出 EOI 调用。<sup>[16](https://docs.kernel.org/core-api/genericirq.html)</sup>

**线程化中断与实时调度。** 中断处理程序在禁用抢占且不受调度器控制的情况下执行，这与实时要求冲突。[PREEMPT_RT](https://www.edgepedia.org/zh/preempt-rt) 补丁集强制实施线程化中断处理程序：处理被拆分为一个在 IRQ 上下文中执行、禁用中断的主处理程序，其唯一职责是唤醒关联的线程化处理程序，以及一个线程化处理程序，即传递给 request_irq() 的函数，由内核调度在进程上下文中运行。从唤醒线程到处理完成，中断源在控制器中被屏蔽，因此设备中断保持待处理状态而不会再次触发 CPU。默认情况下，线程化处理程序以 SCHED_FIFO 策略、优先级 50 运行，该优先级处于最低和最高实时优先级的正中间。<sup>[6](https://docs.kernel.org/core-api/real-time/theory.html)</sup>

**IRQ 亲和性。** 在多处理器系统上，/proc/irq/IRQ#/smp_affinity 以位掩码指定给定 IRQ 源允许使用哪些目标 CPU，smp_affinity_list 则接受 CPU 范围；不允许关闭所有 CPU，默认掩码为 0xffffffff，意味着任何 CPU 都可以处理该中断。<sup>[17](https://docs.kernel.org/next/core-api/irq/irq-affinity.html)</sup> 在网络中，将接收中断分散到各 CPU 之间是有利的，因为数据包处理的不可忽略的一部分发生在接收中断处理中；使用 MSI-X 时，每个接收队列都有单独的 IRQ，可以路由到特定 CPU；对于低延迟网络，建议配置分配与系统中 CPU 数量一样多的队列，如果网卡上限更低则取网卡上限。<sup>[13](https://docs.kernel.org/networking/scaling.html)</sup>

## 不可屏蔽中断

不可屏蔽中断（NMI）是操作系统无法通过普通屏蔽机制忽略的中断。在 Arm A 配置（A-profile）处理器上，体系结构层面的 NMI 支持随 Armv8.8-A 和 Armv9.3-A 扩展到来：配置了 NMI 属性的中断被视为所属安全状态的最高优先级，具有不同的屏蔽和抢占规则，操作系统通过 SCTLR_ELx 系统寄存器中的选择控制来决定如何屏蔽 NMI，而普通中断仍由现有的 PSTATE.I 位屏蔽。<sup>[18](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/arm-a-profile-architecture-developments-2021)</sup> 在该支持出现之前，arm64 上的 Linux 依赖用 GIC 中断优先级特性实现的伪 NMI，这种方案可用于性能分析，但在 KVM 客户机进入等关键路径上增加了软件复杂性，因为管理程序必须经过若干谨慎步骤来操作优先级掩码和 PSTATE.I。<sup>[19](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/a-profile-non-maskable-interrupts)</sup>

## References

1. [Intel® 64 and IA-32 Architectures Software Developer's Manual, Volume 3A: System Programming Guide, Part 1, Chapter 7, "Interrupt and Exception Handling"](https://cdrdv2-public.intel.com/835754/253668-sdm-vol-3a.pdf)
2. [Intel® 64 and IA-32 Architectures Software Developer's Manual, Combined Volumes, "Procedure Calls, Interrupts, and Exceptions"](https://cdrdv2-public.intel.com/868137/325462-089-sdm-vol-1-2abcd-3abcd-4.pdf)
3. [Arm CoreLink GICv3/v4 Programmer's Guide, "Arm CoreLink GIC fundamentals"](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/GICv3_v4_overview.pdf?revision=65f91645-cd52-4795-952b-f01095ff5ef8)
4. [Beginner Guide on Interrupt Latency of the Arm Cortex-M Processors, Arm Community](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/beginner-guide-on-interrupt-latency-and-interrupt-latency-of-the-arm-cortex-m-processors)
5. [The MSI Driver Guide HOWTO, Linux Kernel documentation](https://docs.kernel.org/5.19/PCI/msi-howto.html)
6. [Theory of operation (PREEMPT_RT), Linux Kernel documentation](https://docs.kernel.org/core-api/real-time/theory.html)
7. [Intel® 64 and IA-32 Architectures Software Developer's Manual, Volume 3A, "Advanced Programmable Interrupt Controller (APIC)"](https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.pdf)
8. [Intel SDM Documentation Changes, "Issuing Interprocessor Interrupts"](https://cdrdv2-public.intel.com/868136/252046-081-sdm-change-document.pdf)
9. [IO-APIC, Linux Kernel documentation](https://docs.kernel.org/6.6/arch/x86/i386/IO-APIC.html)
10. [Arm CoreLink GICv3/v4 Programmer's Guide, "Running priority and preemption"](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/GICv3_v4_overview.pdf?revision=93ada6c8-22f2-44a8-9fcc-395b870e490f)
11. [AArch64 Programmer's Guides: Exception model, Arm](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Learn%20the%20Architecture/Exception%20model.pdf)
12. [The RISC-V Advanced Interrupt Architecture](https://docs.riscv.org/reference/hardware/aia/_attachments/riscv-interrupts.pdf)
13. [Scaling in the Linux Networking Stack, Linux Kernel documentation](https://docs.kernel.org/networking/scaling.html)
14. [How To Write Linux PCI Drivers, Linux Kernel documentation](https://docs.kernel.org/PCI/pci.html)
15. [Linux generic IRQ handling, Linux Kernel documentation (6.12)](https://docs.kernel.org/6.12/core-api/genericirq.html)
16. [Linux generic IRQ handling, Linux Kernel documentation](https://docs.kernel.org/core-api/genericirq.html)
17. [SMP IRQ affinity, Linux Kernel documentation](https://docs.kernel.org/next/core-api/irq/irq-affinity.html)
18. [Arm A-profile architecture developments 2021 (Armv8.8-A, Armv9.3-A), Arm Community](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/arm-a-profile-architecture-developments-2021)
19. [A-profile Non-maskable Interrupts, Arm Community](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/a-profile-non-maskable-interrupts)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › Processors & processor engineering › Computer architecture theory › CPU internal structure

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/interrupt

English original: https://www.edgepedia.org/interrupt
