如果在第一个中断操作完成之前引发另一个中断,会发生什么情况?
这个问题来自中断处理主题。
假设正在处理一个中断。 如果在第一个中断操作完成之前引发另一个中断,会发生什么情况?
This question is from the interrupt handling topic.
Suppose an interrupt is being serviced. What happens if another interrupt is raised even before the first interrupt action is completed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下内容仅适用于 x86 架构,但其他架构很可能遵循相同的模式:
有一个称为 IF(中断标志)的处理器标志,用于控制是否可以处理硬件中断,或者是否必须处理硬件中断。搁置。 当 IF = 0 时,中断将被推迟,直到重新使能标志为止(NMI 除外,即不可屏蔽中断,其旨在作为无法被阻止的“仅紧急”中断)。
在调用中断服务例程之前,
IF
由处理器自动清除。 这对于防止中断调用变得可重入失控是必要的。 请注意,中断服务代码本身无法自行执行此操作,因为如果在进入例程之前未禁用IF
,则在服务代码有时间执行之前可能会发生更多中断即使是一条指令。 然后,中断的“流水”将立即导致(所有事情)堆栈溢出。因此,回答您的直接问题:通常,当第一个硬件中断正在服务时发生第二个硬件中断时,该中断将被搁置,直到第一个中断完成。
和往常一样,整个故事有点复杂。 英特尔网站上的英特尔架构软件开发人员手册站点从第 10-4 页开始提供了更完整的描述。
The following applies to the x86 architecture only, but other architectures might well follow the same pattern:
There is a processor flag called
IF
(Interrupt Flag) that controls whether hardware interrupts can be processed, or have to be put on hold. When IF = 0, interrupts will be postponed until the flag is reenabled (Except for the NMI, the Non-Maskable Interrupt, which is intended as an 'emergency only' interrupt that cannot be blocked).The
IF
is automatically cleared by the processor before an interrupt servicing routine is called. This is necessary to prevent interrupt calls to become reentrant out of control. Note that the interrupt servicing code itself could not do this on its own, because ifIF
were not disabled before entering the routine, it would be possible for more interrupts to occur before the servicing code has time to execute even a single instruction. Then, a "firehose" of interrupts would immediately result in (of all things) a stack overflow.So, in answer to your direct question: typically, when a second hardware interrupt occurs while an initial one is being serviced, that interrupt will be put on hold until the first one has finished.
As usual, the full story is a bit more complicated. The Intel Architecture Software Developer’s Manual at Intel's web site gives a more complete description starting on page 10-4.
这取决于系统。 通常,如果新中断的优先级高于第一个中断,则会对其进行响应,并挂起第一个中断的处理程序。 当其处理程序完成时,原始中断处理程序将恢复。 最后,假设不再有中断,原始处理程序完成并恢复正常服务。 有时,恢复的进程将是被中断的进程; 有时,它不再是最符合条件的进程,而其他进程将恢复。
同样,如果在第一个处理程序完成之前发生原始中断的第二个或后续实例,或者如果发生较低或相同优先级的中断,则它将被保留,直到第一个处理程序完成。 在恢复正常处理之前,内核会检查本应处理但被阻止的未完成中断。
中断处理程序可能会阻止其他中断。
It depends on the system. Normally, if the new interrupt is a higher priority than the first, then it is responded to, suspending the handler for the first interrupt. When its handler finishes, then the original interrupt handler resumes. Finally, assuming no more interrupts, the original handler finishes and normal service resumes. Sometimes, the resumed process will be the process that was interrupted; sometimes, it will no longer be the most eligible process and some other one will resume.
Similarly, if a second or subsequent instance of the original interrupt occurs before the first handler completes, or if a lower or equal priority interrupt occurs, it will be held up until the first handler completes. Before normal processing is resumed, the kernel checks for outstanding interrupts that should have been handled but were blocked.
An interrupt handler may block other interrupts.
好吧,如果在第一个中断后没有禁用中断,第二个中断将导致您的中断服务例程再次被调用。 您必须确保禁用中断以避免这种明显不良的行为。
因此,如果您的中断服务例程正在执行其任务,然后发生另一个中断,则就像您正在执行其他操作一样:将调用相应的中断例程。
在Intel架构上,“cli”指令将禁用中断,“sti”将再次启用它们。
Well, if interrupts were not disabled after the first interrupt, the second will cause your interrupt service routine to be called again. You must make sure interrupts get disabled to avoid this decidedly undesirable behavior.
So, if your interrupt service routine is doing its thing, and then another interrupt occurs, it will be just as if you were doing anything else: the corresponding interrupt routine will be called.
On the Intel architecture, the "cli" instruction will disable interrupts, and "sti" will enable them again.