Interrupt handling

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
afarnen
Posts: 4
Joined: Mon Dec 03, 2007 6:34 am

Interrupt handling

Post 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?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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]
didroe
Posts: 6
Joined: Wed Nov 28, 2007 9:05 am

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post 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.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
didroe
Posts: 6
Joined: Wed Nov 28, 2007 9:05 am

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