除以零错误不会调用 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