键盘 IRQ 仅触发一次

Keyboard IRQ fires only once

我正在开发一个玩具 unix 克隆,我正在尝试正确连接我的中断。我 运行 遇到一个问题,即即使在我正确确认后,我的键盘 IRQ (IRQ 1) 也只触发一次,等等。我也启用了 PIT 中断,以仔细检查我的 ACK 是否正常发送到 PIC,这似乎工作正常。 (触发多次)

interrup.s 的一个问题是,我在堆栈上传递结构 register_t(按值),而编译器在 C 中断 returns 之后将其丢弃处理程序。令人惊讶的是,唯一被丢弃的值是堆栈顶部的值(在这种情况下是数据段寄存器),我已经通过前后打印堆栈来验证堆栈中的其余值看起来没问题调用中断处理程序。我已经添加了一个临时解决方法来解决这个问题,但我稍后会清理它。

我还通过多次触发 int 验证了软件中断工作正常。

如有任何建议,我们将不胜感激!这是代码:

interrupt.s

.macro ISR_NOERRCODE int_no # A macro for ISRs that don't push an error code
.global isr\int_no
isr\int_no:
  cli
  push [=10=]
  push $\int_no
  jmp isr_irq_common_stub
.endm

.macro ISR_ERRORCODE int_no # A macro for ISRs that do push an error code
.global isr\int_no
isr\int_no:
  cli
  push $\int_no
  jmp isr_irq_common_stub
.endm

.macro IRQ irq_no, isr_map # A macro for IRQs from the PIC
.global irq\irq_no
irq\irq_no:
  xchgw %bx, %bx
  cli
  push [=10=] # Error code
  push $\isr_map # Interrupt number
  jmp isr_irq_common_stub
.endm

ISR_NOERRCODE 0
ISR_NOERRCODE 1
ISR_NOERRCODE 2
ISR_NOERRCODE 3
ISR_NOERRCODE 4
ISR_NOERRCODE 5
ISR_NOERRCODE 6
ISR_NOERRCODE 7
ISR_ERRORCODE 8  # ISR 8 pushes error code onto stack
ISR_NOERRCODE 9
ISR_ERRORCODE 10 # ISR 10 - 14 push error codes onto stack
ISR_ERRORCODE 11
ISR_ERRORCODE 12
ISR_ERRORCODE 13
ISR_ERRORCODE 14
ISR_NOERRCODE 15
ISR_NOERRCODE 16
ISR_ERRORCODE 17
ISR_NOERRCODE 18
ISR_NOERRCODE 19
ISR_NOERRCODE 20
ISR_NOERRCODE 21
ISR_NOERRCODE 22
ISR_NOERRCODE 23
ISR_NOERRCODE 24
ISR_NOERRCODE 25
ISR_NOERRCODE 26
ISR_NOERRCODE 27
ISR_NOERRCODE 28
ISR_NOERRCODE 29
ISR_ERRORCODE 30
ISR_NOERRCODE 31
IRQ 0, 32
IRQ 1, 33
IRQ 2, 34
IRQ 3, 35
IRQ 4, 36
IRQ 5, 37
IRQ 6, 38
IRQ 7, 39
IRQ 8, 40
IRQ 9, 41
IRQ 10, 42
IRQ 11, 43
IRQ 12, 44
IRQ 13, 45
IRQ 14, 46
IRQ 15, 47

# This is in isr.c
.extern isr_irq_handler

# This is our common isr stub. It saves the processor state, sets up for kernel
# mode segments, calls the C-level fault handler, and finally restores the stack
# frame
isr_irq_common_stub:

    pusha                    # Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
    mov %ds, %ax             # Lower 16-bits of eax = ds.
    push %eax                # save the data segment descriptor

    mov [=10=]x10, %ax           # load the kernel data segment descriptor
    mov %ax, %ds             # Right now, we dont really have to do this
    mov %ax, %es             # but after we enter the user mode, the segment
    mov %ax, %fs             # registers will be different (0x18? and 0x20?)
    mov %ax, %gs

    call isr_irq_handler

    # This does not work because the structure value we passed earlier
    # is being messed up by the compiler. It does not preserve the previous eax
    # we pushed on to the stack.
    pop %eax
    mov [=10=]x10, %ax           # reload the original data segment descriptor
    mov %ax, %ds
    mov %ax, %es
    mov %ax, %fs
    mov %ax, %gs

    popa                     # Pops edi,esi,ebp...
    add , %esp             # Cleans up the pushed error code and pushed ISR number
    sti
    iret                     # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP

isr.h

#ifndef __isr_h
#define __isr_h

#include <stdint.h>

struct Registers {
  uint32_t ds;
  uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax;
  uint32_t int_no, err_code;
  uint32_t eip, cs, eflags, useresp, ss;
} __attribute__((packed));

typedef struct Registers register_t;
typedef void (*isr_t)(registers_t);

void register_interrupt_handler(uint8_t n, isr_t handler);
void isr_irq_handler(register_t regs);

#endif

isr.c

#include <kernel/isr.h>
#include <kernel/pic.h>
#include <stdio.h>
#include <log.h>
#include <kernel/tty.h>

isr_t interrupt_handlers[256];

void register_interrupt_handler(uint8_t n, isr_t handler)
{
    interrupt_handlers[n] = handler;
}

void isr_irq_handler(register_t regs)
{
  printf("Received ISR/IRQ: %d\n", regs.int_no);

  if (interrupt_handlers[regs.int_no]) {
    interrupt_handlers[regs.int_no]();
  }

  if (regs.int_no >= 32 && regs.int_no <= 47) {
    pic_send_eoi(regs.int_no - 32);
  }
  return;
}

pic.c

#include <kernel/pic.h>
#include <asm.h>

#define PIC1        0x20        /* IO base address for master PIC */
#define PIC2        0xA0        /* IO base address for slave PIC */
#define PIC1_COMMAND    PIC1
#define PIC1_DATA   (PIC1+1)
#define PIC2_COMMAND    PIC2
#define PIC2_DATA   (PIC2+1)
#define PIC_EOI     0x20        /* End-of-interrupt command code */

#define ICW1_ICW4      0x01 /* ICW4 (not) needed */
#define ICW1_SINGLE    0x02 /* Single (cascade) mode */
#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */
#define ICW1_LEVEL     0x08 /* Level triggered (edge) mode */
#define ICW1_INIT      0x10 /* Initialization - required! */

#define ICW4_8086       0x01 /* 8086/88 (MCS-80/85) mode */
#define ICW4_AUTO       0x02 /* Auto (normal) EOI */
#define ICW4_BUF_SLAVE  0x08 /* Buffered mode/slave */
#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
#define ICW4_SFNM       0x10 /* Special fully nested (not) */
#define PIC_READ_IRR                0x0a    /* OCW3 irq ready next CMD read */
#define PIC_READ_ISR                0x0b    /* OCW3 irq service next CMD read */

#define PIC1_OFFSET 0x20
#define PIC2_OFFSET 0x28

static void pic_mask(int pic_num, uint16_t mask);
static uint16_t pic_get_irq_reg(int ocw3);
static uint16_t pic_get_irr(void);
static uint16_t pic_get_isr(void);

void setup_remap_pics()
{
  uint8_t a1, a2;

  // Save existing masks
  a1 = inb(PIC1_DATA);
  a2 = inb(PIC2_DATA);

  outb(PIC1_COMMAND, ICW1_INIT);
  io_wait();
  outb(PIC2_COMMAND, ICW1_INIT);
  io_wait();
  outb(PIC1_DATA, PIC1_OFFSET); // We're remapping the PICs interrupt codes from (0x07-0x7F) to (offset, offset + 8)
  io_wait();
  outb(PIC2_DATA, PIC2_OFFSET);
  io_wait();
  outb(PIC1_DATA, 4); // Tell the master PIC that there is a slave PIC at IRQ2 (00000100)
  io_wait();
  outb(PIC2_DATA, 2); // Tell the slave pic it's cascade identity (00000010)
  io_wait();

  outb(PIC1_DATA, ICW4_8086);
  io_wait();
  outb(PIC2_DATA, ICW4_8086);
  io_wait();

  // Restore saved masks
  outb(PIC1_DATA, a1);
  outb(PIC2_DATA, a2);

  enable_interrupts();

  // Mask everything except the keyboard, timer
  pic_mask(1, 0xFD);
  pic_mask(2, 0xFF);
}

static void pic_mask(int pic_num, uint16_t mask) {
    uint16_t port = (pic_num == 1) ? PIC1_DATA : PIC2_DATA;
    outb(port, mask);
}

// MARK :- Helpers
void pic_send_eoi(uint8_t irq)
{
  if (irq >= 8) {
    outb(PIC2_COMMAND, PIC_EOI);
  }

  printf("Sending EOI for IRQ: %d, EOI: %x, to CMD: %x\n", irq, PIC_EOI, PIC1_COMMAND);

  // Always signal PIC1 that an interrupt has been handled
  // because it's the PIC that forwards PIC2's irqs as well.
  outb(PIC1_COMMAND, PIC_EOI);
}

static uint16_t pic_get_irq_reg(int ocw3)
{
    /* OCW3 to PIC CMD to get the register values.  PIC2 is chained, and
     * represents IRQs 8-15.  PIC1 is IRQs 0-7, with 2 being the chain */
    outb(PIC1_COMMAND, ocw3);
    outb(PIC2_COMMAND, ocw3);
    return (inb(PIC2_COMMAND) << 8) | inb(PIC1_COMMAND);
}

/* Returns the combined value of the cascaded PICs irq request register */
static uint16_t pic_get_irr(void)
{
    return pic_get_irq_reg(PIC_READ_IRR);
}

/* Returns the combined value of the cascaded PICs in-service register */
static uint16_t pic_get_isr(void)
{
    return pic_get_irq_reg(PIC_READ_ISR);
}

我没有看到任何键盘 ISR 代码,这很可能就是您的问题所在。我猜您正在使用 PC/AT 样式界面与 KB 控制器(如果我没记错的话,端口 60、61、64)进行交互。这些端口中的一个(或多个)在它们产生另一个中断之前有一个或多个输出——如果有内存的话。

一般来说,任何硬件设备在产生中断后都需要引起注意。 PIC产生time tick中断异常

在咨询的方式上,我有几点建议:

  1. 检查您认为错误地破坏了 AX 寄存器 and/or register_t 结构的编译器代码(我不完全确定您所说的 OP 是被丢弃)。准确指出您认为编译器不正确的地方。我怀疑编译器不是不正确,而是从 ISR 调用 C 函数很棘手。在过去这样做时,我必须经过多次迭代才能做到正确。

  2. 想出一个可以查看最近事件的调试方案。

    一个。一种方法是使用 VGA 屏幕内存中的固定位置(我假设您此时正在文本模式下工作),您可以在其中写入指示程序正在做什么的单个字符。特别是顶行末尾的几个字符,比方说 4。这 4 个字符代表 4 个最近发生的事件。当一个事件(例如中断或从用户模式程序调用内核)发生时,您将 3 个最近的字符移动到左侧(或右侧),然后将最新的字符放入 "most recent" 槽中。当您的程序正常工作时,您会在屏幕上看到这些位置有点模糊,因为它们会由于各种事件而迅速更新。但是,如果您的程序挂起,字符将停止更改,您将看到 4 个最近的事件,即使您无法使用调试器进行中断也是如此。

    b。在内存跟踪日志中创建循环。日志中的每个条目都代表一个事件,但可以包含有关事件的详细信息,例如寄存器值或程序状态。如果您使用调试器进行中断,您可以将日志查看为字节、字、双字,并在脑海中解码事件。或者,如果您有一个允许自定义扩展的调试器,则编写一个支持显示和查询日志以查找感兴趣事件的扩展。

FWIW,我实现了一个 V86 监视器(旧的 Intel 术语,用于虚拟化实模式管理程序),所以这些建议来自经验。这些建议适用于在裸机上对内核进行开发测试,并在较小程度上使用 VM 作为开发测试平台。

有了 VM 和正确的虚拟机管理程序,您将可以访问专门设计用于调试 VM 的调试器。例如,Virtual Box 就有这样一个调试器。 Hyper-V上可以用Windbg,虽然我上次试的时候很慢。然而,有人编写了一个扩展,使 Hyper-V VM 的内核调试变得更快。