exception - 软件生成的中断和软件生成的异常有什么区别?

标签 exception x86 interrupt interrupt-handling

我正在阅读英特尔手册 3A 第 6 章中断和异常处理。

中断和异常分别有3个来源。

对于软件生成的中断,它表示:

The INT n instruction permits interrupts to be generated from within software by supplying an interrupt vector number as an operand. For example, the INT 35 instruction forces an implicit call to the interrupt handler for interrupt 35. Any of the interrupt vectors from 0 to 255 can be used as a parameter in this instruction. If the processor’s predefined NMI vector is used, however, the response of the processor will not be the same as it would be from an NMI interrupt generated in the normal manner. If vector number 2 (the NMI vector) is used in this instruction, the NMI interrupt handler is called, but the processor’s NMI-handling hardware is not activated. Interrupts generated in software with the INT n instruction cannot be masked by the IF flag in the EFLAGS register.

对于软件生成的异常,它说:

The INTO, INT 3, and BOUND instructions permit exceptions to be generated in software. These instructions allow checks for exception conditions to be performed at points in the instruction stream. For example, INT 3 causes a breakpoint exception to be generated. The INT n instruction can be used to emulate exceptions in software; but there is a limitation. If INT n provides a vector for one of the architecturally-defined exceptions, the processor generates an interrupt to the correct vector (to access the exception handler) but does not push an error code on the stack. This is true even if the associated hardware-generated exception normally produces an error code. The exception handler will still attempt to pop an error code from the stack while handling the exception. Because no error code was pushed, the handler will pop off and discard the EIP instead (in place of the missing error code). This sends the return to the wrong location.

那么,有什么区别呢?似乎都利用了 int n 指令。如何判断一段汇编代码中是否产生异常或中断?

最佳答案

在 x86 架构中,异常被作为中断处理,名义上是通过中断处理程序来处理的。
所以中断和异常是重叠的术语,后者是前者的一种。

从0到31的中断号是为CPU异常保留的,例如中断号0是#DE(除法错误),中断号13是#GP(一般保护)。

当 CPU 检测到应引发异常的情况(例如访问不存在的页面)时,它会执行一系列任务。

首先,如果需要,它会推送错误代码,有些异常(如#PF和#GP)会推送错误代码,有些(如#DE)则不会。
Intel manual 3 的第 6.15 节列出所有异常及其最终错误代码。

其次,它“调用”适当的中断处理程序,这类似于远调用,但将 EFLAGS 压入堆栈。

int n 仅执行第二步,它调用中断但不会推送任何错误代码,因为硬件中首先没有错误条件(并且因为 int n错误代码的概念出现之前就已经存在。
因此它可以用来模拟异常,软件最终必须推送适当的错误代码。

当您在代码中看到 int n 时,它绝不是异常。它是一个中断,最终用于将控制流引导到特定的操作系统异常处理程序。


琐事:int3(没有空格)很特殊,因为它被编码为CC,只有一个字节(正常的int nCD imm8)。这对于调试很有用,因为调试器可以将其放在代码段中的任何位置。
into 仅在 OF = 1 时生成 #OF 异常。

关于exception - 软件生成的中断和软件生成的异常有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40501713/

相关文章:

c - stm32 WFI不触发

c# - 在 N 层、域驱动设计、MVC 应用程序中处理异常

java - Spring异常消息?它是什么?

java - try catch 异常总是返回 null

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

c - 为什么省略 printf 打印垃圾的参数?

java - 外部结束无限循环java

linux - 我可以使用 bash 编写 linux GPIO 中断处理程序吗

assembly - 优化第 7 代英特尔酷睿视频 RAM 中递增的 ASCII 十进制计数器

assembly - 我什么时候应该单独使用 AESIMC,而不是使用 AESDEC