From patchwork Tue May 28 10:18:54 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 246813 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 E6D672C030F for ; Tue, 28 May 2013 20:19:08 +1000 (EST) Received: from localhost ([::1]:45657 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UhGzj-0003VK-2d for incoming@patchwork.ozlabs.org; Tue, 28 May 2013 06:19:07 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54846) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UhGzJ-0003VB-TT for qemu-devel@nongnu.org; Tue, 28 May 2013 06:18:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UhGzG-0005P6-6n for qemu-devel@nongnu.org; Tue, 28 May 2013 06:18:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41203) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UhGzF-0005P0-VV for qemu-devel@nongnu.org; Tue, 28 May 2013 06:18:38 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r4SAIXYa009465 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 28 May 2013 06:18:33 -0400 Received: from redhat.com (vpn-202-197.tlv.redhat.com [10.35.202.197]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id r4SAITcM002751; Tue, 28 May 2013 06:18:30 -0400 Date: Tue, 28 May 2013 13:18:54 +0300 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <20130528101854.GA29978@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Peter Maydell , Anthony Liguori , agraf@suse.de, blauwirbel@gmail.com, Paolo Bonzini , KONRAD Frederic Subject: [Qemu-devel] [PATCH v2 RFC] virtio-pci: fix LE host/BE guest capacity for blk 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 When a BE guest reads capacity from an LE host virtio-blk device or vice versa, it will get the dwords of the qword field swapped. As virtio-blk is the only one with such a quirk, and as non-pci transports don't do byte-swaps at all, solve this with a bit of device-specific hackery in virtio-pci. Signed-off-by: Michael S. Tsirkin --- Changes from v1: fixed some obvious bugs. I don't seem to be able to boot any big-endian guests ATM, so this is only compile-tested - sending this out for early feedback/flames. Testing reports also wellcome! diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index 9668b2b..e7971cc 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -98,6 +98,12 @@ /* HACK for virtio to determine if it's running a big endian guest */ bool virtio_is_big_endian(void); +#ifdef HOST_WORDS_BIGENDIAN +#define VIRTIO_HOST_IS_BIG_ENDIAN true +#else +#define VIRTIO_HOST_IS_BIG_ENDIAN false +#endif + static void virtio_pci_bus_new(VirtioBusState *bus, VirtIOPCIProxy *dev); /* virtio device */ @@ -411,6 +417,15 @@ static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, } break; case 4: + /* Most devices don't have 64 bit config fields. + * Block is an exception: first 8 bytes include + * a 64 bit capacity field. + */ + if (virtio_is_big_endian() != VIRTIO_HOST_IS_BIG_ENDIAN && + proxy->vdev->device_id == VIRTIO_ID_BLOCK && addr < 8) { + /* Swap first two words */ + addr ^= 0x4; + } val = virtio_config_readl(proxy->vdev, addr); if (virtio_is_big_endian()) { val = bswap32(val);