Skip to contentSkip to conversation

hw/audio/gus: Fix registers 32-bit access

PATCH In review Allan Peramaki 5 participants 11 messages Jun 15, 2020 – Jun 18, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/audio/gus: Fix registers 32-bit access

Fix audio on software that accesses DRAM above 64k via register peek/poke and some cases when more than 16 voices are used.

Fixes 135f5ae1974c ("audio: GUSsample is int16_t")
---
 hw/audio/gusemu_hal.c   | 6 +++---
 hw/audio/gusemu_mixer.c | 8 ++++----
 2 files changed, 7 insertions(+), 7 deletions(-)
hw/audio/gusemu_hal.c +3 -3
index ae40ca341c..e35e941926 100644
@@ -30,9 +30,9 @@
 #include "gustate.h"
 #include "gusemu.h"
 
-#define GUSregb(position) (*            (gusptr+(position)))
-#define GUSregw(position) (*(uint16_t *) (gusptr+(position)))
-#define GUSregd(position) (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position) (*(gusptr + (position)))
+#define GUSregw(position) (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position) (*(uint32_t *)(gusptr + (position)))
 
 /* size given in bytes */
 unsigned int gus_read(GUSEmuState * state, int port, int size)
hw/audio/gusemu_mixer.c +4 -4
index 00b9861b92..3b39254518 100644
@@ -26,11 +26,11 @@
 #include "gusemu.h"
 #include "gustate.h"
 
-#define GUSregb(position)  (*            (gusptr+(position)))
-#define GUSregw(position)  (*(uint16_t *) (gusptr+(position)))
-#define GUSregd(position)  (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position)  (*(gusptr + (position)))
+#define GUSregw(position)  (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position)  (*(uint32_t *)(gusptr + (position)))
 
-#define GUSvoice(position) (*(uint16_t *)(voiceptr+(position)))
+#define GUSvoice(position) (*(uint16_t *)(voiceptr + (position)))
 
 /* samples are always 16bit stereo (4 bytes each, first right then left interleaved) */
 void gus_mixvoices(GUSEmuState * state, unsigned int playback_freq, unsigned int numsamples,
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 37 lines

Fix audio on software that accesses DRAM above 64k via register peek/poke and some cases when more than 16 voices are used.

Fixes 135f5ae1974c ("audio: GUSsample is int16_t")
---
 hw/audio/gusemu_hal.c   | 6 +++---
 hw/audio/gusemu_mixer.c | 8 ++++----
 2 files changed, 7 insertions(+), 7 deletions(-)
hw/audio/gusemu_hal.c +3 -3
index ae40ca341c..e35e941926 100644
@@ -30,9 +30,9 @@
 #include "gustate.h"
 #include "gusemu.h"
 
-#define GUSregb(position) (*            (gusptr+(position)))
-#define GUSregw(position) (*(uint16_t *) (gusptr+(position)))
-#define GUSregd(position) (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position) (*(gusptr + (position)))
+#define GUSregw(position) (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position) (*(uint32_t *)(gusptr + (position)))
 
 /* size given in bytes */
 unsigned int gus_read(GUSEmuState * state, int port, int size)
hw/audio/gusemu_mixer.c +0 -2
index 00b9861b92..3b39254518 100644
@@ -26,11 +26,11 @@
 #include "gusemu.h"
 #include "gustate.h"
 
-#define GUSregb(position)  (*            (gusptr+(position)))
-#define GUSregw(position)  (*(uint16_t *) (gusptr+(position)))

hw/audio/gusemu_mixer.c line 36

Quoted 10 lines
hw/audio/gusemu_mixer.c +4 -2
-#define GUSregd(position)  (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position)  (*(gusptr + (position)))
+#define GUSregw(position)  (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position)  (*(uint32_t *)(gusptr + (position)))
 
-#define GUSvoice(position) (*(uint16_t *)(voiceptr+(position)))
+#define GUSvoice(position) (*(uint16_t *)(voiceptr + (position)))
 
 /* samples are always 16bit stereo (4 bytes each, first right then left interleaved) */
 void gus_mixvoices(GUSEmuState * state, unsigned int playback_freq, unsigned int numsamples,

With this patch I can hear the sound effects of DOOM Shareware in a FreeDOS 1.2 guest with a GUS audio device.

#2 lore raw
In reply to Allan Peramaki
Quoted 6 lines · Allan Peramaki

Fix audio on software that accesses DRAM above 64k via register peek/poke and some cases when more than 16 voices are used.

Fixes 135f5ae1974c ("audio: GUSsample is int16_t")

This patch is quite difficult to read because it mixes some whitespace only changes with some actual changes of behaviour.

hw/audio/gusemu_hal.c line 35

Quoted 6 lines
hw/audio/gusemu_hal.c +3 -3
-#define GUSregb(position) (*            (gusptr+(position)))
-#define GUSregw(position) (*(uint16_t *) (gusptr+(position)))
-#define GUSregd(position) (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position) (*(gusptr + (position)))
+#define GUSregw(position) (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position) (*(uint32_t *)(gusptr + (position)))

So, I think the actual bugfix change here is just the changing of uint16_t to uint32_t in the GUSregd definition...

hw/audio/gusemu_hal.c line 35

Quoted 6 lines
hw/audio/gusemu_hal.c +3 -3
-#define GUSregb(position)  (*            (gusptr+(position)))
-#define GUSregw(position)  (*(uint16_t *) (gusptr+(position)))
-#define GUSregd(position)  (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position)  (*(gusptr + (position)))
+#define GUSregw(position)  (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position)  (*(uint32_t *)(gusptr + (position)))

...and similarly here, and all the other changes are purely cleaning up the spaces. Is that right?

hw/audio/gusemu_mixer.c line 33

Quoted 2 lines
hw/audio/gusemu_mixer.c +1 -1
-#define GUSvoice(position) (*(uint16_t *)(voiceptr+(position)))
+#define GUSvoice(position) (*(uint16_t *)(voiceptr + (position)))

Are these accesses all guaranteed to be correctly aligned to be 16 or 32 bit loads/stores ? Otherwise it would be better to use the ldl_p/stl_p/ldw_p/stw_p/etc accessors, which correctly handle possibly misaligned pointers.

thanks
-- PMM

#3 lore raw
Quoted 4 lines · Peter Maydell

This patch is quite difficult to read because it mixes some whitespace only changes with some actual changes of behaviour.

Sorry about that. I had to put some whitespace in the two lines I modified because of checkpatch.pl, but then the nearby lines would have had inconsistent style if left unmodified. On the other hand, QEMU wiki said it is ok to fix style issues in the immediate area.

Quoted 12 lines

So, I think the actual bugfix change here is just the changing of uint16_t to uint32_t in the GUSregd definition...

hw/audio/gusemu_hal.c line 35

Quoted 6 lines
hw/audio/gusemu_hal.c +3 -3
-#define GUSregb(position)  (*            (gusptr+(position)))
-#define GUSregw(position)  (*(uint16_t *) (gusptr+(position)))
-#define GUSregd(position)  (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position)  (*(gusptr + (position)))
+#define GUSregw(position)  (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position)  (*(uint32_t *)(gusptr + (position)))

...and similarly here, and all the other changes are purely cleaning up the spaces. Is that right?

That's right. The only real change is uint16_t -> uint32_t in GUSregd. This reverses what seems to be an accident in commit 135f5ae1974c, where GUSdword (defined as "unsigned int" or "uint32_t" depending on compiler) was replaced with uint16_t. That change broke applications that read/write DRAM above 64k via I/O ports (in lieu of using DMA.)

Quoted 8 lines

hw/audio/gusemu_mixer.c line 33

Quoted 3 lines
hw/audio/gusemu_mixer.c +1 -1
-#define GUSvoice(position) (*(uint16_t *)(voiceptr+(position)))
+#define GUSvoice(position) (*(uint16_t *)(voiceptr + (position)))

Are these accesses all guaranteed to be correctly aligned to be 16 or 32 bit loads/stores ? Otherwise it would be better to use the ldl_p/stl_p/ldw_p/stw_p/etc accessors, which correctly handle possibly misaligned pointers.

Yes (assuming compiler aligns struct fields properly.) It is not obvious though (and easy to break), so I suppose refactoring much of the GUS code would be nice. For example, the few places where GUSregd(position) is used, position is 2 (mod 4). On the other hand, gusptr is also 2 (mod 4) bytes (making gusptr+position = 0 (mod 4)), because we have the struct

typedef struct GUSState {
    ISADevice dev;
    GUSEmuState emu;
    QEMUSoundCard card;
    uint32_t freq;
    uint32_t port;
    int pos, left, shift, irqs;
    int16_t *mixbuf;
    uint8_t himem[1024 * 1024 + 32 + 4096];
    int samples;
    SWVoiceOut *voice;
    int64_t last_ticks;
    qemu_irq pic;
    IsaDma *isa_dma;
    PortioList portio_list1;
    PortioList portio_list2;
} GUSState;

where himem is 2 (mod 4). When GUSregd() is used, gusptr is (himem + 1024 * 1024 + 32), so gusptr is also 2 (mod 4). Similar applies to GUSvoice(position).

Best regards,
Allan

#4 lore raw
Quoted 7 lines · Allan Peramaki
Quoted 4 lines · Peter Maydell

Are these accesses all guaranteed to be correctly aligned to be 16 or 32 bit loads/stores ? Otherwise it would be better to use the ldl_p/stl_p/ldw_p/stw_p/etc accessors, which correctly handle possibly misaligned pointers.

Yes (assuming compiler aligns struct fields properly.) It is not obvious though (and easy to break), so I suppose refactoring much of the GUS code would be nice. For example, the few places where GUSregd(position) is used, position is 2 (mod 4). On the other hand, gusptr is also 2 (mod 4) bytes (making gusptr+position = 0 (mod 4)), because we have the struct

It is aligned, but my reasoning had a mistake. Both gusptr and position (GUSDRAMPOS24bit and voicewavetableirq) are 0 (mod 4). (mixbuf is a pointer.) Sorry.

Quoted 10 lines

typedef struct GUSState {
    ISADevice dev;
    GUSEmuState emu;
    QEMUSoundCard card;
    uint32_t freq;
    uint32_t port;
    int pos, left, shift, irqs;
    int16_t *mixbuf;
    uint8_t himem[1024 * 1024 + 32 + 4096];

Quoted 10 lines

    int samples;
    SWVoiceOut *voice;
    int64_t last_ticks;
    qemu_irq pic;
    IsaDma *isa_dma;
    PortioList portio_list1;
    PortioList portio_list2;
} GUSState;

Best regards,
Allan

Reviewed-by
#5 lore raw
In reply to Allan Peramaki
Quoted 9 lines · Allan Peramaki
Quoted 4 lines · Peter Maydell

This patch is quite difficult to read because it mixes some whitespace only changes with some actual changes of behaviour.

Sorry about that. I had to put some whitespace in the two lines I modified because of checkpatch.pl, but then the nearby lines would have had inconsistent style if left unmodified.

Hi Allan!

Makes perfect sense, but for the review, it might have been helpful if you'd put this information in the commit message.

Anyway, the change looks correct to me, so:

Reviewed-by
#6 lore raw
In reply to Allan Peramaki
Quoted 11 lines · Allan Peramaki
Quoted 4 lines · Peter Maydell

This patch is quite difficult to read because it mixes some whitespace only changes with some actual changes of behaviour.

Sorry about that. I had to put some whitespace in the two lines I modified because of checkpatch.pl, but then the nearby lines would have had inconsistent style if left unmodified. On the other hand, QEMU wiki said it is ok to fix style issues in the immediate area.

Thanks for the explanation.

Yeah, we usually allow that (largely with the goal of "make the changes that keep checkpatch from complaining about a patch"). I think the changes here just ended up a bit more lost in the style fixes than they would for some cases (eg if the style fixup is just "add braces to an if"). One approach if the style-fixups swamp the actual content is to split into two, so patch 1 is purely style fixups and patch 2 the bugfix (or the other way round); but it's certainly not worth rerolling this patch just for that.

-- PMM

#7 lore raw
In reply to Peter Maydell
Quoted 11 lines · Peter Maydell
Quoted 6 lines · Allan Peramaki

Fix audio on software that accesses DRAM above 64k via register peek/poke and some cases when more than 16 voices are used.

Fixes 135f5ae1974c ("audio: GUSsample is int16_t")

This patch is quite difficult to read because it mixes some whitespace only changes with some actual changes of behaviour.

In such cases the author might add a comment in the commit description "this patch is easier to review with git-diff -w" or other options.

Allan, can we get the patch with only the changed lines?

See:

---

hw/audio/gusemu_hal.c +1 -1
index ae40ca341c..e35e941926 100644
@@ -32,7 +32,7 @@
 
 #define GUSregb(position) (*(gusptr + (position)))
 #define GUSregw(position) (*(uint16_t *)(gusptr + (position)))
-#define GUSregd(position) (*(uint16_t *)(gusptr+(position)))
+#define GUSregd(position) (*(uint32_t *)(gusptr + (position)))
 
 /* size given in bytes */
 unsigned int gus_read(GUSEmuState * state, int port, int size)
hw/audio/gusemu_mixer.c +1 -1
index 00b9861b92..3b39254518 100644
@@ -28,7 +28,7 @@
 
 #define GUSregb(position)  (*(gusptr + (position)))
 #define GUSregw(position)  (*(uint16_t *)(gusptr + (position)))
-#define GUSregd(position)  (*(uint16_t *)(gusptr+(position)))
+#define GUSregd(position)  (*(uint32_t *)(gusptr + (position)))
 
 #define GUSvoice(position) (*(uint16_t *)(voiceptr + (position)))
 

---

Quoted 32 lines

Review

Quoted 7 lines
(quoted diff) +3 -3
-#define GUSregb(position) (*            (gusptr+(position)))
-#define GUSregw(position) (*(uint16_t *) (gusptr+(position)))
-#define GUSregd(position) (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position) (*(gusptr + (position)))
+#define GUSregw(position) (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position) (*(uint32_t *)(gusptr + (position)))

So, I think the actual bugfix change here is just the changing of uint16_t to uint32_t in the GUSregd definition...

Review

Quoted 6 lines
(quoted diff) +3 -3
-#define GUSregb(position)  (*            (gusptr+(position)))
-#define GUSregw(position)  (*(uint16_t *) (gusptr+(position)))
-#define GUSregd(position)  (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position)  (*(gusptr + (position)))
+#define GUSregw(position)  (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position)  (*(uint32_t *)(gusptr + (position)))

...and similarly here, and all the other changes are purely cleaning up the spaces. Is that right?

Review

Quoted 2 lines
(quoted diff) +1 -1
-#define GUSvoice(position) (*(uint16_t *)(voiceptr+(position)))
+#define GUSvoice(position) (*(uint16_t *)(voiceptr + (position)))

Are these accesses all guaranteed to be correctly aligned to be 16 or 32 bit loads/stores ? Otherwise it would be better to use the ldl_p/stl_p/ldw_p/stw_p/etc accessors, which correctly handle possibly misaligned pointers.

thanks
-- PMM

#8 lore raw
Quoted 14 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Peter Maydell
Quoted 6 lines · Allan Peramaki

Fix audio on software that accesses DRAM above 64k via register peek/poke and some cases when more than 16 voices are used.

Fixes 135f5ae1974c ("audio: GUSsample is int16_t")

This patch is quite difficult to read because it mixes some whitespace only changes with some actual changes of behaviour.

Quoted 5 lines

In such cases the author might add a comment in the commit description "this patch is easier to review with git-diff -w" or other options.

Allan, can we get the patch with only the changed lines?

Is it worth the effort of a respin at this point?

thanks
-- PMM

#9 lore raw
Quoted 15 lines · Peter Maydell
Quoted 14 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Peter Maydell
Quoted 6 lines · Allan Peramaki

Fix audio on software that accesses DRAM above 64k via register peek/poke and some cases when more than 16 voices are used.

Fixes 135f5ae1974c ("audio: GUSsample is int16_t")

This patch is quite difficult to read because it mixes some whitespace only changes with some actual changes of behaviour.

Quoted 8 lines
Quoted 5 lines

In such cases the author might add a comment in the commit description "this patch is easier to review with git-diff -w" or other options.

Allan, can we get the patch with only the changed lines?

Is it worth the effort of a respin at this point?

I'll give it a try to see if it takes me less than 2min.

#10 lore raw
In reply to Allan Peramaki
Quoted 38 lines · Allan Peramaki

Fix audio on software that accesses DRAM above 64k via register peek/poke and some cases when more than 16 voices are used.

Fixes 135f5ae1974c ("audio: GUSsample is int16_t")
---
 hw/audio/gusemu_hal.c   | 6 +++---
 hw/audio/gusemu_mixer.c | 8 ++++----
 2 files changed, 7 insertions(+), 7 deletions(-)
hw/audio/gusemu_hal.c +3 -3
index ae40ca341c..e35e941926 100644
@@ -30,9 +30,9 @@
 #include "gustate.h"
 #include "gusemu.h"
 
-#define GUSregb(position) (*            (gusptr+(position)))
-#define GUSregw(position) (*(uint16_t *) (gusptr+(position)))
-#define GUSregd(position) (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position) (*(gusptr + (position)))
+#define GUSregw(position) (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position) (*(uint32_t *)(gusptr + (position)))
 
 /* size given in bytes */
 unsigned int gus_read(GUSEmuState * state, int port, int size)
hw/audio/gusemu_mixer.c +0 -2
index 00b9861b92..3b39254518 100644
@@ -26,11 +26,11 @@
 #include "gusemu.h"
 #include "gustate.h"
 
-#define GUSregb(position)  (*            (gusptr+(position)))
-#define GUSregw(position)  (*(uint16_t *) (gusptr+(position)))

hw/audio/gusemu_mixer.c line 36

Quoted 10 lines
hw/audio/gusemu_mixer.c +4 -2
-#define GUSregd(position)  (*(uint16_t *)(gusptr+(position)))
+#define GUSregb(position)  (*(gusptr + (position)))
+#define GUSregw(position)  (*(uint16_t *)(gusptr + (position)))
+#define GUSregd(position)  (*(uint32_t *)(gusptr + (position)))
 
-#define GUSvoice(position) (*(uint16_t *)(voiceptr+(position)))
+#define GUSvoice(position) (*(uint16_t *)(voiceptr + (position)))
 
 /* samples are always 16bit stereo (4 bytes each, first right then left interleaved) */
 void gus_mixvoices(GUSEmuState * state, unsigned int playback_freq, unsigned int numsamples,

This might be a good candidate for the stable branches, too (now on the
CC: list).

Thomas