From: Anthony Liguori <anthony@codemonkey.ws>
To: Rusty Russell <rusty@rustcorp.com.au>, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 1/7] virtio: allow byte swapping for vring and config access
Date: Thu, 08 Aug 2013 08:31:22 -0500 [thread overview]
Message-ID: <87li4cgvh1.fsf@codemonkey.ws> (raw)
In-Reply-To: <1375938949-22622-2-git-send-email-rusty@rustcorp.com.au>
Rusty Russell <rusty@rustcorp.com.au> writes:
> Virtio is currently defined to work as "guest endian", but this is a
> problem if the guest can change endian. As most targets can't change
> endian, we make it a per-target option to avoid pessimising.
>
> This is based on a simpler patch by Anthony Liguouri, which only handled
> the vring accesses. We also need some drivers to access these helpers,
> eg. for data which contains headers.
>
> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
> ---
> hw/virtio/virtio.c | 46 +++++++++----
> include/hw/virtio/virtio-access.h | 138 ++++++++++++++++++++++++++++++++++++++
> 2 files changed, 170 insertions(+), 14 deletions(-)
> create mode 100644 include/hw/virtio/virtio-access.h
>
> diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
> index 8176c14..2887f17 100644
> --- a/hw/virtio/virtio.c
> +++ b/hw/virtio/virtio.c
> @@ -18,6 +18,7 @@
> #include "hw/virtio/virtio.h"
> #include "qemu/atomic.h"
> #include "hw/virtio/virtio-bus.h"
> +#include "hw/virtio/virtio-access.h"
>
> /* The alignment to use between consumer and producer parts of vring.
> * x86 pagesize again. */
> @@ -84,6 +85,20 @@ struct VirtQueue
> EventNotifier host_notifier;
> };
>
> +#ifdef TARGET_VIRTIO_SWAPENDIAN
> +bool virtio_byteswap;
> +
> +/* Ask target code if we should swap endian for all vring and config access. */
> +static void mark_endian(void)
> +{
> + virtio_byteswap = virtio_swap_endian();
> +}
> +#else
> +static void mark_endian(void)
> +{
> +}
> +#endif
> +
It would be very good to avoid a target specific define here. We would
like to move to only building a single copy of the virtio code.
We have a mechanism to do weak functions via stubs/. I think it would
be better to do cpu_get_byteswap() as a stub function and then overload
it in the ppc64 code.
> /* virt queue functions */
> static void virtqueue_init(VirtQueue *vq)
> {
> @@ -100,49 +115,49 @@ static inline uint64_t vring_desc_addr(hwaddr desc_pa, int i)
> {
> hwaddr pa;
> pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr);
> - return ldq_phys(pa);
> + return virtio_ldq_phys(pa);
> }
>
> static inline uint32_t vring_desc_len(hwaddr desc_pa, int i)
> {
> hwaddr pa;
> pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len);
> - return ldl_phys(pa);
> + return virtio_ldl_phys(pa);
> }
>
> static inline uint16_t vring_desc_flags(hwaddr desc_pa, int i)
> {
> hwaddr pa;
> pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags);
> - return lduw_phys(pa);
> + return virtio_lduw_phys(pa);
> }
>
> static inline uint16_t vring_desc_next(hwaddr desc_pa, int i)
> {
> hwaddr pa;
> pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next);
> - return lduw_phys(pa);
> + return virtio_lduw_phys(pa);
> }
>
> static inline uint16_t vring_avail_flags(VirtQueue *vq)
> {
> hwaddr pa;
> pa = vq->vring.avail + offsetof(VRingAvail, flags);
> - return lduw_phys(pa);
> + return virtio_lduw_phys(pa);
> }
>
> static inline uint16_t vring_avail_idx(VirtQueue *vq)
> {
> hwaddr pa;
> pa = vq->vring.avail + offsetof(VRingAvail, idx);
> - return lduw_phys(pa);
> + return virtio_lduw_phys(pa);
> }
>
> static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
> {
> hwaddr pa;
> pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
> - return lduw_phys(pa);
> + return virtio_lduw_phys(pa);
> }
>
> static inline uint16_t vring_used_event(VirtQueue *vq)
> @@ -154,42 +169,42 @@ static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val)
> {
> hwaddr pa;
> pa = vq->vring.used + offsetof(VRingUsed, ring[i].id);
> - stl_phys(pa, val);
> + virtio_stl_phys(pa, val);
> }
>
> static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val)
> {
> hwaddr pa;
> pa = vq->vring.used + offsetof(VRingUsed, ring[i].len);
> - stl_phys(pa, val);
> + virtio_stl_phys(pa, val);
> }
>
> static uint16_t vring_used_idx(VirtQueue *vq)
> {
> hwaddr pa;
> pa = vq->vring.used + offsetof(VRingUsed, idx);
> - return lduw_phys(pa);
> + return virtio_lduw_phys(pa);
> }
>
> static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
> {
> hwaddr pa;
> pa = vq->vring.used + offsetof(VRingUsed, idx);
> - stw_phys(pa, val);
> + virtio_stw_phys(pa, val);
> }
>
> static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
> {
> hwaddr pa;
> pa = vq->vring.used + offsetof(VRingUsed, flags);
> - stw_phys(pa, lduw_phys(pa) | mask);
> + virtio_stw_phys(pa, virtio_lduw_phys(pa) | mask);
> }
>
> static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
> {
> hwaddr pa;
> pa = vq->vring.used + offsetof(VRingUsed, flags);
> - stw_phys(pa, lduw_phys(pa) & ~mask);
> + virtio_stw_phys(pa, virtio_lduw_phys(pa) & ~mask);
> }
>
> static inline void vring_avail_event(VirtQueue *vq, uint16_t val)
> @@ -199,7 +214,7 @@ static inline void vring_avail_event(VirtQueue *vq, uint16_t val)
> return;
> }
> pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]);
> - stw_phys(pa, val);
> + virtio_stw_phys(pa, val);
> }
>
> void virtio_queue_set_notification(VirtQueue *vq, int enable)
> @@ -525,6 +540,9 @@ void virtio_set_status(VirtIODevice *vdev, uint8_t val)
> VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
> trace_virtio_set_status(vdev, val);
>
> + /* If guest virtio endian is uncertain, set it now. */
> + mark_endian();
> +
> if (k->set_status) {
> k->set_status(vdev, val);
> }
> diff --git a/include/hw/virtio/virtio-access.h b/include/hw/virtio/virtio-access.h
> new file mode 100644
> index 0000000..b1d531e
> --- /dev/null
> +++ b/include/hw/virtio/virtio-access.h
> @@ -0,0 +1,138 @@
> +/*
> + * Virtio Accessor Support: In case your target can change endian.
> + *
> + * Copyright IBM, Corp. 2013
> + *
> + * Authors:
> + * Rusty Russell <rusty@au.ibm.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2. See
> + * the COPYING file in the top-level directory.
> + *
> + */
> +#ifndef _QEMU_VIRTIO_ACCESS_H
> +#define _QEMU_VIRTIO_ACCESS_H
> +
> +#ifdef TARGET_VIRTIO_SWAPENDIAN
> +/* Architectures which need biendian define this function. */
> +extern bool virtio_swap_endian(void);
> +
> +extern bool virtio_byteswap;
> +#else
> +#define virtio_byteswap false
> +#endif
I suspect this is a premature optimization. With a weak function called
directly in the accessors below, I suspect you would see no measurable
performance overhead compared to this approach.
It's all very predictable so the CPU should do a decent job optimizing
the if () away.
Regards,
Anthony Liguori
next prev parent reply other threads:[~2013-08-08 13:31 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-08 5:15 [Qemu-devel] [PATCH 0/7] Virtio support for endian-curious guests Rusty Russell
2013-08-08 5:15 ` [Qemu-devel] [PATCH 1/7] virtio: allow byte swapping for vring and config access Rusty Russell
2013-08-08 13:31 ` Anthony Liguori [this message]
2013-08-08 14:28 ` Andreas Färber
2013-08-08 15:40 ` Anthony Liguori
2013-08-08 15:45 ` Daniel P. Berrange
2013-08-08 16:07 ` Anthony Liguori
2013-08-08 16:14 ` Peter Maydell
2013-08-08 16:25 ` Anthony Liguori
2013-08-08 16:30 ` Peter Maydell
2013-08-09 2:58 ` Rusty Russell
2013-08-09 4:39 ` Anton Blanchard
2013-08-09 8:05 ` Peter Maydell
2013-08-09 14:16 ` Anthony Liguori
2013-08-08 15:48 ` Peter Maydell
2013-08-08 16:11 ` Anthony Liguori
2013-08-08 16:24 ` Andreas Färber
2013-08-09 7:35 ` Rusty Russell
2013-08-09 7:42 ` Peter Maydell
2013-08-12 7:49 ` Rusty Russell
2013-08-09 7:49 ` Benjamin Herrenschmidt
2013-08-12 0:28 ` Rusty Russell
2013-08-12 0:49 ` Benjamin Herrenschmidt
2013-08-09 15:15 ` Andreas Färber
2013-08-09 0:08 ` Rusty Russell
2013-08-09 7:00 ` Rusty Russell
2013-08-09 14:24 ` Andreas Färber
2013-08-09 6:40 ` Rusty Russell
2013-08-09 14:10 ` Anthony Liguori
2013-08-11 23:46 ` Rusty Russell
2013-08-08 5:15 ` [Qemu-devel] [PATCH 2/7] target-ppc: ppc64 targets can be either endian Rusty Russell
2013-08-08 5:15 ` [Qemu-devel] [PATCH 3/7] hw/net/virtio-net: use virtio wrappers to access headers Rusty Russell
2013-08-08 13:32 ` Anthony Liguori
2013-08-08 5:15 ` [Qemu-devel] [PATCH 4/7] hw/net/virtio-balloon: use virtio wrappers to access page frame numbers Rusty Russell
2013-08-08 13:32 ` Anthony Liguori
2013-08-08 5:15 ` [Qemu-devel] [PATCH 5/7] hw/block/virtio-blk: use virtio wrappers to access headers Rusty Russell
2013-08-08 9:57 ` Peter Maydell
2013-08-08 13:32 ` Anthony Liguori
2013-08-08 5:15 ` [Qemu-devel] [PATCH 6/7] hw/scsi/virtio-scsi: " Rusty Russell
2013-08-08 13:33 ` Anthony Liguori
2013-08-08 5:15 ` [Qemu-devel] [PATCH 7/7] hw/char/virtio-serial-bus: " Rusty Russell
2013-08-08 13:34 ` Anthony Liguori
2013-08-08 5:15 ` [Qemu-devel] [PATCH 7/7] patch virtio-serial-biendian.patch Rusty Russell
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=87li4cgvh1.fsf@codemonkey.ws \
--to=anthony@codemonkey.ws \
--cc=qemu-devel@nongnu.org \
--cc=rusty@rustcorp.com.au \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).