Demo 8 problem

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator:Moderators

Post Reply
vijaymohan
Posts:9
Joined:Fri Sep 18, 2009 6:23 am
Demo 8 problem

Post by vijaymohan » Mon Sep 21, 2009 7:13 pm

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/

User avatar
Mike
Site Admin
Posts:465
Joined:Sat Oct 20, 2007 7:58 pm
Contact:

Re: Demo 8 problem

Post by Mike » Tue Sep 22, 2009 4:48 am

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.
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

vijaymohan
Posts:9
Joined:Fri Sep 18, 2009 6:23 am

Re: Demo 8 problem

Post by vijaymohan » Tue Sep 22, 2009 6:19 am

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

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Re: Demo 8 problem

Post by Andyhhp » Tue Sep 22, 2009 2:53 pm

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
Image

Post Reply