Page 1 of 1

IDT with C

Posted: Sat Sep 10, 2005 1:27 pm
by OSMAN
Hello.

Please tell me very well how can I make Interrupt Descriptor Table with C language (and then load it).

Re:IDT with C

Posted: Sat Sep 10, 2005 1:49 pm
by QuiTeVexat
You do something like this:

Code: Select all

struct idt_entry
{
   uint16 addr_l;
   uint16 sel;
   uint8  nulls;
   uint8  attrib;
   uint16 addr_h;
};

struct idt_entry idt[49];
And then you write a couple of functions to fill the table in with the right values. It's hard to make this one generated at compile/link-time because of the division between the low parts of the address and the high parts, and linkers won't usually do bitmasks and shifts for you.

As for loading, it works like LGDT, you need a pointer structure, so set up a temporary location for that, load it up (like you would for a GDT pointer), and use some inline assembly like so:

Code: Select all

asm("lidt (%0)" ... <fill in the rest here>

Re:IDT with C

Posted: Sat Sep 10, 2005 3:35 pm
by GLneo

Re:IDT with C

Posted: Sat Sep 10, 2005 11:36 pm
by AR
QuiTeVexat wrote: You do something like this:

Code: Select all

struct idt_entry
{
   uint16 addr_l;
   uint16 sel;
   uint8  nulls;
   uint8  attrib;
   uint16 addr_h;
};
It is important to not forget __attribute__((__packed__)):

Code: Select all

struct idt_entry
{
   uint16 addr_l;
   uint16 sel;
   uint8  nulls;
   uint8  attrib;
   uint16 addr_h;
} __attribute__((packed));
Otherwise each one of those variables will be uint32 regardless of the type.

Re:IDT with C

Posted: Sun Sep 11, 2005 5:28 am
by JoeKayzA
Isn't it also neccessary to add

Code: Select all

struct idt_entry idt[49] __attribute__((aligned(4)));
?

AFAIK, the descriptor structures need to be aligned on a 32-bit border.

cheers Joe

Re:IDT with C

Posted: Sun Sep 11, 2005 5:34 am
by Pype.Clicker
the compiler is much likely to align *anything* on a 32bits boundary, btw. it even happen to align strings of characters on 32bits boundaries some days.