Skip to contentSkip to conversation

pc: Support firmware configuration with -blockdev

PATCH Superseded Markus Armbruster 6 participants 27 messages Mar 8, 2019 – Mar 12, 2019

lore
Share & tools
lore
#0 lore raw

v3 cover pc: Support firmware configuration with -blockdev

Based-on: <[email protected]>

v3:
* PATCH 05: Commit message tidied up [Philippe, László]
* PATCH 06: Comment tidied up [checkpatch]
* PATCH 12: Comment polish [László]

v2:
* PATCH 05,06: New [Philippe]
* PATCH 07: Old PATCH 5 rebased onto new patches, with new comment [Philippe]
* PATCH 08+10: New, factored out of old PATCH 6 [by Philippe]
* PATCH 09: New [by Philippe]
* PATCH 11: Remainder of old PATCH 6, with FIXME resolved, literal 4096 de-duplicated [László], extraneous error check deleted [László], and comment typos fixed
* PATCH 12: New

Markus Armbruster (9):
qdev: Fix latent bug with compat_props and onboard devices
qom: Move compat_props machinery from qdev to QOM
vl: Fix latent bug with -global and onboard devices
sysbus: Fix latent bug with onboard devices
vl: Improve legibility of BlockdevOptions queue
vl: Factor configure_blockdev() out of main()
vl: Create block backends before setting machine properties
pc: Support firmware configuration with -blockdev docs/interop/firmware.json: Prefer -machine to if=pflash

Philippe Mathieu-Daudé (3):
pflash_cfi01: Add pflash_cfi01_get_blk() helper
pc_sysfw: Remove unused PcSysFwDevice
pc_sysfw: Pass PCMachineState to pc_system_firmware_init()

 accel/accel.c              |   1 +
 docs/interop/firmware.json |  20 ++-
 hw/block/pflash_cfi01.c    |   5 +
 hw/core/qdev.c             |  21 +---
 hw/core/sysbus.c           |   3 -
 hw/i386/pc.c               |   4 +-
 hw/i386/pc_sysfw.c         | 249 ++++++++++++++++++++++++-------------
 include/hw/block/flash.h   |   1 +
 include/hw/i386/pc.h       |   6 +-
 include/hw/qdev-core.h     |   2 -
 include/qom/object.h       |   3 +
 qom/object.c               |  39 ++++++
 vl.c                       | 125 ++++++++++---------
 13 files changed, 303 insertions(+), 176 deletions(-)
Signature · 1 line
2.17.2
#1 lore raw

PATCH v3 1/12 qdev: Fix latent bug with compat_props and onboard devices

Compatibility properties started life as a qdev property thing: we supported them only for qdev properties, and implemented them with the machinery backing command line option -global.

Recent commit fa0cb34d221 put them to use (tacitly) with memory backend objects (subtypes of TYPE_MEMORY_BACKEND). To make that possible, we first moved the work of applying them from the -global machinery into TYPE_DEVICE's .instance_post_init() method device_post_init(), in commits ea9ce8934c5 and b66bbee39f6, then made it available to TYPE_MEMORY_BACKEND's .instance_post_init() method host_memory_backend_post_init() as object_apply_compat_props(), in commit 1c3994f6d2a.

Note the code smell: we now have function name starting with object_ in hw/core/qdev.c. It has to be there rather than in qom/, because it calls qdev_get_machine() to find the current accelerator's and machine's compat_props.

Turns out calling qdev_get_machine() there is problematic. If we qdev_create() from a machine's .instance_init() method, we call device_post_init() and thus qdev_get_machine() before main() can create "/machine" in QOM. qdev_get_machine() tries to get it with container_get(), which "helpfully" creates it as "container" object, and returns that. object_apply_compat_props() tries to paper over the problem by doing nothing when the value of qdev_get_machine() isn't a TYPE_MACHINE. But the damage is done already: when main() later attempts to create the real "/machine", it fails with "attempt to add duplicate property 'machine' to object (type 'container')", and aborts.

Since no machine .instance_init() calls qdev_create() so far, the bug is latent. But since I want to do that, I get to fix the bug first.

Observe that object_apply_compat_props() doesn't actually need the MachineState, only its the compat_props member of its MachineClass and AccelClass. This permits a simple fix: register MachineClass and AccelClass compat_props with the object_apply_compat_props() machinery right after these classes get selected.

This is actually similar to how things worked before commits ea9ce8934c5 and b66bbee39f6, except we now register much earlier. The old code registered them only after the machine's .instance_init() ran, which would've broken compatibility properties for any devices created there.

---
 accel/accel.c          |  1 +
 hw/core/qdev.c         | 48 ++++++++++++++++++++++++++++++++----------
 include/hw/qdev-core.h |  2 ++
 vl.c                   |  1 +
 4 files changed, 41 insertions(+), 11 deletions(-)
accel/accel.c +1 -0
index 68b6d56323..4a1670e404 100644
@@ -66,6 +66,7 @@ static int accel_init_machine(AccelClass *acc, MachineState *ms)
         *(acc->allowed) = false;
         object_unref(OBJECT(accel));
     }
+    object_set_accelerator_compat_props(acc->compat_props);
     return ret;
 }
 
hw/core/qdev.c +37 -11

62 lines not on this page · raw message

include/hw/qdev-core.h +2 -0
index 17f09aac72..aa8a3ea782 100644
@@ -431,6 +431,8 @@ const char *qdev_fw_name(DeviceState *dev);
 
 Object *qdev_get_machine(void);
 
+void object_set_machine_compat_props(GPtrArray *compat_props);
+void object_set_accelerator_compat_props(GPtrArray *compat_props);
 void object_apply_compat_props(Object *obj);
 
 /* FIXME: make this a link<> */
vl.c +1 -0
index 4c5cc0d8ad..712b92d707 100644
@@ -3955,6 +3955,7 @@ int main(int argc, char **argv, char **envp)
     configure_rtc(qemu_find_opts_singleton("rtc"));
 
     machine_class = select_machine();
+    object_set_machine_compat_props(machine_class->compat_props);
 
     set_memory_options(&ram_slots, &maxram_size, machine_class);
 
Signature · 1 line
2.17.2
#2 lore raw

PATCH v3 2/12 qom: Move compat_props machinery from qdev to QOM

In reply to Markus Armbruster

See the previous commit for rationale.

---
 hw/core/qdev.c         | 39 ---------------------------------------
 include/hw/qdev-core.h |  4 ----
 include/qom/object.h   |  3 +++
 qom/object.c           | 39 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 42 insertions(+), 43 deletions(-)
hw/core/qdev.c +0 -39
index 4f3200d54b..f9b6efe509 100644
@@ -978,45 +978,6 @@ static void device_initfn(Object *obj)
     QLIST_INIT(&dev->gpios);
 }
 
-/*
- * Global property defaults
- * Slot 0: accelerator's global property defaults
- * Slot 1: machine's global property defaults
- * Each is a GPtrArray of of GlobalProperty.
- * Applied in order, later entries override earlier ones.
- */
-static GPtrArray *object_compat_props[2];
-
-/*
- * Set machine's global property defaults to @compat_props.
- * May be called at most once.
- */
-void object_set_machine_compat_props(GPtrArray *compat_props)
-{
-    assert(!object_compat_props[1]);
-    object_compat_props[1] = compat_props;
-}
-
-/*
- * Set accelerator's global property defaults to @compat_props.
- * May be called at most once.
- */
-void object_set_accelerator_compat_props(GPtrArray *compat_props)
-{
-    assert(!object_compat_props[0]);
-    object_compat_props[0] = compat_props;
-}
-
-void object_apply_compat_props(Object *obj)
-{
-    int i;
-
-    for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
-        object_apply_global_props(obj, object_compat_props[i],
-                                  &error_abort);
-    }
-}
-
 static void device_post_init(Object *obj)
 {
     /*
include/hw/qdev-core.h +0 -4
index aa8a3ea782..33ed3b8dde 100644
@@ -431,10 +431,6 @@ const char *qdev_fw_name(DeviceState *dev);
 
 Object *qdev_get_machine(void);
 
-void object_set_machine_compat_props(GPtrArray *compat_props);
-void object_set_accelerator_compat_props(GPtrArray *compat_props);
-void object_apply_compat_props(Object *obj);
-
 /* FIXME: make this a link<> */
 void qdev_set_parent_bus(DeviceState *dev, BusState *bus);
 
include/qom/object.h +3 -0
index e0262962b5..288cdddf44 100644
@@ -677,6 +677,9 @@ Object *object_new_with_propv(const char *typename,
 
 void object_apply_global_props(Object *obj, const GPtrArray *props,
                                Error **errp);
+void object_set_machine_compat_props(GPtrArray *compat_props);
+void object_set_accelerator_compat_props(GPtrArray *compat_props);
+void object_apply_compat_props(Object *obj);
 
 /**
  * object_set_props:
qom/object.c +39 -0
index 05a8567041..e3206d6799 100644
@@ -408,6 +408,45 @@ void object_apply_global_props(Object *obj, const GPtrArray *props, Error **errp
     }
 }
 
+/*
+ * Global property defaults
+ * Slot 0: accelerator's global property defaults
+ * Slot 1: machine's global property defaults
+ * Each is a GPtrArray of of GlobalProperty.
+ * Applied in order, later entries override earlier ones.
+ */
+static GPtrArray *object_compat_props[2];
+
+/*
+ * Set machine's global property defaults to @compat_props.
+ * May be called at most once.
+ */
+void object_set_machine_compat_props(GPtrArray *compat_props)
+{
+    assert(!object_compat_props[1]);
+    object_compat_props[1] = compat_props;
+}
+
+/*
+ * Set accelerator's global property defaults to @compat_props.
+ * May be called at most once.
+ */
+void object_set_accelerator_compat_props(GPtrArray *compat_props)
+{
+    assert(!object_compat_props[0]);
+    object_compat_props[0] = compat_props;
+}
+
+void object_apply_compat_props(Object *obj)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
+        object_apply_global_props(obj, object_compat_props[i],
+                                  &error_abort);
+    }
+}
+
 static void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
 {
     Object *obj = data;
Signature · 1 line
2.17.2
#3 lore raw

PATCH v3 3/12 vl: Fix latent bug with -global and onboard devices

In reply to Markus Armbruster

main() registers the user's -global only after we create the machine object, i.e. too late for devices created in the machine's .instance_init().

Fortunately, we know the bug is only latent: the commit before previous fixed a bug that would've crashed any attempt to create a device in an .instance_init().

---
 vl.c | 19 ++-----------------
 1 file changed, 2 insertions(+), 17 deletions(-)
vl.c +2 -17
index 712b92d707..b6368662e0 100644
@@ -2939,17 +2939,6 @@ static void user_register_global_props(void)
                       global_init_func, NULL, NULL);
 }
 
-/*
- * Note: we should see that these properties are actually having a
- * priority: accel < machine < user. This means e.g. when user
- * specifies something in "-global", it'll always be used with highest
- * priority than either machine/accelerator compat properties.
- */
-static void register_global_properties(MachineState *ms)
-{
-    user_register_global_props();
-}
-
 int main(int argc, char **argv, char **envp)
 {
     int i;
@@ -3944,6 +3933,8 @@ int main(int argc, char **argv, char **envp)
      */
     loc_set_none();
 
+    user_register_global_props();
+
     replay_configure(icount_opts);
 
     if (incoming && !preconfig_exit_requested) {
@@ -4252,12 +4243,6 @@ int main(int argc, char **argv, char **envp)
                      machine_class->name, machine_class->deprecation_reason);
     }
 
-    /*
-     * Register all the global properties, including accel properties,
-     * machine properties, and user-specified ones.
-     */
-    register_global_properties(current_machine);
-
     /*
      * Migration object can only be created after global properties
      * are applied correctly.
Signature · 1 line
2.17.2
#4 lore raw

PATCH v3 4/12 sysbus: Fix latent bug with onboard devices

In reply to Markus Armbruster

The first call of sysbus_get_default() creates the main system bus and stores it in QOM as "/machine/unattached/sysbus". This must not happen before main() creates "/machine", or else container_get() would "helpfully" create it as "container" object, and the real creation of "/machine" would later abort with "attempt to add duplicate property 'machine' to object (type 'container')". Has been that way ever since we wired up busses in QOM (commit f968fc6892d, v1.2.0).

I believe the bug is latent. I got it to bite by trying to qdev_create() a sysbus device from a machine's .instance_init() method.

The fix is obvious: store the main system bus in QOM right after creating "/machine".

---
 hw/core/sysbus.c | 3 ---
 vl.c             | 4 ++++
 2 files changed, 4 insertions(+), 3 deletions(-)
hw/core/sysbus.c +0 -3
index 9f9edbcab9..307cf90a51 100644
@@ -357,9 +357,6 @@ static void main_system_bus_create(void)
     qbus_create_inplace(main_system_bus, system_bus_info.instance_size,
                         TYPE_SYSTEM_BUS, NULL, "main-system-bus");
     OBJECT(main_system_bus)->free = g_free;
-    object_property_add_child(container_get(qdev_get_machine(),
-                                            "/unattached"),
-                              "sysbus", OBJECT(main_system_bus), NULL);
 }
 
 BusState *sysbus_get_default(void)
vl.c +4 -0
index b6368662e0..c38739e34c 100644
@@ -3991,6 +3991,10 @@ int main(int argc, char **argv, char **envp)
     }
     object_property_add_child(object_get_root(), "machine",
                               OBJECT(current_machine), &error_abort);
+    object_property_add_child(container_get(OBJECT(current_machine),
+                                            "/unattached"),
+                              "sysbus", OBJECT(sysbus_get_default()),
+                              NULL);
 
     if (machine_class->minimum_page_bits) {
         if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
Signature · 1 line
2.17.2
#5 lore raw

PATCH v3 5/12 vl: Improve legibility of BlockdevOptions queue

In reply to Markus Armbruster

Give the queue head type a name: BlockdevOptionsQueue.

Rename the queue entry type from BlockdevOptions_queue to BlockdevOptionsQueueEntry.

---
 vl.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
vl.c +12 -10
index c38739e34c..3045ab0df3 100644
@@ -1193,6 +1193,14 @@ static void default_drive(int enable, int snapshot, BlockInterfaceType type,
 
 }
 
+typedef struct BlockdevOptionsQueueEntry {
+    BlockdevOptions *bdo;
+    Location loc;
+    QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
+} BlockdevOptionsQueueEntry;
+
+typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
+
 static QemuOptsList qemu_smp_opts = {
     .name = "smp-opts",
     .implied_opt_name = "cpus",
@@ -2973,13 +2981,7 @@ int main(int argc, char **argv, char **envp)
     Error *err = NULL;
     bool list_data_dirs = false;
     char *dir, **dirs;
-    typedef struct BlockdevOptions_queue {
-        BlockdevOptions *bdo;
-        Location loc;
-        QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
-    } BlockdevOptions_queue;
-    QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
-        = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
+    BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
 
     module_call_init(MODULE_INIT_TRACE);
 
@@ -3103,12 +3105,12 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_blockdev:
                 {
                     Visitor *v;
-                    BlockdevOptions_queue *bdo;
+                    BlockdevOptionsQueueEntry *bdo;
 
                     v = qobject_input_visitor_new_str(optarg, "driver",
                                                       &error_fatal);
 
-                    bdo = g_new(BlockdevOptions_queue, 1);
+                    bdo = g_new(BlockdevOptionsQueueEntry, 1);
                     visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
                                                &error_fatal);
                     visit_free(v);
@@ -4368,7 +4370,7 @@ int main(int argc, char **argv, char **envp)
 
     /* open the virtual block devices */
     while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
-        BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
+        BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(&bdo_queue);
 
         QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
         loc_push_restore(&bdo->loc);
Signature · 1 line
2.17.2
Reviewed-by on v3 5/12
#6 lore raw

Re: [Qemu-devel] [PATCH v3 05/12] vl: Improve legibility of BlockdevOptions queue

Quoted 6 lines · Markus Armbruster

Give the queue head type a name: BlockdevOptionsQueue.

Rename the queue entry type from BlockdevOptions_queue to BlockdevOptionsQueueEntry.

Quoted 63 lines
---
 vl.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
vl.c +12 -10
index c38739e34c..3045ab0df3 100644
@@ -1193,6 +1193,14 @@ static void default_drive(int enable, int snapshot, BlockInterfaceType type,
 
 }
 
+typedef struct BlockdevOptionsQueueEntry {
+    BlockdevOptions *bdo;
+    Location loc;
+    QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
+} BlockdevOptionsQueueEntry;
+
+typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
+
 static QemuOptsList qemu_smp_opts = {
     .name = "smp-opts",
     .implied_opt_name = "cpus",
@@ -2973,13 +2981,7 @@ int main(int argc, char **argv, char **envp)
     Error *err = NULL;
     bool list_data_dirs = false;
     char *dir, **dirs;
-    typedef struct BlockdevOptions_queue {
-        BlockdevOptions *bdo;
-        Location loc;
-        QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
-    } BlockdevOptions_queue;
-    QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
-        = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
+    BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
 
     module_call_init(MODULE_INIT_TRACE);
 
@@ -3103,12 +3105,12 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_blockdev:
                 {
                     Visitor *v;
-                    BlockdevOptions_queue *bdo;
+                    BlockdevOptionsQueueEntry *bdo;
 
                     v = qobject_input_visitor_new_str(optarg, "driver",
                                                       &error_fatal);
 
-                    bdo = g_new(BlockdevOptions_queue, 1);
+                    bdo = g_new(BlockdevOptionsQueueEntry, 1);
                     visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
                                                &error_fatal);
                     visit_free(v);
@@ -4368,7 +4370,7 @@ int main(int argc, char **argv, char **envp)
 
     /* open the virtual block devices */
     while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
-        BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
+        BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(&bdo_queue);
 
         QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
         loc_push_restore(&bdo->loc);
#7 lore raw

Re: [Qemu-devel] [PATCH v3 05/12] vl: Improve legibility of BlockdevOptions queue

In reply to Markus Armbruster
Quoted 1 line · Markus Armbruster

Give the queue head type a name: BlockdevOptionsQueue.

No particular need for this, but no huge objections either.

Quoted 2 lines

Rename the queue entry type from BlockdevOptions_queue to BlockdevOptionsQueueEntry.

This is welcome. ;)

Paolo

#8 lore raw

PATCH v3 6/12 vl: Factor configure_blockdev() out of main()

In reply to Markus Armbruster
---
 vl.c | 74 ++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 32 deletions(-)
vl.c +42 -32
index 3045ab0df3..573de5ec4a 100644
@@ -1201,6 +1201,47 @@ typedef struct BlockdevOptionsQueueEntry {
 
 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
 
+static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
+                               MachineClass *machine_class, int snapshot)
+{
+    /*
+     * If the currently selected machine wishes to override the
+     * units-per-bus property of its default HBA interface type, do so
+     * now.
+     */
+    if (machine_class->units_per_default_bus) {
+        override_max_devs(machine_class->block_default_type,
+                          machine_class->units_per_default_bus);
+    }
+
+    /* open the virtual block devices */
+    while (!QSIMPLEQ_EMPTY(bdo_queue)) {
+        BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
+
+        QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
+        loc_push_restore(&bdo->loc);
+        qmp_blockdev_add(bdo->bdo, &error_fatal);
+        loc_pop(&bdo->loc);
+        qapi_free_BlockdevOptions(bdo->bdo);
+        g_free(bdo);
+    }
+    if (snapshot || replay_mode != REPLAY_MODE_NONE) {
+        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
+                          NULL, NULL);
+    }
+    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
+                          &machine_class->block_default_type, &error_fatal)) {
+        /* We printed help */
+        exit(0);
+    }
+
+    default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
+                  CDROM_OPTS);
+    default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
+    default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
+
+}
+
 static QemuOptsList qemu_smp_opts = {
     .name = "smp-opts",
     .implied_opt_name = "cpus",
@@ -4361,38 +4402,7 @@ int main(int argc, char **argv, char **envp)
     ram_mig_init();
     dirty_bitmap_mig_init();
 
-    /* If the currently selected machine wishes to override the units-per-bus
-     * property of its default HBA interface type, do so now. */
-    if (machine_class->units_per_default_bus) {
-        override_max_devs(machine_class->block_default_type,
-                          machine_class->units_per_default_bus);
-    }
-
-    /* open the virtual block devices */
-    while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
-        BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(&bdo_queue);
-
-        QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
-        loc_push_restore(&bdo->loc);
-        qmp_blockdev_add(bdo->bdo, &error_fatal);
-        loc_pop(&bdo->loc);
-        qapi_free_BlockdevOptions(bdo->bdo);
-        g_free(bdo);
-    }
-    if (snapshot || replay_mode != REPLAY_MODE_NONE) {
-        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
-                          NULL, NULL);
-    }
-    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
-                          &machine_class->block_default_type, &error_fatal)) {
-        /* We printed help */
-        exit(0);
-    }
-
-    default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
-                  CDROM_OPTS);
-    default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
-    default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
+    configure_blockdev(&bdo_queue, machine_class, snapshot);
 
     qemu_opts_foreach(qemu_find_opts("mon"),
                       mon_init_func, NULL, &error_fatal);
Signature · 1 line
2.17.2
#9 lore raw

Re: [Qemu-devel] [PATCH v3 06/12] vl: Factor configure_blockdev() out of main()

Quoted 11 lines · Markus Armbruster
---
 vl.c | 74 ++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 42 insertions(+), 32 deletions(-)
vl.c +0 -0
index 3045ab0df3..573de5ec4a 100644

vl.c typedef struct BlockdevOptionsQueueEntry { · line 1204

Quoted 4 lines
vl.c +1 -0
@@ -1201,6 +1201,47 @@ typedef struct BlockdevOptionsQueueEntry {
 
 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
 
+static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,

Aha. :)

Paolo

Quoted 84 lines
vl.c +41 -32
+                               MachineClass *machine_class, int snapshot)
+{
+    /*
+     * If the currently selected machine wishes to override the
+     * units-per-bus property of its default HBA interface type, do so
+     * now.
+     */
+    if (machine_class->units_per_default_bus) {
+        override_max_devs(machine_class->block_default_type,
+                          machine_class->units_per_default_bus);
+    }
+
+    /* open the virtual block devices */
+    while (!QSIMPLEQ_EMPTY(bdo_queue)) {
+        BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
+
+        QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
+        loc_push_restore(&bdo->loc);
+        qmp_blockdev_add(bdo->bdo, &error_fatal);
+        loc_pop(&bdo->loc);
+        qapi_free_BlockdevOptions(bdo->bdo);
+        g_free(bdo);
+    }
+    if (snapshot || replay_mode != REPLAY_MODE_NONE) {
+        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
+                          NULL, NULL);
+    }
+    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
+                          &machine_class->block_default_type, &error_fatal)) {
+        /* We printed help */
+        exit(0);
+    }
+
+    default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
+                  CDROM_OPTS);
+    default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
+    default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
+
+}
+
 static QemuOptsList qemu_smp_opts = {
     .name = "smp-opts",
     .implied_opt_name = "cpus",
@@ -4361,38 +4402,7 @@ int main(int argc, char **argv, char **envp)
     ram_mig_init();
     dirty_bitmap_mig_init();
 
-    /* If the currently selected machine wishes to override the units-per-bus
-     * property of its default HBA interface type, do so now. */
-    if (machine_class->units_per_default_bus) {
-        override_max_devs(machine_class->block_default_type,
-                          machine_class->units_per_default_bus);
-    }
-
-    /* open the virtual block devices */
-    while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
-        BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(&bdo_queue);
-
-        QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
-        loc_push_restore(&bdo->loc);
-        qmp_blockdev_add(bdo->bdo, &error_fatal);
-        loc_pop(&bdo->loc);
-        qapi_free_BlockdevOptions(bdo->bdo);
-        g_free(bdo);
-    }
-    if (snapshot || replay_mode != REPLAY_MODE_NONE) {
-        qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
-                          NULL, NULL);
-    }
-    if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
-                          &machine_class->block_default_type, &error_fatal)) {
-        /* We printed help */
-        exit(0);
-    }
-
-    default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
-                  CDROM_OPTS);
-    default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
-    default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
+    configure_blockdev(&bdo_queue, machine_class, snapshot);
 
     qemu_opts_foreach(qemu_find_opts("mon"),
                       mon_init_func, NULL, &error_fatal);
#10 lore raw

PATCH v3 7/12 vl: Create block backends before setting machine properties

In reply to Markus Armbruster

qemu-system-FOO's main() acts on command line arguments in its own idiosyncratic order. There's not much method to its madness. Whenever we find a case where one kind of command line argument needs to refer to something created for another kind later, we rejigger the order.

Block devices get created long after machine properties get processed. Therefore, block device machine properties can be created, but not set. No such properties exist. But the next commit will create some. Time to rejigger again: create block devices earlier.

---
 vl.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
vl.c +7 -2
index 573de5ec4a..58e05b296f 100644
@@ -4276,6 +4276,13 @@ int main(int argc, char **argv, char **envp)
         exit(0);
     }
 
+    /*
+     * Note: we need to create block backends before
+     * machine_set_property(), so machine properties can refer to
+     * them.
+     */
+    configure_blockdev(&bdo_queue, machine_class, snapshot);
+
     machine_opts = qemu_get_machine_opts();
     qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
                      &error_fatal);
@@ -4402,8 +4409,6 @@ int main(int argc, char **argv, char **envp)
     ram_mig_init();
     dirty_bitmap_mig_init();
 
-    configure_blockdev(&bdo_queue, machine_class, snapshot);
-
     qemu_opts_foreach(qemu_find_opts("mon"),
                       mon_init_func, NULL, &error_fatal);
 
Signature · 1 line
2.17.2
#11 lore raw

PATCH v3 8/12 pflash_cfi01: Add pflash_cfi01_get_blk() helper

In reply to Markus Armbruster

From: Philippe Mathieu-Daudé <[email protected]>

Add an helper to access the opaque struct PFlashCFI01.

---
 hw/block/pflash_cfi01.c  | 5 +++++
 include/hw/block/flash.h | 1 +
 2 files changed, 6 insertions(+)
hw/block/pflash_cfi01.c +5 -0
index 9d1c356eb6..125f70b8e4 100644
@@ -967,6 +967,11 @@ PFlashCFI01 *pflash_cfi01_register(hwaddr base,
     return PFLASH_CFI01(dev);
 }
 
+BlockBackend *pflash_cfi01_get_blk(PFlashCFI01 *fl)
+{
+    return fl->blk;
+}
+
 MemoryRegion *pflash_cfi01_get_memory(PFlashCFI01 *fl)
 {
     return &fl->mem;
include/hw/block/flash.h +1 -0
index 914932eaec..a0f488732a 100644
@@ -22,6 +22,7 @@ PFlashCFI01 *pflash_cfi01_register(hwaddr base,
                                    uint16_t id0, uint16_t id1,
                                    uint16_t id2, uint16_t id3,
                                    int be);
+BlockBackend *pflash_cfi01_get_blk(PFlashCFI01 *fl);
 MemoryRegion *pflash_cfi01_get_memory(PFlashCFI01 *fl);
 
 /* pflash_cfi02.c */
Signature · 1 line
2.17.2
#12 lore raw

PATCH v3 9/12 pc_sysfw: Remove unused PcSysFwDevice

In reply to Markus Armbruster

From: Philippe Mathieu-Daudé <[email protected]>

This structure is not used since commit 6dd2a5c98a.

---
 hw/i386/pc_sysfw.c | 5 -----
 1 file changed, 5 deletions(-)
hw/i386/pc_sysfw.c +0 -5
index 34727c5b1f..46b87afe23 100644
@@ -40,11 +40,6 @@
 
 #define BIOS_FILENAME "bios.bin"
 
-typedef struct PcSysFwDevice {
-    SysBusDevice busdev;
-    uint8_t isapc_ram_fw;
-} PcSysFwDevice;
-
 static void pc_isa_bios_init(MemoryRegion *rom_memory,
                              MemoryRegion *flash_mem,
                              int ram_size)
Signature · 1 line
2.17.2
#13 lore raw

PATCH v3 10/12 pc_sysfw: Pass PCMachineState to pc_system_firmware_init()

In reply to Markus Armbruster

From: Philippe Mathieu-Daudé <[email protected]>

pc_system_firmware_init() parameter @isapc_ram_fw is PCMachineState member pci_enabled negated. The next commit will need more of PCMachineState. To prepare for that, pass a PCMachineState *, and drop the now redundant parameter @isapc_ram_fw.

---
 hw/i386/pc.c         | 2 +-
 hw/i386/pc_sysfw.c   | 5 ++++-
 include/hw/i386/pc.h | 3 +--
 3 files changed, 6 insertions(+), 4 deletions(-)
hw/i386/pc.c +1 -1
index 42128183e9..b2947e433b 100644
@@ -1803,7 +1803,7 @@ void pc_memory_init(PCMachineState *pcms,
     }
 
     /* Initialize PC system firmware */
-    pc_system_firmware_init(rom_memory, !pcmc->pci_enabled);
+    pc_system_firmware_init(pcms, rom_memory);
 
     option_rom_mr = g_malloc(sizeof(*option_rom_mr));
     memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
hw/i386/pc_sysfw.c +4 -1
index 46b87afe23..785123252c 100644
@@ -231,8 +231,11 @@ static void old_pc_system_rom_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
                                 bios);
 }
 
-void pc_system_firmware_init(MemoryRegion *rom_memory, bool isapc_ram_fw)
+void pc_system_firmware_init(PCMachineState *pcms,
+                             MemoryRegion *rom_memory)
 {
+    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+    bool isapc_ram_fw = !pcmc->pci_enabled;
     DriveInfo *pflash_drv;
 
     pflash_drv = drive_get(IF_PFLASH, 0, 0);
include/hw/i386/pc.h +1 -2
index 54222a202d..4f5ed7cefc 100644
@@ -277,8 +277,7 @@ extern PCIDevice *piix4_dev;
 int piix4_init(PCIBus *bus, ISABus **isa_bus, int devfn);
 
 /* pc_sysfw.c */
-void pc_system_firmware_init(MemoryRegion *rom_memory,
-                             bool isapc_ram_fw);
+void pc_system_firmware_init(PCMachineState *pcms, MemoryRegion *rom_memory);
 
 /* acpi-build.c */
 void pc_madt_cpu_entry(AcpiDeviceIf *adev, int uid,
Signature · 1 line
2.17.2
#14 lore raw

PATCH v3 11/12 pc: Support firmware configuration with -blockdev

In reply to Markus Armbruster

The PC machines put firmware in ROM by default. To get it put into flash memory (required by OVMF), you have to use -drive if=pflash,unit=0,... and optionally -drive if=pflash,unit=1,...

Why two -drive? This permits setting up one part of the flash memory read-only, and the other part read/write. Below the hood, it creates two separate flash devices, because we were too lazy to improve our flash device models to support sector protection.

The problem at hand is to do the same with -blockdev somehow, as one more step towards deprecating -drive.

Mapping -drive if=none,... to -blockdev is a solved problem. With if=T other than if=none, -drive additionally configures a block device frontend. For non-onboard devices, that part maps to -device. Also a solved problem. For onboard devices such as PC flash memory, we have an unsolved problem.

This is actually an instance of a wider problem: our general device configuration interface doesn't cover onboard devices. Instead, we have a zoo of ad hoc interfaces that are much more limited. Some of them we'd rather deprecate (-drive, -net), but can't until we have suitable replacements.

Sadly, I can't attack the wider problem today. So back to the narrow problem.

My first idea was to reduce it to its solved buddy by using pluggable instead of onboard devices for the flash memory. Workable, but it requires some extra smarts in firmware descriptors and libvirt. Paolo had an idea that is simpler for libvirt: keep the devices onboard, and add machine properties for their block backends.

The implementation is less than straightforward, I'm afraid.

First, block backend properties are *qdev* properties. Machines can't have those, as they're not devices. I could duplicate these qdev properties as QOM properties, but I hate that.

More seriously, the properties do not belong to the machine, they belong to the onboard flash devices. Adding them to the machine would then require bad magic to somehow transfer them to the flash devices. Fortunately, QOM provides the means to handle exactly this case: add alias properties to the machine that forward to the onboard devices' properties.

Properties need to be created in .instance_init() methods. For PC machines, that's pc_machine_initfn(). To make alias properties work, we need to create the onboard flash devices there, too. Requires several bug fixes, in the previous commits. We also have to realize the devices. More on that below.

If the user sets pflash0, firmware resides in flash memory. pc_system_firmware_init() maps and realizes the flash devices.

Else, firmware resides in ROM. The onboard flash devices aren't used then. pc_system_firmware_init() destroys them unrealized, along with the alias properties.

The existing code to pick up drives defined with -drive if=pflash is replaced by code to desugar into the machine properties.

---
 hw/i386/pc.c         |   2 +
 hw/i386/pc_sysfw.c   | 243 ++++++++++++++++++++++++++++---------------
 include/hw/i386/pc.h |   3 +
 3 files changed, 166 insertions(+), 82 deletions(-)
hw/i386/pc.c +2 -0
index b2947e433b..7e7f4cbfb4 100644
@@ -2643,6 +2643,8 @@ static void pc_machine_initfn(Object *obj)
     pcms->smbus_enabled = true;
     pcms->sata_enabled = true;
     pcms->pit_enabled = true;
+
+    pc_system_flash_create(pcms);
 }
 
 static void pc_machine_reset(void)
hw/i386/pc_sysfw.c +161 -82

286 lines not on this page · raw message

include/hw/i386/pc.h +3 -0
index 4f5ed7cefc..276ff15d4d 100644
@@ -6,6 +6,7 @@
 #include "hw/boards.h"
 #include "hw/isa/isa.h"
 #include "hw/block/fdc.h"
+#include "hw/block/flash.h"
 #include "net/net.h"
 #include "hw/i386/ioapic.h"
 
@@ -39,6 +40,7 @@ struct PCMachineState {
     PCIBus *bus;
     FWCfgState *fw_cfg;
     qemu_irq *gsi;
+    PFlashCFI01 *flash[2];
 
     /* Configuration options: */
     uint64_t max_ram_below_4g;
@@ -277,6 +279,7 @@ extern PCIDevice *piix4_dev;
 int piix4_init(PCIBus *bus, ISABus **isa_bus, int devfn);
 
 /* pc_sysfw.c */
+void pc_system_flash_create(PCMachineState *pcms);
 void pc_system_firmware_init(PCMachineState *pcms, MemoryRegion *rom_memory);
 
 /* acpi-build.c */
Signature · 1 line
2.17.2
#15 lore raw
Quoted 8 lines · Markus Armbruster

The PC machines put firmware in ROM by default. To get it put into flash memory (required by OVMF), you have to use -drive if=pflash,unit=0,... and optionally -drive if=pflash,unit=1,...

Why two -drive? This permits setting up one part of the flash memory read-only, and the other part read/write. Below the hood, it creates two separate flash devices, because we were too lazy to improve our flash device models to support sector protection.

Not just that, it avoids the need to "upgrade the firmware" of old virtual machines. Instead, you just need to upgrade the files on the host and restart QEMU, just like with non-UEFI firmware.

Quoted 5 lines

This is actually an instance of a wider problem: our general device configuration interface doesn't cover onboard devices. Instead, we have a zoo of ad hoc interfaces that are much more limited. Some of them we'd rather deprecate (-drive, -net), but can't until we have suitable replacements.

-net already has a better replacement, -nic, which is basically "like -netdev but also tell board creation code about it". I suppose you'd prefer that it were also accessible as "-netdev ...,id=foo -machine net0=foo"?

hw/i386/pc_sysfw.c static void pc_isa_bios_init(MemoryRegion *rom_memory, · line 123

Quoted 13 lines
hw/i386/pc_sysfw.c +13 -0
+static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
+{
+    char *prop_name;
+    int i;
+    Object *dev_obj;
+
+    assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
+
+    for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
+        dev_obj = OBJECT(pcms->flash[i]);
+        if (!object_property_get_bool(dev_obj, "realized", &error_abort)) {
+            prop_name = g_strdup_printf("pflash%d", i);
+            object_property_del(OBJECT(pcms), prop_name, &error_abort);

Why didn't this already call object->class->unparent?

hw/i386/pc_sysfw.c static void pc_isa_bios_init(MemoryRegion *rom_memory,

Quoted 7 lines
hw/i386/pc_sysfw.c +7 -0
+            g_free(prop_name);
+            /*
+             * Delete @dev_obj.  Straight object_unref() is wrong,
+             * since it leaves dangling references in the parent bus
+             * behind.  object_unparent() doesn't work, either: since
+             * @dev_obj hasn't been realized, dev_obj->parent is null,
+             * and object_unparent() does nothing.

Does it work if you add the device yourself as a child of /machine, instead of relying on /machine/unattached?

hw/i386/pc_sysfw.c static void pc_isa_bios_init(MemoryRegion *rom_memory, · line 139

Quoted 10 lines

DeviceClass method

hw/i386/pc_sysfw.c +9 -0
+             * device_unparent() works, but we have to take a
+             * temporary reference, or else device_unparent() commits
+             * a use-after-free error.
+             */
+            object_ref(dev_obj);
+            object_get_class(dev_obj)->unparent(dev_obj);
+            object_unref(dev_obj);
+            pcms->flash[i] = NULL;
+        }

Paolo

Quoted 222 lines
hw/i386/pc_sysfw.c +97 -63
+    }
+}
+
+/*
+ * Map the pcms->flash[] from 4GiB downward, and realize.
+ * Map them in descending order, i.e. pcms->flash[0] at the top,
+ * without gaps.
+ * Stop at the first pcms->flash[0] lacking a block backend.
+ * Set each flash's size from its block backend.  Fatal error if the
+ * size isn't a non-zero multiple of 4KiB, or the total size exceeds
+ * FLASH_SIZE_LIMIT.
  *
- * The drive with unit#0 (if available) is mapped at the highest address, and
- * it is passed to pc_isa_bios_init(). Merging several drives for isa-bios is
+ * If pcms->flash[0] has a block backend, its memory is passed to
+ * pc_isa_bios_init().  Merging several flash devices for isa-bios is
  * not supported.
  */
-static void pc_system_flash_init(MemoryRegion *rom_memory)
+static void pc_system_flash_map(PCMachineState *pcms,
+                                MemoryRegion *rom_memory)
 {
-    int unit;
-    DriveInfo *pflash_drv;
+    hwaddr total_size = 0;
+    int i;
     BlockBackend *blk;
     int64_t size;
-    char *fatal_errmsg = NULL;
-    hwaddr phys_addr = 0x100000000ULL;
-    uint32_t sector_size = 4096;
     PFlashCFI01 *system_flash;
     MemoryRegion *flash_mem;
-    char name[64];
     void *flash_ptr;
     int ret, flash_size;
 
-    for (unit = 0;
-         (unit < FLASH_MAP_UNIT_MAX &&
-          (pflash_drv = drive_get(IF_PFLASH, 0, unit)) != NULL);
-         ++unit) {
-        blk = blk_by_legacy_dinfo(pflash_drv);
+    assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
+
+    for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
+        system_flash = pcms->flash[i];
+        blk = pflash_cfi01_get_blk(system_flash);
+        if (!blk) {
+            break;
+        }
         size = blk_getlength(blk);
         if (size < 0) {
-            fatal_errmsg = g_strdup_printf("failed to get backing file size");
-        } else if (size == 0) {
-            fatal_errmsg = g_strdup_printf("PC system firmware (pflash) "
-                               "cannot have zero size");
-        } else if ((size % sector_size) != 0) {
-            fatal_errmsg = g_strdup_printf("PC system firmware (pflash) "
-                               "must be a multiple of 0x%x", sector_size);
-        } else if (phys_addr < size || phys_addr - size < FLASH_MAP_BASE_MIN) {
-            fatal_errmsg = g_strdup_printf("oversized backing file, pflash "
-                               "segments cannot be mapped under "
-                               TARGET_FMT_plx, FLASH_MAP_BASE_MIN);
+            error_report("can't get size of block device %s: %s",
+                         blk_name(blk), strerror(-size));
+            exit(1);
         }
-        if (fatal_errmsg != NULL) {
-            Location loc;
-
-            /* push a new, "none" location on the location stack; overwrite its
-             * contents with the location saved in the option; print the error
-             * (includes location); pop the top
-             */
-            loc_push_none(&loc);
-            if (pflash_drv->opts != NULL) {
-                qemu_opts_loc_restore(pflash_drv->opts);
-            }
-            error_report("%s", fatal_errmsg);
-            loc_pop(&loc);
-            g_free(fatal_errmsg);
+        if (size == 0 || size % FLASH_SECTOR_SIZE != 0) {
+            error_report("system firmware block device %s has invalid size "
+                         "%" PRId64,
+                         blk_name(blk), size);
+            info_report("its size must be a non-zero multiple of 0x%x",
+                        FLASH_SECTOR_SIZE);
+            exit(1);
+        }
+        if ((hwaddr)size != size
+            || total_size > HWADDR_MAX - size
+            || total_size + size > FLASH_SIZE_LIMIT) {
+            error_report("combined size of system firmware exceeds "
+                         "%" PRIu64 " bytes",
+                         FLASH_SIZE_LIMIT);
             exit(1);
         }
 
-        phys_addr -= size;
+        total_size += size;
+        qdev_prop_set_uint32(DEVICE(system_flash), "num-blocks",
+                             size / FLASH_SECTOR_SIZE);
+        qdev_init_nofail(DEVICE(system_flash));
+        sysbus_mmio_map(SYS_BUS_DEVICE(system_flash), 0,
+                        0x100000000ULL - total_size);
 
-        /* pflash_cfi01_register() creates a deep copy of the name */
-        snprintf(name, sizeof name, "system.flash%d", unit);
-        system_flash = pflash_cfi01_register(phys_addr, name,
-                                             size, blk, sector_size,
-                                             1      /* width */,
-                                             0x0000 /* id0 */,
-                                             0x0000 /* id1 */,
-                                             0x0000 /* id2 */,
-                                             0x0000 /* id3 */,
-                                             0      /* be */);
-        if (unit == 0) {
+        if (i == 0) {
             flash_mem = pflash_cfi01_get_memory(system_flash);
             pc_isa_bios_init(rom_memory, flash_mem, size);
@@ -235,23 +278,59 @@ void pc_system_firmware_init(PCMachineState *pcms,
                              MemoryRegion *rom_memory)
 {
     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
-    bool isapc_ram_fw = !pcmc->pci_enabled;
+    int i;
     DriveInfo *pflash_drv;
+    BlockBackend *pflash_blk[ARRAY_SIZE(pcms->flash)];
+    Location loc;
 
-    pflash_drv = drive_get(IF_PFLASH, 0, 0);
-
-    if (isapc_ram_fw || pflash_drv == NULL) {
-        /* When a pflash drive is not found, use rom-mode */
-        old_pc_system_rom_init(rom_memory, isapc_ram_fw);
+    if (!pcmc->pci_enabled) {
+        old_pc_system_rom_init(rom_memory, true);
         return;
     }
 
-    if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
-        /* Older KVM cannot execute from device memory. So, flash memory
-         * cannot be used unless the readonly memory kvm capability is present. */
-        fprintf(stderr, "qemu: pflash with kvm requires KVM readonly memory support\n");
-        exit(1);
+    /* Map legacy -drive if=pflash to machine properties */
+    for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
+        pflash_blk[i] = pflash_cfi01_get_blk(pcms->flash[i]);
+        pflash_drv = drive_get(IF_PFLASH, 0, i);
+        if (!pflash_drv) {
+            continue;
+        }
+        loc_push_none(&loc);
+        qemu_opts_loc_restore(pflash_drv->opts);
+        if (pflash_blk[i]) {
+            error_report("clashes with -machine");
+            exit(1);
+        }
+        pflash_blk[i] = blk_by_legacy_dinfo(pflash_drv);
+        qdev_prop_set_drive(DEVICE(pcms->flash[i]),
+                            "drive", pflash_blk[i], &error_fatal);
+        loc_pop(&loc);
     }
 
-    pc_system_flash_init(rom_memory);
+    /* Reject gaps */
+    for (i = 1; i < ARRAY_SIZE(pcms->flash); i++) {
+        if (pflash_blk[i] && !pflash_blk[i - 1]) {
+            error_report("pflash%d requires pflash%d", i, i - 1);
+            exit(1);
+        }
+    }
+
+    if (!pflash_blk[0]) {
+        /* Machine property pflash0 not set, use ROM mode */
+        old_pc_system_rom_init(rom_memory, false);
+    } else {
+        if (kvm_enabled() && !kvm_readonly_mem_enabled()) {
+            /*
+             * Older KVM cannot execute from device memory. So, flash
+             * memory cannot be used unless the readonly memory kvm
+             * capability is present.
+             */
+            error_report("pflash with kvm requires KVM readonly memory support");
+            exit(1);
+        }
+
+        pc_system_flash_map(pcms, rom_memory);
+    }
+
+    pc_system_flash_cleanup_unused(pcms);
 }
include/hw/i386/pc.h +3 -0
index 4f5ed7cefc..276ff15d4d 100644
@@ -6,6 +6,7 @@
 #include "hw/boards.h"
 #include "hw/isa/isa.h"
 #include "hw/block/fdc.h"
+#include "hw/block/flash.h"
 #include "net/net.h"
 #include "hw/i386/ioapic.h"
 
@@ -39,6 +40,7 @@ struct PCMachineState {
     PCIBus *bus;
     FWCfgState *fw_cfg;
     qemu_irq *gsi;
+    PFlashCFI01 *flash[2];
 
     /* Configuration options: */
     uint64_t max_ram_below_4g;
@@ -277,6 +279,7 @@ extern PCIDevice *piix4_dev;
 int piix4_init(PCIBus *bus, ISABus **isa_bus, int devfn);
 
 /* pc_sysfw.c */
+void pc_system_flash_create(PCMachineState *pcms);
 void pc_system_firmware_init(PCMachineState *pcms, MemoryRegion *rom_memory);
 
 /* acpi-build.c */
#16 lore raw
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Markus Armbruster

The PC machines put firmware in ROM by default. To get it put into flash memory (required by OVMF), you have to use -drive if=pflash,unit=0,... and optionally -drive if=pflash,unit=1,...

Quoted 8 lines
Quoted 4 lines

Why two -drive? This permits setting up one part of the flash memory read-only, and the other part read/write. Below the hood, it creates two separate flash devices, because we were too lazy to improve our flash device models to support sector protection.

Not just that, it avoids the need to "upgrade the firmware" of old virtual machines. Instead, you just need to upgrade the files on the host and restart QEMU, just like with non-UEFI firmware.

True. I'll insert "It also makes upgrading firmware on the host easier."

Quoted 10 lines
Quoted 5 lines

This is actually an instance of a wider problem: our general device configuration interface doesn't cover onboard devices. Instead, we have a zoo of ad hoc interfaces that are much more limited. Some of them we'd rather deprecate (-drive, -net), but can't until we have suitable replacements.

(quoted diff) +0 -2
-net already has a better replacement, -nic, which is basically "like
-netdev but also tell board creation code about it".  I suppose you'd

prefer that it were also accessible as "-netdev ...,id=foo -machine net0=foo"?

I got confused. Or rather, my mind was stuck in the past. Along with parts of our UI, to be frank. Yes, -net already has suitable replacements (-nic and -netdev hubport). We've kept around -net anyway. If this situation confuses me, chances are it confuses users, too. I think we should deprecate it.

I'll drop -net from my parenthesis.

Restoring a bit more quoted material for later:

+void pc_system_flash_create(PCMachineState *pcms) +{
+ PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+
+ if (pcmc->pci_enabled) {

   +        pcms->flash[0] = pc_pflash_create("system.flash0");
   +        pcms->flash[1] = pc_pflash_create("system.flash1");
   +        object_property_add_alias(OBJECT(pcms), "pflash0",

+ OBJECT(pcms->flash[0]), "drive",
+ &error_abort);
+ object_property_add_alias(OBJECT(pcms), "pflash1",
+ OBJECT(pcms->flash[1]), "drive",
+ &error_abort);
+ }
+}
+

Quoted 7 lines
Quoted 7 lines
hw/i386/pc_sysfw.c +7 -0
+static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
+{
+    char *prop_name;
+    int i;
+    Object *dev_obj;
+
+    assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
Quoted 8 lines

hw/i386/pc_sysfw.c static void pc_isa_bios_init(MemoryRegion *rom_memory, · line 123

Quoted 6 lines
hw/i386/pc_sysfw.c +6 -0
+
+    for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
+        dev_obj = OBJECT(pcms->flash[i]);
+        if (!object_property_get_bool(dev_obj, "realized", &error_abort)) {
+            prop_name = g_strdup_printf("pflash%d", i);
+            object_property_del(OBJECT(pcms), prop_name, &error_abort);

Why didn't this already call object->class->unparent?

Let's see...

    void object_property_del(Object *obj, const char *name, Error **errp)
    {
        ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);

        if (!prop) {
            error_setg(errp, "Property '.%s' not found", name);
            return;
        }

        if (prop->release) {
            prop->release(obj, name, prop->opaque);
        }
        g_hash_table_remove(obj->properties, name);
    }

The only thing that could possibly call object->class->unparent() is prop->release(). In gdb:

    (gdb) p *prop
    $1 = {name = 0x555556a4aa20 "pflash0", type = 0x555556a4aa40 "str", 
      description = 0x555556a4aa80 "Node name or ID of a block device to use as a backend", get = 0x555555d02734 <property_get_alias>, 
      set = 0x555555d0277a <property_set_alias>, 
      resolve = 0x555555d027c0 <property_resolve_alias>, 
      release = 0x555555d027f8 <property_release_alias>, opaque = 0x555556a4a990}

property_release_alias() doesn't:

    static void property_release_alias(Object *obj, const char *name, void *opaque)
    {
        AliasProperty *prop = opaque;

        g_free(prop->target_name);
        g_free(prop);
    }

Remember, the machine property is merely an alias that forwards to the pflash device's property.

Quoted 10 lines

hw/i386/pc_sysfw.c static void pc_isa_bios_init(MemoryRegion *rom_memory,

Quoted 7 lines
hw/i386/pc_sysfw.c +7 -0
+            g_free(prop_name);
+            /*
+             * Delete @dev_obj.  Straight object_unref() is wrong,
+             * since it leaves dangling references in the parent bus
+             * behind.  object_unparent() doesn't work, either: since
+             * @dev_obj hasn't been realized, dev_obj->parent is null,
+             * and object_unparent() does nothing.

Does it work if you add the device yourself as a child of /machine, instead of relying on /machine/unattached?

I figure you're suggesting something like this incremental patch:

diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c index ccf2221acb..9d3a80c51a 100644
--- a/hw/i386/pc_sysfw.c
+++ b/hw/i386/pc_sysfw.c
@@ -99,6 +99,10 @@ void pc_system_flash_create(PCMachineState *pcms)

        if (pcmc->pci_enabled) {
            pcms->flash[0] = pc_pflash_create("system.flash0");
            pcms->flash[1] = pc_pflash_create("system.flash1");
   +        object_property_add_child(OBJECT(pcms), "pfl0",
   +                                  OBJECT(pcms->flash[0]), &error_abort);
   +        object_property_add_child(OBJECT(pcms), "pfl1",
   +                                  OBJECT(pcms->flash[1]), &error_abort);
            object_property_add_alias(OBJECT(pcms), "pflash0",
                                      OBJECT(pcms->flash[0]), "drive",
                                      &error_abort);
   @@ -122,19 +126,7 @@ static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
                prop_name = g_strdup_printf("pflash%d", i);
                object_property_del(OBJECT(pcms), prop_name, &error_abort);
                g_free(prop_name);
   -            /*
   -             * Delete @dev_obj.  Straight object_unref() is wrong,
   -             * since it leaves dangling references in the parent bus
   -             * behind.  object_unparent() doesn't work, either: since
   -             * @dev_obj hasn't been realized, dev_obj->parent is null,
   -             * and object_unparent() does nothing.  DeviceClass method
   -             * device_unparent() works, but we have to take a
   -             * temporary reference, or else device_unparent() commits
   -             * a use-after-free error.
   -             */
   -            object_ref(dev_obj);
   -            object_get_class(dev_obj)->unparent(dev_obj);
   -            object_unref(dev_obj);
   +            object_unparent(dev_obj);
                pcms->flash[i] = NULL;
            }
        }

Passes "make check" and "info qtree" looks good both with and without -machine pflash0,...

I'm not exactly happy with "pfl0" and "pfl1". Got a favorite color for my bikeshed?

Quoted 11 lines

hw/i386/pc_sysfw.c static void pc_isa_bios_init(MemoryRegion *rom_memory, · line 139

Quoted 10 lines

DeviceClass method

hw/i386/pc_sysfw.c +9 -0
+             * device_unparent() works, but we have to take a
+             * temporary reference, or else device_unparent() commits
+             * a use-after-free error.
+             */
+            object_ref(dev_obj);
+            object_get_class(dev_obj)->unparent(dev_obj);
+            object_unref(dev_obj);
+            pcms->flash[i] = NULL;
+        }
Quoted 1 line

Paolo

Thank you!

[...]

#17 lore raw
Quoted 4 lines · Markus Armbruster
Quoted 2 lines

Does it work if you add the device yourself as a child of /machine, instead of relying on /machine/unattached?

I figure you're suggesting something like this incremental patch:

Yes.

Quoted 5 lines

Passes "make check" and "info qtree" looks good both with and without -machine pflash0,...

I'm not exactly happy with "pfl0" and "pfl1". Got a favorite color for my bikeshed?

"pflash[0]" and "pflash[1]"?

Paolo

#18 lore raw
In reply to Markus Armbruster
Quoted 169 lines · Markus Armbruster
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Markus Armbruster

The PC machines put firmware in ROM by default. To get it put into flash memory (required by OVMF), you have to use -drive if=pflash,unit=0,... and optionally -drive if=pflash,unit=1,...

Quoted 8 lines
Quoted 4 lines

Why two -drive? This permits setting up one part of the flash memory read-only, and the other part read/write. Below the hood, it creates two separate flash devices, because we were too lazy to improve our flash device models to support sector protection.

Not just that, it avoids the need to "upgrade the firmware" of old virtual machines. Instead, you just need to upgrade the files on the host and restart QEMU, just like with non-UEFI firmware.

True. I'll insert "It also makes upgrading firmware on the host easier."

Quoted 10 lines
Quoted 5 lines

This is actually an instance of a wider problem: our general device configuration interface doesn't cover onboard devices. Instead, we have a zoo of ad hoc interfaces that are much more limited. Some of them we'd rather deprecate (-drive, -net), but can't until we have suitable replacements.

(quoted diff) +0 -2
-net already has a better replacement, -nic, which is basically "like
-netdev but also tell board creation code about it".  I suppose you'd

prefer that it were also accessible as "-netdev ...,id=foo -machine net0=foo"?

I got confused. Or rather, my mind was stuck in the past. Along with parts of our UI, to be frank. Yes, -net already has suitable replacements (-nic and -netdev hubport). We've kept around -net anyway. If this situation confuses me, chances are it confuses users, too. I think we should deprecate it.

I'll drop -net from my parenthesis.

Restoring a bit more quoted material for later:

+void pc_system_flash_create(PCMachineState *pcms) +{
+ PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+
+ if (pcmc->pci_enabled) {

   +        pcms->flash[0] = pc_pflash_create("system.flash0");
   +        pcms->flash[1] = pc_pflash_create("system.flash1");
   +        object_property_add_alias(OBJECT(pcms), "pflash0",
   +                                  OBJECT(pcms->flash[0]), "drive",
   +                                  &error_abort);
   +        object_property_add_alias(OBJECT(pcms), "pflash1",
   +                                  OBJECT(pcms->flash[1]), "drive",
   +                                  &error_abort);
   +    }
   +}
   +
Quoted 11 lines

hw/i386/pc_sysfw.c static void pc_isa_bios_init(MemoryRegion *rom_memory,

Quoted 11 lines
hw/i386/pc_sysfw.c +11 -0
+static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
+{
+    char *prop_name;
+    int i;
+    Object *dev_obj;
+
+    assert(PC_MACHINE_GET_CLASS(pcms)->pci_enabled);
+
+    for (i = 0; i < ARRAY_SIZE(pcms->flash); i++) {
+        dev_obj = OBJECT(pcms->flash[i]);
+        if (!object_property_get_bool(dev_obj, "realized",

&error_abort)) {

Quoted 4 lines

hw/i386/pc_sysfw.c static void pc_isa_bios_init(MemoryRegion *rom_memory, · line 123

Quoted 2 lines
hw/i386/pc_sysfw.c +2 -0
+            prop_name = g_strdup_printf("pflash%d", i);
+            object_property_del(OBJECT(pcms), prop_name, &error_abort);

Why didn't this already call object->class->unparent?

Let's see...

    void object_property_del(Object *obj, const char *name, Error **errp)
    {
        ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);

        if (!prop) {
            error_setg(errp, "Property '.%s' not found", name);
            return;
        }

        if (prop->release) {
            prop->release(obj, name, prop->opaque);
        }
        g_hash_table_remove(obj->properties, name);
    }

The only thing that could possibly call object->class->unparent() is prop->release(). In gdb:

    (gdb) p *prop
    $1 = {name = 0x555556a4aa20 "pflash0", type = 0x555556a4aa40 "str",
      description = 0x555556a4aa80 "Node name or ID of a block device to
use as a backend", get = 0x555555d02734 <property_get_alias>,
      set = 0x555555d0277a <property_set_alias>,
      resolve = 0x555555d027c0 <property_resolve_alias>,
      release = 0x555555d027f8 <property_release_alias>, opaque =

0x555556a4a990}

property_release_alias() doesn't:

static void property_release_alias(Object *obj, const char *name, void
*opaque)

    {
        AliasProperty *prop = opaque;

        g_free(prop->target_name);
        g_free(prop);
    }

Remember, the machine property is merely an alias that forwards to the pflash device's property.

Quoted 10 lines

hw/i386/pc_sysfw.c static void pc_isa_bios_init(MemoryRegion *rom_memory,

Quoted 7 lines
hw/i386/pc_sysfw.c +7 -0
+            g_free(prop_name);
+            /*
+             * Delete @dev_obj.  Straight object_unref() is wrong,
+             * since it leaves dangling references in the parent bus
+             * behind.  object_unparent() doesn't work, either: since
+             * @dev_obj hasn't been realized, dev_obj->parent is null,
+             * and object_unparent() does nothing.

Does it work if you add the device yourself as a child of /machine, instead of relying on /machine/unattached?

I figure you're suggesting something like this incremental patch:

   diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c
   index ccf2221acb..9d3a80c51a 100644
   --- a/hw/i386/pc_sysfw.c
   +++ b/hw/i386/pc_sysfw.c
   @@ -99,6 +99,10 @@ void pc_system_flash_create(PCMachineState *pcms)
        if (pcmc->pci_enabled) {
            pcms->flash[0] = pc_pflash_create("system.flash0");
            pcms->flash[1] = pc_pflash_create("system.flash1");
   +        object_property_add_child(OBJECT(pcms), "pfl0",
   +                                  OBJECT(pcms->flash[0]),
&error_abort);
   +        object_property_add_child(OBJECT(pcms), "pfl1",
   +                                  OBJECT(pcms->flash[1]),
&error_abort);
            object_property_add_alias(OBJECT(pcms), "pflash0",
                                      OBJECT(pcms->flash[0]), "drive",
                                      &error_abort);
   @@ -122,19 +126,7 @@ static void
pc_system_flash_cleanup_unused(PCMachineState *pcms)
                prop_name = g_strdup_printf("pflash%d", i);
                object_property_del(OBJECT(pcms), prop_name, &error_abort);
                g_free(prop_name);
   -            /*
   -             * Delete @dev_obj.  Straight object_unref() is wrong,
   -             * since it leaves dangling references in the parent bus
   -             * behind.  object_unparent() doesn't work, either: since
   -             * @dev_obj hasn't been realized, dev_obj->parent is null,
   -             * and object_unparent() does nothing.  DeviceClass method
   -             * device_unparent() works, but we have to take a
   -             * temporary reference, or else device_unparent() commits
   -             * a use-after-free error.
   -             */
   -            object_ref(dev_obj);
   -            object_get_class(dev_obj)->unparent(dev_obj);
   -            object_unref(dev_obj);
   +            object_unparent(dev_obj);
                pcms->flash[i] = NULL;
            }
        }
Quoted 6 lines

Passes "make check" and "info qtree" looks good both with and without -machine pflash0,...

I'm not exactly happy with "pfl0" and "pfl1". Got a favorite color for my bikeshed?

ovmf_code & ovmf_vars?

Quoted 19 lines
Quoted 12 lines

hw/i386/pc_sysfw.c static void pc_isa_bios_init(MemoryRegion *rom_memory, · line 139

Quoted 11 lines

DeviceClass method

hw/i386/pc_sysfw.c +9 -0
+             * device_unparent() works, but we have to take a
+             * temporary reference, or else device_unparent() commits
+             * a use-after-free error.
+             */
+            object_ref(dev_obj);
+            object_get_class(dev_obj)->unparent(dev_obj);
+            object_unref(dev_obj);
+            pcms->flash[i] = NULL;
+        }
Quoted 1 line

Paolo

Thank you!

[...]

#19 lore raw
Quoted 3 lines · Philippe Mathieu-Daudé
Quoted 1 line · Markus Armbruster

Paolo Bonzini <[email protected]> writes:

[...]

Quoted 45 lines
Quoted 45 lines
Quoted 2 lines

Does it work if you add the device yourself as a child of /machine, instead of relying on /machine/unattached?

I figure you're suggesting something like this incremental patch:

   diff --git a/hw/i386/pc_sysfw.c b/hw/i386/pc_sysfw.c
   index ccf2221acb..9d3a80c51a 100644
   --- a/hw/i386/pc_sysfw.c
   +++ b/hw/i386/pc_sysfw.c
   @@ -99,6 +99,10 @@ void pc_system_flash_create(PCMachineState *pcms)
        if (pcmc->pci_enabled) {
            pcms->flash[0] = pc_pflash_create("system.flash0");
            pcms->flash[1] = pc_pflash_create("system.flash1");
   +        object_property_add_child(OBJECT(pcms), "pfl0",
   +                                  OBJECT(pcms->flash[0]),
&error_abort);
   +        object_property_add_child(OBJECT(pcms), "pfl1",
   +                                  OBJECT(pcms->flash[1]),
&error_abort);
            object_property_add_alias(OBJECT(pcms), "pflash0",
                                      OBJECT(pcms->flash[0]), "drive",
                                      &error_abort);
   @@ -122,19 +126,7 @@ static void
pc_system_flash_cleanup_unused(PCMachineState *pcms)
                prop_name = g_strdup_printf("pflash%d", i);
                object_property_del(OBJECT(pcms), prop_name, &error_abort);
                g_free(prop_name);
   -            /*
   -             * Delete @dev_obj.  Straight object_unref() is wrong,
   -             * since it leaves dangling references in the parent bus
   -             * behind.  object_unparent() doesn't work, either: since
   -             * @dev_obj hasn't been realized, dev_obj->parent is null,
   -             * and object_unparent() does nothing.  DeviceClass method
   -             * device_unparent() works, but we have to take a
   -             * temporary reference, or else device_unparent() commits
   -             * a use-after-free error.
   -             */
   -            object_ref(dev_obj);
   -            object_get_class(dev_obj)->unparent(dev_obj);
   -            object_unref(dev_obj);
   +            object_unparent(dev_obj);
                pcms->flash[i] = NULL;
            }
        }
Quoted 8 lines
Quoted 6 lines

Passes "make check" and "info qtree" looks good both with and without -machine pflash0,...

I'm not exactly happy with "pfl0" and "pfl1". Got a favorite color for my bikeshed?

ovmf_code & ovmf_vars?

Tempting, since we use these guys only for OVMF so far. However, we could also use them for SeaBIOS[*], or whatever other firmware comes up.

[...]

[*] See also
Subject: Re: Configuring pflash devices for OVMF firmware
Message-ID: <[email protected]>
https://lists.nongnu.org/archive/html/qemu-devel/2019-01/msg08145.html

#20 lore raw

PATCH v3 12/12 docs/interop/firmware.json: Prefer -machine to if=pflash

In reply to Markus Armbruster

The previous commit added a way to configure firmware with -blockdev rather than -drive if=pflash. Document it as the preferred way.

---
 docs/interop/firmware.json | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
docs/interop/firmware.json +14 -6
index 28f9bc1591..ff8c2ce5f2 100644
@@ -212,9 +212,13 @@
 #
 # @executable: Identifies the firmware executable. The firmware
 #              executable may be shared by multiple virtual machine
-#              definitions. The corresponding QEMU command line option
-#              is "-drive
-#              if=pflash,unit=0,readonly=on,file=@executable.@filename,format=@executable.@format".
+#              definitions. The preferred corresponding QEMU command
+#              line options are
+#                  -drive if=none,id=pflash0,readonly=on,file=@executable.@filename,format=@executable.@format
+#                  -machine pflash0=pflash0
+#              or equivalent -blockdev instead of -drive.
+#              With QEMU versions older than 4.0, you have to use
+#                  -drive if=pflash,unit=0,readonly=on,file=@executable.@filename,format=@executable.@format
 #
 # @nvram-template: Identifies the NVRAM template compatible with
 #                  @executable. Management software instantiates an
@@ -225,9 +229,13 @@
 #                  individual copies of it are. An NVRAM file is
 #                  typically used for persistently storing the
 #                  non-volatile UEFI variables of a virtual machine
-#                  definition. The corresponding QEMU command line
-#                  option is "-drive
-#                  if=pflash,unit=1,readonly=off,file=FILENAME_OF_PRIVATE_NVRAM_FILE,format=@nvram-template.@format".
+#                  definition. The preferred corresponding QEMU
+#                  command line options are
+#                      -drive if=none,id=pflash1,readonly=off,file=FILENAME_OF_PRIVATE_NVRAM_FILE,format=@nvram-template.@format
+#                      -machine pflash1=pflash1
+#                  or equivalent -blockdev instead of -drive.
+#                  With QEMU versions older than 4.0, you have to use
+#                      -drive if=pflash,unit=1,readonly=off,file=FILENAME_OF_PRIVATE_NVRAM_FILE,format=@nvram-template.@format
 #
 # Since: 3.0
 ##
Signature · 1 line
2.17.2
Reviewed-by on v3 12/12
#21 lore raw

Re: [Qemu-devel] [PATCH v3 12/12] docs/interop/firmware.json: Prefer -machine to if=pflash

Quoted 37 lines · Markus Armbruster

The previous commit added a way to configure firmware with -blockdev rather than -drive if=pflash. Document it as the preferred way.

---
 docs/interop/firmware.json | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
docs/interop/firmware.json +7 -6
index 28f9bc1591..ff8c2ce5f2 100644
@@ -212,9 +212,13 @@
 #
 # @executable: Identifies the firmware executable. The firmware
 #              executable may be shared by multiple virtual machine
-#              definitions. The corresponding QEMU command line option
-#              is "-drive
-#              if=pflash,unit=0,readonly=on,file=@executable.@filename,format=@executable.@format".
+#              definitions. The preferred corresponding QEMU command
+#              line options are
+#                  -drive if=none,id=pflash0,readonly=on,file=@executable.@filename,format=@executable.@format
+#                  -machine pflash0=pflash0
+#              or equivalent -blockdev instead of -drive.
+#              With QEMU versions older than 4.0, you have to use
+#                  -drive if=pflash,unit=0,readonly=on,file=@executable.@filename,format=@executable.@format
 #
 # @nvram-template: Identifies the NVRAM template compatible with
 #                  @executable. Management software instantiates an
@@ -225,9 +229,13 @@
 #                  individual copies of it are. An NVRAM file is
 #                  typically used for persistently storing the
 #                  non-volatile UEFI variables of a virtual machine
-#                  definition. The corresponding QEMU command line
-#                  option is "-drive
-#                  if=pflash,unit=1,readonly=off,file=FILENAME_OF_PRIVATE_NVRAM_FILE,format=@nvram-template.@format".

docs/interop/firmware.json line 241

Quoted 10 lines
docs/interop/firmware.json +7 -0
+#                  definition. The preferred corresponding QEMU
+#                  command line options are
+#                      -drive if=none,id=pflash1,readonly=off,file=FILENAME_OF_PRIVATE_NVRAM_FILE,format=@nvram-template.@format
+#                      -machine pflash1=pflash1
+#                  or equivalent -blockdev instead of -drive.
+#                  With QEMU versions older than 4.0, you have to use
+#                      -drive if=pflash,unit=1,readonly=off,file=FILENAME_OF_PRIVATE_NVRAM_FILE,format=@nvram-template.@format
 #
 # Since: 3.0
 ##

Awesome.

Thanks!
Laszlo

Reviewed-by on the v3 series
#22 lore raw
In reply to Markus Armbruster
Quoted 29 lines · Markus Armbruster

Based-on: <[email protected]>

v3:
* PATCH 05: Commit message tidied up [Philippe, László]
* PATCH 06: Comment tidied up [checkpatch]
* PATCH 12: Comment polish [László]

v2:
* PATCH 05,06: New [Philippe]
* PATCH 07: Old PATCH 5 rebased onto new patches, with new comment [Philippe]
* PATCH 08+10: New, factored out of old PATCH 6 [by Philippe]
* PATCH 09: New [by Philippe]
* PATCH 11: Remainder of old PATCH 6, with FIXME resolved, literal 4096 de-duplicated [László], extraneous error check deleted [László], and comment typos fixed
* PATCH 12: New

Markus Armbruster (9):
qdev: Fix latent bug with compat_props and onboard devices
qom: Move compat_props machinery from qdev to QOM
vl: Fix latent bug with -global and onboard devices
sysbus: Fix latent bug with onboard devices
vl: Improve legibility of BlockdevOptions queue
vl: Factor configure_blockdev() out of main()
vl: Create block backends before setting machine properties
pc: Support firmware configuration with -blockdev docs/interop/firmware.json: Prefer -machine to if=pflash

Quoted 4 lines

Philippe Mathieu-Daudé (3):
pflash_cfi01: Add pflash_cfi01_get_blk() helper
pc_sysfw: Remove unused PcSysFwDevice
pc_sysfw: Pass PCMachineState to pc_system_firmware_init()

OK looks good to me.

Paolo will you be merging this?

Quoted 17 lines
 accel/accel.c              |   1 +
 docs/interop/firmware.json |  20 ++-
 hw/block/pflash_cfi01.c    |   5 +
 hw/core/qdev.c             |  21 +---
 hw/core/sysbus.c           |   3 -
 hw/i386/pc.c               |   4 +-
 hw/i386/pc_sysfw.c         | 249 ++++++++++++++++++++++++-------------
 include/hw/block/flash.h   |   1 +
 include/hw/i386/pc.h       |   6 +-
 include/hw/qdev-core.h     |   2 -
 include/qom/object.h       |   3 +
 qom/object.c               |  39 ++++++
 vl.c                       | 125 ++++++++++---------
 13 files changed, 303 insertions(+), 176 deletions(-)
Signature · 1 line
2.17.2
#23 lore raw
Quoted 30 lines · Michael S. Tsirkin
Quoted 29 lines · Markus Armbruster

Based-on: <[email protected]>

v3:
* PATCH 05: Commit message tidied up [Philippe, László]
* PATCH 06: Comment tidied up [checkpatch]
* PATCH 12: Comment polish [László]

v2:
* PATCH 05,06: New [Philippe]
* PATCH 07: Old PATCH 5 rebased onto new patches, with new comment [Philippe]
* PATCH 08+10: New, factored out of old PATCH 6 [by Philippe]
* PATCH 09: New [by Philippe]
* PATCH 11: Remainder of old PATCH 6, with FIXME resolved, literal 4096 de-duplicated [László], extraneous error check deleted [László], and comment typos fixed
* PATCH 12: New

Markus Armbruster (9):
qdev: Fix latent bug with compat_props and onboard devices
qom: Move compat_props machinery from qdev to QOM
vl: Fix latent bug with -global and onboard devices
sysbus: Fix latent bug with onboard devices
vl: Improve legibility of BlockdevOptions queue
vl: Factor configure_blockdev() out of main()
vl: Create block backends before setting machine properties
pc: Support firmware configuration with -blockdev docs/interop/firmware.json: Prefer -machine to if=pflash

Quoted 8 lines
Quoted 4 lines

Philippe Mathieu-Daudé (3):
pflash_cfi01: Add pflash_cfi01_get_blk() helper
pc_sysfw: Remove unused PcSysFwDevice
pc_sysfw: Pass PCMachineState to pc_system_firmware_init()

OK looks good to me.

Thanks!

Quoted 1 line

Paolo will you be merging this?

Alternatively, I can do the pull request myself. Paolo, would you like me to?

#24 lore raw

PATCH v4 11/12 pc: Support firmware configuration with -blockdev

In reply to Markus Armbruster

The PC machines put firmware in ROM by default. To get it put into flash memory (required by OVMF), you have to use -drive if=pflash,unit=0,... and optionally -drive if=pflash,unit=1,...

Why two -drive? This permits setting up one part of the flash memory read-only, and the other part read/write. It also makes upgrading firmware on the host easier. Below the hood, it creates two separate flash devices, because we were too lazy to improve our flash device models to support sector protection.

The problem at hand is to do the same with -blockdev somehow, as one more step towards deprecating -drive.

Mapping -drive if=none,... to -blockdev is a solved problem. With if=T other than if=none, -drive additionally configures a block device frontend. For non-onboard devices, that part maps to -device. Also a solved problem. For onboard devices such as PC flash memory, we have an unsolved problem.

This is actually an instance of a wider problem: our general device configuration interface doesn't cover onboard devices. Instead, we have a zoo of ad hoc interfaces that are much more limited. One of them is -drive, which we'd rather deprecate, but can't until we have suitable replacements for all its uses.

Sadly, I can't attack the wider problem today. So back to the narrow problem.

My first idea was to reduce it to its solved buddy by using pluggable instead of onboard devices for the flash memory. Workable, but it requires some extra smarts in firmware descriptors and libvirt. Paolo had an idea that is simpler for libvirt: keep the devices onboard, and add machine properties for their block backends.

The implementation is less than straightforward, I'm afraid.

First, block backend properties are *qdev* properties. Machines can't have those, as they're not devices. I could duplicate these qdev properties as QOM properties, but I hate that.

More seriously, the properties do not belong to the machine, they belong to the onboard flash devices. Adding them to the machine would then require bad magic to somehow transfer them to the flash devices. Fortunately, QOM provides the means to handle exactly this case: add alias properties to the machine that forward to the onboard devices' properties.

Properties need to be created in .instance_init() methods. For PC machines, that's pc_machine_initfn(). To make alias properties work, we need to create the onboard flash devices there, too. Requires several bug fixes, in the previous commits. We also have to realize the devices. More on that below.

If the user sets pflash0, firmware resides in flash memory. pc_system_firmware_init() maps and realizes the flash devices.

Else, firmware resides in ROM. The onboard flash devices aren't used then. pc_system_firmware_init() destroys them unrealized, along with the alias properties.

The existing code to pick up drives defined with -drive if=pflash is replaced by code to desugar into the machine properties.

---
 hw/i386/pc.c         |   2 +
 hw/i386/pc_sysfw.c   | 233 ++++++++++++++++++++++++++++---------------
 include/hw/i386/pc.h |   3 +
 3 files changed, 156 insertions(+), 82 deletions(-)
hw/i386/pc.c +2 -0
index b2947e433b..7e7f4cbfb4 100644
@@ -2643,6 +2643,8 @@ static void pc_machine_initfn(Object *obj)
     pcms->smbus_enabled = true;
     pcms->sata_enabled = true;
     pcms->pit_enabled = true;
+
+    pc_system_flash_create(pcms);
 }
 
 static void pc_machine_reset(void)
hw/i386/pc_sysfw.c +151 -82

276 lines not on this page · raw message

include/hw/i386/pc.h +3 -0
index 4f5ed7cefc..276ff15d4d 100644
@@ -6,6 +6,7 @@
 #include "hw/boards.h"
 #include "hw/isa/isa.h"
 #include "hw/block/fdc.h"
+#include "hw/block/flash.h"
 #include "net/net.h"
 #include "hw/i386/ioapic.h"
 
@@ -39,6 +40,7 @@ struct PCMachineState {
     PCIBus *bus;
     FWCfgState *fw_cfg;
     qemu_irq *gsi;
+    PFlashCFI01 *flash[2];
 
     /* Configuration options: */
     uint64_t max_ram_below_4g;
@@ -277,6 +279,7 @@ extern PCIDevice *piix4_dev;
 int piix4_init(PCIBus *bus, ISABus **isa_bus, int devfn);
 
 /* pc_sysfw.c */
+void pc_system_flash_create(PCMachineState *pcms);
 void pc_system_firmware_init(PCMachineState *pcms, MemoryRegion *rom_memory);
 
 /* acpi-build.c */
Signature · 1 line
2.17.2
#25 lore raw

Incremental diff since v3:

hw/i386/pc_sysfw.c +12 -22
index ccf2221acb..c628540774 100644
@@ -82,13 +82,19 @@ static void pc_isa_bios_init(MemoryRegion *rom_memory,
     memory_region_set_readonly(isa_bios, true);
 }
 
-static PFlashCFI01 *pc_pflash_create(const char *name)
+static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms,
+                                     const char *name,
+                                     const char *alias_prop_name)
 {
     DeviceState *dev = qdev_create(NULL, TYPE_PFLASH_CFI01);
 
     qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE);
     qdev_prop_set_uint8(dev, "width", 1);
     qdev_prop_set_string(dev, "name", name);
+    object_property_add_child(OBJECT(pcms), name, OBJECT(dev),
+                              &error_abort);
+    object_property_add_alias(OBJECT(pcms), alias_prop_name,
+                              OBJECT(dev), "drive", &error_abort);
     return PFLASH_CFI01(dev);
 }
 
@@ -97,14 +103,10 @@ void pc_system_flash_create(PCMachineState *pcms)
     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
 
     if (pcmc->pci_enabled) {
-        pcms->flash[0] = pc_pflash_create("system.flash0");
-        pcms->flash[1] = pc_pflash_create("system.flash1");
-        object_property_add_alias(OBJECT(pcms), "pflash0",
-                                  OBJECT(pcms->flash[0]), "drive",
-                                  &error_abort);
-        object_property_add_alias(OBJECT(pcms), "pflash1",
-                                  OBJECT(pcms->flash[1]), "drive",
-                                  &error_abort);
+        pcms->flash[0] = pc_pflash_create(pcms, "system.flash0",
+                                          "pflash0");
+        pcms->flash[1] = pc_pflash_create(pcms, "system.flash1",
+                                          "pflash1");
     }
 }
 
@@ -122,19 +124,7 @@ static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
             prop_name = g_strdup_printf("pflash%d", i);
             object_property_del(OBJECT(pcms), prop_name, &error_abort);
             g_free(prop_name);
-            /*
-             * Delete @dev_obj.  Straight object_unref() is wrong,
-             * since it leaves dangling references in the parent bus
-             * behind.  object_unparent() doesn't work, either: since
-             * @dev_obj hasn't been realized, dev_obj->parent is null,
-             * and object_unparent() does nothing.  DeviceClass method
-             * device_unparent() works, but we have to take a
-             * temporary reference, or else device_unparent() commits
-             * a use-after-free error.
-             */
-            object_ref(dev_obj);
-            object_get_class(dev_obj)->unparent(dev_obj);
-            object_unref(dev_obj);
+            object_unparent(dev_obj);
             pcms->flash[i] = NULL;
         }
     }
#26 lore raw
Quoted 1 line · Markus Armbruster

Incremental diff since v3:

Ack, thanks.

Paolo

Quoted 66 lines
hw/i386/pc_sysfw.c +12 -22
index ccf2221acb..c628540774 100644
@@ -82,13 +82,19 @@ static void pc_isa_bios_init(MemoryRegion *rom_memory,
     memory_region_set_readonly(isa_bios, true);
 }
 
-static PFlashCFI01 *pc_pflash_create(const char *name)
+static PFlashCFI01 *pc_pflash_create(PCMachineState *pcms,
+                                     const char *name,
+                                     const char *alias_prop_name)
 {
     DeviceState *dev = qdev_create(NULL, TYPE_PFLASH_CFI01);
 
     qdev_prop_set_uint64(dev, "sector-length", FLASH_SECTOR_SIZE);
     qdev_prop_set_uint8(dev, "width", 1);
     qdev_prop_set_string(dev, "name", name);
+    object_property_add_child(OBJECT(pcms), name, OBJECT(dev),
+                              &error_abort);
+    object_property_add_alias(OBJECT(pcms), alias_prop_name,
+                              OBJECT(dev), "drive", &error_abort);
     return PFLASH_CFI01(dev);
 }
 
@@ -97,14 +103,10 @@ void pc_system_flash_create(PCMachineState *pcms)
     PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
 
     if (pcmc->pci_enabled) {
-        pcms->flash[0] = pc_pflash_create("system.flash0");
-        pcms->flash[1] = pc_pflash_create("system.flash1");
-        object_property_add_alias(OBJECT(pcms), "pflash0",
-                                  OBJECT(pcms->flash[0]), "drive",
-                                  &error_abort);
-        object_property_add_alias(OBJECT(pcms), "pflash1",
-                                  OBJECT(pcms->flash[1]), "drive",
-                                  &error_abort);
+        pcms->flash[0] = pc_pflash_create(pcms, "system.flash0",
+                                          "pflash0");
+        pcms->flash[1] = pc_pflash_create(pcms, "system.flash1",
+                                          "pflash1");
     }
 }
 
@@ -122,19 +124,7 @@ static void pc_system_flash_cleanup_unused(PCMachineState *pcms)
             prop_name = g_strdup_printf("pflash%d", i);
             object_property_del(OBJECT(pcms), prop_name, &error_abort);
             g_free(prop_name);
-            /*
-             * Delete @dev_obj.  Straight object_unref() is wrong,
-             * since it leaves dangling references in the parent bus
-             * behind.  object_unparent() doesn't work, either: since
-             * @dev_obj hasn't been realized, dev_obj->parent is null,
-             * and object_unparent() does nothing.  DeviceClass method
-             * device_unparent() works, but we have to take a
-             * temporary reference, or else device_unparent() commits
-             * a use-after-free error.
-             */
-            object_ref(dev_obj);
-            object_get_class(dev_obj)->unparent(dev_obj);
-            object_unref(dev_obj);
+            object_unparent(dev_obj);
             pcms->flash[i] = NULL;
         }
     }