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 26KB

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