wrong value passed..

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
MoneyCat

wrong value passed..

Post by MoneyCat »

Hello,

I have the following isr:

Code: Select all

_int0:
   cli         ; disable interrupts
   push eax      ; store the current state of cpu
   push ebx      
   push ecx
   push edx
   push ebp
   push edi
   push esi
   push es
   push ds
   push fs
   push gs

   mov eax, 0
   mov gs, eax
   mov fs, eax
   mov eax, 0x10      ; LINEAR_DATA_SEL == 0x10
   mov es, eax
   mov ds, eax

        mov eax, 0
   call IntHandler      ; call the interrupt handler

   pop gs         ; begin to restore the saved cpu state
   pop fs
   pop ds
   pop es
   pop esi
   pop edi
   pop ebp
   pop edx
   pop ecx
   pop ebx
   pop eax
   sti          ; re-enable interrupts
   iretd         ; return 

and my interrupt handler function:

Code: Select all

void IntHandler(unsigned int x)
{
  char* msg[] = {"Divide Error Exception!", "blah", "blah2" }; 
  kprintf("x is: %x\n", x);
  // kprintf("%s\n", msg[x]);
  kprintf("Halting the CPU...");
  asm("cli");
  asm("hlt");
}

And I do a simple: x += x / 0; to trigger the divide by 0 exception in kernel

My handler gets called but the argument that is passed to the handler is 0x10. I tried assigning 0 to eax before calling the function. Yet when I print the argument it is always 0x10... Can anyone please shed light on this ?? 

thanks


Slasher

Re:wrong value passed..

Post by Slasher »

hi, your
mov eax, 0
call IntHandler ; call the interrupt handler

should be a push,as in

push dword 0
call _IntHandler
('_' is appended before C funtion names and C uses the stack to pass arguments to functions not registers)
also add

extern _IntHandler to the top of the asm file so that LD can find the function
Global _xxxx ->export function _xxx in asm to c as xxxx(...)
Extern _xxxx -> import function from C as _xxx in asm (parameters are on stack and you should manage them ie recall how many they are and what size each)
Therx

Re:wrong value passed..

Post by Therx »

push eax ; store the current state of cpu
push ebx
push ecx
push edx
push ebp
push edi
push esi
You could shorten this to:-
pushad

Pete
MoneyCat

Re:wrong value passed..

Post by MoneyCat »

Code: Select all

extern  _IntHandler to the top of the asm file so that LD can find the function
Global  _xxxx ->export function _xxx in asm to c as xxxx(...)
Extern _xxxx -> import function from C as _xxx in asm (parameters are on stack and you should manage them ie recall how many they are and what size each)
I am using elf format so I don't think I'll need those underscore. I tried the push dword 0 and it now works great! Thanks!

Once last question, in my isr is the lable at the start of first exception used internally? If so, then the label can be anything? Or does it mimic the order defined in idt ?

thanks
Slasher

Re:wrong value passed..

Post by Slasher »

I don't understand the question, What table are you talking about? if its the Global xxxx,xxxx,..... and the Extern xxx,xxx..... you mean, then its just info that the linker and other routines in the asm file use to refernce functions in another file (Extern...) and to provide their addresses (Global....) to functions in another file. If you don't want any function to be available outside the asm file, then do not put it in the Global list.
MoneyCat

Re:wrong value passed..

Post by MoneyCat »

Hello,

I mean the:

Code: Select all

_int0:        <=== can be any label name ???
   cli         
   pushad
   ...
   ...
   iretd

_int1:        <=== what actually calls this label? CPU? 
   cli
   pushad
   ...
   ...
   iretd     

Thanks for the feedback!
Therx

Re:wrong value passed..

Post by Therx »

The CPU calls the address in the GDT. If you know exactly where the function starts you don't need the label you could just enter the address, but this would be very inpractical. So as long as you change the value in the IDT then it can be called whatever. How do you initalize your IDT. Is it in a similar style to the OSDs. That's how I do it. For people who don't know this style it basically relies on all the stubs being the same size and then loops filling the IDT incrementing the pointer by a set ammount every time.

Pete
MoneyCat

Re:wrong value passed..

Post by MoneyCat »

Pete,

I was looking at your start.asm from http://therx.sourceforge.net/src/i386/start.asm.html. I couldnt locate your linker script... can you please show me that script you use...

thanks
Post Reply