Hi, 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.
Use sigaction
. See man 2 sigaction
.
To cause the interrupt, use raise
or kill
.
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?)
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.