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