vhost: cross-endian support for legacy devices
This patch brings cross-endian support to vhost when used to implement legacy virtio devices. Since it is a relatively rare situation, the feature availability is controlled by a kernel config option (not set by default). The vq->is_le boolean field is added to cache the endianness to be used for ring accesses. It defaults to native endian, as expected by legacy virtio devices. When the ring gets active, we force little endian if the device is modern. When the ring is deactivated, we revert to the native endian default. If cross-endian was compiled in, a vq->user_be boolean field is added so that userspace may request a specific endianness. This field is used to override the default when activating the ring of a legacy device. It has no effect on modern devices. Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Reviewed-by: Cornelia Huck <cornelia.huck@de.ibm.com> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
parent
7d82410950
commit
2751c9882b
|
@ -32,3 +32,18 @@ config VHOST
|
||||||
---help---
|
---help---
|
||||||
This option is selected by any driver which needs to access
|
This option is selected by any driver which needs to access
|
||||||
the core of vhost.
|
the core of vhost.
|
||||||
|
|
||||||
|
config VHOST_CROSS_ENDIAN_LEGACY
|
||||||
|
bool "Cross-endian support for vhost"
|
||||||
|
default n
|
||||||
|
---help---
|
||||||
|
This option allows vhost to support guests with a different byte
|
||||||
|
ordering from host while using legacy virtio.
|
||||||
|
|
||||||
|
Userspace programs can control the feature using the
|
||||||
|
VHOST_SET_VRING_ENDIAN and VHOST_GET_VRING_ENDIAN ioctls.
|
||||||
|
|
||||||
|
This is only useful on a few platforms (ppc64 and arm64). Since it
|
||||||
|
adds some overhead, it is disabled by default.
|
||||||
|
|
||||||
|
If unsure, say "N".
|
||||||
|
|
|
@ -36,6 +36,77 @@ enum {
|
||||||
#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
|
#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
|
||||||
#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
|
#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
|
||||||
|
|
||||||
|
#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
|
||||||
|
static void vhost_vq_reset_user_be(struct vhost_virtqueue *vq)
|
||||||
|
{
|
||||||
|
vq->user_be = !virtio_legacy_is_little_endian();
|
||||||
|
}
|
||||||
|
|
||||||
|
static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
|
||||||
|
{
|
||||||
|
struct vhost_vring_state s;
|
||||||
|
|
||||||
|
if (vq->private_data)
|
||||||
|
return -EBUSY;
|
||||||
|
|
||||||
|
if (copy_from_user(&s, argp, sizeof(s)))
|
||||||
|
return -EFAULT;
|
||||||
|
|
||||||
|
if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
|
||||||
|
s.num != VHOST_VRING_BIG_ENDIAN)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
vq->user_be = s.num;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
|
||||||
|
int __user *argp)
|
||||||
|
{
|
||||||
|
struct vhost_vring_state s = {
|
||||||
|
.index = idx,
|
||||||
|
.num = vq->user_be
|
||||||
|
};
|
||||||
|
|
||||||
|
if (copy_to_user(argp, &s, sizeof(s)))
|
||||||
|
return -EFAULT;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void vhost_init_is_le(struct vhost_virtqueue *vq)
|
||||||
|
{
|
||||||
|
/* Note for legacy virtio: user_be is initialized at reset time
|
||||||
|
* according to the host endianness. If userspace does not set an
|
||||||
|
* explicit endianness, the default behavior is native endian, as
|
||||||
|
* expected by legacy virtio.
|
||||||
|
*/
|
||||||
|
vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
static void vhost_vq_reset_user_be(struct vhost_virtqueue *vq)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
|
||||||
|
{
|
||||||
|
return -ENOIOCTLCMD;
|
||||||
|
}
|
||||||
|
|
||||||
|
static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
|
||||||
|
int __user *argp)
|
||||||
|
{
|
||||||
|
return -ENOIOCTLCMD;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void vhost_init_is_le(struct vhost_virtqueue *vq)
|
||||||
|
{
|
||||||
|
if (vhost_has_feature(vq, VIRTIO_F_VERSION_1))
|
||||||
|
vq->is_le = true;
|
||||||
|
}
|
||||||
|
#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
|
||||||
|
|
||||||
static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
|
static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
|
||||||
poll_table *pt)
|
poll_table *pt)
|
||||||
{
|
{
|
||||||
|
@ -199,6 +270,8 @@ static void vhost_vq_reset(struct vhost_dev *dev,
|
||||||
vq->call = NULL;
|
vq->call = NULL;
|
||||||
vq->log_ctx = NULL;
|
vq->log_ctx = NULL;
|
||||||
vq->memory = NULL;
|
vq->memory = NULL;
|
||||||
|
vq->is_le = virtio_legacy_is_little_endian();
|
||||||
|
vhost_vq_reset_user_be(vq);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int vhost_worker(void *data)
|
static int vhost_worker(void *data)
|
||||||
|
@ -806,6 +879,12 @@ long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
|
||||||
} else
|
} else
|
||||||
filep = eventfp;
|
filep = eventfp;
|
||||||
break;
|
break;
|
||||||
|
case VHOST_SET_VRING_ENDIAN:
|
||||||
|
r = vhost_set_vring_endian(vq, argp);
|
||||||
|
break;
|
||||||
|
case VHOST_GET_VRING_ENDIAN:
|
||||||
|
r = vhost_get_vring_endian(vq, idx, argp);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
r = -ENOIOCTLCMD;
|
r = -ENOIOCTLCMD;
|
||||||
}
|
}
|
||||||
|
@ -1044,8 +1123,12 @@ int vhost_init_used(struct vhost_virtqueue *vq)
|
||||||
{
|
{
|
||||||
__virtio16 last_used_idx;
|
__virtio16 last_used_idx;
|
||||||
int r;
|
int r;
|
||||||
if (!vq->private_data)
|
if (!vq->private_data) {
|
||||||
|
vq->is_le = virtio_legacy_is_little_endian();
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
vhost_init_is_le(vq);
|
||||||
|
|
||||||
r = vhost_update_used_flags(vq);
|
r = vhost_update_used_flags(vq);
|
||||||
if (r)
|
if (r)
|
||||||
|
|
|
@ -106,6 +106,14 @@ struct vhost_virtqueue {
|
||||||
/* Log write descriptors */
|
/* Log write descriptors */
|
||||||
void __user *log_base;
|
void __user *log_base;
|
||||||
struct vhost_log *log;
|
struct vhost_log *log;
|
||||||
|
|
||||||
|
/* Ring endianness. Defaults to legacy native endianness.
|
||||||
|
* Set to true when starting a modern virtio device. */
|
||||||
|
bool is_le;
|
||||||
|
#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
|
||||||
|
/* Ring endianness requested by userspace for cross-endian support. */
|
||||||
|
bool user_be;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
struct vhost_dev {
|
struct vhost_dev {
|
||||||
|
@ -175,8 +183,7 @@ static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
|
||||||
|
|
||||||
static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
|
static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
|
||||||
{
|
{
|
||||||
return vhost_has_feature(vq, VIRTIO_F_VERSION_1) ||
|
return vq->is_le;
|
||||||
virtio_legacy_is_little_endian();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Memory accessors */
|
/* Memory accessors */
|
||||||
|
|
|
@ -103,6 +103,20 @@ struct vhost_memory {
|
||||||
/* Get accessor: reads index, writes value in num */
|
/* Get accessor: reads index, writes value in num */
|
||||||
#define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
|
#define VHOST_GET_VRING_BASE _IOWR(VHOST_VIRTIO, 0x12, struct vhost_vring_state)
|
||||||
|
|
||||||
|
/* Set the vring byte order in num. Valid values are VHOST_VRING_LITTLE_ENDIAN
|
||||||
|
* or VHOST_VRING_BIG_ENDIAN (other values return -EINVAL).
|
||||||
|
* The byte order cannot be changed while the device is active: trying to do so
|
||||||
|
* returns -EBUSY.
|
||||||
|
* This is a legacy only API that is simply ignored when VIRTIO_F_VERSION_1 is
|
||||||
|
* set.
|
||||||
|
* Not all kernel configurations support this ioctl, but all configurations that
|
||||||
|
* support SET also support GET.
|
||||||
|
*/
|
||||||
|
#define VHOST_VRING_LITTLE_ENDIAN 0
|
||||||
|
#define VHOST_VRING_BIG_ENDIAN 1
|
||||||
|
#define VHOST_SET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x13, struct vhost_vring_state)
|
||||||
|
#define VHOST_GET_VRING_ENDIAN _IOW(VHOST_VIRTIO, 0x14, struct vhost_vring_state)
|
||||||
|
|
||||||
/* The following ioctls use eventfd file descriptors to signal and poll
|
/* The following ioctls use eventfd file descriptors to signal and poll
|
||||||
* for events. */
|
* for events. */
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue