ISRS没有被调用

时间:2013-07-08 14:58:16

标签: c assembly operating-system kernel

除以零错误不会调用ISR0。
我的内核主要来自boot.s

 void kernel_main()
 {       
 gdt_install();
 idt_install();
 isrs_install();    
 terminal_initialize();
 terminal_writestring("Hello, kernel World!\n");
 int a = 5/0;  // I expect to call ISR0 by system from here
 terminal_writestring("Hello, kernel World!22222"); // And this should not print
 }

IDT.c

 struct idt_entry
 {
 uint16_t base_lo;
 uint16_t sel;
 uint8_t always0;
 uint8_t flags;
 uint16_t base_hi;
 } __attribute__((packed));

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

 struct idt_entry idt[256];
 struct idt_ptr idtp;

 void idt_set_gate(uint8_t num, uint64_t base, uint16_t sel, uint8_t flags)
 {
 idt[num].base_lo = (base & 0xFFFF);
 idt[num].base_hi = (base >> 16) & 0xFFFF ;

 idt[num].sel = sel;
 idt[num].always0 = 0;
 idt[num].flags = flags;
 }

 void idt_install()
 {
 idtp.limit = (sizeof(struct idt_entry) * 256) - 1;
 idtp.base = &idt;
 memset(&idt, 0, sizeof(struct idt_entry) * 256);
 idt_load();
 }

ISRS.c:ISRS的填充结构和ISR的fault_handler

 void isrs_install()
 {
 idt_set_gate(0, (unsigned)isr0, 0x08, 0x8E);
 .....
 idt_set_gate(31, (unsigned)isr31, 0x08, 0x8E);
 }

 void fault_handler(struct regs *r)
 {
 if(r->int_no < 32)
 {
    terminal_writestring(exception_messages[r->int_no]);
    terminal_writestring("Exception. System Halted!\n");
    for(;;);
 }
 }

boot.s包含ISR定义和ISR的全局声明。 boot.s的主要部分,调用kernel_main。

IDT加载并通过lidt安装:

 .global idt_load
 idt_load:
 lidt idtp
 ret

 .global isr0
  .....

 isr0:
 cli
 push $0x0
 push $0x0
 jmp isr_common_stub 


 isr_common_stub ; Common place to handle ISRS

.extern fault_handler

isr_common_stub :
pusha
push %ds
push %es
push %fs
push %gs
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %esp, %eax
push %eax
mov $fault_handler, %eax   ;Not reaching here when exception occurs
call *%eax
pop %eax
pop %gs
pop %fs
pop %es
pop %ds
popa
add $0x8, %esp
iret

2 个答案:

答案 0 :(得分:1)

我之前遇到过类似的问题。在我的情况下(我也猜测你的),编译器将除法除去。通过将volatile关键字与全局变量结合使用,我能够解决这个问题。

volatile int globalZero = 0;

int  myRtn (void) {
    volatile int  error;
    ...
    error = error / globalZero;
}

希望这有帮助。

答案 1 :(得分:0)

问题必须在您的make文件中,如果您在gcc命令中使用-O选项将其删除,它将起作用。