Ufo
Ufo

Reputation: 225

Interrupt Descriptor Table Gate

I've written this code to create a sample IDT and load it in to the proper register. I've checked Intel System programming guides for proper structures, and yet I can't get interrupts working. While running kernel code in Bochs, and triggering interrupt (using __asm__ ("int $32"); I get a log that says:

00135687199e[CPU0  ] interrupt(): gate not present
00135687199e[CPU0  ] interrupt(): gate descriptor is not valid sys seg (vector=0x0b)
00135687199e[CPU0  ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)

Of course this causes a CPU reset, as interrupt is unhandled. To load IDT I use following:

extern void _lidt(_IDT_PTR* idtPtr); //C code side
//Assembly
.global _lidt
_lidt:
push %ebp
mov %esp,%ebp
mov 8(%esp), %eax
lidt (%eax)
leave
ret

This is called like this:

static struct __InteruptDescriptorTableEntry InteruptDescriptorTable[NUM_IDT_ENTRIES];


void zeroIDT()
{   
    unsigned i;
    for(i=0;i<NUM_IDT_ENTRIES-1;++i)
    {
        IDTEntry nullIDTEntry = fillIDTEntry(0,0,0);
        registerInterupt(nullIDTEntry, i);
    }
}

void registerInterupt(const IDTEntry entry, const unsigned intNo)
{
    if(intNo < NUM_IDT_ENTRIES)
        InteruptDescriptorTable[intNo] = entry;
    else
    {
        __asm__("mov $0xDEADC0DE, %eax");
        __asm__("hlt");
    }
}

#define LOW_FUN_ADDR(fun) ( (uint32_t)fun & 0xFFFF )
#define UP_FUN_ADDR(fun) ( (uint32_t)fun >> 16) & 0xFFFF

IDTEntry fillIDTEntry(uint32_t intHandler,
                      uint16_t selector,   
                      uint8_t type_attr)

{   IDTEntry newEntry;
    newEntry.offset_low = LOW_FUN_ADDR(intHandler); 
    newEntry.selector = selector; 
    newEntry.zero = 0;     
    newEntry.type_attr = type_attr;
    newEntry.offset_up = UP_FUN_ADDR(intHandler);
    return newEntry;
}

extern void _lidt(_IDT_PTR* idtPtr);
void loadIDT()
{
    zeroIDT();
    _IDT_PTR idtPtr;
    idtPtr.idtSize = sizeof(struct __InteruptDescriptorTableEntry)*256 - 1;
    idtPtr.idtBaseAddr = (uint32_t) &InteruptDescriptorTable;

    IDTEntry printOnScreenInt = fillIDTEntry((uint32_t)interupt_pritnOnScreen, 0x18, 0xe);
    registerInterupt(printOnScreenInt, 32);
    _lidt(&idtPtr);
}

Data structures:

struct __InteruptDescriptorTableEntry
{
   uint16_t offset_low;
   uint16_t selector; 
   uint8_t zero;      
   uint8_t type_attr; 
   uint16_t offset_up; 
} __attribute__((packed));
typedef struct __InteruptDescriptorTableEntry IDTEntry;

struct _ITD_PTR
{
    uint16_t idtSize;
    uint32_t idtBaseAddr;
} __attribute__((packed));
typedef struct _ITD_PTR _IDT_PTR;

And sample Interrupt routine:

.global interupt_pritnOnScreen
interupt_pritnOnScreen:
   mov $0xf00ff00f, %eax
   hlt
iret

I've checked if IDT register is loaded with ptr to IDT in quemu, and it is. I am loading IDT right after being booted up by GRUB (protected mode is set and GDT selectors span across the whole RAM). I think I am incorrectly register Interrupt routines, but I can point any mistake in my code.

Upvotes: 0

Views: 755

Answers (1)

Jester
Jester

Reputation: 58762

The log clearly says not present. You forgot to set the present bit in the IDT entry. You should use type_attr of 0x8E.

Upvotes: 2

Related Questions