From patchwork Mon May 7 13:13:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Denis V. Lunev\" via" X-Patchwork-Id: 10384123 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id ACFF660236 for ; Mon, 7 May 2018 13:19:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD23B27031 for ; Mon, 7 May 2018 13:19:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A161528B14; Mon, 7 May 2018 13:19:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 005E527031 for ; Mon, 7 May 2018 13:19:49 +0000 (UTC) Received: from localhost ([::1]:46251 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fFg3R-0004Tk-58 for patchwork-qemu-devel@patchwork.kernel.org; Mon, 07 May 2018 09:19:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37694) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fFg1t-0003ry-PU for qemu-devel@nongnu.org; Mon, 07 May 2018 09:18:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fFg1n-0001jY-D1 for qemu-devel@nongnu.org; Mon, 07 May 2018 09:18:13 -0400 Received: from mail1.hostfission.com ([139.99.139.48]:50300) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fFg1m-0001is-RB for qemu-devel@nongnu.org; Mon, 07 May 2018 09:18:07 -0400 Received: from moya.office.hostfission.com (office.hostfission.com [158.140.217.90]) by mail1.hostfission.com (Postfix) with ESMTP id 0652841ED8; Mon, 7 May 2018 23:18:06 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=hostfission.com; s=mail; t=1525699086; bh=dVHYTcLZ3KeVoD+nXqL/TCRD31ciQbUAVos/Noyu1Gw=; h=From:Date:Subject:To:From; b=oGi94rq760HTDiwp9Xx1o+tAA2N7EdoEwwOeg1X5EI9uGZYBVsVwOq3ufhaH+3kiN p5xcs1FLAabQiVQAYF6iuf4Gnb+nqyodDvAfg8HIUcSxHnhGFmk2FFUSHfdmimi6wI e6JJWsy6Ad7Czcl0n1RgprLz+5X8bbLoveu2xfD4= Received: by moya.office.hostfission.com (Postfix, from userid 0) id EBE2A381908; Mon, 7 May 2018 23:18:05 +1000 (AEST) Date: Mon, 7 May 2018 23:13:12 +1000 To: qemu-devel@nongnu.org, Gerd Hoffmann Message-Id: <20180507131805.EBE2A381908@moya.office.hostfission.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 139.99.139.48 Subject: [Qemu-devel] [PATCHv2 2/2] ps2: Fix mouse stream corruption due to lost data X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Geoffrey McRae via Qemu-devel From: "Denis V. Lunev\" via" Reply-To: Geoffrey McRae Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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. Signed-off-by: Geoffrey McRae --- hw/input/ps2.c | 120 +++++++++++++++++++++++++++++++++++++------------ include/hw/input/ps2.h | 5 +++ 2 files changed, 96 insertions(+), 29 deletions(-) diff --git a/hw/input/ps2.c b/hw/input/ps2.c index f84a8f5179..0580ca0700 100644 --- a/hw/input/ps2.c +++ b/hw/input/ps2.c @@ -188,16 +188,60 @@ static void ps2_reset_queue(PS2State *s) q->count = 0; } -void ps2_queue(PS2State *s, int b) +void ps2_queue_noirq(PS2State *s, int b) { PS2Queue *q = &s->queue; - if (q->count >= PS2_QUEUE_SIZE - 1) + if (q->count == PS2_QUEUE_SIZE) return; + q->data[q->wptr] = b; if (++q->wptr == PS2_QUEUE_SIZE) q->wptr = 0; q->count++; +} + +void ps2_raise_irq(PS2State *s) +{ + s->update_irq(s->update_arg, 1); +} + +void ps2_queue(PS2State *s, int b) +{ + ps2_queue_noirq(s, b); + s->update_irq(s->update_arg, 1); +} + +void ps2_queue_2(PS2State *s, int b1, int b2) +{ + if (PS2_QUEUE_SIZE - s->queue.count < 2) + return; + + ps2_queue_noirq(s, b1); + ps2_queue_noirq(s, b2); + s->update_irq(s->update_arg, 1); +} + +void ps2_queue_3(PS2State *s, int b1, int b2, int b3) +{ + if (PS2_QUEUE_SIZE - s->queue.count < 3) + return; + + ps2_queue_noirq(s, b1); + ps2_queue_noirq(s, b2); + ps2_queue_noirq(s, b3); + s->update_irq(s->update_arg, 1); +} + +void ps2_queue_4(PS2State *s, int b1, int b2, int b3, int b4) +{ + if (PS2_QUEUE_SIZE - s->queue.count < 4) + return; + + ps2_queue_noirq(s, b1); + ps2_queue_noirq(s, b2); + ps2_queue_noirq(s, b3); + ps2_queue_noirq(s, b4); s->update_irq(s->update_arg, 1); } @@ -499,13 +543,17 @@ void ps2_write_keyboard(void *opaque, int val) ps2_queue(&s->common, KBD_REPLY_RESEND); break; case KBD_CMD_GET_ID: - ps2_queue(&s->common, KBD_REPLY_ACK); /* We emulate a MF2 AT keyboard here */ - ps2_queue(&s->common, KBD_REPLY_ID); if (s->translate) - ps2_queue(&s->common, 0x41); + ps2_queue_3(&s->common, + KBD_REPLY_ACK, + KBD_REPLY_ID, + 0x41); else - ps2_queue(&s->common, 0x83); + ps2_queue_3(&s->common, + KBD_REPLY_ACK, + KBD_REPLY_ID, + 0x83); break; case KBD_CMD_ECHO: ps2_queue(&s->common, KBD_CMD_ECHO); @@ -532,8 +580,9 @@ void ps2_write_keyboard(void *opaque, int val) break; case KBD_CMD_RESET: ps2_reset_keyboard(s); - ps2_queue(&s->common, KBD_REPLY_ACK); - ps2_queue(&s->common, KBD_REPLY_POR); + ps2_queue_2(&s->common, + KBD_REPLY_ACK, + KBD_REPLY_POR); break; default: ps2_queue(&s->common, KBD_REPLY_RESEND); @@ -542,8 +591,11 @@ void ps2_write_keyboard(void *opaque, int val) break; case KBD_CMD_SCANCODE: if (val == 0) { - ps2_queue(&s->common, KBD_REPLY_ACK); - ps2_put_keycode(s, s->scancode_set); + if (s->common.queue.count <= PS2_QUEUE_SIZE - 2) + { + ps2_queue(&s->common, KBD_REPLY_ACK); + ps2_put_keycode(s, s->scancode_set); + } } else if (val >= 1 && val <= 3) { s->scancode_set = val; ps2_queue(&s->common, KBD_REPLY_ACK); @@ -575,11 +627,15 @@ void ps2_keyboard_set_translation(void *opaque, int mode) s->translate = mode; } -static void ps2_mouse_send_packet(PS2MouseState *s) +static int ps2_mouse_send_packet(PS2MouseState *s) { unsigned int b; int dx1, dy1, dz1; + const int needed = 3 + (s->mouse_type - 2); + if (PS2_QUEUE_SIZE - s->common.queue.count < needed) + return 0; + dx1 = s->mouse_dx; dy1 = s->mouse_dy; dz1 = s->mouse_dz; @@ -593,9 +649,9 @@ static void ps2_mouse_send_packet(PS2MouseState *s) else if (dy1 < -127) dy1 = -127; b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07); - ps2_queue(&s->common, b); - ps2_queue(&s->common, dx1 & 0xff); - ps2_queue(&s->common, dy1 & 0xff); + ps2_queue_noirq(&s->common, b); + ps2_queue_noirq(&s->common, dx1 & 0xff); + ps2_queue_noirq(&s->common, dy1 & 0xff); /* extra byte for IMPS/2 or IMEX */ switch(s->mouse_type) { default: @@ -605,7 +661,7 @@ static void ps2_mouse_send_packet(PS2MouseState *s) dz1 = 127; else if (dz1 < -127) dz1 = -127; - ps2_queue(&s->common, dz1 & 0xff); + ps2_queue_noirq(&s->common, dz1 & 0xff); break; case 4: if (dz1 > 7) @@ -613,15 +669,19 @@ static void ps2_mouse_send_packet(PS2MouseState *s) else if (dz1 < -7) dz1 = -7; b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1); - ps2_queue(&s->common, b); + ps2_queue_noirq(&s->common, b); break; } + ps2_raise_irq(&s->common); + trace_ps2_mouse_send_packet(s, dx1, dy1, dz1, b); /* update deltas */ s->mouse_dx -= dx1; s->mouse_dy -= dy1; s->mouse_dz -= dz1; + + return 1; } static void ps2_mouse_event(DeviceState *dev, QemuConsole *src, @@ -683,10 +743,9 @@ static void ps2_mouse_sync(DeviceState *dev) qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); } if (!(s->mouse_status & MOUSE_STATUS_REMOTE)) { - while (s->common.queue.count < PS2_QUEUE_SIZE - 4) { - /* if not remote, send event. Multiple events are sent if - too big deltas */ - ps2_mouse_send_packet(s); + /* if not remote, send event. Multiple events are sent if + too big deltas */ + while(ps2_mouse_send_packet(s)) { if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0) break; } @@ -745,8 +804,9 @@ void ps2_write_mouse(void *opaque, int val) ps2_queue(&s->common, AUX_ACK); break; case AUX_GET_TYPE: - ps2_queue(&s->common, AUX_ACK); - ps2_queue(&s->common, s->mouse_type); + ps2_queue_2(&s->common, + AUX_ACK, + s->mouse_type); break; case AUX_SET_RES: case AUX_SET_SAMPLE: @@ -754,10 +814,11 @@ void ps2_write_mouse(void *opaque, int val) ps2_queue(&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_4(&s->common, + AUX_ACK, + s->mouse_status, + s->mouse_resolution, + s->mouse_sample_rate); break; case AUX_POLL: ps2_queue(&s->common, AUX_ACK); @@ -783,9 +844,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_3(&s->common, + AUX_ACK, + 0xaa, + s->mouse_type); break; default: break; diff --git a/include/hw/input/ps2.h b/include/hw/input/ps2.h index 94709b8502..213aa16aa3 100644 --- a/include/hw/input/ps2.h +++ b/include/hw/input/ps2.h @@ -37,7 +37,12 @@ void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg); void ps2_write_mouse(void *, int val); void ps2_write_keyboard(void *, int val); uint32_t ps2_read_data(PS2State *s); +void ps2_queue_noirq(PS2State *s, int b); +void ps2_raise_irq(PS2State *s); void ps2_queue(PS2State *s, int b); +void ps2_queue_2(PS2State *s, int b1, int b2); +void ps2_queue_3(PS2State *s, int b1, int b2, int b3); +void ps2_queue_4(PS2State *s, int b1, int b2, int b3, int b4); void ps2_keyboard_set_translation(void *opaque, int mode); void ps2_mouse_fake_event(void *opaque);