Skip to contentSkip to conversation

hw/misc/mos6522: Fix bad class definition of the MOS6522 device

PATCH In review Thomas Huth 3 participants 5 messages Nov 14, 2024 – Nov 18, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/misc/mos6522: Fix bad class definition of the MOS6522 device

When compiling QEMU with --enable-cfi, the "q800" m68k machine currently crashes very early, when the q800_machine_init() function tries to wire the interrupts of the "via1" device.
This happens because TYPE_MOS6522_Q800_VIA1 is supposed to be a proper SysBus device, but its parent (TYPE_MOS6522) has a mistake in its class definition where it is only derived from DeviceClass, and not from SysBusDeviceClass, so we end up in funny memory access issues here. Using the right class hierarchy for the MOS6522 device fixes the problem.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2675

---
 include/hw/misc/mos6522.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/misc/mos6522.h +1 -1
index fba45668ab..920871a598 100644
@@ -154,7 +154,7 @@ struct MOS6522State {
 OBJECT_DECLARE_TYPE(MOS6522State, MOS6522DeviceClass, MOS6522)
 
 struct MOS6522DeviceClass {
-    DeviceClass parent_class;
+    SysBusDeviceClass parent_class;
 
     ResettablePhases parent_phases;
     void (*portB_write)(MOS6522State *dev);
Signature · 1 line
2.47.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · Thomas Huth

When compiling QEMU with --enable-cfi, the "q800" m68k machine currently crashes very early, when the q800_machine_init() function tries to wire the interrupts of the "via1" device.
This happens because TYPE_MOS6522_Q800_VIA1 is supposed to be a proper SysBus device, but its parent (TYPE_MOS6522) has a mistake in its class definition where it is only derived from DeviceClass, and not from SysBusDeviceClass, so we end up in funny memory access issues here. Using the right class hierarchy for the MOS6522 device fixes the problem.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2675

---
  include/hw/misc/mos6522.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/misc/mos6522.h +0 -0
index fba45668ab..920871a598 100644

include/hw/misc/mos6522.h struct MOS6522State { · line 160

Quoted 8 lines
include/hw/misc/mos6522.h +1 -1
@@ -154,7 +154,7 @@ struct MOS6522State {
  OBJECT_DECLARE_TYPE(MOS6522State, MOS6522DeviceClass, MOS6522)
 
  struct MOS6522DeviceClass {
-    DeviceClass parent_class;
+    SysBusDeviceClass parent_class;
 
      ResettablePhases parent_phases;
      void (*portB_write)(MOS6522State *dev);

Ooof. I suspect I started using DeviceClass first before switching to SysBusDeviceClass later to implement reset functionality. Anyhow the patch looks good: thanks Thomas!

ATB,

Mark.

#2 lore raw
Quoted 32 lines · Mark Cave-Ayland
Quoted 21 lines · Thomas Huth

When compiling QEMU with --enable-cfi, the "q800" m68k machine currently crashes very early, when the q800_machine_init() function tries to wire the interrupts of the "via1" device.
This happens because TYPE_MOS6522_Q800_VIA1 is supposed to be a proper SysBus device, but its parent (TYPE_MOS6522) has a mistake in its class definition where it is only derived from DeviceClass, and not from SysBusDeviceClass, so we end up in funny memory access issues here. Using the right class hierarchy for the MOS6522 device fixes the problem.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2675

---
  include/hw/misc/mos6522.h | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/misc/mos6522.h +0 -0
index fba45668ab..920871a598 100644

include/hw/misc/mos6522.h struct MOS6522State { · line 158

Quoted 6 lines
include/hw/misc/mos6522.h +1 -1
@@ -154,7 +154,7 @@ struct MOS6522State {
   OBJECT_DECLARE_TYPE(MOS6522State, MOS6522DeviceClass, MOS6522)
   struct MOS6522DeviceClass {
-    DeviceClass parent_class;
+    SysBusDeviceClass parent_class;
       ResettablePhases parent_phases;
       void (*portB_write)(MOS6522State *dev);

Ooof. I suspect I started using DeviceClass first before switching to SysBusDeviceClass later to implement reset functionality. Anyhow the patch looks good: thanks Thomas!

Quoted 1 line

Thanks!

By the way, fun fact why this problem was likely only triggering with CFI enabled: SysBusDeviceClass just extends DeviceClass by two function pointers, second one is connect_irq_notifier. This one gets called from sysbus_connect_irq() (which is called by q800_machine_init()).

But MOS6522DeviceClass is directly derived from DeviceClass, that memory space is filled by the ResettablePhases struct instead. So instead of calling an irq notifier, the sysbus_connect_irq() likely called the ResettableHoldPhase() function instead! It likely did not crash without CFI, since the parameters are similar enough, but with CFI, this got successfully flagged as an illegal call via function pointer :-)

Thomas

Reviewed-by on v1 1/1
#3 lore raw
In reply to Thomas Huth
Quoted 10 lines · Thomas Huth

When compiling QEMU with --enable-cfi, the "q800" m68k machine currently crashes very early, when the q800_machine_init() function tries to wire the interrupts of the "via1" device.
This happens because TYPE_MOS6522_Q800_VIA1 is supposed to be a proper SysBus device, but its parent (TYPE_MOS6522) has a mistake in its class definition where it is only derived from DeviceClass, and not from SysBusDeviceClass, so we end up in funny memory access issues here. Using the right class hierarchy for the MOS6522 device fixes the problem.

Quoted 5 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2675

---
  include/hw/misc/mos6522.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Fixes 51f233ec92 ("misc: introduce new mos6522 VIA device")

and Cc qemu-stable?

#4 lore raw
In reply to Thomas Huth
Quoted 10 lines · Thomas Huth

When compiling QEMU with --enable-cfi, the "q800" m68k machine currently crashes very early, when the q800_machine_init() function tries to wire the interrupts of the "via1" device.
This happens because TYPE_MOS6522_Q800_VIA1 is supposed to be a proper SysBus device, but its parent (TYPE_MOS6522) has a mistake in its class definition where it is only derived from DeviceClass, and not from SysBusDeviceClass, so we end up in funny memory access issues here. Using the right class hierarchy for the MOS6522 device fixes the problem.

Quoted 5 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2675

---
  include/hw/misc/mos6522.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Patch queued to hw-misc, thanks.