中断教程:IDT、ISR 与中断启用
文章目录
| 难度级别 |
|---|
初级 |
每个需要(高效地)与硬件交互的操作系统都必须使用中断。例如,你可以使用整个 AP 来轮询鼠标,或者你可以改用鼠标 IRQ,从而节省更多的 CPU 时间和大量的电力负载。因此,每个合理的操作系统都会使用中断。
前置条件
在创建 IDT 之前,你需要创建一个 GDT,正确加载它,并相应地配置段寄存器。
中断描述符表
条目
为了使用中断,你需要一个 IDT。
当中断触发时,CPU 使用向量作为 IDT 的索引。CPU 读取 IDT 的条目,以确定在调用 ISR 之前要做什么,以及处理程序的地址是什么。
这是一个单个(32 位)IDT 条目的结构:
typedef struct { uint16_t isr_low; // The lower 16 bits of the ISR's address uint16_t kernel_cs; // The GDT segment selector that the CPU will load into CS before calling the ISR uint8_t reserved; // Set to zero uint8_t attributes; // Type and attributes; see the IDT page uint16_t isr_high; // The higher 16 bits of the ISR's address } __attribute__((packed)) idt_entry_t;
而这是一个单个(64 位)IDT 条目的结构:
typedef struct { uint16_t isr_low; // The lower 16 bits of the ISR's address uint16_t kernel_cs; // The GDT segment selector that the CPU will load into CS before calling the ISR uint8_t ist; // The IST in the TSS that the CPU will load into RSP; set to zero for now uint8_t attributes; // Type and attributes; see the IDT page uint16_t isr_mid; // The higher 16 bits of the lower 32 bits of the ISR's address uint32_t isr_high; // The higher 32 bits of the ISR's address uint32_t reserved; // Set to zero } __attribute__((packed)) idt_entry_t;
表
要创建 IDT,只需创建一个 256 个描述符的数组:
__attribute__((aligned(0x10))) static idt_entry_t idt[256]; // Create an array of IDT entries; aligned for performance
你还需要一个特殊的 IDTR 结构,它看起来像:
typedef struct { uint16_t limit; uint32_t base; } __attribute__((packed)) idtr_t;
对于 32 位 IDT,或者像:
typedef struct { uint16_t limit; uint64_t base; } __attribute__((packed)) idtr_t;
对于 64 位 IDT。
别忘了定义一个 IDTR:
static idtr_t idtr;
ISR
在 C 源文件中,定义一个通用异常处理程序:
__attribute__((noreturn)) void exception_handler(void); void exception_handler() { __asm__ volatile ("cli; hlt"); // Completely hangs the computer }
这将充当你的主异常处理程序。当你收到 CPU 异常时,这就是你将调用的处理程序。
现在,在汇编文件(具体来说是 nasm 汇编器)中,定义这两个宏:
%macro isr_err_stub 1 isr_stub_%+%1: call exception_handler iret %endmacro ; if writing for 64-bit, use iretq instead %macro isr_no_err_stub 1 isr_stub_%+%1: call exception_handler iret %endmacro
然后,使用这些宏来定义你的 32 个异常处理程序:
extern exception_handler isr_no_err_stub 0 isr_no_err_stub 1 isr_no_err_stub 2 isr_no_err_stub 3 isr_no_err_stub 4 isr_no_err_stub 5 isr_no_err_stub 6 isr_no_err_stub 7 isr_err_stub 8 isr_no_err_stub 9 isr_err_stub 10 isr_err_stub 11 isr_err_stub 12 isr_err_stub 13 isr_err_stub 14 isr_no_err_stub 15 isr_no_err_stub 16 isr_err_stub 17 isr_no_err_stub 18 isr_no_err_stub 19 isr_no_err_stub 20 isr_no_err_stub 21 isr_no_err_stub 22 isr_no_err_stub 23 isr_no_err_stub 24 isr_no_err_stub 25 isr_no_err_stub 26 isr_no_err_stub 27 isr_no_err_stub 28 isr_no_err_stub 29 isr_err_stub 30 isr_no_err_stub 31
最后,在汇编中,定义一个“存根表”。(这用于防止过度的代码重用,与实际功能无关。)
使用 NASM 宏:
global isr_stub_table isr_stub_table: %assign i 0 %rep 32 dd isr_stub_%+i ; use DQ instead if targeting 64-bit %assign i i+1 %endrep
汇编
最后,我们可以汇编 IDT: 我们需要
- 1. 用正确的值分配 IDT 条目,
- 2. 重新加载 IDTR 寄存器,
- 3. 设置中断标志。
要定义这些条目,使用一个辅助函数是合适的。该辅助函数看起来像:
void idt_set_descriptor(uint8_t vector, void* isr, uint8_t flags); void idt_set_descriptor(uint8_t vector, void* isr, uint8_t flags) { idt_entry_t* descriptor = &idt[vector]; descriptor->isr_low = (uint32_t)isr & 0xFFFF; descriptor->kernel_cs = 0x08; // this value can be whatever offset your kernel code selector is in your GDT descriptor->attributes = flags; descriptor->isr_high = (uint32_t)isr >> 16; descriptor->reserved = 0; }
对于 32 位 IDT,或者像:
void idt_set_descriptor(uint8_t vector, void* isr, uint8_t flags); void idt_set_descriptor(uint8_t vector, void* isr, uint8_t flags) { idt_entry_t* descriptor = &idt[vector]; descriptor->isr_low = (uint64_t)isr & 0xFFFF; descriptor->kernel_cs = GDT_OFFSET_KERNEL_CODE; descriptor->ist = 0; descriptor->attributes = flags; descriptor->isr_mid = ((uint64_t)isr >> 16) & 0xFFFF; descriptor->isr_high = ((uint64_t)isr >> 32) & 0xFFFFFFFF; descriptor->reserved = 0; }
对于 64 位 IDT。
最后,为了最终设置这些条目,函数看起来像这样:
static bool vectors[IDT_MAX_DESCRIPTORS]; extern void* isr_stub_table[]; void idt_init(void); void idt_init() { idtr.base = (uintptr_t)&idt[0]; idtr.limit = (uint16_t)sizeof(idt_entry_t) * IDT_MAX_DESCRIPTORS - 1; for (uint8_t vector = 0; vector < 32; vector++) { idt_set_descriptor(vector, isr_stub_table[vector], 0x8E); vectors[vector] = true; } __asm__ volatile ("lidt %0" : : "m"(idtr)); // load the new IDT __asm__ volatile ("sti"); // set the interrupt flag }
(IDT_MAX_DESCRIPTORS 是你的 IDT 中的条目数——或最后一个条目的索引 + 1)
恭喜!你已成功定义了你的 IDT,加载了它,并启用了中断。
接下来做什么
完成本教程后,要充分利用中断的强大功能,你还有很多事情要做。
你可以:
另请参阅
线程
参考
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Interrupts Tutorial;离线修订号:29118。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论