Interrupt Service Routines: Difference between revisions
[unchecked revision] | [unchecked revision] |
m reworded one sentence, looks more sane now |
|||
Line 132: | Line 132: | ||
====gcc / g++==== |
====gcc / g++==== |
||
gcc nor g++ offer any means (on x86 or x86-64) |
Neither gcc nor g++ offer any means (on x86 or x86-64) to have an interrupt service routine only in C or C++ without performing '''black magic'''. |
||
===Black Magic=== |
===Black Magic=== |
Revision as of 23:55, 27 June 2012
The x86 architecture is an interrupt driven system. External events trigger an interrupt - the normal control flow is interrupted and a Interrupt Service Routine (ISR) is called.
Such events can be triggered by hardware or software. An example of a hardware interrupt is the keyboard: Every time you press a key, the keyboard triggers IRQ1 (Interrupt Request 1), and the corresponding interrupt handler is called. Timers, and disk request completion are other possible sources of hardware interrupts.
Software driven interrupts are triggered by the int opcode; e.g. the services provided by MS-DOS are called by the software triggering INT 21h and passing the applicable parameters in CPU registers.
For the system to know which interrupt service routine to call when a certain interrupt occurs, offsets to the ISR's are stored in the Interrupt Descriptor Table when you're in Protected mode, or in the Interrupt Vector Table when you're in Real Mode.
An ISR is called directly by the CPU, and the protocol for calling an ISR differs from calling e.g. a C function. Most importantly, an ISR has to end with the iret opcode, whereas usual C functions end with ret or retf. The obvious but nevertheless wrong solution leads to one of the most "popular" triple-fault errors among OS programmers.
The Problem
Many people shun away from Assembler, and want to do as much as possible in their favorite high-level language. GCC (as well as other compilers) allow you to add inline Assembler, so many programmers are tempted to write an ISR like this:
/* How NOT to write an interrupt handler */
void interrupt_handler(void)
{
__asm__("pushad"); /* Save registers. */
/* do something */
__asm__("popad"); /* Restore registers. */
__asm__("iret"); /* This will triple-fault! */
}
This cannot work. The compiler adds stack handling code before and after your function, which together with the iret results in Assembler code resembling this:
push %ebp
mov %esp,%ebp
sub $<size of local variables>,%esp
pushad
# C code comes here
popad
iret
# 'leave' if you use local variables, 'pop %ebp' otherwise.
leave
ret
It should be obvious how this messes up the stack (ebp gets push'ed but never pop'ed). Don't do this. Instead, these are your options.
Solutions
Plain Assembler
Learn enough about Assembler to write your interrupt handlers in it. ;-)
Two-Stage Assembler Wrapping
Write an Assembler wrapper calling the C function to do the real work, and then doing the iret.
/* filename : isr_wrapper.asm */
.globl _isr_wrapper
.align 4
_isr_wrapper:
pushad
call _interrupt_handler
popad
iret
/* filename : interrupt_handler.c */
void interrupt_handler(void)
{
/* do something */
}
Compiler Specific Interrupt Directives
Some compilers for some processors have directives allowing you to declare a routine interrupt, offer a #pragma interrupt, or a dedicated macro. Borland C, Watcom C/C++, Microsoft C 6.0 and Free Pascal Compiler 1.9.* and up offer this, while GCC does not. Visual C++ and clang-llvm offer an alternative shown under Naked Functions:
Borland C
/* Borland C */
void interrupt interrupt_handler(void)
{
/* do something */
}
Watcom C/C++
/* Watcom C/C++ */
void _interrupt interrupt_handler(void)
{
/* do something */
}
Naked Functions
Some Compilers can be used to make interrupt routines, but requires you to manually handle the stack and return operations. Doing so requires that the function be generated without an epilogue or prologue. This is called making the function naked - this is done in Visual C++ by adding the attribute _declspec(naked) and in clang-llvm by adding the attribute __attribute__((naked)) to the function. You need to verify that you do include a return operation (such as iretd) as that is part of the prologue that the compiler has now been instructed to not include.
If you intend to use local variables, you must set up the stack frame in the manner which the compiler expects; as ISRs are non-reentrant, however, you can simply use static variables.
Visual C++
Visual C++ also supplies the __LOCAL_SIZE assembler macro, which notifies you how much space is required by the objects on the stack for the function.
/* Microsoft Visual C++ */
void _declspec(naked) interrupt_handler()
{
_asm pushad;
/* do something */
_asm{
popad
iretd
}
}
clang-llvm
As of at least clang-llvm head (July 9th, 2011), clang-llvm supports naked functions.
/* clang-llvm */
__attribute__((naked)) void interrupt_handler()
{
asm ("pushad");
/* do something */
asm (
"popad" \
"iretd"
);
}
gcc / g++
Neither gcc nor g++ offer any means (on x86 or x86-64) to have an interrupt service routine only in C or C++ without performing black magic.
Black Magic
Look at the faulty code above, where the proper C function exit code was skipped, screwing up the stack. Now, consider this code snippet, where the exit code is added manually:
/* BLACK MAGIC - Strongly Discouraged! */
void interrupt_handler() {
__asm__("pushad");
/* do something */
__asm__("popad; leave; iret"); /* BLACK MAGIC! */
}
The corresponding output would look somewhat like this:
push %ebp
mov %esp,%ebp
sub $<size of local variables>,%esp
pushad
# C code comes here
popad
leave
iret
leave # dead code
ret # dead code
This assumes that leave is the correct end-of-function handling - you are doing the function return code "by hand", and leave the compiler-generated handling as "dead code". Needless to say, such assumptions on compiler internals are dangerous. This code can break on a different compiler, or even a different version of the same compiler. It is therefore strongly discouraged, and listed only for completeness.
Having read all the above warnings and still eager to have a solution for writing ISRs in C(++) code using gcc? Consider the following:
// Call this function when populating the IDT, to get the address of the ISR handling code
void * interrupt_handler( )
{
__asm__ __volatile__ goto( "jmp %l[endOfISR]" : : : "memory" : endOfISR );
__asm__ __volatile__( ".align 16\t\n" : : : "memory" ); // align by 16 for efficiency - could be even higher, depending on CPU
startOfISR:
__asm__ __volatile__( "pushad\t\n" : : : "memory" );
printf( "Hello, world of ISRs!" );
__asm__ __volatile__( "popad\t\niret\t\n" : : : "memory" );
endOfISR:
__asm__ __volatile__ goto( "mov %l[startOfISR], %%eax" : : : "memory" : startOfISR );
}
This code uses "asm goto" - a relatively new GCC-specific feature (see http://wiki.osdev.org/Inline_Assembly#asm_goto). Gotos are evil, and asm gotos are worse - but in this case they might come in handy.
See Also
External Links
- Interrupts & IRQs at osdever.net
- IRQs and the PIT by James Molloy