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
how to write an interrupt handler ?
-
- Member
- Posts: 190
- Joined: Tue Aug 26, 2008 11:24 am
- GitHub: https://github.com/sebihepp
Re: how to write an interrupt handler ?
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])
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 ?
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.
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
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
Re: how to write an interrupt handler ?
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 ?
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.
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
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
- Combuster
- 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 ?
There's a single byte push, normally for putting small constants onto the stack:
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.x86 manual wrote:6A PUSH imm8
Re: how to write an interrupt handler ?
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.
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.