From patchwork Mon Dec 10 14:29:49 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 204925 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 17F752C0193 for ; Tue, 11 Dec 2012 01:31:10 +1100 (EST) Received: from localhost ([::1]:46946 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ti4Nw-0000BO-7w for incoming@patchwork.ozlabs.org; Mon, 10 Dec 2012 09:31:08 -0500 Received: from eggs.gnu.org ([208.118.235.92]:39584) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ti4NG-0007Eq-RA for qemu-devel@nongnu.org; Mon, 10 Dec 2012 09:30:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ti4NA-0001ya-IF for qemu-devel@nongnu.org; Mon, 10 Dec 2012 09:30:26 -0500 Received: from e28smtp08.in.ibm.com ([122.248.162.8]:54905) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ti4N9-0001yH-VY for qemu-devel@nongnu.org; Mon, 10 Dec 2012 09:30:20 -0500 Received: from /spool/local by e28smtp08.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 10 Dec 2012 19:59:51 +0530 Received: from d28dlp02.in.ibm.com (9.184.220.127) by e28smtp08.in.ibm.com (192.168.1.138) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 10 Dec 2012 19:59:49 +0530 Received: from d28relay02.in.ibm.com (d28relay02.in.ibm.com [9.184.220.59]) by d28dlp02.in.ibm.com (Postfix) with ESMTP id 485A4394004C for ; Mon, 10 Dec 2012 20:00:13 +0530 (IST) Received: from d28av04.in.ibm.com (d28av04.in.ibm.com [9.184.220.66]) by d28relay02.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id qBAEUBXA16974028 for ; Mon, 10 Dec 2012 20:00:12 +0530 Received: from d28av04.in.ibm.com (loopback [127.0.0.1]) by d28av04.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id qBAEUCJC001959 for ; Tue, 11 Dec 2012 01:30:12 +1100 Received: from titi.austin.rr.com (sig-9-77-141-109.mts.ibm.com [9.77.141.109]) by d28av04.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id qBAEU0qC000348; Tue, 11 Dec 2012 01:30:10 +1100 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Mon, 10 Dec 2012 08:29:49 -0600 Message-Id: <1355149790-8125-4-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.8.0 In-Reply-To: <1355149790-8125-1-git-send-email-aliguori@us.ibm.com> References: <1355149790-8125-1-git-send-email-aliguori@us.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12121014-2000-0000-0000-00000A33D870 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.8 Cc: Anthony Liguori , Rusty Russell , David Gibson , "Michael S. Tsirkin" Subject: [Qemu-devel] [PATCH 3/4] virtio: modify savevm to have a stable wire format X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org We were memcpy()'ing a structure to the wire :-/ Since savevm really only works on x86 today, lets just declare that this element is sent over the wire as a little endian value in order to fix the bitness. Unfortunately, we also send raw pointers and size_t which are going to be different values on a 32-bit vs. 64-bit QEMU so we need to also deal with that case. A lot of values that should have been previously ignored are now sent as 0 and ignored on the receive side too. Signed-off-by: Anthony Liguori --- hw/virtio.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 2 deletions(-) diff --git a/hw/virtio.c b/hw/virtio.c index 8eb8f69..0108d62 100644 --- a/hw/virtio.c +++ b/hw/virtio.c @@ -875,14 +875,124 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f) return 0; } +/* We used to memcpy the structure to the wire so that's the reason for all of + * this ugliness */ + +#if HOST_LONG_BITS == 32 +static uint32 virtio_get_long(QEMUFile *f) +{ + return qemu_get_le32(f); +} + +static void virtio_put_long(QEMUFile *f, uint32_t value) +{ + qemu_put_le32(f, value); +} +#elif HOST_LONG_BITS == 64 +static uint64 virtio_get_long(QEMUFile *f) +{ + return qemu_get_le64(f); +} + +static void virtio_put_long(QEMUFile *f, uint64_t value) +{ + qemu_put_le64(f, value); +} +#else +#error Invalid HOST_LONG_BITS +#endif + void virtio_put_virt_queue_element(QEMUFile *f, const VirtQueueElement *elem) { - qemu_put_buffer(f, (unsigned char*)elem, sizeof(*elem)); + int i; + + qemu_put_le32(f, elem->index); + qemu_put_le32(f, elem->out_num); + qemu_put_le32(f, elem->in_num); + + qemu_put_le32(f, 0); /* padding for structure */ + + for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) { + if (i < elem->in_num) { + qemu_put_le64(f, elem->in_addr[i]); + } else { + qemu_put_le64(f, 0); + } + } + + for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) { + if (i < elem->out_num) { + qemu_put_le64(f, elem->out_addr[i]); + } else { + qemu_put_le64(f, 0); + } + } + + for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) { + virtio_put_long(f, 0); + if (i < elem->in_num) { + virtio_put_long(f, elem->in_sg[i].iov_len); + } else { + virtio_put_long(f, 0); + } + } + + for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) { + virtio_put_long(f, 0); + if (i < elem->out_num) { + virtio_put_long(f, elem->out_sg[i].iov_len); + } else { + virtio_put_long(f, 0); + } + } } void virtio_get_virt_queue_element(QEMUFile *f, VirtQueueElement *elem) { - qemu_get_buffer(f, (unsigned char *)elem, sizeof(*elem)); + int i; + + elem->index = qemu_get_le32(f); + elem->out_num = qemu_get_le32(f); + elem->in_num = qemu_get_le32(f); + + assert(elem->out_num <= VIRTQUEUE_MAX_SIZE && + elem->in_num <= VIRTQUEUE_MAX_SIZE); + + qemu_get_le32(f); /* padding for structure */ + + for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) { + if (i < elem->in_num) { + elem->in_addr[i] = qemu_get_le64(f); + } else { + qemu_get_le64(f); + } + } + + for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) { + if (i < elem->out_num) { + elem->out_addr[i] = qemu_get_le64(f); + } else { + qemu_get_le64(f); + } + } + + for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) { + virtio_get_long(f); + if (i < elem->in_num) { + elem->in_sg[i].iov_len = virtio_get_long(f); + } else { + virtio_get_long(f); + } + } + + for (i = 0; i < VIRTQUEUE_MAX_SIZE; i++) { + virtio_get_long(f); + if (i < elem->out_num) { + elem->out_sg[i].iov_len = virtio_get_long(f); + } else { + virtio_get_long(f); + } + } virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1); virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0);