blob: b8d2c4dadd1184ea546f3eed3614e0b1805d9d2b [file] [log] [blame]
Anthony Liguoriee46d8a2011-12-22 15:24:20 -06001/*
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
Chetan Pant61f3c912020-10-23 12:44:24 +00009 * version 2.1 of the License, or (at your option) any later version.
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060010 *
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
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Andreas Färber2f7bd822013-04-16 03:50:21 +020021#include "hw/sysbus.h"
Markus Armbrusterd94bd092019-07-09 20:59:36 +020022#include "monitor/hmp.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010023#include "monitor/monitor.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010024#include "monitor/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/arch_init.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010026#include "qapi/error.h"
Markus Armbrusterc577ff62019-06-19 22:10:37 +020027#include "qapi/qapi-commands-qdev.h"
Paolo Bonzini164dafd2020-10-27 04:44:18 -040028#include "qapi/qmp/dispatch.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010029#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010030#include "qapi/qmp/qerror.h"
Kevin Wolfc34efec2021-10-08 15:34:34 +020031#include "qapi/qmp/qstring.h"
32#include "qapi/qobject-input-visitor.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010033#include "qemu/config-file.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010034#include "qemu/error-report.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020035#include "qemu/help_option.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010036#include "qemu/option.h"
Markus Armbruster8acb2a72019-04-17 21:06:41 +020037#include "qemu/qemu-print.h"
Jens Freimannf3a85052019-10-29 12:48:55 +010038#include "qemu/option_int.h"
Kevin Wolf9680cae2016-09-20 13:38:42 +020039#include "sysemu/block-backend.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020040#include "migration/misc.h"
Jens Freimannf3a85052019-10-29 12:48:55 +010041#include "migration/migration.h"
Marc-André Lureaue1043d62020-01-10 19:30:39 +040042#include "qemu/cutils.h"
Eduardo Habkostd3fd6e72020-12-11 17:05:00 -050043#include "hw/qdev-properties.h"
Damien Hedde9f2ff992020-04-06 15:52:51 +020044#include "hw/clock.h"
Damien Hedde312e1b12021-10-29 16:22:57 +020045#include "hw/boards.h"
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060046
47/*
48 * Aliases were a bad idea from the start. Let's keep them
49 * from spreading further.
50 */
51typedef struct QDevAlias
52{
53 const char *typename;
54 const char *alias;
Alexander Graf5f629d92012-05-18 02:36:26 +020055 uint32_t arch_mask;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060056} QDevAlias;
57
Peter Maydell36692822021-07-30 11:59:45 +010058/* 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 | \
Xiaojuan Yang256309e2022-06-06 20:43:26 +080063 QEMU_ARCH_SPARC | QEMU_ARCH_XTENSA | \
64 QEMU_ARCH_LOONGARCH)
Peter Maydell36692822021-07-30 11:59:45 +010065#define QEMU_ARCH_VIRTIO_CCW (QEMU_ARCH_S390X)
66#define QEMU_ARCH_VIRTIO_MMIO (QEMU_ARCH_M68K)
67
Sascha Silbe36e99162016-02-18 22:44:13 +010068/* Please keep this table sorted by typename. */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060069static const QDevAlias qdev_alias_table[] = {
Gerd Hoffmann2957f5a2020-07-02 15:25:09 +020070 { "AC97", "ac97" }, /* -soundhw name */
Sascha Silbe36e99162016-02-18 22:44:13 +010071 { "e1000", "e1000-82540em" },
Gerd Hoffmann0e933fe2020-07-02 15:25:10 +020072 { "ES1370", "es1370" }, /* -soundhw name */
Sascha Silbe36e99162016-02-18 22:44:13 +010073 { "ich9-ahci", "ahci" },
Sascha Silbe36e99162016-02-18 22:44:13 +010074 { "lsi53c895a", "lsi" },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000075 { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000076 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW },
77 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000078 { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000079 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW },
80 { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000081 { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000082 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW },
83 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000084 { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000085 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW },
86 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI },
Akihiko Odakif4ba24b2022-02-13 11:18:00 +090087 { "virtio-gpu-gl-device", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_MMIO },
88 { "virtio-gpu-gl-pci", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000089 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000090 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW },
91 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI },
92 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000093 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000094 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW },
95 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000096 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000097 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW },
98 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000099 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +0000100 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW },
101 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +0000102 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +0000103 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW },
104 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +0000105 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +0000106 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW },
107 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +0000108 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +0000109 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW },
110 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI},
Laurent Vivier4c5806a2021-03-23 16:53:03 +0000111 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +0000112 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW },
113 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600114 { }
115};
116
117static const char *qdev_class_get_alias(DeviceClass *dc)
118{
119 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
120 int i;
121
122 for (i = 0; qdev_alias_table[i].typename; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200123 if (qdev_alias_table[i].arch_mask &&
124 !(qdev_alias_table[i].arch_mask & arch_type)) {
125 continue;
126 }
127
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600128 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
129 return qdev_alias_table[i].alias;
130 }
131 }
132
133 return NULL;
134}
135
136static bool qdev_class_has_alias(DeviceClass *dc)
137{
138 return (qdev_class_get_alias(dc) != NULL);
139}
140
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200141static void qdev_print_devinfo(DeviceClass *dc)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600142{
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200143 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
Anthony Liguori0d936922012-05-02 09:00:20 +0200144 if (dc->bus_type) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200145 qemu_printf(", bus %s", dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600146 }
147 if (qdev_class_has_alias(dc)) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200148 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600149 }
150 if (dc->desc) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200151 qemu_printf(", desc \"%s\"", dc->desc);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600152 }
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300153 if (!dc->user_creatable) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200154 qemu_printf(", no-user");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600155 }
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200156 qemu_printf("\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600157}
158
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200159static void qdev_print_devinfos(bool show_no_user)
160{
161 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
162 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
163 [DEVICE_CATEGORY_USB] = "USB",
164 [DEVICE_CATEGORY_STORAGE] = "Storage",
165 [DEVICE_CATEGORY_NETWORK] = "Network",
166 [DEVICE_CATEGORY_INPUT] = "Input",
167 [DEVICE_CATEGORY_DISPLAY] = "Display",
168 [DEVICE_CATEGORY_SOUND] = "Sound",
169 [DEVICE_CATEGORY_MISC] = "Misc",
Thomas Huthba31cc72017-01-20 14:01:16 +0100170 [DEVICE_CATEGORY_CPU] = "CPU",
Paolo Bonzinib10cb622021-10-27 14:34:53 +0200171 [DEVICE_CATEGORY_WATCHDOG]= "Watchdog",
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200172 [DEVICE_CATEGORY_MAX] = "Uncategorized",
173 };
174 GSList *list, *elt;
175 int i;
176 bool cat_printed;
177
Gerd Hoffmann7ab6e7f2020-06-24 15:10:38 +0200178 module_load_qom_all();
Paolo Bonzini47c66002018-03-03 08:33:10 +0100179 list = object_class_get_list_sorted(TYPE_DEVICE, false);
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200180
181 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
182 cat_printed = false;
183 for (elt = list; elt; elt = elt->next) {
184 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
185 TYPE_DEVICE);
186 if ((i < DEVICE_CATEGORY_MAX
187 ? !test_bit(i, dc->categories)
188 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100189 || (!show_no_user
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300190 && !dc->user_creatable)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200191 continue;
192 }
193 if (!cat_printed) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200194 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200195 cat_printed = true;
196 }
197 qdev_print_devinfo(dc);
198 }
199 }
200
201 g_slist_free(list);
202}
203
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600204static const char *find_typename_by_alias(const char *alias)
205{
206 int i;
207
208 for (i = 0; qdev_alias_table[i].alias; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200209 if (qdev_alias_table[i].arch_mask &&
210 !(qdev_alias_table[i].arch_mask & arch_type)) {
211 continue;
212 }
213
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600214 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
215 return qdev_alias_table[i].typename;
216 }
217 }
218
219 return NULL;
220}
221
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200222static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
223{
224 ObjectClass *oc;
225 DeviceClass *dc;
Sascha Silbef6b53192016-02-18 22:44:12 +0100226 const char *original_name = *driver;
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200227
Gerd Hoffmann7ab6e7f2020-06-24 15:10:38 +0200228 oc = module_object_class_by_name(*driver);
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200229 if (!oc) {
230 const char *typename = find_typename_by_alias(*driver);
231
232 if (typename) {
233 *driver = typename;
Gerd Hoffmann7ab6e7f2020-06-24 15:10:38 +0200234 oc = module_object_class_by_name(*driver);
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200235 }
236 }
237
238 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
Sascha Silbef6b53192016-02-18 22:44:12 +0100239 if (*driver != original_name) {
240 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
241 " name", original_name, *driver);
242 } else {
243 error_setg(errp, "'%s' is not a valid device model name", *driver);
244 }
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200245 return NULL;
246 }
247
248 if (object_class_is_abstract(oc)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100249 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
Markus Armbruster6cc06672020-11-13 09:26:26 +0100250 "a non-abstract device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200251 return NULL;
252 }
253
254 dc = DEVICE_CLASS(oc);
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300255 if (!dc->user_creatable ||
Paolo Bonzini2f181fb2020-11-12 09:38:36 -0500256 (phase_check(PHASE_MACHINE_READY) && !dc->hotpluggable)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100257 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
Markus Armbruster6cc06672020-11-13 09:26:26 +0100258 "a pluggable device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200259 return NULL;
260 }
261
Damien Hedde312e1b12021-10-29 16:22:57 +0200262 if (object_class_dynamic_cast(oc, TYPE_SYS_BUS_DEVICE)) {
263 /* sysbus devices need to be allowed by the machine */
264 MachineClass *mc = MACHINE_CLASS(object_get_class(qdev_get_machine()));
265 if (!device_type_is_dynamic_sysbus(mc, *driver)) {
266 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
267 "a dynamic sysbus device type for the machine");
268 return NULL;
269 }
270 }
271
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200272 return dc;
273}
274
275
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600276int qdev_device_help(QemuOpts *opts)
277{
Stefan Hajnoczief523582014-07-09 14:01:32 +0200278 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600279 const char *driver;
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100280 ObjectPropertyInfoList *prop_list;
281 ObjectPropertyInfoList *prop;
Marc-André Lureaue1043d62020-01-10 19:30:39 +0400282 GPtrArray *array;
283 int i;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600284
285 driver = qemu_opt_get(opts, "driver");
Peter Maydellc8057f92012-08-02 13:45:54 +0100286 if (driver && is_help_option(driver)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200287 qdev_print_devinfos(false);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600288 return 1;
289 }
290
Peter Maydellc8057f92012-08-02 13:45:54 +0100291 if (!driver || !qemu_opt_has_help_opt(opts)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600292 return 0;
293 }
294
Markus Armbruster33fe9682015-10-01 10:59:59 +0200295 if (!object_class_by_name(driver)) {
296 const char *typename = find_typename_by_alias(driver);
297
298 if (typename) {
299 driver = typename;
300 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600301 }
302
Stefan Hajnoczief523582014-07-09 14:01:32 +0200303 prop_list = qmp_device_list_properties(driver, &local_err);
Gonglei0722eba2014-09-16 10:19:33 +0800304 if (local_err) {
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200305 goto error;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600306 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200307
Max Reitz9c2762b2018-10-19 18:49:27 +0200308 if (prop_list) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200309 qemu_printf("%s options:\n", driver);
Max Reitz9c2762b2018-10-19 18:49:27 +0200310 } else {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200311 qemu_printf("There are no options for %s.\n", driver);
Max Reitz9c2762b2018-10-19 18:49:27 +0200312 }
Marc-André Lureaue1043d62020-01-10 19:30:39 +0400313 array = g_ptr_array_new();
Stefan Hajnoczief523582014-07-09 14:01:32 +0200314 for (prop = prop_list; prop; prop = prop->next) {
Marc-André Lureaue1043d62020-01-10 19:30:39 +0400315 g_ptr_array_add(array,
316 object_property_help(prop->value->name,
317 prop->value->type,
318 prop->value->default_value,
319 prop->value->description));
Stefan Hajnoczief523582014-07-09 14:01:32 +0200320 }
Marc-André Lureaue1043d62020-01-10 19:30:39 +0400321 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
322 for (i = 0; i < array->len; i++) {
Markus Armbruster029afc42020-07-14 18:01:58 +0200323 qemu_printf("%s\n", (char *)array->pdata[i]);
Marc-André Lureaue1043d62020-01-10 19:30:39 +0400324 }
325 g_ptr_array_set_free_func(array, g_free);
326 g_ptr_array_free(array, true);
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100327 qapi_free_ObjectPropertyInfoList(prop_list);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600328 return 1;
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200329
330error:
Markus Armbruster78288672015-12-18 16:35:07 +0100331 error_report_err(local_err);
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200332 return 1;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600333}
334
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600335static Object *qdev_get_peripheral(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600336{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600337 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600338
339 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200340 dev = container_get(qdev_get_machine(), "/peripheral");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600341 }
342
Anthony Liguori8b45d442011-12-23 09:08:05 -0600343 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600344}
345
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600346static Object *qdev_get_peripheral_anon(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600347{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600348 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600349
350 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200351 dev = container_get(qdev_get_machine(), "/peripheral-anon");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600352 }
353
Anthony Liguori8b45d442011-12-23 09:08:05 -0600354 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600355}
356
Vladimir Sementsov-Ogievskiy34077322019-12-05 20:46:20 +0300357static void qbus_error_append_bus_list_hint(DeviceState *dev,
358 Error *const *errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600359{
360 BusState *child;
361 const char *sep = " ";
362
Eric Blake50b7b002015-09-10 10:19:16 -0600363 error_append_hint(errp, "child buses at \"%s\":",
364 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600365 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Eric Blake50b7b002015-09-10 10:19:16 -0600366 error_append_hint(errp, "%s\"%s\"", sep, child->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600367 sep = ", ";
368 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100369 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600370}
371
Vladimir Sementsov-Ogievskiy34077322019-12-05 20:46:20 +0300372static void qbus_error_append_dev_list_hint(BusState *bus,
373 Error *const *errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600374{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600375 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600376 const char *sep = " ";
377
Eric Blake50b7b002015-09-10 10:19:16 -0600378 error_append_hint(errp, "devices at \"%s\":", bus->name);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600379 QTAILQ_FOREACH(kid, &bus->children, sibling) {
380 DeviceState *dev = kid->child;
Eric Blake50b7b002015-09-10 10:19:16 -0600381 error_append_hint(errp, "%s\"%s\"", sep,
382 object_get_typename(OBJECT(dev)));
383 if (dev->id) {
384 error_append_hint(errp, "/\"%s\"", dev->id);
385 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600386 sep = ", ";
387 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100388 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600389}
390
391static BusState *qbus_find_bus(DeviceState *dev, char *elem)
392{
393 BusState *child;
394
395 QLIST_FOREACH(child, &dev->child_bus, sibling) {
396 if (strcmp(child->name, elem) == 0) {
397 return child;
398 }
399 }
400 return NULL;
401}
402
403static DeviceState *qbus_find_dev(BusState *bus, char *elem)
404{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600405 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600406
407 /*
408 * try to match in order:
409 * (1) instance id, if present
410 * (2) driver name
411 * (3) driver alias, if present
412 */
Anthony Liguori0866aca2011-12-23 15:34:39 -0600413 QTAILQ_FOREACH(kid, &bus->children, sibling) {
414 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600415 if (dev->id && strcmp(dev->id, elem) == 0) {
416 return dev;
417 }
418 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600419 QTAILQ_FOREACH(kid, &bus->children, sibling) {
420 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600421 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
422 return dev;
423 }
424 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600425 QTAILQ_FOREACH(kid, &bus->children, sibling) {
426 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600427 DeviceClass *dc = DEVICE_GET_CLASS(dev);
428
429 if (qdev_class_has_alias(dc) &&
430 strcmp(qdev_class_get_alias(dc), elem) == 0) {
431 return dev;
432 }
433 }
434 return NULL;
435}
436
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100437static inline bool qbus_is_full(BusState *bus)
438{
Peter Maydell15185622021-09-13 16:07:25 +0100439 BusClass *bus_class;
440
441 if (bus->full) {
442 return true;
443 }
444 bus_class = BUS_GET_CLASS(bus);
Tony Krowiak12b2e9f2018-12-17 10:57:30 -0500445 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100446}
447
448/*
449 * Search the tree rooted at @bus for a bus.
450 * If @name, search for a bus with that name. Note that bus names
451 * need not be unique. Yes, that's screwed up.
452 * Else search for a bus that is a subtype of @bus_typename.
453 * If more than one exists, prefer one that can take another device.
454 * Return the bus if found, else %NULL.
455 */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600456static BusState *qbus_find_recursive(BusState *bus, const char *name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200457 const char *bus_typename)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600458{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600459 BusChild *kid;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100460 BusState *pick, *child, *ret;
461 bool match;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600462
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100463 assert(name || bus_typename);
464 if (name) {
465 match = !strcmp(bus->name, name);
466 } else {
467 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
KONRAD Frederic1395af62013-01-15 00:08:00 +0100468 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100469
470 if (match && !qbus_is_full(bus)) {
471 return bus; /* root matches and isn't full */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600472 }
473
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100474 pick = match ? bus : NULL;
475
Anthony Liguori0866aca2011-12-23 15:34:39 -0600476 QTAILQ_FOREACH(kid, &bus->children, sibling) {
477 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600478 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200479 ret = qbus_find_recursive(child, name, bus_typename);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100480 if (ret && !qbus_is_full(ret)) {
481 return ret; /* a descendant matches and isn't full */
482 }
483 if (ret && !pick) {
484 pick = ret;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600485 }
486 }
487 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100488
489 /* root or a descendant matches, but is full */
490 return pick;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600491}
492
Markus Armbrusterd2828422015-03-11 19:16:04 +0100493static BusState *qbus_find(const char *path, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600494{
495 DeviceState *dev;
496 BusState *bus;
497 char elem[128];
498 int pos, len;
499
500 /* find start element */
501 if (path[0] == '/') {
502 bus = sysbus_get_default();
503 pos = 0;
504 } else {
505 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
506 assert(!path[0]);
507 elem[0] = len = 0;
508 }
509 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
510 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100511 error_setg(errp, "Bus '%s' not found", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600512 return NULL;
513 }
514 pos = len;
515 }
516
517 for (;;) {
518 assert(path[pos] == '/' || !path[pos]);
519 while (path[pos] == '/') {
520 pos++;
521 }
522 if (path[pos] == '\0') {
Markus Armbrustered238ba2015-03-11 18:39:16 +0100523 break;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600524 }
525
526 /* find device */
527 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200528 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600529 elem[0] = len = 0;
530 }
531 pos += len;
532 dev = qbus_find_dev(bus, elem);
533 if (!dev) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100534 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
535 "Device '%s' not found", elem);
Vladimir Sementsov-Ogievskiy34077322019-12-05 20:46:20 +0300536 qbus_error_append_dev_list_hint(bus, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600537 return NULL;
538 }
539
540 assert(path[pos] == '/' || !path[pos]);
541 while (path[pos] == '/') {
542 pos++;
543 }
544 if (path[pos] == '\0') {
545 /* last specified element is a device. If it has exactly
546 * one child bus accept it nevertheless */
Markus Armbrustered238ba2015-03-11 18:39:16 +0100547 if (dev->num_child_bus == 1) {
548 bus = QLIST_FIRST(&dev->child_bus);
549 break;
550 }
551 if (dev->num_child_bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100552 error_setg(errp, "Device '%s' has multiple child buses",
553 elem);
Vladimir Sementsov-Ogievskiy34077322019-12-05 20:46:20 +0300554 qbus_error_append_bus_list_hint(dev, errp);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100555 } else {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100556 error_setg(errp, "Device '%s' has no child bus", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600557 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100558 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600559 }
560
561 /* find bus */
562 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200563 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600564 elem[0] = len = 0;
565 }
566 pos += len;
567 bus = qbus_find_bus(dev, elem);
568 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100569 error_setg(errp, "Bus '%s' not found", elem);
Vladimir Sementsov-Ogievskiy34077322019-12-05 20:46:20 +0300570 qbus_error_append_bus_list_hint(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600571 return NULL;
572 }
573 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100574
575 if (qbus_is_full(bus)) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100576 error_setg(errp, "Bus '%s' is full", path);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100577 return NULL;
578 }
579 return bus;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600580}
581
Kevin Wolf163f3842021-10-08 15:34:35 +0200582/* Takes ownership of @id, will be freed when deleting the device */
Damien Hedde4a1d9372021-10-08 15:34:36 +0200583const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
Juergen Grossce49b732016-11-22 07:10:57 +0100584{
Damien Hedde4a1d9372021-10-08 15:34:36 +0200585 ObjectProperty *prop;
Juergen Grossce49b732016-11-22 07:10:57 +0100586
Damien Hedde4a1d9372021-10-08 15:34:36 +0200587 assert(!dev->id && !dev->realized);
588
589 /*
590 * object_property_[try_]add_child() below will assert the device
591 * has no parent
592 */
593 if (id) {
594 prop = object_property_try_add_child(qdev_get_peripheral(), id,
595 OBJECT(dev), NULL);
596 if (prop) {
597 dev->id = id;
598 } else {
Damien Hedde4a1d9372021-10-08 15:34:36 +0200599 error_setg(errp, "Duplicate device ID '%s'", id);
Stefan Hajnoczib66cecb2021-11-02 16:33:42 +0000600 g_free(id);
Damien Hedde4a1d9372021-10-08 15:34:36 +0200601 return NULL;
602 }
Juergen Grossce49b732016-11-22 07:10:57 +0100603 } else {
604 static int anon_count;
605 gchar *name = g_strdup_printf("device[%d]", anon_count++);
Damien Hedde4a1d9372021-10-08 15:34:36 +0200606 prop = object_property_add_child(qdev_get_peripheral_anon(), name,
607 OBJECT(dev));
Juergen Grossce49b732016-11-22 07:10:57 +0100608 g_free(name);
609 }
Damien Hedde4a1d9372021-10-08 15:34:36 +0200610
611 return prop->name;
Juergen Grossce49b732016-11-22 07:10:57 +0100612}
613
Kevin Wolff3558b12021-10-08 15:34:41 +0200614DeviceState *qdev_device_add_from_qdict(const QDict *opts,
615 bool from_json, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600616{
Kevin Wolf7d618082021-10-08 15:34:38 +0200617 ERRP_GUARD();
Andreas Färberf4d85792013-08-24 01:21:22 +0200618 DeviceClass *dc;
Juergen Grossce49b732016-11-22 07:10:57 +0100619 const char *driver, *path;
Kevin Wolff3558b12021-10-08 15:34:41 +0200620 char *id;
Jens Freimannf3a85052019-10-29 12:48:55 +0100621 DeviceState *dev = NULL;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200622 BusState *bus = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600623
Kevin Wolff3558b12021-10-08 15:34:41 +0200624 driver = qdict_get_try_str(opts, "driver");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600625 if (!driver) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100626 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600627 return NULL;
628 }
629
630 /* find driver */
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100631 dc = qdev_get_device_class(&driver, errp);
632 if (!dc) {
Markus Armbruster7ea5e782013-11-28 17:27:03 +0100633 return NULL;
634 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600635
636 /* find bus */
Kevin Wolff3558b12021-10-08 15:34:41 +0200637 path = qdict_get_try_str(opts, "bus");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600638 if (path != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100639 bus = qbus_find(path, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600640 if (!bus) {
641 return NULL;
642 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200643 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100644 error_setg(errp, "Device '%s' can't go on %s bus",
645 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600646 return NULL;
647 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200648 } else if (dc->bus_type != NULL) {
649 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100650 if (!bus || qbus_is_full(bus)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100651 error_setg(errp, "No '%s' bus found for device '%s'",
652 dc->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600653 return NULL;
654 }
655 }
Jens Freimannf3a85052019-10-29 12:48:55 +0100656
Laurent Vivierbcfc9062021-10-19 09:15:32 +0200657 if (qdev_should_hide_device(opts, from_json, errp)) {
658 if (bus && !qbus_is_hotpluggable(bus)) {
659 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
Juan Quintelafec037c2020-11-18 09:37:42 +0100660 }
Laurent Vivierbcfc9062021-10-19 09:15:32 +0200661 return NULL;
662 } else if (*errp) {
663 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600664 }
665
Paolo Bonzini2f181fb2020-11-12 09:38:36 -0500666 if (phase_check(PHASE_MACHINE_READY) && bus && !qbus_is_hotpluggable(bus)) {
Juan Quintela5f2ef3b2020-11-18 09:37:40 +0100667 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
Jens Freimannf3a85052019-10-29 12:48:55 +0100668 return NULL;
669 }
670
Juan Quintelab06424d2017-03-22 17:34:27 +0100671 if (!migration_is_idle()) {
672 error_setg(errp, "device_add not allowed while migrating");
673 return NULL;
674 }
675
Amos Kong7b030942014-03-03 15:57:55 +0800676 /* create device */
Markus Armbruster4e3a6772020-06-10 07:31:54 +0200677 dev = qdev_new(driver);
Andreas Färber2f7bd822013-04-16 03:50:21 +0200678
Peter Xud2321d32019-09-16 16:07:16 +0800679 /* Check whether the hotplug is allowed by the machine */
Paolo Bonzini2f181fb2020-11-12 09:38:36 -0500680 if (phase_check(PHASE_MACHINE_READY)) {
681 if (!qdev_hotplug_allowed(dev, errp)) {
682 goto err_del_dev;
683 }
Peter Xud2321d32019-09-16 16:07:16 +0800684
Paolo Bonzini2f181fb2020-11-12 09:38:36 -0500685 if (!bus && !qdev_get_machine_hotplug_handler(dev)) {
686 /* No bus, no machine hotplug handler --> device is not hotpluggable */
687 error_setg(errp, "Device '%s' can not be hotplugged on this machine",
688 driver);
689 goto err_del_dev;
690 }
Andreas Färber2f7bd822013-04-16 03:50:21 +0200691 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600692
Damien Hedde4a1d9372021-10-08 15:34:36 +0200693 /*
694 * set dev's parent and register its id.
695 * If it fails it means the id is already taken.
696 */
Kevin Wolff3558b12021-10-08 15:34:41 +0200697 id = g_strdup(qdict_get_try_str(opts, "id"));
698 if (!qdev_set_id(dev, id, errp)) {
Damien Hedde4a1d9372021-10-08 15:34:36 +0200699 goto err_del_dev;
700 }
Bandan Das52aa17c2014-02-26 10:32:40 -0700701
Amos Kong7b030942014-03-03 15:57:55 +0800702 /* set properties */
Kevin Wolff3558b12021-10-08 15:34:41 +0200703 dev->opts = qdict_clone_shallow(opts);
704 qdict_del(dev->opts, "driver");
705 qdict_del(dev->opts, "bus");
706 qdict_del(dev->opts, "id");
707
708 object_set_properties_from_keyval(&dev->parent_obj, dev->opts, from_json,
709 errp);
710 if (*errp) {
Thomas Huth58346212017-11-02 11:10:05 +0100711 goto err_del_dev;
Amos Kong7b030942014-03-03 15:57:55 +0800712 }
713
Markus Armbruster992861f2020-07-07 18:06:04 +0200714 if (!qdev_realize(DEVICE(dev), bus, errp)) {
Thomas Huth58346212017-11-02 11:10:05 +0100715 goto err_del_dev;
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200716 }
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200717 return dev;
Thomas Huth58346212017-11-02 11:10:05 +0100718
719err_del_dev:
Jens Freimannf3a85052019-10-29 12:48:55 +0100720 if (dev) {
721 object_unparent(OBJECT(dev));
722 object_unref(OBJECT(dev));
723 }
Thomas Huth58346212017-11-02 11:10:05 +0100724 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600725}
726
Kevin Wolff3558b12021-10-08 15:34:41 +0200727/* Takes ownership of @opts on success */
728DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
729{
730 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
731 DeviceState *ret;
732
733 ret = qdev_device_add_from_qdict(qdict, false, errp);
734 if (ret) {
735 qemu_opts_del(opts);
736 }
737 qobject_unref(qdict);
738 return ret;
739}
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600740
741#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
742static void qbus_print(Monitor *mon, BusState *bus, int indent);
743
744static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200745 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600746{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600747 if (!props)
748 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100749 for (; props->name; props++) {
Paolo Bonzinid8229792012-02-02 09:47:13 +0100750 char *value;
751 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
Markus Armbrustera43770d2020-07-07 18:06:13 +0200752
Paolo Bonzinid8229792012-02-02 09:47:13 +0100753 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
Markus Armbrustera43770d2020-07-07 18:06:13 +0200754 value = object_property_get_str(OBJECT(dev), legacy_name, NULL);
Paolo Bonzinid8229792012-02-02 09:47:13 +0100755 } else {
Markus Armbrustera43770d2020-07-07 18:06:13 +0200756 value = object_property_print(OBJECT(dev), props->name, true,
757 NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600758 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100759 g_free(legacy_name);
760
Markus Armbrustera43770d2020-07-07 18:06:13 +0200761 if (!value) {
Paolo Bonzinid8229792012-02-02 09:47:13 +0100762 continue;
763 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200764 qdev_printf("%s = %s\n", props->name,
Markus Armbrustera43770d2020-07-07 18:06:13 +0200765 *value ? value : "<null>");
Paolo Bonzinid8229792012-02-02 09:47:13 +0100766 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600767 }
768}
769
Anthony Liguori0d936922012-05-02 09:00:20 +0200770static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
771{
772 BusClass *bc = BUS_GET_CLASS(bus);
773
774 if (bc->print_dev) {
775 bc->print_dev(mon, dev, indent);
776 }
777}
778
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600779static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
780{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200781 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600782 BusState *child;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700783 NamedGPIOList *ngl;
Damien Hedde9f2ff992020-04-06 15:52:51 +0200784 NamedClockList *ncl;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700785
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600786 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
787 dev->id ? dev->id : "");
788 indent += 2;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700789 QLIST_FOREACH(ngl, &dev->gpios, node) {
790 if (ngl->num_in) {
791 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
792 ngl->num_in);
793 }
794 if (ngl->num_out) {
795 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
796 ngl->num_out);
797 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600798 }
Damien Hedde9f2ff992020-04-06 15:52:51 +0200799 QLIST_FOREACH(ncl, &dev->clocks, node) {
Peter Maydellb7cd9c12020-12-15 15:09:29 +0000800 g_autofree char *freq_str = clock_display_freq(ncl->clock);
801 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
Damien Hedde9f2ff992020-04-06 15:52:51 +0200802 ncl->output ? "out" : "in",
803 ncl->alias ? " (alias)" : "",
Peter Maydellb7cd9c12020-12-15 15:09:29 +0000804 ncl->name, freq_str);
Damien Hedde9f2ff992020-04-06 15:52:51 +0200805 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200806 class = object_get_class(OBJECT(dev));
807 do {
Paolo Bonzini385d8f22020-01-23 12:11:38 +0100808 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
Paolo Bonzinibce54472012-03-28 18:12:47 +0200809 class = object_class_get_parent(class);
810 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200811 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600812 QLIST_FOREACH(child, &dev->child_bus, sibling) {
813 qbus_print(mon, child, indent);
814 }
815}
816
817static void qbus_print(Monitor *mon, BusState *bus, int indent)
818{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600819 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600820
821 qdev_printf("bus: %s\n", bus->name);
822 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200823 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600824 QTAILQ_FOREACH(kid, &bus->children, sibling) {
825 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600826 qdev_print(mon, dev, indent);
827 }
828}
829#undef qdev_printf
830
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100831void hmp_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600832{
833 if (sysbus_get_default())
834 qbus_print(mon, sysbus_get_default(), 0);
835}
836
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100837void hmp_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600838{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200839 qdev_print_devinfos(true);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600840}
841
Markus Armbruster485febc2015-03-13 17:25:50 +0100842void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600843{
844 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100845 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600846
Markus Armbrusterc6ecec42020-07-07 18:05:35 +0200847 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp);
848 if (!opts) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100849 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600850 }
851 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
852 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100853 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600854 }
Markus Armbruster668f62e2020-07-07 18:06:02 +0200855 dev = qdev_device_add(opts, errp);
Maxim Levitsky7bed8992020-10-06 14:38:58 +0200856
857 /*
858 * Drain all pending RCU callbacks. This is done because
859 * some bus related operations can delay a device removal
860 * (in this case this can happen if device is added and then
861 * removed due to a configuration error)
862 * to a RCU callback, but user might expect that this interface
863 * will finish its job completely once qmp command returns result
864 * to the user
865 */
866 drain_call_rcu();
867
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100868 if (!dev) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600869 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100870 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600871 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100872 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600873}
874
Kevin Wolf6c1db522016-09-20 13:38:41 +0200875static DeviceState *find_device_state(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600876{
Markus Armbruster1bf4d322021-10-19 10:57:11 +0200877 Object *obj = object_resolve_path_at(qdev_get_peripheral(), id);
Markus Armbruster69520262021-09-16 13:17:07 +0200878 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600879
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000880 if (!obj) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100881 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
882 "Device '%s' not found", id);
Kevin Wolf6c1db522016-09-20 13:38:41 +0200883 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600884 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300885
Markus Armbruster69520262021-09-16 13:17:07 +0200886 dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
887 if (!dev) {
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100888 error_setg(errp, "%s is not a hotpluggable device", id);
Kevin Wolf6c1db522016-09-20 13:38:41 +0200889 return NULL;
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100890 }
891
Markus Armbruster69520262021-09-16 13:17:07 +0200892 return dev;
Kevin Wolf6c1db522016-09-20 13:38:41 +0200893}
894
Juan Quintela32900672017-03-28 11:22:51 +0200895void qdev_unplug(DeviceState *dev, Error **errp)
896{
897 DeviceClass *dc = DEVICE_GET_CLASS(dev);
898 HotplugHandler *hotplug_ctrl;
899 HotplugHandlerClass *hdc;
David Hildenbrand07578b02019-02-28 13:28:47 +0100900 Error *local_err = NULL;
Juan Quintela32900672017-03-28 11:22:51 +0200901
Jagannathan Raman217c7f02022-06-13 16:26:21 -0400902 if (qdev_unplug_blocked(dev, errp)) {
903 return;
904 }
905
Juan Quintela32900672017-03-28 11:22:51 +0200906 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
907 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
908 return;
909 }
910
911 if (!dc->hotpluggable) {
912 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
913 object_get_typename(OBJECT(dev)));
914 return;
915 }
916
Jens Freimanna1190ab2019-10-29 12:49:01 +0100917 if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
Juan Quintelab06424d2017-03-22 17:34:27 +0100918 error_setg(errp, "device_del not allowed while migrating");
919 return;
920 }
921
Juan Quintela32900672017-03-28 11:22:51 +0200922 qdev_hot_removed = true;
923
924 hotplug_ctrl = qdev_get_hotplug_handler(dev);
925 /* hotpluggable device MUST have HotplugHandler, if it doesn't
926 * then something is very wrong with it */
927 g_assert(hotplug_ctrl);
928
929 /* If device supports async unplug just request it to be done,
930 * otherwise just remove it synchronously */
931 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
932 if (hdc->unplug_request) {
David Hildenbrand07578b02019-02-28 13:28:47 +0100933 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
Juan Quintela32900672017-03-28 11:22:51 +0200934 } else {
David Hildenbrand07578b02019-02-28 13:28:47 +0100935 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
936 if (!local_err) {
937 object_unparent(OBJECT(dev));
938 }
Juan Quintela32900672017-03-28 11:22:51 +0200939 }
David Hildenbrand07578b02019-02-28 13:28:47 +0100940 error_propagate(errp, local_err);
Juan Quintela32900672017-03-28 11:22:51 +0200941}
942
Kevin Wolf6c1db522016-09-20 13:38:41 +0200943void qmp_device_del(const char *id, Error **errp)
944{
945 DeviceState *dev = find_device_state(id, errp);
946 if (dev != NULL) {
Gerd Hoffmann18416c62021-11-11 14:08:59 +0100947 if (dev->pending_deleted_event &&
948 (dev->pending_deleted_expires_ms == 0 ||
949 dev->pending_deleted_expires_ms > qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL))) {
Julia Suvorovacce89442020-02-20 17:55:56 +0100950 error_setg(errp, "Device %s is already in the "
951 "process of unplug", id);
952 return;
953 }
954
Kevin Wolf6c1db522016-09-20 13:38:41 +0200955 qdev_unplug(dev, errp);
956 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600957}
958
Markus Armbrusterd94bd092019-07-09 20:59:36 +0200959void hmp_device_add(Monitor *mon, const QDict *qdict)
960{
961 Error *err = NULL;
962
963 qmp_device_add((QDict *)qdict, NULL, &err);
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +0300964 hmp_handle_error(mon, err);
Markus Armbrusterd94bd092019-07-09 20:59:36 +0200965}
966
967void hmp_device_del(Monitor *mon, const QDict *qdict)
968{
969 const char *id = qdict_get_str(qdict, "id");
970 Error *err = NULL;
971
972 qmp_device_del(id, &err);
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +0300973 hmp_handle_error(mon, err);
Markus Armbrusterd94bd092019-07-09 20:59:36 +0200974}
975
Markus Armbruster9c9c5ce2023-01-24 13:19:39 +0100976void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
977{
978 GSList *list, *elt;
979 size_t len;
980
981 if (nb_args != 2) {
982 return;
983 }
984
985 len = strlen(str);
986 readline_set_completion_index(rs, len);
987 list = elt = object_class_get_list(TYPE_DEVICE, false);
988 while (elt) {
989 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
990 TYPE_DEVICE);
991
992 if (dc->user_creatable) {
993 readline_add_completion_of(rs, str,
994 object_class_get_name(OBJECT_CLASS(dc)));
995 }
996 elt = elt->next;
997 }
998 g_slist_free(list);
999}
1000
1001static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1002{
1003 GSList **list = opaque;
1004 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
1005
1006 if (dev == NULL) {
1007 return 0;
1008 }
1009
1010 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1011 *list = g_slist_append(*list, dev);
1012 }
1013
1014 return 0;
1015}
1016
1017static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1018{
1019 GSList *list = NULL;
1020
1021 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1022
1023 return list;
1024}
1025
1026static void peripheral_device_del_completion(ReadLineState *rs,
1027 const char *str)
1028{
1029 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1030 GSList *list, *item;
1031
1032 list = qdev_build_hotpluggable_device_list(peripheral);
1033 if (!list) {
1034 return;
1035 }
1036
1037 for (item = list; item; item = g_slist_next(item)) {
1038 DeviceState *dev = item->data;
1039
1040 if (dev->id) {
1041 readline_add_completion_of(rs, str, dev->id);
1042 }
1043 }
1044
1045 g_slist_free(list);
1046}
1047
1048void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1049{
1050 if (nb_args != 2) {
1051 return;
1052 }
1053
1054 readline_set_completion_index(rs, strlen(str));
1055 peripheral_device_del_completion(rs, str);
1056}
1057
Kevin Wolf9680cae2016-09-20 13:38:42 +02001058BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
1059{
1060 DeviceState *dev;
1061 BlockBackend *blk;
1062
Emanuele Giuseppe Esposito0439c5a2022-03-03 10:15:53 -05001063 GLOBAL_STATE_CODE();
1064
Kevin Wolf9680cae2016-09-20 13:38:42 +02001065 dev = find_device_state(id, errp);
1066 if (dev == NULL) {
1067 return NULL;
1068 }
1069
1070 blk = blk_by_dev(dev);
1071 if (!blk) {
1072 error_setg(errp, "Device does not have a block device backend");
1073 }
1074 return blk;
1075}
1076
Paolo Bonzini4d454572012-11-26 16:03:42 +01001077QemuOptsList qemu_device_opts = {
1078 .name = "device",
1079 .implied_opt_name = "driver",
1080 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
1081 .desc = {
1082 /*
1083 * no elements => accept any
1084 * sanity checking will happen later
1085 * when setting device properties
1086 */
1087 { /* end of list */ }
1088 },
1089};
1090
1091QemuOptsList qemu_global_opts = {
1092 .name = "global",
1093 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
1094 .desc = {
1095 {
1096 .name = "driver",
1097 .type = QEMU_OPT_STRING,
1098 },{
1099 .name = "property",
1100 .type = QEMU_OPT_STRING,
1101 },{
1102 .name = "value",
1103 .type = QEMU_OPT_STRING,
1104 },
1105 { /* end of list */ }
1106 },
1107};
1108
1109int qemu_global_option(const char *str)
1110{
1111 char driver[64], property[64];
1112 QemuOpts *opts;
1113 int rc, offset;
1114
Paolo Bonzini3751d7c2015-04-09 14:16:19 +02001115 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
1116 if (rc == 2 && str[offset] == '=') {
1117 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
1118 qemu_opt_set(opts, "driver", driver, &error_abort);
1119 qemu_opt_set(opts, "property", property, &error_abort);
1120 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
1121 return 0;
1122 }
1123
Markus Armbruster70b94332015-02-13 12:50:26 +01001124 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
Paolo Bonzini3751d7c2015-04-09 14:16:19 +02001125 if (!opts) {
Paolo Bonzini4d454572012-11-26 16:03:42 +01001126 return -1;
1127 }
Rohit Kumar818e1632022-02-15 23:15:08 -08001128 if (!qemu_opt_get(opts, "driver")
1129 || !qemu_opt_get(opts, "property")
1130 || !qemu_opt_get(opts, "value")) {
1131 error_report("options 'driver', 'property', and 'value'"
1132 " are required");
1133 return -1;
1134 }
Paolo Bonzini4d454572012-11-26 16:03:42 +01001135
Paolo Bonzini4d454572012-11-26 16:03:42 +01001136 return 0;
1137}
Paolo Bonzini164dafd2020-10-27 04:44:18 -04001138
1139bool qmp_command_available(const QmpCommand *cmd, Error **errp)
1140{
Paolo Bonzini2f181fb2020-11-12 09:38:36 -05001141 if (!phase_check(PHASE_MACHINE_READY) &&
Paolo Bonzini164dafd2020-10-27 04:44:18 -04001142 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
1143 error_setg(errp, "The command '%s' is permitted only after machine initialization has completed",
1144 cmd->name);
1145 return false;
1146 }
1147 return true;
1148}