qdev-monitor.c 33 KB

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