c - C 中 int 汇编指令的精确控制流程,以及由此产生的段错误

标签 c intel

考虑一下这个完全愚蠢的代码:

int main() { __asm__("int $0x2"); }

这会在运行时导致段错误。 2 是 intel IDT 中 NMI 的代码(第 6.3.1 节 here )。

我很好奇为什么会出现段错误?最终导致段错误的控制流到底是什么?

同时将手册的第 6.3.3 节粘贴到此处:

6.3.3 Software-Generated Interrupts
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.

最佳答案

idt 中的门包含一个描述符特权级别 (DPL),它是允许调用此条目的最大调用者特权级别 (CPL)。真正的NMI是由CPU上的电信号引起的,它提供了一个人工的CPL 0。这样,内核就不必区分真信号和假信号。

通过int xx调用的系统服务将具有数值更大的DPL,以允许指令用指令打开门。根据您的内核,int 3(断点)、4(溢出)和 5(边界)可能会作为直接操作码以方便调试,分别为“into”和“bounds”操作码。

关于c - C 中 int 汇编指令的精确控制流程,以及由此产生的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59254571/

相关文章:

c - 为什么我在尝试接受连接时出现 "Invalid argument"?

c++ - gcc AtomicBuiltins gcc 4.1.1 中的奇怪行为

c - 结构类型未知

使用多个线程计算给定间隔的总和

x86 - Spectre (v2) 的内部工作原理

c - 变量声明上的函数

intel - vaainfo 中的 vaapi 权限问题

unit-testing - 为大型 Fortran 项目寻找严肃的代码覆盖工具

c++ - 事务中的线程信号

image-processing - openCV 使用什么样的加速?怎么处理的这么快?