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