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