软件中断
如何用 C 语言编写软件中断程序?我知道需要编写一个中断服务例程,然后中断 CPU,以便可以调用该例程,但我不知道如何在 C 中执行此操作。另外,我不知道如何使用中断描述符注册该例程桌子。 我有一个 x86-64 CPU (AMD Turion64 X2) 并且我正在使用 gcc 编译器。 任何帮助将不胜感激。
How can I program software interrupt in C? I know need to write an interrupt servicing routine and then interrupt the CPU so that the routine can be called, but I don't know how to do that in C. Also, I don't know how to register that routine with Interrupt descriptor table.
I have an x86-64 CPU (AMD Turion64 X2) and I am using gcc compiler.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
中断的概念不包含在 C 规范中(并且在某种程度上也是特定于处理器的)。大多数编译器,包括 GCC,都允许您编写内联汇编代码(或者您当然可以将用汇编语言编写的文件链接到您的程序)。但最大的问题是,常见的操作系统(尤其是在 64 位模式下运行的操作系统)不允许您更改中断表。我想你最好的选择是寻找一个简单的开源操作系统,然后从普通程序安装中断处理程序(如果操作系统允许)或将代码添加到内核中。之所以不能只在处理器模拟器(或虚拟机)中运行一小段代码,是因为处理器需要进行大量设置才能进入 64 位模式。有关如何更改中断表的确切细节取决于该设置。
The concept of interrupts is not included in the C specification (and is also somewhat processor specific). Most compilers, including GCC, let you write inline assembly code (or you can of course link a file written in assembly to your program). But the big problem is that common operating systems (especially those running in 64-bit mode) will not let you alter the interrupt table. I guess your best bet is to look for a simple open source OS and either install your interrupt handler from a normal program (if the OS allows it) or add your code to the kernel. The reason why you can't just run a small piece of code in a processor simulator (or virtual machine) is that the processor needs quite a bit of setup to get into 64-bit mode. And the exact details about how you alter the interrupt table depends on that setup.
操作系统对应用程序隐藏了真正的中断。据我所知这是不可能的,除非你运行在内核层(ring 0?)
The operating system hides real interrupts from applications. As far as I know it isn't possible unless you are running in the kernel layer (ring 0?)
使用
sigaction
。请参阅man 2 sigaction
。要引发中断,请使用
raise
或kill
。Use
sigaction
. Seeman 2 sigaction
.To cause the interrupt, use
raise
orkill
.编写 ISR 不是语言问题。是的,你可以用 C 语言做到这一点。
这涉及到对操作系统内核的理解,涉及调用例程以在某个中断线上使用 GIC 或其他中断控制器注册 ISR 并在中断描述符表中创建一个条目。
就像 ARM 上的 Linux 一样。它的 request_irq() 向 IRQ 控制器注册 ISR 并在 IRQ_DESC 表中创建一个条目。
因此建议首先检查您各自操作系统的 ISR 框架。
writting ISR is not a matter of Language. yes you can do it in C.
Its a matter of Understanding of your OS Kernel, about routines that are called to register your ISR on a certain interrupt line with GIC or other interrupt controllers and make an Entry in Interrupt Descriptor table .
Like with Linux on ARM .. Its request_irq() to register an ISR with IRQ contoller and make an entry in IRQ_DESC table.
So will suggest first check ISR framework of your respective OS.