Similar to virtio infra, vhost core maintain the features
status in the widest format available and allow the devices
to implement extended version of the getter/setter.

Some care is needed for features bit manipulation: when clearing
a bit with 'and not' bitwise operations, the bit mask must be
extended to the feature format, or the all highest bits will be
unintentionally cleared.

Note that 'protocol_features' are not extended: they are only
used by vhost-user, and the latter device is not going to implement
extended features soon.

Signed-off-by: Paolo Abeni <pab...@redhat.com>
---
 hw/virtio/vhost.c                 | 58 ++++++++++++++++++++++++-------
 include/hw/virtio/vhost-backend.h | 10 ++++++
 include/hw/virtio/vhost.h         | 13 +++----
 3 files changed, 62 insertions(+), 19 deletions(-)

diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
index 4cae7c1664..20592473f3 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -985,20 +985,34 @@ static int vhost_virtqueue_set_addr(struct vhost_dev *dev,
 static int vhost_dev_set_features(struct vhost_dev *dev,
                                   bool enable_log)
 {
-    uint64_t features = dev->acked_features;
+    virtio_features_t features = dev->acked_features;
     int r;
     if (enable_log) {
-        features |= 0x1ULL << VHOST_F_LOG_ALL;
+        features |= VIRTIO_BIT(VHOST_F_LOG_ALL);
     }
     if (!vhost_dev_has_iommu(dev)) {
-        features &= ~(0x1ULL << VIRTIO_F_IOMMU_PLATFORM);
+        features &= ~VIRTIO_BIT(VIRTIO_F_IOMMU_PLATFORM);
     }
     if (dev->vhost_ops->vhost_force_iommu) {
         if (dev->vhost_ops->vhost_force_iommu(dev) == true) {
-            features |= 0x1ULL << VIRTIO_F_IOMMU_PLATFORM;
+            features |= VIRTIO_BIT(VIRTIO_F_IOMMU_PLATFORM);
        }
     }
-    r = dev->vhost_ops->vhost_set_features(dev, features);
+
+#ifdef CONFIG_INT128
+    if ((features >> 64) && !dev->vhost_ops->vhost_set_features_ex) {
+        VHOST_OPS_DEBUG(r, "extended features without device support");
+        r = -EINVAL;
+        goto out;
+    }
+
+    if (dev->vhost_ops->vhost_set_features_ex) {
+        r = dev->vhost_ops->vhost_set_features_ex(dev, features);
+    } else
+#endif
+    {
+        r = dev->vhost_ops->vhost_set_features(dev, features);
+    }
     if (r < 0) {
         VHOST_OPS_DEBUG(r, "vhost_set_features failed");
         goto out;
@@ -1505,12 +1519,29 @@ static void vhost_virtqueue_cleanup(struct 
vhost_virtqueue *vq)
     }
 }
 
+static int vhost_dev_get_features(struct vhost_dev *hdev,
+                                  virtio_features_t *features)
+{
+    uint64_t features64;
+    int r;
+
+#ifdef CONFIG_INT128
+    if (hdev->vhost_ops->vhost_get_features_ex)
+        return hdev->vhost_ops->vhost_get_features_ex(hdev, features);
+    else
+#endif
+
+    r = hdev->vhost_ops->vhost_get_features(hdev, &features64);
+    *features = features64;
+    return r;
+}
+
 int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
                    VhostBackendType backend_type, uint32_t busyloop_timeout,
                    Error **errp)
 {
     unsigned int used, reserved, limit;
-    uint64_t features;
+    virtio_features_t features;
     int i, r, n_initialized_vqs = 0;
 
     hdev->vdev = NULL;
@@ -1530,7 +1561,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
         goto fail;
     }
 
-    r = hdev->vhost_ops->vhost_get_features(hdev, &features);
+    r = vhost_dev_get_features(hdev, &features);
     if (r < 0) {
         error_setg_errno(errp, -r, "vhost_get_features failed");
         goto fail;
@@ -1591,7 +1622,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
     };
 
     if (hdev->migration_blocker == NULL) {
-        if (!(hdev->features & (0x1ULL << VHOST_F_LOG_ALL))) {
+        if (!(hdev->features & VIRTIO_BIT(VHOST_F_LOG_ALL))) {
             error_setg(&hdev->migration_blocker,
                        "Migration disabled: vhost lacks VHOST_F_LOG_ALL 
feature.");
         } else if (vhost_dev_log_is_shared(hdev) && !qemu_memfd_alloc_check()) 
{
@@ -1860,12 +1891,13 @@ static void vhost_start_config_intr(struct vhost_dev 
*dev)
     }
 }
 
-uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
-                            uint64_t features)
+virtio_features_t vhost_get_features(struct vhost_dev *hdev,
+                                     const int *feature_bits,
+                                     virtio_features_t features)
 {
     const int *bit = feature_bits;
     while (*bit != VHOST_INVALID_FEATURE_BIT) {
-        uint64_t bit_mask = (1ULL << *bit);
+        virtio_features_t bit_mask = VIRTIO_BIT(*bit);
         if (!(hdev->features & bit_mask)) {
             features &= ~bit_mask;
         }
@@ -1875,11 +1907,11 @@ uint64_t vhost_get_features(struct vhost_dev *hdev, 
const int *feature_bits,
 }
 
 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
-                        uint64_t features)
+                        virtio_features_t features)
 {
     const int *bit = feature_bits;
     while (*bit != VHOST_INVALID_FEATURE_BIT) {
-        uint64_t bit_mask = (1ULL << *bit);
+        virtio_features_t bit_mask = VIRTIO_BIT(*bit);
         if (features & bit_mask) {
             hdev->acked_features |= bit_mask;
         }
diff --git a/include/hw/virtio/vhost-backend.h 
b/include/hw/virtio/vhost-backend.h
index d6df209a2f..de9bcaf95f 100644
--- a/include/hw/virtio/vhost-backend.h
+++ b/include/hw/virtio/vhost-backend.h
@@ -95,6 +95,12 @@ typedef int (*vhost_new_worker_op)(struct vhost_dev *dev,
                                    struct vhost_worker_state *worker);
 typedef int (*vhost_free_worker_op)(struct vhost_dev *dev,
                                     struct vhost_worker_state *worker);
+#ifdef CONFIG_INT128
+typedef int (*vhost_set_features_ex_op)(struct vhost_dev *dev,
+                                        __uint128_t features);
+typedef int (*vhost_get_features_ex_op)(struct vhost_dev *dev,
+                                        __uint128_t *features);
+#endif
 typedef int (*vhost_set_features_op)(struct vhost_dev *dev,
                                      uint64_t features);
 typedef int (*vhost_get_features_op)(struct vhost_dev *dev,
@@ -186,6 +192,10 @@ typedef struct VhostOps {
     vhost_free_worker_op vhost_free_worker;
     vhost_get_vring_worker_op vhost_get_vring_worker;
     vhost_attach_vring_worker_op vhost_attach_vring_worker;
+#ifdef CONFIG_INT128
+    vhost_set_features_ex_op vhost_set_features_ex;
+    vhost_get_features_ex_op vhost_get_features_ex;
+#endif
     vhost_set_features_op vhost_set_features;
     vhost_get_features_op vhost_get_features;
     vhost_set_backend_cap_op vhost_set_backend_cap;
diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h
index bb4b58e115..ea5ad117c5 100644
--- a/include/hw/virtio/vhost.h
+++ b/include/hw/virtio/vhost.h
@@ -106,9 +106,9 @@ struct vhost_dev {
      * future use should be discouraged and the variable retired as
      * its easy to confuse with the VirtIO backend_features.
      */
-    uint64_t features;
-    uint64_t acked_features;
-    uint64_t backend_features;
+    virtio_features_t features;
+    virtio_features_t acked_features;
+    virtio_features_t backend_features;
 
     /**
      * @protocol_features: is the vhost-user only feature set by
@@ -308,8 +308,9 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, 
VirtIODevice *vdev, int n,
  * is supported by the vhost backend (hdev->features), the supported
  * feature_bits and the requested feature set.
  */
-uint64_t vhost_get_features(struct vhost_dev *hdev, const int *feature_bits,
-                            uint64_t features);
+virtio_features_t vhost_get_features(struct vhost_dev *hdev,
+                                     const int *feature_bits,
+                                     virtio_features_t features);
 
 /**
  * vhost_ack_features() - set vhost acked_features
@@ -321,7 +322,7 @@ uint64_t vhost_get_features(struct vhost_dev *hdev, const 
int *feature_bits,
  * the backends advertised features and the supported feature_bits.
  */
 void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits,
-                        uint64_t features);
+                        virtio_features_t features);
 unsigned int vhost_get_max_memslots(void);
 unsigned int vhost_get_free_memslots(void);
 
-- 
2.49.0


Reply via email to