]> git.proxmox.com Git - mirror_qemu.git/blob - qdev-monitor.c
e5136d78e0c1d9a7138efb2bdddf9069bd3f3fd4
[mirror_qemu.git] / qdev-monitor.c
1 /*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "hw/qdev.h"
22 #include "hw/sysbus.h"
23 #include "monitor/monitor.h"
24 #include "monitor/qdev.h"
25 #include "qmp-commands.h"
26 #include "sysemu/arch_init.h"
27 #include "qapi/qmp/qerror.h"
28 #include "qemu/config-file.h"
29 #include "qemu/error-report.h"
30
31 /*
32 * Aliases were a bad idea from the start. Let's keep them
33 * from spreading further.
34 */
35 typedef struct QDevAlias
36 {
37 const char *typename;
38 const char *alias;
39 uint32_t arch_mask;
40 } QDevAlias;
41
42 static const QDevAlias qdev_alias_table[] = {
43 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
44 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
45 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
46 { "virtio-balloon-pci", "virtio-balloon",
47 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
48 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
49 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
50 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
51 { "lsi53c895a", "lsi" },
52 { "ich9-ahci", "ahci" },
53 { "kvm-pci-assign", "pci-assign" },
54 { "e1000", "e1000-82540em" },
55 { }
56 };
57
58 static const char *qdev_class_get_alias(DeviceClass *dc)
59 {
60 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
61 int i;
62
63 for (i = 0; qdev_alias_table[i].typename; i++) {
64 if (qdev_alias_table[i].arch_mask &&
65 !(qdev_alias_table[i].arch_mask & arch_type)) {
66 continue;
67 }
68
69 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
70 return qdev_alias_table[i].alias;
71 }
72 }
73
74 return NULL;
75 }
76
77 static bool qdev_class_has_alias(DeviceClass *dc)
78 {
79 return (qdev_class_get_alias(dc) != NULL);
80 }
81
82 static void qdev_print_devinfo(DeviceClass *dc)
83 {
84 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
85 if (dc->bus_type) {
86 error_printf(", bus %s", dc->bus_type);
87 }
88 if (qdev_class_has_alias(dc)) {
89 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
90 }
91 if (dc->desc) {
92 error_printf(", desc \"%s\"", dc->desc);
93 }
94 if (dc->cannot_instantiate_with_device_add_yet) {
95 error_printf(", no-user");
96 }
97 error_printf("\n");
98 }
99
100 static gint devinfo_cmp(gconstpointer a, gconstpointer b)
101 {
102 return strcasecmp(object_class_get_name((ObjectClass *)a),
103 object_class_get_name((ObjectClass *)b));
104 }
105
106 static void qdev_print_devinfos(bool show_no_user)
107 {
108 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
109 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
110 [DEVICE_CATEGORY_USB] = "USB",
111 [DEVICE_CATEGORY_STORAGE] = "Storage",
112 [DEVICE_CATEGORY_NETWORK] = "Network",
113 [DEVICE_CATEGORY_INPUT] = "Input",
114 [DEVICE_CATEGORY_DISPLAY] = "Display",
115 [DEVICE_CATEGORY_SOUND] = "Sound",
116 [DEVICE_CATEGORY_MISC] = "Misc",
117 [DEVICE_CATEGORY_MAX] = "Uncategorized",
118 };
119 GSList *list, *elt;
120 int i;
121 bool cat_printed;
122
123 list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false),
124 devinfo_cmp);
125
126 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
127 cat_printed = false;
128 for (elt = list; elt; elt = elt->next) {
129 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
130 TYPE_DEVICE);
131 if ((i < DEVICE_CATEGORY_MAX
132 ? !test_bit(i, dc->categories)
133 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
134 || (!show_no_user
135 && dc->cannot_instantiate_with_device_add_yet)) {
136 continue;
137 }
138 if (!cat_printed) {
139 error_printf("%s%s devices:\n", i ? "\n" : "",
140 cat_name[i]);
141 cat_printed = true;
142 }
143 qdev_print_devinfo(dc);
144 }
145 }
146
147 g_slist_free(list);
148 }
149
150 static int set_property(void *opaque, const char *name, const char *value,
151 Error **errp)
152 {
153 Object *obj = opaque;
154 Error *err = NULL;
155
156 if (strcmp(name, "driver") == 0)
157 return 0;
158 if (strcmp(name, "bus") == 0)
159 return 0;
160
161 object_property_parse(obj, value, name, &err);
162 if (err != NULL) {
163 error_propagate(errp, err);
164 return -1;
165 }
166 return 0;
167 }
168
169 static const char *find_typename_by_alias(const char *alias)
170 {
171 int i;
172
173 for (i = 0; qdev_alias_table[i].alias; i++) {
174 if (qdev_alias_table[i].arch_mask &&
175 !(qdev_alias_table[i].arch_mask & arch_type)) {
176 continue;
177 }
178
179 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
180 return qdev_alias_table[i].typename;
181 }
182 }
183
184 return NULL;
185 }
186
187 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
188 {
189 ObjectClass *oc;
190 DeviceClass *dc;
191 const char *original_name = *driver;
192
193 oc = object_class_by_name(*driver);
194 if (!oc) {
195 const char *typename = find_typename_by_alias(*driver);
196
197 if (typename) {
198 *driver = typename;
199 oc = object_class_by_name(*driver);
200 }
201 }
202
203 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
204 if (*driver != original_name) {
205 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
206 " name", original_name, *driver);
207 } else {
208 error_setg(errp, "'%s' is not a valid device model name", *driver);
209 }
210 return NULL;
211 }
212
213 if (object_class_is_abstract(oc)) {
214 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
215 "non-abstract device type");
216 return NULL;
217 }
218
219 dc = DEVICE_CLASS(oc);
220 if (dc->cannot_instantiate_with_device_add_yet ||
221 (qdev_hotplug && !dc->hotpluggable)) {
222 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
223 "pluggable device type");
224 return NULL;
225 }
226
227 return dc;
228 }
229
230
231 int qdev_device_help(QemuOpts *opts)
232 {
233 Error *local_err = NULL;
234 const char *driver;
235 DevicePropertyInfoList *prop_list;
236 DevicePropertyInfoList *prop;
237
238 driver = qemu_opt_get(opts, "driver");
239 if (driver && is_help_option(driver)) {
240 qdev_print_devinfos(false);
241 return 1;
242 }
243
244 if (!driver || !qemu_opt_has_help_opt(opts)) {
245 return 0;
246 }
247
248 if (!object_class_by_name(driver)) {
249 const char *typename = find_typename_by_alias(driver);
250
251 if (typename) {
252 driver = typename;
253 }
254 }
255
256 prop_list = qmp_device_list_properties(driver, &local_err);
257 if (local_err) {
258 goto error;
259 }
260
261 for (prop = prop_list; prop; prop = prop->next) {
262 error_printf("%s.%s=%s", driver,
263 prop->value->name,
264 prop->value->type);
265 if (prop->value->has_description) {
266 error_printf(" (%s)\n", prop->value->description);
267 } else {
268 error_printf("\n");
269 }
270 }
271
272 qapi_free_DevicePropertyInfoList(prop_list);
273 return 1;
274
275 error:
276 error_report_err(local_err);
277 return 1;
278 }
279
280 static Object *qdev_get_peripheral(void)
281 {
282 static Object *dev;
283
284 if (dev == NULL) {
285 dev = container_get(qdev_get_machine(), "/peripheral");
286 }
287
288 return dev;
289 }
290
291 static Object *qdev_get_peripheral_anon(void)
292 {
293 static Object *dev;
294
295 if (dev == NULL) {
296 dev = container_get(qdev_get_machine(), "/peripheral-anon");
297 }
298
299 return dev;
300 }
301
302 static void qbus_list_bus(DeviceState *dev, Error **errp)
303 {
304 BusState *child;
305 const char *sep = " ";
306
307 error_append_hint(errp, "child buses at \"%s\":",
308 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
309 QLIST_FOREACH(child, &dev->child_bus, sibling) {
310 error_append_hint(errp, "%s\"%s\"", sep, child->name);
311 sep = ", ";
312 }
313 error_append_hint(errp, "\n");
314 }
315
316 static void qbus_list_dev(BusState *bus, Error **errp)
317 {
318 BusChild *kid;
319 const char *sep = " ";
320
321 error_append_hint(errp, "devices at \"%s\":", bus->name);
322 QTAILQ_FOREACH(kid, &bus->children, sibling) {
323 DeviceState *dev = kid->child;
324 error_append_hint(errp, "%s\"%s\"", sep,
325 object_get_typename(OBJECT(dev)));
326 if (dev->id) {
327 error_append_hint(errp, "/\"%s\"", dev->id);
328 }
329 sep = ", ";
330 }
331 error_append_hint(errp, "\n");
332 }
333
334 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
335 {
336 BusState *child;
337
338 QLIST_FOREACH(child, &dev->child_bus, sibling) {
339 if (strcmp(child->name, elem) == 0) {
340 return child;
341 }
342 }
343 return NULL;
344 }
345
346 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
347 {
348 BusChild *kid;
349
350 /*
351 * try to match in order:
352 * (1) instance id, if present
353 * (2) driver name
354 * (3) driver alias, if present
355 */
356 QTAILQ_FOREACH(kid, &bus->children, sibling) {
357 DeviceState *dev = kid->child;
358 if (dev->id && strcmp(dev->id, elem) == 0) {
359 return dev;
360 }
361 }
362 QTAILQ_FOREACH(kid, &bus->children, sibling) {
363 DeviceState *dev = kid->child;
364 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
365 return dev;
366 }
367 }
368 QTAILQ_FOREACH(kid, &bus->children, sibling) {
369 DeviceState *dev = kid->child;
370 DeviceClass *dc = DEVICE_GET_CLASS(dev);
371
372 if (qdev_class_has_alias(dc) &&
373 strcmp(qdev_class_get_alias(dc), elem) == 0) {
374 return dev;
375 }
376 }
377 return NULL;
378 }
379
380 static inline bool qbus_is_full(BusState *bus)
381 {
382 BusClass *bus_class = BUS_GET_CLASS(bus);
383 return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
384 }
385
386 /*
387 * Search the tree rooted at @bus for a bus.
388 * If @name, search for a bus with that name. Note that bus names
389 * need not be unique. Yes, that's screwed up.
390 * Else search for a bus that is a subtype of @bus_typename.
391 * If more than one exists, prefer one that can take another device.
392 * Return the bus if found, else %NULL.
393 */
394 static BusState *qbus_find_recursive(BusState *bus, const char *name,
395 const char *bus_typename)
396 {
397 BusChild *kid;
398 BusState *pick, *child, *ret;
399 bool match;
400
401 assert(name || bus_typename);
402 if (name) {
403 match = !strcmp(bus->name, name);
404 } else {
405 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
406 }
407
408 if (match && !qbus_is_full(bus)) {
409 return bus; /* root matches and isn't full */
410 }
411
412 pick = match ? bus : NULL;
413
414 QTAILQ_FOREACH(kid, &bus->children, sibling) {
415 DeviceState *dev = kid->child;
416 QLIST_FOREACH(child, &dev->child_bus, sibling) {
417 ret = qbus_find_recursive(child, name, bus_typename);
418 if (ret && !qbus_is_full(ret)) {
419 return ret; /* a descendant matches and isn't full */
420 }
421 if (ret && !pick) {
422 pick = ret;
423 }
424 }
425 }
426
427 /* root or a descendant matches, but is full */
428 return pick;
429 }
430
431 static BusState *qbus_find(const char *path, Error **errp)
432 {
433 DeviceState *dev;
434 BusState *bus;
435 char elem[128];
436 int pos, len;
437
438 /* find start element */
439 if (path[0] == '/') {
440 bus = sysbus_get_default();
441 pos = 0;
442 } else {
443 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
444 assert(!path[0]);
445 elem[0] = len = 0;
446 }
447 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
448 if (!bus) {
449 error_setg(errp, "Bus '%s' not found", elem);
450 return NULL;
451 }
452 pos = len;
453 }
454
455 for (;;) {
456 assert(path[pos] == '/' || !path[pos]);
457 while (path[pos] == '/') {
458 pos++;
459 }
460 if (path[pos] == '\0') {
461 break;
462 }
463
464 /* find device */
465 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
466 g_assert_not_reached();
467 elem[0] = len = 0;
468 }
469 pos += len;
470 dev = qbus_find_dev(bus, elem);
471 if (!dev) {
472 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
473 "Device '%s' not found", elem);
474 qbus_list_dev(bus, errp);
475 return NULL;
476 }
477
478 assert(path[pos] == '/' || !path[pos]);
479 while (path[pos] == '/') {
480 pos++;
481 }
482 if (path[pos] == '\0') {
483 /* last specified element is a device. If it has exactly
484 * one child bus accept it nevertheless */
485 if (dev->num_child_bus == 1) {
486 bus = QLIST_FIRST(&dev->child_bus);
487 break;
488 }
489 if (dev->num_child_bus) {
490 error_setg(errp, "Device '%s' has multiple child buses",
491 elem);
492 qbus_list_bus(dev, errp);
493 } else {
494 error_setg(errp, "Device '%s' has no child bus", elem);
495 }
496 return NULL;
497 }
498
499 /* find bus */
500 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
501 g_assert_not_reached();
502 elem[0] = len = 0;
503 }
504 pos += len;
505 bus = qbus_find_bus(dev, elem);
506 if (!bus) {
507 error_setg(errp, "Bus '%s' not found", elem);
508 qbus_list_bus(dev, errp);
509 return NULL;
510 }
511 }
512
513 if (qbus_is_full(bus)) {
514 error_setg(errp, "Bus '%s' is full", path);
515 return NULL;
516 }
517 return bus;
518 }
519
520 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
521 {
522 DeviceClass *dc;
523 const char *driver, *path, *id;
524 DeviceState *dev;
525 BusState *bus = NULL;
526 Error *err = NULL;
527
528 driver = qemu_opt_get(opts, "driver");
529 if (!driver) {
530 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
531 return NULL;
532 }
533
534 /* find driver */
535 dc = qdev_get_device_class(&driver, errp);
536 if (!dc) {
537 return NULL;
538 }
539
540 /* find bus */
541 path = qemu_opt_get(opts, "bus");
542 if (path != NULL) {
543 bus = qbus_find(path, errp);
544 if (!bus) {
545 return NULL;
546 }
547 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
548 error_setg(errp, "Device '%s' can't go on %s bus",
549 driver, object_get_typename(OBJECT(bus)));
550 return NULL;
551 }
552 } else if (dc->bus_type != NULL) {
553 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
554 if (!bus || qbus_is_full(bus)) {
555 error_setg(errp, "No '%s' bus found for device '%s'",
556 dc->bus_type, driver);
557 return NULL;
558 }
559 }
560 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
561 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
562 return NULL;
563 }
564
565 /* create device */
566 dev = DEVICE(object_new(driver));
567
568 if (bus) {
569 qdev_set_parent_bus(dev, bus);
570 }
571
572 id = qemu_opts_id(opts);
573 if (id) {
574 dev->id = id;
575 }
576
577 if (dev->id) {
578 object_property_add_child(qdev_get_peripheral(), dev->id,
579 OBJECT(dev), NULL);
580 } else {
581 static int anon_count;
582 gchar *name = g_strdup_printf("device[%d]", anon_count++);
583 object_property_add_child(qdev_get_peripheral_anon(), name,
584 OBJECT(dev), NULL);
585 g_free(name);
586 }
587
588 /* set properties */
589 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
590 error_propagate(errp, err);
591 object_unparent(OBJECT(dev));
592 object_unref(OBJECT(dev));
593 return NULL;
594 }
595
596 dev->opts = opts;
597 object_property_set_bool(OBJECT(dev), true, "realized", &err);
598 if (err != NULL) {
599 error_propagate(errp, err);
600 dev->opts = NULL;
601 object_unparent(OBJECT(dev));
602 object_unref(OBJECT(dev));
603 return NULL;
604 }
605 return dev;
606 }
607
608
609 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
610 static void qbus_print(Monitor *mon, BusState *bus, int indent);
611
612 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
613 int indent)
614 {
615 if (!props)
616 return;
617 for (; props->name; props++) {
618 Error *err = NULL;
619 char *value;
620 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
621 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
622 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
623 } else {
624 value = object_property_print(OBJECT(dev), props->name, true, &err);
625 }
626 g_free(legacy_name);
627
628 if (err) {
629 error_free(err);
630 continue;
631 }
632 qdev_printf("%s = %s\n", props->name,
633 value && *value ? value : "<null>");
634 g_free(value);
635 }
636 }
637
638 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
639 {
640 BusClass *bc = BUS_GET_CLASS(bus);
641
642 if (bc->print_dev) {
643 bc->print_dev(mon, dev, indent);
644 }
645 }
646
647 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
648 {
649 ObjectClass *class;
650 BusState *child;
651 NamedGPIOList *ngl;
652
653 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
654 dev->id ? dev->id : "");
655 indent += 2;
656 QLIST_FOREACH(ngl, &dev->gpios, node) {
657 if (ngl->num_in) {
658 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
659 ngl->num_in);
660 }
661 if (ngl->num_out) {
662 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
663 ngl->num_out);
664 }
665 }
666 class = object_get_class(OBJECT(dev));
667 do {
668 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
669 class = object_class_get_parent(class);
670 } while (class != object_class_by_name(TYPE_DEVICE));
671 bus_print_dev(dev->parent_bus, mon, dev, indent);
672 QLIST_FOREACH(child, &dev->child_bus, sibling) {
673 qbus_print(mon, child, indent);
674 }
675 }
676
677 static void qbus_print(Monitor *mon, BusState *bus, int indent)
678 {
679 BusChild *kid;
680
681 qdev_printf("bus: %s\n", bus->name);
682 indent += 2;
683 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
684 QTAILQ_FOREACH(kid, &bus->children, sibling) {
685 DeviceState *dev = kid->child;
686 qdev_print(mon, dev, indent);
687 }
688 }
689 #undef qdev_printf
690
691 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
692 {
693 if (sysbus_get_default())
694 qbus_print(mon, sysbus_get_default(), 0);
695 }
696
697 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
698 {
699 qdev_print_devinfos(true);
700 }
701
702 typedef struct QOMCompositionState {
703 Monitor *mon;
704 int indent;
705 } QOMCompositionState;
706
707 static void print_qom_composition(Monitor *mon, Object *obj, int indent);
708
709 static int print_qom_composition_child(Object *obj, void *opaque)
710 {
711 QOMCompositionState *s = opaque;
712
713 print_qom_composition(s->mon, obj, s->indent);
714
715 return 0;
716 }
717
718 static void print_qom_composition(Monitor *mon, Object *obj, int indent)
719 {
720 QOMCompositionState s = {
721 .mon = mon,
722 .indent = indent + 2,
723 };
724 char *name;
725
726 if (obj == object_get_root()) {
727 name = g_strdup("");
728 } else {
729 name = object_get_canonical_path_component(obj);
730 }
731 monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
732 object_get_typename(obj));
733 g_free(name);
734 object_child_foreach(obj, print_qom_composition_child, &s);
735 }
736
737 void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
738 {
739 const char *path = qdict_get_try_str(dict, "path");
740 Object *obj;
741 bool ambiguous = false;
742
743 if (path) {
744 obj = object_resolve_path(path, &ambiguous);
745 if (!obj) {
746 monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
747 return;
748 }
749 if (ambiguous) {
750 monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
751 return;
752 }
753 } else {
754 obj = qdev_get_machine();
755 }
756 print_qom_composition(mon, obj, 0);
757 }
758
759 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
760 {
761 Error *local_err = NULL;
762 QemuOpts *opts;
763 DeviceState *dev;
764
765 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
766 if (local_err) {
767 error_propagate(errp, local_err);
768 return;
769 }
770 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
771 qemu_opts_del(opts);
772 return;
773 }
774 dev = qdev_device_add(opts, &local_err);
775 if (!dev) {
776 error_propagate(errp, local_err);
777 qemu_opts_del(opts);
778 return;
779 }
780 object_unref(OBJECT(dev));
781 }
782
783 void qmp_device_del(const char *id, Error **errp)
784 {
785 Object *obj;
786
787 if (id[0] == '/') {
788 obj = object_resolve_path(id, NULL);
789 } else {
790 char *root_path = object_get_canonical_path(qdev_get_peripheral());
791 char *path = g_strdup_printf("%s/%s", root_path, id);
792
793 g_free(root_path);
794 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
795 g_free(path);
796 }
797
798 if (!obj) {
799 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
800 "Device '%s' not found", id);
801 return;
802 }
803
804 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
805 error_setg(errp, "%s is not a hotpluggable device", id);
806 return;
807 }
808
809 qdev_unplug(DEVICE(obj), errp);
810 }
811
812 void qdev_machine_init(void)
813 {
814 qdev_get_peripheral_anon();
815 qdev_get_peripheral();
816 }
817
818 QemuOptsList qemu_device_opts = {
819 .name = "device",
820 .implied_opt_name = "driver",
821 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
822 .desc = {
823 /*
824 * no elements => accept any
825 * sanity checking will happen later
826 * when setting device properties
827 */
828 { /* end of list */ }
829 },
830 };
831
832 QemuOptsList qemu_global_opts = {
833 .name = "global",
834 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
835 .desc = {
836 {
837 .name = "driver",
838 .type = QEMU_OPT_STRING,
839 },{
840 .name = "property",
841 .type = QEMU_OPT_STRING,
842 },{
843 .name = "value",
844 .type = QEMU_OPT_STRING,
845 },
846 { /* end of list */ }
847 },
848 };
849
850 int qemu_global_option(const char *str)
851 {
852 char driver[64], property[64];
853 QemuOpts *opts;
854 int rc, offset;
855
856 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
857 if (rc == 2 && str[offset] == '=') {
858 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
859 qemu_opt_set(opts, "driver", driver, &error_abort);
860 qemu_opt_set(opts, "property", property, &error_abort);
861 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
862 return 0;
863 }
864
865 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
866 if (!opts) {
867 return -1;
868 }
869
870 return 0;
871 }