gccnasminterrupt-handling

Make GCC Use iret instead of ret


I am trying to use interrupts in my simple operating system. I have successfully set up the IDT, and have made a C function in assembly to set a gate (below), but now I have a problem: How would I write an interrupt handler, ending with iret, in C? Here is my code:

setGate: ;the first argument is the gate number
         ;the second the address to the function
push ebp
mov  ebp, esp
push eax
push ebx

mov  eax, [ebp+12]
mov  ebx, idtStart
add  ebx, [ebp+8]
mov  [ebx*8], ax
mov  word [ebx*8+2], 0x8
mov  word [ebx*8+4], 0x8E00
shr  eax, 16
mov  [ebx*8+6], ax

pop  ebx
pop  eax
pop  ebp
ret

My C code is:

void handler(void)
{
    print("The interrupt has been handled", 15, 0);
}

void main(void)
{
    loadIDT();
    setGate(32, (unsigned long)&handler);
    __asm__("int $32");
}

This code causes the OS to crash, however when I use assembly and iret it doesn't.

I am compiling with GCC and Nasm, and emulating on Qemu, in 32-bit protected mode.


Solution

  • As suggested by Michael Petch,__attribute__((interrupt))__ on my interrupt handler/function worked for my situation.

    Unfortunately, as this was five years ago I can’t give any more details on what I did. At the time I was very satisfied with the solution.