From af336b49c908dc0d2b62846a19001d4dac7cad61 Mon Sep 17 00:00:00 2001 From: Minep Date: Tue, 3 May 2022 23:51:48 +0100 Subject: [PATCH] Add back the missing reserved field in FADT. Add a FADT version check before using IAPC_BOOT_ARCH flag for 8042 existence. Fixed the spurious IRQ#1 happens during 8042 initialization under VirtualBox environment. --- lunaix-os/hal/ioapic.c | 3 -- lunaix-os/includes/hal/acpi/fadt.h | 2 +- lunaix-os/includes/hal/io.h | 10 +++++++ lunaix-os/kernel/peripheral/ps2kbd.c | 41 ++++++++++++++++++++++++---- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/lunaix-os/hal/ioapic.c b/lunaix-os/hal/ioapic.c index de4be2a..3ca3dee 100644 --- a/lunaix-os/hal/ioapic.c +++ b/lunaix-os/hal/ioapic.c @@ -24,12 +24,9 @@ ioapic_init() { // grab ourselves these irq numbers uint8_t irq_rtc = ioapic_get_irq(acpi_ctx, PC_AT_IRQ_RTC); - uint8_t irq_kbd = ioapic_get_irq(acpi_ctx, PC_AT_IRQ_KBD); // PC_AT_IRQ_RTC -> RTC_TIMER_IV, fixed, edge trigged, polarity=high, physical, APIC ID 0 ioapic_redirect(irq_rtc, RTC_TIMER_IV, 0, IOAPIC_DELMOD_FIXED); - - ioapic_redirect(irq_kbd, PC_KBD_IV, 0, IOAPIC_DELMOD_FIXED); } uint8_t diff --git a/lunaix-os/includes/hal/acpi/fadt.h b/lunaix-os/includes/hal/acpi/fadt.h index a613a6c..e0b9943 100644 --- a/lunaix-os/includes/hal/acpi/fadt.h +++ b/lunaix-os/includes/hal/acpi/fadt.h @@ -43,6 +43,7 @@ typedef struct acpi_fadt { acpi_sdthdr_t header; uint32_t firmware_controller_addr; uint32_t dsdt_addr; + uint8_t reserved; uint8_t pm_profile; uint16_t sci_int; uint32_t smi_cmd_port_addr; @@ -68,7 +69,6 @@ typedef struct acpi_fadt { uint16_t boot_arch; } ACPI_TABLE_PACKED acpi_fadt_t; - // TODO: FADT parser & support #endif /* __LUNAIX_FADT_H */ diff --git a/lunaix-os/includes/hal/io.h b/lunaix-os/includes/hal/io.h index c57de59..dd44509 100644 --- a/lunaix-os/includes/hal/io.h +++ b/lunaix-os/includes/hal/io.h @@ -110,5 +110,15 @@ io_outl(int port, uint32_t data) { asm volatile("outl %0,%w1" : : "a"(data), "d"(port)); } +static inline void +io_delay(int counter) +{ + asm volatile ( + " test %0, %0\n" + " jz 1f\n" + "2: dec %0\n" + " jnz 2b\n" + "1: dec %0"::"a"(counter)); +} #endif /* __LUNAIX_IO_H */ diff --git a/lunaix-os/kernel/peripheral/ps2kbd.c b/lunaix-os/kernel/peripheral/ps2kbd.c index 0cd3f2c..ae51dcf 100644 --- a/lunaix-os/kernel/peripheral/ps2kbd.c +++ b/lunaix-os/kernel/peripheral/ps2kbd.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -106,12 +107,26 @@ void ps2_kbd_init() { kbd_state.state = KBD_STATE_KWAIT; acpi_context* acpi_ctx = acpi_get_context(); - if (!(acpi_ctx->fadt.boot_arch & IAPC_ARCH_8042)) { - kprintf(KERROR "No PS/2 controller detected.\n"); - // FUTURE: Some alternative fallback on this? Check PCI bus for USB controller instead? - return; + if (acpi_ctx->fadt.header.rev > 1) { + /* + * 只有当前ACPI版本大于1时,我们才使用FADT的IAPC_BOOT_ARCH去判断8042是否存在。 + * 这是一个坑,在ACPI v1中,这个字段是reserved!而这及至APCI v2才出现。 + * 需要注意:Bochs 和 QEMU 使用的是ACPI v1,而非 v2 (virtualbox好像是v4) + * + * 请看Bochs的bios源码(QEMU的BIOS其实是照抄bochs的,所以也是一个德行。。): + * https://bochs.sourceforge.io/cgi-bin/lxr/source/bios/rombios32.c#L1314 + */ + if (!(acpi_ctx->fadt.boot_arch & IAPC_ARCH_8042)) { + kprintf(KERROR "No PS/2 controller detected.\n"); + // FUTURE: Some alternative fallback on this? Check PCI bus for USB controller instead? + return; + } + } + else { + kprintf(KWARN "Outdated FADT used, assuming 8042 always exist.\n"); } + cpu_disable_interrupt(); // 1、禁用任何的PS/2设备 @@ -158,6 +173,19 @@ void ps2_kbd_init() { // 因为我们需要保证isr尽量的简短,运行起来快速。而发送这些命令非常的耗时。 timer_run_ms(5, ps2_process_cmd, NULL, TIMER_MODE_PERIODIC); + /* + * 一切准备就绪后,我们才教ioapic去启用IRQ#1。 + * 至于为什么要在这里,原因是:初始化所使用的一些指令可能会导致IRQ#1的触发(因为返回码),或者是一些什么 + * 情况导致IRQ#1的误触发(可能是未初始化导致IRQ#1线上不稳定)。于是这些IRQ#1会堆积在APIC的队列里(因为此时我们正在 + * 初始化8042,屏蔽了所有中断,IF=0)。 + * 当sti后,这些堆积的中断会紧跟着递送进CPU里,导致我们的键盘handler误认为由按键按下,从而将这个毫无意义的数值加入 + * 我们的队列中,以供上层读取。 + * + * 所以,保险的方法是:在初始化后才去设置ioapic,这样一来我们就能有一个稳定的IRQ#1以放心使用。 + */ + uint8_t irq_kbd = ioapic_get_irq(acpi_ctx, PC_AT_IRQ_KBD); + ioapic_redirect(irq_kbd, PC_KBD_IV, 0, IOAPIC_DELMOD_FIXED); + done: cpu_enable_interrupt(); } @@ -261,7 +289,7 @@ void intr_ps2_kbd_handler(const isr_param* param) { return; } - //kprintf(KINFO "%x\n", scancode & 0xff); + //kprintf(KDEBUG "%x\n", scancode & 0xff); switch (kbd_state.state) { @@ -331,9 +359,12 @@ static void ps2_post_cmd(uint8_t port, char cmd, uint16_t arg) { while((result = io_inb(PS2_PORT_CTRL_STATUS)) & PS2_STATUS_IFULL); io_outb(port, cmd); + io_delay(PS2_DELAY); + if (!(arg & PS2_NO_ARG)) { // 所有参数一律通过0x60传入。 io_outb(PS2_PORT_ENC_CMDREG, (uint8_t)(arg & 0x00ff)); + io_delay(PS2_DELAY); } } -- 2.27.0