在保护模式下设置中断 (x86)

Setting up interrupts in protected mode (x86)

保护模式设置中断的过程是怎样的?

This link 说应该:

  • Make space for the interrupt descriptor table
  • Tell the CPU where that space is (see GDT Tutorial: lidt works the very same way as lgdt)
  • Tell the PIC that you no longer want to use the BIOS defaults (see Programming the PIC chips)
  • Write a couple of ISR handlers (see Interrupt Service Routines) for both IRQs and exceptions
  • Put the addresses of the ISR handlers in the appropriate descriptors
  • Enable all supported interrupts in the IRQ mask (of the PIC)

第三步对我来说毫无意义(我看了this link 但是没有任何关于告诉PIC的任何事情)所以我忽略它并完成了接下来的两个步骤,走到最后一步时,又是一头雾水。然而,根据我对中断的理解,我不理解的两个步骤都与 PIC 控制器的硬件中断有关,不应该影响 PIT 在 IRQ 0 上引发的中断。因此我也忽略了这一步。

当我 运行 我的代码编译得很好,甚至 运行 在虚拟机中,但中断似乎只触发一次。然后我意识到我没有向 PIC 发送 EOI,以防止它引发更多中断。但是,在 iret 指令之前添加 mov al, 0x20out 0x20, al 会使虚拟机崩溃。

这是我的 IDT:

; idt
idt_start :

    dw 0x00         ; The interrupt handler is located at absolute address 0x00
    dw CODE_SEG     ; CODE_SEG points to the GDT entry for code
    db 0x0          ; The unused byte
    db 0b11101001   ; 1110 Defines a 32 bit Interrupt gate, 0 is mandatory, privilege level = 0 (0b00), the last bit is one so that the CPU knows that the interrupt will be used
    dw 0x00         ; The higher part of the offset (0x00) is 0x00

idt_end:

idt_descriptor :
    dw idt_end - idt_start - 1 ; Size of our idt, always one less than the actual size
    dd idt_start ; Start address of our idt

这是我的中断处理程序(位于内存中的绝对位置 0x00):

ISR_0:
    push eax
    add [0x300], byte 
    mov al, 0x20
    out 0x20, al
    pop eax
    iret    
    times 512-($-$$) db 0

这是我用来进入保护模式并将 GDT 和 IDT 加载到内存中的代码:

[bits 16]

switch_to_pm:

    cli
    lgdt [gdt_descriptor]
    lidt [idt_descriptor]
    mov eax, cr0
    or eax, 1
    mov cr0,eax
    jmp CODE_SEG:init_pm

[bits 32]

init_pm :

    mov ax, DATA_SEG
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ebp, 0x90000
    mov esp, ebp
    sti
    call BEGIN_PM

我的主要功能(检查 0x300 的值)如下:

void main() {
    char iii[15];
    int * aa = (int *)0x300;
    for (;;)
    {
        setCursor(0, 0);
        print(itoab(*aa, iii));
    }
}

顺便说一句,我已经使用内存转储验证了所有内容都加载到正确的地址并且所有内容都在预期的位置。例如,0x300 是内存的空闲部分,仅用于简化我的代码。

让我们看看一些相对较小的内核,即 Linux 0.01 是如何做到的!

  • Make space for the interrupt descriptor table

这完成了两次(好吧,技术上只有一次):首先,引导加载程序(路径为 /boot/boot.s)初始化 IDTR,因此 CPU 在跳入 Protected 时很高兴模式。 IDTR内容如下:

idt_48:
    .word   0            | idt limit=0
    .word   0,0        | idt base=0L

IDTR 是这样加载的:

lidt     idt_48     | load idt with 0,0

现在,可以进行跳跃了。
请注意,这里没有 IDT。它只是一个虚拟对象,因此内核中的某处没有发生错误。

之后,真正的IDT被初始化(路径为/boot/head.s)。 space 是这样分配的:

_idt:   .fill 256,8,0       # idt is uninitialized
  • Tell the CPU where that space is (see GDT Tutorial: lidt works the very same way as lgdt)

lidt 需要一个包含 IDTR 内容的线性地址。该内容如下所示:

idt_descr:
    .word 256*8-1       # idt contains 256 entries
    .long _idt

IDTR初始化如下:

lidt idt_descr
  • Tell the PIC that you no longer want to use the BIOS defaults (see Programming the PIC chips)

正如@RossRidge 在对您的问题的评论中提到的,这意味着重新映射 IRQ 中断向量 (IV)。
由于 PIC IV 与 Intel x86 异常地址重叠,我们必须重新映射其中之一。异常地址是hard-wired,所以我们需要重新映射PIC向量。
另请参阅 Linus 相应代码上方的评论:

| well, that went ok, I hope. Now we have to reprogram the interrupts :-(
| we put them right after the intel-reserved hardware interrupts, at
| int 0x20-0x2F. There they won't mess up anything. Sadly IBM really
| messed this up with the original PC, and they haven't been able to
| rectify it afterwards. Thus the bios puts interrupts at 0x08-0x0f,
| which is used for the internal hardware interrupts as well. We just
| have to reprogram the 8259's, and it isn't fun.

现在,这是真正的代码。中间的jmp是用来同步CPU和PIC的,所以CPU不会发送PIC还接收不到的数据。这类似于写入内存时的等待状态:当 CPU 比 memory/memory 仲裁器快时,它需要等待一段时间才能下次访问内存。

mov al,#0x11        | initialization sequence
out #0x20,al        | send it to 8259A-1
.word   0x00eb,0x00eb       | jmp $+2, jmp $+2
out #0xA0,al        | and to 8259A-2
.word   0x00eb,0x00eb
mov al,#0x20        | start of hardware int's (0x20)
out #0x21,al
.word   0x00eb,0x00eb
mov al,#0x28        | start of hardware int's 2 (0x28)
out #0xA1,al
.word   0x00eb,0x00eb
mov al,#0x04        | 8259-1 is master
out #0x21,al
.word   0x00eb,0x00eb
mov al,#0x02        | 8259-2 is slave
out #0xA1,al
.word   0x00eb,0x00eb
mov al,#0x01        | 8086 mode for both
out #0x21,al
.word   0x00eb,0x00eb
out #0xA1,al
.word   0x00eb,0x00eb
mov al,#0xFF        | mask off all interrupts for now
out #0x21,al
.word   0x00eb,0x00eb
out #0xA1,al
  • Write a couple of ISR handlers (see Interrupt Service Routines) for both IRQs and exceptions

对于异常,您可以在 /kernel/traps.c/kernel/asm.s 中找到处理程序代码。
一些异常在跳转到处理程序之前将错误代码压入堆栈,您必须将其弹出,否则 iret 指令将失败。页面错误还会将相应的虚拟地址另外写入cr2
IRQ 处理程序分布在整个系统中。 -.- 定时器和磁盘中断处理程序在/kernel/system_call.s,键盘中断处理程序在/kernel/keyboard.s,例如

  • Put the addresses of the ISR handlers in the appropriate descriptors

异常的初始化在 trap_init 函数的 /kernel/traps.c 中完成:

void trap_init(void)
{
    int i;

    set_trap_gate(0,&divide_error);
    set_trap_gate(1,&debug);
    set_trap_gate(2,&nmi);
    set_system_gate(3,&int3);   /* int3-5 can be called from all */
    set_system_gate(4,&overflow);
    set_system_gate(5,&bounds);
    set_trap_gate(6,&invalid_op);
    set_trap_gate(7,&device_not_available);
    set_trap_gate(8,&double_fault);
    set_trap_gate(9,&coprocessor_segment_overrun);
    set_trap_gate(10,&invalid_TSS);
    set_trap_gate(11,&segment_not_present);
    set_trap_gate(12,&stack_segment);
    set_trap_gate(13,&general_protection);
    set_trap_gate(14,&page_fault);
    set_trap_gate(15,&reserved);
    set_trap_gate(16,&coprocessor_error);
    for (i=17;i<32;i++)
        set_trap_gate(i,&reserved);
/*  __asm__("movl [=17=]x3ff000,%%eax\n\t"
        "movl %%eax,%%db0\n\t"
        "movl [=17=]x000d0303,%%eax\n\t"
        "movl %%eax,%%db7"
        :::"ax");*/
}

IRQ 处理程序条目初始化再次分布在多个文件中。例如,来自 /kernel/sched.csched_init 初始化定时器中断处理程序的地址。

  • Enable all supported interrupts in the IRQ mask (of the PIC)

这是在 /init/main.cmain 函数中使用宏 sti 完成的。在/asm/system.h中定义如下:

#define sti() __asm__ ("sti"::)