]>
Commit | Line | Data |
---|---|---|
ee46d8a5 AL |
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 "qdev.h" | |
21 | #include "monitor.h" | |
a15fef21 | 22 | #include "qmp-commands.h" |
ee46d8a5 AL |
23 | |
24 | /* | |
25 | * Aliases were a bad idea from the start. Let's keep them | |
26 | * from spreading further. | |
27 | */ | |
28 | typedef struct QDevAlias | |
29 | { | |
30 | const char *typename; | |
31 | const char *alias; | |
32 | } QDevAlias; | |
33 | ||
34 | static const QDevAlias qdev_alias_table[] = { | |
35 | { "virtio-blk-pci", "virtio-blk" }, | |
36 | { "virtio-net-pci", "virtio-net" }, | |
37 | { "virtio-serial-pci", "virtio-serial" }, | |
38 | { "virtio-balloon-pci", "virtio-balloon" }, | |
39 | { "virtio-blk-s390", "virtio-blk" }, | |
40 | { "virtio-net-s390", "virtio-net" }, | |
41 | { "virtio-serial-s390", "virtio-serial" }, | |
42 | { "lsi53c895a", "lsi" }, | |
43 | { "ich9-ahci", "ahci" }, | |
44 | { } | |
45 | }; | |
46 | ||
47 | static const char *qdev_class_get_alias(DeviceClass *dc) | |
48 | { | |
49 | const char *typename = object_class_get_name(OBJECT_CLASS(dc)); | |
50 | int i; | |
51 | ||
52 | for (i = 0; qdev_alias_table[i].typename; i++) { | |
53 | if (strcmp(qdev_alias_table[i].typename, typename) == 0) { | |
54 | return qdev_alias_table[i].alias; | |
55 | } | |
56 | } | |
57 | ||
58 | return NULL; | |
59 | } | |
60 | ||
61 | static bool qdev_class_has_alias(DeviceClass *dc) | |
62 | { | |
63 | return (qdev_class_get_alias(dc) != NULL); | |
64 | } | |
65 | ||
66 | static void qdev_print_devinfo(ObjectClass *klass, void *opaque) | |
67 | { | |
68 | DeviceClass *dc; | |
69 | bool *show_no_user = opaque; | |
70 | ||
71 | dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE); | |
72 | ||
73 | if (!dc || (show_no_user && !*show_no_user && dc->no_user)) { | |
74 | return; | |
75 | } | |
76 | ||
77 | error_printf("name \"%s\"", object_class_get_name(klass)); | |
78 | if (dc->bus_info) { | |
79 | error_printf(", bus %s", dc->bus_info->name); | |
80 | } | |
81 | if (qdev_class_has_alias(dc)) { | |
82 | error_printf(", alias \"%s\"", qdev_class_get_alias(dc)); | |
83 | } | |
84 | if (dc->desc) { | |
85 | error_printf(", desc \"%s\"", dc->desc); | |
86 | } | |
87 | if (dc->no_user) { | |
88 | error_printf(", no-user"); | |
89 | } | |
90 | error_printf("\n"); | |
91 | } | |
92 | ||
93 | static int set_property(const char *name, const char *value, void *opaque) | |
94 | { | |
95 | DeviceState *dev = opaque; | |
96 | ||
97 | if (strcmp(name, "driver") == 0) | |
98 | return 0; | |
99 | if (strcmp(name, "bus") == 0) | |
100 | return 0; | |
101 | ||
102 | if (qdev_prop_parse(dev, name, value) == -1) { | |
103 | return -1; | |
104 | } | |
105 | return 0; | |
106 | } | |
107 | ||
108 | static const char *find_typename_by_alias(const char *alias) | |
109 | { | |
110 | int i; | |
111 | ||
112 | for (i = 0; qdev_alias_table[i].alias; i++) { | |
113 | if (strcmp(qdev_alias_table[i].alias, alias) == 0) { | |
114 | return qdev_alias_table[i].typename; | |
115 | } | |
116 | } | |
117 | ||
118 | return NULL; | |
119 | } | |
120 | ||
121 | int qdev_device_help(QemuOpts *opts) | |
122 | { | |
123 | const char *driver; | |
124 | Property *prop; | |
125 | ObjectClass *klass; | |
126 | DeviceClass *info; | |
127 | ||
128 | driver = qemu_opt_get(opts, "driver"); | |
129 | if (driver && !strcmp(driver, "?")) { | |
130 | bool show_no_user = false; | |
131 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user); | |
132 | return 1; | |
133 | } | |
134 | ||
135 | if (!driver || !qemu_opt_get(opts, "?")) { | |
136 | return 0; | |
137 | } | |
138 | ||
139 | klass = object_class_by_name(driver); | |
140 | if (!klass) { | |
141 | const char *typename = find_typename_by_alias(driver); | |
142 | ||
143 | if (typename) { | |
144 | driver = typename; | |
145 | klass = object_class_by_name(driver); | |
146 | } | |
147 | } | |
148 | ||
149 | if (!klass) { | |
150 | return 0; | |
151 | } | |
152 | info = DEVICE_CLASS(klass); | |
153 | ||
154 | for (prop = info->props; prop && prop->name; prop++) { | |
155 | /* | |
156 | * TODO Properties without a parser are just for dirty hacks. | |
157 | * qdev_prop_ptr is the only such PropertyInfo. It's marked | |
158 | * for removal. This conditional should be removed along with | |
159 | * it. | |
160 | */ | |
161 | if (!prop->info->parse) { | |
162 | continue; /* no way to set it, don't show */ | |
163 | } | |
164 | error_printf("%s.%s=%s\n", driver, prop->name, | |
165 | prop->info->legacy_name ?: prop->info->name); | |
166 | } | |
d03d6b4e AL |
167 | if (info->bus_info) { |
168 | for (prop = info->bus_info->props; prop && prop->name; prop++) { | |
169 | if (!prop->info->parse) { | |
170 | continue; /* no way to set it, don't show */ | |
171 | } | |
172 | error_printf("%s.%s=%s\n", driver, prop->name, | |
173 | prop->info->legacy_name ?: prop->info->name); | |
ee46d8a5 | 174 | } |
ee46d8a5 AL |
175 | } |
176 | return 1; | |
177 | } | |
178 | ||
57c9fafe | 179 | static Object *qdev_get_peripheral(void) |
ee46d8a5 | 180 | { |
8b45d447 | 181 | static Object *dev; |
ee46d8a5 AL |
182 | |
183 | if (dev == NULL) { | |
f05f6b4a | 184 | dev = container_get("/machine/peripheral"); |
ee46d8a5 AL |
185 | } |
186 | ||
8b45d447 | 187 | return dev; |
ee46d8a5 AL |
188 | } |
189 | ||
57c9fafe | 190 | static Object *qdev_get_peripheral_anon(void) |
ee46d8a5 | 191 | { |
8b45d447 | 192 | static Object *dev; |
ee46d8a5 AL |
193 | |
194 | if (dev == NULL) { | |
f05f6b4a | 195 | dev = container_get("/machine/peripheral-anon"); |
ee46d8a5 AL |
196 | } |
197 | ||
8b45d447 | 198 | return dev; |
ee46d8a5 AL |
199 | } |
200 | ||
201 | static void qbus_list_bus(DeviceState *dev) | |
202 | { | |
203 | BusState *child; | |
204 | const char *sep = " "; | |
205 | ||
206 | error_printf("child busses at \"%s\":", | |
207 | dev->id ? dev->id : object_get_typename(OBJECT(dev))); | |
208 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
209 | error_printf("%s\"%s\"", sep, child->name); | |
210 | sep = ", "; | |
211 | } | |
212 | error_printf("\n"); | |
213 | } | |
214 | ||
215 | static void qbus_list_dev(BusState *bus) | |
216 | { | |
217 | DeviceState *dev; | |
218 | const char *sep = " "; | |
219 | ||
220 | error_printf("devices at \"%s\":", bus->name); | |
221 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
222 | error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev))); | |
223 | if (dev->id) | |
224 | error_printf("/\"%s\"", dev->id); | |
225 | sep = ", "; | |
226 | } | |
227 | error_printf("\n"); | |
228 | } | |
229 | ||
230 | static BusState *qbus_find_bus(DeviceState *dev, char *elem) | |
231 | { | |
232 | BusState *child; | |
233 | ||
234 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
235 | if (strcmp(child->name, elem) == 0) { | |
236 | return child; | |
237 | } | |
238 | } | |
239 | return NULL; | |
240 | } | |
241 | ||
242 | static DeviceState *qbus_find_dev(BusState *bus, char *elem) | |
243 | { | |
244 | DeviceState *dev; | |
245 | ||
246 | /* | |
247 | * try to match in order: | |
248 | * (1) instance id, if present | |
249 | * (2) driver name | |
250 | * (3) driver alias, if present | |
251 | */ | |
252 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
253 | if (dev->id && strcmp(dev->id, elem) == 0) { | |
254 | return dev; | |
255 | } | |
256 | } | |
257 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
258 | if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) { | |
259 | return dev; | |
260 | } | |
261 | } | |
262 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
263 | DeviceClass *dc = DEVICE_GET_CLASS(dev); | |
264 | ||
265 | if (qdev_class_has_alias(dc) && | |
266 | strcmp(qdev_class_get_alias(dc), elem) == 0) { | |
267 | return dev; | |
268 | } | |
269 | } | |
270 | return NULL; | |
271 | } | |
272 | ||
273 | static BusState *qbus_find_recursive(BusState *bus, const char *name, | |
274 | const BusInfo *info) | |
275 | { | |
276 | DeviceState *dev; | |
277 | BusState *child, *ret; | |
278 | int match = 1; | |
279 | ||
280 | if (name && (strcmp(bus->name, name) != 0)) { | |
281 | match = 0; | |
282 | } | |
283 | if (info && (bus->info != info)) { | |
284 | match = 0; | |
285 | } | |
286 | if (match) { | |
287 | return bus; | |
288 | } | |
289 | ||
290 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
291 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
292 | ret = qbus_find_recursive(child, name, info); | |
293 | if (ret) { | |
294 | return ret; | |
295 | } | |
296 | } | |
297 | } | |
298 | return NULL; | |
299 | } | |
300 | ||
301 | static BusState *qbus_find(const char *path) | |
302 | { | |
303 | DeviceState *dev; | |
304 | BusState *bus; | |
305 | char elem[128]; | |
306 | int pos, len; | |
307 | ||
308 | /* find start element */ | |
309 | if (path[0] == '/') { | |
310 | bus = sysbus_get_default(); | |
311 | pos = 0; | |
312 | } else { | |
313 | if (sscanf(path, "%127[^/]%n", elem, &len) != 1) { | |
314 | assert(!path[0]); | |
315 | elem[0] = len = 0; | |
316 | } | |
317 | bus = qbus_find_recursive(sysbus_get_default(), elem, NULL); | |
318 | if (!bus) { | |
319 | qerror_report(QERR_BUS_NOT_FOUND, elem); | |
320 | return NULL; | |
321 | } | |
322 | pos = len; | |
323 | } | |
324 | ||
325 | for (;;) { | |
326 | assert(path[pos] == '/' || !path[pos]); | |
327 | while (path[pos] == '/') { | |
328 | pos++; | |
329 | } | |
330 | if (path[pos] == '\0') { | |
331 | return bus; | |
332 | } | |
333 | ||
334 | /* find device */ | |
335 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { | |
336 | assert(0); | |
337 | elem[0] = len = 0; | |
338 | } | |
339 | pos += len; | |
340 | dev = qbus_find_dev(bus, elem); | |
341 | if (!dev) { | |
342 | qerror_report(QERR_DEVICE_NOT_FOUND, elem); | |
343 | if (!monitor_cur_is_qmp()) { | |
344 | qbus_list_dev(bus); | |
345 | } | |
346 | return NULL; | |
347 | } | |
348 | ||
349 | assert(path[pos] == '/' || !path[pos]); | |
350 | while (path[pos] == '/') { | |
351 | pos++; | |
352 | } | |
353 | if (path[pos] == '\0') { | |
354 | /* last specified element is a device. If it has exactly | |
355 | * one child bus accept it nevertheless */ | |
356 | switch (dev->num_child_bus) { | |
357 | case 0: | |
358 | qerror_report(QERR_DEVICE_NO_BUS, elem); | |
359 | return NULL; | |
360 | case 1: | |
361 | return QLIST_FIRST(&dev->child_bus); | |
362 | default: | |
363 | qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem); | |
364 | if (!monitor_cur_is_qmp()) { | |
365 | qbus_list_bus(dev); | |
366 | } | |
367 | return NULL; | |
368 | } | |
369 | } | |
370 | ||
371 | /* find bus */ | |
372 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { | |
373 | assert(0); | |
374 | elem[0] = len = 0; | |
375 | } | |
376 | pos += len; | |
377 | bus = qbus_find_bus(dev, elem); | |
378 | if (!bus) { | |
379 | qerror_report(QERR_BUS_NOT_FOUND, elem); | |
380 | if (!monitor_cur_is_qmp()) { | |
381 | qbus_list_bus(dev); | |
382 | } | |
383 | return NULL; | |
384 | } | |
385 | } | |
386 | } | |
387 | ||
388 | DeviceState *qdev_device_add(QemuOpts *opts) | |
389 | { | |
390 | ObjectClass *obj; | |
391 | DeviceClass *k; | |
392 | const char *driver, *path, *id; | |
393 | DeviceState *qdev; | |
394 | BusState *bus; | |
395 | ||
396 | driver = qemu_opt_get(opts, "driver"); | |
397 | if (!driver) { | |
398 | qerror_report(QERR_MISSING_PARAMETER, "driver"); | |
399 | return NULL; | |
400 | } | |
401 | ||
402 | /* find driver */ | |
403 | obj = object_class_by_name(driver); | |
404 | if (!obj) { | |
405 | const char *typename = find_typename_by_alias(driver); | |
406 | ||
407 | if (typename) { | |
408 | driver = typename; | |
409 | obj = object_class_by_name(driver); | |
410 | } | |
411 | } | |
412 | ||
413 | if (!obj) { | |
414 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type"); | |
415 | return NULL; | |
416 | } | |
417 | ||
418 | k = DEVICE_CLASS(obj); | |
419 | ||
420 | /* find bus */ | |
421 | path = qemu_opt_get(opts, "bus"); | |
422 | if (path != NULL) { | |
423 | bus = qbus_find(path); | |
424 | if (!bus) { | |
425 | return NULL; | |
426 | } | |
427 | if (bus->info != k->bus_info) { | |
428 | qerror_report(QERR_BAD_BUS_FOR_DEVICE, | |
429 | driver, bus->info->name); | |
430 | return NULL; | |
431 | } | |
432 | } else { | |
433 | bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_info); | |
434 | if (!bus) { | |
435 | qerror_report(QERR_NO_BUS_FOR_DEVICE, | |
436 | driver, k->bus_info->name); | |
437 | return NULL; | |
438 | } | |
439 | } | |
440 | if (qdev_hotplug && !bus->allow_hotplug) { | |
441 | qerror_report(QERR_BUS_NO_HOTPLUG, bus->name); | |
442 | return NULL; | |
443 | } | |
444 | ||
445 | if (!bus) { | |
446 | bus = sysbus_get_default(); | |
447 | } | |
448 | ||
449 | /* create device, set properties */ | |
450 | qdev = DEVICE(object_new(driver)); | |
451 | qdev_set_parent_bus(qdev, bus); | |
452 | qdev_prop_set_globals(qdev); | |
453 | ||
454 | id = qemu_opts_id(opts); | |
455 | if (id) { | |
456 | qdev->id = id; | |
b2d4b3f7 AL |
457 | } |
458 | if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) { | |
459 | qdev_free(qdev); | |
460 | return NULL; | |
461 | } | |
b2d4b3f7 | 462 | if (qdev->id) { |
57c9fafe AL |
463 | object_property_add_child(qdev_get_peripheral(), qdev->id, |
464 | OBJECT(qdev), NULL); | |
ee46d8a5 AL |
465 | } else { |
466 | static int anon_count; | |
467 | gchar *name = g_strdup_printf("device[%d]", anon_count++); | |
57c9fafe AL |
468 | object_property_add_child(qdev_get_peripheral_anon(), name, |
469 | OBJECT(qdev), NULL); | |
ee46d8a5 AL |
470 | g_free(name); |
471 | } | |
f424d5c4 PB |
472 | if (qdev_init(qdev) < 0) { |
473 | qerror_report(QERR_DEVICE_INIT_FAILED, driver); | |
474 | return NULL; | |
475 | } | |
ee46d8a5 AL |
476 | qdev->opts = opts; |
477 | return qdev; | |
478 | } | |
479 | ||
480 | ||
481 | #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) | |
482 | static void qbus_print(Monitor *mon, BusState *bus, int indent); | |
483 | ||
484 | static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, | |
485 | const char *prefix, int indent) | |
486 | { | |
ee46d8a5 AL |
487 | if (!props) |
488 | return; | |
d822979b PB |
489 | for (; props->name; props++) { |
490 | Error *err = NULL; | |
491 | char *value; | |
492 | char *legacy_name = g_strdup_printf("legacy-%s", props->name); | |
493 | if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) { | |
494 | value = object_property_get_str(OBJECT(dev), legacy_name, &err); | |
495 | } else { | |
496 | value = object_property_get_str(OBJECT(dev), props->name, &err); | |
497 | } | |
498 | g_free(legacy_name); | |
499 | ||
500 | if (err) { | |
501 | error_free(err); | |
502 | continue; | |
ee46d8a5 | 503 | } |
d822979b PB |
504 | qdev_printf("%s-prop: %s = %s\n", prefix, props->name, |
505 | value && *value ? value : "<null>"); | |
506 | g_free(value); | |
ee46d8a5 AL |
507 | } |
508 | } | |
509 | ||
510 | static void qdev_print(Monitor *mon, DeviceState *dev, int indent) | |
511 | { | |
512 | BusState *child; | |
513 | qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), | |
514 | dev->id ? dev->id : ""); | |
515 | indent += 2; | |
516 | if (dev->num_gpio_in) { | |
517 | qdev_printf("gpio-in %d\n", dev->num_gpio_in); | |
518 | } | |
519 | if (dev->num_gpio_out) { | |
520 | qdev_printf("gpio-out %d\n", dev->num_gpio_out); | |
521 | } | |
522 | qdev_print_props(mon, dev, qdev_get_props(dev), "dev", indent); | |
523 | qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent); | |
524 | if (dev->parent_bus->info->print_dev) | |
525 | dev->parent_bus->info->print_dev(mon, dev, indent); | |
526 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
527 | qbus_print(mon, child, indent); | |
528 | } | |
529 | } | |
530 | ||
531 | static void qbus_print(Monitor *mon, BusState *bus, int indent) | |
532 | { | |
533 | struct DeviceState *dev; | |
534 | ||
535 | qdev_printf("bus: %s\n", bus->name); | |
536 | indent += 2; | |
537 | qdev_printf("type %s\n", bus->info->name); | |
538 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
539 | qdev_print(mon, dev, indent); | |
540 | } | |
541 | } | |
542 | #undef qdev_printf | |
543 | ||
544 | void do_info_qtree(Monitor *mon) | |
545 | { | |
546 | if (sysbus_get_default()) | |
547 | qbus_print(mon, sysbus_get_default(), 0); | |
548 | } | |
549 | ||
550 | void do_info_qdm(Monitor *mon) | |
551 | { | |
552 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL); | |
553 | } | |
554 | ||
555 | int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
556 | { | |
557 | QemuOpts *opts; | |
558 | ||
559 | opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict); | |
560 | if (!opts) { | |
561 | return -1; | |
562 | } | |
563 | if (!monitor_cur_is_qmp() && qdev_device_help(opts)) { | |
564 | qemu_opts_del(opts); | |
565 | return 0; | |
566 | } | |
567 | if (!qdev_device_add(opts)) { | |
568 | qemu_opts_del(opts); | |
569 | return -1; | |
570 | } | |
571 | return 0; | |
572 | } | |
573 | ||
a15fef21 | 574 | void qmp_device_del(const char *id, Error **errp) |
ee46d8a5 | 575 | { |
ee46d8a5 AL |
576 | DeviceState *dev; |
577 | ||
578 | dev = qdev_find_recursive(sysbus_get_default(), id); | |
579 | if (NULL == dev) { | |
a15fef21 LC |
580 | error_set(errp, QERR_DEVICE_NOT_FOUND, id); |
581 | return; | |
56f9107e LC |
582 | } |
583 | ||
a15fef21 | 584 | qdev_unplug(dev, errp); |
ee46d8a5 AL |
585 | } |
586 | ||
587 | void qdev_machine_init(void) | |
588 | { | |
589 | qdev_get_peripheral_anon(); | |
590 | qdev_get_peripheral(); | |
591 | } |