qdev-monitor.c 35 KB

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