Skip to contentSkip to conversation

hw/audio/virtio-snd: Use device endianness instead of target one

PATCH In review Philippe Mathieu-Daudé 4 participants 19 messages Apr 22, 2024 – Apr 25, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 hw/audio/virtio-snd: Use device endianness instead of target one

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")

---
v2: Use virtio_is_big_endian()
v3: Remove "hw/core/cpu.h

---
 hw/audio/virtio-snd.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/audio/virtio-snd.c +5 -4
index c80b58bf5d..939cd78026 100644
@@ -24,7 +24,6 @@
 #include "trace.h"
 #include "qapi/error.h"
 #include "hw/audio/virtio-snd.h"
-#include "hw/core/cpu.h"
 
 #define VIRTIO_SOUND_VM_VERSION 1
 #define VIRTIO_SOUND_JACK_DEFAULT 0
@@ -395,13 +394,15 @@ static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
  * Get QEMU Audiosystem compatible audsettings from virtio based pcm stream
  * params.
  */
-static void virtio_snd_get_qemu_audsettings(audsettings *as,
+static void virtio_snd_get_qemu_audsettings(VirtIOSound *s, audsettings *as,
                                             virtio_snd_pcm_set_params *params)
 {
+    VirtIODevice *vdev = VIRTIO_DEVICE(s);
+
     as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
     as->fmt = virtio_snd_get_qemu_format(params->format);
     as->freq = virtio_snd_get_qemu_freq(params->rate);
-    as->endianness = target_words_bigendian() ? 1 : 0;
+    as->endianness = virtio_is_big_endian(vdev) ? 1 : 0;
 }
 
 /*
@@ -464,7 +465,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
         s->pcm->streams[stream_id] = stream;
     }
 
-    virtio_snd_get_qemu_audsettings(&as, params);
+    virtio_snd_get_qemu_audsettings(s, &as, params);
     stream->info.direction = stream_id < s->snd_conf.streams / 2 +
         (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT;
     stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID;
Signature · 1 line
2.41.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 8 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")

---

Thanks for the explanation on v2 btw. virtio_is_big_endian()'s doc should probably reflect it's not just about legacy devices (virtio sound isn't legacy) but about target originating data streams too

#2 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.
If it's not it's a guest bug pls just fix it there.

Quoted 48 lines

---
v2: Use virtio_is_big_endian()
v3: Remove "hw/core/cpu.h

---
 hw/audio/virtio-snd.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/audio/virtio-snd.c +5 -4
index c80b58bf5d..939cd78026 100644
@@ -24,7 +24,6 @@
 #include "trace.h"
 #include "qapi/error.h"
 #include "hw/audio/virtio-snd.h"
-#include "hw/core/cpu.h"
 
 #define VIRTIO_SOUND_VM_VERSION 1
 #define VIRTIO_SOUND_JACK_DEFAULT 0
@@ -395,13 +394,15 @@ static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
  * Get QEMU Audiosystem compatible audsettings from virtio based pcm stream
  * params.
  */
-static void virtio_snd_get_qemu_audsettings(audsettings *as,
+static void virtio_snd_get_qemu_audsettings(VirtIOSound *s, audsettings *as,
                                             virtio_snd_pcm_set_params *params)
 {
+    VirtIODevice *vdev = VIRTIO_DEVICE(s);
+
     as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
     as->fmt = virtio_snd_get_qemu_format(params->format);
     as->freq = virtio_snd_get_qemu_freq(params->rate);
-    as->endianness = target_words_bigendian() ? 1 : 0;
+    as->endianness = virtio_is_big_endian(vdev) ? 1 : 0;
 }
 
 /*
@@ -464,7 +465,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
         s->pcm->streams[stream_id] = stream;
     }
 
-    virtio_snd_get_qemu_audsettings(&as, params);
+    virtio_snd_get_qemu_audsettings(s, &as, params);
     stream->info.direction = stream_id < s->snd_conf.streams / 2 +
         (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT;
     stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID;
Signature · 1 line
2.41.0
#3 lore raw
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Quoted 8 lines
Quoted 3 lines
Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

Oh, then the fix is as simple as:

- as->endianness = target_words_bigendian() ? 1 : 0;
+ as->endianness = 0; /* VIRTIO 1.0: always LE. */

Quoted 52 lines

If it's not it's a guest bug pls just fix it there.

Quoted 48 lines

---
v2: Use virtio_is_big_endian()
v3: Remove "hw/core/cpu.h

---
  hw/audio/virtio-snd.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
hw/audio/virtio-snd.c +5 -4
index c80b58bf5d..939cd78026 100644
@@ -24,7 +24,6 @@
  #include "trace.h"
  #include "qapi/error.h"
  #include "hw/audio/virtio-snd.h"
-#include "hw/core/cpu.h"
 
  #define VIRTIO_SOUND_VM_VERSION 1
  #define VIRTIO_SOUND_JACK_DEFAULT 0
@@ -395,13 +394,15 @@ static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
   * Get QEMU Audiosystem compatible audsettings from virtio based pcm stream
   * params.
   */
-static void virtio_snd_get_qemu_audsettings(audsettings *as,
+static void virtio_snd_get_qemu_audsettings(VirtIOSound *s, audsettings *as,
                                              virtio_snd_pcm_set_params *params)
  {
+    VirtIODevice *vdev = VIRTIO_DEVICE(s);
+
      as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
      as->fmt = virtio_snd_get_qemu_format(params->format);
      as->freq = virtio_snd_get_qemu_freq(params->rate);
-    as->endianness = target_words_bigendian() ? 1 : 0;
+    as->endianness = virtio_is_big_endian(vdev) ? 1 : 0;
  }
 
  /*
@@ -464,7 +465,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
          s->pcm->streams[stream_id] = stream;
      }
 
-    virtio_snd_get_qemu_audsettings(&as, params);
+    virtio_snd_get_qemu_audsettings(s, &as, params);
      stream->info.direction = stream_id < s->snd_conf.streams / 2 +
          (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT;
      stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID;
Signature · 1 line
2.41.0
#4 lore raw
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")
Quoted 7 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

Oh, then the fix is as simple as:

- as->endianness = target_words_bigendian() ? 1 : 0;
+ as->endianness = 0; /* VIRTIO 1.0: always LE. */

Makes sense. Pls clarify in commit log whether the incorrect value leads to any failures or this was found by code review.

Quoted 49 lines
Quoted 49 lines

If it's not it's a guest bug pls just fix it there.

Quoted 45 lines

---
v2: Use virtio_is_big_endian()
v3: Remove "hw/core/cpu.h

---
  hw/audio/virtio-snd.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
hw/audio/virtio-snd.c +5 -4
index c80b58bf5d..939cd78026 100644
@@ -24,7 +24,6 @@
  #include "trace.h"
  #include "qapi/error.h"
  #include "hw/audio/virtio-snd.h"
-#include "hw/core/cpu.h"
  #define VIRTIO_SOUND_VM_VERSION 1
  #define VIRTIO_SOUND_JACK_DEFAULT 0
@@ -395,13 +394,15 @@ static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
   * Get QEMU Audiosystem compatible audsettings from virtio based pcm stream
   * params.
   */
-static void virtio_snd_get_qemu_audsettings(audsettings *as,
+static void virtio_snd_get_qemu_audsettings(VirtIOSound *s, audsettings *as,
                                              virtio_snd_pcm_set_params *params)
  {
+    VirtIODevice *vdev = VIRTIO_DEVICE(s);
+
      as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
      as->fmt = virtio_snd_get_qemu_format(params->format);
      as->freq = virtio_snd_get_qemu_freq(params->rate);
-    as->endianness = target_words_bigendian() ? 1 : 0;
+    as->endianness = virtio_is_big_endian(vdev) ? 1 : 0;
  }
  /*
@@ -464,7 +465,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)
          s->pcm->streams[stream_id] = stream;
      }
-    virtio_snd_get_qemu_audsettings(&as, params);
+    virtio_snd_get_qemu_audsettings(s, &as, params);
      stream->info.direction = stream_id < s->snd_conf.streams / 2 +
          (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT;
      stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID;
Signature · 1 line
2.41.0
#5 lore raw
Quoted 8 lines · Michael S. Tsirkin
Quoted 6 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

#6 lore raw
Quoted 16 lines · Manos Pitsidianakis
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

#7 lore raw
Quoted 22 lines · Manos Pitsidianakis
Quoted 20 lines · Manos Pitsidianakis
Quoted 16 lines · Michael S. Tsirkin
Quoted 14 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

Quoted 7 lines
Quoted 2 lines

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

#8 lore raw
Quoted 26 lines · Philippe Mathieu-Daudé
Quoted 25 lines · Manos Pitsidianakis
Quoted 22 lines · Manos Pitsidianakis
Quoted 8 lines · Michael S. Tsirkin
Quoted 6 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Quoted 8 lines
Quoted 4 lines

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

It feels to me that the endianness is something that should be negotiated as part of the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

My feeling is that either the VIRTIO_SND_PCM_FMT_* constants should be extended to have both _LE and _BE variants, or all frame formats are defined to always be little endian.

ATB,

Mark.

#9 lore raw
Quoted 34 lines · Mark Cave-Ayland
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Manos Pitsidianakis
Quoted 16 lines · Manos Pitsidianakis
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Quoted 8 lines
Quoted 3 lines

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

It feels to me that the endianness is something that should be negotiated as part of the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

But that is the job of the hardware drivers, isn't it? Here we are taking frames passed from the guest to its virtio driver in the format specified in the target cpu's endianness and QEMU as the device passes it to host ALSA/Pipewire/etc which in turn passes it to the actual audio hardware driver..

Quoted 9 lines

My feeling is that either the VIRTIO_SND_PCM_FMT_* constants should be extended to have both _LE and _BE variants, or all frame formats are defined to always be little endian.

ATB,

Mark.

#10 lore raw
Quoted 45 lines · Manos Pitsidianakis
Quoted 34 lines · Mark Cave-Ayland
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Manos Pitsidianakis
Quoted 16 lines · Manos Pitsidianakis
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Quoted 8 lines
Quoted 3 lines

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

It feels to me that the endianness is something that should be negotiated as part of the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

Quoted 5 lines

But that is the job of the hardware drivers, isn't it? Here we are taking frames passed from the guest to its virtio driver in the format specified in the target cpu's endianness and QEMU as the device passes it to host ALSA/Pipewire/etc which in turn passes it to the actual audio hardware driver..

The problem is that the notion of target CPU endian is not fixed. For example the PowerPC CPU starts off in big-endian mode, but these days most systems will switch the CPU to little-endian mode on startup to run ppc64le. There's also the ILE bit which can be configured so that a big-endian PowerPC CPU can dynamically switch to little-endian mode when processing an interrupt, so you could potentially end up with either depending upon the current mode of the CPU.

These are the kinds of issues that led to the later virtio specifications simply using little-endian for everything, since then there is zero ambiguity over what endian is required for the virtio configuration space accesses.

It feels to me that assuming a target CPU endian is fixed for the PCM frame formats is simply repeating the mistakes of the past - and even the fact that we are discussing this within this thread suggests that at a very minimum the virtio-snd specification needs to be updated to clarify the byte ordering of the PCM frame formats.

ATB,

Mark.

#11 lore raw
Quoted 71 lines · Mark Cave-Ayland
Quoted 45 lines · Manos Pitsidianakis
Quoted 34 lines · Mark Cave-Ayland
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Manos Pitsidianakis
Quoted 16 lines · Manos Pitsidianakis
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Quoted 8 lines
Quoted 3 lines

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

It feels to me that the endianness is something that should be negotiated as part of the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

Quoted 5 lines

But that is the job of the hardware drivers, isn't it? Here we are taking frames passed from the guest to its virtio driver in the format specified in the target cpu's endianness and QEMU as the device passes it to host ALSA/Pipewire/etc which in turn passes it to the actual audio hardware driver..

The problem is that the notion of target CPU endian is not fixed. For example the PowerPC CPU starts off in big-endian mode, but these days most systems will switch the CPU to little-endian mode on startup to run ppc64le. There's also the ILE bit which can be configured so that a big-endian PowerPC CPU can dynamically switch to little-endian mode when processing an interrupt, so you could potentially end up with either depending upon the current mode of the CPU.

These are the kinds of issues that led to the later virtio specifications simply using little-endian for everything, since then there is zero ambiguity over what endian is required for the virtio configuration space accesses.

It feels to me that assuming a target CPU endian is fixed for the PCM frame formats is simply repeating the mistakes of the past - and even the fact that we are discussing this within this thread suggests that at a very minimum the virtio-snd specification needs to be updated to clarify the byte ordering of the PCM frame formats.

Quoted 4 lines

ATB,

Mark.

Agreed, I think we are saying approximately the same thing here.

We need a mechanism to retrieve the vCPUs endianness and a way to
notify subscribed devices when it changes.

I think that then, since the virtio device is mostly certain of the
correct target endianness and completely certain of the host endianness, it can perform the necessary conversions.

I don't recall seeing a restriction on the byte ordering of PCM
formats other than the CPU order; except for the ones which have explicit endianness in their definitions. Please correct me if I am wrong!

A straightforward solution would be to set an endianness change notify callback in DeviceClass. What do you think?

Signature · 2 lines
Manos Pitsidianakis
Emulation and Virtualization Engineer at Linaro Ltd
#12 lore raw
Quoted 75 lines · Manos Pitsidianakis
Quoted 73 lines · Mark Cave-Ayland
Quoted 45 lines · Manos Pitsidianakis
Quoted 34 lines · Mark Cave-Ayland
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Manos Pitsidianakis
Quoted 16 lines · Manos Pitsidianakis
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Quoted 8 lines
Quoted 3 lines

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

It feels to me that the endianness is something that should be negotiated as part of the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

Quoted 5 lines

But that is the job of the hardware drivers, isn't it? Here we are taking frames passed from the guest to its virtio driver in the format specified in the target cpu's endianness and QEMU as the device passes it to host ALSA/Pipewire/etc which in turn passes it to the actual audio hardware driver..

The problem is that the notion of target CPU endian is not fixed. For example the PowerPC CPU starts off in big-endian mode, but these days most systems will switch the CPU to little-endian mode on startup to run ppc64le. There's also the ILE bit which can be configured so that a big-endian PowerPC CPU can dynamically switch to little-endian mode when processing an interrupt, so you could potentially end up with either depending upon the current mode of the CPU.

These are the kinds of issues that led to the later virtio specifications simply using little-endian for everything, since then there is zero ambiguity over what endian is required for the virtio configuration space accesses.

It feels to me that assuming a target CPU endian is fixed for the PCM frame formats is simply repeating the mistakes of the past - and even the fact that we are discussing this within this thread suggests that at a very minimum the virtio-snd specification needs to be updated to clarify the byte ordering of the PCM frame formats.

ATB,

Quoted 8 lines
Quoted 2 lines

Mark.

Agreed, I think we are saying approximately the same thing here.

We need a mechanism to retrieve the vCPUs endianness and a way to
notify subscribed devices when it changes.

I don't think I agree, it's not the same thing. Guest should just convert and send data in LE format. Host should then convert from LE format. Target endian-ness does not come into it.

Quoted 15 lines

I think that then, since the virtio device is mostly certain of the
correct target endianness and completely certain of the host endianness, it can perform the necessary conversions.

I don't recall seeing a restriction on the byte ordering of PCM
formats other than the CPU order; except for the ones which have explicit endianness in their definitions. Please correct me if I am wrong!

A straightforward solution would be to set an endianness change notify callback in DeviceClass. What do you think?

Signature · 2 lines
Manos Pitsidianakis
Emulation and Virtualization Engineer at Linaro Ltd
#13 lore raw
Quoted 83 lines · Michael S. Tsirkin
Quoted 81 lines · Manos Pitsidianakis
Quoted 71 lines · Mark Cave-Ayland
Quoted 45 lines · Manos Pitsidianakis
Quoted 34 lines · Mark Cave-Ayland
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Manos Pitsidianakis
Quoted 16 lines · Manos Pitsidianakis
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Quoted 8 lines
Quoted 3 lines

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

It feels to me that the endianness is something that should be negotiated as part of the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

Quoted 5 lines

But that is the job of the hardware drivers, isn't it? Here we are taking frames passed from the guest to its virtio driver in the format specified in the target cpu's endianness and QEMU as the device passes it to host ALSA/Pipewire/etc which in turn passes it to the actual audio hardware driver..

The problem is that the notion of target CPU endian is not fixed. For example the PowerPC CPU starts off in big-endian mode, but these days most systems will switch the CPU to little-endian mode on startup to run ppc64le. There's also the ILE bit which can be configured so that a big-endian PowerPC CPU can dynamically switch to little-endian mode when processing an interrupt, so you could potentially end up with either depending upon the current mode of the CPU.

These are the kinds of issues that led to the later virtio specifications simply using little-endian for everything, since then there is zero ambiguity over what endian is required for the virtio configuration space accesses.

It feels to me that assuming a target CPU endian is fixed for the PCM frame formats is simply repeating the mistakes of the past - and even the fact that we are discussing this within this thread suggests that at a very minimum the virtio-snd specification needs to be updated to clarify the byte ordering of the PCM frame formats.

Quoted 4 lines

ATB,

Mark.

Agreed, I think we are saying approximately the same thing here.

Quoted 7 lines
Quoted 2 lines

We need a mechanism to retrieve the vCPUs endianness and a way to
notify subscribed devices when it changes.

I don't think I agree, it's not the same thing. Guest should just convert and send data in LE format. Host should then convert from LE format. Target endian-ness does not come into it.

That's not in the VIRTIO 1.2 spec. We are talking about supporting things as they currently stand, not as they could have been.

#14 lore raw
Quoted 87 lines · Manos Pitsidianakis
Quoted 86 lines · Michael S. Tsirkin
Quoted 83 lines · Manos Pitsidianakis
Quoted 71 lines · Mark Cave-Ayland
Quoted 45 lines · Manos Pitsidianakis
Quoted 34 lines · Mark Cave-Ayland
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Manos Pitsidianakis
Quoted 16 lines · Manos Pitsidianakis
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime,
we need to use the device endianness, not the target
one.

Cc: [email protected]
Fixes: eb9ad377bb ("virtio-sound: handle control messages and streams")
Signed-off-by: Philippe Mathieu-Daudé <[email protected]>

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Quoted 8 lines
Quoted 3 lines

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

It feels to me that the endianness is something that should be negotiated as part of the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

Quoted 5 lines

But that is the job of the hardware drivers, isn't it? Here we are taking frames passed from the guest to its virtio driver in the format specified in the target cpu's endianness and QEMU as the device passes it to host ALSA/Pipewire/etc which in turn passes it to the actual audio hardware driver..

The problem is that the notion of target CPU endian is not fixed. For example the PowerPC CPU starts off in big-endian mode, but these days most systems will switch the CPU to little-endian mode on startup to run ppc64le. There's also the ILE bit which can be configured so that a big-endian PowerPC CPU can dynamically switch to little-endian mode when processing an interrupt, so you could potentially end up with either depending upon the current mode of the CPU.

These are the kinds of issues that led to the later virtio specifications simply using little-endian for everything, since then there is zero ambiguity over what endian is required for the virtio configuration space accesses.

It feels to me that assuming a target CPU endian is fixed for the PCM frame formats is simply repeating the mistakes of the past - and even the fact that we are discussing this within this thread suggests that at a very minimum the virtio-snd specification needs to be updated to clarify the byte ordering of the PCM frame formats.

Quoted 4 lines

ATB,

Mark.

Agreed, I think we are saying approximately the same thing here.

We need a mechanism to retrieve the vCPUs endianness and a way to
notify subscribed devices when it changes.

Quoted 7 lines
Quoted 4 lines

I don't think I agree, it's not the same thing. Guest should just convert and send data in LE format. Host should then convert from LE format. Target endian-ness does not come into it.

That's not in the VIRTIO 1.2 spec. We are talking about supporting things as they currently stand, not as they could have been.

Okay it's a spec bug then.
I do worry that if we just work around it now no one will bother fixing it properly though.

How do other drivers handle this? Could you check please?

Signature · 1 line
MST
#15 lore raw
In reply to Manos Pitsidianakis
Quoted 87 lines · Manos Pitsidianakis
Quoted 86 lines · Michael S. Tsirkin
Quoted 83 lines · Manos Pitsidianakis
Quoted 71 lines · Mark Cave-Ayland
Quoted 45 lines · Manos Pitsidianakis
Quoted 34 lines · Mark Cave-Ayland
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Manos Pitsidianakis
Quoted 16 lines · Manos Pitsidianakis
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime,
we need to use the device endianness, not the target
one.

Cc: [email protected]
Fixes: eb9ad377bb ("virtio-sound: handle control messages and streams")
Signed-off-by: Philippe Mathieu-Daudé <[email protected]>

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Quoted 8 lines
Quoted 3 lines

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

It feels to me that the endianness is something that should be negotiated as part of the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

Quoted 5 lines

But that is the job of the hardware drivers, isn't it? Here we are taking frames passed from the guest to its virtio driver in the format specified in the target cpu's endianness and QEMU as the device passes it to host ALSA/Pipewire/etc which in turn passes it to the actual audio hardware driver..

The problem is that the notion of target CPU endian is not fixed. For example the PowerPC CPU starts off in big-endian mode, but these days most systems will switch the CPU to little-endian mode on startup to run ppc64le. There's also the ILE bit which can be configured so that a big-endian PowerPC CPU can dynamically switch to little-endian mode when processing an interrupt, so you could potentially end up with either depending upon the current mode of the CPU.

These are the kinds of issues that led to the later virtio specifications simply using little-endian for everything, since then there is zero ambiguity over what endian is required for the virtio configuration space accesses.

It feels to me that assuming a target CPU endian is fixed for the PCM frame formats is simply repeating the mistakes of the past - and even the fact that we are discussing this within this thread suggests that at a very minimum the virtio-snd specification needs to be updated to clarify the byte ordering of the PCM frame formats.

Quoted 4 lines

ATB,

Mark.

Agreed, I think we are saying approximately the same thing here.

We need a mechanism to retrieve the vCPUs endianness and a way to
notify subscribed devices when it changes.

Quoted 7 lines
Quoted 4 lines

I don't think I agree, it's not the same thing. Guest should just convert and send data in LE format. Host should then convert from LE format. Target endian-ness does not come into it.

That's not in the VIRTIO 1.2 spec. We are talking about supporting things as they currently stand, not as they could have been.

Can you also clarify the particular case that you're trying to fix - is it big-endian on ARM, or something else?

ATB,

Mark.

#16 lore raw
Quoted 94 lines · Mark Cave-Ayland
Quoted 92 lines · Manos Pitsidianakis
Quoted 90 lines · Michael S. Tsirkin
Quoted 75 lines · Manos Pitsidianakis
Quoted 73 lines · Mark Cave-Ayland
Quoted 45 lines · Manos Pitsidianakis
Quoted 34 lines · Mark Cave-Ayland
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Manos Pitsidianakis
Quoted 16 lines · Manos Pitsidianakis
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin

On Mon, Apr 22, 2024 at 04:20:56PM +0200, Philippe Mathieu-Daudé wrote:
Since VirtIO devices can change endianness at runtime,
we need to use the device endianness, not the target
one.

Cc: [email protected]
Fixes: eb9ad377bb ("virtio-sound: handle control messages and streams")
Signed-off-by: Philippe Mathieu-Daudé <[email protected]>

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only.
It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Quoted 8 lines
Quoted 3 lines

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

It feels to me that the endianness is something that should be negotiated as part of the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

Quoted 5 lines

But that is the job of the hardware drivers, isn't it? Here we are taking frames passed from the guest to its virtio driver in the format specified in the target cpu's endianness and QEMU as the device passes it to host ALSA/Pipewire/etc which in turn passes it to the actual audio hardware driver..

The problem is that the notion of target CPU endian is not fixed. For example the PowerPC CPU starts off in big-endian mode, but these days most systems will switch the CPU to little-endian mode on startup to run ppc64le. There's also the ILE bit which can be configured so that a big-endian PowerPC CPU can dynamically switch to little-endian mode when processing an interrupt, so you could potentially end up with either depending upon the current mode of the CPU.

These are the kinds of issues that led to the later virtio specifications simply using little-endian for everything, since then there is zero ambiguity over what endian is required for the virtio configuration space accesses.

It feels to me that assuming a target CPU endian is fixed for the PCM frame formats is simply repeating the mistakes of the past - and even the fact that we are discussing this within this thread suggests that at a very minimum the virtio-snd specification needs to be updated to clarify the byte ordering of the PCM frame formats.

ATB,

Quoted 8 lines
Quoted 2 lines

Mark.

Agreed, I think we are saying approximately the same thing here.

  We need a mechanism to retrieve the vCPUs endianness and a way to notify subscribed devices when it changes.

I don't think I agree, it's not the same thing. Guest should just convert and send data in LE format. Host should then convert from LE format. Target endian-ness does not come into it.

Quoted 4 lines
Quoted 2 lines

That's not in the VIRTIO 1.2 spec. We are talking about supporting things as they currently stand, not as they could have been.

Can you also clarify the particular case that you're trying to fix - is it big-endian on ARM, or something else?

I'm only aware of big-endian on ARM.

Regards,

Phil.

#17 lore raw
Quoted 131 lines · Philippe Mathieu-Daudé
Quoted 130 lines · Mark Cave-Ayland
Quoted 127 lines · Manos Pitsidianakis
Quoted 116 lines · Michael S. Tsirkin
Quoted 114 lines · Manos Pitsidianakis
Quoted 104 lines · Mark Cave-Ayland
Quoted 65 lines · Manos Pitsidianakis
Quoted 55 lines · Mark Cave-Ayland
Quoted 48 lines · Philippe Mathieu-Daudé
Quoted 39 lines · Manos Pitsidianakis
Quoted 33 lines · Manos Pitsidianakis
Quoted 23 lines · Michael S. Tsirkin
Quoted 19 lines · Philippe Mathieu-Daudé

On 22/4/24 23:02, Michael S. Tsirkin wrote:
On Mon, Apr 22, 2024 at
04:20:56PM +0200,
Philippe Mathieu-Daudé
wrote:
Since VirtIO devices can change endianness at runtime,
we need to use the device endianness, not the target
one.

Cc: [email protected]
Fixes: eb9ad377bb
("virtio-sound:
handle control
messages and
streams")
Signed-off-by: Philippe Mathieu-Daudé <[email protected]>

Quoted 4 lines
Quoted 4 lines

This is all completely
bogus. Virtio SND is
from Virtio 1.0 only.
It is unconditionally little endian.

Quoted 3 lines

This part of the code is for PCM
frames (raw bytes), not virtio spec
fields (which indeed must be LE in modern VIRTIO).

Quoted 7 lines

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Quoted 3 lines

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

Quoted 7 lines

It feels to me that the endianness is something that should be negotiated as part of
the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

Quoted 7 lines

But that is the job of the hardware drivers, isn't it? Here we are taking frames passed from the guest to its virtio driver in the format
specified in the target cpu's endianness and QEMU as the device passes
it to host ALSA/Pipewire/etc which in turn passes it to the actual audio hardware driver..

The problem is that the notion of target CPU endian is not fixed. For example the
PowerPC CPU starts off in big-endian mode, but these days most systems will switch
the CPU to little-endian mode on startup to run ppc64le. There's also the ILE bit
which can be configured so that a big-endian PowerPC CPU can dynamically switch to
little-endian mode when processing an interrupt, so you could potentially end up with
either depending upon the current mode of the CPU.

These are the kinds of issues that led to the later virtio specifications simply
using little-endian for everything, since then there is zero ambiguity over what
endian is required for the virtio configuration space accesses.

It feels to me that assuming a target CPU endian is fixed for the PCM frame formats
is simply repeating the mistakes of the past - and even the fact that we are
discussing this within this thread suggests that at a very minimum the virtio-snd
specification needs to be updated to clarify the byte ordering of the PCM frame formats.

Quoted 4 lines

ATB,

Mark.

Agreed, I think we are saying approximately the same thing here.

Quoted 7 lines
Quoted 2 lines

  We need a mechanism to retrieve the vCPUs endianness and a way to notify subscribed devices when it changes.

I don't think I agree, it's not the same thing. Guest should just convert and send data in LE format. Host should then convert from LE format. Target endian-ness does not come into it.

That's not in the VIRTIO 1.2 spec. We are talking about supporting things as they currently stand, not as they could have been.

Quoted 8 lines
Quoted 2 lines

Can you also clarify the particular case that you're trying to fix - is it big-endian on ARM, or something else?

I'm only aware of big-endian on ARM.

Regards,

Phil.

But of course this applies to any BE emulated on LE or vice versa.

Signature · 1 line
MST
#18 lore raw
In reply to Manos Pitsidianakis
Quoted 81 lines · Manos Pitsidianakis
Quoted 61 lines · Mark Cave-Ayland
Quoted 45 lines · Manos Pitsidianakis
Quoted 34 lines · Mark Cave-Ayland
Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Manos Pitsidianakis
Quoted 16 lines · Manos Pitsidianakis
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Philippe Mathieu-Daudé

Since VirtIO devices can change endianness at runtime, we need to use the device endianness, not the target one.

Fixes eb9ad377bb ("virtio-sound: handle control messages and streams")
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This is all completely bogus. Virtio SND is from Virtio 1.0 only. It is unconditionally little endian.

This part of the code is for PCM frames (raw bytes), not virtio spec fields (which indeed must be LE in modern VIRTIO).

Thought a little more about it. We should keep the target's endianness here, if it's mutable then we should query the machine the device is attached to somehow. the virtio device should never change endianness like Michael says since it's not legacy.

Quoted 8 lines
Quoted 3 lines

Grr. So as Richard suggested, this need to be pass as a device property then.
(https://lore.kernel.org/qemu-devel/[email protected]/)

It feels to me that the endianness is something that should be negotiated as part of the frame format, since the endianness of the audio hardware can be different from that of the CPU (think PReP machines where it was common that a big endian CPU is driving little endian hardware as found on x86).

Quoted 5 lines

But that is the job of the hardware drivers, isn't it? Here we are taking frames passed from the guest to its virtio driver in the format specified in the target cpu's endianness and QEMU as the device passes it to host ALSA/Pipewire/etc which in turn passes it to the actual audio hardware driver..

The problem is that the notion of target CPU endian is not fixed. For example the PowerPC CPU starts off in big-endian mode, but these days most systems will switch the CPU to little-endian mode on startup to run ppc64le. There's also the ILE bit which can be configured so that a big-endian PowerPC CPU can dynamically switch to little-endian mode when processing an interrupt, so you could potentially end up with either depending upon the current mode of the CPU.

Quoted 8 lines

These are the kinds of issues that led to the later virtio specifications simply using little-endian for everything, since then there is zero ambiguity over what endian is required for the virtio configuration space accesses.

It feels to me that assuming a target CPU endian is fixed for the PCM frame formats is simply repeating the mistakes of the past - and even the fact that we are discussing this within this thread suggests that at a very minimum the virtio-snd specification needs to be updated to clarify the byte ordering of the PCM frame formats.

Agreed, I think we are saying approximately the same thing here.

We need a mechanism to retrieve the vCPUs endianness and a way to
notify subscribed devices when it changes.

I think that then, since the virtio device is mostly certain of the
correct target endianness and completely certain of the host endianness, it can perform the necessary conversions.

Quoted 7 lines

I don't recall seeing a restriction on the byte ordering of PCM
formats other than the CPU order; except for the ones which have explicit endianness in their definitions. Please correct me if I am wrong!

A straightforward solution would be to set an endianness change notify callback in DeviceClass. What do you think?

Well, I guess... but why propose such a complex solution when you can simply specify the endianness of the PCM frame? If you think back to pre-1.0 virtio where everything was done using target vCPU endianness, why did they not implement this solution back then to solve the problem instead of mandating that virtio-1.0 onwards should simply use little-endian? Clearly specifying that all implementations should use little-endian was the deemed to be the best way to solve all these issues.

Have you raised this issue on the virtio-dev mailing list? It's worth explaining the problem you're trying to solve there, since hopefully the person or persons who designed and proposed the virtio-snd specification will be able to give you the correct advice.

ATB,

Mark.