]>
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" | |
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 | for (prop = info->bus_info->props; prop && prop->name; prop++) { | |
167 | if (!prop->info->parse) { | |
168 | continue; /* no way to set it, don't show */ | |
169 | } | |
170 | error_printf("%s.%s=%s\n", driver, prop->name, | |
171 | prop->info->legacy_name ?: prop->info->name); | |
172 | } | |
173 | return 1; | |
174 | } | |
175 | ||
176 | static DeviceState *qdev_get_peripheral(void) | |
177 | { | |
178 | static DeviceState *dev; | |
179 | ||
180 | if (dev == NULL) { | |
181 | dev = qdev_create(NULL, "container"); | |
182 | qdev_property_add_child(qdev_get_root(), "peripheral", dev, NULL); | |
183 | qdev_init_nofail(dev); | |
184 | } | |
185 | ||
186 | return dev; | |
187 | } | |
188 | ||
189 | static DeviceState *qdev_get_peripheral_anon(void) | |
190 | { | |
191 | static DeviceState *dev; | |
192 | ||
193 | if (dev == NULL) { | |
194 | dev = qdev_create(NULL, "container"); | |
195 | qdev_property_add_child(qdev_get_root(), "peripheral-anon", dev, NULL); | |
196 | qdev_init_nofail(dev); | |
197 | } | |
198 | ||
199 | return dev; | |
200 | } | |
201 | ||
202 | static void qbus_list_bus(DeviceState *dev) | |
203 | { | |
204 | BusState *child; | |
205 | const char *sep = " "; | |
206 | ||
207 | error_printf("child busses at \"%s\":", | |
208 | dev->id ? dev->id : object_get_typename(OBJECT(dev))); | |
209 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
210 | error_printf("%s\"%s\"", sep, child->name); | |
211 | sep = ", "; | |
212 | } | |
213 | error_printf("\n"); | |
214 | } | |
215 | ||
216 | static void qbus_list_dev(BusState *bus) | |
217 | { | |
218 | DeviceState *dev; | |
219 | const char *sep = " "; | |
220 | ||
221 | error_printf("devices at \"%s\":", bus->name); | |
222 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
223 | error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev))); | |
224 | if (dev->id) | |
225 | error_printf("/\"%s\"", dev->id); | |
226 | sep = ", "; | |
227 | } | |
228 | error_printf("\n"); | |
229 | } | |
230 | ||
231 | static BusState *qbus_find_bus(DeviceState *dev, char *elem) | |
232 | { | |
233 | BusState *child; | |
234 | ||
235 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
236 | if (strcmp(child->name, elem) == 0) { | |
237 | return child; | |
238 | } | |
239 | } | |
240 | return NULL; | |
241 | } | |
242 | ||
243 | static DeviceState *qbus_find_dev(BusState *bus, char *elem) | |
244 | { | |
245 | DeviceState *dev; | |
246 | ||
247 | /* | |
248 | * try to match in order: | |
249 | * (1) instance id, if present | |
250 | * (2) driver name | |
251 | * (3) driver alias, if present | |
252 | */ | |
253 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
254 | if (dev->id && strcmp(dev->id, elem) == 0) { | |
255 | return dev; | |
256 | } | |
257 | } | |
258 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
259 | if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) { | |
260 | return dev; | |
261 | } | |
262 | } | |
263 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
264 | DeviceClass *dc = DEVICE_GET_CLASS(dev); | |
265 | ||
266 | if (qdev_class_has_alias(dc) && | |
267 | strcmp(qdev_class_get_alias(dc), elem) == 0) { | |
268 | return dev; | |
269 | } | |
270 | } | |
271 | return NULL; | |
272 | } | |
273 | ||
274 | static BusState *qbus_find_recursive(BusState *bus, const char *name, | |
275 | const BusInfo *info) | |
276 | { | |
277 | DeviceState *dev; | |
278 | BusState *child, *ret; | |
279 | int match = 1; | |
280 | ||
281 | if (name && (strcmp(bus->name, name) != 0)) { | |
282 | match = 0; | |
283 | } | |
284 | if (info && (bus->info != info)) { | |
285 | match = 0; | |
286 | } | |
287 | if (match) { | |
288 | return bus; | |
289 | } | |
290 | ||
291 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
292 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
293 | ret = qbus_find_recursive(child, name, info); | |
294 | if (ret) { | |
295 | return ret; | |
296 | } | |
297 | } | |
298 | } | |
299 | return NULL; | |
300 | } | |
301 | ||
302 | static BusState *qbus_find(const char *path) | |
303 | { | |
304 | DeviceState *dev; | |
305 | BusState *bus; | |
306 | char elem[128]; | |
307 | int pos, len; | |
308 | ||
309 | /* find start element */ | |
310 | if (path[0] == '/') { | |
311 | bus = sysbus_get_default(); | |
312 | pos = 0; | |
313 | } else { | |
314 | if (sscanf(path, "%127[^/]%n", elem, &len) != 1) { | |
315 | assert(!path[0]); | |
316 | elem[0] = len = 0; | |
317 | } | |
318 | bus = qbus_find_recursive(sysbus_get_default(), elem, NULL); | |
319 | if (!bus) { | |
320 | qerror_report(QERR_BUS_NOT_FOUND, elem); | |
321 | return NULL; | |
322 | } | |
323 | pos = len; | |
324 | } | |
325 | ||
326 | for (;;) { | |
327 | assert(path[pos] == '/' || !path[pos]); | |
328 | while (path[pos] == '/') { | |
329 | pos++; | |
330 | } | |
331 | if (path[pos] == '\0') { | |
332 | return bus; | |
333 | } | |
334 | ||
335 | /* find device */ | |
336 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { | |
337 | assert(0); | |
338 | elem[0] = len = 0; | |
339 | } | |
340 | pos += len; | |
341 | dev = qbus_find_dev(bus, elem); | |
342 | if (!dev) { | |
343 | qerror_report(QERR_DEVICE_NOT_FOUND, elem); | |
344 | if (!monitor_cur_is_qmp()) { | |
345 | qbus_list_dev(bus); | |
346 | } | |
347 | return NULL; | |
348 | } | |
349 | ||
350 | assert(path[pos] == '/' || !path[pos]); | |
351 | while (path[pos] == '/') { | |
352 | pos++; | |
353 | } | |
354 | if (path[pos] == '\0') { | |
355 | /* last specified element is a device. If it has exactly | |
356 | * one child bus accept it nevertheless */ | |
357 | switch (dev->num_child_bus) { | |
358 | case 0: | |
359 | qerror_report(QERR_DEVICE_NO_BUS, elem); | |
360 | return NULL; | |
361 | case 1: | |
362 | return QLIST_FIRST(&dev->child_bus); | |
363 | default: | |
364 | qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem); | |
365 | if (!monitor_cur_is_qmp()) { | |
366 | qbus_list_bus(dev); | |
367 | } | |
368 | return NULL; | |
369 | } | |
370 | } | |
371 | ||
372 | /* find bus */ | |
373 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { | |
374 | assert(0); | |
375 | elem[0] = len = 0; | |
376 | } | |
377 | pos += len; | |
378 | bus = qbus_find_bus(dev, elem); | |
379 | if (!bus) { | |
380 | qerror_report(QERR_BUS_NOT_FOUND, elem); | |
381 | if (!monitor_cur_is_qmp()) { | |
382 | qbus_list_bus(dev); | |
383 | } | |
384 | return NULL; | |
385 | } | |
386 | } | |
387 | } | |
388 | ||
389 | DeviceState *qdev_device_add(QemuOpts *opts) | |
390 | { | |
391 | ObjectClass *obj; | |
392 | DeviceClass *k; | |
393 | const char *driver, *path, *id; | |
394 | DeviceState *qdev; | |
395 | BusState *bus; | |
396 | ||
397 | driver = qemu_opt_get(opts, "driver"); | |
398 | if (!driver) { | |
399 | qerror_report(QERR_MISSING_PARAMETER, "driver"); | |
400 | return NULL; | |
401 | } | |
402 | ||
403 | /* find driver */ | |
404 | obj = object_class_by_name(driver); | |
405 | if (!obj) { | |
406 | const char *typename = find_typename_by_alias(driver); | |
407 | ||
408 | if (typename) { | |
409 | driver = typename; | |
410 | obj = object_class_by_name(driver); | |
411 | } | |
412 | } | |
413 | ||
414 | if (!obj) { | |
415 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type"); | |
416 | return NULL; | |
417 | } | |
418 | ||
419 | k = DEVICE_CLASS(obj); | |
420 | ||
421 | /* find bus */ | |
422 | path = qemu_opt_get(opts, "bus"); | |
423 | if (path != NULL) { | |
424 | bus = qbus_find(path); | |
425 | if (!bus) { | |
426 | return NULL; | |
427 | } | |
428 | if (bus->info != k->bus_info) { | |
429 | qerror_report(QERR_BAD_BUS_FOR_DEVICE, | |
430 | driver, bus->info->name); | |
431 | return NULL; | |
432 | } | |
433 | } else { | |
434 | bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_info); | |
435 | if (!bus) { | |
436 | qerror_report(QERR_NO_BUS_FOR_DEVICE, | |
437 | driver, k->bus_info->name); | |
438 | return NULL; | |
439 | } | |
440 | } | |
441 | if (qdev_hotplug && !bus->allow_hotplug) { | |
442 | qerror_report(QERR_BUS_NO_HOTPLUG, bus->name); | |
443 | return NULL; | |
444 | } | |
445 | ||
446 | if (!bus) { | |
447 | bus = sysbus_get_default(); | |
448 | } | |
449 | ||
450 | /* create device, set properties */ | |
451 | qdev = DEVICE(object_new(driver)); | |
452 | qdev_set_parent_bus(qdev, bus); | |
453 | qdev_prop_set_globals(qdev); | |
454 | ||
455 | id = qemu_opts_id(opts); | |
456 | if (id) { | |
457 | qdev->id = id; | |
458 | qdev_property_add_child(qdev_get_peripheral(), qdev->id, qdev, NULL); | |
459 | } else { | |
460 | static int anon_count; | |
461 | gchar *name = g_strdup_printf("device[%d]", anon_count++); | |
462 | qdev_property_add_child(qdev_get_peripheral_anon(), name, | |
463 | qdev, NULL); | |
464 | g_free(name); | |
465 | } | |
466 | if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) { | |
467 | qdev_free(qdev); | |
468 | return NULL; | |
469 | } | |
470 | if (qdev_init(qdev) < 0) { | |
471 | qerror_report(QERR_DEVICE_INIT_FAILED, driver); | |
472 | return NULL; | |
473 | } | |
474 | qdev->opts = opts; | |
475 | return qdev; | |
476 | } | |
477 | ||
478 | ||
479 | #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) | |
480 | static void qbus_print(Monitor *mon, BusState *bus, int indent); | |
481 | ||
482 | static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, | |
483 | const char *prefix, int indent) | |
484 | { | |
485 | char buf[64]; | |
486 | ||
487 | if (!props) | |
488 | return; | |
489 | while (props->name) { | |
490 | /* | |
491 | * TODO Properties without a print method are just for dirty | |
492 | * hacks. qdev_prop_ptr is the only such PropertyInfo. It's | |
493 | * marked for removal. The test props->info->print should be | |
494 | * removed along with it. | |
495 | */ | |
496 | if (props->info->print) { | |
497 | props->info->print(dev, props, buf, sizeof(buf)); | |
498 | qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf); | |
499 | } | |
500 | props++; | |
501 | } | |
502 | } | |
503 | ||
504 | static void qdev_print(Monitor *mon, DeviceState *dev, int indent) | |
505 | { | |
506 | BusState *child; | |
507 | qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), | |
508 | dev->id ? dev->id : ""); | |
509 | indent += 2; | |
510 | if (dev->num_gpio_in) { | |
511 | qdev_printf("gpio-in %d\n", dev->num_gpio_in); | |
512 | } | |
513 | if (dev->num_gpio_out) { | |
514 | qdev_printf("gpio-out %d\n", dev->num_gpio_out); | |
515 | } | |
516 | qdev_print_props(mon, dev, qdev_get_props(dev), "dev", indent); | |
517 | qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent); | |
518 | if (dev->parent_bus->info->print_dev) | |
519 | dev->parent_bus->info->print_dev(mon, dev, indent); | |
520 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
521 | qbus_print(mon, child, indent); | |
522 | } | |
523 | } | |
524 | ||
525 | static void qbus_print(Monitor *mon, BusState *bus, int indent) | |
526 | { | |
527 | struct DeviceState *dev; | |
528 | ||
529 | qdev_printf("bus: %s\n", bus->name); | |
530 | indent += 2; | |
531 | qdev_printf("type %s\n", bus->info->name); | |
532 | QTAILQ_FOREACH(dev, &bus->children, sibling) { | |
533 | qdev_print(mon, dev, indent); | |
534 | } | |
535 | } | |
536 | #undef qdev_printf | |
537 | ||
538 | void do_info_qtree(Monitor *mon) | |
539 | { | |
540 | if (sysbus_get_default()) | |
541 | qbus_print(mon, sysbus_get_default(), 0); | |
542 | } | |
543 | ||
544 | void do_info_qdm(Monitor *mon) | |
545 | { | |
546 | object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL); | |
547 | } | |
548 | ||
549 | int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
550 | { | |
551 | QemuOpts *opts; | |
552 | ||
553 | opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict); | |
554 | if (!opts) { | |
555 | return -1; | |
556 | } | |
557 | if (!monitor_cur_is_qmp() && qdev_device_help(opts)) { | |
558 | qemu_opts_del(opts); | |
559 | return 0; | |
560 | } | |
561 | if (!qdev_device_add(opts)) { | |
562 | qemu_opts_del(opts); | |
563 | return -1; | |
564 | } | |
565 | return 0; | |
566 | } | |
567 | ||
568 | int do_device_del(Monitor *mon, const QDict *qdict, QObject **ret_data) | |
569 | { | |
570 | const char *id = qdict_get_str(qdict, "id"); | |
571 | DeviceState *dev; | |
572 | ||
573 | dev = qdev_find_recursive(sysbus_get_default(), id); | |
574 | if (NULL == dev) { | |
575 | qerror_report(QERR_DEVICE_NOT_FOUND, id); | |
576 | return -1; | |
577 | } | |
578 | return qdev_unplug(dev); | |
579 | } | |
580 | ||
581 | void qdev_machine_init(void) | |
582 | { | |
583 | qdev_get_peripheral_anon(); | |
584 | qdev_get_peripheral(); | |
585 | } |