All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Eduardo Habkost" <ehabkost@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Volker Rümelin" <vr_qemu@t-online.de>,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>
Subject: [PULL 07/14] pckbd: PS/2 keyboard throttle
Date: Wed, 26 May 2021 16:06:20 +0200	[thread overview]
Message-ID: <20210526140627.381857-8-kraxel@redhat.com> (raw)
In-Reply-To: <20210526140627.381857-1-kraxel@redhat.com>

From: Volker Rümelin <vr_qemu@t-online.de>

Limit the keyboard data rate to the serial link speed. Some old
DOS software relies on being able to read an incoming scan-code
more than once. After reading keyboard data from the i8042
controller, the guest software has 1ms to read the same data
again.

Use -global i8042.kbd-throttle=on to enable this feature.

To see how this patch works, start a FreeDOS 1.2 guest with the
qemu option -global i8042.kbd-throttle=on and open a text file
with the vim 7.3 32 bit for DOS executable. Then use the cursor
keys (not the cursor keys on the numeric keypad) to move through
the text. Without the kbd-throttle option enabled each keystroke
will move the cursor two positions.

Buglink: https://bugs.launchpad.net/bugs/1895363
Buglink: https://bugs.launchpad.net/bugs/1897568
Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
Message-Id: <20210525181441.27768-6-vr_qemu@t-online.de>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/input/pckbd.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/hw/input/pckbd.c b/hw/input/pckbd.c
index fbd57bf0f572..516ed5a397cd 100644
--- a/hw/input/pckbd.c
+++ b/hw/input/pckbd.c
@@ -23,7 +23,9 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "qemu/log.h"
+#include "qemu/timer.h"
 #include "hw/isa/isa.h"
 #include "migration/vmstate.h"
 #include "hw/acpi/aml-build.h"
@@ -132,11 +134,14 @@
 #define KBD_PENDING_KBD         1
 #define KBD_PENDING_AUX         2
 
+#define KBD_MIGR_TIMER_PENDING  0x1
+
 typedef struct KBDState {
     uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
     uint8_t status;
     uint8_t mode;
     uint8_t outport;
+    uint32_t migration_flags;
     bool outport_present;
     bool extended_state;
     /* Bitmask of devices with data available.  */
@@ -144,6 +149,7 @@ typedef struct KBDState {
     uint8_t obdata;
     void *kbd;
     void *mouse;
+    QEMUTimer *throttle_timer;
 
     qemu_irq irq_kbd;
     qemu_irq irq_mouse;
@@ -208,6 +214,10 @@ static void kbd_safe_update_irq(KBDState *s)
     if (s->status & KBD_STAT_OBF) {
         return;
     }
+    /* the throttle timer is pending and will call kbd_update_irq() */
+    if (s->throttle_timer && timer_pending(s->throttle_timer)) {
+        return;
+    }
     if (s->pending) {
         kbd_update_irq(s);
     }
@@ -237,6 +247,15 @@ static void kbd_update_aux_irq(void *opaque, int level)
     kbd_safe_update_irq(s);
 }
 
+static void kbd_throttle_timeout(void *opaque)
+{
+    KBDState *s = opaque;
+
+    if (s->pending) {
+        kbd_update_irq(s);
+    }
+}
+
 static uint64_t kbd_read_status(void *opaque, hwaddr addr,
                                 unsigned size)
 {
@@ -358,6 +377,10 @@ static uint64_t kbd_read_data(void *opaque, hwaddr addr,
         if (status & KBD_STAT_MOUSE_OBF) {
             s->obdata = ps2_read_data(s->mouse);
         } else {
+            if (s->throttle_timer) {
+                timer_mod(s->throttle_timer,
+                          qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + 1000);
+            }
             s->obdata = ps2_read_data(s->kbd);
         }
     }
@@ -419,6 +442,9 @@ static void kbd_reset(void *opaque)
     s->outport_present = false;
     s->pending = 0;
     kbd_deassert_irq(s);
+    if (s->throttle_timer) {
+        timer_del(s->throttle_timer);
+    }
 }
 
 static uint8_t kbd_outport_default(KBDState *s)
@@ -453,6 +479,29 @@ static const VMStateDescription vmstate_kbd_outport = {
     }
 };
 
+static int kbd_extended_state_pre_save(void *opaque)
+{
+    KBDState *s = opaque;
+
+    s->migration_flags = 0;
+    if (s->throttle_timer && timer_pending(s->throttle_timer)) {
+        s->migration_flags |= KBD_MIGR_TIMER_PENDING;
+    }
+
+    return 0;
+}
+
+static int kbd_extended_state_post_load(void *opaque, int version_id)
+{
+    KBDState *s = opaque;
+
+    if (s->migration_flags & KBD_MIGR_TIMER_PENDING) {
+        kbd_throttle_timeout(s);
+    }
+
+    return 0;
+}
+
 static bool kbd_extended_state_needed(void *opaque)
 {
     KBDState *s = opaque;
@@ -462,8 +511,11 @@ static bool kbd_extended_state_needed(void *opaque)
 
 static const VMStateDescription vmstate_kbd_extended_state = {
     .name = "pckbd/extended_state",
+    .post_load = kbd_extended_state_post_load,
+    .pre_save = kbd_extended_state_pre_save,
     .needed = kbd_extended_state_needed,
     .fields = (VMStateField[]) {
+        VMSTATE_UINT32(migration_flags, KBDState),
         VMSTATE_UINT8(obdata, KBDState),
         VMSTATE_END_OF_LIST()
     }
@@ -554,6 +606,7 @@ struct ISAKBDState {
     ISADevice parent_obj;
 
     KBDState kbd;
+    bool kbd_throttle;
     MemoryRegion io[2];
 };
 
@@ -626,6 +679,13 @@ static void i8042_realizefn(DeviceState *dev, Error **errp)
 
     s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
     s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
+    if (isa_s->kbd_throttle && !isa_s->kbd.extended_state) {
+        warn_report(TYPE_I8042 ": can't enable kbd-throttle without"
+                    " extended-state, disabling kbd-throttle");
+    } else if (isa_s->kbd_throttle) {
+        s->throttle_timer = timer_new_us(QEMU_CLOCK_VIRTUAL,
+                                         kbd_throttle_timeout, s);
+    }
     qemu_register_reset(kbd_reset, s);
 }
 
@@ -659,6 +719,7 @@ static void i8042_build_aml(ISADevice *isadev, Aml *scope)
 
 static Property i8042_properties[] = {
     DEFINE_PROP_BOOL("extended-state", ISAKBDState, kbd.extended_state, true),
+    DEFINE_PROP_BOOL("kbd-throttle", ISAKBDState, kbd_throttle, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
-- 
2.31.1



  parent reply	other threads:[~2021-05-26 14:08 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-26 14:06 [PULL 00/14] Input 20210526 patches Gerd Hoffmann
2021-05-26 14:06 ` [PULL 01/14] hw/input: expand trace info reported for ps2 device Gerd Hoffmann
2021-05-26 14:06 ` [PULL 02/14] ps2: fix mouse stream corruption Gerd Hoffmann
2021-05-26 14:06 ` [PULL 03/14] ps2: don't raise an interrupt if queue is full Gerd Hoffmann
2021-05-26 14:06 ` [PULL 04/14] ps2: don't deassert irq twice if queue is empty Gerd Hoffmann
2021-05-26 14:06 ` [PULL 05/14] pckbd: split out interrupt line changing code Gerd Hoffmann
2021-05-26 14:06 ` [PULL 06/14] pckbd: don't update OBF flags if KBD_STAT_OBF is set Gerd Hoffmann
2021-05-26 14:06 ` Gerd Hoffmann [this message]
2021-05-26 14:06 ` [PULL 08/14] pckbd: add state variable for interrupt source Gerd Hoffmann
2021-05-26 14:06 ` [PULL 09/14] pckbd: add controller response queue Gerd Hoffmann
2021-05-26 14:06 ` [PULL 10/14] pckbd: add function kbd_pending() Gerd Hoffmann
2021-05-26 14:06 ` [PULL 11/14] pckbd: correctly disable PS/2 communication Gerd Hoffmann
2021-05-26 14:06 ` [PULL 12/14] pckbd: remove duplicated keyboard and mouse defines Gerd Hoffmann
2021-05-26 14:06 ` [PULL 13/14] pckbd: clear outport_present in outer pre_load() Gerd Hoffmann
2021-05-26 14:06 ` [PULL 14/14] hw/input/ps2: Use ps2_raise_irq() instead of open coding it Gerd Hoffmann
2021-05-26 20:04 ` [PULL 00/14] Input 20210526 patches Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210526140627.381857-8-kraxel@redhat.com \
    --to=kraxel@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=vr_qemu@t-online.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.