ps2: Fix mouse stream corruption due to lost data
PATCH 2/2 ps2: Fix mouse stream corruption due to lost data
This fixes an issue by adding bounds checking to multi-byte packets where the PS/2 mouse data stream may become corrupted due to data being discarded when the PS/2 ringbuffer is full.
Interrupts for Multi-byte responses are postponed until the final byte has been queued.
These changes fix a bug where windows guests drop the mouse device entirely requring the guest to be restarted.
--- hw/input/pckbd.c | 6 +-- hw/input/ps2.c | 160 +++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 110 insertions(+), 56 deletions(-)
hw/input/pckbd.c +2 -2
@@ -216,9 +216,9 @@ static uint64_t kbd_read_status(void *opaque, hwaddr addr,static void kbd_queue (KBDState *s,int b,int aux) {if (aux) -ps2_queue (s->mouse, b); +ps2_queue_raise (s->mouse, b);else -ps2_queue (s->kbd, b); +ps2_queue_raise (s->kbd, b); }static void outport_write (KBDState *s,uint32_t val)
hw/input/ps2.c +80 -29
207 lines not on this page · raw message
0)
break;
}
@@ -713,66 +764,68 @@ void ps2_write_mouse(void *opaque, int val)
if (s->mouse_wrap) {
if (val == AUX_RESET_WRAP) {
s->mouse_wrap = 0;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
return;
} else if (val != AUX_RESET) {
- ps2_queue(&s->common, val);
+ ps2_queue_raise(&s->common, val);
return;
}
}
switch(val) {
case AUX_SET_SCALE11:
s->mouse_status &= ~MOUSE_STATUS_SCALE21;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
break;
case AUX_SET_SCALE21:
s->mouse_status |= MOUSE_STATUS_SCALE21;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
break;
case AUX_SET_STREAM:
s->mouse_status &= ~MOUSE_STATUS_REMOTE;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
break;
case AUX_SET_WRAP:
s->mouse_wrap = 1;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
break;
case AUX_SET_REMOTE:
s->mouse_status |= MOUSE_STATUS_REMOTE;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
break;
case AUX_GET_TYPE:
- ps2_queue(&s->common, AUX_ACK);
- ps2_queue(&s->common, s->mouse_type);
+ ps2_queue_bytes(&s->common, 2,
+ AUX_ACK,
+ s->mouse_type);
break;
case AUX_SET_RES:
case AUX_SET_SAMPLE:
s->common.write_cmd = val;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
break;
case AUX_GET_SCALE:
- ps2_queue(&s->common, AUX_ACK);
- ps2_queue(&s->common, s->mouse_status);
- ps2_queue(&s->common, s->mouse_resolution);
- ps2_queue(&s->common, s->mouse_sample_rate);
+ ps2_queue_bytes(&s->common, 4,
+ AUX_ACK,
+ s->mouse_status,
+ s->mouse_resolution,
+ s->mouse_sample_rate);
break;
case AUX_POLL:
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
ps2_mouse_send_packet(s);
break;
case AUX_ENABLE_DEV:
s->mouse_status |= MOUSE_STATUS_ENABLED;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
break;
case AUX_DISABLE_DEV:
s->mouse_status &= ~MOUSE_STATUS_ENABLED;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
break;
case AUX_SET_DEFAULT:
s->mouse_sample_rate = 100;
s->mouse_resolution = 2;
s->mouse_status = 0;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
break;
case AUX_RESET:
s->mouse_sample_rate = 100;
@@ -780,9 +833,10 @@ void ps2_write_mouse(void *opaque, int val)
s->mouse_status = 0;
s->mouse_type = 0;
ps2_reset_queue(&s->common);
- ps2_queue(&s->common, AUX_ACK);
- ps2_queue(&s->common, 0xaa);
- ps2_queue(&s->common, s->mouse_type);
+ ps2_queue_bytes(&s->common, 3,
+ AUX_ACK,
+ 0xaa,
+ s->mouse_type);
break;
default:
break;
@@ -816,12 +870,12 @@ void ps2_write_mouse(void *opaque, int val)
s->mouse_detect_state = 0;
break;
}
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
s->common.write_cmd = -1;
break;
case AUX_SET_RES:
s->mouse_resolution = val;
- ps2_queue(&s->common, AUX_ACK);
+ ps2_queue_raise(&s->common, AUX_ACK);
s->common.write_cmd = -1;
break;
}Signature · 1 line
2.14.2
Quoted 5 lines
hw/input/ps2.c +0
-0
hw/input/ps2.c void ps2_queue(PS2State *s, int b) · line 199
Quoted 9 lines
hw/input/ps2.c +4
-1@@ -192,12 +192,50 @@ void ps2_queue(PS2State *s, int b) {PS2Queue *q = &s->queue; -if (q->count >=PS2_QUEUE_SIZE -1 ) +if (q->count ==PS2_QUEUE_SIZE ) + { +printf ("Warning! PS2 Queue Overflow!\n" );return ; + }
Leftover debug printf?
hw/input/ps2.c void ps2_queue(PS2State *s, int b) · line 216
Quoted 10 lines
hw/input/ps2.c +10
-0+void ps2_raise (PS2State *s) +{ + s->update_irq (s->update_arg,1 ); +} + +void ps2_queue_raise (PS2State *s,int b) +{ +ps2_queue (s, b); + s->update_irq (s->update_arg,1 ); +}
I'd suggest to keep the ps2_queue() name. Makes the patch much smaller and easier to review. Factor out the code to actually queue things to a new ps2_queue_noirq() function.
hw/input/ps2.c void ps2_queue(PS2State *s, int b) · line 218
Quoted 1 line
hw/input/ps2.c +1
-0+void ps2_queue_bytes (PS2State *s,const int length, ...)
I'd prefer to not use vaargs here as gcc can't check the arguments then. Suggest to just have ps2_queue_{2,3,4}() helpers instead to queue multibyte messages.
cheers,
Gerd
Quoted 8 lines · Gerd Hoffmann
Quoted 8 lines
hw/input/ps2.c +0
-0@@ -192,12 +192,50 @@ void ps2_queue(PS2State *s, int b) {PS2Queue *q = &s->queue;
Quoted 8 lines
hw/input/ps2.c void ps2_queue(PS2State *s, int b) · line 199
Quoted 6 lines
hw/input/ps2.c +4
-1-if (q->count >=PS2_QUEUE_SIZE -1 ) +if (q->count ==PS2_QUEUE_SIZE ) + { +printf ("Warning! PS2 Queue Overflow!\n" );return ; + }Leftover debug printf?
Correct :), I will remove it.
Quoted 17 lines
hw/input/ps2.c void ps2_queue(PS2State *s, int b) · line 216
Quoted 11 lines
hw/input/ps2.c +10
-0+void ps2_raise (PS2State *s) +{ + s->update_irq (s->update_arg,1 ); +} + +void ps2_queue_raise (PS2State *s,int b) +{ +ps2_queue (s, b); + s->update_irq (s->update_arg,1 ); +}I'd suggest to keep the
ps2_queue()name. Makes the patch much smaller and easier to review. Factor out the code to actually queue things to a newps2_queue_noirq()function.Quoted 1 line
hw/input/ps2.c +1
-0+void ps2_queue_bytes (PS2State *s,const int length, ...)
Ack.
Quoted 4 lines
I'd prefer to not use vaargs here as gcc can't check the arguments then. Suggest to just have ps2_queue_{2,3,4}() helpers instead to queue multibyte messages.
Ack.
Quoted 3 lines
cheers,
Gerd
Thanks,
Geoff