/src/qemu/system/qdev-monitor.c
Line | Count | Source |
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.1 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 "qemu/osdep.h" |
21 | | #include "hw/core/sysbus.h" |
22 | | #include "monitor/hmp.h" |
23 | | #include "monitor/hmp-completion.h" |
24 | | #include "monitor/monitor.h" |
25 | | #include "monitor/qdev.h" |
26 | | #include "qemu/base-arch-defs.h" |
27 | | #include "system/runstate.h" |
28 | | #include "qapi/error.h" |
29 | | #include "qapi/qapi-commands-qdev.h" |
30 | | #include "qapi/qmp-registry.h" |
31 | | #include "qobject/qdict.h" |
32 | | #include "qapi/qmp/qerror.h" |
33 | | #include "qobject/qstring.h" |
34 | | #include "qapi/qobject-input-visitor.h" |
35 | | #include "qemu/config-file.h" |
36 | | #include "qemu/error-report.h" |
37 | | #include "qemu/help_option.h" |
38 | | #include "qemu/id.h" |
39 | | #include "qemu/option.h" |
40 | | #include "qemu/qemu-print.h" |
41 | | #include "qemu/option_int.h" |
42 | | #include "system/block-backend.h" |
43 | | #include "migration/misc.h" |
44 | | #include "qemu/cutils.h" |
45 | | #include "hw/core/qdev-properties.h" |
46 | | #include "hw/core/clock.h" |
47 | | #include "hw/core/boards.h" |
48 | | |
49 | | /* |
50 | | * Aliases were a bad idea from the start. Let's keep them |
51 | | * from spreading further. |
52 | | */ |
53 | | typedef struct QDevAlias |
54 | | { |
55 | | const char *typename; |
56 | | const char *alias; |
57 | | uint64_t arch_mask; |
58 | | } QDevAlias; |
59 | | |
60 | | /* default virtio transport per architecture */ |
61 | | #define QEMU_ARCH_VIRTIO_PCI (QEMU_ARCH_ALPHA | \ |
62 | | QEMU_ARCH_ARM | \ |
63 | | QEMU_ARCH_HPPA | \ |
64 | | QEMU_ARCH_I386 | \ |
65 | | QEMU_ARCH_LOONGARCH | \ |
66 | | QEMU_ARCH_MIPS | \ |
67 | | QEMU_ARCH_OR1K | \ |
68 | | QEMU_ARCH_PPC | \ |
69 | | QEMU_ARCH_RISCV | \ |
70 | | QEMU_ARCH_SH4 | \ |
71 | | QEMU_ARCH_SPARC | \ |
72 | | QEMU_ARCH_XTENSA) |
73 | | #define QEMU_ARCH_VIRTIO_CCW (QEMU_ARCH_S390X) |
74 | | #define QEMU_ARCH_VIRTIO_MMIO (QEMU_ARCH_M68K | QEMU_ARCH_HEXAGON) |
75 | | |
76 | | /* Please keep this table sorted by typename. */ |
77 | | static const QDevAlias qdev_alias_table[] = { |
78 | | { "AC97", "ac97" }, |
79 | | { "e1000", "e1000-82540em" }, |
80 | | { "ES1370", "es1370" }, |
81 | | { "ich9-ahci", "ahci" }, |
82 | | { "lsi53c895a", "lsi" }, |
83 | | { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO }, |
84 | | { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW }, |
85 | | { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI }, |
86 | | { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO }, |
87 | | { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW }, |
88 | | { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI }, |
89 | | { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO }, |
90 | | { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW }, |
91 | | { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI }, |
92 | | { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO }, |
93 | | { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW }, |
94 | | { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI }, |
95 | | { "virtio-gpu-gl-device", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_MMIO }, |
96 | | { "virtio-gpu-gl-pci", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_PCI }, |
97 | | { "virtio-gpu-rutabaga-device", "virtio-gpu-rutabaga", |
98 | | QEMU_ARCH_VIRTIO_MMIO }, |
99 | | { "virtio-gpu-rutabaga-pci", "virtio-gpu-rutabaga", QEMU_ARCH_VIRTIO_PCI }, |
100 | | { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO }, |
101 | | { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW }, |
102 | | { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI }, |
103 | | { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI }, |
104 | | { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO }, |
105 | | { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW }, |
106 | | { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI }, |
107 | | { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO }, |
108 | | { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW }, |
109 | | { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI }, |
110 | | { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO }, |
111 | | { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW }, |
112 | | { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI }, |
113 | | { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO }, |
114 | | { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW }, |
115 | | { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI }, |
116 | | { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO }, |
117 | | { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW }, |
118 | | { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI }, |
119 | | { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO }, |
120 | | { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW }, |
121 | | { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI}, |
122 | | { "virtio-sound-device", "virtio-sound", QEMU_ARCH_VIRTIO_MMIO }, |
123 | | { "virtio-sound-pci", "virtio-sound", QEMU_ARCH_VIRTIO_PCI }, |
124 | | { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO }, |
125 | | { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW }, |
126 | | { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI }, |
127 | | { } |
128 | | }; |
129 | | |
130 | | static const char *qdev_class_get_alias(DeviceClass *dc) |
131 | 0 | { |
132 | 0 | const char *typename = object_class_get_name(OBJECT_CLASS(dc)); |
133 | 0 | int i; |
134 | |
|
135 | 0 | for (i = 0; qdev_alias_table[i].typename; i++) { |
136 | 0 | if (qdev_alias_table[i].arch_mask && |
137 | 0 | !qemu_arch_available(qdev_alias_table[i].arch_mask)) { |
138 | 0 | continue; |
139 | 0 | } |
140 | | |
141 | 0 | if (strcmp(qdev_alias_table[i].typename, typename) == 0) { |
142 | 0 | return qdev_alias_table[i].alias; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | 0 | return NULL; |
147 | 0 | } |
148 | | |
149 | | static bool qdev_class_has_alias(DeviceClass *dc) |
150 | 0 | { |
151 | 0 | return (qdev_class_get_alias(dc) != NULL); |
152 | 0 | } |
153 | | |
154 | | static void qdev_print_devinfo(DeviceClass *dc) |
155 | 0 | { |
156 | 0 | qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc))); |
157 | 0 | if (dc->bus_type) { |
158 | 0 | qemu_printf(", bus %s", dc->bus_type); |
159 | 0 | } |
160 | 0 | if (qdev_class_has_alias(dc)) { |
161 | 0 | qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc)); |
162 | 0 | } |
163 | 0 | if (dc->desc) { |
164 | 0 | qemu_printf(", desc \"%s\"", dc->desc); |
165 | 0 | } |
166 | 0 | if (!dc->user_creatable) { |
167 | 0 | qemu_printf(", no-user"); |
168 | 0 | } |
169 | 0 | if (object_class_is_secure(OBJECT_CLASS(dc))) { |
170 | 0 | qemu_printf(", secure"); |
171 | 0 | } |
172 | 0 | qemu_printf("\n"); |
173 | 0 | } |
174 | | |
175 | | static void qdev_print_devinfos(bool show_no_user) |
176 | 0 | { |
177 | 0 | static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = { |
178 | 0 | [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub", |
179 | 0 | [DEVICE_CATEGORY_USB] = "USB", |
180 | 0 | [DEVICE_CATEGORY_STORAGE] = "Storage", |
181 | 0 | [DEVICE_CATEGORY_NETWORK] = "Network", |
182 | 0 | [DEVICE_CATEGORY_INPUT] = "Input", |
183 | 0 | [DEVICE_CATEGORY_DISPLAY] = "Display", |
184 | 0 | [DEVICE_CATEGORY_SOUND] = "Sound", |
185 | 0 | [DEVICE_CATEGORY_MISC] = "Misc", |
186 | 0 | [DEVICE_CATEGORY_CPU] = "CPU", |
187 | 0 | [DEVICE_CATEGORY_WATCHDOG]= "Watchdog", |
188 | 0 | [DEVICE_CATEGORY_MAX] = "Uncategorized", |
189 | 0 | }; |
190 | 0 | GSList *list, *elt; |
191 | 0 | int i; |
192 | 0 | bool cat_printed; |
193 | |
|
194 | 0 | module_load_qom_all(); |
195 | 0 | list = object_class_get_list_sorted(TYPE_DEVICE, false); |
196 | |
|
197 | 0 | for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) { |
198 | 0 | cat_printed = false; |
199 | 0 | for (elt = list; elt; elt = elt->next) { |
200 | 0 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, |
201 | 0 | TYPE_DEVICE); |
202 | 0 | if ((i < DEVICE_CATEGORY_MAX |
203 | 0 | ? !test_bit(i, dc->categories) |
204 | 0 | : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX)) |
205 | 0 | || (!show_no_user |
206 | 0 | && !dc->user_creatable)) { |
207 | 0 | continue; |
208 | 0 | } |
209 | 0 | if (!cat_printed) { |
210 | 0 | qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]); |
211 | 0 | cat_printed = true; |
212 | 0 | } |
213 | 0 | qdev_print_devinfo(dc); |
214 | 0 | } |
215 | 0 | } |
216 | |
|
217 | 0 | g_slist_free(list); |
218 | 0 | } |
219 | | |
220 | | static const char *find_typename_by_alias(const char *alias) |
221 | 23 | { |
222 | 23 | int i; |
223 | | |
224 | 457 | for (i = 0; qdev_alias_table[i].alias; i++) { |
225 | 457 | if (qdev_alias_table[i].arch_mask && |
226 | 354 | !qemu_arch_available(qdev_alias_table[i].arch_mask)) { |
227 | 224 | continue; |
228 | 224 | } |
229 | | |
230 | 233 | if (strcmp(qdev_alias_table[i].alias, alias) == 0) { |
231 | 23 | return qdev_alias_table[i].typename; |
232 | 23 | } |
233 | 233 | } |
234 | | |
235 | 0 | return NULL; |
236 | 23 | } |
237 | | |
238 | | static DeviceClass *qdev_get_device_class(const char **driver, Error **errp) |
239 | 101 | { |
240 | 101 | ObjectClass *oc; |
241 | 101 | DeviceClass *dc; |
242 | 101 | const char *original_name = *driver; |
243 | | |
244 | 101 | oc = module_object_class_by_name(*driver); |
245 | 101 | if (!oc) { |
246 | 23 | const char *typename = find_typename_by_alias(*driver); |
247 | | |
248 | 23 | if (typename) { |
249 | 23 | *driver = typename; |
250 | 23 | oc = module_object_class_by_name(*driver); |
251 | 23 | } |
252 | 23 | } |
253 | | |
254 | 101 | if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) { |
255 | 0 | if (*driver != original_name) { |
256 | 0 | error_setg(errp, "'%s' (alias '%s') is not a valid device model" |
257 | 0 | " name", original_name, *driver); |
258 | 0 | } else { |
259 | 0 | error_setg(errp, "'%s' is not a valid device model name", *driver); |
260 | 0 | } |
261 | 0 | return NULL; |
262 | 0 | } |
263 | | |
264 | 101 | if (object_class_is_abstract(oc)) { |
265 | 0 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", |
266 | 0 | "a non-abstract device type"); |
267 | 0 | return NULL; |
268 | 0 | } |
269 | | |
270 | 101 | dc = DEVICE_CLASS(oc); |
271 | 101 | if (!dc->user_creatable) { |
272 | 0 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", |
273 | 0 | "a pluggable device type"); |
274 | 0 | return NULL; |
275 | 0 | } |
276 | | |
277 | 101 | if (object_class_dynamic_cast(oc, TYPE_SYS_BUS_DEVICE)) { |
278 | | /* sysbus devices need to be allowed by the machine */ |
279 | 0 | MachineClass *mc = MACHINE_CLASS(object_get_class(qdev_get_machine())); |
280 | 0 | if (!device_type_is_dynamic_sysbus(mc, *driver)) { |
281 | 0 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver", |
282 | 0 | "a dynamic sysbus device type for the machine"); |
283 | 0 | return NULL; |
284 | 0 | } |
285 | 0 | } |
286 | | |
287 | 101 | return dc; |
288 | 101 | } |
289 | | |
290 | | |
291 | | int qdev_device_help(QemuOpts *opts) |
292 | 101 | { |
293 | 101 | Error *local_err = NULL; |
294 | 101 | const char *driver; |
295 | 101 | ObjectPropertyInfoList *prop_list; |
296 | 101 | ObjectPropertyInfoList *prop; |
297 | 101 | GPtrArray *array; |
298 | 101 | int i; |
299 | | |
300 | 101 | driver = qemu_opt_get(opts, "driver"); |
301 | 101 | if (driver && is_help_option(driver)) { |
302 | 0 | qdev_print_devinfos(false); |
303 | 0 | return 1; |
304 | 0 | } |
305 | | |
306 | 101 | if (!driver || !qemu_opt_has_help_opt(opts)) { |
307 | 101 | return 0; |
308 | 101 | } |
309 | | |
310 | 0 | if (!object_class_by_name(driver)) { |
311 | 0 | const char *typename = find_typename_by_alias(driver); |
312 | |
|
313 | 0 | if (typename) { |
314 | 0 | driver = typename; |
315 | 0 | } |
316 | 0 | } |
317 | |
|
318 | 0 | prop_list = qmp_device_list_properties(driver, &local_err); |
319 | 0 | if (local_err) { |
320 | 0 | goto error; |
321 | 0 | } |
322 | | |
323 | 0 | if (prop_list) { |
324 | 0 | qemu_printf("%s options:\n", driver); |
325 | 0 | } else { |
326 | 0 | qemu_printf("There are no options for %s.\n", driver); |
327 | 0 | } |
328 | 0 | array = g_ptr_array_new(); |
329 | 0 | for (prop = prop_list; prop; prop = prop->next) { |
330 | 0 | g_ptr_array_add(array, |
331 | 0 | object_property_help(prop->value->name, |
332 | 0 | prop->value->type, |
333 | 0 | prop->value->default_value, |
334 | 0 | prop->value->description)); |
335 | 0 | } |
336 | 0 | g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0); |
337 | 0 | for (i = 0; i < array->len; i++) { |
338 | 0 | qemu_printf("%s\n", (char *)array->pdata[i]); |
339 | 0 | } |
340 | 0 | g_ptr_array_set_free_func(array, g_free); |
341 | 0 | g_ptr_array_free(array, true); |
342 | 0 | qapi_free_ObjectPropertyInfoList(prop_list); |
343 | 0 | return 1; |
344 | | |
345 | 0 | error: |
346 | 0 | error_report_err(local_err); |
347 | 0 | return 1; |
348 | 0 | } |
349 | | |
350 | | static Object *qdev_get_peripheral(void) |
351 | 9 | { |
352 | 9 | static Object *dev; |
353 | | |
354 | 9 | if (dev == NULL) { |
355 | 9 | dev = machine_get_container("peripheral"); |
356 | 9 | } |
357 | | |
358 | 9 | return dev; |
359 | 9 | } |
360 | | |
361 | | static Object *qdev_get_peripheral_anon(void) |
362 | 92 | { |
363 | 92 | static Object *dev; |
364 | | |
365 | 92 | if (dev == NULL) { |
366 | 57 | dev = machine_get_container("peripheral-anon"); |
367 | 57 | } |
368 | | |
369 | 92 | return dev; |
370 | 92 | } |
371 | | |
372 | | static void qbus_error_append_bus_list_hint(DeviceState *dev, |
373 | | Error *const *errp) |
374 | 0 | { |
375 | 0 | BusState *child; |
376 | 0 | const char *sep = " "; |
377 | |
|
378 | 0 | error_append_hint(errp, "child buses at \"%s\":", |
379 | 0 | dev->id ? dev->id : object_get_typename(OBJECT(dev))); |
380 | 0 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
381 | 0 | error_append_hint(errp, "%s\"%s\"", sep, child->name); |
382 | 0 | sep = ", "; |
383 | 0 | } |
384 | 0 | error_append_hint(errp, "\n"); |
385 | 0 | } |
386 | | |
387 | | static void qbus_error_append_dev_list_hint(BusState *bus, |
388 | | Error *const *errp) |
389 | 0 | { |
390 | 0 | BusChild *kid; |
391 | 0 | const char *sep = " "; |
392 | |
|
393 | 0 | error_append_hint(errp, "devices at \"%s\":", bus->name); |
394 | 0 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
395 | 0 | DeviceState *dev = kid->child; |
396 | 0 | error_append_hint(errp, "%s\"%s\"", sep, |
397 | 0 | object_get_typename(OBJECT(dev))); |
398 | 0 | if (dev->id) { |
399 | 0 | error_append_hint(errp, "/\"%s\"", dev->id); |
400 | 0 | } |
401 | 0 | sep = ", "; |
402 | 0 | } |
403 | 0 | error_append_hint(errp, "\n"); |
404 | 0 | } |
405 | | |
406 | | static BusState *qbus_find_bus(DeviceState *dev, char *elem) |
407 | 0 | { |
408 | 0 | BusState *child; |
409 | |
|
410 | 0 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
411 | 0 | if (strcmp(child->name, elem) == 0) { |
412 | 0 | return child; |
413 | 0 | } |
414 | 0 | } |
415 | 0 | return NULL; |
416 | 0 | } |
417 | | |
418 | | static DeviceState *qbus_find_dev(BusState *bus, char *elem) |
419 | 0 | { |
420 | 0 | BusChild *kid; |
421 | | |
422 | | /* |
423 | | * try to match in order: |
424 | | * (1) instance id, if present |
425 | | * (2) driver name |
426 | | * (3) driver alias, if present |
427 | | */ |
428 | 0 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
429 | 0 | DeviceState *dev = kid->child; |
430 | 0 | if (dev->id && strcmp(dev->id, elem) == 0) { |
431 | 0 | return dev; |
432 | 0 | } |
433 | 0 | } |
434 | 0 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
435 | 0 | DeviceState *dev = kid->child; |
436 | 0 | if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) { |
437 | 0 | return dev; |
438 | 0 | } |
439 | 0 | } |
440 | 0 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
441 | 0 | DeviceState *dev = kid->child; |
442 | 0 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
443 | |
|
444 | 0 | if (qdev_class_has_alias(dc) && |
445 | 0 | strcmp(qdev_class_get_alias(dc), elem) == 0) { |
446 | 0 | return dev; |
447 | 0 | } |
448 | 0 | } |
449 | 0 | return NULL; |
450 | 0 | } |
451 | | |
452 | | static inline bool qbus_is_full(BusState *bus) |
453 | 356 | { |
454 | 356 | BusClass *bus_class; |
455 | | |
456 | 356 | if (bus->full) { |
457 | 0 | return true; |
458 | 0 | } |
459 | 356 | bus_class = BUS_GET_CLASS(bus); |
460 | 356 | return bus_class->max_dev && bus->num_children >= bus_class->max_dev; |
461 | 356 | } |
462 | | |
463 | | /* |
464 | | * Search the tree rooted at @bus for a bus. |
465 | | * If @name, search for a bus with that name. Note that bus names |
466 | | * need not be unique. Yes, that's screwed up. |
467 | | * Else search for a bus that is a subtype of @bus_typename. |
468 | | * If more than one exists, prefer one that can take another device. |
469 | | * Return the bus if found, else %NULL. |
470 | | */ |
471 | | static BusState *qbus_find_recursive(BusState *bus, const char *name, |
472 | | const char *bus_typename) |
473 | 299 | { |
474 | 299 | BusChild *kid; |
475 | 299 | BusState *pick, *child, *ret; |
476 | 299 | bool match; |
477 | | |
478 | 299 | assert(name || bus_typename); |
479 | 299 | if (name) { |
480 | 34 | match = !strcmp(bus->name, name); |
481 | 265 | } else { |
482 | 265 | match = !!object_dynamic_cast(OBJECT(bus), bus_typename); |
483 | 265 | } |
484 | | |
485 | 299 | if (match && !qbus_is_full(bus)) { |
486 | 101 | return bus; /* root matches and isn't full */ |
487 | 101 | } |
488 | | |
489 | 198 | pick = match ? bus : NULL; |
490 | | |
491 | 714 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
492 | 714 | DeviceState *dev = kid->child; |
493 | 714 | QLIST_FOREACH(child, &dev->child_bus, sibling) { |
494 | 198 | ret = qbus_find_recursive(child, name, bus_typename); |
495 | 198 | if (ret && !qbus_is_full(ret)) { |
496 | 154 | return ret; /* a descendant matches and isn't full */ |
497 | 154 | } |
498 | 44 | if (ret && !pick) { |
499 | 0 | pick = ret; |
500 | 0 | } |
501 | 44 | } |
502 | 714 | } |
503 | | |
504 | | /* root or a descendant matches, but is full */ |
505 | 44 | return pick; |
506 | 198 | } |
507 | | |
508 | | static BusState *qbus_find(const char *path, Error **errp) |
509 | 14 | { |
510 | 14 | DeviceState *dev; |
511 | 14 | BusState *bus; |
512 | 14 | char elem[128]; |
513 | 14 | int pos, len; |
514 | | |
515 | | /* find start element */ |
516 | 14 | if (path[0] == '/') { |
517 | 0 | bus = sysbus_get_default(); |
518 | 0 | pos = 0; |
519 | 14 | } else { |
520 | 14 | if (sscanf(path, "%127[^/]%n", elem, &len) != 1) { |
521 | 0 | assert(!path[0]); |
522 | 0 | elem[0] = len = 0; |
523 | 0 | } |
524 | 14 | bus = qbus_find_recursive(sysbus_get_default(), elem, NULL); |
525 | 14 | if (!bus) { |
526 | 0 | error_setg(errp, "Bus '%s' not found", elem); |
527 | 0 | return NULL; |
528 | 0 | } |
529 | 14 | pos = len; |
530 | 14 | } |
531 | | |
532 | 14 | for (;;) { |
533 | 14 | assert(path[pos] == '/' || !path[pos]); |
534 | 14 | while (path[pos] == '/') { |
535 | 0 | pos++; |
536 | 0 | } |
537 | 14 | if (path[pos] == '\0') { |
538 | 14 | break; |
539 | 14 | } |
540 | | |
541 | | /* find device */ |
542 | 0 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
543 | 0 | g_assert_not_reached(); |
544 | 0 | elem[0] = len = 0; |
545 | 0 | } |
546 | 0 | pos += len; |
547 | 0 | dev = qbus_find_dev(bus, elem); |
548 | 0 | if (!dev) { |
549 | 0 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
550 | 0 | "Device '%s' not found", elem); |
551 | 0 | qbus_error_append_dev_list_hint(bus, errp); |
552 | 0 | return NULL; |
553 | 0 | } |
554 | | |
555 | 0 | assert(path[pos] == '/' || !path[pos]); |
556 | 0 | while (path[pos] == '/') { |
557 | 0 | pos++; |
558 | 0 | } |
559 | 0 | if (path[pos] == '\0') { |
560 | | /* last specified element is a device. If it has exactly |
561 | | * one child bus accept it nevertheless */ |
562 | 0 | if (dev->num_child_bus == 1) { |
563 | 0 | bus = QLIST_FIRST(&dev->child_bus); |
564 | 0 | break; |
565 | 0 | } |
566 | 0 | if (dev->num_child_bus) { |
567 | 0 | error_setg(errp, "Device '%s' has multiple child buses", |
568 | 0 | elem); |
569 | 0 | qbus_error_append_bus_list_hint(dev, errp); |
570 | 0 | } else { |
571 | 0 | error_setg(errp, "Device '%s' has no child bus", elem); |
572 | 0 | } |
573 | 0 | return NULL; |
574 | 0 | } |
575 | | |
576 | | /* find bus */ |
577 | 0 | if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) { |
578 | 0 | g_assert_not_reached(); |
579 | 0 | elem[0] = len = 0; |
580 | 0 | } |
581 | 0 | pos += len; |
582 | 0 | bus = qbus_find_bus(dev, elem); |
583 | 0 | if (!bus) { |
584 | 0 | error_setg(errp, "Bus '%s' not found", elem); |
585 | 0 | qbus_error_append_bus_list_hint(dev, errp); |
586 | 0 | return NULL; |
587 | 0 | } |
588 | 0 | } |
589 | | |
590 | 14 | if (qbus_is_full(bus)) { |
591 | 0 | error_setg(errp, "Bus '%s' is full", path); |
592 | 0 | return NULL; |
593 | 0 | } |
594 | 14 | return bus; |
595 | 14 | } |
596 | | |
597 | | /* Takes ownership of @id, will be freed when deleting the device */ |
598 | | const char *qdev_set_id(DeviceState *dev, char *id, Error **errp) |
599 | 101 | { |
600 | 101 | ObjectProperty *prop; |
601 | | |
602 | 101 | assert(!dev->id && !dev->realized); |
603 | | |
604 | | /* |
605 | | * object_property_[try_]add_child() below will assert the device |
606 | | * has no parent |
607 | | */ |
608 | 101 | if (id) { |
609 | 9 | if (!id_wellformed(id)) { |
610 | 0 | error_setg(errp, "Invalid qdev ID '%s'", id); |
611 | 0 | goto err; |
612 | 0 | } |
613 | 9 | prop = object_property_try_add_child(qdev_get_peripheral(), id, |
614 | 9 | OBJECT(dev), NULL); |
615 | 9 | if (prop) { |
616 | 9 | dev->id = id; |
617 | 9 | } else { |
618 | 0 | error_setg(errp, "Duplicate device ID '%s'", id); |
619 | 0 | goto err; |
620 | 0 | } |
621 | 92 | } else { |
622 | 92 | static int anon_count; |
623 | 92 | gchar *name = g_strdup_printf("device[%d]", anon_count++); |
624 | 92 | prop = object_property_add_child(qdev_get_peripheral_anon(), name, |
625 | 92 | OBJECT(dev)); |
626 | 92 | g_free(name); |
627 | 92 | } |
628 | | |
629 | 101 | return prop->name; |
630 | | |
631 | 0 | err: |
632 | 0 | g_free(id); |
633 | 0 | return NULL; |
634 | 101 | } |
635 | | |
636 | | BusState *qdev_find_default_bus(DeviceClass *dc, Error **errp) |
637 | 87 | { |
638 | 87 | BusState *bus = NULL; |
639 | | |
640 | 87 | assert(dc->bus_type != NULL); |
641 | 87 | bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type); |
642 | 87 | if (!bus) { |
643 | 0 | error_setg(errp, "No '%s' bus found for device '%s'", |
644 | 0 | dc->bus_type, object_class_get_name(OBJECT_CLASS(dc))); |
645 | 0 | return NULL; |
646 | 0 | } |
647 | 87 | if (qbus_is_full(bus)) { |
648 | 0 | error_setg(errp, "A '%s' bus was found but is full", dc->bus_type); |
649 | 0 | return NULL; |
650 | 0 | } |
651 | | |
652 | 87 | return bus; |
653 | 87 | } |
654 | | |
655 | | DeviceState *qdev_device_add_from_qdict(const QDict *opts, |
656 | | bool from_json, Error **errp) |
657 | 101 | { |
658 | 101 | ERRP_GUARD(); |
659 | 101 | DeviceClass *dc; |
660 | 101 | const char *driver, *path; |
661 | 101 | char *id; |
662 | 101 | DeviceState *dev; |
663 | 101 | BusState *bus = NULL; |
664 | 101 | QDict *properties; |
665 | | |
666 | 101 | driver = qdict_get_try_str(opts, "driver"); |
667 | 101 | if (!driver) { |
668 | 0 | error_setg(errp, QERR_MISSING_PARAMETER, "driver"); |
669 | 0 | return NULL; |
670 | 0 | } |
671 | | |
672 | | /* find driver */ |
673 | 101 | dc = qdev_get_device_class(&driver, errp); |
674 | 101 | if (!dc) { |
675 | 0 | return NULL; |
676 | 0 | } |
677 | | |
678 | 101 | if (!object_class_check_security(OBJECT_CLASS(dc), errp)) { |
679 | 0 | return NULL; |
680 | 0 | } |
681 | | |
682 | | /* find bus */ |
683 | 101 | path = qdict_get_try_str(opts, "bus"); |
684 | 101 | if (path != NULL) { |
685 | 14 | bus = qbus_find(path, errp); |
686 | 14 | if (!bus) { |
687 | 0 | return NULL; |
688 | 0 | } |
689 | 14 | if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) { |
690 | 0 | error_setg(errp, "Device '%s' can't go on %s bus", |
691 | 0 | driver, object_get_typename(OBJECT(bus))); |
692 | 0 | return NULL; |
693 | 0 | } |
694 | 87 | } else if (dc->bus_type != NULL) { |
695 | 87 | bus = qdev_find_default_bus(dc, errp); |
696 | 87 | if (!bus) { |
697 | 0 | return NULL; |
698 | 0 | } |
699 | 87 | } |
700 | | |
701 | 101 | if (qdev_should_hide_device(opts, from_json, errp)) { |
702 | 0 | if (bus && !qbus_is_hotpluggable(bus)) { |
703 | 0 | error_setg(errp, "Bus '%s' does not support hotplugging", |
704 | 0 | bus->name); |
705 | 0 | } |
706 | 0 | return NULL; |
707 | 101 | } else if (*errp) { |
708 | 0 | return NULL; |
709 | 0 | } |
710 | | |
711 | 101 | if (migration_is_running()) { |
712 | 0 | error_setg(errp, "device_add not allowed while migrating"); |
713 | 0 | return NULL; |
714 | 0 | } |
715 | | |
716 | | /* create device */ |
717 | 101 | dev = qdev_new(driver); |
718 | | |
719 | | /* Check whether the hotplug is allowed by the machine */ |
720 | 101 | if (phase_check(PHASE_MACHINE_READY) && |
721 | 0 | !qdev_hotplug_allowed(dev, bus, errp)) { |
722 | 0 | goto err_del_dev; |
723 | 0 | } |
724 | | |
725 | | /* |
726 | | * set dev's parent and register its id. |
727 | | * If it fails it means the id is already taken. |
728 | | */ |
729 | 101 | id = g_strdup(qdict_get_try_str(opts, "id")); |
730 | 101 | if (!qdev_set_id(dev, id, errp)) { |
731 | 0 | goto err_del_dev; |
732 | 0 | } |
733 | | |
734 | | /* set properties */ |
735 | 101 | properties = qdict_clone_shallow(opts); |
736 | 101 | qdict_del(properties, "driver"); |
737 | 101 | qdict_del(properties, "bus"); |
738 | 101 | qdict_del(properties, "id"); |
739 | | |
740 | 101 | object_set_props_from_keyval(&dev->parent_obj, properties, from_json, |
741 | 101 | errp); |
742 | 101 | qobject_unref(properties); |
743 | 101 | if (*errp) { |
744 | 0 | goto err_del_dev; |
745 | 0 | } |
746 | | |
747 | 101 | if (!qdev_realize(dev, bus, errp)) { |
748 | 0 | goto err_del_dev; |
749 | 0 | } |
750 | 101 | return dev; |
751 | | |
752 | 0 | err_del_dev: |
753 | 0 | object_unparent(OBJECT(dev)); |
754 | 0 | object_unref(OBJECT(dev)); |
755 | |
|
756 | 0 | return NULL; |
757 | 101 | } |
758 | | |
759 | | /* Takes ownership of @opts on success */ |
760 | | DeviceState *qdev_device_add(QemuOpts *opts, Error **errp) |
761 | 101 | { |
762 | 101 | QDict *qdict = qemu_opts_to_qdict(opts, NULL); |
763 | 101 | DeviceState *ret; |
764 | | |
765 | 101 | ret = qdev_device_add_from_qdict(qdict, false, errp); |
766 | 101 | if (ret) { |
767 | 101 | qemu_opts_del(opts); |
768 | 101 | } |
769 | 101 | qobject_unref(qdict); |
770 | 101 | return ret; |
771 | 101 | } |
772 | | |
773 | | #ifdef CONFIG_HMP |
774 | | #define qdev_printf(fmt, ...) \ |
775 | 0 | monitor_hmp_printf(hmp, "%*s" fmt, indent, "", ## __VA_ARGS__) |
776 | | |
777 | | static void qdev_print_props(MonitorHMP *hmp, DeviceState *dev, DeviceClass *dc, |
778 | | int indent) |
779 | 0 | { |
780 | 0 | for (int i = 0, n = dc->props_count_; i < n; ++i) { |
781 | 0 | const Property *prop = &dc->props_[i]; |
782 | 0 | char *value; |
783 | |
|
784 | 0 | if (prop->info->print) { |
785 | 0 | value = prop->info->print(OBJECT(dev), prop); |
786 | 0 | } else { |
787 | 0 | value = object_property_print(OBJECT(dev), prop->name, true, |
788 | 0 | NULL); |
789 | 0 | } |
790 | |
|
791 | 0 | if (!value) { |
792 | 0 | continue; |
793 | 0 | } |
794 | | |
795 | 0 | qdev_printf("%s = %s\n", prop->name, |
796 | 0 | *value ? value : "<null>"); |
797 | 0 | g_free(value); |
798 | 0 | } |
799 | 0 | } |
800 | | |
801 | | static void bus_print_dev(BusState *bus, MonitorHMP *hmp, DeviceState *dev, int indent) |
802 | 0 | { |
803 | 0 | BusClass *bc = BUS_GET_CLASS(bus); |
804 | |
|
805 | 0 | if (bc->print_dev) { |
806 | 0 | bc->print_dev(hmp, dev, indent); |
807 | 0 | } |
808 | 0 | } |
809 | | |
810 | | static void qdev_print(MonitorHMP *hmp, DeviceState *dev, int indent) |
811 | 0 | { |
812 | 0 | ObjectClass *class; |
813 | 0 | NamedGPIOList *ngl; |
814 | 0 | NamedClockList *ncl; |
815 | |
|
816 | 0 | QLIST_FOREACH(ngl, &dev->gpios, node) { |
817 | 0 | if (ngl->num_in) { |
818 | 0 | qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "", |
819 | 0 | ngl->num_in); |
820 | 0 | } |
821 | 0 | if (ngl->num_out) { |
822 | 0 | qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "", |
823 | 0 | ngl->num_out); |
824 | 0 | } |
825 | 0 | } |
826 | 0 | QLIST_FOREACH(ncl, &dev->clocks, node) { |
827 | 0 | g_autofree char *freq_str = clock_display_freq(ncl->clock); |
828 | 0 | qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n", |
829 | 0 | ncl->output ? "out" : "in", |
830 | 0 | ncl->alias ? " (alias)" : "", |
831 | 0 | ncl->name, freq_str); |
832 | 0 | } |
833 | 0 | class = object_get_class(OBJECT(dev)); |
834 | 0 | do { |
835 | 0 | qdev_print_props(hmp, dev, DEVICE_CLASS(class), indent); |
836 | 0 | class = object_class_get_parent(class); |
837 | 0 | } while (class != object_class_by_name(TYPE_DEVICE)); |
838 | 0 | bus_print_dev(dev->parent_bus, hmp, dev, indent); |
839 | 0 | } |
840 | | |
841 | | static void qbus_print(MonitorHMP *hmp, BusState *bus, int indent, bool details) |
842 | 0 | { |
843 | 0 | BusChild *kid; |
844 | |
|
845 | 0 | qdev_printf("bus: %s\n", bus->name); |
846 | 0 | indent += 2; |
847 | 0 | qdev_printf("type %s\n", object_get_typename(OBJECT(bus))); |
848 | 0 | QTAILQ_FOREACH(kid, &bus->children, sibling) { |
849 | 0 | BusState *child_bus; |
850 | 0 | DeviceState *dev = kid->child; |
851 | 0 | qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)), |
852 | 0 | dev->id ? dev->id : ""); |
853 | 0 | if (details) { |
854 | 0 | qdev_print(hmp, dev, indent + 2); |
855 | 0 | } |
856 | 0 | QLIST_FOREACH(child_bus, &dev->child_bus, sibling) { |
857 | 0 | qbus_print(hmp, child_bus, indent + 2, details); |
858 | 0 | } |
859 | 0 | } |
860 | 0 | } |
861 | | #undef qdev_printf |
862 | | |
863 | | void hmp_info_qtree(MonitorHMP *hmp, const QDict *qdict) |
864 | 0 | { |
865 | 0 | bool details = !qdict_get_try_bool(qdict, "brief", false); |
866 | |
|
867 | 0 | if (sysbus_get_default()) { |
868 | 0 | qbus_print(hmp, sysbus_get_default(), 0, details); |
869 | 0 | } |
870 | 0 | } |
871 | | |
872 | | void hmp_info_qdm(MonitorHMP *hmp, const QDict *qdict) |
873 | 0 | { |
874 | 0 | qdev_print_devinfos(true); |
875 | 0 | } |
876 | | #endif /* CONFIG_HMP */ |
877 | | |
878 | | void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp) |
879 | 0 | { |
880 | 0 | DeviceState *dev; |
881 | |
|
882 | 0 | dev = qdev_device_add_from_qdict(qdict, true, errp); |
883 | 0 | if (!dev) { |
884 | | /* |
885 | | * Drain all pending RCU callbacks. This is done because |
886 | | * some bus related operations can delay a device removal |
887 | | * (in this case this can happen if device is added and then |
888 | | * removed due to a configuration error) |
889 | | * to a RCU callback, but user might expect that this interface |
890 | | * will finish its job completely once qmp command returns result |
891 | | * to the user |
892 | | */ |
893 | 0 | drain_call_rcu(); |
894 | 0 | } |
895 | 0 | object_unref(OBJECT(dev)); |
896 | 0 | } |
897 | | |
898 | | /* |
899 | | * Note that creating new APIs using error classes other than GenericError is |
900 | | * not recommended. Set use_generic_error=true for new interfaces. |
901 | | */ |
902 | | static DeviceState *find_device_state(const char *id, bool use_generic_error, |
903 | | Error **errp) |
904 | 0 | { |
905 | 0 | Object *obj = object_resolve_path_at(qdev_get_peripheral(), id); |
906 | 0 | DeviceState *dev; |
907 | |
|
908 | 0 | if (!obj) { |
909 | 0 | error_set(errp, |
910 | 0 | (use_generic_error ? |
911 | 0 | ERROR_CLASS_GENERIC_ERROR : ERROR_CLASS_DEVICE_NOT_FOUND), |
912 | 0 | "Device '%s' not found", id); |
913 | 0 | return NULL; |
914 | 0 | } |
915 | | |
916 | 0 | dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE); |
917 | 0 | if (!dev) { |
918 | 0 | error_setg(errp, "%s is not a device", id); |
919 | 0 | return NULL; |
920 | 0 | } |
921 | | |
922 | 0 | return dev; |
923 | 0 | } |
924 | | |
925 | | void qdev_unplug(DeviceState *dev, Error **errp) |
926 | 0 | { |
927 | 0 | const HotplugHandler *hotplug_ctrl; |
928 | 0 | const HotplugHandlerClass *hdc; |
929 | 0 | Error *local_err = NULL; |
930 | |
|
931 | 0 | if (!qdev_hotunplug_allowed(dev, errp)) { |
932 | 0 | return; |
933 | 0 | } |
934 | | |
935 | 0 | if (migration_is_running() && !dev->allow_unplug_during_migration) { |
936 | 0 | error_setg(errp, "device_del not allowed while migrating"); |
937 | 0 | return; |
938 | 0 | } |
939 | | |
940 | 0 | qdev_hot_removed = true; |
941 | |
|
942 | 0 | hotplug_ctrl = qdev_get_hotplug_handler(dev); |
943 | | /* hotpluggable device MUST have HotplugHandler, if it doesn't |
944 | | * then something is very wrong with it */ |
945 | 0 | g_assert(hotplug_ctrl); |
946 | | |
947 | | /* If device supports async unplug just request it to be done, |
948 | | * otherwise just remove it synchronously */ |
949 | 0 | hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl); |
950 | 0 | if (hdc->unplug_request) { |
951 | 0 | hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err); |
952 | 0 | } else { |
953 | 0 | hotplug_handler_unplug(hotplug_ctrl, dev, &local_err); |
954 | 0 | if (!local_err) { |
955 | 0 | object_unparent(OBJECT(dev)); |
956 | 0 | } |
957 | 0 | } |
958 | 0 | error_propagate(errp, local_err); |
959 | 0 | } |
960 | | |
961 | | void qmp_device_del(const char *id, Error **errp) |
962 | 0 | { |
963 | 0 | DeviceState *dev = find_device_state(id, false, errp); |
964 | 0 | if (dev != NULL) { |
965 | 0 | if (dev->pending_deleted_event && |
966 | 0 | (dev->pending_deleted_expires_ms == 0 || |
967 | 0 | dev->pending_deleted_expires_ms > qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL))) { |
968 | 0 | error_setg(errp, "Device %s is already in the " |
969 | 0 | "process of unplug", id); |
970 | 0 | return; |
971 | 0 | } |
972 | | |
973 | 0 | qdev_unplug(dev, errp); |
974 | 0 | } |
975 | 0 | } |
976 | | |
977 | | int qdev_sync_config(DeviceState *dev, Error **errp) |
978 | 0 | { |
979 | 0 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
980 | |
|
981 | 0 | if (!dc->sync_config) { |
982 | 0 | error_setg(errp, "device-sync-config is not supported for '%s'", |
983 | 0 | object_get_typename(OBJECT(dev))); |
984 | 0 | return -ENOTSUP; |
985 | 0 | } |
986 | | |
987 | 0 | return dc->sync_config(dev, errp); |
988 | 0 | } |
989 | | |
990 | | void qmp_device_sync_config(const char *id, Error **errp) |
991 | 0 | { |
992 | 0 | DeviceState *dev; |
993 | | |
994 | | /* |
995 | | * During migration there is a race between syncing`configuration |
996 | | * and migrating it (if migrate first, that target would get |
997 | | * outdated version), so let's just not allow it. |
998 | | */ |
999 | |
|
1000 | 0 | if (migration_is_running()) { |
1001 | 0 | error_setg(errp, "Config synchronization is not allowed " |
1002 | 0 | "during migration"); |
1003 | 0 | return; |
1004 | 0 | } |
1005 | | |
1006 | 0 | dev = find_device_state(id, true, errp); |
1007 | 0 | if (!dev) { |
1008 | 0 | return; |
1009 | 0 | } |
1010 | | |
1011 | 0 | qdev_sync_config(dev, errp); |
1012 | 0 | } |
1013 | | |
1014 | | #ifdef CONFIG_HMP |
1015 | | void hmp_device_add(MonitorHMP *hmp, const QDict *qdict) |
1016 | 0 | { |
1017 | 0 | Error *err = NULL; |
1018 | 0 | QemuOpts *opts; |
1019 | 0 | DeviceState *dev; |
1020 | |
|
1021 | 0 | opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &err); |
1022 | 0 | if (!opts) { |
1023 | 0 | goto out; |
1024 | 0 | } |
1025 | 0 | if (qdev_device_help(opts)) { |
1026 | 0 | qemu_opts_del(opts); |
1027 | 0 | return; |
1028 | 0 | } |
1029 | 0 | dev = qdev_device_add(opts, &err); |
1030 | 0 | if (!dev) { |
1031 | | /* |
1032 | | * Drain all pending RCU callbacks. This is done because |
1033 | | * some bus related operations can delay a device removal |
1034 | | * (in this case this can happen if device is added and then |
1035 | | * removed due to a configuration error) |
1036 | | * to a RCU callback, but user might expect that this interface |
1037 | | * will finish its job completely once qmp command returns result |
1038 | | * to the user |
1039 | | */ |
1040 | 0 | drain_call_rcu(); |
1041 | |
|
1042 | 0 | qemu_opts_del(opts); |
1043 | 0 | } |
1044 | 0 | object_unref(dev); |
1045 | 0 | out: |
1046 | 0 | hmp_handle_error(hmp, err); |
1047 | 0 | } |
1048 | | |
1049 | | void hmp_device_del(MonitorHMP *hmp, const QDict *qdict) |
1050 | 0 | { |
1051 | 0 | const char *id = qdict_get_str(qdict, "id"); |
1052 | 0 | Error *err = NULL; |
1053 | |
|
1054 | 0 | qmp_device_del(id, &err); |
1055 | 0 | hmp_handle_error(hmp, err); |
1056 | 0 | } |
1057 | | #endif |
1058 | | |
1059 | | void device_add_completion(ReadLineState *rs, int nb_args, const char *str) |
1060 | 0 | { |
1061 | 0 | GSList *list, *elt; |
1062 | 0 | size_t len; |
1063 | |
|
1064 | 0 | if (nb_args != 2) { |
1065 | 0 | return; |
1066 | 0 | } |
1067 | | |
1068 | 0 | len = strlen(str); |
1069 | 0 | readline_set_completion_index(rs, len); |
1070 | 0 | list = elt = object_class_get_list(TYPE_DEVICE, false); |
1071 | 0 | while (elt) { |
1072 | 0 | DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, |
1073 | 0 | TYPE_DEVICE); |
1074 | |
|
1075 | 0 | if (dc->user_creatable) { |
1076 | 0 | readline_add_completion_of(rs, str, |
1077 | 0 | object_class_get_name(OBJECT_CLASS(dc))); |
1078 | 0 | } |
1079 | 0 | elt = elt->next; |
1080 | 0 | } |
1081 | 0 | g_slist_free(list); |
1082 | 0 | } |
1083 | | |
1084 | | static int qdev_add_hotpluggable_device(Object *obj, void *opaque) |
1085 | 0 | { |
1086 | 0 | GSList **list = opaque; |
1087 | 0 | DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE); |
1088 | |
|
1089 | 0 | if (dev == NULL) { |
1090 | 0 | return 0; |
1091 | 0 | } |
1092 | | |
1093 | 0 | if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) { |
1094 | 0 | *list = g_slist_append(*list, dev); |
1095 | 0 | } |
1096 | |
|
1097 | 0 | return 0; |
1098 | 0 | } |
1099 | | |
1100 | | static GSList *qdev_build_hotpluggable_device_list(Object *peripheral) |
1101 | 0 | { |
1102 | 0 | GSList *list = NULL; |
1103 | |
|
1104 | 0 | object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list); |
1105 | |
|
1106 | 0 | return list; |
1107 | 0 | } |
1108 | | |
1109 | | static void peripheral_device_del_completion(ReadLineState *rs, |
1110 | | const char *str) |
1111 | 0 | { |
1112 | 0 | Object *peripheral = machine_get_container("peripheral"); |
1113 | 0 | GSList *list, *item; |
1114 | |
|
1115 | 0 | list = qdev_build_hotpluggable_device_list(peripheral); |
1116 | 0 | if (!list) { |
1117 | 0 | return; |
1118 | 0 | } |
1119 | | |
1120 | 0 | for (item = list; item; item = g_slist_next(item)) { |
1121 | 0 | DeviceState *dev = item->data; |
1122 | |
|
1123 | 0 | if (dev->id) { |
1124 | 0 | readline_add_completion_of(rs, str, dev->id); |
1125 | 0 | } |
1126 | 0 | } |
1127 | |
|
1128 | 0 | g_slist_free(list); |
1129 | 0 | } |
1130 | | |
1131 | | void device_del_completion(ReadLineState *rs, int nb_args, const char *str) |
1132 | 0 | { |
1133 | 0 | if (nb_args != 2) { |
1134 | 0 | return; |
1135 | 0 | } |
1136 | | |
1137 | 0 | readline_set_completion_index(rs, strlen(str)); |
1138 | 0 | peripheral_device_del_completion(rs, str); |
1139 | 0 | } |
1140 | | |
1141 | | BlockBackend *blk_by_qdev_id(const char *id, Error **errp) |
1142 | 0 | { |
1143 | 0 | DeviceState *dev; |
1144 | 0 | BlockBackend *blk; |
1145 | |
|
1146 | 0 | GLOBAL_STATE_CODE(); |
1147 | |
|
1148 | 0 | dev = find_device_state(id, false, errp); |
1149 | 0 | if (dev == NULL) { |
1150 | 0 | return NULL; |
1151 | 0 | } |
1152 | | |
1153 | 0 | blk = blk_by_dev(dev); |
1154 | 0 | if (!blk) { |
1155 | 0 | error_setg(errp, "Device does not have a block device backend"); |
1156 | 0 | } |
1157 | 0 | return blk; |
1158 | 0 | } |
1159 | | |
1160 | | QemuOptsList qemu_device_opts = { |
1161 | | .name = "device", |
1162 | | .implied_opt_name = "driver", |
1163 | | .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head), |
1164 | | .desc = { |
1165 | | /* |
1166 | | * no elements => accept any |
1167 | | * sanity checking will happen later |
1168 | | * when setting device properties |
1169 | | */ |
1170 | | { /* end of list */ } |
1171 | | }, |
1172 | | }; |
1173 | | |
1174 | | QemuOptsList qemu_global_opts = { |
1175 | | .name = "global", |
1176 | | .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head), |
1177 | | .desc = { |
1178 | | { |
1179 | | .name = "driver", |
1180 | | .type = QEMU_OPT_STRING, |
1181 | | },{ |
1182 | | .name = "property", |
1183 | | .type = QEMU_OPT_STRING, |
1184 | | },{ |
1185 | | .name = "value", |
1186 | | .type = QEMU_OPT_STRING, |
1187 | | }, |
1188 | | { /* end of list */ } |
1189 | | }, |
1190 | | }; |
1191 | | |
1192 | | int qemu_global_option(const char *str) |
1193 | 0 | { |
1194 | 0 | char driver[64], property[64]; |
1195 | 0 | QemuOpts *opts; |
1196 | 0 | int rc, offset; |
1197 | |
|
1198 | 0 | rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset); |
1199 | 0 | if (rc == 2 && str[offset] == '=') { |
1200 | 0 | opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort); |
1201 | 0 | qemu_opt_set(opts, "driver", driver, &error_abort); |
1202 | 0 | qemu_opt_set(opts, "property", property, &error_abort); |
1203 | 0 | qemu_opt_set(opts, "value", str + offset + 1, &error_abort); |
1204 | 0 | return 0; |
1205 | 0 | } |
1206 | | |
1207 | 0 | opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false); |
1208 | 0 | if (!opts) { |
1209 | 0 | return -1; |
1210 | 0 | } |
1211 | 0 | if (!qemu_opt_get(opts, "driver") |
1212 | 0 | || !qemu_opt_get(opts, "property") |
1213 | 0 | || !qemu_opt_get(opts, "value")) { |
1214 | 0 | error_report("options 'driver', 'property', and 'value'" |
1215 | 0 | " are required"); |
1216 | 0 | return -1; |
1217 | 0 | } |
1218 | | |
1219 | 0 | return 0; |
1220 | 0 | } |
1221 | | |
1222 | | bool qmp_command_available(const QmpCommand *cmd, Error **errp) |
1223 | 0 | { |
1224 | 0 | if (!phase_check(PHASE_MACHINE_READY) && |
1225 | 0 | !(cmd->options & QCO_ALLOW_PRECONFIG)) { |
1226 | 0 | error_setg(errp, "The command '%s' is permitted only after machine initialization has completed", |
1227 | 0 | cmd->name); |
1228 | 0 | return false; |
1229 | 0 | } |
1230 | 0 | return true; |
1231 | 0 | } |