Page 1 of 1
Interrupt handling
Posted: Mon Dec 03, 2007 7:21 am
by afarnen
In x86 assembly, how do you handle interrupts called by the CPU? Same principles of a try...catch statement in C. For instance, how would I catch the int 0h (divide by zero) thrown by the CPU?
Posted: Mon Dec 03, 2007 7:57 am
by AJ
IDT
[EDIT:]It's not really the same as try..catch - your program flow will be redirected and may then return after the exception. For exception handling of the try...catch type, you will need to port libstdc++ or something similar.
@Mods: This should be in the OS development forum
[/EDIT]
Posted: Mon Dec 03, 2007 10:53 am
by didroe
Sorry to semi-hijack this thread, seems wrong to create a new thread with pretty much the same title and the reply to my question might be useful to the OP anyway...
Are there any decent tutorials on x86-64 interrupts? Or does anyone have links to some code that works? I'm playing about with the div-by-zero exception but I can't get it to return using iret. Bochs gives me an error about having a null code segment. AFAIK the CS is pushed onto the stack when the interrupt handler is called, and then the iret should pop it off again before returning. When I step through up until the iret instruction and look at the stack memory, everything seems fine. I can see the correct CS value on the stack. A Bochs bug maybe?
Note: I realise I need more code in the handler to set the correct return address, at the moment I'm just trying to get it to throw the exception infinitely as a test.
Posted: Mon Dec 03, 2007 11:00 am
by AJ
Hi,
I only consider myself to be a 64 bit newbie, but have a look at
http://www.amd.com/us-en/assets/content ... erview.pdf if you haven't seen it already.
Cheers,
Adam
Posted: Mon Dec 03, 2007 6:55 pm
by mystran
didroe wrote:
Are there any decent tutorials on x86-64 interrupts? Or does anyone have links to some code that works? I'm playing about with the div-by-zero exception but I can't get it to return using iret. Bochs gives me an error about having a null code segment. AFAIK the CS is pushed onto the stack when the interrupt handler is called, and then the iret should pop it off again before returning. When I step through up until the iret instruction and look at the stack memory, everything seems fine. I can see the correct CS value on the stack. A Bochs bug maybe?
Double check that you remove the error-code from the stack before attempting to IRET.
Posted: Mon Dec 03, 2007 8:06 pm
by didroe
I've fixed it now, turned out the compiler I was using was inserting a 32 bit iret instruction. I switched to something different and then got an error in Bochs saying "SS AR must be writable data segment" (or words to that effect). Ended up having to fix a bug in Bochs. It's been a long day just fixing one line of code!