]>
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 | ||
d38ea87a | 20 | #include "qemu/osdep.h" |
2f7bd829 | 21 | #include "hw/sysbus.h" |
d94bd092 | 22 | #include "monitor/hmp.h" |
83c9089e | 23 | #include "monitor/monitor.h" |
b4a42f81 | 24 | #include "monitor/qdev.h" |
9c17d615 | 25 | #include "sysemu/arch_init.h" |
e688df6b | 26 | #include "qapi/error.h" |
c577ff62 | 27 | #include "qapi/qapi-commands-qdev.h" |
452fcdbc | 28 | #include "qapi/qmp/qdict.h" |
cc7a8ea7 | 29 | #include "qapi/qmp/qerror.h" |
1de7afc9 | 30 | #include "qemu/config-file.h" |
d49b6836 | 31 | #include "qemu/error-report.h" |
f348b6d1 | 32 | #include "qemu/help_option.h" |
922a01a0 | 33 | #include "qemu/option.h" |
8acb2a75 | 34 | #include "qemu/qemu-print.h" |
f3a85056 | 35 | #include "qemu/option_int.h" |
9680caee | 36 | #include "sysemu/block-backend.h" |
d5938f29 | 37 | #include "sysemu/sysemu.h" |
c4b63b7c | 38 | #include "migration/misc.h" |
f3a85056 | 39 | #include "migration/migration.h" |
e1043d67 | 40 | #include "qemu/cutils.h" |
ee46d8a5 AL |
41 | |
42 | /* | |
43 | * Aliases were a bad idea from the start. Let's keep them | |
44 | * from spreading further. | |
45 | */ | |
46 | typedef struct QDevAlias | |
47 | { | |
48 | const char *typename; | |
49 | const char *alias; | |
5f629d94 | 50 | uint32_t arch_mask; |
ee46d8a5 AL |
51 | } QDevAlias; |
52 | ||
36e99168 | 53 | /* Please keep this table sorted by typename. */ |
ee46d8a5 | 54 | static const QDevAlias qdev_alias_table[] = { |
36e99168 SS |
55 | { "e1000", "e1000-82540em" }, |
56 | { "ich9-ahci", "ahci" }, | |
36e99168 | 57 | { "lsi53c895a", "lsi" }, |
588c36ca SS |
58 | { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X }, |
59 | { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
60 | { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X }, | |
5f629d94 AG |
61 | { "virtio-balloon-pci", "virtio-balloon", |
62 | QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
1f68f1d3 | 63 | { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X }, |
36e99168 | 64 | { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
588c36ca SS |
65 | { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X }, |
66 | { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
67 | { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X }, | |
68 | { "virtio-input-host-pci", "virtio-input-host", | |
69 | QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
70 | { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X }, | |
71 | { "virtio-keyboard-pci", "virtio-keyboard", | |
72 | QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
73 | { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X }, | |
74 | { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
1f68f1d3 | 75 | { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X }, |
36e99168 | 76 | { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
588c36ca SS |
77 | { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X }, |
78 | { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
79 | { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X }, | |
80 | { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
1f68f1d3 | 81 | { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X }, |
36e99168 | 82 | { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, |
588c36ca SS |
83 | { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X }, |
84 | { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X }, | |
ee46d8a5 AL |
85 | { } |
86 | }; | |
87 | ||
88 | static const char *qdev_class_get_alias(DeviceClass *dc) | |
89 | { | |
90 | const char *typename = object_class_get_name(OBJECT_CLASS(dc)); | |
91 | int i; | |
92 | ||
93 | for (i = 0; qdev_alias_table[i].typename; i++) { | |
5f629d94 AG |
94 | if (qdev_alias_table[i].arch_mask && |
95 | !(qdev_alias_table[i].arch_mask & arch_type)) { | |
96 | continue; | |
97 | } | |
98 | ||
ee46d8a5 AL |
99 | if (strcmp(qdev_alias_table[i].typename, typename) == 0) { |
100 | return qdev_alias_table[i].alias; | |
101 | } | |
102 | } | |
103 | ||
104 | return NULL; | |
105 | } | |
106 | ||
107 | static bool qdev_class_has_alias(DeviceClass *dc) | |
108 | { | |
109 | return (qdev_class_get_alias(dc) != NULL); | |
110 | } | |
111 | ||
a3400aee | 112 | static void qdev_print_devinfo(DeviceClass *dc) |
ee46d8a5 | 113 | { |
8acb2a75 | 114 | qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc))); |
0d936928 | 115 | if (dc->bus_type) { |
8acb2a75 | 116 | qemu_printf(", bus %s", dc->bus_type); |
ee46d8a5 AL |
117 | } |
118 | if (qdev_class_has_alias(dc)) { | |
8acb2a75 | 119 | qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc)); |
ee46d8a5 AL |
120 | } |
121 | if (dc->desc) { | |
8acb2a75 | 122 | qemu_printf(", desc \"%s\"", dc->desc); |
ee46d8a5 | 123 | } |
e90f2a8c | 124 | if (!dc->user_creatable) { |
8acb2a75 | 125 | qemu_printf(", no-user"); |
ee46d8a5 | 126 | } |
8acb2a75 | 127 | qemu_printf("\n"); |
ee46d8a5 AL |
128 | } |
129 | ||
a3400aee MA |
130 | static void qdev_print_devinfos(bool show_no_user) |
131 | { | |
132 | static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = { | |
133 | [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub", | |
134 | [DEVICE_CATEGORY_USB] = "USB", | |
135 | [DEVICE_CATEGORY_STORAGE] = "Storage", | |
136 | [DEVICE_CATEGORY_NETWORK] = "Network", | |
137 | [DEVICE_CATEGORY_INPUT] = "Input", | |
138 | [DEVICE_CATEGORY_DISPLAY] = "Display", | |
139 | [DEVICE_CATEGORY_SOUND] = "Sound", | |
140 | [DEVICE_CATEGORY_MISC] = "Misc", | |
ba31cc72 | 141 | [DEVICE_CATEGORY_CPU] = "CPU", |
a3400aee MA |
142 | [DEVICE_CATEGORY_MAX] = "Uncategorized", |
143 | }; | |
144 | GSList *list, *elt; | |
145 | int i; | |
146 | bool cat_printed; | |
147 | ||
47c66009 | 148 | list = object_class_get_list_sorted(TYPE_DEVICE, false); |
a3400aee MA |
149 | |
150 | for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) { | |
151 | cat_printed = false; | |
152 | for (elt = list; elt; elt = elt->next) { | |
153 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, | |
154 | TYPE_DEVICE); | |
155 | if ((i < DEVICE_CATEGORY_MAX | |
156 | ? !test_bit(i, dc->categories) | |
157 | : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX)) | |
efec3dd6 | 158 | || (!show_no_user |
e90f2a8c | 159 | && !dc->user_creatable)) { |
a3400aee MA |
160 | continue; |
161 | } | |
162 | if (!cat_printed) { | |
8acb2a75 | 163 | qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]); |
a3400aee MA |
164 | cat_printed = true; |
165 | } | |
166 | qdev_print_devinfo(dc); | |
167 | } | |
168 | } | |
169 | ||
170 | g_slist_free(list); | |
171 | } | |
172 | ||
71df1d83 MA |
173 | static int set_property(void *opaque, const char *name, const char *value, |
174 | Error **errp) | |
ee46d8a5 | 175 | { |
98a65284 | 176 | Object *obj = opaque; |
b1fe9bcb | 177 | Error *err = NULL; |
ee46d8a5 AL |
178 | |
179 | if (strcmp(name, "driver") == 0) | |
180 | return 0; | |
181 | if (strcmp(name, "bus") == 0) | |
182 | return 0; | |
183 | ||
98a65284 | 184 | object_property_parse(obj, value, name, &err); |
b1fe9bcb | 185 | if (err != NULL) { |
4caa489d | 186 | error_propagate(errp, err); |
ee46d8a5 AL |
187 | return -1; |
188 | } | |
189 | return 0; | |
190 | } | |
191 | ||
192 | static const char *find_typename_by_alias(const char *alias) | |
193 | { | |
194 | int i; | |
195 | ||
196 | for (i = 0; qdev_alias_table[i].alias; i++) { | |
5f629d94 AG |
197 | if (qdev_alias_table[i].arch_mask && |
198 | !(qdev_alias_table[i].arch_mask & arch_type)) { | |
199 | continue; | |
200 | } | |
201 | ||
ee46d8a5 AL |
202 | if (strcmp(qdev_alias_table[i].alias, alias) == 0) { |
203 | return qdev_alias_table[i].typename; | |
204 | } | |
205 | } | |
206 | ||
207 | return NULL; | |
208 | } | |
209 | ||
43c95d78 EH |
210 | static DeviceClass *qdev_get_device_class(const char **driver, Error **errp) |
211 | { | |
212 | ObjectClass *oc; | |
213 | DeviceClass *dc; | |
f6b5319d | 214 | const char *original_name = *driver; |
43c95d78 EH |
215 | |
216 | oc = object_class_by_name(*driver); | |
217 | if (!oc) { | |
218 | const char *typename = find_typename_by_alias(*driver); | |
219 | ||
220 | if (typename) { | |
221 | *driver = typename; | |
222 | oc = object_class_by_name(*driver); | |
223 | } | |
224 | } | |
225 | ||
226 | if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) { | |
f6b5319d SS |
227 | if (*driver != original_name) { |
228 | error_setg(errp, "'%s' (alias '%s') is not a valid device model" | |
229 | " name", original_name, *driver); | |
230 | } else { | |
231 | error_setg(errp, "'%s' is not a valid device model name", *driver); | |
232 | } | |
43c95d78 EH |
233 | return NULL; |
234 | } | |
235 | ||
236 | if (object_class_is_abstract(oc)) { | |
c6bd8c70 MA |
237 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", |
238 | "non-abstract device type"); | |
43c95d78 EH |
239 | return NULL; |
240 | } | |
241 | ||
242 | dc = DEVICE_CLASS(oc); | |
e90f2a8c | 243 | if (!dc->user_creatable || |
43c95d78 | 244 | (qdev_hotplug && !dc->hotpluggable)) { |
c6bd8c70 MA |
245 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", |
246 | "pluggable device type"); | |
43c95d78 EH |
247 | return NULL; |
248 | } | |
249 | ||
250 | return dc; | |
251 | } | |
252 | ||
253 | ||
ee46d8a5 AL |
254 | int qdev_device_help(QemuOpts *opts) |
255 | { | |
ef523587 | 256 | Error *local_err = NULL; |
ee46d8a5 | 257 | const char *driver; |
35f63767 AK |
258 | ObjectPropertyInfoList *prop_list; |
259 | ObjectPropertyInfoList *prop; | |
e1043d67 MAL |
260 | GPtrArray *array; |
261 | int i; | |
ee46d8a5 AL |
262 | |
263 | driver = qemu_opt_get(opts, "driver"); | |
c8057f95 | 264 | if (driver && is_help_option(driver)) { |
a3400aee | 265 | qdev_print_devinfos(false); |
ee46d8a5 AL |
266 | return 1; |
267 | } | |
268 | ||
c8057f95 | 269 | if (!driver || !qemu_opt_has_help_opt(opts)) { |
ee46d8a5 AL |
270 | return 0; |
271 | } | |
272 | ||
33fe9683 MA |
273 | if (!object_class_by_name(driver)) { |
274 | const char *typename = find_typename_by_alias(driver); | |
275 | ||
276 | if (typename) { | |
277 | driver = typename; | |
278 | } | |
ee46d8a5 AL |
279 | } |
280 | ||
ef523587 | 281 | prop_list = qmp_device_list_properties(driver, &local_err); |
0722eba9 | 282 | if (local_err) { |
5185f0e0 | 283 | goto error; |
ee46d8a5 | 284 | } |
ef523587 | 285 | |
9c2762b4 | 286 | if (prop_list) { |
8acb2a75 | 287 | qemu_printf("%s options:\n", driver); |
9c2762b4 | 288 | } else { |
8acb2a75 | 289 | qemu_printf("There are no options for %s.\n", driver); |
9c2762b4 | 290 | } |
e1043d67 | 291 | array = g_ptr_array_new(); |
ef523587 | 292 | for (prop = prop_list; prop; prop = prop->next) { |
e1043d67 MAL |
293 | g_ptr_array_add(array, |
294 | object_property_help(prop->value->name, | |
295 | prop->value->type, | |
296 | prop->value->default_value, | |
297 | prop->value->description)); | |
ef523587 | 298 | } |
e1043d67 MAL |
299 | g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0); |
300 | for (i = 0; i < array->len; i++) { | |
301 | printf("%s\n", (char *)array->pdata[i]); | |
302 | } | |
303 | g_ptr_array_set_free_func(array, g_free); | |
304 | g_ptr_array_free(array, true); | |
35f63767 | 305 | qapi_free_ObjectPropertyInfoList(prop_list); |
ee46d8a5 | 306 | return 1; |
5185f0e0 EH |
307 | |
308 | error: | |
78288671 | 309 | error_report_err(local_err); |
5185f0e0 | 310 | return 1; |
ee46d8a5 AL |
311 | } |
312 | ||
57c9fafe | 313 | static Object *qdev_get_peripheral(void) |
ee46d8a5 | 314 | { |
8b45d447 | 315 | static Object *dev; |
ee46d8a5 AL |
316 | |
317 | if (dev == NULL) { | |
dfe47e70 | 318 | dev = container_get(qdev_get_machine(), "/peripheral"); |
ee46d8a5 AL |
319 | } |
320 | ||
8b45d447 | 321 | return dev; |
ee46d8a5 AL |
322 | } |
323 | ||
57c9fafe | 324 | static Object *qdev_get_peripheral_anon(void) |
ee46d8a5 | 325 | { |
8b45d447 | 326 | static Object *dev; |
ee46d8a5 AL |
327 | |
328 | if (dev == NULL) { | |
dfe47e70 | 329 | dev = container_get(qdev_get_machine(), "/peripheral-anon"); |
ee46d8a5 AL |
330 | } |
331 | ||
8b45d447 | 332 | return dev; |
ee46d8a5 AL |
333 | } |
334 | ||
34077326 VSO |
335 | static void qbus_error_append_bus_list_hint(DeviceState *dev, |
336 | Error *const *errp) | |
ee46d8a5 AL |
337 | { |
338 | BusState *child; | |
339 | const char *sep = " "; | |
340 | ||
50b7b000 EB |
341 | error_append_hint(errp, "child buses at \"%s\":", |
342 | dev->id ? dev->id : object_get_typename(OBJECT(dev))); | |
ee46d8a5 | 343 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
50b7b000 | 344 | error_append_hint(errp, "%s\"%s\"", sep, child->name); |
ee46d8a5 AL |
345 | sep = ", "; |
346 | } | |
543202c0 | 347 | error_append_hint(errp, "\n"); |
ee46d8a5 AL |
348 | } |
349 | ||
34077326 VSO |
350 | static void qbus_error_append_dev_list_hint(BusState *bus, |
351 | Error *const *errp) | |
ee46d8a5 | 352 | { |
0866aca1 | 353 | BusChild *kid; |
ee46d8a5 AL |
354 | const char *sep = " "; |
355 | ||
50b7b000 | 356 | error_append_hint(errp, "devices at \"%s\":", bus->name); |
0866aca1 AL |
357 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
358 | DeviceState *dev = kid->child; | |
50b7b000 EB |
359 | error_append_hint(errp, "%s\"%s\"", sep, |
360 | object_get_typename(OBJECT(dev))); | |
361 | if (dev->id) { | |
362 | error_append_hint(errp, "/\"%s\"", dev->id); | |
363 | } | |
ee46d8a5 AL |
364 | sep = ", "; |
365 | } | |
543202c0 | 366 | error_append_hint(errp, "\n"); |
ee46d8a5 AL |
367 | } |
368 | ||
369 | static BusState *qbus_find_bus(DeviceState *dev, char *elem) | |
370 | { | |
371 | BusState *child; | |
372 | ||
373 | QLIST_FOREACH(child, &dev->child_bus, sibling) { | |
374 | if (strcmp(child->name, elem) == 0) { | |
375 | return child; | |
376 | } | |
377 | } | |
378 | return NULL; | |
379 | } | |
380 | ||
381 | static DeviceState *qbus_find_dev(BusState *bus, char *elem) | |
382 | { | |
0866aca1 | 383 | BusChild *kid; |
ee46d8a5 AL |
384 | |
385 | /* | |
386 | * try to match in order: | |
387 | * (1) instance id, if present | |
388 | * (2) driver name | |
389 | * (3) driver alias, if present | |
390 | */ | |
0866aca1 AL |
391 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
392 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
393 | if (dev->id && strcmp(dev->id, elem) == 0) { |
394 | return dev; | |
395 | } | |
396 | } | |
0866aca1 AL |
397 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
398 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
399 | if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) { |
400 | return dev; | |
401 | } | |
402 | } | |
0866aca1 AL |
403 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
404 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
405 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
406 | ||
407 | if (qdev_class_has_alias(dc) && | |
408 | strcmp(qdev_class_get_alias(dc), elem) == 0) { | |
409 | return dev; | |
410 | } | |
411 | } | |
412 | return NULL; | |
413 | } | |
414 | ||
a5ec494e MA |
415 | static inline bool qbus_is_full(BusState *bus) |
416 | { | |
417 | BusClass *bus_class = BUS_GET_CLASS(bus); | |
12b2e9f3 | 418 | return bus_class->max_dev && bus->num_children >= bus_class->max_dev; |
a5ec494e MA |
419 | } |
420 | ||
421 | /* | |
422 | * Search the tree rooted at @bus for a bus. | |
423 | * If @name, search for a bus with that name. Note that bus names | |
424 | * need not be unique. Yes, that's screwed up. | |
425 | * Else search for a bus that is a subtype of @bus_typename. | |
426 | * If more than one exists, prefer one that can take another device. | |
427 | * Return the bus if found, else %NULL. | |
428 | */ | |
ee46d8a5 | 429 | static BusState *qbus_find_recursive(BusState *bus, const char *name, |
0d936928 | 430 | const char *bus_typename) |
ee46d8a5 | 431 | { |
0866aca1 | 432 | BusChild *kid; |
a5ec494e MA |
433 | BusState *pick, *child, *ret; |
434 | bool match; | |
435 | ||
436 | assert(name || bus_typename); | |
437 | if (name) { | |
438 | match = !strcmp(bus->name, name); | |
439 | } else { | |
440 | match = !!object_dynamic_cast(OBJECT(bus), bus_typename); | |
1395af6f | 441 | } |
a5ec494e MA |
442 | |
443 | if (match && !qbus_is_full(bus)) { | |
444 | return bus; /* root matches and isn't full */ | |
ee46d8a5 AL |
445 | } |
446 | ||
a5ec494e MA |
447 | pick = match ? bus : NULL; |
448 | ||
0866aca1 AL |
449 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
450 | DeviceState *dev = kid->child; | |
ee46d8a5 | 451 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
0d936928 | 452 | ret = qbus_find_recursive(child, name, bus_typename); |
a5ec494e MA |
453 | if (ret && !qbus_is_full(ret)) { |
454 | return ret; /* a descendant matches and isn't full */ | |
455 | } | |
456 | if (ret && !pick) { | |
457 | pick = ret; | |
ee46d8a5 AL |
458 | } |
459 | } | |
460 | } | |
a5ec494e MA |
461 | |
462 | /* root or a descendant matches, but is full */ | |
463 | return pick; | |
ee46d8a5 AL |
464 | } |
465 | ||
d2828429 | 466 | static BusState *qbus_find(const char *path, Error **errp) |
ee46d8a5 AL |
467 | { |
468 | DeviceState *dev; | |
469 | BusState *bus; | |
470 | char elem[128]; | |
471 | int pos, len; | |
472 | ||
473 | /* find start element */ | |
474 | if (path[0] == '/') { | |
475 | bus = sysbus_get_default(); | |
476 | pos = 0; | |
477 | } else { | |
478 | if (sscanf(path, "%127[^/]%n", elem, &len) != 1) { | |
479 | assert(!path[0]); | |
480 | elem[0] = len = 0; | |
481 | } | |
482 | bus = qbus_find_recursive(sysbus_get_default(), elem, NULL); | |
483 | if (!bus) { | |
d2828429 | 484 | error_setg(errp, "Bus '%s' not found", elem); |
ee46d8a5 AL |
485 | return NULL; |
486 | } | |
487 | pos = len; | |
488 | } | |
489 | ||
490 | for (;;) { | |
491 | assert(path[pos] == '/' || !path[pos]); | |
492 | while (path[pos] == '/') { | |
493 | pos++; | |
494 | } | |
495 | if (path[pos] == '\0') { | |
ed238ba2 | 496 | break; |
ee46d8a5 AL |
497 | } |
498 | ||
499 | /* find device */ | |
500 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { | |
dfc6f865 | 501 | g_assert_not_reached(); |
ee46d8a5 AL |
502 | elem[0] = len = 0; |
503 | } | |
504 | pos += len; | |
505 | dev = qbus_find_dev(bus, elem); | |
506 | if (!dev) { | |
75158ebb MA |
507 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
508 | "Device '%s' not found", elem); | |
34077326 | 509 | qbus_error_append_dev_list_hint(bus, errp); |
ee46d8a5 AL |
510 | return NULL; |
511 | } | |
512 | ||
513 | assert(path[pos] == '/' || !path[pos]); | |
514 | while (path[pos] == '/') { | |
515 | pos++; | |
516 | } | |
517 | if (path[pos] == '\0') { | |
518 | /* last specified element is a device. If it has exactly | |
519 | * one child bus accept it nevertheless */ | |
ed238ba2 MA |
520 | if (dev->num_child_bus == 1) { |
521 | bus = QLIST_FIRST(&dev->child_bus); | |
522 | break; | |
523 | } | |
524 | if (dev->num_child_bus) { | |
d2828429 MA |
525 | error_setg(errp, "Device '%s' has multiple child buses", |
526 | elem); | |
34077326 | 527 | qbus_error_append_bus_list_hint(dev, errp); |
ed238ba2 | 528 | } else { |
d2828429 | 529 | error_setg(errp, "Device '%s' has no child bus", elem); |
ee46d8a5 | 530 | } |
ed238ba2 | 531 | return NULL; |
ee46d8a5 AL |
532 | } |
533 | ||
534 | /* find bus */ | |
535 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { | |
dfc6f865 | 536 | g_assert_not_reached(); |
ee46d8a5 AL |
537 | elem[0] = len = 0; |
538 | } | |
539 | pos += len; | |
540 | bus = qbus_find_bus(dev, elem); | |
541 | if (!bus) { | |
d2828429 | 542 | error_setg(errp, "Bus '%s' not found", elem); |
34077326 | 543 | qbus_error_append_bus_list_hint(dev, errp); |
ee46d8a5 AL |
544 | return NULL; |
545 | } | |
546 | } | |
ed238ba2 MA |
547 | |
548 | if (qbus_is_full(bus)) { | |
d2828429 | 549 | error_setg(errp, "Bus '%s' is full", path); |
ed238ba2 MA |
550 | return NULL; |
551 | } | |
552 | return bus; | |
ee46d8a5 AL |
553 | } |
554 | ||
ce49b734 JG |
555 | void qdev_set_id(DeviceState *dev, const char *id) |
556 | { | |
557 | if (id) { | |
558 | dev->id = id; | |
559 | } | |
560 | ||
561 | if (dev->id) { | |
562 | object_property_add_child(qdev_get_peripheral(), dev->id, | |
563 | OBJECT(dev), NULL); | |
564 | } else { | |
565 | static int anon_count; | |
566 | gchar *name = g_strdup_printf("device[%d]", anon_count++); | |
567 | object_property_add_child(qdev_get_peripheral_anon(), name, | |
568 | OBJECT(dev), NULL); | |
569 | g_free(name); | |
570 | } | |
571 | } | |
572 | ||
f3a85056 JF |
573 | static int is_failover_device(void *opaque, const char *name, const char *value, |
574 | Error **errp) | |
575 | { | |
576 | if (strcmp(name, "failover_pair_id") == 0) { | |
577 | QemuOpts *opts = (QemuOpts *)opaque; | |
578 | ||
579 | if (qdev_should_hide_device(opts)) { | |
580 | return 1; | |
581 | } | |
582 | } | |
583 | ||
584 | return 0; | |
585 | } | |
586 | ||
587 | static bool should_hide_device(QemuOpts *opts) | |
588 | { | |
589 | if (qemu_opt_foreach(opts, is_failover_device, opts, NULL) == 0) { | |
590 | return false; | |
591 | } | |
592 | return true; | |
593 | } | |
594 | ||
f006cf7f | 595 | DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) |
ee46d8a5 | 596 | { |
f4d85795 | 597 | DeviceClass *dc; |
ce49b734 | 598 | const char *driver, *path; |
f3a85056 | 599 | DeviceState *dev = NULL; |
2f7bd829 | 600 | BusState *bus = NULL; |
852e2c50 | 601 | Error *err = NULL; |
f3a85056 | 602 | bool hide; |
ee46d8a5 AL |
603 | |
604 | driver = qemu_opt_get(opts, "driver"); | |
605 | if (!driver) { | |
c6bd8c70 | 606 | error_setg(errp, QERR_MISSING_PARAMETER, "driver"); |
ee46d8a5 AL |
607 | return NULL; |
608 | } | |
609 | ||
610 | /* find driver */ | |
f006cf7f MA |
611 | dc = qdev_get_device_class(&driver, errp); |
612 | if (!dc) { | |
7ea5e78f MA |
613 | return NULL; |
614 | } | |
ee46d8a5 AL |
615 | |
616 | /* find bus */ | |
617 | path = qemu_opt_get(opts, "bus"); | |
618 | if (path != NULL) { | |
f006cf7f | 619 | bus = qbus_find(path, errp); |
ee46d8a5 AL |
620 | if (!bus) { |
621 | return NULL; | |
622 | } | |
f4d85795 | 623 | if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) { |
f006cf7f MA |
624 | error_setg(errp, "Device '%s' can't go on %s bus", |
625 | driver, object_get_typename(OBJECT(bus))); | |
ee46d8a5 AL |
626 | return NULL; |
627 | } | |
f4d85795 AF |
628 | } else if (dc->bus_type != NULL) { |
629 | bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type); | |
a5ec494e | 630 | if (!bus || qbus_is_full(bus)) { |
f006cf7f MA |
631 | error_setg(errp, "No '%s' bus found for device '%s'", |
632 | dc->bus_type, driver); | |
ee46d8a5 AL |
633 | return NULL; |
634 | } | |
635 | } | |
f3a85056 JF |
636 | hide = should_hide_device(opts); |
637 | ||
638 | if ((hide || qdev_hotplug) && bus && !qbus_is_hotpluggable(bus)) { | |
c6bd8c70 | 639 | error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name); |
ee46d8a5 AL |
640 | return NULL; |
641 | } | |
642 | ||
f3a85056 JF |
643 | if (hide) { |
644 | return NULL; | |
645 | } | |
646 | ||
b06424de JQ |
647 | if (!migration_is_idle()) { |
648 | error_setg(errp, "device_add not allowed while migrating"); | |
649 | return NULL; | |
650 | } | |
651 | ||
7b030949 | 652 | /* create device */ |
2bcb0c62 | 653 | dev = DEVICE(object_new(driver)); |
2f7bd829 | 654 | |
d2321d31 PX |
655 | /* Check whether the hotplug is allowed by the machine */ |
656 | if (qdev_hotplug && !qdev_hotplug_allowed(dev, &err)) { | |
657 | /* Error must be set in the machine hook */ | |
658 | assert(err); | |
659 | goto err_del_dev; | |
660 | } | |
661 | ||
2f7bd829 | 662 | if (bus) { |
2bcb0c62 | 663 | qdev_set_parent_bus(dev, bus); |
03fcbd9d TH |
664 | } else if (qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) { |
665 | /* No bus, no machine hotplug handler --> device is not hotpluggable */ | |
666 | error_setg(&err, "Device '%s' can not be hotplugged on this machine", | |
667 | driver); | |
668 | goto err_del_dev; | |
2f7bd829 | 669 | } |
ee46d8a5 | 670 | |
ce49b734 | 671 | qdev_set_id(dev, qemu_opts_id(opts)); |
52aa17cb | 672 | |
7b030949 | 673 | /* set properties */ |
4caa489d | 674 | if (qemu_opt_foreach(opts, set_property, dev, &err)) { |
58346214 | 675 | goto err_del_dev; |
7b030949 AK |
676 | } |
677 | ||
52aa17cb | 678 | dev->opts = opts; |
852e2c50 AF |
679 | object_property_set_bool(OBJECT(dev), true, "realized", &err); |
680 | if (err != NULL) { | |
52aa17cb | 681 | dev->opts = NULL; |
58346214 | 682 | goto err_del_dev; |
f424d5c4 | 683 | } |
2bcb0c62 | 684 | return dev; |
58346214 TH |
685 | |
686 | err_del_dev: | |
687 | error_propagate(errp, err); | |
f3a85056 JF |
688 | if (dev) { |
689 | object_unparent(OBJECT(dev)); | |
690 | object_unref(OBJECT(dev)); | |
691 | } | |
58346214 | 692 | return NULL; |
ee46d8a5 AL |
693 | } |
694 | ||
695 | ||
696 | #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__) | |
697 | static void qbus_print(Monitor *mon, BusState *bus, int indent); | |
698 | ||
699 | static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props, | |
bce54474 | 700 | int indent) |
ee46d8a5 | 701 | { |
ee46d8a5 AL |
702 | if (!props) |
703 | return; | |
d822979b PB |
704 | for (; props->name; props++) { |
705 | Error *err = NULL; | |
706 | char *value; | |
707 | char *legacy_name = g_strdup_printf("legacy-%s", props->name); | |
708 | if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) { | |
709 | value = object_property_get_str(OBJECT(dev), legacy_name, &err); | |
710 | } else { | |
dae3bda4 | 711 | value = object_property_print(OBJECT(dev), props->name, true, &err); |
d822979b PB |
712 | } |
713 | g_free(legacy_name); | |
714 | ||
715 | if (err) { | |
716 | error_free(err); | |
717 | continue; | |
ee46d8a5 | 718 | } |
bce54474 | 719 | qdev_printf("%s = %s\n", props->name, |
d822979b PB |
720 | value && *value ? value : "<null>"); |
721 | g_free(value); | |
ee46d8a5 AL |
722 | } |
723 | } | |
724 | ||
0d936928 AL |
725 | static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent) |
726 | { | |
727 | BusClass *bc = BUS_GET_CLASS(bus); | |
728 | ||
729 | if (bc->print_dev) { | |
730 | bc->print_dev(mon, dev, indent); | |
731 | } | |
732 | } | |
733 | ||
ee46d8a5 AL |
734 | static void qdev_print(Monitor *mon, DeviceState *dev, int indent) |
735 | { | |
bce54474 | 736 | ObjectClass *class; |
ee46d8a5 | 737 | BusState *child; |
a5f54290 PC |
738 | NamedGPIOList *ngl; |
739 | ||
ee46d8a5 AL |
740 | qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), |
741 | dev->id ? dev->id : ""); | |
742 | indent += 2; | |
a5f54290 PC |
743 | QLIST_FOREACH(ngl, &dev->gpios, node) { |
744 | if (ngl->num_in) { | |
745 | qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "", | |
746 | ngl->num_in); | |
747 | } | |
748 | if (ngl->num_out) { | |
749 | qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "", | |
750 | ngl->num_out); | |
751 | } | |
ee46d8a5 | 752 | } |
bce54474 PB |
753 | class = object_get_class(OBJECT(dev)); |
754 | do { | |
385d8f22 | 755 | qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent); |
bce54474 PB |
756 | class = object_class_get_parent(class); |
757 | } while (class != object_class_by_name(TYPE_DEVICE)); | |
da9fbe76 | 758 | bus_print_dev(dev->parent_bus, mon, dev, indent); |
ee46d8a5 AL |
759 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
760 | qbus_print(mon, child, indent); | |
761 | } | |
762 | } | |
763 | ||
764 | static void qbus_print(Monitor *mon, BusState *bus, int indent) | |
765 | { | |
0866aca1 | 766 | BusChild *kid; |
ee46d8a5 AL |
767 | |
768 | qdev_printf("bus: %s\n", bus->name); | |
769 | indent += 2; | |
0d936928 | 770 | qdev_printf("type %s\n", object_get_typename(OBJECT(bus))); |
0866aca1 AL |
771 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
772 | DeviceState *dev = kid->child; | |
ee46d8a5 AL |
773 | qdev_print(mon, dev, indent); |
774 | } | |
775 | } | |
776 | #undef qdev_printf | |
777 | ||
1ce6be24 | 778 | void hmp_info_qtree(Monitor *mon, const QDict *qdict) |
ee46d8a5 AL |
779 | { |
780 | if (sysbus_get_default()) | |
781 | qbus_print(mon, sysbus_get_default(), 0); | |
782 | } | |
783 | ||
1ce6be24 | 784 | void hmp_info_qdm(Monitor *mon, const QDict *qdict) |
ee46d8a5 | 785 | { |
a3400aee | 786 | qdev_print_devinfos(true); |
ee46d8a5 AL |
787 | } |
788 | ||
485febc6 | 789 | void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp) |
ee46d8a5 | 790 | { |
4e89978e | 791 | Error *local_err = NULL; |
ee46d8a5 | 792 | QemuOpts *opts; |
b09995ae | 793 | DeviceState *dev; |
ee46d8a5 | 794 | |
4e89978e | 795 | opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err); |
84d18f06 | 796 | if (local_err) { |
485febc6 MA |
797 | error_propagate(errp, local_err); |
798 | return; | |
ee46d8a5 AL |
799 | } |
800 | if (!monitor_cur_is_qmp() && qdev_device_help(opts)) { | |
801 | qemu_opts_del(opts); | |
485febc6 | 802 | return; |
ee46d8a5 | 803 | } |
f006cf7f | 804 | dev = qdev_device_add(opts, &local_err); |
b09995ae | 805 | if (!dev) { |
485febc6 | 806 | error_propagate(errp, local_err); |
ee46d8a5 | 807 | qemu_opts_del(opts); |
485febc6 | 808 | return; |
ee46d8a5 | 809 | } |
b09995ae | 810 | object_unref(OBJECT(dev)); |
ee46d8a5 AL |
811 | } |
812 | ||
6c1db528 | 813 | static DeviceState *find_device_state(const char *id, Error **errp) |
ee46d8a5 | 814 | { |
b6cc36ab | 815 | Object *obj; |
ee46d8a5 | 816 | |
6287d827 DB |
817 | if (id[0] == '/') { |
818 | obj = object_resolve_path(id, NULL); | |
819 | } else { | |
820 | char *root_path = object_get_canonical_path(qdev_get_peripheral()); | |
821 | char *path = g_strdup_printf("%s/%s", root_path, id); | |
822 | ||
823 | g_free(root_path); | |
824 | obj = object_resolve_path_type(path, TYPE_DEVICE, NULL); | |
825 | g_free(path); | |
826 | } | |
b6cc36ab IM |
827 | |
828 | if (!obj) { | |
75158ebb MA |
829 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
830 | "Device '%s' not found", id); | |
6c1db528 | 831 | return NULL; |
56f9107e LC |
832 | } |
833 | ||
6287d827 DB |
834 | if (!object_dynamic_cast(obj, TYPE_DEVICE)) { |
835 | error_setg(errp, "%s is not a hotpluggable device", id); | |
6c1db528 | 836 | return NULL; |
6287d827 DB |
837 | } |
838 | ||
6c1db528 KW |
839 | return DEVICE(obj); |
840 | } | |
841 | ||
32900679 JQ |
842 | void qdev_unplug(DeviceState *dev, Error **errp) |
843 | { | |
844 | DeviceClass *dc = DEVICE_GET_CLASS(dev); | |
845 | HotplugHandler *hotplug_ctrl; | |
846 | HotplugHandlerClass *hdc; | |
07578b0a | 847 | Error *local_err = NULL; |
32900679 JQ |
848 | |
849 | if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) { | |
850 | error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name); | |
851 | return; | |
852 | } | |
853 | ||
854 | if (!dc->hotpluggable) { | |
855 | error_setg(errp, QERR_DEVICE_NO_HOTPLUG, | |
856 | object_get_typename(OBJECT(dev))); | |
857 | return; | |
858 | } | |
859 | ||
a1190ab6 | 860 | if (!migration_is_idle() && !dev->allow_unplug_during_migration) { |
b06424de JQ |
861 | error_setg(errp, "device_del not allowed while migrating"); |
862 | return; | |
863 | } | |
864 | ||
32900679 JQ |
865 | qdev_hot_removed = true; |
866 | ||
867 | hotplug_ctrl = qdev_get_hotplug_handler(dev); | |
868 | /* hotpluggable device MUST have HotplugHandler, if it doesn't | |
869 | * then something is very wrong with it */ | |
870 | g_assert(hotplug_ctrl); | |
871 | ||
872 | /* If device supports async unplug just request it to be done, | |
873 | * otherwise just remove it synchronously */ | |
874 | hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl); | |
875 | if (hdc->unplug_request) { | |
07578b0a | 876 | hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err); |
32900679 | 877 | } else { |
07578b0a DH |
878 | hotplug_handler_unplug(hotplug_ctrl, dev, &local_err); |
879 | if (!local_err) { | |
880 | object_unparent(OBJECT(dev)); | |
881 | } | |
32900679 | 882 | } |
07578b0a | 883 | error_propagate(errp, local_err); |
32900679 JQ |
884 | } |
885 | ||
6c1db528 KW |
886 | void qmp_device_del(const char *id, Error **errp) |
887 | { | |
888 | DeviceState *dev = find_device_state(id, errp); | |
889 | if (dev != NULL) { | |
890 | qdev_unplug(dev, errp); | |
891 | } | |
ee46d8a5 AL |
892 | } |
893 | ||
d94bd092 MA |
894 | void hmp_device_add(Monitor *mon, const QDict *qdict) |
895 | { | |
896 | Error *err = NULL; | |
897 | ||
898 | qmp_device_add((QDict *)qdict, NULL, &err); | |
187c6147 | 899 | hmp_handle_error(mon, err); |
d94bd092 MA |
900 | } |
901 | ||
902 | void hmp_device_del(Monitor *mon, const QDict *qdict) | |
903 | { | |
904 | const char *id = qdict_get_str(qdict, "id"); | |
905 | Error *err = NULL; | |
906 | ||
907 | qmp_device_del(id, &err); | |
187c6147 | 908 | hmp_handle_error(mon, err); |
d94bd092 MA |
909 | } |
910 | ||
9680caee KW |
911 | BlockBackend *blk_by_qdev_id(const char *id, Error **errp) |
912 | { | |
913 | DeviceState *dev; | |
914 | BlockBackend *blk; | |
915 | ||
916 | dev = find_device_state(id, errp); | |
917 | if (dev == NULL) { | |
918 | return NULL; | |
919 | } | |
920 | ||
921 | blk = blk_by_dev(dev); | |
922 | if (!blk) { | |
923 | error_setg(errp, "Device does not have a block device backend"); | |
924 | } | |
925 | return blk; | |
926 | } | |
927 | ||
ee46d8a5 AL |
928 | void qdev_machine_init(void) |
929 | { | |
930 | qdev_get_peripheral_anon(); | |
931 | qdev_get_peripheral(); | |
932 | } | |
4d454574 PB |
933 | |
934 | QemuOptsList qemu_device_opts = { | |
935 | .name = "device", | |
936 | .implied_opt_name = "driver", | |
937 | .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head), | |
938 | .desc = { | |
939 | /* | |
940 | * no elements => accept any | |
941 | * sanity checking will happen later | |
942 | * when setting device properties | |
943 | */ | |
944 | { /* end of list */ } | |
945 | }, | |
946 | }; | |
947 | ||
948 | QemuOptsList qemu_global_opts = { | |
949 | .name = "global", | |
950 | .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head), | |
951 | .desc = { | |
952 | { | |
953 | .name = "driver", | |
954 | .type = QEMU_OPT_STRING, | |
955 | },{ | |
956 | .name = "property", | |
957 | .type = QEMU_OPT_STRING, | |
958 | },{ | |
959 | .name = "value", | |
960 | .type = QEMU_OPT_STRING, | |
961 | }, | |
962 | { /* end of list */ } | |
963 | }, | |
964 | }; | |
965 | ||
966 | int qemu_global_option(const char *str) | |
967 | { | |
968 | char driver[64], property[64]; | |
969 | QemuOpts *opts; | |
970 | int rc, offset; | |
971 | ||
3751d7c4 PB |
972 | rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset); |
973 | if (rc == 2 && str[offset] == '=') { | |
974 | opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort); | |
975 | qemu_opt_set(opts, "driver", driver, &error_abort); | |
976 | qemu_opt_set(opts, "property", property, &error_abort); | |
977 | qemu_opt_set(opts, "value", str + offset + 1, &error_abort); | |
978 | return 0; | |
979 | } | |
980 | ||
70b94331 | 981 | opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false); |
3751d7c4 | 982 | if (!opts) { |
4d454574 PB |
983 | return -1; |
984 | } | |
985 | ||
4d454574 PB |
986 | return 0; |
987 | } |