tags:

views:

67

answers:

2

Can I do floating point operations in interrupt handler ? (x86 or 64) Also I would like know can I use print function inside the interrupt handler ?

+2  A: 

Inside an interrupt handler, don't use anything that can block. That means don't use print functions unless they are non-blocking. Ideally, your ISR should do the bare minimum needed to clear the interrupt and then launch a normal thread to do the rest of the processing. If you need the print statements for debugging, then consider having the non-ISR part of your program declare a global, volatile buffer and have your ISR write your debug data into it. Your non-ISR code can check the buffer and printf the data from it if needed.

You should avoid floating-point operations inside an ISR (and in kernel code in general) as well.

bta
if I do floating point operations does it work ?
mousey
Yes the float operations will "work", but depending on a whole lot of factors that don't fit in this margin, it might corrupt the state of floating point operations in non-interrupt threads. The general advice is just don't unless you know for certain that the FPU state is properly preserved across the interrupt, and even then really just don't.
RBerteig
@RBerteig Thank you very much
mousey
Both floating-point operations and print statements will "work" in an ISR in the sense that the compiler won't throw an error that says "You can't do that in a kernel ISR!". However, neither of these are supported in an interrupt context and using them will dramatically increase the chance of all hell breaking loose (causing all sorts of extremely hard-to-debug problems). More importantly, there shouldn't really be a need to do any of these in an ISR. If the situation arises where you think you need them, you are probably doing too much inside the ISR.
bta
A: 

Most floating point units can generate exceptions divide by zero etc. If your code triggered one of these exceptions from inside an interrupt context the result would be very messy.

Also on x86 some memory/string operations have MMX versions that use the floating point register space as temporary storage so they can do 64bit read/write operations so depending on processor/kernel configuration you might be in for some nasty surprises.

You could end up spending a lot of time debugging this so my advice would be to try and avoid using floating point in kernel code if you possibly can.

Andrew Roca