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.
_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
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
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)
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 ?
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.
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.