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