编写内核但在加载IDT后崩溃

时间:2015-10-22 01:36:34

标签: c kernel system

我正在编写自己的内核,并且正在尝试设置自己的中断描述符表。我正在使用64位架构。我已经尝试了所有我能想到的东西但是每次加载这个idt时我的内核崩溃了。我希望有人提出建议......

#include <sys/defs.h>
#include "print.h"
#include "pic.h"


struct idt_entry_struct
{
   uint16_t base_lo;             // The lower 16 bits of the address to jump to when this interrupt fires.
   uint16_t sel;                 // Kernel segment selector.
   uint8_t  always0;             // This must always be zero.
   uint8_t  flags;               // More flags. See documentation.
   uint16_t base_hi;             // The upper 16 bits of the address to jump to.
   uint32_t base_hi2; 
   uint32_t always02;
} __attribute__((packed));
typedef struct idt_entry_struct idt_entry_t;


void int_handle(void){


    __asm__ __volatile__(
    "push %rax\n\t"
    "push %rcx\n\t"
    "push %rdx\n\t"
    "push %rbx\n\t"
    "push %rsp\n\t"
    "push %rbp\n\t"
    "push %rsi\n\t"
    "push %rdi\n\t"
    "push %r8\n\t"
    "push %r9\n\t"
    "push %r10\n\t"
    "push %r11\n\t"
    "push %r12\n\t"
    "push %r13\n\t"
    "push %r14\n\t"
    "push %r15\n\t"
    );

    //Interrupt Code goes here


    __asm__ __volatile__(
    "pop %r15\n\t"
    "pop %r14\n\t"
    "pop %r13\n\t"
    "pop %r12\n\t"
    "pop %r11\n\t"
    "pop %r10\n\t"
    "pop %r9\n\t"
    "pop %r8\n\t"
    "pop %rdi\n\t"
    "pop %rsi\n\t"
    "pop %rsp\n\t"
    "pop %rbx\n\t"
    "pop %rdx\n\t"
    "pop %rcx\n\t"
    "pop %rax\n\t"


    "iretq\n\t"
    );
}

static struct idt_entry_struct idts [256];


struct idtr_t 
{
        uint16_t size;
        uint64_t addr;
}__attribute__((packed));


static struct idtr_t idtr = 
{
        (uint16_t)sizeof(struct idt_entry_struct) * 256-1,
        (uint64_t)idts,
};

void _x86_64_asm_lidt(struct idtr_t* idtr);


void init_idt()
{

        idts[0].base_lo = (uint16_t)((uint64_t) &int_handle & 0xFFFFF);
        idts[0].sel =0x8;
        idts[0].always0 = 0;
        idts[0].flags = 0x8E;
        idts[0].base_hi = (uint16_t) (((uint64_t)&int_handle >> 16) & 0x0FFFF);
        idts[0].base_hi2 =  (uint32_t)((uint64_t)&int_handle>> 16);
        idts[0].always02 = 0;

    _x86_64_asm_lidt(&idtr);

}

当我单步执行调试器时,我能够进入int_handle中断处理程序,但是一旦退出,内核就会崩溃。我和Qemu一起跑步。有什么想法吗?

0 个答案:

没有答案