2015-07-13 19:35:41 +02:00
|
|
|
/*
|
|
|
|
* Dynamic device configuration and creation -- buses.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 CodeSourcery
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2020-10-23 14:44:24 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2015-07-13 19:35:41 +02:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2019-08-12 07:23:51 +02:00
|
|
|
#include "hw/qdev-properties.h"
|
2019-05-23 16:35:06 +02:00
|
|
|
#include "qemu/ctype.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2015-07-13 19:35:41 +02:00
|
|
|
#include "qapi/error.h"
|
|
|
|
|
qdev: Drop qbus_set_hotplug_handler() parameter @errp
qbus_set_hotplug_handler() is a simple wrapper around
object_property_set_link().
object_property_set_link() fails when the property doesn't exist, is
not settable, or its .check() method fails. These are all programming
errors here, so passing &error_abort to qbus_set_hotplug_handler() is
appropriate.
Most of its callers do. Exceptions:
* pcie_cap_slot_init(), shpc_init(), spapr_phb_realize() pass NULL,
i.e. they ignore errors.
* spapr_machine_init() passes &error_fatal.
* s390_pcihost_realize(), virtio_serial_device_realize(),
s390_pcihost_plug() pass the error to their callers. The latter two
keep going after the error, which looks wrong.
Drop the @errp parameter, and instead pass &error_abort to
object_property_set_link().
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200630090351.1247703-15-armbru@redhat.com>
2020-06-30 11:03:39 +02:00
|
|
|
void qbus_set_hotplug_handler(BusState *bus, Object *handler)
|
2015-07-13 19:35:41 +02:00
|
|
|
{
|
qom: Put name parameter before value / visitor parameter
The object_property_set_FOO() setters take property name and value in
an unusual order:
void object_property_set_FOO(Object *obj, FOO_TYPE value,
const char *name, Error **errp)
Having to pass value before name feels grating. Swap them.
Same for object_property_set(), object_property_get(), and
object_property_parse().
Convert callers with this Coccinelle script:
@@
identifier fun = {
object_property_get, object_property_parse, object_property_set_str,
object_property_set_link, object_property_set_bool,
object_property_set_int, object_property_set_uint, object_property_set,
object_property_set_qobject
};
expression obj, v, name, errp;
@@
- fun(obj, v, name, errp)
+ fun(obj, name, v, errp)
Chokes on hw/arm/musicpal.c's lcd_refresh() with the unhelpful error
message "no position information". Convert that one manually.
Fails to convert hw/arm/armsse.c, because Coccinelle gets confused by
ARMSSE being used both as typedef and function-like macro there.
Convert manually.
Fails to convert hw/rx/rx-gdbsim.c, because Coccinelle gets confused
by RXCPU being used both as typedef and function-like macro there.
Convert manually. The other files using RXCPU that way don't need
conversion.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200707160613.848843-27-armbru@redhat.com>
[Straightforwad conflict with commit 2336172d9b "audio: set default
value for pcspk.iobase property" resolved]
2020-07-07 18:05:54 +02:00
|
|
|
object_property_set_link(OBJECT(bus), QDEV_HOTPLUG_HANDLER_PROPERTY,
|
|
|
|
handler, &error_abort);
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 11:03:38 +02:00
|
|
|
void qbus_set_bus_hotplug_handler(BusState *bus)
|
2015-07-13 19:35:41 +02:00
|
|
|
{
|
qdev: Drop qbus_set_hotplug_handler() parameter @errp
qbus_set_hotplug_handler() is a simple wrapper around
object_property_set_link().
object_property_set_link() fails when the property doesn't exist, is
not settable, or its .check() method fails. These are all programming
errors here, so passing &error_abort to qbus_set_hotplug_handler() is
appropriate.
Most of its callers do. Exceptions:
* pcie_cap_slot_init(), shpc_init(), spapr_phb_realize() pass NULL,
i.e. they ignore errors.
* spapr_machine_init() passes &error_fatal.
* s390_pcihost_realize(), virtio_serial_device_realize(),
s390_pcihost_plug() pass the error to their callers. The latter two
keep going after the error, which looks wrong.
Drop the @errp parameter, and instead pass &error_abort to
object_property_set_link().
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200630090351.1247703-15-armbru@redhat.com>
2020-06-30 11:03:39 +02:00
|
|
|
qbus_set_hotplug_handler(bus, OBJECT(bus));
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int qbus_walk_children(BusState *bus,
|
|
|
|
qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
|
|
|
|
qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
BusChild *kid;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (pre_busfn) {
|
|
|
|
err = pre_busfn(bus, opaque);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 14:38:59 +02:00
|
|
|
WITH_RCU_READ_LOCK_GUARD() {
|
|
|
|
QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) {
|
|
|
|
err = qdev_walk_children(kid->child,
|
|
|
|
pre_devfn, pre_busfn,
|
|
|
|
post_devfn, post_busfn, opaque);
|
|
|
|
if (err < 0) {
|
|
|
|
return err;
|
|
|
|
}
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (post_busfn) {
|
|
|
|
err = post_busfn(bus, opaque);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-30 17:02:04 +01:00
|
|
|
void bus_cold_reset(BusState *bus)
|
|
|
|
{
|
|
|
|
resettable_reset(OBJECT(bus), RESET_TYPE_COLD);
|
|
|
|
}
|
|
|
|
|
2020-01-30 17:02:04 +01:00
|
|
|
bool bus_is_in_reset(BusState *bus)
|
|
|
|
{
|
|
|
|
return resettable_is_in_reset(OBJECT(bus));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ResettableState *bus_get_reset_state(Object *obj)
|
|
|
|
{
|
|
|
|
BusState *bus = BUS(obj);
|
|
|
|
return &bus->reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bus_reset_child_foreach(Object *obj, ResettableChildCallback cb,
|
|
|
|
void *opaque, ResetType type)
|
|
|
|
{
|
|
|
|
BusState *bus = BUS(obj);
|
|
|
|
BusChild *kid;
|
|
|
|
|
2020-10-06 14:38:59 +02:00
|
|
|
WITH_RCU_READ_LOCK_GUARD() {
|
|
|
|
QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) {
|
|
|
|
cb(OBJECT(kid->child), opaque, type);
|
|
|
|
}
|
2020-01-30 17:02:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 07:31:50 +02:00
|
|
|
static void qbus_init(BusState *bus, DeviceState *parent, const char *name)
|
2015-07-13 19:35:41 +02:00
|
|
|
{
|
|
|
|
const char *typename = object_get_typename(OBJECT(bus));
|
|
|
|
BusClass *bc;
|
2016-07-15 12:04:49 +02:00
|
|
|
int i, bus_id;
|
2015-07-13 19:35:41 +02:00
|
|
|
|
|
|
|
bus->parent = parent;
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
bus->name = g_strdup(name);
|
|
|
|
} else if (bus->parent && bus->parent->id) {
|
|
|
|
/* parent device has id -> use it plus parent-bus-id for bus name */
|
|
|
|
bus_id = bus->parent->num_child_bus;
|
2016-07-15 12:04:49 +02:00
|
|
|
bus->name = g_strdup_printf("%s.%d", bus->parent->id, bus_id);
|
2015-07-13 19:35:41 +02:00
|
|
|
} else {
|
|
|
|
/* no id -> use lowercase bus type plus global bus-id for bus name */
|
|
|
|
bc = BUS_GET_CLASS(bus);
|
|
|
|
bus_id = bc->automatic_ids++;
|
2016-07-15 12:04:49 +02:00
|
|
|
bus->name = g_strdup_printf("%s.%d", typename, bus_id);
|
|
|
|
for (i = 0; bus->name[i]; i++) {
|
|
|
|
bus->name[i] = qemu_tolower(bus->name[i]);
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bus->parent) {
|
|
|
|
QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
|
|
|
|
bus->parent->num_child_bus++;
|
qom: Drop parameter @errp of object_property_add() & friends
The only way object_property_add() can fail is when a property with
the same name already exists. Since our property names are all
hardcoded, failure is a programming error, and the appropriate way to
handle it is passing &error_abort.
Same for its variants, except for object_property_add_child(), which
additionally fails when the child already has a parent. Parentage is
also under program control, so this is a programming error, too.
We have a bit over 500 callers. Almost half of them pass
&error_abort, slightly fewer ignore errors, one test case handles
errors, and the remaining few callers pass them to their own callers.
The previous few commits demonstrated once again that ignoring
programming errors is a bad idea.
Of the few ones that pass on errors, several violate the Error API.
The Error ** argument must be NULL, &error_abort, &error_fatal, or a
pointer to a variable containing NULL. Passing an argument of the
latter kind twice without clearing it in between is wrong: if the
first call sets an error, it no longer points to NULL for the second
call. ich9_pm_add_properties(), sparc32_ledma_realize(),
sparc32_dma_realize(), xilinx_axidma_realize(), xilinx_enet_realize()
are wrong that way.
When the one appropriate choice of argument is &error_abort, letting
users pick the argument is a bad idea.
Drop parameter @errp and assert the preconditions instead.
There's one exception to "duplicate property name is a programming
error": the way object_property_add() implements the magic (and
undocumented) "automatic arrayification". Don't drop @errp there.
Instead, rename object_property_add() to object_property_try_add(),
and add the obvious wrapper object_property_add().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-15-armbru@redhat.com>
[Two semantic rebase conflicts resolved]
2020-05-05 17:29:22 +02:00
|
|
|
object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus));
|
2018-06-13 19:28:15 +02:00
|
|
|
object_unref(OBJECT(bus));
|
2019-05-23 17:05:43 +02:00
|
|
|
} else {
|
|
|
|
/* The only bus without a parent is the main system bus */
|
|
|
|
assert(bus == sysbus_get_default());
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bus_unparent(Object *obj)
|
|
|
|
{
|
|
|
|
BusState *bus = BUS(obj);
|
|
|
|
BusChild *kid;
|
|
|
|
|
2019-05-23 17:05:43 +02:00
|
|
|
/* Only the main system bus has no parent, and that bus is never freed */
|
|
|
|
assert(bus->parent);
|
|
|
|
|
2015-07-13 19:35:41 +02:00
|
|
|
while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
|
|
|
|
DeviceState *dev = kid->child;
|
|
|
|
object_unparent(OBJECT(dev));
|
|
|
|
}
|
2019-05-23 17:05:43 +02:00
|
|
|
QLIST_REMOVE(bus, sibling);
|
|
|
|
bus->parent->num_child_bus--;
|
|
|
|
bus->parent = NULL;
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void qbus_create_inplace(void *bus, size_t size, const char *typename,
|
|
|
|
DeviceState *parent, const char *name)
|
|
|
|
{
|
|
|
|
object_initialize(bus, size, typename);
|
2020-06-10 07:31:50 +02:00
|
|
|
qbus_init(bus, parent, name);
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
|
|
|
|
{
|
|
|
|
BusState *bus;
|
|
|
|
|
|
|
|
bus = BUS(object_new(typename));
|
2020-06-10 07:31:50 +02:00
|
|
|
qbus_init(bus, parent, name);
|
2015-07-13 19:35:41 +02:00
|
|
|
|
|
|
|
return bus;
|
|
|
|
}
|
|
|
|
|
qdev: New qdev_new(), qdev_realize(), etc.
We commonly plug devices into their bus right when we create them,
like this:
dev = qdev_create(bus, type_name);
Note that @dev is a weak reference. The reference from @bus to @dev
is the only strong one.
We realize at some later time, either with
object_property_set_bool(OBJECT(dev), true, "realized", errp);
or its convenience wrapper
qdev_init_nofail(dev);
If @dev still has no QOM parent then, realizing makes the
/machine/unattached/ orphanage its QOM parent.
Note that the device returned by qdev_create() is plugged into a bus,
but doesn't have a QOM parent, yet. Until it acquires one,
unrealizing the bus will hang in bus_unparent():
while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
DeviceState *dev = kid->child;
object_unparent(OBJECT(dev));
}
object_unparent() does nothing when its argument has no QOM parent,
and the loop spins forever.
Device state "no QOM parent, but plugged into bus" is dangerous.
Paolo suggested to delay plugging into the bus until realize. We need
to plug into the parent bus before we call the device's realize
method, in case it uses the parent bus. So the dangerous state still
exists, but only within realization, where we can manage it safely.
This commit creates infrastructure to do this:
dev = qdev_new(type_name);
...
qdev_realize_and_unref(dev, bus, errp)
Note that @dev becomes a strong reference here.
qdev_realize_and_unref() drops it. There is also plain
qdev_realize(), which doesn't drop it.
The remainder of this series will convert all users to this new
interface.
Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
Cc: Alistair Francis <alistair@alistair23.me>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Cc: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200610053247.1583243-5-armbru@redhat.com>
2020-06-10 07:31:53 +02:00
|
|
|
bool qbus_realize(BusState *bus, Error **errp)
|
|
|
|
{
|
2020-07-07 18:05:57 +02:00
|
|
|
return object_property_set_bool(OBJECT(bus), "realized", true, errp);
|
qdev: New qdev_new(), qdev_realize(), etc.
We commonly plug devices into their bus right when we create them,
like this:
dev = qdev_create(bus, type_name);
Note that @dev is a weak reference. The reference from @bus to @dev
is the only strong one.
We realize at some later time, either with
object_property_set_bool(OBJECT(dev), true, "realized", errp);
or its convenience wrapper
qdev_init_nofail(dev);
If @dev still has no QOM parent then, realizing makes the
/machine/unattached/ orphanage its QOM parent.
Note that the device returned by qdev_create() is plugged into a bus,
but doesn't have a QOM parent, yet. Until it acquires one,
unrealizing the bus will hang in bus_unparent():
while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
DeviceState *dev = kid->child;
object_unparent(OBJECT(dev));
}
object_unparent() does nothing when its argument has no QOM parent,
and the loop spins forever.
Device state "no QOM parent, but plugged into bus" is dangerous.
Paolo suggested to delay plugging into the bus until realize. We need
to plug into the parent bus before we call the device's realize
method, in case it uses the parent bus. So the dangerous state still
exists, but only within realization, where we can manage it safely.
This commit creates infrastructure to do this:
dev = qdev_new(type_name);
...
qdev_realize_and_unref(dev, bus, errp)
Note that @dev becomes a strong reference here.
qdev_realize_and_unref() drops it. There is also plain
qdev_realize(), which doesn't drop it.
The remainder of this series will convert all users to this new
interface.
Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
Cc: Alistair Francis <alistair@alistair23.me>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Cc: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200610053247.1583243-5-armbru@redhat.com>
2020-06-10 07:31:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void qbus_unrealize(BusState *bus)
|
|
|
|
{
|
qom: Put name parameter before value / visitor parameter
The object_property_set_FOO() setters take property name and value in
an unusual order:
void object_property_set_FOO(Object *obj, FOO_TYPE value,
const char *name, Error **errp)
Having to pass value before name feels grating. Swap them.
Same for object_property_set(), object_property_get(), and
object_property_parse().
Convert callers with this Coccinelle script:
@@
identifier fun = {
object_property_get, object_property_parse, object_property_set_str,
object_property_set_link, object_property_set_bool,
object_property_set_int, object_property_set_uint, object_property_set,
object_property_set_qobject
};
expression obj, v, name, errp;
@@
- fun(obj, v, name, errp)
+ fun(obj, name, v, errp)
Chokes on hw/arm/musicpal.c's lcd_refresh() with the unhelpful error
message "no position information". Convert that one manually.
Fails to convert hw/arm/armsse.c, because Coccinelle gets confused by
ARMSSE being used both as typedef and function-like macro there.
Convert manually.
Fails to convert hw/rx/rx-gdbsim.c, because Coccinelle gets confused
by RXCPU being used both as typedef and function-like macro there.
Convert manually. The other files using RXCPU that way don't need
conversion.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200707160613.848843-27-armbru@redhat.com>
[Straightforwad conflict with commit 2336172d9b "audio: set default
value for pcspk.iobase property" resolved]
2020-07-07 18:05:54 +02:00
|
|
|
object_property_set_bool(OBJECT(bus), "realized", false, &error_abort);
|
qdev: New qdev_new(), qdev_realize(), etc.
We commonly plug devices into their bus right when we create them,
like this:
dev = qdev_create(bus, type_name);
Note that @dev is a weak reference. The reference from @bus to @dev
is the only strong one.
We realize at some later time, either with
object_property_set_bool(OBJECT(dev), true, "realized", errp);
or its convenience wrapper
qdev_init_nofail(dev);
If @dev still has no QOM parent then, realizing makes the
/machine/unattached/ orphanage its QOM parent.
Note that the device returned by qdev_create() is plugged into a bus,
but doesn't have a QOM parent, yet. Until it acquires one,
unrealizing the bus will hang in bus_unparent():
while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
DeviceState *dev = kid->child;
object_unparent(OBJECT(dev));
}
object_unparent() does nothing when its argument has no QOM parent,
and the loop spins forever.
Device state "no QOM parent, but plugged into bus" is dangerous.
Paolo suggested to delay plugging into the bus until realize. We need
to plug into the parent bus before we call the device's realize
method, in case it uses the parent bus. So the dangerous state still
exists, but only within realization, where we can manage it safely.
This commit creates infrastructure to do this:
dev = qdev_new(type_name);
...
qdev_realize_and_unref(dev, bus, errp)
Note that @dev becomes a strong reference here.
qdev_realize_and_unref() drops it. There is also plain
qdev_realize(), which doesn't drop it.
The remainder of this series will convert all users to this new
interface.
Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
Cc: Alistair Francis <alistair@alistair23.me>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Cc: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200610053247.1583243-5-armbru@redhat.com>
2020-06-10 07:31:53 +02:00
|
|
|
}
|
|
|
|
|
2015-07-13 19:35:41 +02:00
|
|
|
static bool bus_get_realized(Object *obj, Error **errp)
|
|
|
|
{
|
|
|
|
BusState *bus = BUS(obj);
|
|
|
|
|
|
|
|
return bus->realized;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bus_set_realized(Object *obj, bool value, Error **errp)
|
|
|
|
{
|
|
|
|
BusState *bus = BUS(obj);
|
|
|
|
BusClass *bc = BUS_GET_CLASS(bus);
|
|
|
|
BusChild *kid;
|
|
|
|
|
|
|
|
if (value && !bus->realized) {
|
|
|
|
if (bc->realize) {
|
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 17:29:24 +02:00
|
|
|
bc->realize(bus, errp);
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: recursive realization */
|
|
|
|
} else if (!value && bus->realized) {
|
2020-10-06 14:38:59 +02:00
|
|
|
WITH_RCU_READ_LOCK_GUARD() {
|
|
|
|
QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) {
|
|
|
|
DeviceState *dev = kid->child;
|
|
|
|
qdev_unrealize(dev);
|
|
|
|
}
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 17:29:24 +02:00
|
|
|
if (bc->unrealize) {
|
|
|
|
bc->unrealize(bus);
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bus->realized = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qbus_initfn(Object *obj)
|
|
|
|
{
|
|
|
|
BusState *bus = BUS(obj);
|
|
|
|
|
|
|
|
QTAILQ_INIT(&bus->children);
|
|
|
|
object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
|
|
|
|
TYPE_HOTPLUG_HANDLER,
|
|
|
|
(Object **)&bus->hotplug_handler,
|
|
|
|
object_property_allow_set_link,
|
qom: Drop parameter @errp of object_property_add() & friends
The only way object_property_add() can fail is when a property with
the same name already exists. Since our property names are all
hardcoded, failure is a programming error, and the appropriate way to
handle it is passing &error_abort.
Same for its variants, except for object_property_add_child(), which
additionally fails when the child already has a parent. Parentage is
also under program control, so this is a programming error, too.
We have a bit over 500 callers. Almost half of them pass
&error_abort, slightly fewer ignore errors, one test case handles
errors, and the remaining few callers pass them to their own callers.
The previous few commits demonstrated once again that ignoring
programming errors is a bad idea.
Of the few ones that pass on errors, several violate the Error API.
The Error ** argument must be NULL, &error_abort, &error_fatal, or a
pointer to a variable containing NULL. Passing an argument of the
latter kind twice without clearing it in between is wrong: if the
first call sets an error, it no longer points to NULL for the second
call. ich9_pm_add_properties(), sparc32_ledma_realize(),
sparc32_dma_realize(), xilinx_axidma_realize(), xilinx_enet_realize()
are wrong that way.
When the one appropriate choice of argument is &error_abort, letting
users pick the argument is a bad idea.
Drop parameter @errp and assert the preconditions instead.
There's one exception to "duplicate property name is a programming
error": the way object_property_add() implements the magic (and
undocumented) "automatic arrayification". Don't drop @errp there.
Instead, rename object_property_add() to object_property_try_add(),
and add the obvious wrapper object_property_add().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-15-armbru@redhat.com>
[Two semantic rebase conflicts resolved]
2020-05-05 17:29:22 +02:00
|
|
|
0);
|
2015-07-13 19:35:41 +02:00
|
|
|
object_property_add_bool(obj, "realized",
|
qom: Drop parameter @errp of object_property_add() & friends
The only way object_property_add() can fail is when a property with
the same name already exists. Since our property names are all
hardcoded, failure is a programming error, and the appropriate way to
handle it is passing &error_abort.
Same for its variants, except for object_property_add_child(), which
additionally fails when the child already has a parent. Parentage is
also under program control, so this is a programming error, too.
We have a bit over 500 callers. Almost half of them pass
&error_abort, slightly fewer ignore errors, one test case handles
errors, and the remaining few callers pass them to their own callers.
The previous few commits demonstrated once again that ignoring
programming errors is a bad idea.
Of the few ones that pass on errors, several violate the Error API.
The Error ** argument must be NULL, &error_abort, &error_fatal, or a
pointer to a variable containing NULL. Passing an argument of the
latter kind twice without clearing it in between is wrong: if the
first call sets an error, it no longer points to NULL for the second
call. ich9_pm_add_properties(), sparc32_ledma_realize(),
sparc32_dma_realize(), xilinx_axidma_realize(), xilinx_enet_realize()
are wrong that way.
When the one appropriate choice of argument is &error_abort, letting
users pick the argument is a bad idea.
Drop parameter @errp and assert the preconditions instead.
There's one exception to "duplicate property name is a programming
error": the way object_property_add() implements the magic (and
undocumented) "automatic arrayification". Don't drop @errp there.
Instead, rename object_property_add() to object_property_try_add(),
and add the obvious wrapper object_property_add().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-15-armbru@redhat.com>
[Two semantic rebase conflicts resolved]
2020-05-05 17:29:22 +02:00
|
|
|
bus_get_realized, bus_set_realized);
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *default_bus_get_fw_dev_path(DeviceState *dev)
|
|
|
|
{
|
|
|
|
return g_strdup(object_get_typename(OBJECT(dev)));
|
|
|
|
}
|
|
|
|
|
2020-01-30 17:02:04 +01:00
|
|
|
/**
|
|
|
|
* bus_phases_reset:
|
|
|
|
* Transition reset method for buses to allow moving
|
|
|
|
* smoothly from legacy reset method to multi-phases
|
|
|
|
*/
|
|
|
|
static void bus_phases_reset(BusState *bus)
|
|
|
|
{
|
|
|
|
ResettableClass *rc = RESETTABLE_GET_CLASS(bus);
|
|
|
|
|
|
|
|
if (rc->phases.enter) {
|
|
|
|
rc->phases.enter(OBJECT(bus), RESET_TYPE_COLD);
|
|
|
|
}
|
|
|
|
if (rc->phases.hold) {
|
|
|
|
rc->phases.hold(OBJECT(bus));
|
|
|
|
}
|
|
|
|
if (rc->phases.exit) {
|
|
|
|
rc->phases.exit(OBJECT(bus));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bus_transitional_reset(Object *obj)
|
|
|
|
{
|
|
|
|
BusClass *bc = BUS_GET_CLASS(obj);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This will call either @bus_phases_reset (for multi-phases transitioned
|
|
|
|
* buses) or a bus's specific method for not-yet transitioned buses.
|
|
|
|
* In both case, it does not reset children.
|
|
|
|
*/
|
|
|
|
if (bc->reset) {
|
|
|
|
bc->reset(BUS(obj));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bus_get_transitional_reset:
|
|
|
|
* check if the bus's class is ready for multi-phase
|
|
|
|
*/
|
|
|
|
static ResettableTrFunction bus_get_transitional_reset(Object *obj)
|
|
|
|
{
|
|
|
|
BusClass *dc = BUS_GET_CLASS(obj);
|
|
|
|
if (dc->reset != bus_phases_reset) {
|
|
|
|
/*
|
|
|
|
* dc->reset has been overridden by a subclass,
|
|
|
|
* the bus is not ready for multi phase yet.
|
|
|
|
*/
|
|
|
|
return bus_transitional_reset;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-07-13 19:35:41 +02:00
|
|
|
static void bus_class_init(ObjectClass *class, void *data)
|
|
|
|
{
|
|
|
|
BusClass *bc = BUS_CLASS(class);
|
2020-01-30 17:02:04 +01:00
|
|
|
ResettableClass *rc = RESETTABLE_CLASS(class);
|
2015-07-13 19:35:41 +02:00
|
|
|
|
|
|
|
class->unparent = bus_unparent;
|
|
|
|
bc->get_fw_dev_path = default_bus_get_fw_dev_path;
|
2020-01-30 17:02:04 +01:00
|
|
|
|
|
|
|
rc->get_state = bus_get_reset_state;
|
|
|
|
rc->child_foreach = bus_reset_child_foreach;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @bus_phases_reset is put as the default reset method below, allowing
|
|
|
|
* to do the multi-phase transition from base classes to leaf classes. It
|
|
|
|
* allows a legacy-reset Bus class to extend a multi-phases-reset
|
|
|
|
* Bus class for the following reason:
|
|
|
|
* + If a base class B has been moved to multi-phase, then it does not
|
|
|
|
* override this default reset method and may have defined phase methods.
|
|
|
|
* + A child class C (extending class B) which uses
|
|
|
|
* bus_class_set_parent_reset() (or similar means) to override the
|
|
|
|
* reset method will still work as expected. @bus_phases_reset function
|
|
|
|
* will be registered as the parent reset method and effectively call
|
|
|
|
* parent reset phases.
|
|
|
|
*/
|
|
|
|
bc->reset = bus_phases_reset;
|
|
|
|
rc->get_transitional_function = bus_get_transitional_reset;
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qbus_finalize(Object *obj)
|
|
|
|
{
|
|
|
|
BusState *bus = BUS(obj);
|
|
|
|
|
2016-07-15 12:04:49 +02:00
|
|
|
g_free(bus->name);
|
2015-07-13 19:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo bus_info = {
|
|
|
|
.name = TYPE_BUS,
|
|
|
|
.parent = TYPE_OBJECT,
|
|
|
|
.instance_size = sizeof(BusState),
|
|
|
|
.abstract = true,
|
|
|
|
.class_size = sizeof(BusClass),
|
|
|
|
.instance_init = qbus_initfn,
|
|
|
|
.instance_finalize = qbus_finalize,
|
|
|
|
.class_init = bus_class_init,
|
2020-01-30 17:02:04 +01:00
|
|
|
.interfaces = (InterfaceInfo[]) {
|
|
|
|
{ TYPE_RESETTABLE_INTERFACE },
|
|
|
|
{ }
|
|
|
|
},
|
2015-07-13 19:35:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bus_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&bus_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(bus_register_types)
|