blob: 12fe60c4670df9c7a9e15ffb0a4e9a06cfb77c40 [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 | \
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
Sascha Silbe36e99162016-02-18 22:44:13 +010067/* Please keep this table sorted by typename. */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060068static const QDevAlias qdev_alias_table[] = {
Gerd Hoffmann2957f5a2020-07-02 15:25:09 +020069 { "AC97", "ac97" }, /* -soundhw name */
Sascha Silbe36e99162016-02-18 22:44:13 +010070 { "e1000", "e1000-82540em" },
Gerd Hoffmann0e933fe2020-07-02 15:25:10 +020071 { "ES1370", "es1370" }, /* -soundhw name */
Sascha Silbe36e99162016-02-18 22:44:13 +010072 { "ich9-ahci", "ahci" },
Sascha Silbe36e99162016-02-18 22:44:13 +010073 { "lsi53c895a", "lsi" },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000074 { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000075 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW },
76 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000077 { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000078 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW },
79 { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000080 { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000081 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW },
82 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000083 { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000084 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW },
85 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI },
Akihiko Odakif4ba24b2022-02-13 11:18:00 +090086 { "virtio-gpu-gl-device", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_MMIO },
87 { "virtio-gpu-gl-pci", "virtio-gpu-gl", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000088 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000089 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW },
90 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI },
91 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000092 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000093 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW },
94 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000095 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000096 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW },
97 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +000098 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +000099 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW },
100 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +0000101 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +0000102 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW },
103 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +0000104 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +0000105 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW },
106 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI },
Laurent Vivier4c5806a2021-03-23 16:53:03 +0000107 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +0000108 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW },
109 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI},
Laurent Vivier4c5806a2021-03-23 16:53:03 +0000110 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO },
Laurent Vivier203adb42021-03-23 16:53:02 +0000111 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW },
112 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600113 { }
114};
115
116static const char *qdev_class_get_alias(DeviceClass *dc)
117{
118 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
119 int i;
120
121 for (i = 0; qdev_alias_table[i].typename; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200122 if (qdev_alias_table[i].arch_mask &&
123 !(qdev_alias_table[i].arch_mask & arch_type)) {
124 continue;
125 }
126
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600127 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
128 return qdev_alias_table[i].alias;
129 }
130 }
131
132 return NULL;
133}
134
135static bool qdev_class_has_alias(DeviceClass *dc)
136{
137 return (qdev_class_get_alias(dc) != NULL);
138}
139
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200140static void qdev_print_devinfo(DeviceClass *dc)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600141{
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200142 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
Anthony Liguori0d936922012-05-02 09:00:20 +0200143 if (dc->bus_type) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200144 qemu_printf(", bus %s", dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600145 }
146 if (qdev_class_has_alias(dc)) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200147 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600148 }
149 if (dc->desc) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200150 qemu_printf(", desc \"%s\"", dc->desc);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600151 }
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300152 if (!dc->user_creatable) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200153 qemu_printf(", no-user");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600154 }
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200155 qemu_printf("\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600156}
157
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200158static void qdev_print_devinfos(bool show_no_user)
159{
160 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
161 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
162 [DEVICE_CATEGORY_USB] = "USB",
163 [DEVICE_CATEGORY_STORAGE] = "Storage",
164 [DEVICE_CATEGORY_NETWORK] = "Network",
165 [DEVICE_CATEGORY_INPUT] = "Input",
166 [DEVICE_CATEGORY_DISPLAY] = "Display",
167 [DEVICE_CATEGORY_SOUND] = "Sound",
168 [DEVICE_CATEGORY_MISC] = "Misc",
Thomas Huthba31cc72017-01-20 14:01:16 +0100169 [DEVICE_CATEGORY_CPU] = "CPU",
Paolo Bonzinib10cb622021-10-27 14:34:53 +0200170 [DEVICE_CATEGORY_WATCHDOG]= "Watchdog",
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200171 [DEVICE_CATEGORY_MAX] = "Uncategorized",
172 };
173 GSList *list, *elt;
174 int i;
175 bool cat_printed;
176
Gerd Hoffmann7ab6e7f2020-06-24 15:10:38 +0200177 module_load_qom_all();
Paolo Bonzini47c66002018-03-03 08:33:10 +0100178 list = object_class_get_list_sorted(TYPE_DEVICE, false);
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200179
180 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
181 cat_printed = false;
182 for (elt = list; elt; elt = elt->next) {
183 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
184 TYPE_DEVICE);
185 if ((i < DEVICE_CATEGORY_MAX
186 ? !test_bit(i, dc->categories)
187 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100188 || (!show_no_user
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300189 && !dc->user_creatable)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200190 continue;
191 }
192 if (!cat_printed) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200193 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200194 cat_printed = true;
195 }
196 qdev_print_devinfo(dc);
197 }
198 }
199
200 g_slist_free(list);
201}
202
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600203static const char *find_typename_by_alias(const char *alias)
204{
205 int i;
206
207 for (i = 0; qdev_alias_table[i].alias; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200208 if (qdev_alias_table[i].arch_mask &&
209 !(qdev_alias_table[i].arch_mask & arch_type)) {
210 continue;
211 }
212
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600213 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
214 return qdev_alias_table[i].typename;
215 }
216 }
217
218 return NULL;
219}
220
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200221static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
222{
223 ObjectClass *oc;
224 DeviceClass *dc;
Sascha Silbef6b53192016-02-18 22:44:12 +0100225 const char *original_name = *driver;
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200226
Gerd Hoffmann7ab6e7f2020-06-24 15:10:38 +0200227 oc = module_object_class_by_name(*driver);
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200228 if (!oc) {
229 const char *typename = find_typename_by_alias(*driver);
230
231 if (typename) {
232 *driver = typename;
Gerd Hoffmann7ab6e7f2020-06-24 15:10:38 +0200233 oc = module_object_class_by_name(*driver);
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200234 }
235 }
236
237 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
Sascha Silbef6b53192016-02-18 22:44:12 +0100238 if (*driver != original_name) {
239 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
240 " name", original_name, *driver);
241 } else {
242 error_setg(errp, "'%s' is not a valid device model name", *driver);
243 }
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200244 return NULL;
245 }
246
247 if (object_class_is_abstract(oc)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100248 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
Markus Armbruster6cc06672020-11-13 09:26:26 +0100249 "a non-abstract device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200250 return NULL;
251 }
252
253 dc = DEVICE_CLASS(oc);
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300254 if (!dc->user_creatable ||
Paolo Bonzini2f181fb2020-11-12 09:38:36 -0500255 (phase_check(PHASE_MACHINE_READY) && !dc->hotpluggable)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100256 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
Markus Armbruster6cc06672020-11-13 09:26:26 +0100257 "a pluggable device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200258 return NULL;
259 }
260
Damien Hedde312e1b12021-10-29 16:22:57 +0200261 if (object_class_dynamic_cast(oc, TYPE_SYS_BUS_DEVICE)) {
262 /* sysbus devices need to be allowed by the machine */
263 MachineClass *mc = MACHINE_CLASS(object_get_class(qdev_get_machine()));
264 if (!device_type_is_dynamic_sysbus(mc, *driver)) {
265 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
266 "a dynamic sysbus device type for the machine");
267 return NULL;
268 }
269 }
270
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200271 return dc;
272}
273
274
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600275int qdev_device_help(QemuOpts *opts)
276{
Stefan Hajnoczief523582014-07-09 14:01:32 +0200277 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600278 const char *driver;
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100279 ObjectPropertyInfoList *prop_list;
280 ObjectPropertyInfoList *prop;
Marc-André Lureaue1043d62020-01-10 19:30:39 +0400281 GPtrArray *array;
282 int i;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600283
284 driver = qemu_opt_get(opts, "driver");
Peter Maydellc8057f92012-08-02 13:45:54 +0100285 if (driver && is_help_option(driver)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200286 qdev_print_devinfos(false);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600287 return 1;
288 }
289
Peter Maydellc8057f92012-08-02 13:45:54 +0100290 if (!driver || !qemu_opt_has_help_opt(opts)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600291 return 0;
292 }
293
Markus Armbruster33fe9682015-10-01 10:59:59 +0200294 if (!object_class_by_name(driver)) {
295 const char *typename = find_typename_by_alias(driver);
296
297 if (typename) {
298 driver = typename;
299 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600300 }
301
Stefan Hajnoczief523582014-07-09 14:01:32 +0200302 prop_list = qmp_device_list_properties(driver, &local_err);
Gonglei0722eba2014-09-16 10:19:33 +0800303 if (local_err) {
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200304 goto error;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600305 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200306
Max Reitz9c2762b2018-10-19 18:49:27 +0200307 if (prop_list) {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200308 qemu_printf("%s options:\n", driver);
Max Reitz9c2762b2018-10-19 18:49:27 +0200309 } else {
Markus Armbruster8acb2a72019-04-17 21:06:41 +0200310 qemu_printf("There are no options for %s.\n", driver);
Max Reitz9c2762b2018-10-19 18:49:27 +0200311 }
Marc-André Lureaue1043d62020-01-10 19:30:39 +0400312 array = g_ptr_array_new();
Stefan Hajnoczief523582014-07-09 14:01:32 +0200313 for (prop = prop_list; prop; prop = prop->next) {
Marc-André Lureaue1043d62020-01-10 19:30:39 +0400314 g_ptr_array_add(array,
315 object_property_help(prop->value->name,
316 prop->value->type,
317 prop->value->default_value,
318 prop->value->description));
Stefan Hajnoczief523582014-07-09 14:01:32 +0200319 }
Marc-André Lureaue1043d62020-01-10 19:30:39 +0400320 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
321 for (i = 0; i < array->len; i++) {
Markus Armbruster029afc42020-07-14 18:01:58 +0200322 qemu_printf("%s\n", (char *)array->pdata[i]);
Marc-André Lureaue1043d62020-01-10 19:30:39 +0400323 }
324 g_ptr_array_set_free_func(array, g_free);
325 g_ptr_array_free(array, true);
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100326 qapi_free_ObjectPropertyInfoList(prop_list);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600327 return 1;
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200328
329error:
Markus Armbruster78288672015-12-18 16:35:07 +0100330 error_report_err(local_err);
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200331 return 1;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600332}
333
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600334static Object *qdev_get_peripheral(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600335{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600336 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600337
338 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200339 dev = container_get(qdev_get_machine(), "/peripheral");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600340 }
341
Anthony Liguori8b45d442011-12-23 09:08:05 -0600342 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600343}
344
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600345static Object *qdev_get_peripheral_anon(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600346{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600347 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600348
349 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200350 dev = container_get(qdev_get_machine(), "/peripheral-anon");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600351 }
352
Anthony Liguori8b45d442011-12-23 09:08:05 -0600353 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600354}
355
Vladimir Sementsov-Ogievskiy34077322019-12-05 20:46:20 +0300356static void qbus_error_append_bus_list_hint(DeviceState *dev,
357 Error *const *errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600358{
359 BusState *child;
360 const char *sep = " ";
361
Eric Blake50b7b002015-09-10 10:19:16 -0600362 error_append_hint(errp, "child buses at \"%s\":",
363 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600364 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Eric Blake50b7b002015-09-10 10:19:16 -0600365 error_append_hint(errp, "%s\"%s\"", sep, child->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600366 sep = ", ";
367 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100368 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600369}
370
Vladimir Sementsov-Ogievskiy34077322019-12-05 20:46:20 +0300371static void qbus_error_append_dev_list_hint(BusState *bus,
372 Error *const *errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600373{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600374 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600375 const char *sep = " ";
376
Eric Blake50b7b002015-09-10 10:19:16 -0600377 error_append_hint(errp, "devices at \"%s\":", bus->name);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600378 QTAILQ_FOREACH(kid, &bus->children, sibling) {
379 DeviceState *dev = kid->child;
Eric Blake50b7b002015-09-10 10:19:16 -0600380 error_append_hint(errp, "%s\"%s\"", sep,
381 object_get_typename(OBJECT(dev)));
382 if (dev->id) {
383 error_append_hint(errp, "/\"%s\"", dev->id);
384 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600385 sep = ", ";
386 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100387 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600388}
389
390static BusState *qbus_find_bus(DeviceState *dev, char *elem)
391{
392 BusState *child;
393
394 QLIST_FOREACH(child, &dev->child_bus, sibling) {
395 if (strcmp(child->name, elem) == 0) {
396 return child;
397 }
398 }
399 return NULL;
400}
401
402static DeviceState *qbus_find_dev(BusState *bus, char *elem)
403{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600404 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600405
406 /*
407 * try to match in order:
408 * (1) instance id, if present
409 * (2) driver name
410 * (3) driver alias, if present
411 */
Anthony Liguori0866aca2011-12-23 15:34:39 -0600412 QTAILQ_FOREACH(kid, &bus->children, sibling) {
413 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600414 if (dev->id && strcmp(dev->id, elem) == 0) {
415 return dev;
416 }
417 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600418 QTAILQ_FOREACH(kid, &bus->children, sibling) {
419 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600420 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
421 return dev;
422 }
423 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600424 QTAILQ_FOREACH(kid, &bus->children, sibling) {
425 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600426 DeviceClass *dc = DEVICE_GET_CLASS(dev);
427
428 if (qdev_class_has_alias(dc) &&
429 strcmp(qdev_class_get_alias(dc), elem) == 0) {
430 return dev;
431 }
432 }
433 return NULL;
434}
435
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100436static inline bool qbus_is_full(BusState *bus)
437{
Peter Maydell15185622021-09-13 16:07:25 +0100438 BusClass *bus_class;
439
440 if (bus->full) {
441 return true;
442 }
443 bus_class = BUS_GET_CLASS(bus);
Tony Krowiak12b2e9f2018-12-17 10:57:30 -0500444 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100445}
446
447/*
448 * Search the tree rooted at @bus for a bus.
449 * If @name, search for a bus with that name. Note that bus names
450 * need not be unique. Yes, that's screwed up.
451 * Else search for a bus that is a subtype of @bus_typename.
452 * If more than one exists, prefer one that can take another device.
453 * Return the bus if found, else %NULL.
454 */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600455static BusState *qbus_find_recursive(BusState *bus, const char *name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200456 const char *bus_typename)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600457{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600458 BusChild *kid;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100459 BusState *pick, *child, *ret;
460 bool match;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600461
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100462 assert(name || bus_typename);
463 if (name) {
464 match = !strcmp(bus->name, name);
465 } else {
466 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
KONRAD Frederic1395af62013-01-15 00:08:00 +0100467 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100468
469 if (match && !qbus_is_full(bus)) {
470 return bus; /* root matches and isn't full */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600471 }
472
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100473 pick = match ? bus : NULL;
474
Anthony Liguori0866aca2011-12-23 15:34:39 -0600475 QTAILQ_FOREACH(kid, &bus->children, sibling) {
476 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600477 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200478 ret = qbus_find_recursive(child, name, bus_typename);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100479 if (ret && !qbus_is_full(ret)) {
480 return ret; /* a descendant matches and isn't full */
481 }
482 if (ret && !pick) {
483 pick = ret;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600484 }
485 }
486 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100487
488 /* root or a descendant matches, but is full */
489 return pick;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600490}
491
Markus Armbrusterd2828422015-03-11 19:16:04 +0100492static BusState *qbus_find(const char *path, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600493{
494 DeviceState *dev;
495 BusState *bus;
496 char elem[128];
497 int pos, len;
498
499 /* find start element */
500 if (path[0] == '/') {
501 bus = sysbus_get_default();
502 pos = 0;
503 } else {
504 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
505 assert(!path[0]);
506 elem[0] = len = 0;
507 }
508 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
509 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100510 error_setg(errp, "Bus '%s' not found", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600511 return NULL;
512 }
513 pos = len;
514 }
515
516 for (;;) {
517 assert(path[pos] == '/' || !path[pos]);
518 while (path[pos] == '/') {
519 pos++;
520 }
521 if (path[pos] == '\0') {
Markus Armbrustered238ba2015-03-11 18:39:16 +0100522 break;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600523 }
524
525 /* find device */
526 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200527 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600528 elem[0] = len = 0;
529 }
530 pos += len;
531 dev = qbus_find_dev(bus, elem);
532 if (!dev) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100533 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
534 "Device '%s' not found", elem);
Vladimir Sementsov-Ogievskiy34077322019-12-05 20:46:20 +0300535 qbus_error_append_dev_list_hint(bus, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600536 return NULL;
537 }
538
539 assert(path[pos] == '/' || !path[pos]);
540 while (path[pos] == '/') {
541 pos++;
542 }
543 if (path[pos] == '\0') {
544 /* last specified element is a device. If it has exactly
545 * one child bus accept it nevertheless */
Markus Armbrustered238ba2015-03-11 18:39:16 +0100546 if (dev->num_child_bus == 1) {
547 bus = QLIST_FIRST(&dev->child_bus);
548 break;
549 }
550 if (dev->num_child_bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100551 error_setg(errp, "Device '%s' has multiple child buses",
552 elem);
Vladimir Sementsov-Ogievskiy34077322019-12-05 20:46:20 +0300553 qbus_error_append_bus_list_hint(dev, errp);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100554 } else {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100555 error_setg(errp, "Device '%s' has no child bus", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600556 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100557 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600558 }
559
560 /* find bus */
561 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200562 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600563 elem[0] = len = 0;
564 }
565 pos += len;
566 bus = qbus_find_bus(dev, elem);
567 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100568 error_setg(errp, "Bus '%s' not found", elem);
Vladimir Sementsov-Ogievskiy34077322019-12-05 20:46:20 +0300569 qbus_error_append_bus_list_hint(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600570 return NULL;
571 }
572 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100573
574 if (qbus_is_full(bus)) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100575 error_setg(errp, "Bus '%s' is full", path);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100576 return NULL;
577 }
578 return bus;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600579}
580
Kevin Wolf163f3842021-10-08 15:34:35 +0200581/* Takes ownership of @id, will be freed when deleting the device */
Damien Hedde4a1d9372021-10-08 15:34:36 +0200582const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
Juergen Grossce49b732016-11-22 07:10:57 +0100583{
Damien Hedde4a1d9372021-10-08 15:34:36 +0200584 ObjectProperty *prop;
Juergen Grossce49b732016-11-22 07:10:57 +0100585
Damien Hedde4a1d9372021-10-08 15:34:36 +0200586 assert(!dev->id && !dev->realized);
587
588 /*
589 * object_property_[try_]add_child() below will assert the device
590 * has no parent
591 */
592 if (id) {
593 prop = object_property_try_add_child(qdev_get_peripheral(), id,
594 OBJECT(dev), NULL);
595 if (prop) {
596 dev->id = id;
597 } else {
Damien Hedde4a1d9372021-10-08 15:34:36 +0200598 error_setg(errp, "Duplicate device ID '%s'", id);
Stefan Hajnoczib66cecb2021-11-02 16:33:42 +0000599 g_free(id);
Damien Hedde4a1d9372021-10-08 15:34:36 +0200600 return NULL;
601 }
Juergen Grossce49b732016-11-22 07:10:57 +0100602 } else {
603 static int anon_count;
604 gchar *name = g_strdup_printf("device[%d]", anon_count++);
Damien Hedde4a1d9372021-10-08 15:34:36 +0200605 prop = object_property_add_child(qdev_get_peripheral_anon(), name,
606 OBJECT(dev));
Juergen Grossce49b732016-11-22 07:10:57 +0100607 g_free(name);
608 }
Damien Hedde4a1d9372021-10-08 15:34:36 +0200609
610 return prop->name;
Juergen Grossce49b732016-11-22 07:10:57 +0100611}
612
Kevin Wolff3558b12021-10-08 15:34:41 +0200613DeviceState *qdev_device_add_from_qdict(const QDict *opts,
614 bool from_json, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600615{
Kevin Wolf7d618082021-10-08 15:34:38 +0200616 ERRP_GUARD();
Andreas Färberf4d85792013-08-24 01:21:22 +0200617 DeviceClass *dc;
Juergen Grossce49b732016-11-22 07:10:57 +0100618 const char *driver, *path;
Kevin Wolff3558b12021-10-08 15:34:41 +0200619 char *id;
Jens Freimannf3a85052019-10-29 12:48:55 +0100620 DeviceState *dev = NULL;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200621 BusState *bus = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600622
Kevin Wolff3558b12021-10-08 15:34:41 +0200623 driver = qdict_get_try_str(opts, "driver");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600624 if (!driver) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100625 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600626 return NULL;
627 }
628
629 /* find driver */
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100630 dc = qdev_get_device_class(&driver, errp);
631 if (!dc) {
Markus Armbruster7ea5e782013-11-28 17:27:03 +0100632 return NULL;
633 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600634
635 /* find bus */
Kevin Wolff3558b12021-10-08 15:34:41 +0200636 path = qdict_get_try_str(opts, "bus");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600637 if (path != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100638 bus = qbus_find(path, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600639 if (!bus) {
640 return NULL;
641 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200642 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100643 error_setg(errp, "Device '%s' can't go on %s bus",
644 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600645 return NULL;
646 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200647 } else if (dc->bus_type != NULL) {
648 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100649 if (!bus || qbus_is_full(bus)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100650 error_setg(errp, "No '%s' bus found for device '%s'",
651 dc->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600652 return NULL;
653 }
654 }
Jens Freimannf3a85052019-10-29 12:48:55 +0100655
Laurent Vivierbcfc9062021-10-19 09:15:32 +0200656 if (qdev_should_hide_device(opts, from_json, errp)) {
657 if (bus && !qbus_is_hotpluggable(bus)) {
658 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
Juan Quintelafec037c2020-11-18 09:37:42 +0100659 }
Laurent Vivierbcfc9062021-10-19 09:15:32 +0200660 return NULL;
661 } else if (*errp) {
662 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600663 }
664
Paolo Bonzini2f181fb2020-11-12 09:38:36 -0500665 if (phase_check(PHASE_MACHINE_READY) && bus && !qbus_is_hotpluggable(bus)) {
Juan Quintela5f2ef3b2020-11-18 09:37:40 +0100666 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
Jens Freimannf3a85052019-10-29 12:48:55 +0100667 return NULL;
668 }
669
Juan Quintelab06424d2017-03-22 17:34:27 +0100670 if (!migration_is_idle()) {
671 error_setg(errp, "device_add not allowed while migrating");
672 return NULL;
673 }
674
Amos Kong7b030942014-03-03 15:57:55 +0800675 /* create device */
Markus Armbruster4e3a6772020-06-10 07:31:54 +0200676 dev = qdev_new(driver);
Andreas Färber2f7bd822013-04-16 03:50:21 +0200677
Peter Xud2321d32019-09-16 16:07:16 +0800678 /* Check whether the hotplug is allowed by the machine */
Paolo Bonzini2f181fb2020-11-12 09:38:36 -0500679 if (phase_check(PHASE_MACHINE_READY)) {
680 if (!qdev_hotplug_allowed(dev, errp)) {
681 goto err_del_dev;
682 }
Peter Xud2321d32019-09-16 16:07:16 +0800683
Paolo Bonzini2f181fb2020-11-12 09:38:36 -0500684 if (!bus && !qdev_get_machine_hotplug_handler(dev)) {
685 /* No bus, no machine hotplug handler --> device is not hotpluggable */
686 error_setg(errp, "Device '%s' can not be hotplugged on this machine",
687 driver);
688 goto err_del_dev;
689 }
Andreas Färber2f7bd822013-04-16 03:50:21 +0200690 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600691
Damien Hedde4a1d9372021-10-08 15:34:36 +0200692 /*
693 * set dev's parent and register its id.
694 * If it fails it means the id is already taken.
695 */
Kevin Wolff3558b12021-10-08 15:34:41 +0200696 id = g_strdup(qdict_get_try_str(opts, "id"));
697 if (!qdev_set_id(dev, id, errp)) {
Damien Hedde4a1d9372021-10-08 15:34:36 +0200698 goto err_del_dev;
699 }
Bandan Das52aa17c2014-02-26 10:32:40 -0700700
Amos Kong7b030942014-03-03 15:57:55 +0800701 /* set properties */
Kevin Wolff3558b12021-10-08 15:34:41 +0200702 dev->opts = qdict_clone_shallow(opts);
703 qdict_del(dev->opts, "driver");
704 qdict_del(dev->opts, "bus");
705 qdict_del(dev->opts, "id");
706
707 object_set_properties_from_keyval(&dev->parent_obj, dev->opts, from_json,
708 errp);
709 if (*errp) {
Thomas Huth58346212017-11-02 11:10:05 +0100710 goto err_del_dev;
Amos Kong7b030942014-03-03 15:57:55 +0800711 }
712
Markus Armbruster992861f2020-07-07 18:06:04 +0200713 if (!qdev_realize(DEVICE(dev), bus, errp)) {
Thomas Huth58346212017-11-02 11:10:05 +0100714 goto err_del_dev;
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200715 }
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200716 return dev;
Thomas Huth58346212017-11-02 11:10:05 +0100717
718err_del_dev:
Jens Freimannf3a85052019-10-29 12:48:55 +0100719 if (dev) {
720 object_unparent(OBJECT(dev));
721 object_unref(OBJECT(dev));
722 }
Thomas Huth58346212017-11-02 11:10:05 +0100723 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600724}
725
Kevin Wolff3558b12021-10-08 15:34:41 +0200726/* Takes ownership of @opts on success */
727DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
728{
729 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
730 DeviceState *ret;
731
732 ret = qdev_device_add_from_qdict(qdict, false, errp);
733 if (ret) {
734 qemu_opts_del(opts);
735 }
736 qobject_unref(qdict);
737 return ret;
738}
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600739
740#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
741static void qbus_print(Monitor *mon, BusState *bus, int indent);
742
743static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200744 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600745{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600746 if (!props)
747 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100748 for (; props->name; props++) {
Paolo Bonzinid8229792012-02-02 09:47:13 +0100749 char *value;
750 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
Markus Armbrustera43770d2020-07-07 18:06:13 +0200751
Paolo Bonzinid8229792012-02-02 09:47:13 +0100752 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
Markus Armbrustera43770d2020-07-07 18:06:13 +0200753 value = object_property_get_str(OBJECT(dev), legacy_name, NULL);
Paolo Bonzinid8229792012-02-02 09:47:13 +0100754 } else {
Markus Armbrustera43770d2020-07-07 18:06:13 +0200755 value = object_property_print(OBJECT(dev), props->name, true,
756 NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600757 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100758 g_free(legacy_name);
759
Markus Armbrustera43770d2020-07-07 18:06:13 +0200760 if (!value) {
Paolo Bonzinid8229792012-02-02 09:47:13 +0100761 continue;
762 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200763 qdev_printf("%s = %s\n", props->name,
Markus Armbrustera43770d2020-07-07 18:06:13 +0200764 *value ? value : "<null>");
Paolo Bonzinid8229792012-02-02 09:47:13 +0100765 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600766 }
767}
768
Anthony Liguori0d936922012-05-02 09:00:20 +0200769static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
770{
771 BusClass *bc = BUS_GET_CLASS(bus);
772
773 if (bc->print_dev) {
774 bc->print_dev(mon, dev, indent);
775 }
776}
777
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600778static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
779{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200780 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600781 BusState *child;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700782 NamedGPIOList *ngl;
Damien Hedde9f2ff992020-04-06 15:52:51 +0200783 NamedClockList *ncl;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700784
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600785 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
786 dev->id ? dev->id : "");
787 indent += 2;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700788 QLIST_FOREACH(ngl, &dev->gpios, node) {
789 if (ngl->num_in) {
790 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
791 ngl->num_in);
792 }
793 if (ngl->num_out) {
794 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
795 ngl->num_out);
796 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600797 }
Damien Hedde9f2ff992020-04-06 15:52:51 +0200798 QLIST_FOREACH(ncl, &dev->clocks, node) {
Peter Maydellb7cd9c12020-12-15 15:09:29 +0000799 g_autofree char *freq_str = clock_display_freq(ncl->clock);
800 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
Damien Hedde9f2ff992020-04-06 15:52:51 +0200801 ncl->output ? "out" : "in",
802 ncl->alias ? " (alias)" : "",
Peter Maydellb7cd9c12020-12-15 15:09:29 +0000803 ncl->name, freq_str);
Damien Hedde9f2ff992020-04-06 15:52:51 +0200804 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200805 class = object_get_class(OBJECT(dev));
806 do {
Paolo Bonzini385d8f22020-01-23 12:11:38 +0100807 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
Paolo Bonzinibce54472012-03-28 18:12:47 +0200808 class = object_class_get_parent(class);
809 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200810 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600811 QLIST_FOREACH(child, &dev->child_bus, sibling) {
812 qbus_print(mon, child, indent);
813 }
814}
815
816static void qbus_print(Monitor *mon, BusState *bus, int indent)
817{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600818 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600819
820 qdev_printf("bus: %s\n", bus->name);
821 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200822 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600823 QTAILQ_FOREACH(kid, &bus->children, sibling) {
824 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600825 qdev_print(mon, dev, indent);
826 }
827}
828#undef qdev_printf
829
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100830void hmp_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600831{
832 if (sysbus_get_default())
833 qbus_print(mon, sysbus_get_default(), 0);
834}
835
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100836void hmp_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600837{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200838 qdev_print_devinfos(true);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600839}
840
Markus Armbruster485febc2015-03-13 17:25:50 +0100841void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600842{
843 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100844 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600845
Markus Armbrusterc6ecec42020-07-07 18:05:35 +0200846 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp);
847 if (!opts) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100848 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600849 }
850 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
851 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100852 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600853 }
Markus Armbruster668f62e2020-07-07 18:06:02 +0200854 dev = qdev_device_add(opts, errp);
Maxim Levitsky7bed8992020-10-06 14:38:58 +0200855
856 /*
857 * Drain all pending RCU callbacks. This is done because
858 * some bus related operations can delay a device removal
859 * (in this case this can happen if device is added and then
860 * removed due to a configuration error)
861 * to a RCU callback, but user might expect that this interface
862 * will finish its job completely once qmp command returns result
863 * to the user
864 */
865 drain_call_rcu();
866
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100867 if (!dev) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600868 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100869 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600870 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100871 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600872}
873
Kevin Wolf6c1db522016-09-20 13:38:41 +0200874static DeviceState *find_device_state(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600875{
Markus Armbruster1bf4d322021-10-19 10:57:11 +0200876 Object *obj = object_resolve_path_at(qdev_get_peripheral(), id);
Markus Armbruster69520262021-09-16 13:17:07 +0200877 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600878
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000879 if (!obj) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100880 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
881 "Device '%s' not found", id);
Kevin Wolf6c1db522016-09-20 13:38:41 +0200882 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600883 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300884
Markus Armbruster69520262021-09-16 13:17:07 +0200885 dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
886 if (!dev) {
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100887 error_setg(errp, "%s is not a hotpluggable device", id);
Kevin Wolf6c1db522016-09-20 13:38:41 +0200888 return NULL;
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100889 }
890
Markus Armbruster69520262021-09-16 13:17:07 +0200891 return dev;
Kevin Wolf6c1db522016-09-20 13:38:41 +0200892}
893
Juan Quintela32900672017-03-28 11:22:51 +0200894void qdev_unplug(DeviceState *dev, Error **errp)
895{
896 DeviceClass *dc = DEVICE_GET_CLASS(dev);
897 HotplugHandler *hotplug_ctrl;
898 HotplugHandlerClass *hdc;
David Hildenbrand07578b02019-02-28 13:28:47 +0100899 Error *local_err = NULL;
Juan Quintela32900672017-03-28 11:22:51 +0200900
901 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
902 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
903 return;
904 }
905
906 if (!dc->hotpluggable) {
907 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
908 object_get_typename(OBJECT(dev)));
909 return;
910 }
911
Jens Freimanna1190ab2019-10-29 12:49:01 +0100912 if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
Juan Quintelab06424d2017-03-22 17:34:27 +0100913 error_setg(errp, "device_del not allowed while migrating");
914 return;
915 }
916
Juan Quintela32900672017-03-28 11:22:51 +0200917 qdev_hot_removed = true;
918
919 hotplug_ctrl = qdev_get_hotplug_handler(dev);
920 /* hotpluggable device MUST have HotplugHandler, if it doesn't
921 * then something is very wrong with it */
922 g_assert(hotplug_ctrl);
923
924 /* If device supports async unplug just request it to be done,
925 * otherwise just remove it synchronously */
926 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
927 if (hdc->unplug_request) {
David Hildenbrand07578b02019-02-28 13:28:47 +0100928 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
Juan Quintela32900672017-03-28 11:22:51 +0200929 } else {
David Hildenbrand07578b02019-02-28 13:28:47 +0100930 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
931 if (!local_err) {
932 object_unparent(OBJECT(dev));
933 }
Juan Quintela32900672017-03-28 11:22:51 +0200934 }
David Hildenbrand07578b02019-02-28 13:28:47 +0100935 error_propagate(errp, local_err);
Juan Quintela32900672017-03-28 11:22:51 +0200936}
937
Kevin Wolf6c1db522016-09-20 13:38:41 +0200938void qmp_device_del(const char *id, Error **errp)
939{
940 DeviceState *dev = find_device_state(id, errp);
941 if (dev != NULL) {
Gerd Hoffmann18416c62021-11-11 14:08:59 +0100942 if (dev->pending_deleted_event &&
943 (dev->pending_deleted_expires_ms == 0 ||
944 dev->pending_deleted_expires_ms > qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL))) {
Julia Suvorovacce89442020-02-20 17:55:56 +0100945 error_setg(errp, "Device %s is already in the "
946 "process of unplug", id);
947 return;
948 }
949
Kevin Wolf6c1db522016-09-20 13:38:41 +0200950 qdev_unplug(dev, errp);
951 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600952}
953
Markus Armbrusterd94bd092019-07-09 20:59:36 +0200954void hmp_device_add(Monitor *mon, const QDict *qdict)
955{
956 Error *err = NULL;
957
958 qmp_device_add((QDict *)qdict, NULL, &err);
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +0300959 hmp_handle_error(mon, err);
Markus Armbrusterd94bd092019-07-09 20:59:36 +0200960}
961
962void hmp_device_del(Monitor *mon, const QDict *qdict)
963{
964 const char *id = qdict_get_str(qdict, "id");
965 Error *err = NULL;
966
967 qmp_device_del(id, &err);
Vladimir Sementsov-Ogievskiy187c6142019-12-05 20:46:18 +0300968 hmp_handle_error(mon, err);
Markus Armbrusterd94bd092019-07-09 20:59:36 +0200969}
970
Kevin Wolf9680cae2016-09-20 13:38:42 +0200971BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
972{
973 DeviceState *dev;
974 BlockBackend *blk;
975
Emanuele Giuseppe Esposito0439c5a2022-03-03 10:15:53 -0500976 GLOBAL_STATE_CODE();
977
Kevin Wolf9680cae2016-09-20 13:38:42 +0200978 dev = find_device_state(id, errp);
979 if (dev == NULL) {
980 return NULL;
981 }
982
983 blk = blk_by_dev(dev);
984 if (!blk) {
985 error_setg(errp, "Device does not have a block device backend");
986 }
987 return blk;
988}
989
Paolo Bonzini4d454572012-11-26 16:03:42 +0100990QemuOptsList qemu_device_opts = {
991 .name = "device",
992 .implied_opt_name = "driver",
993 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
994 .desc = {
995 /*
996 * no elements => accept any
997 * sanity checking will happen later
998 * when setting device properties
999 */
1000 { /* end of list */ }
1001 },
1002};
1003
1004QemuOptsList qemu_global_opts = {
1005 .name = "global",
1006 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
1007 .desc = {
1008 {
1009 .name = "driver",
1010 .type = QEMU_OPT_STRING,
1011 },{
1012 .name = "property",
1013 .type = QEMU_OPT_STRING,
1014 },{
1015 .name = "value",
1016 .type = QEMU_OPT_STRING,
1017 },
1018 { /* end of list */ }
1019 },
1020};
1021
1022int qemu_global_option(const char *str)
1023{
1024 char driver[64], property[64];
1025 QemuOpts *opts;
1026 int rc, offset;
1027
Paolo Bonzini3751d7c2015-04-09 14:16:19 +02001028 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
1029 if (rc == 2 && str[offset] == '=') {
1030 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
1031 qemu_opt_set(opts, "driver", driver, &error_abort);
1032 qemu_opt_set(opts, "property", property, &error_abort);
1033 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
1034 return 0;
1035 }
1036
Markus Armbruster70b94332015-02-13 12:50:26 +01001037 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
Paolo Bonzini3751d7c2015-04-09 14:16:19 +02001038 if (!opts) {
Paolo Bonzini4d454572012-11-26 16:03:42 +01001039 return -1;
1040 }
Rohit Kumar818e1632022-02-15 23:15:08 -08001041 if (!qemu_opt_get(opts, "driver")
1042 || !qemu_opt_get(opts, "property")
1043 || !qemu_opt_get(opts, "value")) {
1044 error_report("options 'driver', 'property', and 'value'"
1045 " are required");
1046 return -1;
1047 }
Paolo Bonzini4d454572012-11-26 16:03:42 +01001048
Paolo Bonzini4d454572012-11-26 16:03:42 +01001049 return 0;
1050}
Paolo Bonzini164dafd2020-10-27 04:44:18 -04001051
1052bool qmp_command_available(const QmpCommand *cmd, Error **errp)
1053{
Paolo Bonzini2f181fb2020-11-12 09:38:36 -05001054 if (!phase_check(PHASE_MACHINE_READY) &&
Paolo Bonzini164dafd2020-10-27 04:44:18 -04001055 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
1056 error_setg(errp, "The command '%s' is permitted only after machine initialization has completed",
1057 cmd->name);
1058 return false;
1059 }
1060 return true;
1061}