Page 1 of 1

how to write an interrupt handler ?

Posted: Fri Nov 20, 2015 6:29 am
by matan
I have succesfully managed to install an IDT table , I have a nasm file containing all of the handlers to all of the 256 interrupts:

extern C_handler
interrupt_handler:
;what the hell should I do in here ?

call C_handler ;I want the C_handler to be passed the interrupt number and an error code.

;what the hell should I do in here ?
iret

interrupt_handler_0:
cli
push dword 0 ;dummy error code to keep uniform stack frame
push byte 0 ;pushing interrupt number for use later in C function
jmp interrupt_handler

... more handlers here

interrupt_handler_8:
cli
;not pushing dummy error because an error code ha already been pushed in this interrupt
push byte 8 ;pushing interrupt number for use later in C function
jmp interrupt_handler

... more handlers here

Re: how to write an interrupt handler ?

Posted: Fri Nov 20, 2015 6:50 am
by sebihepp
Well, it's up to you, what you do exactly in interrupt_handler.

But I would recommend saving all registers. If you don't save them and your C_handler overwrites them (because he uses them), then, after iret, the normal process could fail, because he relies on values in the registers, that have changed in the mean time due to the interrupt.
You can save the registers by pushing them on the stack and popping them back after C_handler, for example.

(I would save: | eax, ebx, ecx, edx | esi, edi, ebp | eventually ds, es, fs, gs | [ If you programm in 64Bit, then you save rax, rbx, etc. instead])

Re: how to write an interrupt handler ?

Posted: Fri Nov 20, 2015 7:07 am
by SpyderTL
If the interrupt came from an IRQ from the master PIC (IRQ 0-7), then you'll need to acknowledge the IRQ before you IRET. If the IRQ came from the slave PIC (IRQ 8-15), then you'll need to acknowledge the IRQ on both the master and slave PICs.

If not, then you'll end up in an endless loop.

Re: how to write an interrupt handler ?

Posted: Fri Nov 20, 2015 7:20 am
by matan
BTW , is pushing and popping always 4 bytes ? when i say `push byte 0` it still pushes 4 bytes ?

Re: how to write an interrupt handler ?

Posted: Fri Nov 20, 2015 8:20 am
by SpyderTL
Apparently, there is no Push Byte instruction, so you can only push Words and Dwords. (And Qwords in Long Mode)

You could probably do it manually by changing the SP register by one byte, but you'll probably see a performance hit.

Re: how to write an interrupt handler ?

Posted: Fri Nov 20, 2015 10:17 am
by Combuster
There's a single byte push, normally for putting small constants onto the stack:
x86 manual wrote:6A PUSH imm8
This will actually modify the stackpointer by 2 or 4 bytes depending on your BITS settings so that a pop will allow you to read an entire register's worth.

Re: how to write an interrupt handler ?

Posted: Sat Nov 21, 2015 8:10 am
by Boris
You will soon want to get the value of EIP /EBP before the interrupt, in case of exceptions, in order to get a stack trace, or to do a context switch.
Anyway, how you pass parameters to your C function depends on your C calling convention.
Look at how your generated ASM code from C code looks like.