Skip to contentSkip to conversation

hw/virtio: Build most objects as target independent units

RFC Superseded Philippe Mathieu-Daudé 5 participants 26 messages Dec 12, 2022 – Apr 11, 2025 Arm/Amlogic Meson SoC support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hw/virtio: Build most objects as target independent units

Currently the inlined virtio_access_is_big_endian() function "hw/virtio/virtio-access.h" which is used by all I/O accesses force any virtio device to be built as target-dependent object.

This series isolates the few VirtIO target specific bits, trying to not impact the performance (a function is un-inlined once not in the hot path).

At the end only 6 files remain in the target specific source set, all other files are built once.

On a Linux/x86_64 host when building QEMU configured with --disable-user, before this series a "make clean all" builds 7713 objects, after it only build 6831... I don't think my maths are correct, because that would save building a few hundreds objects.

RFC because only build-tested.

Regards,

Phil.

Philippe Mathieu-Daudé (10):
hw/virtio: Add missing "hw/core/cpu.h" include
hw/virtio: Rename virtio_ss[] -> specific_virtio_ss[] hw/virtio: Constify qmp_virtio_feature_map_t[]
hw/virtio: Extract config read/write accessors to virtio-config.c hw/virtio: Extract QMP related code virtio-qmp.c hw/virtio: Cache access_is_big_endian value in VirtIODevice state hw/virtio: Directly access cached VirtIODevice::access_is_big_endian hw/virtio: Un-inline virtio_access_is_big_endian() hw/virtio: Extract vhost_user_ram_slots_max() to vhost-user-target.c hw/virtio: Make most of virtio devices target-independent

 hw/9pfs/meson.build               |   2 +-
 hw/block/dataplane/meson.build    |   2 +-
 hw/block/meson.build              |   4 +-
 hw/char/meson.build               |   2 +-
 hw/net/meson.build                |   2 +-
 hw/virtio/meson.build             |  45 +-
 hw/virtio/vhost-user-target.c     |  29 ++
 hw/virtio/vhost-user.c            |  26 +-
 hw/virtio/virtio-config.c         | 224 +++++++++
 hw/virtio/virtio-qmp.c            | 631 +++++++++++++++++++++++
 hw/virtio/virtio-qmp.h            |  20 +
 hw/virtio/virtio.c                | 799 +-----------------------------
 include/hw/virtio/vhost-user.h    |   7 +
 include/hw/virtio/virtio-access.h |  63 +--
 include/hw/virtio/virtio.h        |   1 +
 15 files changed, 974 insertions(+), 883 deletions(-)
 create mode 100644 hw/virtio/vhost-user-target.c
 create mode 100644 hw/virtio/virtio-config.c
 create mode 100644 hw/virtio/virtio-qmp.c
 create mode 100644 hw/virtio/virtio-qmp.h
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/10 hw/virtio: Add missing "hw/core/cpu.h" include

virtio.c uses target_words_bigendian() which is declared in "hw/core/cpu.h". Add the missing header to avoid when refactoring:

hw/virtio/virtio.c:2451:9: error: implicit declaration of function 'target_words_bigendian' is invalid in C99 [-Werror,-Wimplicit-function-declaration]

    if (target_words_bigendian()) {
        ^
---
 hw/virtio/virtio.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/virtio.c +1 -0
index eb6347ab5d..5817f4cbc9 100644
@@ -25,6 +25,7 @@
 #include "qemu/main-loop.h"
 #include "qemu/module.h"
 #include "qom/object_interfaces.h"
+#include "hw/core/cpu.h"
 #include "hw/virtio/virtio.h"
 #include "migration/qemu-file-types.h"
 #include "qemu/atomic.h"
Signature · 1 line
2.38.1
#2 lore raw

PATCH 2/10 hw/virtio: Rename virtio_ss[] -> specific_virtio_ss[]

In reply to Philippe Mathieu-Daudé

Since virtio_ss[] is added to specific_ss[], rename it as specific_virtio_ss[] to make it clearer.

---
 hw/virtio/meson.build | 41 +++++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 20 deletions(-)
hw/virtio/meson.build +21 -20
index dfed1e7af5..23a980efaa 100644
@@ -3,34 +3,34 @@ softmmu_virtio_ss.add(files('virtio-bus.c'))
 softmmu_virtio_ss.add(when: 'CONFIG_VIRTIO_PCI', if_true: files('virtio-pci.c'))
 softmmu_virtio_ss.add(when: 'CONFIG_VIRTIO_MMIO', if_true: files('virtio-mmio.c'))
 
-virtio_ss = ss.source_set()
-virtio_ss.add(files('virtio.c'))
+specific_virtio_ss = ss.source_set()
+specific_virtio_ss.add(files('virtio.c'))
 
 if have_vhost
-  virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c'))
+  specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c'))
   if have_vhost_user
-    virtio_ss.add(files('vhost-user.c'))
+    specific_virtio_ss.add(files('vhost-user.c'))
   endif
   if have_vhost_vdpa
-    virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-virtqueue.c'))
+    specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-virtqueue.c'))
   endif
 else
   softmmu_virtio_ss.add(files('vhost-stub.c'))
 endif
 
-virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
-virtio_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('virtio-crypto.c'))
-virtio_ss.add(when: 'CONFIG_VHOST_USER_FS', if_true: files('vhost-user-fs.c'))
-virtio_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem.c'))
-virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock.c', 'vhost-vsock-common.c'))
-virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock.c', 'vhost-vsock-common.c'))
-virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c'))
-virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c'))
-virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c'))
-virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c'))
-virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rng.c'))
-virtio_ss.add(when: 'CONFIG_VHOST_USER_GPIO', if_true: files('vhost-user-gpio.c'))
-virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_GPIO'], if_true: files('vhost-user-gpio-pci.c'))
+specific_virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
+specific_virtio_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('virtio-crypto.c'))
+specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_FS', if_true: files('vhost-user-fs.c'))
+specific_virtio_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem.c'))
+specific_virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock.c', 'vhost-vsock-common.c'))
+specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock.c', 'vhost-vsock-common.c'))
+specific_virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c'))
+specific_virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c'))
+specific_virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c'))
+specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c'))
+specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rng.c'))
+specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_GPIO', if_true: files('vhost-user-gpio.c'))
+specific_virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_GPIO'], if_true: files('vhost-user-gpio-pci.c'))
 
 virtio_pci_ss = ss.source_set()
 virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c'))
@@ -57,11 +57,12 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem-pci.c'
 virtio_pci_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu-pci.c'))
 virtio_pci_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem-pci.c'))
 
-virtio_ss.add_all(when: 'CONFIG_VIRTIO_PCI', if_true: virtio_pci_ss)
+specific_virtio_ss.add_all(when: 'CONFIG_VIRTIO_PCI', if_true: virtio_pci_ss)
 
-specific_ss.add_all(when: 'CONFIG_VIRTIO', if_true: virtio_ss)
 softmmu_ss.add_all(when: 'CONFIG_VIRTIO', if_true: softmmu_virtio_ss)
 softmmu_ss.add(when: 'CONFIG_VIRTIO', if_false: files('vhost-stub.c'))
 softmmu_ss.add(when: 'CONFIG_VIRTIO', if_false: files('virtio-stub.c'))
 softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c'))
 softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('virtio-stub.c'))
+
+specific_ss.add_all(when: 'CONFIG_VIRTIO', if_true: specific_virtio_ss)
Signature · 1 line
2.38.1
#3 lore raw

PATCH 3/10 hw/virtio: Constify qmp_virtio_feature_map_t[]

In reply to Philippe Mathieu-Daudé

These arrays are only accessed read-only, move them to .rodata.

---
 hw/virtio/virtio.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
hw/virtio/virtio.c +17 -17
index 5817f4cbc9..f54cc23304 100644
@@ -80,7 +80,7 @@ enum VhostUserProtocolFeature {
 };
 
 /* Virtio transport features mapping */
-static qmp_virtio_feature_map_t virtio_transport_map[] = {
+static const qmp_virtio_feature_map_t virtio_transport_map[] = {
     /* Virtio device transport features */
 #ifndef VIRTIO_CONFIG_NO_LEGACY
     FEATURE_ENTRY(VIRTIO_F_NOTIFY_ON_EMPTY, \
@@ -111,7 +111,7 @@ static qmp_virtio_feature_map_t virtio_transport_map[] = {
 };
 
 /* Vhost-user protocol features mapping */
-static qmp_virtio_feature_map_t vhost_user_protocol_map[] = {
+static const qmp_virtio_feature_map_t vhost_user_protocol_map[] = {
     FEATURE_ENTRY(VHOST_USER_PROTOCOL_F_MQ, \
             "VHOST_USER_PROTOCOL_F_MQ: Multiqueue protocol supported"),
     FEATURE_ENTRY(VHOST_USER_PROTOCOL_F_LOG_SHMFD, \
@@ -161,7 +161,7 @@ static qmp_virtio_feature_map_t vhost_user_protocol_map[] = {
 };
 
 /* virtio device configuration statuses */
-static qmp_virtio_feature_map_t virtio_config_status_map[] = {
+static const qmp_virtio_feature_map_t virtio_config_status_map[] = {
     FEATURE_ENTRY(VIRTIO_CONFIG_S_DRIVER_OK, \
             "VIRTIO_CONFIG_S_DRIVER_OK: Driver setup and ready"),
     FEATURE_ENTRY(VIRTIO_CONFIG_S_FEATURES_OK, \
@@ -179,7 +179,7 @@ static qmp_virtio_feature_map_t virtio_config_status_map[] = {
 };
 
 /* virtio-blk features mapping */
-qmp_virtio_feature_map_t virtio_blk_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_blk_feature_map[] = {
     FEATURE_ENTRY(VIRTIO_BLK_F_SIZE_MAX, \
             "VIRTIO_BLK_F_SIZE_MAX: Max segment size is size_max"),
     FEATURE_ENTRY(VIRTIO_BLK_F_SEG_MAX, \
@@ -218,7 +218,7 @@ qmp_virtio_feature_map_t virtio_blk_feature_map[] = {
 };
 
 /* virtio-serial features mapping */
-qmp_virtio_feature_map_t virtio_serial_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_serial_feature_map[] = {
     FEATURE_ENTRY(VIRTIO_CONSOLE_F_SIZE, \
             "VIRTIO_CONSOLE_F_SIZE: Host providing console size"),
     FEATURE_ENTRY(VIRTIO_CONSOLE_F_MULTIPORT, \
@@ -229,7 +229,7 @@ qmp_virtio_feature_map_t virtio_serial_feature_map[] = {
 };
 
 /* virtio-gpu features mapping */
-qmp_virtio_feature_map_t virtio_gpu_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_gpu_feature_map[] = {
     FEATURE_ENTRY(VIRTIO_GPU_F_VIRGL, \
             "VIRTIO_GPU_F_VIRGL: Virgl 3D mode supported"),
     FEATURE_ENTRY(VIRTIO_GPU_F_EDID, \
@@ -250,7 +250,7 @@ qmp_virtio_feature_map_t virtio_gpu_feature_map[] = {
 };
 
 /* virtio-input features mapping */
-qmp_virtio_feature_map_t virtio_input_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_input_feature_map[] = {
     FEATURE_ENTRY(VHOST_F_LOG_ALL, \
             "VHOST_F_LOG_ALL: Logging write descriptors supported"),
     FEATURE_ENTRY(VHOST_USER_F_PROTOCOL_FEATURES, \
@@ -260,7 +260,7 @@ qmp_virtio_feature_map_t virtio_input_feature_map[] = {
 };
 
 /* virtio-net features mapping */
-qmp_virtio_feature_map_t virtio_net_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_net_feature_map[] = {
     FEATURE_ENTRY(VIRTIO_NET_F_CSUM, \
             "VIRTIO_NET_F_CSUM: Device handling packets with partial checksum "
             "supported"),
@@ -338,7 +338,7 @@ qmp_virtio_feature_map_t virtio_net_feature_map[] = {
 };
 
 /* virtio-scsi features mapping */
-qmp_virtio_feature_map_t virtio_scsi_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_scsi_feature_map[] = {
     FEATURE_ENTRY(VIRTIO_SCSI_F_INOUT, \
             "VIRTIO_SCSI_F_INOUT: Requests including read and writable data "
             "buffers suppoted"),
@@ -359,7 +359,7 @@ qmp_virtio_feature_map_t virtio_scsi_feature_map[] = {
 };
 
 /* virtio/vhost-user-fs features mapping */
-qmp_virtio_feature_map_t virtio_fs_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_fs_feature_map[] = {
     FEATURE_ENTRY(VHOST_F_LOG_ALL, \
             "VHOST_F_LOG_ALL: Logging write descriptors supported"),
     FEATURE_ENTRY(VHOST_USER_F_PROTOCOL_FEATURES, \
@@ -369,7 +369,7 @@ qmp_virtio_feature_map_t virtio_fs_feature_map[] = {
 };
 
 /* virtio/vhost-user-i2c features mapping */
-qmp_virtio_feature_map_t virtio_i2c_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_i2c_feature_map[] = {
     FEATURE_ENTRY(VIRTIO_I2C_F_ZERO_LENGTH_REQUEST, \
             "VIRTIO_I2C_F_ZERO_LEGNTH_REQUEST: Zero length requests supported"),
     FEATURE_ENTRY(VHOST_F_LOG_ALL, \
@@ -381,7 +381,7 @@ qmp_virtio_feature_map_t virtio_i2c_feature_map[] = {
 };
 
 /* virtio/vhost-vsock features mapping */
-qmp_virtio_feature_map_t virtio_vsock_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_vsock_feature_map[] = {
     FEATURE_ENTRY(VIRTIO_VSOCK_F_SEQPACKET, \
             "VIRTIO_VSOCK_F_SEQPACKET: SOCK_SEQPACKET supported"),
     FEATURE_ENTRY(VHOST_F_LOG_ALL, \
@@ -393,7 +393,7 @@ qmp_virtio_feature_map_t virtio_vsock_feature_map[] = {
 };
 
 /* virtio-balloon features mapping */
-qmp_virtio_feature_map_t virtio_balloon_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_balloon_feature_map[] = {
     FEATURE_ENTRY(VIRTIO_BALLOON_F_MUST_TELL_HOST, \
             "VIRTIO_BALLOON_F_MUST_TELL_HOST: Tell host before reclaiming "
             "pages"),
@@ -411,14 +411,14 @@ qmp_virtio_feature_map_t virtio_balloon_feature_map[] = {
 };
 
 /* virtio-crypto features mapping */
-qmp_virtio_feature_map_t virtio_crypto_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_crypto_feature_map[] = {
     FEATURE_ENTRY(VHOST_F_LOG_ALL, \
             "VHOST_F_LOG_ALL: Logging write descriptors supported"),
     { -1, "" }
 };
 
 /* virtio-iommu features mapping */
-qmp_virtio_feature_map_t virtio_iommu_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_iommu_feature_map[] = {
     FEATURE_ENTRY(VIRTIO_IOMMU_F_INPUT_RANGE, \
             "VIRTIO_IOMMU_F_INPUT_RANGE: Range of available virtual addrs. "
             "available"),
@@ -441,7 +441,7 @@ qmp_virtio_feature_map_t virtio_iommu_feature_map[] = {
 };
 
 /* virtio-mem features mapping */
-qmp_virtio_feature_map_t virtio_mem_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_mem_feature_map[] = {
 #ifndef CONFIG_ACPI
     FEATURE_ENTRY(VIRTIO_MEM_F_ACPI_PXM, \
             "VIRTIO_MEM_F_ACPI_PXM: node_id is an ACPI PXM and is valid"),
@@ -453,7 +453,7 @@ qmp_virtio_feature_map_t virtio_mem_feature_map[] = {
 };
 
 /* virtio-rng features mapping */
-qmp_virtio_feature_map_t virtio_rng_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_rng_feature_map[] = {
     FEATURE_ENTRY(VHOST_F_LOG_ALL, \
             "VHOST_F_LOG_ALL: Logging write descriptors supported"),
     FEATURE_ENTRY(VHOST_USER_F_PROTOCOL_FEATURES, \
Signature · 1 line
2.38.1
#4 lore raw

Re: [PATCH-for-8.0 03/10] hw/virtio: Constify qmp_virtio_feature_map_t[]

Quoted 10 lines · Philippe Mathieu-Daudé
hw/virtio/virtio.c +1 -1
@@ -161,7 +161,7 @@ static qmp_virtio_feature_map_t vhost_user_protocol_map[] = {
  };
 
  /* virtio device configuration statuses */
-static qmp_virtio_feature_map_t virtio_config_status_map[] = {
+static const qmp_virtio_feature_map_t virtio_config_status_map[] = {
      FEATURE_ENTRY(VIRTIO_CONFIG_S_DRIVER_OK, \
              "VIRTIO_CONFIG_S_DRIVER_OK: Driver setup and ready"),
      FEATURE_ENTRY(VIRTIO_CONFIG_S_FEATURES_OK, \

hw/virtio/virtio.c static qmp_virtio_feature_map_t virtio_config_status_map[] = { · line 185

Quoted 8 lines
hw/virtio/virtio.c +1 -1
@@ -179,7 +179,7 @@ static qmp_virtio_feature_map_t virtio_config_status_map[] = {
  };
 
  /* virtio-blk features mapping */
-qmp_virtio_feature_map_t virtio_blk_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_blk_feature_map[] = {
      FEATURE_ENTRY(VIRTIO_BLK_F_SIZE_MAX, \
              "VIRTIO_BLK_F_SIZE_MAX: Max segment size is size_max"),
      FEATURE_ENTRY(VIRTIO_BLK_F_SEG_MAX, \

It appears all of the ones not marked static can be? Otherwise you should have needed to adjust some header file as well.

r~

#5 lore raw

Re: [PATCH-for-8.0 03/10] hw/virtio: Constify qmp_virtio_feature_map_t[]

Quoted 13 lines · Richard Henderson
Quoted 12 lines · Philippe Mathieu-Daudé
hw/virtio/virtio.c +1 -1
@@ -161,7 +161,7 @@ static qmp_virtio_feature_map_t 
 vhost_user_protocol_map[] = {
   };
   /* virtio device configuration statuses */
-static qmp_virtio_feature_map_t virtio_config_status_map[] = {
+static const qmp_virtio_feature_map_t virtio_config_status_map[] = {
       FEATURE_ENTRY(VIRTIO_CONFIG_S_DRIVER_OK, \
               "VIRTIO_CONFIG_S_DRIVER_OK: Driver setup and ready"),
       FEATURE_ENTRY(VIRTIO_CONFIG_S_FEATURES_OK, \
@@ -179,7 +179,7 @@ static qmp_virtio_feature_map_t 
 virtio_config_status_map[] = {
   };
Quoted 8 lines

hw/virtio/virtio.c static qmp_virtio_feature_map_t virtio_config_status_map[] = { · line 185

Quoted 6 lines
hw/virtio/virtio.c +1 -1
   /* virtio-blk features mapping */
-qmp_virtio_feature_map_t virtio_blk_feature_map[] = {
+const qmp_virtio_feature_map_t virtio_blk_feature_map[] = {
       FEATURE_ENTRY(VIRTIO_BLK_F_SIZE_MAX, \
               "VIRTIO_BLK_F_SIZE_MAX: Max segment size is size_max"),
       FEATURE_ENTRY(VIRTIO_BLK_F_SEG_MAX, \

It appears all of the ones not marked static can be?

It seems some are not static to avoid "declared static but not used" warnings due to how they are conditionally used with the CONFIG_VIRTIO_xxx guards in qmp_decode_features():

    /* device features */
    switch (device_id) {

#ifdef CONFIG_VIRTIO_SERIAL
case VIRTIO_ID_CONSOLE:
features->dev_features =
CONVERT_FEATURES(strList, virtio_serial_feature_map, 0,

bitmap);
        break;
#endif
#ifdef CONFIG_VIRTIO_BLK
    case VIRTIO_ID_BLOCK:
        features->dev_features =
            CONVERT_FEATURES(strList, virtio_blk_feature_map, 0, bitmap);
        break;
#endif
#ifdef CONFIG_VIRTIO_GPU
    case VIRTIO_ID_GPU:
        features->dev_features =
            CONVERT_FEATURES(strList, virtio_gpu_feature_map, 0, bitmap);
        break;

#endif

Quoted 1 line

Otherwise you should have needed to adjust some header file as well.

OK I'll guard them with the corresponding #ifdef'ry.

#6 lore raw

PATCH 4/10 hw/virtio: Extract config read/write accessors to virtio-config.c

In reply to Philippe Mathieu-Daudé

These config helpers use the target-dependent LD/ST API.

---
 hw/virtio/meson.build     |   1 +
 hw/virtio/virtio-config.c | 204 ++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio.c        | 190 -----------------------------------
 3 files changed, 205 insertions(+), 190 deletions(-)
 create mode 100644 hw/virtio/virtio-config.c
hw/virtio/meson.build +1 -0
index 23a980efaa..097220b4b5 100644
@@ -5,6 +5,7 @@ softmmu_virtio_ss.add(when: 'CONFIG_VIRTIO_MMIO', if_true: files('virtio-mmio.c'
 
 specific_virtio_ss = ss.source_set()
 specific_virtio_ss.add(files('virtio.c'))
+specific_virtio_ss.add(files('virtio-config.c'))
 
 if have_vhost
   specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c'))
hw/virtio/virtio-config.c new file +204 -0

204 lines not on this page · raw message

hw/virtio/virtio.c +0 -190

202 lines not on this page · raw message

Signature · 1 line
2.38.1
#7 lore raw

PATCH 5/10 hw/virtio: Extract QMP related code virtio-qmp.c

In reply to Philippe Mathieu-Daudé

The monitor decoders are the only functions using the CONFIG_xxx definitions declared in the target specific CONFIG_DEVICES header.

---
 hw/virtio/meson.build  |   2 +-
 hw/virtio/virtio-qmp.c | 631 +++++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-qmp.h |  20 ++
 hw/virtio/virtio.c     | 607 +--------------------------------------
 4 files changed, 654 insertions(+), 606 deletions(-)
 create mode 100644 hw/virtio/virtio-qmp.c
 create mode 100644 hw/virtio/virtio-qmp.h
hw/virtio/meson.build +1 -1
index 097220b4b5..eb7ee8ea92 100644
@@ -5,7 +5,7 @@ softmmu_virtio_ss.add(when: 'CONFIG_VIRTIO_MMIO', if_true: files('virtio-mmio.c'
 
 specific_virtio_ss = ss.source_set()
 specific_virtio_ss.add(files('virtio.c'))
-specific_virtio_ss.add(files('virtio-config.c'))
+specific_virtio_ss.add(files('virtio-config.c', 'virtio-qmp.c'))
 
 if have_vhost
   specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c'))
hw/virtio/virtio-qmp.c new file +631 -0

631 lines not on this page · raw message

hw/virtio/virtio-qmp.h new file +20 -0
new file mode 100644
index 0000000000..075fc27030
@@ -0,0 +1,20 @@
+/*
+ * Virtio QMP helpers
+ *
+ * Copyright IBM, Corp. 2007
+ *
+ * Authors:
+ *  Anthony Liguori   <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef HW_VIRTIO_QMP_H
+#define HW_VIRTIO_QMP_H
+
+#include "qapi/qapi-types-virtio.h"
+
+VirtioDeviceStatus *qmp_decode_status(uint8_t bitmap);
+VhostDeviceProtocols *qmp_decode_protocols(uint64_t bitmap);
+VirtioDeviceFeatures *qmp_decode_features(uint16_t device_id, uint64_t bitmap);
+
+#endif
hw/virtio/virtio.c +2 -605

637 lines not on this page · raw message

Signature · 1 line
2.38.1
#8 lore raw

RFC 6/10 hw/virtio: Cache access_is_big_endian value in VirtIODevice state

In reply to Philippe Mathieu-Daudé

The device endianness doesn't change during runtime. Cache it in the VirtIODevice state.

---
RFC: I'm not sure virtio_init() is the correct place to add this
check. We want to initialize this field once the features are negociated.

---
 hw/virtio/virtio.c         | 1 +
 include/hw/virtio/virtio.h | 1 +
 2 files changed, 2 insertions(+)
hw/virtio/virtio.c +1 -0
index 09b1a0e3d9..dbb1fe33f7 100644
@@ -3193,6 +3193,7 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size)
     vdev->vmstate = qdev_add_vm_change_state_handler(DEVICE(vdev),
             virtio_vmstate_change, vdev);
     vdev->device_endian = virtio_default_endian();
+    vdev->access_is_big_endian = virtio_access_is_big_endian(vdev);
     vdev->use_guest_notifier_mask = true;
 }
 
include/hw/virtio/virtio.h +1 -0
index acfd4df125..5f28e51e93 100644
@@ -130,6 +130,7 @@ struct VirtIODevice
     bool vhost_started;
     VMChangeStateEntry *vmstate;
     char *bus_name;
+    bool access_is_big_endian;
     uint8_t device_endian;
     bool use_guest_notifier_mask;
     AddressSpace *dma_as;
Signature · 1 line
2.38.1
#9 lore raw

Re: [RFC PATCH-for-8.0 06/10] hw/virtio: Cache access_is_big_endian value in VirtIODevice state

Quoted 1 line · Philippe Mathieu-Daudé

The device endianness doesn't change during runtime.

What are you talking about? Of course it does.

I mean, it doesn't often in practice, because the Linux kernel is compiled for one endianness and doesn't keep toggling state, but the hooks that you're replacing test for the *current* endianness state of the cpu. So this is a behaviour change.

Have you considered that the bootloader and the kernel may use different endianness?

r~

#10 lore raw

Re: [RFC PATCH-for-8.0 06/10] hw/virtio: Cache access_is_big_endian value in VirtIODevice state

Quoted 4 lines · Richard Henderson
Quoted 1 line · Philippe Mathieu-Daudé

The device endianness doesn't change during runtime.

What are you talking about?  Of course it does.

The host CPU certainly does, but the virtio device doesn't... Does it?

This check only consider the device, not the CPU:

    bool virtio_access_is_big_endian(VirtIODevice *vdev)
    {
    #if defined(LEGACY_VIRTIO_IS_BIENDIAN)
        return virtio_is_big_endian(vdev);
    #elif TARGET_BIG_ENDIAN
        if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
            /*Devices conforming to VIRTIO 1.0 or later are always LE.*/
            return false;
        }
        return true;
    #else
        return false;
    #endif
    }

    static inline bool virtio_is_big_endian(VirtIODevice *vdev)
    {
        if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
            assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
            return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
        }
        /* Devices conforming to VIRTIO 1.0 or later are always LE. */
        return false;
    }

and once the features are negotiated it doesn't seem to change.

Quoted 1 line

I mean, it doesn't often in practice, because the Linux kernel is compiled for one endianness and doesn't keep toggling state, but the hooks that you're replacing test for the *current* endianness state of the cpu.  So this is a behaviour change.

I agree. Note however currently the CPU endianness is only checked once upon virtio device reset (or from a migration stream):

    void virtio_reset(void *opaque)
    {
        VirtIODevice *vdev = opaque;
        VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
        int i;

        virtio_set_status(vdev, 0);
        if (current_cpu) {
            /* Guest initiated reset */
            vdev->device_endian = virtio_current_cpu_endian();
        } else {
            /* System reset */
            vdev->device_endian = virtio_default_endian();
        }

    bool cpu_virtio_is_big_endian(CPUState *cpu)
    {
        CPUClass *cc = CPU_GET_CLASS(cpu);

        if (cc->sysemu_ops->virtio_is_big_endian) {
            return cc->sysemu_ops->virtio_is_big_endian(cpu);
        }
        return target_words_bigendian();
    }

ARM being the single arch implementing a runtime endianness check:

    static bool arm_cpu_virtio_is_big_endian(CPUState *cs)
    {
        ARMCPU *cpu = ARM_CPU(cs);
        CPUARMState *env = &cpu->env;

        cpu_synchronize_state(cs);
        return arm_cpu_data_is_big_endian(env);
    }
Quoted 1 line

Have you considered that the bootloader and the kernel may use different endianness?

Certainly, but I'll revisit the code more thoughtfully.

Thanks,

Phil.

#11 lore raw

Re: [RFC PATCH-for-8.0 06/10] hw/virtio: Cache access_is_big_endian value in VirtIODevice state

Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Richard Henderson
Quoted 1 line · Philippe Mathieu-Daudé

The device endianness doesn't change during runtime.

What are you talking about?  Of course it does.

The host CPU certainly does, but the virtio device doesn't... Does it?

This check only consider the device, not the CPU:

Quoted 10 lines

    bool virtio_access_is_big_endian(VirtIODevice *vdev)
    {
    #if defined(LEGACY_VIRTIO_IS_BIENDIAN)
        return virtio_is_big_endian(vdev);
    #elif TARGET_BIG_ENDIAN
        if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { /*Devices conforming to VIRTIO 1.0 or later are always LE.*/ return false;
        }
        return true;

Well, this part here means that the endianness can indeed change on the device side during runtime. Depending on whether VIRTIO_F_VERSION_1 is negotiated or not, the device is little or big endian. Happens on s390x for example - for legacy virtio, big endian is used, and for modern virtio, little endian is used instead.

Thomas

#12 lore raw

Re: [RFC PATCH-for-8.0 06/10] hw/virtio: Cache access_is_big_endian value in VirtIODevice state

Quoted 22 lines · Thomas Huth
Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Richard Henderson
Quoted 1 line · Philippe Mathieu-Daudé

The device endianness doesn't change during runtime.

What are you talking about?  Of course it does.

The host CPU certainly does, but the virtio device doesn't... Does it?

This check only consider the device, not the CPU:

Quoted 10 lines

     bool virtio_access_is_big_endian(VirtIODevice *vdev)
     {
     #if defined(LEGACY_VIRTIO_IS_BIENDIAN)
         return virtio_is_big_endian(vdev);
     #elif TARGET_BIG_ENDIAN
         if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { /*Devices conforming to VIRTIO 1.0 or later are always LE.*/ return false;
         }
         return true;

Quoted 1 line

Well, this part here means that the endianness can indeed change on the device side during runtime. Depending on whether VIRTIO_F_VERSION_1 is negotiated or not, the device is little or big endian. Happens on s390x for example - for legacy virtio, big endian is used, and for modern virtio, little endian is used instead.

virtio_is_big_endian() depends on vdev->device_endian which is set in:

1) virtio_init()

    void virtio_init(VirtIODevice *vdev, uint16_t device_id,
                     size_t config_size)
    {
        ....
        vdev->device_endian = virtio_default_endian();

2) virtio_load()

    int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
    {
        int i, ret;
        int32_t config_len;
        uint32_t num;
        uint32_t features;
        BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
        VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
        VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);

        /*
         * We poison the endianness to ensure it does not get
         * used before subsections have been loaded.
         */
        vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN;
        ....

        if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) {
            vdev->device_endian = virtio_default_endian();
        }

3) virtio_reset()

    void virtio_reset(void *opaque)
    {
        VirtIODevice *vdev = opaque;
        VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
        int i;

        virtio_set_status(vdev, 0);
        if (current_cpu) {
            /* Guest initiated reset */
            vdev->device_endian = virtio_current_cpu_endian();
        } else {
            /* System reset */
            vdev->device_endian = virtio_default_endian();
        }

So the current patch is not complete and should be:

-- >8 --

hw/virtio/virtio.c +3 -1
index 09b1a0e3d9..b02b9058f9 100644
@@ -2124,6 +2124,7 @@ void virtio_reset(void *opaque)
         /* System reset */
         vdev->device_endian = virtio_default_endian();
     }
+    vdev->access_is_big_endian = virtio_access_is_big_endian(vdev);
 
     if (k->reset) {
         k->reset(vdev);
@@ -3018,6 +3019,7 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
 
     if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) {
         vdev->device_endian = virtio_default_endian();
+        vdev->access_is_big_endian = virtio_access_is_big_endian(vdev);
     }
 
     if (virtio_64bit_features_needed(vdev)) {
@@ -3193,6 +3195,7 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size)
     vdev->vmstate = qdev_add_vm_change_state_handler(DEVICE(vdev),
             virtio_vmstate_change, vdev);
     vdev->device_endian = virtio_default_endian();
+    vdev->access_is_big_endian = virtio_access_is_big_endian(vdev);
     vdev->use_guest_notifier_mask = true;
 }
---

Still, the result of virtio_access_is_big_endian() doesn't depend on the CPU endianness in my analysis... What am I missing?

Thanks,

Phil.

#13 lore raw

Re: [RFC PATCH-for-8.0 06/10] hw/virtio: Cache access_is_big_endian value in VirtIODevice state

Quoted 66 lines · Philippe Mathieu-Daudé
Quoted 22 lines · Thomas Huth
Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Richard Henderson
Quoted 1 line · Philippe Mathieu-Daudé

The device endianness doesn't change during runtime.

What are you talking about?  Of course it does.

The host CPU certainly does, but the virtio device doesn't... Does it?

This check only consider the device, not the CPU:

Quoted 10 lines

     bool virtio_access_is_big_endian(VirtIODevice *vdev)
     {
     #if defined(LEGACY_VIRTIO_IS_BIENDIAN)
         return virtio_is_big_endian(vdev);
     #elif TARGET_BIG_ENDIAN
         if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { /*Devices conforming to VIRTIO 1.0 or later are always LE.*/ return false;
         }
         return true;

Quoted 1 line

Well, this part here means that the endianness can indeed change on the device side during runtime. Depending on whether VIRTIO_F_VERSION_1 is negotiated or not, the device is little or big endian. Happens on s390x for example - for legacy virtio, big endian is used, and for modern virtio, little endian is used instead.

virtio_is_big_endian() depends on vdev->device_endian which is set in:

1) virtio_init()

    void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size)
    {
        ....
        vdev->device_endian = virtio_default_endian();

2) virtio_load()

    int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) {
        int i, ret;
        int32_t config_len;
        uint32_t num;
        uint32_t features;
        BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);

        /*
         * We poison the endianness to ensure it does not get
         * used before subsections have been loaded. */
        vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN;
        ....

        if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) { vdev->device_endian = virtio_default_endian(); }

3) virtio_reset()

    void virtio_reset(void *opaque)
    {
        VirtIODevice *vdev = opaque; VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); int i;

Quoted 4 lines

        virtio_set_status(vdev, 0);
        if (current_cpu) {
            /* Guest initiated reset */ vdev->device_endian = virtio_current_cpu_endian();

This is where the virtio endianness depends on the CPU endianness. Looks like it is fortunately only taken into account after a device reset, and not for every access (as I originally thought).

Thomas

#14 lore raw

Re: [RFC PATCH-for-8.0 06/10] hw/virtio: Cache access_is_big_endian value in VirtIODevice state

In reply to Philippe Mathieu-Daudé
Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Richard Henderson
Quoted 1 line · Philippe Mathieu-Daudé

The device endianness doesn't change during runtime.

What are you talking about?  Of course it does.

The host CPU certainly does, but the virtio device doesn't... Does it?

This check only consider the device, not the CPU:

    bool virtio_access_is_big_endian(VirtIODevice *vdev)
    {
    #if defined(LEGACY_VIRTIO_IS_BIENDIAN)
        return virtio_is_big_endian(vdev);
    #elif TARGET_BIG_ENDIAN
        if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { /*Devices conforming to VIRTIO 1.0 or later are always LE.*/ return false;
        }
        return true;
    #else
        return false;
    #endif
    }

    static inline bool virtio_is_big_endian(VirtIODevice *vdev) {
        if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN); return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG; }
        /* Devices conforming to VIRTIO 1.0 or later are always LE. */ return false;
    }

Quoted 1 line

and once the features are negotiated it doesn't seem to change.

Per the spec, if the device changes its endianness, it must set the VIRTIO_CONFIG_S_NEEDS_RESET bit:

3.2.1 Notification of Device Configuration Changes

For devices where the device-specific configuration information can be changed, a configuration change notification is sent when a device-specific configuration change occurs.
In addition, this notification is triggered by the device setting DEVICE_NEEDS_RESET

However QEMU doesn't read this bit, only sets it:

hw/virtio/virtio.c:3551: vdev->status = vdev->status | VIRTIO_CONFIG_S_NEEDS_RESET;
include/standard-headers/linux/virtio_config.h:44:#define VIRTIO_CONFIG_S_NEEDS_RESET 0x40

Quoted 32 lines
Quoted 1 line

I mean, it doesn't often in practice, because the Linux kernel is compiled for one endianness and doesn't keep toggling state, but the hooks that you're replacing test for the *current* endianness state of the cpu.  So this is a behaviour change.

I agree. Note however currently the CPU endianness is only checked once upon virtio device reset (or from a migration stream):

    void virtio_reset(void *opaque)
    {
        VirtIODevice *vdev = opaque; VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); int i;

        virtio_set_status(vdev, 0);
        if (current_cpu) {
            /* Guest initiated reset */ vdev->device_endian = virtio_current_cpu_endian(); } else {
            /* System reset */
            vdev->device_endian = virtio_default_endian(); }

    bool cpu_virtio_is_big_endian(CPUState *cpu)
    {
        CPUClass *cc = CPU_GET_CLASS(cpu);

        if (cc->sysemu_ops->virtio_is_big_endian) { return cc->sysemu_ops->virtio_is_big_endian(cpu); }
        return target_words_bigendian();
    }

ARM being the single arch implementing a runtime endianness check:

Quoted 8 lines

    static bool arm_cpu_virtio_is_big_endian(CPUState *cs) {
        ARMCPU *cpu = ARM_CPU(cs);
        CPUARMState *env = &cpu->env;

        cpu_synchronize_state(cs);
        return arm_cpu_data_is_big_endian(env);
    }

virtio_reset() is only called by virtio_bus_reset().

$ git grep -w virtio_bus_reset
hw/s390x/virtio-ccw.c:256:    virtio_bus_reset(&dev->bus);
hw/virtio/virtio-bus.c:102:void virtio_bus_reset(VirtioBusState *bus)
hw/virtio/virtio-mmio.c:75:    virtio_bus_reset(&proxy->bus);
hw/virtio/virtio-pci.c:1998:    virtio_bus_reset(bus);

So the result of virtio_access_is_big_endian() is only updated there, after a virtio_bus_reset() call, and IIUC isn't dependent on the CPU endianness state, thus can be cached in VirtIODevice. But maybe the current implementation is incomplete and my change is simply making it worst...

#15 lore raw

Re: [RFC PATCH-for-8.0 06/10] hw/virtio: Cache access_is_big_endian value in VirtIODevice state

In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Richard Henderson
Quoted 1 line · Philippe Mathieu-Daudé

The device endianness doesn't change during runtime.

What are you talking about?  Of course it does.

Quoted 8 lines

The host CPU certainly does, but the virtio device doesn't... Does it?

This check only consider the device, not the CPU:

    bool virtio_access_is_big_endian(VirtIODevice *vdev) {
    #if defined(LEGACY_VIRTIO_IS_BIENDIAN) return virtio_is_big_endian(vdev);

This is set for both ARM and PPC, which checks current cpu endianness in both cases.

Quoted 1 line

and once the features are negotiated it doesn't seem to change.

Incorrect.

r~

#16 lore raw

RFC 7/10 hw/virtio: Directly access cached VirtIODevice::access_is_big_endian

In reply to Philippe Mathieu-Daudé

Since the device endianness doesn't change at runtime, use the cached value instead of evaluating it on each call.

---
 include/hw/virtio/virtio-access.h | 44 +++++++++++++++----------------
 1 file changed, 22 insertions(+), 22 deletions(-)
include/hw/virtio/virtio-access.h +22 -22
index 07aae69042..985f39fe16 100644
@@ -43,7 +43,7 @@ static inline uint16_t virtio_lduw_phys(VirtIODevice *vdev, hwaddr pa)
 {
     AddressSpace *dma_as = vdev->dma_as;
 
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return lduw_be_phys(dma_as, pa);
     }
     return lduw_le_phys(dma_as, pa);
@@ -53,7 +53,7 @@ static inline uint32_t virtio_ldl_phys(VirtIODevice *vdev, hwaddr pa)
 {
     AddressSpace *dma_as = vdev->dma_as;
 
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldl_be_phys(dma_as, pa);
     }
     return ldl_le_phys(dma_as, pa);
@@ -63,7 +63,7 @@ static inline uint64_t virtio_ldq_phys(VirtIODevice *vdev, hwaddr pa)
 {
     AddressSpace *dma_as = vdev->dma_as;
 
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldq_be_phys(dma_as, pa);
     }
     return ldq_le_phys(dma_as, pa);
@@ -74,7 +74,7 @@ static inline void virtio_stw_phys(VirtIODevice *vdev, hwaddr pa,
 {
     AddressSpace *dma_as = vdev->dma_as;
 
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stw_be_phys(dma_as, pa, value);
     } else {
         stw_le_phys(dma_as, pa, value);
@@ -86,7 +86,7 @@ static inline void virtio_stl_phys(VirtIODevice *vdev, hwaddr pa,
 {
     AddressSpace *dma_as = vdev->dma_as;
 
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stl_be_phys(dma_as, pa, value);
     } else {
         stl_le_phys(dma_as, pa, value);
@@ -95,7 +95,7 @@ static inline void virtio_stl_phys(VirtIODevice *vdev, hwaddr pa,
 
 static inline void virtio_stw_p(VirtIODevice *vdev, void *ptr, uint16_t v)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stw_be_p(ptr, v);
     } else {
         stw_le_p(ptr, v);
@@ -104,7 +104,7 @@ static inline void virtio_stw_p(VirtIODevice *vdev, void *ptr, uint16_t v)
 
 static inline void virtio_stl_p(VirtIODevice *vdev, void *ptr, uint32_t v)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stl_be_p(ptr, v);
     } else {
         stl_le_p(ptr, v);
@@ -113,7 +113,7 @@ static inline void virtio_stl_p(VirtIODevice *vdev, void *ptr, uint32_t v)
 
 static inline void virtio_stq_p(VirtIODevice *vdev, void *ptr, uint64_t v)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stq_be_p(ptr, v);
     } else {
         stq_le_p(ptr, v);
@@ -122,7 +122,7 @@ static inline void virtio_stq_p(VirtIODevice *vdev, void *ptr, uint64_t v)
 
 static inline int virtio_lduw_p(VirtIODevice *vdev, const void *ptr)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return lduw_be_p(ptr);
     } else {
         return lduw_le_p(ptr);
@@ -131,7 +131,7 @@ static inline int virtio_lduw_p(VirtIODevice *vdev, const void *ptr)
 
 static inline int virtio_ldl_p(VirtIODevice *vdev, const void *ptr)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldl_be_p(ptr);
     } else {
         return ldl_le_p(ptr);
@@ -140,7 +140,7 @@ static inline int virtio_ldl_p(VirtIODevice *vdev, const void *ptr)
 
 static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldq_be_p(ptr);
     } else {
         return ldq_le_p(ptr);
@@ -150,9 +150,9 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr)
 static inline uint16_t virtio_tswap16(VirtIODevice *vdev, uint16_t s)
 {
 #if HOST_BIG_ENDIAN
-    return virtio_access_is_big_endian(vdev) ? s : bswap16(s);
+    return vdev->access_is_big_endian ? s : bswap16(s);
 #else
-    return virtio_access_is_big_endian(vdev) ? bswap16(s) : s;
+    return vdev->access_is_big_endian ? bswap16(s) : s;
 #endif
 }
 
@@ -160,7 +160,7 @@ static inline uint16_t virtio_lduw_phys_cached(VirtIODevice *vdev,
                                                MemoryRegionCache *cache,
                                                hwaddr pa)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return lduw_be_phys_cached(cache, pa);
     }
     return lduw_le_phys_cached(cache, pa);
@@ -170,7 +170,7 @@ static inline uint32_t virtio_ldl_phys_cached(VirtIODevice *vdev,
                                               MemoryRegionCache *cache,
                                               hwaddr pa)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldl_be_phys_cached(cache, pa);
     }
     return ldl_le_phys_cached(cache, pa);
@@ -180,7 +180,7 @@ static inline uint64_t virtio_ldq_phys_cached(VirtIODevice *vdev,
                                               MemoryRegionCache *cache,
                                               hwaddr pa)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldq_be_phys_cached(cache, pa);
     }
     return ldq_le_phys_cached(cache, pa);
@@ -190,7 +190,7 @@ static inline void virtio_stw_phys_cached(VirtIODevice *vdev,
                                           MemoryRegionCache *cache,
                                           hwaddr pa, uint16_t value)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stw_be_phys_cached(cache, pa, value);
     } else {
         stw_le_phys_cached(cache, pa, value);
@@ -201,7 +201,7 @@ static inline void virtio_stl_phys_cached(VirtIODevice *vdev,
                                           MemoryRegionCache *cache,
                                           hwaddr pa, uint32_t value)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stl_be_phys_cached(cache, pa, value);
     } else {
         stl_le_phys_cached(cache, pa, value);
@@ -216,9 +216,9 @@ static inline void virtio_tswap16s(VirtIODevice *vdev, uint16_t *s)
 static inline uint32_t virtio_tswap32(VirtIODevice *vdev, uint32_t s)
 {
 #if HOST_BIG_ENDIAN
-    return virtio_access_is_big_endian(vdev) ? s : bswap32(s);
+    return vdev->access_is_big_endian ? s : bswap32(s);
 #else
-    return virtio_access_is_big_endian(vdev) ? bswap32(s) : s;
+    return vdev->access_is_big_endian ? bswap32(s) : s;
 #endif
 }
 
@@ -230,9 +230,9 @@ static inline void virtio_tswap32s(VirtIODevice *vdev, uint32_t *s)
 static inline uint64_t virtio_tswap64(VirtIODevice *vdev, uint64_t s)
 {
 #if HOST_BIG_ENDIAN
-    return virtio_access_is_big_endian(vdev) ? s : bswap64(s);
+    return vdev->access_is_big_endian ? s : bswap64(s);
 #else
-    return virtio_access_is_big_endian(vdev) ? bswap64(s) : s;
+    return vdev->access_is_big_endian ? bswap64(s) : s;
 #endif
 }
 
Signature · 1 line
2.38.1
#17 lore raw

Re: [RFC PATCH-for-8.0 07/10] hw/virtio: Directly access cached VirtIODevice::access_is_big_endian

Quoted 13 lines · On Tue, 13 Dec 2022 00:05:14 +0100 Philippe Mathieu-Daudé <[email protected]>…

Since the device endianness doesn't change at runtime, use the cached value instead of evaluating it on each call.

---
 include/hw/virtio/virtio-access.h | 44 +++++++++++++++----------------
 1 file changed, 22 insertions(+), 22 deletions(-)
include/hw/virtio/virtio-access.h +0 -0
index 07aae69042..985f39fe16 100644

include/hw/virtio/virtio-access.h static inline uint16_t virtio_lduw_phys(VirtIODevice *vdev, hwaddr pa) · line 46

Quoted 5 lines
include/hw/virtio/virtio-access.h +1 -1
@@ -43,7 +43,7 @@ static inline uint16_t virtio_lduw_phys(VirtIODevice *vdev, hwaddr pa)
 {
     AddressSpace *dma_as = vdev->dma_as;
 
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {

For x86, virtio_access_is_big_endian() expands to :

static inline bool virtio_access_is_big_endian(VirtIODevice *vdev)

{
    return false;
}

When I added these memory accessors, there was a strong requirement from MST that x86 shouldn't have to pay for an out-of-line check when it is known that everything is always little endian. Not sure exactly what you're trying to achieve with VirtIODevice::access_is_big_endian but this shouldn't mess with this fast path.

Quoted 174 lines
include/hw/virtio/virtio-access.h +21 -21
         return lduw_be_phys(dma_as, pa);
     }
     return lduw_le_phys(dma_as, pa);
@@ -53,7 +53,7 @@ static inline uint32_t virtio_ldl_phys(VirtIODevice *vdev, hwaddr pa)
 {
     AddressSpace *dma_as = vdev->dma_as;
 
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldl_be_phys(dma_as, pa);
     }
     return ldl_le_phys(dma_as, pa);
@@ -63,7 +63,7 @@ static inline uint64_t virtio_ldq_phys(VirtIODevice *vdev, hwaddr pa)
 {
     AddressSpace *dma_as = vdev->dma_as;
 
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldq_be_phys(dma_as, pa);
     }
     return ldq_le_phys(dma_as, pa);
@@ -74,7 +74,7 @@ static inline void virtio_stw_phys(VirtIODevice *vdev, hwaddr pa,
 {
     AddressSpace *dma_as = vdev->dma_as;
 
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stw_be_phys(dma_as, pa, value);
     } else {
         stw_le_phys(dma_as, pa, value);
@@ -86,7 +86,7 @@ static inline void virtio_stl_phys(VirtIODevice *vdev, hwaddr pa,
 {
     AddressSpace *dma_as = vdev->dma_as;
 
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stl_be_phys(dma_as, pa, value);
     } else {
         stl_le_phys(dma_as, pa, value);
@@ -95,7 +95,7 @@ static inline void virtio_stl_phys(VirtIODevice *vdev, hwaddr pa,
 
 static inline void virtio_stw_p(VirtIODevice *vdev, void *ptr, uint16_t v)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stw_be_p(ptr, v);
     } else {
         stw_le_p(ptr, v);
@@ -104,7 +104,7 @@ static inline void virtio_stw_p(VirtIODevice *vdev, void *ptr, uint16_t v)
 
 static inline void virtio_stl_p(VirtIODevice *vdev, void *ptr, uint32_t v)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stl_be_p(ptr, v);
     } else {
         stl_le_p(ptr, v);
@@ -113,7 +113,7 @@ static inline void virtio_stl_p(VirtIODevice *vdev, void *ptr, uint32_t v)
 
 static inline void virtio_stq_p(VirtIODevice *vdev, void *ptr, uint64_t v)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stq_be_p(ptr, v);
     } else {
         stq_le_p(ptr, v);
@@ -122,7 +122,7 @@ static inline void virtio_stq_p(VirtIODevice *vdev, void *ptr, uint64_t v)
 
 static inline int virtio_lduw_p(VirtIODevice *vdev, const void *ptr)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return lduw_be_p(ptr);
     } else {
         return lduw_le_p(ptr);
@@ -131,7 +131,7 @@ static inline int virtio_lduw_p(VirtIODevice *vdev, const void *ptr)
 
 static inline int virtio_ldl_p(VirtIODevice *vdev, const void *ptr)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldl_be_p(ptr);
     } else {
         return ldl_le_p(ptr);
@@ -140,7 +140,7 @@ static inline int virtio_ldl_p(VirtIODevice *vdev, const void *ptr)
 
 static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldq_be_p(ptr);
     } else {
         return ldq_le_p(ptr);
@@ -150,9 +150,9 @@ static inline uint64_t virtio_ldq_p(VirtIODevice *vdev, const void *ptr)
 static inline uint16_t virtio_tswap16(VirtIODevice *vdev, uint16_t s)
 {
 #if HOST_BIG_ENDIAN
-    return virtio_access_is_big_endian(vdev) ? s : bswap16(s);
+    return vdev->access_is_big_endian ? s : bswap16(s);
 #else
-    return virtio_access_is_big_endian(vdev) ? bswap16(s) : s;
+    return vdev->access_is_big_endian ? bswap16(s) : s;
 #endif
 }
 
@@ -160,7 +160,7 @@ static inline uint16_t virtio_lduw_phys_cached(VirtIODevice *vdev,
                                                MemoryRegionCache *cache,
                                                hwaddr pa)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return lduw_be_phys_cached(cache, pa);
     }
     return lduw_le_phys_cached(cache, pa);
@@ -170,7 +170,7 @@ static inline uint32_t virtio_ldl_phys_cached(VirtIODevice *vdev,
                                               MemoryRegionCache *cache,
                                               hwaddr pa)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldl_be_phys_cached(cache, pa);
     }
     return ldl_le_phys_cached(cache, pa);
@@ -180,7 +180,7 @@ static inline uint64_t virtio_ldq_phys_cached(VirtIODevice *vdev,
                                               MemoryRegionCache *cache,
                                               hwaddr pa)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         return ldq_be_phys_cached(cache, pa);
     }
     return ldq_le_phys_cached(cache, pa);
@@ -190,7 +190,7 @@ static inline void virtio_stw_phys_cached(VirtIODevice *vdev,
                                           MemoryRegionCache *cache,
                                           hwaddr pa, uint16_t value)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stw_be_phys_cached(cache, pa, value);
     } else {
         stw_le_phys_cached(cache, pa, value);
@@ -201,7 +201,7 @@ static inline void virtio_stl_phys_cached(VirtIODevice *vdev,
                                           MemoryRegionCache *cache,
                                           hwaddr pa, uint32_t value)
 {
-    if (virtio_access_is_big_endian(vdev)) {
+    if (vdev->access_is_big_endian) {
         stl_be_phys_cached(cache, pa, value);
     } else {
         stl_le_phys_cached(cache, pa, value);
@@ -216,9 +216,9 @@ static inline void virtio_tswap16s(VirtIODevice *vdev, uint16_t *s)
 static inline uint32_t virtio_tswap32(VirtIODevice *vdev, uint32_t s)
 {
 #if HOST_BIG_ENDIAN
-    return virtio_access_is_big_endian(vdev) ? s : bswap32(s);
+    return vdev->access_is_big_endian ? s : bswap32(s);
 #else
-    return virtio_access_is_big_endian(vdev) ? bswap32(s) : s;
+    return vdev->access_is_big_endian ? bswap32(s) : s;
 #endif
 }
 
@@ -230,9 +230,9 @@ static inline void virtio_tswap32s(VirtIODevice *vdev, uint32_t *s)
 static inline uint64_t virtio_tswap64(VirtIODevice *vdev, uint64_t s)
 {
 #if HOST_BIG_ENDIAN
-    return virtio_access_is_big_endian(vdev) ? s : bswap64(s);
+    return vdev->access_is_big_endian ? s : bswap64(s);
 #else
-    return virtio_access_is_big_endian(vdev) ? bswap64(s) : s;
+    return vdev->access_is_big_endian ? bswap64(s) : s;
 #endif
 }
 
#18 lore raw

PATCH 8/10 hw/virtio: Un-inline virtio_access_is_big_endian()

In reply to Philippe Mathieu-Daudé

In order to avoid target-specific code in VirtIO headers, move this particular function -- which is only called once in virtio_init() -- in its own unit.

---
 hw/virtio/virtio-config.c         | 20 ++++++++++++++++++++
 include/hw/virtio/virtio-access.h | 19 +------------------
 2 files changed, 21 insertions(+), 18 deletions(-)
hw/virtio/virtio-config.c +20 -0
index ad78e0b9bc..aca6ef5e1b 100644
@@ -11,8 +11,28 @@
 
 #include "qemu/osdep.h"
 #include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-access.h"
 #include "cpu.h"
 
+#if defined(TARGET_PPC64) || defined(TARGET_ARM)
+#define LEGACY_VIRTIO_IS_BIENDIAN 1
+#endif
+
+bool virtio_access_is_big_endian(VirtIODevice *vdev)
+{
+#if defined(LEGACY_VIRTIO_IS_BIENDIAN)
+    return virtio_is_big_endian(vdev);
+#elif TARGET_BIG_ENDIAN
+    if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
+        /* Devices conforming to VIRTIO 1.0 or later are always LE. */
+        return false;
+    }
+    return true;
+#else
+    return false;
+#endif
+}
+
 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
 {
     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
include/hw/virtio/virtio-access.h +1 -18
index 985f39fe16..7229088b7c 100644
@@ -20,24 +20,7 @@
 #include "hw/virtio/virtio.h"
 #include "hw/virtio/virtio-bus.h"
 
-#if defined(TARGET_PPC64) || defined(TARGET_ARM)
-#define LEGACY_VIRTIO_IS_BIENDIAN 1
-#endif
-
-static inline bool virtio_access_is_big_endian(VirtIODevice *vdev)
-{
-#if defined(LEGACY_VIRTIO_IS_BIENDIAN)
-    return virtio_is_big_endian(vdev);
-#elif TARGET_BIG_ENDIAN
-    if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
-        /* Devices conforming to VIRTIO 1.0 or later are always LE. */
-        return false;
-    }
-    return true;
-#else
-    return false;
-#endif
-}
+bool virtio_access_is_big_endian(VirtIODevice *vdev);
 
 static inline uint16_t virtio_lduw_phys(VirtIODevice *vdev, hwaddr pa)
 {
Signature · 1 line
2.38.1
#19 lore raw

RFC 9/10 hw/virtio: Extract vhost_user_ram_slots_max() to vhost-user-target.c

In reply to Philippe Mathieu-Daudé

The current definition of VHOST_USER_MAX_RAM_SLOTS is target specific. By converting this definition to a runtime vhost_user_ram_slots_max() helper declared in a target specific unit, we can have the rest of vhost-user.c target independent.

To avoid variable length array or using the heap to store arrays of vhost_user_ram_slots_max() elements, we simply declare an array of the biggest VHOST_USER_MAX_RAM_SLOTS, and each target uses up to vhost_user_ram_slots_max() elements of it. Ensure arrays are big enough by adding an assertion in vhost_user_init().

---
RFC: Should I add VHOST_USER_MAX_RAM_SLOTS to vhost-user.h
or create an internal header for it?

---
 hw/virtio/meson.build          |  1 +
 hw/virtio/vhost-user-target.c  | 29 +++++++++++++++++++++++++++++
 hw/virtio/vhost-user.c         | 26 +++++---------------------
 include/hw/virtio/vhost-user.h |  7 +++++++
 4 files changed, 42 insertions(+), 21 deletions(-)
 create mode 100644 hw/virtio/vhost-user-target.c
hw/virtio/meson.build +1 -0
index eb7ee8ea92..bf7e35fa8a 100644
@@ -11,6 +11,7 @@ if have_vhost
   specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c'))
   if have_vhost_user
     specific_virtio_ss.add(files('vhost-user.c'))
+    specific_virtio_ss.add(files('vhost-user-target.c'))
   endif
   if have_vhost_vdpa
     specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-virtqueue.c'))
hw/virtio/vhost-user-target.c new file +29 -0
new file mode 100644
index 0000000000..6a0d0f53d0
@@ -0,0 +1,29 @@
+/*
+ * vhost-user target-specific helpers
+ *
+ * Copyright (c) 2013 Virtual Open Systems Sarl.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/virtio/vhost-user.h"
+
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+#include "hw/acpi/acpi.h"
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+#include "hw/ppc/spapr.h"
+#endif
+
+unsigned int vhost_user_ram_slots_max(void)
+{
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+    return ACPI_MAX_RAM_SLOTS;
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+    return SPAPR_MAX_RAM_SLOTS;
+#else
+    return 512;
+#endif
+}
hw/virtio/vhost-user.c +5 -21
index 8f635844af..21fc176725 100644
@@ -41,24 +41,7 @@
 #define VHOST_MEMORY_BASELINE_NREGIONS    8
 #define VHOST_USER_F_PROTOCOL_FEATURES 30
 #define VHOST_USER_SLAVE_MAX_FDS     8
-
-/*
- * Set maximum number of RAM slots supported to
- * the maximum number supported by the target
- * hardware plaform.
- */
-#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
-    defined(TARGET_ARM) || defined(TARGET_ARM_64)
-#include "hw/acpi/acpi.h"
-#define VHOST_USER_MAX_RAM_SLOTS ACPI_MAX_RAM_SLOTS
-
-#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
-#include "hw/ppc/spapr.h"
-#define VHOST_USER_MAX_RAM_SLOTS SPAPR_MAX_RAM_SLOTS
-
-#else
 #define VHOST_USER_MAX_RAM_SLOTS 512
-#endif
 
 /*
  * Maximum size of virtio device config space
@@ -935,7 +918,7 @@ static int vhost_user_add_remove_regions(struct vhost_dev *dev,
 
     if (track_ramblocks) {
         memcpy(u->postcopy_client_bases, shadow_pcb,
-               sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
+               sizeof(uint64_t) * vhost_user_ram_slots_max());
         /*
          * Now we've registered this with the postcopy code, we ack to the
          * client, because now we're in the position to be able to deal with
@@ -956,7 +939,7 @@ static int vhost_user_add_remove_regions(struct vhost_dev *dev,
 err:
     if (track_ramblocks) {
         memcpy(u->postcopy_client_bases, shadow_pcb,
-               sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
+               sizeof(uint64_t) * vhost_user_ram_slots_max());
     }
 
     return ret;
@@ -1030,7 +1013,7 @@ static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev,
         }
 
         memset(u->postcopy_client_bases, 0,
-               sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
+               sizeof(uint64_t) * vhost_user_ram_slots_max());
 
         /*
          * They're in the same order as the regions that were sent
@@ -2169,7 +2152,7 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque,
                 return -EINVAL;
             }
 
-            u->user->memory_slots = MIN(ram_slots, VHOST_USER_MAX_RAM_SLOTS);
+            u->user->memory_slots = MIN(ram_slots, vhost_user_ram_slots_max());
         }
     }
 
@@ -2649,6 +2632,7 @@ static void vhost_user_state_destroy(gpointer data)
 
 bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
 {
+    assert(vhost_user_ram_slots_max() <= VHOST_USER_MAX_RAM_SLOTS);
     if (user->chr) {
         error_setg(errp, "Cannot initialize vhost-user state");
         return false;
include/hw/virtio/vhost-user.h +7 -0
index 191216a74f..e13584ade8 100644
@@ -86,4 +86,11 @@ void vhost_user_async_close(DeviceState *d,
                             CharBackend *chardev, struct vhost_dev *vhost,
                             vu_async_close_fn cb);
 
+/**
+ * vhost_user_ram_slots_max()
+ *
+ * Return: maximum number of RAM slots supported by the target hardware plaform.
+ */
+unsigned int vhost_user_ram_slots_max(void);
+
 #endif
Signature · 1 line
2.38.1
#20 lore raw

Re: [RFC PATCH-for-8.0 09/10] hw/virtio: Extract vhost_user_ram_slots_max() to vhost-user-target.c

Hi Pierrick,

Quoted 60 lines · Philippe Mathieu-Daudé

The current definition of VHOST_USER_MAX_RAM_SLOTS is target specific. By converting this definition to a runtime vhost_user_ram_slots_max() helper declared in a target specific unit, we can have the rest of vhost-user.c target independent.

To avoid variable length array or using the heap to store arrays of vhost_user_ram_slots_max() elements, we simply declare an array of the biggest VHOST_USER_MAX_RAM_SLOTS, and each target uses up to vhost_user_ram_slots_max() elements of it. Ensure arrays are big enough by adding an assertion in vhost_user_init().

---
RFC: Should I add VHOST_USER_MAX_RAM_SLOTS to vhost-user.h
or create an internal header for it?

---
  hw/virtio/meson.build          |  1 +
  hw/virtio/vhost-user-target.c  | 29 +++++++++++++++++++++++++++++
  hw/virtio/vhost-user.c         | 26 +++++---------------------
  include/hw/virtio/vhost-user.h |  7 +++++++
  4 files changed, 42 insertions(+), 21 deletions(-)

create mode 100644 hw/virtio/vhost-user-target.c

hw/virtio/meson.build +1 -0
index eb7ee8ea92..bf7e35fa8a 100644
@@ -11,6 +11,7 @@ if have_vhost
    specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c'))
    if have_vhost_user
      specific_virtio_ss.add(files('vhost-user.c'))
+    specific_virtio_ss.add(files('vhost-user-target.c'))
    endif
    if have_vhost_vdpa
      specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-virtqueue.c'))
hw/virtio/vhost-user-target.c new file +17 -0
new file mode 100644
index 0000000000..6a0d0f53d0
@@ -0,0 +1,29 @@
+/*
+ * vhost-user target-specific helpers
+ *
+ * Copyright (c) 2013 Virtual Open Systems Sarl.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/virtio/vhost-user.h"
+
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+#include "hw/acpi/acpi.h"
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+#include "hw/ppc/spapr.h"
+#endif

hw/virtio/vhost-user-target.c line 27

Quoted 10 lines
hw/virtio/vhost-user-target.c new file +10 -0
+
+unsigned int vhost_user_ram_slots_max(void)
+{
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+    return ACPI_MAX_RAM_SLOTS;
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+    return SPAPR_MAX_RAM_SLOTS;
+#else
+    return 512;

Should vhost_user_ram_slots_max be another TargetInfo field?

Quoted 91 lines
hw/virtio/vhost-user-target.c new file +2 -0
+#endif
+}
hw/virtio/vhost-user.c +5 -21
index 8f635844af..21fc176725 100644
@@ -41,24 +41,7 @@
  #define VHOST_MEMORY_BASELINE_NREGIONS    8
  #define VHOST_USER_F_PROTOCOL_FEATURES 30
  #define VHOST_USER_SLAVE_MAX_FDS     8
-
-/*
- * Set maximum number of RAM slots supported to
- * the maximum number supported by the target
- * hardware plaform.
- */
-#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
-    defined(TARGET_ARM) || defined(TARGET_ARM_64)
-#include "hw/acpi/acpi.h"
-#define VHOST_USER_MAX_RAM_SLOTS ACPI_MAX_RAM_SLOTS
-
-#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
-#include "hw/ppc/spapr.h"
-#define VHOST_USER_MAX_RAM_SLOTS SPAPR_MAX_RAM_SLOTS
-
-#else
  #define VHOST_USER_MAX_RAM_SLOTS 512
-#endif
 
  /*
   * Maximum size of virtio device config space
@@ -935,7 +918,7 @@ static int vhost_user_add_remove_regions(struct vhost_dev *dev,
 
      if (track_ramblocks) {
          memcpy(u->postcopy_client_bases, shadow_pcb,
-               sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
+               sizeof(uint64_t) * vhost_user_ram_slots_max());
          /*
           * Now we've registered this with the postcopy code, we ack to the
           * client, because now we're in the position to be able to deal with
@@ -956,7 +939,7 @@ static int vhost_user_add_remove_regions(struct vhost_dev *dev,
  err:
      if (track_ramblocks) {
          memcpy(u->postcopy_client_bases, shadow_pcb,
-               sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
+               sizeof(uint64_t) * vhost_user_ram_slots_max());
      }
 
      return ret;
@@ -1030,7 +1013,7 @@ static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev,
          }
 
          memset(u->postcopy_client_bases, 0,
-               sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
+               sizeof(uint64_t) * vhost_user_ram_slots_max());
 
          /*
           * They're in the same order as the regions that were sent
@@ -2169,7 +2152,7 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque,
                  return -EINVAL;
              }
 
-            u->user->memory_slots = MIN(ram_slots, VHOST_USER_MAX_RAM_SLOTS);
+            u->user->memory_slots = MIN(ram_slots, vhost_user_ram_slots_max());
          }
      }
 
@@ -2649,6 +2632,7 @@ static void vhost_user_state_destroy(gpointer data)
 
  bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
  {
+    assert(vhost_user_ram_slots_max() <= VHOST_USER_MAX_RAM_SLOTS);
      if (user->chr) {
          error_setg(errp, "Cannot initialize vhost-user state");
          return false;
include/hw/virtio/vhost-user.h +7 -0
index 191216a74f..e13584ade8 100644
@@ -86,4 +86,11 @@ void vhost_user_async_close(DeviceState *d,
                              CharBackend *chardev, struct vhost_dev *vhost,
                              vu_async_close_fn cb);
 
+/**
+ * vhost_user_ram_slots_max()
+ *
+ * Return: maximum number of RAM slots supported by the target hardware plaform.
+ */
+unsigned int vhost_user_ram_slots_max(void);
+
  #endif
#21 lore raw

Re: [RFC PATCH-for-8.0 09/10] hw/virtio: Extract vhost_user_ram_slots_max() to vhost-user-target.c

Quoted 67 lines · Philippe Mathieu-Daudé

Hi Pierrick,

Quoted 64 lines · Philippe Mathieu-Daudé

The current definition of VHOST_USER_MAX_RAM_SLOTS is target specific. By converting this definition to a runtime vhost_user_ram_slots_max() helper declared in a target specific unit, we can have the rest of vhost-user.c target independent.

To avoid variable length array or using the heap to store arrays of vhost_user_ram_slots_max() elements, we simply declare an array of the biggest VHOST_USER_MAX_RAM_SLOTS, and each target uses up to vhost_user_ram_slots_max() elements of it. Ensure arrays are big enough by adding an assertion in vhost_user_init().

---
RFC: Should I add VHOST_USER_MAX_RAM_SLOTS to vhost-user.h
or create an internal header for it?

---
   hw/virtio/meson.build          |  1 +
   hw/virtio/vhost-user-target.c  | 29 +++++++++++++++++++++++++++++
   hw/virtio/vhost-user.c         | 26 +++++---------------------
   include/hw/virtio/vhost-user.h |  7 +++++++
   4 files changed, 42 insertions(+), 21 deletions(-)

create mode 100644 hw/virtio/vhost-user-target.c

hw/virtio/meson.build +1 -0
index eb7ee8ea92..bf7e35fa8a 100644
@@ -11,6 +11,7 @@ if have_vhost
     specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c'))
     if have_vhost_user
       specific_virtio_ss.add(files('vhost-user.c'))
+    specific_virtio_ss.add(files('vhost-user-target.c'))
     endif
     if have_vhost_vdpa
       specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-virtqueue.c'))
hw/virtio/vhost-user-target.c new file +21 -0
new file mode 100644
index 0000000000..6a0d0f53d0
@@ -0,0 +1,29 @@
+/*
+ * vhost-user target-specific helpers
+ *
+ * Copyright (c) 2013 Virtual Open Systems Sarl.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/virtio/vhost-user.h"
+
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+#include "hw/acpi/acpi.h"
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+#include "hw/ppc/spapr.h"
+#endif
+
+unsigned int vhost_user_ram_slots_max(void)
+{
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
Quoted 9 lines

hw/virtio/vhost-user-target.c line 27

Quoted 6 lines
hw/virtio/vhost-user-target.c new file +6 -0
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+    return ACPI_MAX_RAM_SLOTS;
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+    return SPAPR_MAX_RAM_SLOTS;
+#else
+    return 512;

Should vhost_user_ram_slots_max be another TargetInfo field?

I don't think so, it would be better to transform the existing function in something like:

switch (target_current()) {
case TARGET_X86:
case TARGET_ARM:
case TARGET_X86_64:
case TARGET_ARM_64:

	return ACPI_MAX_RAM_SLOTS;
case TARGET PPC:
case TARGET PPC64:
	return SPAPR_MAX_RAM_SLOTS;
default:
	return 512;
}

We should not add anything possible to TargetInfo, just for the sake of it. Especially becomes it's hard to follow values set per architecture. In a case like this, a switch is much more readable and located in one place. With a generated jump table, it's quite efficient also.

In my opinion, it's another proof we need to have TARGET_X, and target_X() available at runtime.

Quoted 92 lines
Quoted 91 lines
hw/virtio/vhost-user-target.c new file +2 -0
+#endif
+}
hw/virtio/vhost-user.c +5 -21
index 8f635844af..21fc176725 100644
@@ -41,24 +41,7 @@
   #define VHOST_MEMORY_BASELINE_NREGIONS    8
   #define VHOST_USER_F_PROTOCOL_FEATURES 30
   #define VHOST_USER_SLAVE_MAX_FDS     8
-
-/*
- * Set maximum number of RAM slots supported to
- * the maximum number supported by the target
- * hardware plaform.
- */
-#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
-    defined(TARGET_ARM) || defined(TARGET_ARM_64)
-#include "hw/acpi/acpi.h"
-#define VHOST_USER_MAX_RAM_SLOTS ACPI_MAX_RAM_SLOTS
-
-#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
-#include "hw/ppc/spapr.h"
-#define VHOST_USER_MAX_RAM_SLOTS SPAPR_MAX_RAM_SLOTS
-
-#else
   #define VHOST_USER_MAX_RAM_SLOTS 512
-#endif
 
   /*
    * Maximum size of virtio device config space
@@ -935,7 +918,7 @@ static int vhost_user_add_remove_regions(struct vhost_dev *dev,
 
       if (track_ramblocks) {
           memcpy(u->postcopy_client_bases, shadow_pcb,
-               sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
+               sizeof(uint64_t) * vhost_user_ram_slots_max());
           /*
            * Now we've registered this with the postcopy code, we ack to the
            * client, because now we're in the position to be able to deal with
@@ -956,7 +939,7 @@ static int vhost_user_add_remove_regions(struct vhost_dev *dev,
   err:
       if (track_ramblocks) {
           memcpy(u->postcopy_client_bases, shadow_pcb,
-               sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
+               sizeof(uint64_t) * vhost_user_ram_slots_max());
       }
 
       return ret;
@@ -1030,7 +1013,7 @@ static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev,
           }
 
           memset(u->postcopy_client_bases, 0,
-               sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
+               sizeof(uint64_t) * vhost_user_ram_slots_max());
 
           /*
            * They're in the same order as the regions that were sent
@@ -2169,7 +2152,7 @@ static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque,
                   return -EINVAL;
               }
 
-            u->user->memory_slots = MIN(ram_slots, VHOST_USER_MAX_RAM_SLOTS);
+            u->user->memory_slots = MIN(ram_slots, vhost_user_ram_slots_max());
           }
       }
 
@@ -2649,6 +2632,7 @@ static void vhost_user_state_destroy(gpointer data)
 
   bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
   {
+    assert(vhost_user_ram_slots_max() <= VHOST_USER_MAX_RAM_SLOTS);
       if (user->chr) {
           error_setg(errp, "Cannot initialize vhost-user state");
           return false;
include/hw/virtio/vhost-user.h +7 -0
index 191216a74f..e13584ade8 100644
@@ -86,4 +86,11 @@ void vhost_user_async_close(DeviceState *d,
                               CharBackend *chardev, struct vhost_dev *vhost,
                               vu_async_close_fn cb);
 
+/**
+ * vhost_user_ram_slots_max()
+ *
+ * Return: maximum number of RAM slots supported by the target hardware plaform.
+ */
+unsigned int vhost_user_ram_slots_max(void);
+
   #endif
#22 lore raw

Re: [RFC PATCH-for-8.0 09/10] hw/virtio: Extract vhost_user_ram_slots_max() to vhost-user-target.c

Quoted 80 lines · Pierrick Bouvier
Quoted 67 lines · Philippe Mathieu-Daudé

Hi Pierrick,

Quoted 64 lines · Philippe Mathieu-Daudé

The current definition of VHOST_USER_MAX_RAM_SLOTS is target specific. By converting this definition to a runtime vhost_user_ram_slots_max() helper declared in a target specific unit, we can have the rest of vhost-user.c target independent.

To avoid variable length array or using the heap to store arrays of vhost_user_ram_slots_max() elements, we simply declare an array of the biggest VHOST_USER_MAX_RAM_SLOTS, and each target uses up to vhost_user_ram_slots_max() elements of it. Ensure arrays are big enough by adding an assertion in vhost_user_init().

---
RFC: Should I add VHOST_USER_MAX_RAM_SLOTS to vhost-user.h or create an internal header for it?
---
   hw/virtio/meson.build          |  1 + hw/virtio/vhost-user-target.c  | 29 +++++++++++++++++++++++++++++ hw/virtio/vhost-user.c         | 26 +++++--------------------- include/hw/virtio/vhost-user.h |  7 +++++++

   4 files changed, 42 insertions(+), 21 deletions(-)

   create mode 100644 hw/virtio/vhost-user-target.c

hw/virtio/meson.build +0 -0
index eb7ee8ea92..bf7e35fa8a 100644
@@ -11,6 +11,7 @@ if have_vhost
      specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c'))

     if have_vhost_user specific_virtio_ss.add(files('vhost-user.c'))
+    specific_virtio_ss.add(files('vhost-user-target.c'))
     endif
     if have_vhost_vdpa
       specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow- virtqueue.c'))

hw/virtio/vhost-user-target.c new file +21 -0
new file mode 100644
index 0000000000..6a0d0f53d0
@@ -0,0 +1,29 @@
+/*
+ * vhost-user target-specific helpers
+ *
+ * Copyright (c) 2013 Virtual Open Systems Sarl.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/virtio/vhost-user.h"
+
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+#include "hw/acpi/acpi.h"
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+#include "hw/ppc/spapr.h"
+#endif
+
+unsigned int vhost_user_ram_slots_max(void)
+{
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
Quoted 9 lines

hw/virtio/vhost-user-target.c line 27

Quoted 6 lines
hw/virtio/vhost-user-target.c new file +6 -0
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+    return ACPI_MAX_RAM_SLOTS;
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+    return SPAPR_MAX_RAM_SLOTS;
+#else
+    return 512;

Should vhost_user_ram_slots_max be another TargetInfo field?

I don't think so, it would be better to transform the existing function in something like:

Quoted 12 lines

switch (target_current()) {
case TARGET_X86:
case TARGET_ARM:
case TARGET_X86_64:
case TARGET_ARM_64:
    return ACPI_MAX_RAM_SLOTS;
case TARGET PPC:
case TARGET PPC64:
    return SPAPR_MAX_RAM_SLOTS;
default:
    return 512;
}

Clever, I like it, thanks!

#23 lore raw

Re: [RFC PATCH-for-8.0 09/10] hw/virtio: Extract vhost_user_ram_slots_max() to vhost-user-target.c

Quoted 98 lines · Philippe Mathieu-Daudé
Quoted 84 lines · Pierrick Bouvier
Quoted 70 lines · Philippe Mathieu-Daudé

Hi Pierrick,

Quoted 67 lines · Philippe Mathieu-Daudé

The current definition of VHOST_USER_MAX_RAM_SLOTS is target specific. By converting this definition to a runtime vhost_user_ram_slots_max() helper declared in a target specific unit, we can have the rest of vhost-user.c target independent.

To avoid variable length array or using the heap to store arrays of vhost_user_ram_slots_max() elements, we simply declare an array of the biggest VHOST_USER_MAX_RAM_SLOTS, and each target uses up to vhost_user_ram_slots_max() elements of it. Ensure arrays are big enough by adding an assertion in vhost_user_init().

---
RFC: Should I add VHOST_USER_MAX_RAM_SLOTS to vhost-user.h
       or create an internal header for it?

---
    hw/virtio/meson.build          |  1 +
    hw/virtio/vhost-user-target.c  | 29 +++++++++++++++++++++++++++++
    hw/virtio/vhost-user.c         | 26 +++++---------------------
    include/hw/virtio/vhost-user.h |  7 +++++++
    4 files changed, 42 insertions(+), 21 deletions(-)

   create mode 100644 hw/virtio/vhost-user-target.c

hw/virtio/meson.build +1 -0
index eb7ee8ea92..bf7e35fa8a 100644
@@ -11,6 +11,7 @@ if have_vhost
      specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c',
 'vhost-iova-tree.c'))
      if have_vhost_user
        specific_virtio_ss.add(files('vhost-user.c'))
+    specific_virtio_ss.add(files('vhost-user-target.c'))
      endif
      if have_vhost_vdpa

       specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-
virtqueue.c'))

hw/virtio/vhost-user- renamed from hw/virtio/vhost-user-target.c +0 -0

target.c
new file mode 100644
index 0000000000..6a0d0f53d0

hw/virtio/vhost-user-target.c new file +21 -0
@@ -0,0 +1,29 @@
+/*
+ * vhost-user target-specific helpers
+ *
+ * Copyright (c) 2013 Virtual Open Systems Sarl.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/virtio/vhost-user.h"
+
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+#include "hw/acpi/acpi.h"
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+#include "hw/ppc/spapr.h"
+#endif
+
+unsigned int vhost_user_ram_slots_max(void)
+{
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
Quoted 9 lines

hw/virtio/vhost-user-target.c line 27

Quoted 6 lines
hw/virtio/vhost-user-target.c new file +6 -0
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+    return ACPI_MAX_RAM_SLOTS;
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+    return SPAPR_MAX_RAM_SLOTS;
+#else
+    return 512;

Should vhost_user_ram_slots_max be another TargetInfo field?

I don't think so, it would be better to transform the existing function in something like:

Quoted 12 lines

switch (target_current()) {
case TARGET_X86:
case TARGET_ARM:
case TARGET_X86_64:
case TARGET_ARM_64:
    return ACPI_MAX_RAM_SLOTS;
case TARGET PPC:
case TARGET PPC64:
    return SPAPR_MAX_RAM_SLOTS;
default:
    return 512;
}

Quoted 1 line

Clever, I like it, thanks!

It's a pattern we can reuse in all places where it'll be needed.
It's better if we keep in TargetInfo only global information, that is used through all the codebase, and not specifics about a given subsystem/device/file.

By the way, TARGET_ARM_64 is probably TARGET_AARCH64.

author Reviewed-by Reviewed-by Alex Bennée Reviewed-by Michael S. Tsirkin
#24 lore raw

Re: [RFC PATCH-for-8.0 09/10] hw/virtio: Extract vhost_user_ram_slots_max() to vhost-user-target.c

Quoted 99 lines · Pierrick Bouvier
Quoted 98 lines · Philippe Mathieu-Daudé
Quoted 96 lines · Pierrick Bouvier
Quoted 70 lines · Philippe Mathieu-Daudé

Hi Pierrick,

Quoted 67 lines · Philippe Mathieu-Daudé

The current definition of VHOST_USER_MAX_RAM_SLOTS is target specific. By converting this definition to a runtime vhost_user_ram_slots_max() helper declared in a target specific unit, we can have the rest of vhost-user.c target independent.

To avoid variable length array or using the heap to store arrays of vhost_user_ram_slots_max() elements, we simply declare an array of the biggest VHOST_USER_MAX_RAM_SLOTS, and each target uses up to vhost_user_ram_slots_max() elements of it. Ensure arrays are big enough by adding an assertion in vhost_user_init().

---
RFC: Should I add VHOST_USER_MAX_RAM_SLOTS to vhost-user.h or create an internal header for it?
---
    hw/virtio/meson.build          |  1 + hw/virtio/vhost-user-target.c  | 29 +++++++++++++++++++++++++++++ hw/virtio/vhost-user.c         | 26 +++++--------------------- include/hw/virtio/vhost-user.h |  7 +++++++

    4 files changed, 42 insertions(+), 21 deletions(-)

    create mode 100644 hw/virtio/vhost-user-target.c

hw/virtio/meson.build +0 -0
index eb7ee8ea92..bf7e35fa8a 100644
@@ -11,6 +11,7 @@ if have_vhost
       specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c',
 'vhost-iova-tree.c'))

      if have_vhost_user specific_virtio_ss.add(files('vhost-user.c'))
+    specific_virtio_ss.add(files('vhost-user-target.c')) endif
      if have_vhost_vdpa specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow- virtqueue.c'))

hw/virtio/vhost-user- renamed from hw/virtio/vhost-user-target.c +0 -0

target.c
new file mode 100644
index 0000000000..6a0d0f53d0

hw/virtio/vhost-user-target.c new file +21 -0
@@ -0,0 +1,29 @@
+/*
+ * vhost-user target-specific helpers
+ *
+ * Copyright (c) 2013 Virtual Open Systems Sarl.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/virtio/vhost-user.h"
+
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+#include "hw/acpi/acpi.h"
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+#include "hw/ppc/spapr.h"
+#endif
+
+unsigned int vhost_user_ram_slots_max(void)
+{
+#if defined(TARGET_X86) || defined(TARGET_X86_64) || \
Quoted 9 lines

hw/virtio/vhost-user-target.c line 27

Quoted 6 lines
hw/virtio/vhost-user-target.c new file +6 -0
+    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+    return ACPI_MAX_RAM_SLOTS;
+#elif defined(TARGET_PPC) || defined(TARGET_PPC64)
+    return SPAPR_MAX_RAM_SLOTS;
+#else
+    return 512;

Should vhost_user_ram_slots_max be another TargetInfo field?

I don't think so, it would be better to transform the existing function in something like:

switch (target_current()) {
case TARGET_X86:
case TARGET_ARM:
case TARGET_X86_64:
case TARGET_ARM_64:
      return ACPI_MAX_RAM_SLOTS;
case TARGET PPC:
case TARGET PPC64:
      return SPAPR_MAX_RAM_SLOTS;
default:
      return 512;
}

Quoted 6 lines
Quoted 1 line

Clever, I like it, thanks!

It's a pattern we can reuse in all places where it'll be needed.
It's better if we keep in TargetInfo only global information, that is used through all the codebase, and not specifics about a given subsystem/device/file.

By the way, TARGET_ARM_64 is probably TARGET_AARCH64.

Correct, it has been fixed by Akihiko:

commit 744734ccc9eff28394a453de462b2a155f364118

Author: Akihiko Odaki <[email protected]>
Date:   Mon Jan 9 15:31:30 2023 +0900

vhost-user: Correct a reference of TARGET_AARCH64

Presumably TARGET_ARM_64 should be a mistake of TARGET_AARCH64.

Message-Id: <[email protected]>

Fixes 27598393a2 ("Lift max memory slots limit imposed by vhost-user")
hw/virtio/vhost-user.c +1 -1
index d9ce0501b2c..6c79da953b3 100644
@@ -48,7 +48,7 @@
  * hardware plaform.
  */
 #if defined(TARGET_X86) || defined(TARGET_X86_64) || \
-    defined(TARGET_ARM) || defined(TARGET_ARM_64)
+    defined(TARGET_ARM) || defined(TARGET_AARCH64)
 #include "hw/acpi/acpi.h"
 #define VHOST_USER_MAX_RAM_SLOTS ACPI_MAX_RAM_SLOTS
 
#25 lore raw

RFC 10/10 hw/virtio: Make most of virtio devices target-independent

In reply to Philippe Mathieu-Daudé

Except the following files:
- virtio-config.c
- virtio-qmp.c
- virtio-iommu.c
- virtio-mem.c
- vhost-user-target.c
- vhost-vdpa.c
all other virtio related files are target independent and can be compiled only once for a system emulation build, avoiding compiling hundreds of objects.

---
RFC: Cross-built on ppc64le/s390x but not tested there.

---
 hw/9pfs/meson.build            |  2 +-
 hw/block/dataplane/meson.build |  2 +-
 hw/block/meson.build           |  4 ++--
 hw/char/meson.build            |  2 +-
 hw/net/meson.build             |  2 +-
 hw/virtio/meson.build          | 38 +++++++++++++++++-----------------
 6 files changed, 25 insertions(+), 25 deletions(-)
hw/9pfs/meson.build +1 -1
index 12443b6ad5..ef37532dbf 100644
@@ -18,4 +18,4 @@ fs_ss.add(when: 'CONFIG_DARWIN', if_true: files('9p-util-darwin.c'))
 fs_ss.add(when: 'CONFIG_XEN', if_true: files('xen-9p-backend.c'))
 softmmu_ss.add_all(when: 'CONFIG_FSDEV_9P', if_true: fs_ss)
 
-specific_ss.add(when: 'CONFIG_VIRTIO_9P', if_true: files('virtio-9p-device.c'))
+softmmu_ss.add(when: 'CONFIG_VIRTIO_9P', if_true: files('virtio-9p-device.c'))
hw/block/dataplane/meson.build +1 -1
index 12c6a264f1..e2f3721ce2 100644
@@ -1,2 +1,2 @@
-specific_ss.add(when: 'CONFIG_VIRTIO_BLK', if_true: files('virtio-blk.c'))
+softmmu_ss.add(when: 'CONFIG_VIRTIO_BLK', if_true: files('virtio-blk.c'))
 specific_ss.add(when: 'CONFIG_XEN', if_true: files('xen-block.c'))
hw/block/meson.build +2 -2
index b434d5654c..8a3ca43a5c 100644
@@ -17,7 +17,7 @@ softmmu_ss.add(when: 'CONFIG_SWIM', if_true: files('swim.c'))
 softmmu_ss.add(when: 'CONFIG_XEN', if_true: files('xen-block.c'))
 softmmu_ss.add(when: 'CONFIG_TC58128', if_true: files('tc58128.c'))
 
-specific_ss.add(when: 'CONFIG_VIRTIO_BLK', if_true: files('virtio-blk.c', 'virtio-blk-common.c'))
-specific_ss.add(when: 'CONFIG_VHOST_USER_BLK', if_true: files('vhost-user-blk.c', 'virtio-blk-common.c'))
+softmmu_ss.add(when: 'CONFIG_VIRTIO_BLK', if_true: files('virtio-blk.c', 'virtio-blk-common.c'))
+softmmu_ss.add(when: 'CONFIG_VHOST_USER_BLK', if_true: files('vhost-user-blk.c', 'virtio-blk-common.c'))
 
 subdir('dataplane')
hw/char/meson.build +1 -1
index 7b594f51b8..afd35649cd 100644
@@ -18,6 +18,7 @@ softmmu_ss.add(when: 'CONFIG_SERIAL_PCI', if_true: files('serial-pci.c'))
 softmmu_ss.add(when: 'CONFIG_SERIAL_PCI_MULTI', if_true: files('serial-pci-multi.c'))
 softmmu_ss.add(when: 'CONFIG_SHAKTI_UART', if_true: files('shakti_uart.c'))
 softmmu_ss.add(when: 'CONFIG_VIRTIO_SERIAL', if_true: files('virtio-console.c'))
+softmmu_ss.add(when: 'CONFIG_VIRTIO', if_true: files('virtio-serial-bus.c'))
 softmmu_ss.add(when: 'CONFIG_XEN', if_true: files('xen_console.c'))
 softmmu_ss.add(when: 'CONFIG_XILINX', if_true: files('xilinx_uartlite.c'))
 
@@ -35,7 +36,6 @@ softmmu_ss.add(when: 'CONFIG_MCHP_PFSOC_MMUART', if_true: files('mchp_pfsoc_mmua
 
 specific_ss.add(when: 'CONFIG_HTIF', if_true: files('riscv_htif.c'))
 specific_ss.add(when: 'CONFIG_TERMINAL3270', if_true: files('terminal3270.c'))
-specific_ss.add(when: 'CONFIG_VIRTIO', if_true: files('virtio-serial-bus.c'))
 specific_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr_vty.c'))
 
 specific_ss.add(when: 'CONFIG_GOLDFISH_TTY', if_true: files('goldfish_tty.c'))
hw/net/meson.build +1 -1
index ebac261542..8035aaa560 100644
@@ -44,7 +44,7 @@ specific_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr_llan.c'))
 specific_ss.add(when: 'CONFIG_XILINX_ETHLITE', if_true: files('xilinx_ethlite.c'))
 
 softmmu_ss.add(when: 'CONFIG_VIRTIO_NET', if_true: files('net_rx_pkt.c'))
-specific_ss.add(when: 'CONFIG_VIRTIO_NET', if_true: files('virtio-net.c'))
+softmmu_ss.add(when: 'CONFIG_VIRTIO_NET', if_true: files('virtio-net.c'))
 
 if have_vhost_net
   softmmu_ss.add(when: 'CONFIG_VIRTIO_NET', if_true: files('vhost_net.c'), if_false: files('vhost_net-stub.c'))
hw/virtio/meson.build +19 -19
index bf7e35fa8a..23be895c8e 100644
@@ -1,38 +1,38 @@
 softmmu_virtio_ss = ss.source_set()
-softmmu_virtio_ss.add(files('virtio-bus.c'))
+softmmu_virtio_ss.add(files('virtio.c', 'virtio-bus.c'))
 softmmu_virtio_ss.add(when: 'CONFIG_VIRTIO_PCI', if_true: files('virtio-pci.c'))
 softmmu_virtio_ss.add(when: 'CONFIG_VIRTIO_MMIO', if_true: files('virtio-mmio.c'))
 
 specific_virtio_ss = ss.source_set()
-specific_virtio_ss.add(files('virtio.c'))
 specific_virtio_ss.add(files('virtio-config.c', 'virtio-qmp.c'))
+specific_virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c'))
+specific_virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c'))
 
 if have_vhost
-  specific_virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c'))
+  softmmu_virtio_ss.add(files('vhost.c', 'vhost-backend.c', 'vhost-iova-tree.c'))
   if have_vhost_user
-    specific_virtio_ss.add(files('vhost-user.c'))
+    softmmu_virtio_ss.add(files('vhost-user.c'))
     specific_virtio_ss.add(files('vhost-user-target.c'))
   endif
   if have_vhost_vdpa
-    specific_virtio_ss.add(files('vhost-vdpa.c', 'vhost-shadow-virtqueue.c'))
+    specific_virtio_ss.add(files('vhost-vdpa.c'))
+    softmmu_virtio_ss.add(files('vhost-shadow-virtqueue.c'))
   endif
 else
   softmmu_virtio_ss.add(files('vhost-stub.c'))
 endif
 
-specific_virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
-specific_virtio_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('virtio-crypto.c'))
-specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_FS', if_true: files('vhost-user-fs.c'))
-specific_virtio_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem.c'))
-specific_virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock.c', 'vhost-vsock-common.c'))
-specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock.c', 'vhost-vsock-common.c'))
-specific_virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c'))
-specific_virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c'))
-specific_virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c'))
-specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c'))
-specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rng.c'))
-specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_GPIO', if_true: files('vhost-user-gpio.c'))
-specific_virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_GPIO'], if_true: files('vhost-user-gpio-pci.c'))
+softmmu_virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
+softmmu_virtio_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('virtio-crypto.c'))
+softmmu_virtio_ss.add(when: 'CONFIG_VHOST_USER_FS', if_true: files('vhost-user-fs.c'))
+softmmu_virtio_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem.c'))
+softmmu_virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock.c', 'vhost-vsock-common.c'))
+softmmu_virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: files('vhost-user-vsock.c', 'vhost-vsock-common.c'))
+softmmu_virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: files('virtio-rng.c'))
+softmmu_virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c'))
+softmmu_virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rng.c'))
+softmmu_virtio_ss.add(when: 'CONFIG_VHOST_USER_GPIO', if_true: files('vhost-user-gpio.c'))
+softmmu_virtio_ss.add(when: ['CONFIG_VIRTIO_PCI', 'CONFIG_VHOST_USER_GPIO'], if_true: files('vhost-user-gpio-pci.c'))
 
 virtio_pci_ss = ss.source_set()
 virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c'))
@@ -59,7 +59,7 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem-pci.c'
 virtio_pci_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu-pci.c'))
 virtio_pci_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem-pci.c'))
 
-specific_virtio_ss.add_all(when: 'CONFIG_VIRTIO_PCI', if_true: virtio_pci_ss)
+softmmu_virtio_ss.add_all(when: 'CONFIG_VIRTIO_PCI', if_true: virtio_pci_ss)
 
 softmmu_ss.add_all(when: 'CONFIG_VIRTIO', if_true: softmmu_virtio_ss)
 softmmu_ss.add(when: 'CONFIG_VIRTIO', if_false: files('vhost-stub.c'))
Signature · 1 line
2.38.1