* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
+#include "qemu/osdep.h"
#include "hw/qdev.h"
#include "hw/sysbus.h"
#include "monitor/monitor.h"
#include "monitor/qdev.h"
#include "qmp-commands.h"
#include "sysemu/arch_init.h"
+#include "qapi/qmp/qerror.h"
#include "qemu/config-file.h"
+#include "qemu/error-report.h"
/*
* Aliases were a bad idea from the start. Let's keep them
{ "lsi53c895a", "lsi" },
{ "ich9-ahci", "ahci" },
{ "kvm-pci-assign", "pci-assign" },
+ { "e1000", "e1000-82540em" },
{ }
};
object_property_parse(obj, value, name, &err);
if (err != NULL) {
- qerror_report_err(err);
- error_free(err);
+ error_propagate(errp, err);
return -1;
}
return 0;
}
if (object_class_is_abstract(oc)) {
- error_set(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
- "non-abstract device type");
+ error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
+ "non-abstract device type");
return NULL;
}
dc = DEVICE_CLASS(oc);
if (dc->cannot_instantiate_with_device_add_yet ||
(qdev_hotplug && !dc->hotpluggable)) {
- error_set(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
- "pluggable device type");
+ error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
+ "pluggable device type");
return NULL;
}
return 0;
}
- qdev_get_device_class(&driver, &local_err);
- if (local_err) {
- goto error;
+ if (!object_class_by_name(driver)) {
+ const char *typename = find_typename_by_alias(driver);
+
+ if (typename) {
+ driver = typename;
+ }
}
prop_list = qmp_device_list_properties(driver, &local_err);
return 1;
error:
- error_printf("%s\n", error_get_pretty(local_err));
- error_free(local_err);
+ error_report_err(local_err);
return 1;
}
return dev;
}
-static void qbus_list_bus(DeviceState *dev)
+static void qbus_list_bus(DeviceState *dev, Error **errp)
{
BusState *child;
const char *sep = " ";
- error_printf("child busses at \"%s\":",
- dev->id ? dev->id : object_get_typename(OBJECT(dev)));
+ error_append_hint(errp, "child buses at \"%s\":",
+ dev->id ? dev->id : object_get_typename(OBJECT(dev)));
QLIST_FOREACH(child, &dev->child_bus, sibling) {
- error_printf("%s\"%s\"", sep, child->name);
+ error_append_hint(errp, "%s\"%s\"", sep, child->name);
sep = ", ";
}
- error_printf("\n");
+ error_append_hint(errp, "\n");
}
-static void qbus_list_dev(BusState *bus)
+static void qbus_list_dev(BusState *bus, Error **errp)
{
BusChild *kid;
const char *sep = " ";
- error_printf("devices at \"%s\":", bus->name);
+ error_append_hint(errp, "devices at \"%s\":", bus->name);
QTAILQ_FOREACH(kid, &bus->children, sibling) {
DeviceState *dev = kid->child;
- error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
- if (dev->id)
- error_printf("/\"%s\"", dev->id);
+ error_append_hint(errp, "%s\"%s\"", sep,
+ object_get_typename(OBJECT(dev)));
+ if (dev->id) {
+ error_append_hint(errp, "/\"%s\"", dev->id);
+ }
sep = ", ";
}
- error_printf("\n");
+ error_append_hint(errp, "\n");
}
static BusState *qbus_find_bus(DeviceState *dev, char *elem)
return NULL;
}
+static inline bool qbus_is_full(BusState *bus)
+{
+ BusClass *bus_class = BUS_GET_CLASS(bus);
+ return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
+}
+
+/*
+ * Search the tree rooted at @bus for a bus.
+ * If @name, search for a bus with that name. Note that bus names
+ * need not be unique. Yes, that's screwed up.
+ * Else search for a bus that is a subtype of @bus_typename.
+ * If more than one exists, prefer one that can take another device.
+ * Return the bus if found, else %NULL.
+ */
static BusState *qbus_find_recursive(BusState *bus, const char *name,
const char *bus_typename)
{
- BusClass *bus_class = BUS_GET_CLASS(bus);
BusChild *kid;
- BusState *child, *ret;
- int match = 1;
-
- if (name && (strcmp(bus->name, name) != 0)) {
- match = 0;
- } else if (bus_typename && !object_dynamic_cast(OBJECT(bus), bus_typename)) {
- match = 0;
- } else if ((bus_class->max_dev != 0) && (bus_class->max_dev <= bus->max_index)) {
- if (name != NULL) {
- /* bus was explicitly specified: return an error. */
- qerror_report(ERROR_CLASS_GENERIC_ERROR, "Bus '%s' is full",
- bus->name);
- return NULL;
- } else {
- /* bus was not specified: try to find another one. */
- match = 0;
- }
+ BusState *pick, *child, *ret;
+ bool match;
+
+ assert(name || bus_typename);
+ if (name) {
+ match = !strcmp(bus->name, name);
+ } else {
+ match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
}
- if (match) {
- return bus;
+
+ if (match && !qbus_is_full(bus)) {
+ return bus; /* root matches and isn't full */
}
+ pick = match ? bus : NULL;
+
QTAILQ_FOREACH(kid, &bus->children, sibling) {
DeviceState *dev = kid->child;
QLIST_FOREACH(child, &dev->child_bus, sibling) {
ret = qbus_find_recursive(child, name, bus_typename);
- if (ret) {
- return ret;
+ if (ret && !qbus_is_full(ret)) {
+ return ret; /* a descendant matches and isn't full */
+ }
+ if (ret && !pick) {
+ pick = ret;
}
}
}
- return NULL;
+
+ /* root or a descendant matches, but is full */
+ return pick;
}
-static BusState *qbus_find(const char *path)
+static BusState *qbus_find(const char *path, Error **errp)
{
DeviceState *dev;
BusState *bus;
}
bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
if (!bus) {
- qerror_report(QERR_BUS_NOT_FOUND, elem);
+ error_setg(errp, "Bus '%s' not found", elem);
return NULL;
}
pos = len;
pos++;
}
if (path[pos] == '\0') {
- return bus;
+ break;
}
/* find device */
pos += len;
dev = qbus_find_dev(bus, elem);
if (!dev) {
- qerror_report(QERR_DEVICE_NOT_FOUND, elem);
- if (!monitor_cur_is_qmp()) {
- qbus_list_dev(bus);
- }
+ error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
+ "Device '%s' not found", elem);
+ qbus_list_dev(bus, errp);
return NULL;
}
if (path[pos] == '\0') {
/* last specified element is a device. If it has exactly
* one child bus accept it nevertheless */
- switch (dev->num_child_bus) {
- case 0:
- qerror_report(ERROR_CLASS_GENERIC_ERROR,
- "Device '%s' has no child bus", elem);
- return NULL;
- case 1:
- return QLIST_FIRST(&dev->child_bus);
- default:
- qerror_report(ERROR_CLASS_GENERIC_ERROR,
- "Device '%s' has multiple child busses", elem);
- if (!monitor_cur_is_qmp()) {
- qbus_list_bus(dev);
- }
- return NULL;
+ if (dev->num_child_bus == 1) {
+ bus = QLIST_FIRST(&dev->child_bus);
+ break;
}
+ if (dev->num_child_bus) {
+ error_setg(errp, "Device '%s' has multiple child buses",
+ elem);
+ qbus_list_bus(dev, errp);
+ } else {
+ error_setg(errp, "Device '%s' has no child bus", elem);
+ }
+ return NULL;
}
/* find bus */
pos += len;
bus = qbus_find_bus(dev, elem);
if (!bus) {
- qerror_report(QERR_BUS_NOT_FOUND, elem);
- if (!monitor_cur_is_qmp()) {
- qbus_list_bus(dev);
- }
+ error_setg(errp, "Bus '%s' not found", elem);
+ qbus_list_bus(dev, errp);
return NULL;
}
}
+
+ if (qbus_is_full(bus)) {
+ error_setg(errp, "Bus '%s' is full", path);
+ return NULL;
+ }
+ return bus;
}
-DeviceState *qdev_device_add(QemuOpts *opts)
+DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
{
DeviceClass *dc;
const char *driver, *path, *id;
driver = qemu_opt_get(opts, "driver");
if (!driver) {
- qerror_report(QERR_MISSING_PARAMETER, "driver");
+ error_setg(errp, QERR_MISSING_PARAMETER, "driver");
return NULL;
}
/* find driver */
- dc = qdev_get_device_class(&driver, &err);
- if (err) {
- qerror_report_err(err);
- error_free(err);
+ dc = qdev_get_device_class(&driver, errp);
+ if (!dc) {
return NULL;
}
/* find bus */
path = qemu_opt_get(opts, "bus");
if (path != NULL) {
- bus = qbus_find(path);
+ bus = qbus_find(path, errp);
if (!bus) {
return NULL;
}
if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
- qerror_report(ERROR_CLASS_GENERIC_ERROR,
- "Device '%s' can't go on a %s bus",
- driver, object_get_typename(OBJECT(bus)));
+ error_setg(errp, "Device '%s' can't go on %s bus",
+ driver, object_get_typename(OBJECT(bus)));
return NULL;
}
} else if (dc->bus_type != NULL) {
bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
- if (!bus) {
- qerror_report(ERROR_CLASS_GENERIC_ERROR,
- "No '%s' bus found for device '%s'",
- dc->bus_type, driver);
+ if (!bus || qbus_is_full(bus)) {
+ error_setg(errp, "No '%s' bus found for device '%s'",
+ dc->bus_type, driver);
return NULL;
}
}
if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
- qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
+ error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
return NULL;
}
}
/* set properties */
- if (qemu_opt_foreach(opts, set_property, dev, NULL)) {
+ if (qemu_opt_foreach(opts, set_property, dev, &err)) {
+ error_propagate(errp, err);
object_unparent(OBJECT(dev));
object_unref(OBJECT(dev));
return NULL;
dev->opts = opts;
object_property_set_bool(OBJECT(dev), true, "realized", &err);
if (err != NULL) {
- qerror_report_err(err);
- error_free(err);
+ error_propagate(errp, err);
dev->opts = NULL;
object_unparent(OBJECT(dev));
object_unref(OBJECT(dev));
- qerror_report(QERR_DEVICE_INIT_FAILED, driver);
return NULL;
}
return dev;
print_qom_composition(mon, obj, 0);
}
-int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
+void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
{
Error *local_err = NULL;
QemuOpts *opts;
opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
if (local_err) {
- qerror_report_err(local_err);
- error_free(local_err);
- return -1;
+ error_propagate(errp, local_err);
+ return;
}
if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
qemu_opts_del(opts);
- return 0;
+ return;
}
- dev = qdev_device_add(opts);
+ dev = qdev_device_add(opts, &local_err);
if (!dev) {
+ error_propagate(errp, local_err);
qemu_opts_del(opts);
- return -1;
+ return;
}
object_unref(OBJECT(dev));
- return 0;
}
void qmp_device_del(const char *id, Error **errp)
{
Object *obj;
- char *root_path = object_get_canonical_path(qdev_get_peripheral());
- char *path = g_strdup_printf("%s/%s", root_path, id);
- g_free(root_path);
- obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
- g_free(path);
+ if (id[0] == '/') {
+ obj = object_resolve_path(id, NULL);
+ } else {
+ char *root_path = object_get_canonical_path(qdev_get_peripheral());
+ char *path = g_strdup_printf("%s/%s", root_path, id);
+
+ g_free(root_path);
+ obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
+ g_free(path);
+ }
if (!obj) {
- error_set(errp, QERR_DEVICE_NOT_FOUND, id);
+ error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
+ "Device '%s' not found", id);
+ return;
+ }
+
+ if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
+ error_setg(errp, "%s is not a hotpluggable device", id);
return;
}
return 0;
}
- opts = qemu_opts_parse(&qemu_global_opts, str, false);
+ opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
if (!opts) {
return -1;
}