[PATCH] hw/input/pckbd: raise the keyboard irq for 0xd2 also while disabled

Christian Quante posted 1 patch 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261018051715.12267-1-christian@quante.one
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
hw/input/pckbd.c | 29 +++++++++++++++++++++++------
1 file changed, 23 insertions(+), 6 deletions(-)
[PATCH] hw/input/pckbd: raise the keyboard irq for 0xd2 also while disabled
Posted by Christian Quante 1 month ago
QEMU raises no keyboard irq while the keyboard is disabled
(KBD_MODE_DISABLE_KBD, for example after KBD_CCMD_KBD_DISABLE 0xad).
This also applies to the bytes that the controller itself puts into the
output buffer, such as the answer to KBD_CCMD_READ_MODE (0x20) or a
byte that the guest writes with KBD_CCMD_WRITE_OBUF (0xd2).

The keyboard driver of the OS/2 Warp 4 installation (IBMKBD.SYS,
1996-09-09, code at file offsets 0xf08 and 0xf8b) disables the
keyboard, sends 0x20, reads port 0x60 at once and then waits for the
keyboard irq.  If the irq does not come within about 0.35 seconds, the
driver writes the byte that it has read back with 0xd2 and waits again,
so it expects an irq for this byte.  With
QEMU, this irq does not come either.  The driver then loses track of
its command sequence and leaves the keyboard disabled: the installation
does not accept the Enter key after the request for diskette 2.

0xd2 puts a byte into the output buffer as if the keyboard had sent it
(HelpPC 2.10, 8042 commands: "as if initiated by a device; invokes
interrupt if enabled").  Bochs (controller_enQ() in keyboard.cc) and
86Box (kbc_send_to_ob() in kbc_at.c) raise the keyboard irq for every
byte in the output buffer that does not come from the mouse, if bit 0
of the command byte is set.  They do this also while the keyboard is
disabled.

Raise the irq while the keyboard is disabled, but only for a byte from
0xd2.  The answers to other controller commands keep the old behaviour,
because an irq for them causes a problem with the emulated 8259: the
8259 of QEMU and the 8259 of KVM keep the edge request of a masked line
after the line has gone low again, but a real 8259A drops it (Intel
8259A data sheet: "the request latch is a transparent D type latch",
and "the IR inputs must remain high until after the falling edge of the
first INTA").  Windows for Workgroups 3.11 masks IRQ 1,
sends 0xad and 0x20, and polls for the answer.  With an irq for the
answer, it gets a stale IRQ 1 with an empty output buffer when it
unmasks the line, and then it masks IRQ 1 for good, so the keyboard
does not work any more.  In my traces (start of Windows for Workgroups
up to the first dialog box, and one key press), it does not use 0xd2.

A new flag in "pending" marks the byte from 0xd2.  The flag is only set
with the extended state of the i8042, so machine types 6.0 and older
keep the old behaviour.  It is migrated with
"pending"; an older QEMU clears unknown flags in kbd_post_load() and
keeps its old behaviour for this byte.

With this patch, the Warp 4 installation goes on after the 0xd2
fallback (one delay of about 0.35 seconds, tested with TCG).

Signed-off-by: Christian Quante <christian@quante.one>
---

Notes:
    Windows for Workgroups 3.11 and OS/2 Warp 3 (both with KVM) work as
    before.  Both runs, and the traces of Windows for Workgroups above, used
    my Cirrus patches (the first series is
    <20261018050643.9288-1-christian@quante.one>, the others follow),
    because the Cirrus display drivers of these guests need them; the
    keyboard path does not depend on them.

 hw/input/pckbd.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/hw/input/pckbd.c b/hw/input/pckbd.c
index b09c3bce93..06b4c5d9ea 100644
--- a/hw/input/pckbd.c
+++ b/hw/input/pckbd.c
@@ -139,6 +139,7 @@
 #define KBD_PENDING_CTRL_AUX    0x08
 #define KBD_PENDING_KBD         KBD_MODE_DISABLE_KBD    /* 0x10 */
 #define KBD_PENDING_AUX         KBD_MODE_DISABLE_MOUSE  /* 0x20 */
+#define KBD_PENDING_CTRL_OBUF   0x40
 
 #define KBD_MIGR_TIMER_PENDING  0x1
 
@@ -149,7 +150,10 @@
 
 /*
  * XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
- * incorrect, but it avoids having to simulate exact delays
+ * incorrect, but it avoids having to simulate exact delays.
+ * A byte from KBD_CCMD_WRITE_OBUF raises the irq also while the keyboard
+ * is disabled: a guest may write a byte with this command and then wait
+ * for the keyboard irq.
  */
 static void kbd_update_irq_lines(KBDState *s)
 {
@@ -165,7 +169,9 @@ static void kbd_update_irq_lines(KBDState *s)
             }
         } else {
             if ((s->mode & KBD_MODE_KBD_INT) &&
-                !(s->mode & KBD_MODE_DISABLE_KBD)) {
+                (!(s->mode & KBD_MODE_DISABLE_KBD) ||
+                 ((s->obsrc & KBD_OBSRC_CTRL) &&
+                  (s->pending & KBD_PENDING_CTRL_OBUF)))) {
                 irq_kbd_level = 1;
             }
         }
@@ -282,7 +288,8 @@ static void kbd_queue(KBDState *s, int b, int aux)
 {
     if (s->extended_state) {
         s->cbdata = b;
-        s->pending &= ~KBD_PENDING_CTRL_KBD & ~KBD_PENDING_CTRL_AUX;
+        s->pending &= ~KBD_PENDING_CTRL_KBD & ~KBD_PENDING_CTRL_AUX &
+                      ~KBD_PENDING_CTRL_OBUF;
         s->pending |= aux ? KBD_PENDING_CTRL_AUX : KBD_PENDING_CTRL_KBD;
         kbd_safe_update_irq(s);
     } else {
@@ -290,11 +297,21 @@ static void kbd_queue(KBDState *s, int b, int aux)
     }
 }
 
+static void kbd_queue_obuf(KBDState *s, int b)
+{
+    kbd_queue(s, b, 0);
+    if (s->extended_state) {
+        s->pending |= KBD_PENDING_CTRL_OBUF;
+        kbd_update_irq_lines(s);
+    }
+}
+
 static uint8_t kbd_dequeue(KBDState *s)
 {
     uint8_t b = s->cbdata;
 
-    s->pending &= ~KBD_PENDING_CTRL_KBD & ~KBD_PENDING_CTRL_AUX;
+    s->pending &= ~KBD_PENDING_CTRL_KBD & ~KBD_PENDING_CTRL_AUX &
+                  ~KBD_PENDING_CTRL_OBUF;
     if (kbd_pending(s)) {
         kbd_update_irq(s);
     }
@@ -450,7 +467,7 @@ static void kbd_write_data(void *opaque, hwaddr addr,
         kbd_safe_update_irq(s);
         break;
     case KBD_CCMD_WRITE_OBUF:
-        kbd_queue(s, val, 0);
+        kbd_queue_obuf(s, val);
         break;
     case KBD_CCMD_WRITE_AUX_OBUF:
         kbd_queue(s, val, 1);
@@ -608,7 +625,7 @@ static int kbd_post_load(void *opaque, int version_id)
     }
     /* clear all unused flags */
     s->pending &= KBD_PENDING_CTRL_KBD | KBD_PENDING_CTRL_AUX |
-                  KBD_PENDING_KBD | KBD_PENDING_AUX;
+                  KBD_PENDING_CTRL_OBUF | KBD_PENDING_KBD | KBD_PENDING_AUX;
     return 0;
 }
 
-- 
2.53.0