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