Some IDT Code...in C

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
Slasher

Re:Some IDT Code...in C

Post by Slasher »

seems people don't understand the code i posted.
descriptor_reg idt,gdt;
defines the 6 bytes location needed to store info about where you want to put your idt and gdt descriptors.
this is the 6 bytes needed for the LIDT and LGDT instructions.
so basically, idt.base is the base (just like in the intel manual) and idt.limit is the limit(i.e number of descriptors*8 - 1 cause we start counting from 0)
in memory this looks like
limit base
L L L L B B B B B B B B

don't know how to explain any better. if you need help best to post code, can't work in the DARK! :'(
Unexpected

Re:Some IDT Code...in C

Post by Unexpected »

Code Slasher, your code is good... but without pointer is easer to understand... I think ;)

I correct the IDT and handlers. Now my keyboard handler works and it looks like this:
void KBD_Handler()
{
Print("KBD Test");
outportb(0x20, 0x20);
asm("MOV %ebp,%esp");
asm("POP %ebp");
asm("IRET");
}

Is it all correct?
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

Unexpected...PLEASE post your code for me to see!!!
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

I must have did something wrong to Code Slasher's code...so i'd like to see yours because mine still does a exception 8. :-\
Slasher

Re:Some IDT Code...in C

Post by Slasher »

looks fine to me.
Tom, if you are processing an Interrupt you have to acknowledge it to the pic by sending
0x20 to 0x20 and 0xA0 ie
mov al,0x20
out 0x20,al
out 0xa0,al
i hope you placed this in your code?
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

OPPS!

Where do the ints start at? What number in hex?
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

Hey...wait a second...I only set up 31 ints only for exceptions! There's not a IRQ being called!
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

Ok ints start at 0x20...I only have up to 0x31 set up...so that's not the problem.
Slasher

Re:Some IDT Code...in C

Post by Slasher »

have you reprogrammed the PIC?
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

No... :-[ i'll reprogram it now.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Some IDT Code...in C

Post by df »

Unexpected wrote: I correct the IDT and handlers. Now my keyboard handler works and it looks like this:
void KBD_Handler()
{
Print("KBD Test");
outportb(0x20, 0x20);
asm("MOV %ebp,%esp");
asm("POP %ebp");
asm("IRET");
}

Is it all correct?
i find its easier+cleaner to enter ints via an asm stub, rather than inline asm asm stackframe removale/iret

(what happens if you compile without stackframes! your code bombs)
-- Stu --
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

Well...remaping the pic workd and my IDT works except for one thing...I get a excep13 complaning about a too small stack...my stack and GDT is 0xFFFF ( stack ) and GDT is 0x0FFFF...

How do I make my stack bigger?
beyondsociety

Re:Some IDT Code...in C

Post by beyondsociety »

Tom

Remember that your stack in protected mode is still a 16-bit stack and not a 32 bit one. Does this ring a bell.

Code: Select all

mov ax,0x10
mov ss,ax
mov esp,0xFFFF
NASM assumes that, since you are using esp, you are assigning to it a 32-Bit number. If you use a value like 0xFFFF, it will padd it out with zeros to make 0x0000FFFF. In the case of the example above, this means that the stack pointer is initialised very low (for a 32-Bit stack), and would almost certainly be below the segment limit (unless you have a 4Gb stack), which would cause a stack fault.

Hope this helps ;)
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Some IDT Code...in C

Post by Pype.Clicker »

Unexpected wrote: Code Slasher, your code is good... but without pointer is easer to understand... I think ;)

I correct the IDT and handlers. Now my keyboard handler works and it looks like this:
void KBD_Handler()
{
Print("KBD Test");
outportb(0x20, 0x20);
asm("MOV %ebp,%esp");
asm("POP %ebp");
asm("IRET");
}

Is it all correct?
Sorry, but you enfringed golden rule #1 of interrupts programming: SAVE and RESTORE everything that might be modified.
including machine registers (eax, ecx ...) that you absolutely don't protect ... the CPU is likely to crash if some registers can be modified at random times.

So, i know this is about writing a IDT in pure C, but now you have to face the truth: IDT setup is feasible in pure C (if you just do an asm(lidt)), but interrupt handlers MUST be written in ASM... Look at your own code, forcing pops from the stack frame, do you think it still looks like C ?
maybe you could try the "interrupt" modifier for your code (though i dunno if it's 4|\|51 or if it's something Borland&MS added to C...

However, i beleive that a good ASM stub is the better way to go. Look at this code if you need some reference ... that's something that works.

df's code works for exception because he does nothing but halting the processor. If he have tried to return from his exception, he was likely to get another one ...
Berserk

Re:Some IDT Code...in C

Post by Berserk »

Could Somebody PLEASE, PLEASE explain to me, WHAT IS AN IDT! and What does it do??

;D
Post Reply