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