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