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