qdev-monitor.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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 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/qdev.h"
  21. #include "hw/sysbus.h"
  22. #include "monitor/monitor.h"
  23. #include "monitor/qdev.h"
  24. #include "qmp-commands.h"
  25. #include "sysemu/arch_init.h"
  26. #include "qapi/qmp/qerror.h"
  27. #include "qemu/config-file.h"
  28. #include "qemu/error-report.h"
  29. /*
  30. * Aliases were a bad idea from the start. Let's keep them
  31. * from spreading further.
  32. */
  33. typedef struct QDevAlias
  34. {
  35. const char *typename;
  36. const char *alias;
  37. uint32_t arch_mask;
  38. } QDevAlias;
  39. static const QDevAlias qdev_alias_table[] = {
  40. { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  41. { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  42. { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  43. { "virtio-balloon-pci", "virtio-balloon",
  44. QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
  45. { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
  46. { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
  47. { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
  48. { "lsi53c895a", "lsi" },
  49. { "ich9-ahci", "ahci" },
  50. { "kvm-pci-assign", "pci-assign" },
  51. { "e1000", "e1000-82540em" },
  52. { }
  53. };
  54. static const char *qdev_class_get_alias(DeviceClass *dc)
  55. {
  56. const char *typename = object_class_get_name(OBJECT_CLASS(dc));
  57. int i;
  58. for (i = 0; qdev_alias_table[i].typename; i++) {
  59. if (qdev_alias_table[i].arch_mask &&
  60. !(qdev_alias_table[i].arch_mask & arch_type)) {
  61. continue;
  62. }
  63. if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
  64. return qdev_alias_table[i].alias;
  65. }
  66. }
  67. return NULL;
  68. }
  69. static bool qdev_class_has_alias(DeviceClass *dc)
  70. {
  71. return (qdev_class_get_alias(dc) != NULL);
  72. }
  73. static void qdev_print_devinfo(DeviceClass *dc)
  74. {
  75. error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
  76. if (dc->bus_type) {
  77. error_printf(", bus %s", dc->bus_type);
  78. }
  79. if (qdev_class_has_alias(dc)) {
  80. error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
  81. }
  82. if (dc->desc) {
  83. error_printf(", desc \"%s\"", dc->desc);
  84. }
  85. if (dc->cannot_instantiate_with_device_add_yet) {
  86. error_printf(", no-user");
  87. }
  88. error_printf("\n");
  89. }
  90. static gint devinfo_cmp(gconstpointer a, gconstpointer b)
  91. {
  92. return strcasecmp(object_class_get_name((ObjectClass *)a),
  93. object_class_get_name((ObjectClass *)b));
  94. }
  95. static void qdev_print_devinfos(bool show_no_user)
  96. {
  97. static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
  98. [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
  99. [DEVICE_CATEGORY_USB] = "USB",
  100. [DEVICE_CATEGORY_STORAGE] = "Storage",
  101. [DEVICE_CATEGORY_NETWORK] = "Network",
  102. [DEVICE_CATEGORY_INPUT] = "Input",
  103. [DEVICE_CATEGORY_DISPLAY] = "Display",
  104. [DEVICE_CATEGORY_SOUND] = "Sound",
  105. [DEVICE_CATEGORY_MISC] = "Misc",
  106. [DEVICE_CATEGORY_MAX] = "Uncategorized",
  107. };
  108. GSList *list, *elt;
  109. int i;
  110. bool cat_printed;
  111. list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false),
  112. devinfo_cmp);
  113. for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
  114. cat_printed = false;
  115. for (elt = list; elt; elt = elt->next) {
  116. DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
  117. TYPE_DEVICE);
  118. if ((i < DEVICE_CATEGORY_MAX
  119. ? !test_bit(i, dc->categories)
  120. : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
  121. || (!show_no_user
  122. && dc->cannot_instantiate_with_device_add_yet)) {
  123. continue;
  124. }
  125. if (!cat_printed) {
  126. error_printf("%s%s devices:\n", i ? "\n" : "",
  127. cat_name[i]);
  128. cat_printed = true;
  129. }
  130. qdev_print_devinfo(dc);
  131. }
  132. }
  133. g_slist_free(list);
  134. }
  135. static int set_property(void *opaque, const char *name, const char *value,
  136. Error **errp)
  137. {
  138. Object *obj = opaque;
  139. Error *err = NULL;
  140. if (strcmp(name, "driver") == 0)
  141. return 0;
  142. if (strcmp(name, "bus") == 0)
  143. return 0;
  144. object_property_parse(obj, value, name, &err);
  145. if (err != NULL) {
  146. error_propagate(errp, err);
  147. return -1;
  148. }
  149. return 0;
  150. }
  151. static const char *find_typename_by_alias(const char *alias)
  152. {
  153. int i;
  154. for (i = 0; qdev_alias_table[i].alias; i++) {
  155. if (qdev_alias_table[i].arch_mask &&
  156. !(qdev_alias_table[i].arch_mask & arch_type)) {
  157. continue;
  158. }
  159. if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
  160. return qdev_alias_table[i].typename;
  161. }
  162. }
  163. return NULL;
  164. }
  165. static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
  166. {
  167. ObjectClass *oc;
  168. DeviceClass *dc;
  169. oc = object_class_by_name(*driver);
  170. if (!oc) {
  171. const char *typename = find_typename_by_alias(*driver);
  172. if (typename) {
  173. *driver = typename;
  174. oc = object_class_by_name(*driver);
  175. }
  176. }
  177. if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
  178. error_setg(errp, "'%s' is not a valid device model name", *driver);
  179. return NULL;
  180. }
  181. if (object_class_is_abstract(oc)) {
  182. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
  183. "non-abstract device type");
  184. return NULL;
  185. }
  186. dc = DEVICE_CLASS(oc);
  187. if (dc->cannot_instantiate_with_device_add_yet ||
  188. (qdev_hotplug && !dc->hotpluggable)) {
  189. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
  190. "pluggable device type");
  191. return NULL;
  192. }
  193. return dc;
  194. }
  195. int qdev_device_help(QemuOpts *opts)
  196. {
  197. Error *local_err = NULL;
  198. const char *driver;
  199. DevicePropertyInfoList *prop_list;
  200. DevicePropertyInfoList *prop;
  201. driver = qemu_opt_get(opts, "driver");
  202. if (driver && is_help_option(driver)) {
  203. qdev_print_devinfos(false);
  204. return 1;
  205. }
  206. if (!driver || !qemu_opt_has_help_opt(opts)) {
  207. return 0;
  208. }
  209. if (!object_class_by_name(driver)) {
  210. const char *typename = find_typename_by_alias(driver);
  211. if (typename) {
  212. driver = typename;
  213. }
  214. }
  215. prop_list = qmp_device_list_properties(driver, &local_err);
  216. if (local_err) {
  217. goto error;
  218. }
  219. for (prop = prop_list; prop; prop = prop->next) {
  220. error_printf("%s.%s=%s", driver,
  221. prop->value->name,
  222. prop->value->type);
  223. if (prop->value->has_description) {
  224. error_printf(" (%s)\n", prop->value->description);
  225. } else {
  226. error_printf("\n");
  227. }
  228. }
  229. qapi_free_DevicePropertyInfoList(prop_list);
  230. return 1;
  231. error:
  232. error_report_err(local_err);
  233. return 1;
  234. }
  235. static Object *qdev_get_peripheral(void)
  236. {
  237. static Object *dev;
  238. if (dev == NULL) {
  239. dev = container_get(qdev_get_machine(), "/peripheral");
  240. }
  241. return dev;
  242. }
  243. static Object *qdev_get_peripheral_anon(void)
  244. {
  245. static Object *dev;
  246. if (dev == NULL) {
  247. dev = container_get(qdev_get_machine(), "/peripheral-anon");
  248. }
  249. return dev;
  250. }
  251. static void qbus_list_bus(DeviceState *dev, Error **errp)
  252. {
  253. BusState *child;
  254. const char *sep = " ";
  255. error_append_hint(errp, "child buses at \"%s\":",
  256. dev->id ? dev->id : object_get_typename(OBJECT(dev)));
  257. QLIST_FOREACH(child, &dev->child_bus, sibling) {
  258. error_append_hint(errp, "%s\"%s\"", sep, child->name);
  259. sep = ", ";
  260. }
  261. error_append_hint(errp, "\n");
  262. }
  263. static void qbus_list_dev(BusState *bus, Error **errp)
  264. {
  265. BusChild *kid;
  266. const char *sep = " ";
  267. error_append_hint(errp, "devices at \"%s\":", bus->name);
  268. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  269. DeviceState *dev = kid->child;
  270. error_append_hint(errp, "%s\"%s\"", sep,
  271. object_get_typename(OBJECT(dev)));
  272. if (dev->id) {
  273. error_append_hint(errp, "/\"%s\"", dev->id);
  274. }
  275. sep = ", ";
  276. }
  277. error_append_hint(errp, "\n");
  278. }
  279. static BusState *qbus_find_bus(DeviceState *dev, char *elem)
  280. {
  281. BusState *child;
  282. QLIST_FOREACH(child, &dev->child_bus, sibling) {
  283. if (strcmp(child->name, elem) == 0) {
  284. return child;
  285. }
  286. }
  287. return NULL;
  288. }
  289. static DeviceState *qbus_find_dev(BusState *bus, char *elem)
  290. {
  291. BusChild *kid;
  292. /*
  293. * try to match in order:
  294. * (1) instance id, if present
  295. * (2) driver name
  296. * (3) driver alias, if present
  297. */
  298. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  299. DeviceState *dev = kid->child;
  300. if (dev->id && strcmp(dev->id, elem) == 0) {
  301. return dev;
  302. }
  303. }
  304. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  305. DeviceState *dev = kid->child;
  306. if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
  307. return dev;
  308. }
  309. }
  310. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  311. DeviceState *dev = kid->child;
  312. DeviceClass *dc = DEVICE_GET_CLASS(dev);
  313. if (qdev_class_has_alias(dc) &&
  314. strcmp(qdev_class_get_alias(dc), elem) == 0) {
  315. return dev;
  316. }
  317. }
  318. return NULL;
  319. }
  320. static inline bool qbus_is_full(BusState *bus)
  321. {
  322. BusClass *bus_class = BUS_GET_CLASS(bus);
  323. return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
  324. }
  325. /*
  326. * Search the tree rooted at @bus for a bus.
  327. * If @name, search for a bus with that name. Note that bus names
  328. * need not be unique. Yes, that's screwed up.
  329. * Else search for a bus that is a subtype of @bus_typename.
  330. * If more than one exists, prefer one that can take another device.
  331. * Return the bus if found, else %NULL.
  332. */
  333. static BusState *qbus_find_recursive(BusState *bus, const char *name,
  334. const char *bus_typename)
  335. {
  336. BusChild *kid;
  337. BusState *pick, *child, *ret;
  338. bool match;
  339. assert(name || bus_typename);
  340. if (name) {
  341. match = !strcmp(bus->name, name);
  342. } else {
  343. match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
  344. }
  345. if (match && !qbus_is_full(bus)) {
  346. return bus; /* root matches and isn't full */
  347. }
  348. pick = match ? bus : NULL;
  349. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  350. DeviceState *dev = kid->child;
  351. QLIST_FOREACH(child, &dev->child_bus, sibling) {
  352. ret = qbus_find_recursive(child, name, bus_typename);
  353. if (ret && !qbus_is_full(ret)) {
  354. return ret; /* a descendant matches and isn't full */
  355. }
  356. if (ret && !pick) {
  357. pick = ret;
  358. }
  359. }
  360. }
  361. /* root or a descendant matches, but is full */
  362. return pick;
  363. }
  364. static BusState *qbus_find(const char *path, Error **errp)
  365. {
  366. DeviceState *dev;
  367. BusState *bus;
  368. char elem[128];
  369. int pos, len;
  370. /* find start element */
  371. if (path[0] == '/') {
  372. bus = sysbus_get_default();
  373. pos = 0;
  374. } else {
  375. if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
  376. assert(!path[0]);
  377. elem[0] = len = 0;
  378. }
  379. bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
  380. if (!bus) {
  381. error_setg(errp, "Bus '%s' not found", elem);
  382. return NULL;
  383. }
  384. pos = len;
  385. }
  386. for (;;) {
  387. assert(path[pos] == '/' || !path[pos]);
  388. while (path[pos] == '/') {
  389. pos++;
  390. }
  391. if (path[pos] == '\0') {
  392. break;
  393. }
  394. /* find device */
  395. if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
  396. g_assert_not_reached();
  397. elem[0] = len = 0;
  398. }
  399. pos += len;
  400. dev = qbus_find_dev(bus, elem);
  401. if (!dev) {
  402. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  403. "Device '%s' not found", elem);
  404. qbus_list_dev(bus, errp);
  405. return NULL;
  406. }
  407. assert(path[pos] == '/' || !path[pos]);
  408. while (path[pos] == '/') {
  409. pos++;
  410. }
  411. if (path[pos] == '\0') {
  412. /* last specified element is a device. If it has exactly
  413. * one child bus accept it nevertheless */
  414. if (dev->num_child_bus == 1) {
  415. bus = QLIST_FIRST(&dev->child_bus);
  416. break;
  417. }
  418. if (dev->num_child_bus) {
  419. error_setg(errp, "Device '%s' has multiple child buses",
  420. elem);
  421. qbus_list_bus(dev, errp);
  422. } else {
  423. error_setg(errp, "Device '%s' has no child bus", elem);
  424. }
  425. return NULL;
  426. }
  427. /* find bus */
  428. if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
  429. g_assert_not_reached();
  430. elem[0] = len = 0;
  431. }
  432. pos += len;
  433. bus = qbus_find_bus(dev, elem);
  434. if (!bus) {
  435. error_setg(errp, "Bus '%s' not found", elem);
  436. qbus_list_bus(dev, errp);
  437. return NULL;
  438. }
  439. }
  440. if (qbus_is_full(bus)) {
  441. error_setg(errp, "Bus '%s' is full", path);
  442. return NULL;
  443. }
  444. return bus;
  445. }
  446. DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
  447. {
  448. DeviceClass *dc;
  449. const char *driver, *path, *id;
  450. DeviceState *dev;
  451. BusState *bus = NULL;
  452. Error *err = NULL;
  453. driver = qemu_opt_get(opts, "driver");
  454. if (!driver) {
  455. error_setg(errp, QERR_MISSING_PARAMETER, "driver");
  456. return NULL;
  457. }
  458. /* find driver */
  459. dc = qdev_get_device_class(&driver, errp);
  460. if (!dc) {
  461. return NULL;
  462. }
  463. /* find bus */
  464. path = qemu_opt_get(opts, "bus");
  465. if (path != NULL) {
  466. bus = qbus_find(path, errp);
  467. if (!bus) {
  468. return NULL;
  469. }
  470. if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
  471. error_setg(errp, "Device '%s' can't go on %s bus",
  472. driver, object_get_typename(OBJECT(bus)));
  473. return NULL;
  474. }
  475. } else if (dc->bus_type != NULL) {
  476. bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
  477. if (!bus || qbus_is_full(bus)) {
  478. error_setg(errp, "No '%s' bus found for device '%s'",
  479. dc->bus_type, driver);
  480. return NULL;
  481. }
  482. }
  483. if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
  484. error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
  485. return NULL;
  486. }
  487. /* create device */
  488. dev = DEVICE(object_new(driver));
  489. if (bus) {
  490. qdev_set_parent_bus(dev, bus);
  491. }
  492. id = qemu_opts_id(opts);
  493. if (id) {
  494. dev->id = id;
  495. }
  496. if (dev->id) {
  497. object_property_add_child(qdev_get_peripheral(), dev->id,
  498. OBJECT(dev), NULL);
  499. } else {
  500. static int anon_count;
  501. gchar *name = g_strdup_printf("device[%d]", anon_count++);
  502. object_property_add_child(qdev_get_peripheral_anon(), name,
  503. OBJECT(dev), NULL);
  504. g_free(name);
  505. }
  506. /* set properties */
  507. if (qemu_opt_foreach(opts, set_property, dev, &err)) {
  508. error_propagate(errp, err);
  509. object_unparent(OBJECT(dev));
  510. object_unref(OBJECT(dev));
  511. return NULL;
  512. }
  513. dev->opts = opts;
  514. object_property_set_bool(OBJECT(dev), true, "realized", &err);
  515. if (err != NULL) {
  516. error_propagate(errp, err);
  517. dev->opts = NULL;
  518. object_unparent(OBJECT(dev));
  519. object_unref(OBJECT(dev));
  520. return NULL;
  521. }
  522. return dev;
  523. }
  524. #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
  525. static void qbus_print(Monitor *mon, BusState *bus, int indent);
  526. static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
  527. int indent)
  528. {
  529. if (!props)
  530. return;
  531. for (; props->name; props++) {
  532. Error *err = NULL;
  533. char *value;
  534. char *legacy_name = g_strdup_printf("legacy-%s", props->name);
  535. if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
  536. value = object_property_get_str(OBJECT(dev), legacy_name, &err);
  537. } else {
  538. value = object_property_print(OBJECT(dev), props->name, true, &err);
  539. }
  540. g_free(legacy_name);
  541. if (err) {
  542. error_free(err);
  543. continue;
  544. }
  545. qdev_printf("%s = %s\n", props->name,
  546. value && *value ? value : "<null>");
  547. g_free(value);
  548. }
  549. }
  550. static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
  551. {
  552. BusClass *bc = BUS_GET_CLASS(bus);
  553. if (bc->print_dev) {
  554. bc->print_dev(mon, dev, indent);
  555. }
  556. }
  557. static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
  558. {
  559. ObjectClass *class;
  560. BusState *child;
  561. NamedGPIOList *ngl;
  562. qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
  563. dev->id ? dev->id : "");
  564. indent += 2;
  565. QLIST_FOREACH(ngl, &dev->gpios, node) {
  566. if (ngl->num_in) {
  567. qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
  568. ngl->num_in);
  569. }
  570. if (ngl->num_out) {
  571. qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
  572. ngl->num_out);
  573. }
  574. }
  575. class = object_get_class(OBJECT(dev));
  576. do {
  577. qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
  578. class = object_class_get_parent(class);
  579. } while (class != object_class_by_name(TYPE_DEVICE));
  580. bus_print_dev(dev->parent_bus, mon, dev, indent);
  581. QLIST_FOREACH(child, &dev->child_bus, sibling) {
  582. qbus_print(mon, child, indent);
  583. }
  584. }
  585. static void qbus_print(Monitor *mon, BusState *bus, int indent)
  586. {
  587. BusChild *kid;
  588. qdev_printf("bus: %s\n", bus->name);
  589. indent += 2;
  590. qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
  591. QTAILQ_FOREACH(kid, &bus->children, sibling) {
  592. DeviceState *dev = kid->child;
  593. qdev_print(mon, dev, indent);
  594. }
  595. }
  596. #undef qdev_printf
  597. void hmp_info_qtree(Monitor *mon, const QDict *qdict)
  598. {
  599. if (sysbus_get_default())
  600. qbus_print(mon, sysbus_get_default(), 0);
  601. }
  602. void hmp_info_qdm(Monitor *mon, const QDict *qdict)
  603. {
  604. qdev_print_devinfos(true);
  605. }
  606. typedef struct QOMCompositionState {
  607. Monitor *mon;
  608. int indent;
  609. } QOMCompositionState;
  610. static void print_qom_composition(Monitor *mon, Object *obj, int indent);
  611. static int print_qom_composition_child(Object *obj, void *opaque)
  612. {
  613. QOMCompositionState *s = opaque;
  614. print_qom_composition(s->mon, obj, s->indent);
  615. return 0;
  616. }
  617. static void print_qom_composition(Monitor *mon, Object *obj, int indent)
  618. {
  619. QOMCompositionState s = {
  620. .mon = mon,
  621. .indent = indent + 2,
  622. };
  623. char *name;
  624. if (obj == object_get_root()) {
  625. name = g_strdup("");
  626. } else {
  627. name = object_get_canonical_path_component(obj);
  628. }
  629. monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
  630. object_get_typename(obj));
  631. g_free(name);
  632. object_child_foreach(obj, print_qom_composition_child, &s);
  633. }
  634. void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
  635. {
  636. const char *path = qdict_get_try_str(dict, "path");
  637. Object *obj;
  638. bool ambiguous = false;
  639. if (path) {
  640. obj = object_resolve_path(path, &ambiguous);
  641. if (!obj) {
  642. monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
  643. return;
  644. }
  645. if (ambiguous) {
  646. monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
  647. return;
  648. }
  649. } else {
  650. obj = qdev_get_machine();
  651. }
  652. print_qom_composition(mon, obj, 0);
  653. }
  654. void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
  655. {
  656. Error *local_err = NULL;
  657. QemuOpts *opts;
  658. DeviceState *dev;
  659. opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
  660. if (local_err) {
  661. error_propagate(errp, local_err);
  662. return;
  663. }
  664. if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
  665. qemu_opts_del(opts);
  666. return;
  667. }
  668. dev = qdev_device_add(opts, &local_err);
  669. if (!dev) {
  670. error_propagate(errp, local_err);
  671. qemu_opts_del(opts);
  672. return;
  673. }
  674. object_unref(OBJECT(dev));
  675. }
  676. void qmp_device_del(const char *id, Error **errp)
  677. {
  678. Object *obj;
  679. if (id[0] == '/') {
  680. obj = object_resolve_path(id, NULL);
  681. } else {
  682. char *root_path = object_get_canonical_path(qdev_get_peripheral());
  683. char *path = g_strdup_printf("%s/%s", root_path, id);
  684. g_free(root_path);
  685. obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
  686. g_free(path);
  687. }
  688. if (!obj) {
  689. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  690. "Device '%s' not found", id);
  691. return;
  692. }
  693. if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
  694. error_setg(errp, "%s is not a hotpluggable device", id);
  695. return;
  696. }
  697. qdev_unplug(DEVICE(obj), errp);
  698. }
  699. void qdev_machine_init(void)
  700. {
  701. qdev_get_peripheral_anon();
  702. qdev_get_peripheral();
  703. }
  704. QemuOptsList qemu_device_opts = {
  705. .name = "device",
  706. .implied_opt_name = "driver",
  707. .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
  708. .desc = {
  709. /*
  710. * no elements => accept any
  711. * sanity checking will happen later
  712. * when setting device properties
  713. */
  714. { /* end of list */ }
  715. },
  716. };
  717. QemuOptsList qemu_global_opts = {
  718. .name = "global",
  719. .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
  720. .desc = {
  721. {
  722. .name = "driver",
  723. .type = QEMU_OPT_STRING,
  724. },{
  725. .name = "property",
  726. .type = QEMU_OPT_STRING,
  727. },{
  728. .name = "value",
  729. .type = QEMU_OPT_STRING,
  730. },
  731. { /* end of list */ }
  732. },
  733. };
  734. int qemu_global_option(const char *str)
  735. {
  736. char driver[64], property[64];
  737. QemuOpts *opts;
  738. int rc, offset;
  739. rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
  740. if (rc == 2 && str[offset] == '=') {
  741. opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
  742. qemu_opt_set(opts, "driver", driver, &error_abort);
  743. qemu_opt_set(opts, "property", property, &error_abort);
  744. qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
  745. return 0;
  746. }
  747. opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
  748. if (!opts) {
  749. return -1;
  750. }
  751. return 0;
  752. }