软件中断

发布于 2024-09-14 19:14:43 字数 163 浏览 3 评论 0原文

如何用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可遇━不可求 2024-09-21 19:14:43

中断的概念不包含在 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.

聊慰 2024-09-21 19:14:43

操作系统对应用程序隐藏了真正的中断。据我所知这是不可能的,除非你运行在内核层(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?)

べ映画 2024-09-21 19:14:43

使用sigaction。请参阅man 2 sigaction

要引发中断,请使用 raisekill

Use sigaction. See man 2 sigaction.

To cause the interrupt, use raise or kill.

娇柔作态 2024-09-21 19:14:43

编写 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文