我试图设置内核的IDT,但是我收到了这个链接错误:
bin/obj/idt.o: In function `setup_idt':
idt.c:(.text+0x9b): undefined reference to `interrupt_handler_1'
错误说没有定义interrupt_handler_1,但它是interrupt_manager.asm中的一个宏:
%macro no_error_code_interrupt_handler 1
global interrupt_handler_%1
interrupt_handler_%1:
cli
push dword 0 ; push 0 as error code
push dword %1 ; push the interrupt number
jmp common_interrupt_handler ; jump to the common handler
%endmacro
这是setyup_idt函数:
extern void interrupt_handler_1();
void setup_idt()
{
// Set the special idt_pointer
idt_pointer.limit = ( sizeof(struct InterruptDescriptorTableEntry) * 256 ) - 1; // Subsract 1 because sizeof doesn't start from 0
idt_pointer.address = (uint32)&idt;
// Clear the whole idt to zeros
memset(&idt, 0, sizeof(struct InterruptDescriptorTableEntry) * 256 );
for(unsigned int i = 0; i < 256; i++)
{
idt_set_gate(i, (uint32)&interrupt_handler_1, 0x8, 0x8E);
}
__asm__ __volatile__("lidt %0": :"m"(idt_pointer));
}
我做错了什么?
额外的问题:是否有一种宏/另一种方式将GDT的i条目自动链接到i中断处理程序,让我试着更好地解释自己:
我想做的是这样的事情:
for(unsigned int i = 0; i < 256; i++)
{
idt_set_gate(i, (uint32)&interrupt_handler_[i], 0x8, 0x8E);
}
其中interrupt_handler [i]将是中断处理程序_ [i]将被nasm宏替换
答案 0 :(得分:2)
您需要在NASM代码中展开宏。宏定义本身不会生成任何代码。它需要在NASM代码中明确使用。
您可以使用%rep
指令重复扩展具有不同参数的宏。像这样:
extern common_interrupt_handler
%macro error_code_interrupt_handler 1
global interrupt_handler_%1
interrupt_handler_%1:
push dword %1 ; push the interrupt number
jmp common_interrupt_handler ; jump to the common handler
%endmacro
%macro no_error_code_interrupt_handler 1
global interrupt_handler_%1
interrupt_handler_%1:
push dword 0 ; push 0 as error code
push dword %1 ; push the interrupt number
jmp common_interrupt_handler ; jump to the common handler
%endmacro
; Some CPU exceptions have error codes, some don't
%assign intnum 0
%rep 8 - intnum
no_error_code_interrupt_handler intnum
%assign intnum intnum + 1
%endrep
error_code_interrupt_handler 8
no_error_code_interrupt_handler 9
%assign intnum 10
%rep 16 - intnum
error_code_interrupt_handler intnum
%assign intnum intnum + 1
%endrep
no_error_code_interrupt_handler 16
error_code_interrupt_handler 17
no_error_code_interrupt_handler 18
no_error_code_interrupt_handler 19
no_error_code_interrupt_handler 20
%assign intnum 21 ; first (currently) unassigned CPU exception
%rep 32 - intnum
no_error_code_interrupt_handler intnum
%assign intnum intnum + 1
%endrep
%assign intnum 32 ; first user defined interrupt
%rep 256 - intnum
no_error_code_interrupt_handler intnum
%assign intnum intnum + 1
%endrep
; define a table of interrupt handlers for C code to use
global interrupt_handler_table
interrupt_handler_table:
%assign intnum 0
%rep 256
dd interrupt_handler_ %+ intnum
%assign intnum intnum + 1
%endrep
上面的代码创建了一个表,其中包含您可以在C代码中使用的所有中断处理程序,如下所示:
extern uint32 interrupt_handler_table[256];
for(unsigned int i = 0; i < 256; i++)
{
idt_set_gate(i, interrupt_handler_table[i], 0x8, 0x8E);
}
请注意,我为那些生成错误代码的CPU异常创建了一个error_code_interrupt_handler
宏。此外,我已从您的代码中删除了不必要的CLI指令。由于您在IDT中使用中断门,因此中断使能标志会自动清除。