Add back the missing reserved field in FADT.
authorMinep <zelong56@gmail.com>
Tue, 3 May 2022 22:51:48 +0000 (23:51 +0100)
committerMinep <zelong56@gmail.com>
Tue, 3 May 2022 22:51:48 +0000 (23:51 +0100)
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
lunaix-os/includes/hal/acpi/fadt.h
lunaix-os/includes/hal/io.h
lunaix-os/kernel/peripheral/ps2kbd.c

index de4be2a57a1caebede99b419c3457ff32fe3d2c1..3ca3dee854047cbdd3a2ecc9d02fe01bb4029b5a 100644 (file)
@@ -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
index a613a6c4711d3c2ddc2b38e0ff23bc2611106dff..e0b99431f584f070a3e1dc7006b76379d91ad9ba 100644 (file)
@@ -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 */
index c57de5984a060979e87689d95e77efa375288737..dd445098bf7240b7c64e623457005e3aa898f6ce 100644 (file)
@@ -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 */
index 0cd3f2cb52b8076c192022bdf8ffd2443774bde2..ae51dcf01ce38b9f540b81b26ba4c6cc11484fb7 100644 (file)
@@ -4,6 +4,7 @@
 #include <lunaix/common.h>
 #include <lunaix/syslog.h>
 #include <hal/acpi/acpi.h>
+#include <hal/ioapic.h>
 
 #include <hal/cpu.h>
 #include <arch/x86/interrupts.h>
@@ -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);
     }
 }