how to write an interrupt handler ?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
matan
Posts: 13
Joined: Sat May 02, 2015 4:15 am
Libera.chat IRC: matan

how to write an interrupt handler ?

Post 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
sebihepp
Member
Member
Posts: 190
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: how to write an interrupt handler ?

Post 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])
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: how to write an interrupt handler ?

Post 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.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
matan
Posts: 13
Joined: Sat May 02, 2015 4:15 am
Libera.chat IRC: matan

Re: how to write an interrupt handler ?

Post by matan »

BTW , is pushing and popping always 4 bytes ? when i say `push byte 0` it still pushes 4 bytes ?
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: how to write an interrupt handler ?

Post 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.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how to write an interrupt handler ?

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: how to write an interrupt handler ?

Post 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.
Post Reply