You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qdev-monitor.c 27KB

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