Working with IDT 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
jmmv

Working with IDT in C

Post by jmmv »

Hi all

My kernel is now able to setup a proper GDT for itself, and I'm now in the point of setting up the IDT for exception management.

I've written this code in 'irq.c', so far:

----- irq.c -----

uint16_t Idt_Data[256 * 4];

struct {
? ? ? ?uint16_t limit;
? ? ? ?uint32_t base;
} __attribute__ ((packed)) Idtr;

void
irq_alloc_exception (unsigned short index, void (*func) (void))
{
? ? ? ?uint16_t *entry = &Idt_Data[index * 4];

? ? ? ?*entry = (uint16_t) ((unsigned long) func & 0xFFFF);
? ? ? ?*(entry + 1) = (uint16_t) DESC_KERNEL_CS;
? ? ? ?*(entry + 2) = (uint16_t) 0x8F00;
? ? ? ?*(entry + 3) = (uint16_t) ((unsigned long) func >> 16);
}

void
irq_handler_0 (void)
{
? ? ? ?/* Provisional handler */
? ? ? ?__asm__ ("iret\n\t");
}

void
irq_init (void)
{
? ? ? ?memset ((void *) &Idt_Data, 0, 256 * 8);

? ? ? ?/* Setup IDTR */
? ? ? ?Idtr.base = (uint32_t) &Idt_Data;
? ? ? ?Idtr.limit = 256 * 8;
? ? ? ?__asm__ ("lidt %0\n\t" : /* no output */ : "m" (Idtr));

? ? ? ?irq_alloc_exception (0, &irq_handler_0);

? ? ? ?return;
}

---------------

BTW, uint16_t is an unsigned short, and memset is a function of my own...

Well, after calling irq_init I try a simple exception, dividing a number by 0, and the result... the kernel crashes.

Where is the problem?

Thanks a lot!
Chris Giese

Re: Working with IDT in C

Post by Chris Giese »

>/* Setup IDTR */
>Idtr.base = (uint32_t) &Idt_Data;
>Idtr.limit = 256 * 8;

What is the base address of the kernel data segment?

If it's not zero, you must add it to Idtr.base,
because this is a linear address.
Post Reply