@samitouri / QOSamiQemu / commits / 38155b1f58

hw/virtio: Simplify virtio_access_is_big_endian()

Thanks to previous refactoring, we can see more easily it is strictly equivalent to always call virtio_vdev_is_big_endian. static inline bool virtio_vdev_is_big_endian(VirtIODevice *vdev) { if (virtio_vdev_is_legacy(vdev)) { 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; } The key is to understand that vdev->device_endian is initialized as expected. It always contains cpu endianness, and not device endianness, ignoring if device is legacy or not. By default, it's initialized to vdev->device_endian = virtio_default_endian(), which matches target default endianness. Then, on virtio_reset, it will be initialized with current_cpu endianness (if there is one current_cpu). void virtio_reset() { ... if (current_cpu) { /* Guest initiated reset */ vdev->device_endian = virtio_current_cpu_endian(); } else { /* System reset */ vdev->device_endian = virtio_default_endian(); } Now, we can see how existing virtio_access_is_big_endian is equivalent to virtio_vdev_is_big_endian. Let's break the existing function in its 3 variants, and compare that to virtio_vdev_is_big_endian. static inline bool virtio_access_is_big_endian(VirtIODevice *vdev) - #if defined(LEGACY_VIRTIO_IS_BIENDIAN) return virtio_vdev_is_big_endian(vdev); This is the exact replacement we did, so equivalent. - #elif TARGET_BIG_ENDIAN if (!virtio_vdev_is_legacy(vdev)) { return false; } return true; we know target_is_big_endian(), so vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG. if (virtio_vdev_is_legacy(vdev)) { return VIRTIO_DEVICE_ENDIAN_BIG == VIRTIO_DEVICE_ENDIAN_BIG; } return false; It's written in opposite style compared to existing code (if legacy vs if modern), but it's strictly equivalent. - #else return false; we know !target_is_big_endian(), so vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE. if virtio_vdev_is_legacy(vdev) { return VIRTIO_DEVICE_ENDIAN_LITTLE == VIRTIO_DEVICE_ENDIAN_BIG; } return false; So it always return false, as expected. Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Acked-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Link: https://lore.kernel.org/qemu-devel/20260225041948.52929-3-philmd@linaro.org Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>

Pierrick Bouvier committed Feb 25, 2026 at 05:19 UTC 38155b1f5819aba7b761403d8b04478164e85df9
1 file changed -14
include/hw/virtio/virtio-access.h
-14
@@ -21,23 +21,9 @@
21 #include "hw/virtio/virtio.h"
22 #include "hw/virtio/virtio-bus.h"
23
24 -#if defined(TARGET_PPC64) || defined(TARGET_ARM)
25 -#define LEGACY_VIRTIO_IS_BIENDIAN 1
26 -#endif
27 -
24 static inline bool virtio_access_is_big_endian(VirtIODevice *vdev)
25 {
30 -#if defined(LEGACY_VIRTIO_IS_BIENDIAN)
26 return virtio_vdev_is_big_endian(vdev);
32 -#elif TARGET_BIG_ENDIAN
33 - if (!virtio_vdev_is_legacy(vdev)) {
34 - /* Devices conforming to VIRTIO 1.0 or later are always LE. */
35 - return false;
36 - }
37 - return true;
38 -#else
39 - return false;
40 -#endif
27 }
28
29 static inline void virtio_stw_p(VirtIODevice *vdev, void *ptr, uint16_t v)