]> Git Repo - qemu.git/blob - softmmu/qdev-monitor.c
Remove the deprecated moxie target
[qemu.git] / softmmu / 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.1 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/sysbus.h"
22 #include "monitor/hmp.h"
23 #include "monitor/monitor.h"
24 #include "monitor/qdev.h"
25 #include "sysemu/arch_init.h"
26 #include "qapi/error.h"
27 #include "qapi/qapi-commands-qdev.h"
28 #include "qapi/qmp/dispatch.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "qemu/help_option.h"
34 #include "qemu/option.h"
35 #include "qemu/qemu-print.h"
36 #include "qemu/option_int.h"
37 #include "sysemu/block-backend.h"
38 #include "migration/misc.h"
39 #include "migration/migration.h"
40 #include "qemu/cutils.h"
41 #include "hw/qdev-properties.h"
42 #include "hw/clock.h"
43
44 /*
45  * Aliases were a bad idea from the start.  Let's keep them
46  * from spreading further.
47  */
48 typedef struct QDevAlias
49 {
50     const char *typename;
51     const char *alias;
52     uint32_t arch_mask;
53 } QDevAlias;
54
55 /* Please keep this table sorted by typename. */
56 static const QDevAlias qdev_alias_table[] = {
57     { "AC97", "ac97" }, /* -soundhw name */
58     { "e1000", "e1000-82540em" },
59     { "ES1370", "es1370" }, /* -soundhw name */
60     { "ich9-ahci", "ahci" },
61     { "lsi53c895a", "lsi" },
62     { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO },
63     { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW },
64     { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI },
65     { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO },
66     { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW },
67     { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI },
68     { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO },
69     { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW },
70     { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI },
71     { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO },
72     { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW },
73     { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI },
74     { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO },
75     { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW },
76     { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI },
77     { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI },
78     { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO },
79     { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW },
80     { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI },
81     { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO },
82     { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW },
83     { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI },
84     { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO },
85     { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW },
86     { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI },
87     { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO },
88     { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW },
89     { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI },
90     { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO },
91     { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW },
92     { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI },
93     { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO },
94     { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW },
95     { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI},
96     { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO },
97     { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW },
98     { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI },
99     { }
100 };
101
102 static const char *qdev_class_get_alias(DeviceClass *dc)
103 {
104     const char *typename = object_class_get_name(OBJECT_CLASS(dc));
105     int i;
106
107     for (i = 0; qdev_alias_table[i].typename; i++) {
108         if (qdev_alias_table[i].arch_mask &&
109             !(qdev_alias_table[i].arch_mask & arch_type)) {
110             continue;
111         }
112
113         if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
114             return qdev_alias_table[i].alias;
115         }
116     }
117
118     return NULL;
119 }
120
121 static bool qdev_class_has_alias(DeviceClass *dc)
122 {
123     return (qdev_class_get_alias(dc) != NULL);
124 }
125
126 static void qdev_print_devinfo(DeviceClass *dc)
127 {
128     qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
129     if (dc->bus_type) {
130         qemu_printf(", bus %s", dc->bus_type);
131     }
132     if (qdev_class_has_alias(dc)) {
133         qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
134     }
135     if (dc->desc) {
136         qemu_printf(", desc \"%s\"", dc->desc);
137     }
138     if (!dc->user_creatable) {
139         qemu_printf(", no-user");
140     }
141     qemu_printf("\n");
142 }
143
144 static void qdev_print_devinfos(bool show_no_user)
145 {
146     static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
147         [DEVICE_CATEGORY_BRIDGE]  = "Controller/Bridge/Hub",
148         [DEVICE_CATEGORY_USB]     = "USB",
149         [DEVICE_CATEGORY_STORAGE] = "Storage",
150         [DEVICE_CATEGORY_NETWORK] = "Network",
151         [DEVICE_CATEGORY_INPUT]   = "Input",
152         [DEVICE_CATEGORY_DISPLAY] = "Display",
153         [DEVICE_CATEGORY_SOUND]   = "Sound",
154         [DEVICE_CATEGORY_MISC]    = "Misc",
155         [DEVICE_CATEGORY_CPU]     = "CPU",
156         [DEVICE_CATEGORY_MAX]     = "Uncategorized",
157     };
158     GSList *list, *elt;
159     int i;
160     bool cat_printed;
161
162     module_load_qom_all();
163     list = object_class_get_list_sorted(TYPE_DEVICE, false);
164
165     for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
166         cat_printed = false;
167         for (elt = list; elt; elt = elt->next) {
168             DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
169                                                  TYPE_DEVICE);
170             if ((i < DEVICE_CATEGORY_MAX
171                  ? !test_bit(i, dc->categories)
172                  : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
173                 || (!show_no_user
174                     && !dc->user_creatable)) {
175                 continue;
176             }
177             if (!cat_printed) {
178                 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
179                 cat_printed = true;
180             }
181             qdev_print_devinfo(dc);
182         }
183     }
184
185     g_slist_free(list);
186 }
187
188 static int set_property(void *opaque, const char *name, const char *value,
189                         Error **errp)
190 {
191     Object *obj = opaque;
192
193     if (strcmp(name, "driver") == 0)
194         return 0;
195     if (strcmp(name, "bus") == 0)
196         return 0;
197
198     if (!object_property_parse(obj, name, value, errp)) {
199         return -1;
200     }
201     return 0;
202 }
203
204 static const char *find_typename_by_alias(const char *alias)
205 {
206     int i;
207
208     for (i = 0; qdev_alias_table[i].alias; i++) {
209         if (qdev_alias_table[i].arch_mask &&
210             !(qdev_alias_table[i].arch_mask & arch_type)) {
211             continue;
212         }
213
214         if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
215             return qdev_alias_table[i].typename;
216         }
217     }
218
219     return NULL;
220 }
221
222 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
223 {
224     ObjectClass *oc;
225     DeviceClass *dc;
226     const char *original_name = *driver;
227
228     oc = module_object_class_by_name(*driver);
229     if (!oc) {
230         const char *typename = find_typename_by_alias(*driver);
231
232         if (typename) {
233             *driver = typename;
234             oc = module_object_class_by_name(*driver);
235         }
236     }
237
238     if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
239         if (*driver != original_name) {
240             error_setg(errp, "'%s' (alias '%s') is not a valid device model"
241                        " name", original_name, *driver);
242         } else {
243             error_setg(errp, "'%s' is not a valid device model name", *driver);
244         }
245         return NULL;
246     }
247
248     if (object_class_is_abstract(oc)) {
249         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
250                    "a non-abstract device type");
251         return NULL;
252     }
253
254     dc = DEVICE_CLASS(oc);
255     if (!dc->user_creatable ||
256         (phase_check(PHASE_MACHINE_READY) && !dc->hotpluggable)) {
257         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
258                    "a pluggable device type");
259         return NULL;
260     }
261
262     return dc;
263 }
264
265
266 int qdev_device_help(QemuOpts *opts)
267 {
268     Error *local_err = NULL;
269     const char *driver;
270     ObjectPropertyInfoList *prop_list;
271     ObjectPropertyInfoList *prop;
272     GPtrArray *array;
273     int i;
274
275     driver = qemu_opt_get(opts, "driver");
276     if (driver && is_help_option(driver)) {
277         qdev_print_devinfos(false);
278         return 1;
279     }
280
281     if (!driver || !qemu_opt_has_help_opt(opts)) {
282         return 0;
283     }
284
285     if (!object_class_by_name(driver)) {
286         const char *typename = find_typename_by_alias(driver);
287
288         if (typename) {
289             driver = typename;
290         }
291     }
292
293     prop_list = qmp_device_list_properties(driver, &local_err);
294     if (local_err) {
295         goto error;
296     }
297
298     if (prop_list) {
299         qemu_printf("%s options:\n", driver);
300     } else {
301         qemu_printf("There are no options for %s.\n", driver);
302     }
303     array = g_ptr_array_new();
304     for (prop = prop_list; prop; prop = prop->next) {
305         g_ptr_array_add(array,
306                         object_property_help(prop->value->name,
307                                              prop->value->type,
308                                              prop->value->default_value,
309                                              prop->value->description));
310     }
311     g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
312     for (i = 0; i < array->len; i++) {
313         qemu_printf("%s\n", (char *)array->pdata[i]);
314     }
315     g_ptr_array_set_free_func(array, g_free);
316     g_ptr_array_free(array, true);
317     qapi_free_ObjectPropertyInfoList(prop_list);
318     return 1;
319
320 error:
321     error_report_err(local_err);
322     return 1;
323 }
324
325 static Object *qdev_get_peripheral(void)
326 {
327     static Object *dev;
328
329     if (dev == NULL) {
330         dev = container_get(qdev_get_machine(), "/peripheral");
331     }
332
333     return dev;
334 }
335
336 static Object *qdev_get_peripheral_anon(void)
337 {
338     static Object *dev;
339
340     if (dev == NULL) {
341         dev = container_get(qdev_get_machine(), "/peripheral-anon");
342     }
343
344     return dev;
345 }
346
347 static void qbus_error_append_bus_list_hint(DeviceState *dev,
348                                             Error *const *errp)
349 {
350     BusState *child;
351     const char *sep = " ";
352
353     error_append_hint(errp, "child buses at \"%s\":",
354                       dev->id ? dev->id : object_get_typename(OBJECT(dev)));
355     QLIST_FOREACH(child, &dev->child_bus, sibling) {
356         error_append_hint(errp, "%s\"%s\"", sep, child->name);
357         sep = ", ";
358     }
359     error_append_hint(errp, "\n");
360 }
361
362 static void qbus_error_append_dev_list_hint(BusState *bus,
363                                             Error *const *errp)
364 {
365     BusChild *kid;
366     const char *sep = " ";
367
368     error_append_hint(errp, "devices at \"%s\":", bus->name);
369     QTAILQ_FOREACH(kid, &bus->children, sibling) {
370         DeviceState *dev = kid->child;
371         error_append_hint(errp, "%s\"%s\"", sep,
372                           object_get_typename(OBJECT(dev)));
373         if (dev->id) {
374             error_append_hint(errp, "/\"%s\"", dev->id);
375         }
376         sep = ", ";
377     }
378     error_append_hint(errp, "\n");
379 }
380
381 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
382 {
383     BusState *child;
384
385     QLIST_FOREACH(child, &dev->child_bus, sibling) {
386         if (strcmp(child->name, elem) == 0) {
387             return child;
388         }
389     }
390     return NULL;
391 }
392
393 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
394 {
395     BusChild *kid;
396
397     /*
398      * try to match in order:
399      *   (1) instance id, if present
400      *   (2) driver name
401      *   (3) driver alias, if present
402      */
403     QTAILQ_FOREACH(kid, &bus->children, sibling) {
404         DeviceState *dev = kid->child;
405         if (dev->id  &&  strcmp(dev->id, elem) == 0) {
406             return dev;
407         }
408     }
409     QTAILQ_FOREACH(kid, &bus->children, sibling) {
410         DeviceState *dev = kid->child;
411         if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
412             return dev;
413         }
414     }
415     QTAILQ_FOREACH(kid, &bus->children, sibling) {
416         DeviceState *dev = kid->child;
417         DeviceClass *dc = DEVICE_GET_CLASS(dev);
418
419         if (qdev_class_has_alias(dc) &&
420             strcmp(qdev_class_get_alias(dc), elem) == 0) {
421             return dev;
422         }
423     }
424     return NULL;
425 }
426
427 static inline bool qbus_is_full(BusState *bus)
428 {
429     BusClass *bus_class = BUS_GET_CLASS(bus);
430     return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
431 }
432
433 /*
434  * Search the tree rooted at @bus for a bus.
435  * If @name, search for a bus with that name.  Note that bus names
436  * need not be unique.  Yes, that's screwed up.
437  * Else search for a bus that is a subtype of @bus_typename.
438  * If more than one exists, prefer one that can take another device.
439  * Return the bus if found, else %NULL.
440  */
441 static BusState *qbus_find_recursive(BusState *bus, const char *name,
442                                      const char *bus_typename)
443 {
444     BusChild *kid;
445     BusState *pick, *child, *ret;
446     bool match;
447
448     assert(name || bus_typename);
449     if (name) {
450         match = !strcmp(bus->name, name);
451     } else {
452         match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
453     }
454
455     if (match && !qbus_is_full(bus)) {
456         return bus;             /* root matches and isn't full */
457     }
458
459     pick = match ? bus : NULL;
460
461     QTAILQ_FOREACH(kid, &bus->children, sibling) {
462         DeviceState *dev = kid->child;
463         QLIST_FOREACH(child, &dev->child_bus, sibling) {
464             ret = qbus_find_recursive(child, name, bus_typename);
465             if (ret && !qbus_is_full(ret)) {
466                 return ret;     /* a descendant matches and isn't full */
467             }
468             if (ret && !pick) {
469                 pick = ret;
470             }
471         }
472     }
473
474     /* root or a descendant matches, but is full */
475     return pick;
476 }
477
478 static BusState *qbus_find(const char *path, Error **errp)
479 {
480     DeviceState *dev;
481     BusState *bus;
482     char elem[128];
483     int pos, len;
484
485     /* find start element */
486     if (path[0] == '/') {
487         bus = sysbus_get_default();
488         pos = 0;
489     } else {
490         if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
491             assert(!path[0]);
492             elem[0] = len = 0;
493         }
494         bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
495         if (!bus) {
496             error_setg(errp, "Bus '%s' not found", elem);
497             return NULL;
498         }
499         pos = len;
500     }
501
502     for (;;) {
503         assert(path[pos] == '/' || !path[pos]);
504         while (path[pos] == '/') {
505             pos++;
506         }
507         if (path[pos] == '\0') {
508             break;
509         }
510
511         /* find device */
512         if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
513             g_assert_not_reached();
514             elem[0] = len = 0;
515         }
516         pos += len;
517         dev = qbus_find_dev(bus, elem);
518         if (!dev) {
519             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
520                       "Device '%s' not found", elem);
521             qbus_error_append_dev_list_hint(bus, errp);
522             return NULL;
523         }
524
525         assert(path[pos] == '/' || !path[pos]);
526         while (path[pos] == '/') {
527             pos++;
528         }
529         if (path[pos] == '\0') {
530             /* last specified element is a device.  If it has exactly
531              * one child bus accept it nevertheless */
532             if (dev->num_child_bus == 1) {
533                 bus = QLIST_FIRST(&dev->child_bus);
534                 break;
535             }
536             if (dev->num_child_bus) {
537                 error_setg(errp, "Device '%s' has multiple child buses",
538                            elem);
539                 qbus_error_append_bus_list_hint(dev, errp);
540             } else {
541                 error_setg(errp, "Device '%s' has no child bus", elem);
542             }
543             return NULL;
544         }
545
546         /* find bus */
547         if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
548             g_assert_not_reached();
549             elem[0] = len = 0;
550         }
551         pos += len;
552         bus = qbus_find_bus(dev, elem);
553         if (!bus) {
554             error_setg(errp, "Bus '%s' not found", elem);
555             qbus_error_append_bus_list_hint(dev, errp);
556             return NULL;
557         }
558     }
559
560     if (qbus_is_full(bus)) {
561         error_setg(errp, "Bus '%s' is full", path);
562         return NULL;
563     }
564     return bus;
565 }
566
567 void qdev_set_id(DeviceState *dev, const char *id)
568 {
569     if (id) {
570         dev->id = id;
571     }
572
573     if (dev->id) {
574         object_property_add_child(qdev_get_peripheral(), dev->id,
575                                   OBJECT(dev));
576     } else {
577         static int anon_count;
578         gchar *name = g_strdup_printf("device[%d]", anon_count++);
579         object_property_add_child(qdev_get_peripheral_anon(), name,
580                                   OBJECT(dev));
581         g_free(name);
582     }
583 }
584
585 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
586 {
587     DeviceClass *dc;
588     const char *driver, *path;
589     DeviceState *dev = NULL;
590     BusState *bus = NULL;
591
592     driver = qemu_opt_get(opts, "driver");
593     if (!driver) {
594         error_setg(errp, QERR_MISSING_PARAMETER, "driver");
595         return NULL;
596     }
597
598     /* find driver */
599     dc = qdev_get_device_class(&driver, errp);
600     if (!dc) {
601         return NULL;
602     }
603
604     /* find bus */
605     path = qemu_opt_get(opts, "bus");
606     if (path != NULL) {
607         bus = qbus_find(path, errp);
608         if (!bus) {
609             return NULL;
610         }
611         if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
612             error_setg(errp, "Device '%s' can't go on %s bus",
613                        driver, object_get_typename(OBJECT(bus)));
614             return NULL;
615         }
616     } else if (dc->bus_type != NULL) {
617         bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
618         if (!bus || qbus_is_full(bus)) {
619             error_setg(errp, "No '%s' bus found for device '%s'",
620                        dc->bus_type, driver);
621             return NULL;
622         }
623     }
624
625     if (qemu_opt_get(opts, "failover_pair_id")) {
626         if (!opts->id) {
627             error_setg(errp, "Device with failover_pair_id don't have id");
628             return NULL;
629         }
630         if (qdev_should_hide_device(opts)) {
631             if (bus && !qbus_is_hotpluggable(bus)) {
632                 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
633             }
634             return NULL;
635         }
636     }
637
638     if (phase_check(PHASE_MACHINE_READY) && bus && !qbus_is_hotpluggable(bus)) {
639         error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
640         return NULL;
641     }
642
643     if (!migration_is_idle()) {
644         error_setg(errp, "device_add not allowed while migrating");
645         return NULL;
646     }
647
648     /* create device */
649     dev = qdev_new(driver);
650
651     /* Check whether the hotplug is allowed by the machine */
652     if (phase_check(PHASE_MACHINE_READY)) {
653         if (!qdev_hotplug_allowed(dev, errp)) {
654             goto err_del_dev;
655         }
656
657         if (!bus && !qdev_get_machine_hotplug_handler(dev)) {
658             /* No bus, no machine hotplug handler --> device is not hotpluggable */
659             error_setg(errp, "Device '%s' can not be hotplugged on this machine",
660                        driver);
661             goto err_del_dev;
662         }
663     }
664
665     qdev_set_id(dev, qemu_opts_id(opts));
666
667     /* set properties */
668     if (qemu_opt_foreach(opts, set_property, dev, errp)) {
669         goto err_del_dev;
670     }
671
672     dev->opts = opts;
673     if (!qdev_realize(DEVICE(dev), bus, errp)) {
674         dev->opts = NULL;
675         goto err_del_dev;
676     }
677     return dev;
678
679 err_del_dev:
680     if (dev) {
681         object_unparent(OBJECT(dev));
682         object_unref(OBJECT(dev));
683     }
684     return NULL;
685 }
686
687
688 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
689 static void qbus_print(Monitor *mon, BusState *bus, int indent);
690
691 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
692                              int indent)
693 {
694     if (!props)
695         return;
696     for (; props->name; props++) {
697         char *value;
698         char *legacy_name = g_strdup_printf("legacy-%s", props->name);
699
700         if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
701             value = object_property_get_str(OBJECT(dev), legacy_name, NULL);
702         } else {
703             value = object_property_print(OBJECT(dev), props->name, true,
704                                           NULL);
705         }
706         g_free(legacy_name);
707
708         if (!value) {
709             continue;
710         }
711         qdev_printf("%s = %s\n", props->name,
712                     *value ? value : "<null>");
713         g_free(value);
714     }
715 }
716
717 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
718 {
719     BusClass *bc = BUS_GET_CLASS(bus);
720
721     if (bc->print_dev) {
722         bc->print_dev(mon, dev, indent);
723     }
724 }
725
726 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
727 {
728     ObjectClass *class;
729     BusState *child;
730     NamedGPIOList *ngl;
731     NamedClockList *ncl;
732
733     qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
734                 dev->id ? dev->id : "");
735     indent += 2;
736     QLIST_FOREACH(ngl, &dev->gpios, node) {
737         if (ngl->num_in) {
738             qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
739                         ngl->num_in);
740         }
741         if (ngl->num_out) {
742             qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
743                         ngl->num_out);
744         }
745     }
746     QLIST_FOREACH(ncl, &dev->clocks, node) {
747         g_autofree char *freq_str = clock_display_freq(ncl->clock);
748         qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
749                     ncl->output ? "out" : "in",
750                     ncl->alias ? " (alias)" : "",
751                     ncl->name, freq_str);
752     }
753     class = object_get_class(OBJECT(dev));
754     do {
755         qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
756         class = object_class_get_parent(class);
757     } while (class != object_class_by_name(TYPE_DEVICE));
758     bus_print_dev(dev->parent_bus, mon, dev, indent);
759     QLIST_FOREACH(child, &dev->child_bus, sibling) {
760         qbus_print(mon, child, indent);
761     }
762 }
763
764 static void qbus_print(Monitor *mon, BusState *bus, int indent)
765 {
766     BusChild *kid;
767
768     qdev_printf("bus: %s\n", bus->name);
769     indent += 2;
770     qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
771     QTAILQ_FOREACH(kid, &bus->children, sibling) {
772         DeviceState *dev = kid->child;
773         qdev_print(mon, dev, indent);
774     }
775 }
776 #undef qdev_printf
777
778 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
779 {
780     if (sysbus_get_default())
781         qbus_print(mon, sysbus_get_default(), 0);
782 }
783
784 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
785 {
786     qdev_print_devinfos(true);
787 }
788
789 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
790 {
791     QemuOpts *opts;
792     DeviceState *dev;
793
794     opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp);
795     if (!opts) {
796         return;
797     }
798     if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
799         qemu_opts_del(opts);
800         return;
801     }
802     dev = qdev_device_add(opts, errp);
803
804     /*
805      * Drain all pending RCU callbacks. This is done because
806      * some bus related operations can delay a device removal
807      * (in this case this can happen if device is added and then
808      * removed due to a configuration error)
809      * to a RCU callback, but user might expect that this interface
810      * will finish its job completely once qmp command returns result
811      * to the user
812      */
813     drain_call_rcu();
814
815     if (!dev) {
816         qemu_opts_del(opts);
817         return;
818     }
819     object_unref(OBJECT(dev));
820 }
821
822 static DeviceState *find_device_state(const char *id, Error **errp)
823 {
824     Object *obj;
825
826     if (id[0] == '/') {
827         obj = object_resolve_path(id, NULL);
828     } else {
829         char *root_path = object_get_canonical_path(qdev_get_peripheral());
830         char *path = g_strdup_printf("%s/%s", root_path, id);
831
832         g_free(root_path);
833         obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
834         g_free(path);
835     }
836
837     if (!obj) {
838         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
839                   "Device '%s' not found", id);
840         return NULL;
841     }
842
843     if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
844         error_setg(errp, "%s is not a hotpluggable device", id);
845         return NULL;
846     }
847
848     return DEVICE(obj);
849 }
850
851 void qdev_unplug(DeviceState *dev, Error **errp)
852 {
853     DeviceClass *dc = DEVICE_GET_CLASS(dev);
854     HotplugHandler *hotplug_ctrl;
855     HotplugHandlerClass *hdc;
856     Error *local_err = NULL;
857
858     if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
859         error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
860         return;
861     }
862
863     if (!dc->hotpluggable) {
864         error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
865                    object_get_typename(OBJECT(dev)));
866         return;
867     }
868
869     if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
870         error_setg(errp, "device_del not allowed while migrating");
871         return;
872     }
873
874     qdev_hot_removed = true;
875
876     hotplug_ctrl = qdev_get_hotplug_handler(dev);
877     /* hotpluggable device MUST have HotplugHandler, if it doesn't
878      * then something is very wrong with it */
879     g_assert(hotplug_ctrl);
880
881     /* If device supports async unplug just request it to be done,
882      * otherwise just remove it synchronously */
883     hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
884     if (hdc->unplug_request) {
885         hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
886     } else {
887         hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
888         if (!local_err) {
889             object_unparent(OBJECT(dev));
890         }
891     }
892     error_propagate(errp, local_err);
893 }
894
895 void qmp_device_del(const char *id, Error **errp)
896 {
897     DeviceState *dev = find_device_state(id, errp);
898     if (dev != NULL) {
899         if (dev->pending_deleted_event) {
900             error_setg(errp, "Device %s is already in the "
901                              "process of unplug", id);
902             return;
903         }
904
905         qdev_unplug(dev, errp);
906     }
907 }
908
909 void hmp_device_add(Monitor *mon, const QDict *qdict)
910 {
911     Error *err = NULL;
912
913     qmp_device_add((QDict *)qdict, NULL, &err);
914     hmp_handle_error(mon, err);
915 }
916
917 void hmp_device_del(Monitor *mon, const QDict *qdict)
918 {
919     const char *id = qdict_get_str(qdict, "id");
920     Error *err = NULL;
921
922     qmp_device_del(id, &err);
923     hmp_handle_error(mon, err);
924 }
925
926 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
927 {
928     DeviceState *dev;
929     BlockBackend *blk;
930
931     dev = find_device_state(id, errp);
932     if (dev == NULL) {
933         return NULL;
934     }
935
936     blk = blk_by_dev(dev);
937     if (!blk) {
938         error_setg(errp, "Device does not have a block device backend");
939     }
940     return blk;
941 }
942
943 QemuOptsList qemu_device_opts = {
944     .name = "device",
945     .implied_opt_name = "driver",
946     .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
947     .desc = {
948         /*
949          * no elements => accept any
950          * sanity checking will happen later
951          * when setting device properties
952          */
953         { /* end of list */ }
954     },
955 };
956
957 QemuOptsList qemu_global_opts = {
958     .name = "global",
959     .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
960     .desc = {
961         {
962             .name = "driver",
963             .type = QEMU_OPT_STRING,
964         },{
965             .name = "property",
966             .type = QEMU_OPT_STRING,
967         },{
968             .name = "value",
969             .type = QEMU_OPT_STRING,
970         },
971         { /* end of list */ }
972     },
973 };
974
975 int qemu_global_option(const char *str)
976 {
977     char driver[64], property[64];
978     QemuOpts *opts;
979     int rc, offset;
980
981     rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
982     if (rc == 2 && str[offset] == '=') {
983         opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
984         qemu_opt_set(opts, "driver", driver, &error_abort);
985         qemu_opt_set(opts, "property", property, &error_abort);
986         qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
987         return 0;
988     }
989
990     opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
991     if (!opts) {
992         return -1;
993     }
994
995     return 0;
996 }
997
998 bool qmp_command_available(const QmpCommand *cmd, Error **errp)
999 {
1000     if (!phase_check(PHASE_MACHINE_READY) &&
1001         !(cmd->options & QCO_ALLOW_PRECONFIG)) {
1002         error_setg(errp, "The command '%s' is permitted only after machine initialization has completed",
1003                    cmd->name);
1004         return false;
1005     }
1006     return true;
1007 }
This page took 0.081919 seconds and 4 git commands to generate.