Page 1 of 1

Demo 8 problem

Posted: Mon Sep 21, 2009 7:13 pm
by vijaymohan
i have written my pit irq handler as follows (using gcc and nasm to compile)

//! pit timer interrupt handler
void i86_pit_irq () {

asm ("add $12, %esp");
asm ("pusha");

//! increment tick count
_pit_ticks++;
k_printf("pit irq handler");
//! tell hal we are done
interruptdone(0);

asm ("popa");
asm ("iret");
}


the problem is handler is getting called and the string in k_printf is getting printed but upon calling iret invalid opcode exception is getting triggered.
Image

am i missing something? i am stuck at this for the whole day.

code can be browsed at
http://code.google.com/p/zygote/source/ ... svn/trunk/

Re: Demo 8 problem

Posted: Tue Sep 22, 2009 4:48 am
by Mike
Hello,

It is important to note that the code that handles the stack is Microsoft Visual C++ specific. The add esp, 12 is to undo an operation that the normal Visual C++ eprologue code adds at the beginning of _cdecl functions.

To get around this in another compiler, there are three methods:

-Using assembly language for the IRQ handlers, and link them to your C code.
-Using a GCC-specific extension to allow flat routines (without any eprologue/prologue code, simular to Visual C++'s _declspec (naked))
-Disassembling the interrupt handler to adjust your stack changing code to suite your needs based upon the eprologue code added by GCC. i.e., if the eprologue code adds elements to the stack, you must remove them before using iret/iretd. Else, it will fail do to a now corrupt stack. (This is what the series currently does when with add esp, 12 when building with Visual C++.)
-I suppose you can also guess - changing the add esp, 12 to different values. Might be easier to disassemble though. Values are usually in multiples of 4: 4, 8, 12, 16.

Re: Demo 8 problem

Posted: Tue Sep 22, 2009 6:19 am
by vijaymohan
Thanks for the reply mike. i tried the changing add esp, 12 to different values. 28 worked for me. will there be any problems i will run into with this solution ?

Image

Re: Demo 8 problem

Posted: Tue Sep 22, 2009 2:53 pm
by Andyhhp
the specific number you have to add will depend on compiler optimizations. IMO, you should substitue what you currently have for a more error-resistant method.

I suggest reading http://www.brokenthorn.com/forums/viewt ... f=12&t=131

Enjoy