中断描述符表门

时间:2015-05-24 01:07:03

标签: assembly x86 operating-system interrupt-handling protected-mode

我已编写此代码来创建示例IDT并将其加载到正确的寄存器中。我已经检查了英特尔系统编程指南以获得正确的结构,但我无法使中断工作。在Bochs中运行内核代码并触发中断时(使用__asm__ ("int $32");我得到一个日志,上面写着:

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)

当然这会导致CPU复位,因为中断未处理。要加载IDT,我使用以下内容:

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

这就是这样的:

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);
}

数据结构:

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;

示例中断例程:

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

我已经检查了IDT寄存器是否在quemu中加载了ptr到IDT,它是。我在GRUB启动后立即加载IDT(保护模式已设置且GDT选择器跨越整个RAM)。我认为我错误地注册了中断例程,但我可以在我的代码中指出任何错误。

1 个答案:

答案 0 :(得分:2)

日志清楚地显示not present。您忘记在IDT条目中设置当前位。您应该使用type_attr的{​​{1}}。