Skip to contentSkip to conversation

fw_cfg: misc fixes

PATCH In review Michael S. Tsirkin 4 participants 14 messages May 30, 2013 – May 31, 2013

lore
Share & tools
lore
#0 lore raw

cover fw_cfg: misc fixes

Here are some misc fixes to the fw cfg object handling.
I've queued them on my pci branch temporarily as it's useful to cleanup some pci things. I'm using this with Laszlo's cleanup patch that got rid of void * in pc.c too - that's queued there as well.

Please review and comment.

Michael S. Tsirkin (3):
pvpanic: use FWCfgState explicitly
fw_cfg: add API to find FW cfg object
fw_cfg: fw_cfg is a singleton

 hw/misc/pvpanic.c         |  4 ++--
 hw/nvram/fw_cfg.c         | 18 +++++++++++++-----
 include/hw/nvram/fw_cfg.h |  2 ++
 3 files changed, 17 insertions(+), 7 deletions(-)
Signature · 1 line
MST
#1 lore raw

PATCH 1/3 pvpanic: use FWCfgState explicitly

Use the type-safe FWCfgState structure instead
of the unsafe void *.

---
 hw/misc/pvpanic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/pvpanic.c +1 -1
index 31e1b1d..1483f27 100644
@@ -90,7 +90,7 @@ static int pvpanic_isa_initfn(ISADevice *dev)
 {
     PVPanicState *s = ISA_PVPANIC_DEVICE(dev);
     static bool port_configured;
-    void *fw_cfg;
+    FWCfgState *fw_cfg;
 
     memory_region_init_io(&s->io, &pvpanic_ops, s, "pvpanic", 1);
     isa_register_ioport(dev, &s->io, s->ioport);
Signature · 1 line
MST
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] pvpanic: use FWCfgState explicitly

Quoted 14 lines · Michael S. Tsirkin

Use the type-safe FWCfgState structure instead
of the unsafe void *.

---
 hw/misc/pvpanic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/pvpanic.c +0 -0
index 31e1b1d..1483f27 100644

hw/misc/pvpanic.c static int pvpanic_isa_initfn(ISADevice *dev) · line 96

Quoted 8 lines
hw/misc/pvpanic.c +1 -1
@@ -90,7 +90,7 @@ static int pvpanic_isa_initfn(ISADevice *dev)
 {
     PVPanicState *s = ISA_PVPANIC_DEVICE(dev);
     static bool port_configured;
-    void *fw_cfg;
+    FWCfgState *fw_cfg;
 
     memory_region_init_io(&s->io, &pvpanic_ops, s, "pvpanic", 1);
     isa_register_ioport(dev, &s->io, s->ioport);

Doesn't this break your build? Lower down in the function there's

        fw_cfg = object_resolve_path("/machine/fw_cfg", NULL);

and object_resolve_path() returns a pointer-to-Object, not pointer-to-FWCfgState.

But for starters I'm quite confused about how the unpatched function works. What it does amounts to:

  fw_cfg_add_file(object_resolve_path(...), ...);

But, again object_resolve_path() returns pointer-to-Object. I'm checking "struct Object" in "include/qom/object.h", and it suggests that derived structs should embed Object as first member. However FWCfgState is *not* such a derived member. What's going on here?

http://thread.gmane.org/gmane.comp.emulators.qemu/201544/focus=201564
http://thread.gmane.org/gmane.comp.emulators.qemu/204452/focus=204450

Laszlo

#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] pvpanic: use FWCfgState explicitly

Il 30/05/2013 17:05, Laszlo Ersek ha scritto:

Quoted 7 lines

But, again object_resolve_path() returns pointer-to-Object. I'm checking "struct Object" in "include/qom/object.h", and it suggests that derived structs should embed Object as first member. However FWCfgState is *not* such a derived member. What's going on here?

http://thread.gmane.org/gmane.comp.emulators.qemu/201544/focus=201564
http://thread.gmane.org/gmane.comp.emulators.qemu/204452/focus=204450

FWCfgState embeds Object via SysBusDevice and DeviceState.

Paolo

#4 lore raw

Re: [Qemu-devel] [PATCH 1/3] pvpanic: use FWCfgState explicitly

Quoted 10 lines · Paolo Bonzini

Il 30/05/2013 17:05, Laszlo Ersek ha scritto:

Quoted 7 lines

But, again object_resolve_path() returns pointer-to-Object. I'm checking "struct Object" in "include/qom/object.h", and it suggests that derived structs should embed Object as first member. However FWCfgState is *not* such a derived member. What's going on here?

http://thread.gmane.org/gmane.comp.emulators.qemu/201544/focus=201564
http://thread.gmane.org/gmane.comp.emulators.qemu/204452/focus=204450

FWCfgState embeds Object via SysBusDevice and DeviceState.

Clearly an evil trick by a devious mind.

Thanks :)
Laszlo

#5 lore raw

Re: [Qemu-devel] [PATCH 1/3] pvpanic: use FWCfgState explicitly

In reply to Laszlo Ersek
Quoted 26 lines · Laszlo Ersek
Quoted 14 lines · Michael S. Tsirkin

Use the type-safe FWCfgState structure instead
of the unsafe void *.

---
 hw/misc/pvpanic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/pvpanic.c +0 -0
index 31e1b1d..1483f27 100644

hw/misc/pvpanic.c static int pvpanic_isa_initfn(ISADevice *dev) · line 96

Quoted 8 lines
hw/misc/pvpanic.c +1 -1
@@ -90,7 +90,7 @@ static int pvpanic_isa_initfn(ISADevice *dev)
 {
     PVPanicState *s = ISA_PVPANIC_DEVICE(dev);
     static bool port_configured;
-    void *fw_cfg;
+    FWCfgState *fw_cfg;
 
     memory_region_init_io(&s->io, &pvpanic_ops, s, "pvpanic", 1);
     isa_register_ioport(dev, &s->io, s->ioport);

Doesn't this break your build? Lower down in the function there's

Quoted 4 lines
        fw_cfg = object_resolve_path("/machine/fw_cfg", NULL);

and object_resolve_path() returns a pointer-to-Object, not pointer-to-FWCfgState.

Paolo explained the guts, but don't we still need a downcast here? (No idea how to do that nicely in the object model du jour -- maybe OBJECT_CHECK() or similar?)

Laszlo

#6 lore raw

Re: [Qemu-devel] [PATCH 1/3] pvpanic: use FWCfgState explicitly

Il 30/05/2013 18:03, Laszlo Ersek ha scritto:

Quoted 27 lines · Laszlo Ersek
Quoted 26 lines · Michael S. Tsirkin
Quoted 14 lines

Use the type-safe FWCfgState structure instead
of the unsafe void *.

---
 hw/misc/pvpanic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/pvpanic.c +0 -0
index 31e1b1d..1483f27 100644

hw/misc/pvpanic.c static int pvpanic_isa_initfn(ISADevice *dev) · line 96

Quoted 8 lines
hw/misc/pvpanic.c +1 -1
@@ -90,7 +90,7 @@ static int pvpanic_isa_initfn(ISADevice *dev)
 {
     PVPanicState *s = ISA_PVPANIC_DEVICE(dev);
     static bool port_configured;
-    void *fw_cfg;
+    FWCfgState *fw_cfg;
 
     memory_region_init_io(&s->io, &pvpanic_ops, s, "pvpanic", 1);
     isa_register_ioport(dev, &s->io, s->ioport);

Doesn't this break your build? Lower down in the function there's

Quoted 8 lines
Quoted 4 lines
        fw_cfg = object_resolve_path("/machine/fw_cfg", NULL);

and object_resolve_path() returns a pointer-to-Object, not pointer-to-FWCfgState.

Paolo explained the guts, but don't we still need a downcast here? (No idea how to do that nicely in the object model du jour -- maybe OBJECT_CHECK() or similar?)

Patch 2 addresses that.

Paolo

#7 lore raw

Re: [Qemu-devel] [PATCH 1/3] pvpanic: use FWCfgState explicitly

In reply to Laszlo Ersek
Quoted 26 lines · Laszlo Ersek
Quoted 14 lines · Michael S. Tsirkin

Use the type-safe FWCfgState structure instead
of the unsafe void *.

---
 hw/misc/pvpanic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/misc/pvpanic.c +0 -0
index 31e1b1d..1483f27 100644

hw/misc/pvpanic.c static int pvpanic_isa_initfn(ISADevice *dev) · line 96

Quoted 8 lines
hw/misc/pvpanic.c +1 -1
@@ -90,7 +90,7 @@ static int pvpanic_isa_initfn(ISADevice *dev)
 {
     PVPanicState *s = ISA_PVPANIC_DEVICE(dev);
     static bool port_configured;
-    void *fw_cfg;
+    FWCfgState *fw_cfg;
 
     memory_region_init_io(&s->io, &pvpanic_ops, s, "pvpanic", 1);
     isa_register_ioport(dev, &s->io, s->ioport);

Doesn't this break your build? Lower down in the function there's

Quoted 4 lines
        fw_cfg = object_resolve_path("/machine/fw_cfg", NULL);

and object_resolve_path() returns a pointer-to-Object, not pointer-to-FWCfgState.

I see, I think I should reorder the patches.

Quoted 14 lines

But for starters I'm quite confused about how the unpatched function works. What it does amounts to:

  fw_cfg_add_file(object_resolve_path(...), ...);

But, again object_resolve_path() returns pointer-to-Object. I'm checking "struct Object" in "include/qom/object.h", and it suggests that derived structs should embed Object as first member. However FWCfgState is *not* such a derived member. What's going on here?

http://thread.gmane.org/gmane.comp.emulators.qemu/201544/focus=201564
http://thread.gmane.org/gmane.comp.emulators.qemu/204452/focus=204450

Laszlo

#8 lore raw

PATCH 2/3 fw_cfg: add API to find FW cfg object

In reply to Michael S. Tsirkin

Remove some code duplication by adding a
function to look up the fw cfg file.
This way, we don't need to duplicate same strings everywhere. Use by both fw cfg and pvpanic device.

---
 hw/misc/pvpanic.c         |  2 +-
 hw/nvram/fw_cfg.c         | 15 ++++++++++++---
 include/hw/nvram/fw_cfg.h |  2 ++
 3 files changed, 15 insertions(+), 4 deletions(-)
hw/misc/pvpanic.c +1 -1
index 1483f27..910e44f 100644
@@ -96,7 +96,7 @@ static int pvpanic_isa_initfn(ISADevice *dev)
     isa_register_ioport(dev, &s->io, s->ioport);
 
     if (!port_configured) {
-        fw_cfg = object_resolve_path("/machine/fw_cfg", NULL);
+        fw_cfg = fw_cfg_find();
         if (fw_cfg) {
             fw_cfg_add_file(fw_cfg, "etc/pvpanic-port",
                             g_memdup(&s->ioport, sizeof(s->ioport)),
hw/nvram/fw_cfg.c +12 -3
index 479113b..df3f089 100644
@@ -32,6 +32,9 @@
 
 #define FW_CFG_SIZE 2
 #define FW_CFG_DATA_SIZE 1
+#define TYPE_FW_CFG "fw_cfg"
+#define FW_CFG_NAME "fw_cfg"
+#define FW_CFG_PATH "/machine/" FW_CFG_NAME
 
 typedef struct FWCfgEntry {
     uint32_t len;
@@ -493,8 +496,8 @@ FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
 
     s = DO_UPCAST(FWCfgState, busdev.qdev, dev);
 
-    if (!object_resolve_path("/machine/fw_cfg", NULL)) {
-        object_property_add_child(qdev_get_machine(), "fw_cfg", OBJECT(s),
+    if (!object_resolve_path(FW_CFG_PATH, NULL)) {
+        object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s),
                                   NULL);
     }
 
@@ -553,6 +556,12 @@ static Property fw_cfg_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+FWCfgState *fw_cfg_find(void)
+{
+    return OBJECT_CHECK(FWCfgState, object_resolve_path(FW_CFG_PATH, NULL),
+                        TYPE_FW_CFG);
+}
+
 static void fw_cfg_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
@@ -566,7 +575,7 @@ static void fw_cfg_class_init(ObjectClass *klass, void *data)
 }
 
 static const TypeInfo fw_cfg_info = {
-    .name          = "fw_cfg",
+    .name          = TYPE_FW_CFG,
     .parent        = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(FWCfgState),
     .class_init    = fw_cfg_class_init,
include/hw/nvram/fw_cfg.h +2 -0
index f37714e..f60dd67 100644
@@ -73,6 +73,8 @@ void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data,
 FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
                         hwaddr crl_addr, hwaddr data_addr);
 
+FWCfgState *fw_cfg_find(void);
+
 #endif /* NO_QEMU_PROTOS */
 
 #endif
Signature · 1 line
MST
#9 lore raw

Re: [Qemu-devel] [PATCH 2/3] fw_cfg: add API to find FW cfg object

hw/misc/pvpanic.c static int pvpanic_isa_initfn(ISADevice *dev) · line 102

Quoted 13 lines · Michael S. Tsirkin
hw/misc/pvpanic.c +1 -1
index 1483f27..910e44f 100644
@@ -96,7 +96,7 @@ static int pvpanic_isa_initfn(ISADevice *dev)
     isa_register_ioport(dev, &s->io, s->ioport);
 
     if (!port_configured) {
-        fw_cfg = object_resolve_path("/machine/fw_cfg", NULL);
+        fw_cfg = fw_cfg_find();
         if (fw_cfg) {
             fw_cfg_add_file(fw_cfg, "etc/pvpanic-port",
                             g_memdup(&s->ioport, sizeof(s->ioport)),

Explains 1/3 too, OK.
Laszlo

#10 lore raw

Re: [Qemu-devel] [PATCH 2/3] fw_cfg: add API to find FW cfg object

In reply to Michael S. Tsirkin
Quoted 77 lines · Michael S. Tsirkin

Remove some code duplication by adding a
function to look up the fw cfg file.
This way, we don't need to duplicate same strings everywhere. Use by both fw cfg and pvpanic device.

---
 hw/misc/pvpanic.c         |  2 +-
 hw/nvram/fw_cfg.c         | 15 ++++++++++++---
 include/hw/nvram/fw_cfg.h |  2 ++
 3 files changed, 15 insertions(+), 4 deletions(-)
hw/misc/pvpanic.c +1 -1
index 1483f27..910e44f 100644
@@ -96,7 +96,7 @@ static int pvpanic_isa_initfn(ISADevice *dev)
     isa_register_ioport(dev, &s->io, s->ioport);
 
     if (!port_configured) {
-        fw_cfg = object_resolve_path("/machine/fw_cfg", NULL);
+        fw_cfg = fw_cfg_find();
         if (fw_cfg) {
             fw_cfg_add_file(fw_cfg, "etc/pvpanic-port",
                             g_memdup(&s->ioport, sizeof(s->ioport)),
hw/nvram/fw_cfg.c +12 -3
index 479113b..df3f089 100644
@@ -32,6 +32,9 @@
 
 #define FW_CFG_SIZE 2
 #define FW_CFG_DATA_SIZE 1
+#define TYPE_FW_CFG "fw_cfg"
+#define FW_CFG_NAME "fw_cfg"
+#define FW_CFG_PATH "/machine/" FW_CFG_NAME
 
 typedef struct FWCfgEntry {
     uint32_t len;
@@ -493,8 +496,8 @@ FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
 
     s = DO_UPCAST(FWCfgState, busdev.qdev, dev);
 
-    if (!object_resolve_path("/machine/fw_cfg", NULL)) {
-        object_property_add_child(qdev_get_machine(), "fw_cfg", OBJECT(s),
+    if (!object_resolve_path(FW_CFG_PATH, NULL)) {
+        object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s),
                                   NULL);
     }
 
@@ -553,6 +556,12 @@ static Property fw_cfg_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+FWCfgState *fw_cfg_find(void)
+{
+    return OBJECT_CHECK(FWCfgState, object_resolve_path(FW_CFG_PATH, NULL),
+                        TYPE_FW_CFG);
+}
+
 static void fw_cfg_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
@@ -566,7 +575,7 @@ static void fw_cfg_class_init(ObjectClass *klass, void *data)
 }
 
 static const TypeInfo fw_cfg_info = {
-    .name          = "fw_cfg",
+    .name          = TYPE_FW_CFG,
     .parent        = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(FWCfgState),
     .class_init    = fw_cfg_class_init,
include/hw/nvram/fw_cfg.h +0 -0
index f37714e..f60dd67 100644

include/hw/nvram/fw_cfg.h void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data, · line 76

Quoted 4 lines
include/hw/nvram/fw_cfg.h +1 -0
@@ -73,6 +73,8 @@ void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data,
 FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
                         hwaddr crl_addr, hwaddr data_addr);
 
+FWCfgState *fw_cfg_find(void);

How about fw_cfg_get()? It makes me more comfortable than fw_cfg_find().

Quoted 7 lines
include/hw/nvram/fw_cfg.h +1 -0
+
 #endif /* NO_QEMU_PROTOS */
 
 #endif
Signature · 1 line
MST
#11 lore raw

PATCH 3/3 fw_cfg: fw_cfg is a singleton

In reply to Michael S. Tsirkin

Make sure we only have a single instance ever:
because if it isn't we can't find it so it's
useless anyway.

---
 hw/nvram/fw_cfg.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/nvram/fw_cfg.c +3 -4
index df3f089..3c255ce 100644
@@ -496,10 +496,9 @@ FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
 
     s = DO_UPCAST(FWCfgState, busdev.qdev, dev);
 
-    if (!object_resolve_path(FW_CFG_PATH, NULL)) {
-        object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s),
-                                  NULL);
-    }
+    assert(!object_resolve_path(FW_CFG_PATH, NULL));
+
+    object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL);
 
     qdev_init_nofail(dev);
 
Signature · 1 line
MST
Reviewed-by on the v1 series
#12 lore raw
In reply to Michael S. Tsirkin
Quoted 15 lines · Michael S. Tsirkin

Here are some misc fixes to the fw cfg object handling.
I've queued them on my pci branch temporarily as it's useful to cleanup some pci things. I'm using this with Laszlo's cleanup patch that got rid of void * in pc.c too - that's queued there as well.

Please review and comment.

Michael S. Tsirkin (3):
pvpanic: use FWCfgState explicitly
fw_cfg: add API to find FW cfg object
fw_cfg: fw_cfg is a singleton

Quoted 5 lines
 hw/misc/pvpanic.c         |  4 ++--
 hw/nvram/fw_cfg.c         | 18 +++++++++++++-----
 include/hw/nvram/fw_cfg.h |  2 ++
 3 files changed, 17 insertions(+), 7 deletions(-)

series

#13 lore raw
In reply to Michael S. Tsirkin
Quoted 15 lines · Michael S. Tsirkin

Here are some misc fixes to the fw cfg object handling.
I've queued them on my pci branch temporarily as it's useful to cleanup some pci things. I'm using this with Laszlo's cleanup patch that got rid of void * in pc.c too - that's queued there as well.

Please review and comment.

Michael S. Tsirkin (3):
pvpanic: use FWCfgState explicitly
fw_cfg: add API to find FW cfg object
fw_cfg: fw_cfg is a singleton

Quoted 8 lines
 hw/misc/pvpanic.c         |  4 ++--
 hw/nvram/fw_cfg.c         | 18 +++++++++++++-----
 include/hw/nvram/fw_cfg.h |  2 ++
 3 files changed, 17 insertions(+), 7 deletions(-)
Signature · 1 line
MST

Series looks good to me, except a reorder.