machine.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*
  2. * QEMU Machine
  3. *
  4. * Copyright (C) 2014 Red Hat Inc
  5. *
  6. * Authors:
  7. * Marcel Apfelbaum <marcel.a@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qemu/option.h"
  14. #include "qapi/qmp/qerror.h"
  15. #include "sysemu/replay.h"
  16. #include "qemu/units.h"
  17. #include "hw/boards.h"
  18. #include "qapi/error.h"
  19. #include "qapi/qapi-visit-common.h"
  20. #include "qapi/visitor.h"
  21. #include "hw/sysbus.h"
  22. #include "sysemu/sysemu.h"
  23. #include "sysemu/numa.h"
  24. #include "qemu/error-report.h"
  25. #include "sysemu/qtest.h"
  26. #include "hw/pci/pci.h"
  27. #include "hw/mem/nvdimm.h"
  28. #include "migration/vmstate.h"
  29. GlobalProperty hw_compat_5_1[] = {
  30. { "vhost-scsi", "num_queues", "1"},
  31. { "vhost-user-blk", "num-queues", "1"},
  32. { "vhost-user-scsi", "num_queues", "1"},
  33. { "virtio-blk-device", "num-queues", "1"},
  34. { "virtio-scsi-device", "num_queues", "1"},
  35. };
  36. const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
  37. GlobalProperty hw_compat_5_0[] = {
  38. { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
  39. { "virtio-balloon-device", "page-poison", "false" },
  40. { "vmport", "x-read-set-eax", "off" },
  41. { "vmport", "x-signal-unsupported-cmd", "off" },
  42. { "vmport", "x-report-vmx-type", "off" },
  43. { "vmport", "x-cmds-v2", "off" },
  44. { "virtio-device", "x-disable-legacy-check", "true" },
  45. };
  46. const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
  47. GlobalProperty hw_compat_4_2[] = {
  48. { "virtio-blk-device", "queue-size", "128"},
  49. { "virtio-scsi-device", "virtqueue_size", "128"},
  50. { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
  51. { "virtio-blk-device", "seg-max-adjust", "off"},
  52. { "virtio-scsi-device", "seg_max_adjust", "off"},
  53. { "vhost-blk-device", "seg_max_adjust", "off"},
  54. { "usb-host", "suppress-remote-wake", "off" },
  55. { "usb-redir", "suppress-remote-wake", "off" },
  56. { "qxl", "revision", "4" },
  57. { "qxl-vga", "revision", "4" },
  58. { "fw_cfg", "acpi-mr-restore", "false" },
  59. };
  60. const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
  61. GlobalProperty hw_compat_4_1[] = {
  62. { "virtio-pci", "x-pcie-flr-init", "off" },
  63. { "virtio-device", "use-disabled-flag", "false" },
  64. };
  65. const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
  66. GlobalProperty hw_compat_4_0[] = {
  67. { "VGA", "edid", "false" },
  68. { "secondary-vga", "edid", "false" },
  69. { "bochs-display", "edid", "false" },
  70. { "virtio-vga", "edid", "false" },
  71. { "virtio-gpu-device", "edid", "false" },
  72. { "virtio-device", "use-started", "false" },
  73. { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
  74. { "pl031", "migrate-tick-offset", "false" },
  75. };
  76. const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
  77. GlobalProperty hw_compat_3_1[] = {
  78. { "pcie-root-port", "x-speed", "2_5" },
  79. { "pcie-root-port", "x-width", "1" },
  80. { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
  81. { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
  82. { "tpm-crb", "ppi", "false" },
  83. { "tpm-tis", "ppi", "false" },
  84. { "usb-kbd", "serial", "42" },
  85. { "usb-mouse", "serial", "42" },
  86. { "usb-tablet", "serial", "42" },
  87. { "virtio-blk-device", "discard", "false" },
  88. { "virtio-blk-device", "write-zeroes", "false" },
  89. { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
  90. { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
  91. };
  92. const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
  93. GlobalProperty hw_compat_3_0[] = {};
  94. const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
  95. GlobalProperty hw_compat_2_12[] = {
  96. { "migration", "decompress-error-check", "off" },
  97. { "hda-audio", "use-timer", "false" },
  98. { "cirrus-vga", "global-vmstate", "true" },
  99. { "VGA", "global-vmstate", "true" },
  100. { "vmware-svga", "global-vmstate", "true" },
  101. { "qxl-vga", "global-vmstate", "true" },
  102. };
  103. const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
  104. GlobalProperty hw_compat_2_11[] = {
  105. { "hpet", "hpet-offset-saved", "false" },
  106. { "virtio-blk-pci", "vectors", "2" },
  107. { "vhost-user-blk-pci", "vectors", "2" },
  108. { "e1000", "migrate_tso_props", "off" },
  109. };
  110. const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
  111. GlobalProperty hw_compat_2_10[] = {
  112. { "virtio-mouse-device", "wheel-axis", "false" },
  113. { "virtio-tablet-device", "wheel-axis", "false" },
  114. };
  115. const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
  116. GlobalProperty hw_compat_2_9[] = {
  117. { "pci-bridge", "shpc", "off" },
  118. { "intel-iommu", "pt", "off" },
  119. { "virtio-net-device", "x-mtu-bypass-backend", "off" },
  120. { "pcie-root-port", "x-migrate-msix", "false" },
  121. };
  122. const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
  123. GlobalProperty hw_compat_2_8[] = {
  124. { "fw_cfg_mem", "x-file-slots", "0x10" },
  125. { "fw_cfg_io", "x-file-slots", "0x10" },
  126. { "pflash_cfi01", "old-multiple-chip-handling", "on" },
  127. { "pci-bridge", "shpc", "on" },
  128. { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
  129. { "virtio-pci", "x-pcie-deverr-init", "off" },
  130. { "virtio-pci", "x-pcie-lnkctl-init", "off" },
  131. { "virtio-pci", "x-pcie-pm-init", "off" },
  132. { "cirrus-vga", "vgamem_mb", "8" },
  133. { "isa-cirrus-vga", "vgamem_mb", "8" },
  134. };
  135. const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
  136. GlobalProperty hw_compat_2_7[] = {
  137. { "virtio-pci", "page-per-vq", "on" },
  138. { "virtio-serial-device", "emergency-write", "off" },
  139. { "ioapic", "version", "0x11" },
  140. { "intel-iommu", "x-buggy-eim", "true" },
  141. { "virtio-pci", "x-ignore-backend-features", "on" },
  142. };
  143. const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
  144. GlobalProperty hw_compat_2_6[] = {
  145. { "virtio-mmio", "format_transport_address", "off" },
  146. /* Optional because not all virtio-pci devices support legacy mode */
  147. { "virtio-pci", "disable-modern", "on", .optional = true },
  148. { "virtio-pci", "disable-legacy", "off", .optional = true },
  149. };
  150. const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
  151. GlobalProperty hw_compat_2_5[] = {
  152. { "isa-fdc", "fallback", "144" },
  153. { "pvscsi", "x-old-pci-configuration", "on" },
  154. { "pvscsi", "x-disable-pcie", "on" },
  155. { "vmxnet3", "x-old-msi-offsets", "on" },
  156. { "vmxnet3", "x-disable-pcie", "on" },
  157. };
  158. const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
  159. GlobalProperty hw_compat_2_4[] = {
  160. /* Optional because the 'scsi' property is Linux-only */
  161. { "virtio-blk-device", "scsi", "true", .optional = true },
  162. { "e1000", "extra_mac_registers", "off" },
  163. { "virtio-pci", "x-disable-pcie", "on" },
  164. { "virtio-pci", "migrate-extra", "off" },
  165. { "fw_cfg_mem", "dma_enabled", "off" },
  166. { "fw_cfg_io", "dma_enabled", "off" }
  167. };
  168. const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
  169. GlobalProperty hw_compat_2_3[] = {
  170. { "virtio-blk-pci", "any_layout", "off" },
  171. { "virtio-balloon-pci", "any_layout", "off" },
  172. { "virtio-serial-pci", "any_layout", "off" },
  173. { "virtio-9p-pci", "any_layout", "off" },
  174. { "virtio-rng-pci", "any_layout", "off" },
  175. { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
  176. { "migration", "send-configuration", "off" },
  177. { "migration", "send-section-footer", "off" },
  178. { "migration", "store-global-state", "off" },
  179. };
  180. const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
  181. GlobalProperty hw_compat_2_2[] = {};
  182. const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
  183. GlobalProperty hw_compat_2_1[] = {
  184. { "intel-hda", "old_msi_addr", "on" },
  185. { "VGA", "qemu-extended-regs", "off" },
  186. { "secondary-vga", "qemu-extended-regs", "off" },
  187. { "virtio-scsi-pci", "any_layout", "off" },
  188. { "usb-mouse", "usb_version", "1" },
  189. { "usb-kbd", "usb_version", "1" },
  190. { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
  191. };
  192. const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
  193. static char *machine_get_kernel(Object *obj, Error **errp)
  194. {
  195. MachineState *ms = MACHINE(obj);
  196. return g_strdup(ms->kernel_filename);
  197. }
  198. static void machine_set_kernel(Object *obj, const char *value, Error **errp)
  199. {
  200. MachineState *ms = MACHINE(obj);
  201. g_free(ms->kernel_filename);
  202. ms->kernel_filename = g_strdup(value);
  203. }
  204. static char *machine_get_initrd(Object *obj, Error **errp)
  205. {
  206. MachineState *ms = MACHINE(obj);
  207. return g_strdup(ms->initrd_filename);
  208. }
  209. static void machine_set_initrd(Object *obj, const char *value, Error **errp)
  210. {
  211. MachineState *ms = MACHINE(obj);
  212. g_free(ms->initrd_filename);
  213. ms->initrd_filename = g_strdup(value);
  214. }
  215. static char *machine_get_append(Object *obj, Error **errp)
  216. {
  217. MachineState *ms = MACHINE(obj);
  218. return g_strdup(ms->kernel_cmdline);
  219. }
  220. static void machine_set_append(Object *obj, const char *value, Error **errp)
  221. {
  222. MachineState *ms = MACHINE(obj);
  223. g_free(ms->kernel_cmdline);
  224. ms->kernel_cmdline = g_strdup(value);
  225. }
  226. static char *machine_get_dtb(Object *obj, Error **errp)
  227. {
  228. MachineState *ms = MACHINE(obj);
  229. return g_strdup(ms->dtb);
  230. }
  231. static void machine_set_dtb(Object *obj, const char *value, Error **errp)
  232. {
  233. MachineState *ms = MACHINE(obj);
  234. g_free(ms->dtb);
  235. ms->dtb = g_strdup(value);
  236. }
  237. static char *machine_get_dumpdtb(Object *obj, Error **errp)
  238. {
  239. MachineState *ms = MACHINE(obj);
  240. return g_strdup(ms->dumpdtb);
  241. }
  242. static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
  243. {
  244. MachineState *ms = MACHINE(obj);
  245. g_free(ms->dumpdtb);
  246. ms->dumpdtb = g_strdup(value);
  247. }
  248. static void machine_get_phandle_start(Object *obj, Visitor *v,
  249. const char *name, void *opaque,
  250. Error **errp)
  251. {
  252. MachineState *ms = MACHINE(obj);
  253. int64_t value = ms->phandle_start;
  254. visit_type_int(v, name, &value, errp);
  255. }
  256. static void machine_set_phandle_start(Object *obj, Visitor *v,
  257. const char *name, void *opaque,
  258. Error **errp)
  259. {
  260. MachineState *ms = MACHINE(obj);
  261. int64_t value;
  262. if (!visit_type_int(v, name, &value, errp)) {
  263. return;
  264. }
  265. ms->phandle_start = value;
  266. }
  267. static char *machine_get_dt_compatible(Object *obj, Error **errp)
  268. {
  269. MachineState *ms = MACHINE(obj);
  270. return g_strdup(ms->dt_compatible);
  271. }
  272. static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
  273. {
  274. MachineState *ms = MACHINE(obj);
  275. g_free(ms->dt_compatible);
  276. ms->dt_compatible = g_strdup(value);
  277. }
  278. static bool machine_get_dump_guest_core(Object *obj, Error **errp)
  279. {
  280. MachineState *ms = MACHINE(obj);
  281. return ms->dump_guest_core;
  282. }
  283. static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
  284. {
  285. MachineState *ms = MACHINE(obj);
  286. ms->dump_guest_core = value;
  287. }
  288. static bool machine_get_mem_merge(Object *obj, Error **errp)
  289. {
  290. MachineState *ms = MACHINE(obj);
  291. return ms->mem_merge;
  292. }
  293. static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
  294. {
  295. MachineState *ms = MACHINE(obj);
  296. ms->mem_merge = value;
  297. }
  298. static bool machine_get_usb(Object *obj, Error **errp)
  299. {
  300. MachineState *ms = MACHINE(obj);
  301. return ms->usb;
  302. }
  303. static void machine_set_usb(Object *obj, bool value, Error **errp)
  304. {
  305. MachineState *ms = MACHINE(obj);
  306. ms->usb = value;
  307. ms->usb_disabled = !value;
  308. }
  309. static bool machine_get_graphics(Object *obj, Error **errp)
  310. {
  311. MachineState *ms = MACHINE(obj);
  312. return ms->enable_graphics;
  313. }
  314. static void machine_set_graphics(Object *obj, bool value, Error **errp)
  315. {
  316. MachineState *ms = MACHINE(obj);
  317. ms->enable_graphics = value;
  318. }
  319. static char *machine_get_firmware(Object *obj, Error **errp)
  320. {
  321. MachineState *ms = MACHINE(obj);
  322. return g_strdup(ms->firmware);
  323. }
  324. static void machine_set_firmware(Object *obj, const char *value, Error **errp)
  325. {
  326. MachineState *ms = MACHINE(obj);
  327. g_free(ms->firmware);
  328. ms->firmware = g_strdup(value);
  329. }
  330. static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
  331. {
  332. MachineState *ms = MACHINE(obj);
  333. ms->suppress_vmdesc = value;
  334. }
  335. static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
  336. {
  337. MachineState *ms = MACHINE(obj);
  338. return ms->suppress_vmdesc;
  339. }
  340. static void machine_set_enforce_config_section(Object *obj, bool value,
  341. Error **errp)
  342. {
  343. MachineState *ms = MACHINE(obj);
  344. warn_report("enforce-config-section is deprecated, please use "
  345. "-global migration.send-configuration=on|off instead");
  346. ms->enforce_config_section = value;
  347. }
  348. static bool machine_get_enforce_config_section(Object *obj, Error **errp)
  349. {
  350. MachineState *ms = MACHINE(obj);
  351. return ms->enforce_config_section;
  352. }
  353. static char *machine_get_memory_encryption(Object *obj, Error **errp)
  354. {
  355. MachineState *ms = MACHINE(obj);
  356. return g_strdup(ms->memory_encryption);
  357. }
  358. static void machine_set_memory_encryption(Object *obj, const char *value,
  359. Error **errp)
  360. {
  361. MachineState *ms = MACHINE(obj);
  362. g_free(ms->memory_encryption);
  363. ms->memory_encryption = g_strdup(value);
  364. /*
  365. * With memory encryption, the host can't see the real contents of RAM,
  366. * so there's no point in it trying to merge areas.
  367. */
  368. if (value) {
  369. machine_set_mem_merge(obj, false, errp);
  370. }
  371. }
  372. static bool machine_get_nvdimm(Object *obj, Error **errp)
  373. {
  374. MachineState *ms = MACHINE(obj);
  375. return ms->nvdimms_state->is_enabled;
  376. }
  377. static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
  378. {
  379. MachineState *ms = MACHINE(obj);
  380. ms->nvdimms_state->is_enabled = value;
  381. }
  382. static bool machine_get_hmat(Object *obj, Error **errp)
  383. {
  384. MachineState *ms = MACHINE(obj);
  385. return ms->numa_state->hmat_enabled;
  386. }
  387. static void machine_set_hmat(Object *obj, bool value, Error **errp)
  388. {
  389. MachineState *ms = MACHINE(obj);
  390. ms->numa_state->hmat_enabled = value;
  391. }
  392. static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
  393. {
  394. MachineState *ms = MACHINE(obj);
  395. return g_strdup(ms->nvdimms_state->persistence_string);
  396. }
  397. static void machine_set_nvdimm_persistence(Object *obj, const char *value,
  398. Error **errp)
  399. {
  400. MachineState *ms = MACHINE(obj);
  401. NVDIMMState *nvdimms_state = ms->nvdimms_state;
  402. if (strcmp(value, "cpu") == 0) {
  403. nvdimms_state->persistence = 3;
  404. } else if (strcmp(value, "mem-ctrl") == 0) {
  405. nvdimms_state->persistence = 2;
  406. } else {
  407. error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
  408. value);
  409. return;
  410. }
  411. g_free(nvdimms_state->persistence_string);
  412. nvdimms_state->persistence_string = g_strdup(value);
  413. }
  414. void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
  415. {
  416. strList *item = g_new0(strList, 1);
  417. item->value = g_strdup(type);
  418. item->next = mc->allowed_dynamic_sysbus_devices;
  419. mc->allowed_dynamic_sysbus_devices = item;
  420. }
  421. static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
  422. {
  423. MachineState *machine = opaque;
  424. MachineClass *mc = MACHINE_GET_CLASS(machine);
  425. bool allowed = false;
  426. strList *wl;
  427. for (wl = mc->allowed_dynamic_sysbus_devices;
  428. !allowed && wl;
  429. wl = wl->next) {
  430. allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
  431. }
  432. if (!allowed) {
  433. error_report("Option '-device %s' cannot be handled by this machine",
  434. object_class_get_name(object_get_class(OBJECT(sbdev))));
  435. exit(1);
  436. }
  437. }
  438. static char *machine_get_memdev(Object *obj, Error **errp)
  439. {
  440. MachineState *ms = MACHINE(obj);
  441. return g_strdup(ms->ram_memdev_id);
  442. }
  443. static void machine_set_memdev(Object *obj, const char *value, Error **errp)
  444. {
  445. MachineState *ms = MACHINE(obj);
  446. g_free(ms->ram_memdev_id);
  447. ms->ram_memdev_id = g_strdup(value);
  448. }
  449. static void machine_init_notify(Notifier *notifier, void *data)
  450. {
  451. MachineState *machine = MACHINE(qdev_get_machine());
  452. /*
  453. * Loop through all dynamically created sysbus devices and check if they are
  454. * all allowed. If a device is not allowed, error out.
  455. */
  456. foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
  457. }
  458. HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
  459. {
  460. int i;
  461. HotpluggableCPUList *head = NULL;
  462. MachineClass *mc = MACHINE_GET_CLASS(machine);
  463. /* force board to initialize possible_cpus if it hasn't been done yet */
  464. mc->possible_cpu_arch_ids(machine);
  465. for (i = 0; i < machine->possible_cpus->len; i++) {
  466. Object *cpu;
  467. HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
  468. HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
  469. cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
  470. cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
  471. cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
  472. sizeof(*cpu_item->props));
  473. cpu = machine->possible_cpus->cpus[i].cpu;
  474. if (cpu) {
  475. cpu_item->has_qom_path = true;
  476. cpu_item->qom_path = object_get_canonical_path(cpu);
  477. }
  478. list_item->value = cpu_item;
  479. list_item->next = head;
  480. head = list_item;
  481. }
  482. return head;
  483. }
  484. /**
  485. * machine_set_cpu_numa_node:
  486. * @machine: machine object to modify
  487. * @props: specifies which cpu objects to assign to
  488. * numa node specified by @props.node_id
  489. * @errp: if an error occurs, a pointer to an area to store the error
  490. *
  491. * Associate NUMA node specified by @props.node_id with cpu slots that
  492. * match socket/core/thread-ids specified by @props. It's recommended to use
  493. * query-hotpluggable-cpus.props values to specify affected cpu slots,
  494. * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
  495. *
  496. * However for CLI convenience it's possible to pass in subset of properties,
  497. * which would affect all cpu slots that match it.
  498. * Ex for pc machine:
  499. * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
  500. * -numa cpu,node-id=0,socket_id=0 \
  501. * -numa cpu,node-id=1,socket_id=1
  502. * will assign all child cores of socket 0 to node 0 and
  503. * of socket 1 to node 1.
  504. *
  505. * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
  506. * return error.
  507. * Empty subset is disallowed and function will return with error in this case.
  508. */
  509. void machine_set_cpu_numa_node(MachineState *machine,
  510. const CpuInstanceProperties *props, Error **errp)
  511. {
  512. MachineClass *mc = MACHINE_GET_CLASS(machine);
  513. NodeInfo *numa_info = machine->numa_state->nodes;
  514. bool match = false;
  515. int i;
  516. if (!mc->possible_cpu_arch_ids) {
  517. error_setg(errp, "mapping of CPUs to NUMA node is not supported");
  518. return;
  519. }
  520. /* disabling node mapping is not supported, forbid it */
  521. assert(props->has_node_id);
  522. /* force board to initialize possible_cpus if it hasn't been done yet */
  523. mc->possible_cpu_arch_ids(machine);
  524. for (i = 0; i < machine->possible_cpus->len; i++) {
  525. CPUArchId *slot = &machine->possible_cpus->cpus[i];
  526. /* reject unsupported by board properties */
  527. if (props->has_thread_id && !slot->props.has_thread_id) {
  528. error_setg(errp, "thread-id is not supported");
  529. return;
  530. }
  531. if (props->has_core_id && !slot->props.has_core_id) {
  532. error_setg(errp, "core-id is not supported");
  533. return;
  534. }
  535. if (props->has_socket_id && !slot->props.has_socket_id) {
  536. error_setg(errp, "socket-id is not supported");
  537. return;
  538. }
  539. if (props->has_die_id && !slot->props.has_die_id) {
  540. error_setg(errp, "die-id is not supported");
  541. return;
  542. }
  543. /* skip slots with explicit mismatch */
  544. if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
  545. continue;
  546. }
  547. if (props->has_core_id && props->core_id != slot->props.core_id) {
  548. continue;
  549. }
  550. if (props->has_die_id && props->die_id != slot->props.die_id) {
  551. continue;
  552. }
  553. if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
  554. continue;
  555. }
  556. /* reject assignment if slot is already assigned, for compatibility
  557. * of legacy cpu_index mapping with SPAPR core based mapping do not
  558. * error out if cpu thread and matched core have the same node-id */
  559. if (slot->props.has_node_id &&
  560. slot->props.node_id != props->node_id) {
  561. error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
  562. slot->props.node_id);
  563. return;
  564. }
  565. /* assign slot to node as it's matched '-numa cpu' key */
  566. match = true;
  567. slot->props.node_id = props->node_id;
  568. slot->props.has_node_id = props->has_node_id;
  569. if (machine->numa_state->hmat_enabled) {
  570. if ((numa_info[props->node_id].initiator < MAX_NODES) &&
  571. (props->node_id != numa_info[props->node_id].initiator)) {
  572. error_setg(errp, "The initiator of CPU NUMA node %" PRId64
  573. " should be itself", props->node_id);
  574. return;
  575. }
  576. numa_info[props->node_id].has_cpu = true;
  577. numa_info[props->node_id].initiator = props->node_id;
  578. }
  579. }
  580. if (!match) {
  581. error_setg(errp, "no match found");
  582. }
  583. }
  584. static void smp_parse(MachineState *ms, QemuOpts *opts)
  585. {
  586. if (opts) {
  587. unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
  588. unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
  589. unsigned cores = qemu_opt_get_number(opts, "cores", 0);
  590. unsigned threads = qemu_opt_get_number(opts, "threads", 0);
  591. /* compute missing values, prefer sockets over cores over threads */
  592. if (cpus == 0 || sockets == 0) {
  593. cores = cores > 0 ? cores : 1;
  594. threads = threads > 0 ? threads : 1;
  595. if (cpus == 0) {
  596. sockets = sockets > 0 ? sockets : 1;
  597. cpus = cores * threads * sockets;
  598. } else {
  599. ms->smp.max_cpus =
  600. qemu_opt_get_number(opts, "maxcpus", cpus);
  601. sockets = ms->smp.max_cpus / (cores * threads);
  602. }
  603. } else if (cores == 0) {
  604. threads = threads > 0 ? threads : 1;
  605. cores = cpus / (sockets * threads);
  606. cores = cores > 0 ? cores : 1;
  607. } else if (threads == 0) {
  608. threads = cpus / (cores * sockets);
  609. threads = threads > 0 ? threads : 1;
  610. } else if (sockets * cores * threads < cpus) {
  611. error_report("cpu topology: "
  612. "sockets (%u) * cores (%u) * threads (%u) < "
  613. "smp_cpus (%u)",
  614. sockets, cores, threads, cpus);
  615. exit(1);
  616. }
  617. ms->smp.max_cpus =
  618. qemu_opt_get_number(opts, "maxcpus", cpus);
  619. if (ms->smp.max_cpus < cpus) {
  620. error_report("maxcpus must be equal to or greater than smp");
  621. exit(1);
  622. }
  623. if (sockets * cores * threads != ms->smp.max_cpus) {
  624. error_report("Invalid CPU topology: "
  625. "sockets (%u) * cores (%u) * threads (%u) "
  626. "!= maxcpus (%u)",
  627. sockets, cores, threads,
  628. ms->smp.max_cpus);
  629. exit(1);
  630. }
  631. ms->smp.cpus = cpus;
  632. ms->smp.cores = cores;
  633. ms->smp.threads = threads;
  634. ms->smp.sockets = sockets;
  635. }
  636. if (ms->smp.cpus > 1) {
  637. Error *blocker = NULL;
  638. error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
  639. replay_add_blocker(blocker);
  640. }
  641. }
  642. static void machine_class_init(ObjectClass *oc, void *data)
  643. {
  644. MachineClass *mc = MACHINE_CLASS(oc);
  645. /* Default 128 MB as guest ram size */
  646. mc->default_ram_size = 128 * MiB;
  647. mc->rom_file_has_mr = true;
  648. mc->smp_parse = smp_parse;
  649. /* numa node memory size aligned on 8MB by default.
  650. * On Linux, each node's border has to be 8MB aligned
  651. */
  652. mc->numa_mem_align_shift = 23;
  653. object_class_property_add_str(oc, "kernel",
  654. machine_get_kernel, machine_set_kernel);
  655. object_class_property_set_description(oc, "kernel",
  656. "Linux kernel image file");
  657. object_class_property_add_str(oc, "initrd",
  658. machine_get_initrd, machine_set_initrd);
  659. object_class_property_set_description(oc, "initrd",
  660. "Linux initial ramdisk file");
  661. object_class_property_add_str(oc, "append",
  662. machine_get_append, machine_set_append);
  663. object_class_property_set_description(oc, "append",
  664. "Linux kernel command line");
  665. object_class_property_add_str(oc, "dtb",
  666. machine_get_dtb, machine_set_dtb);
  667. object_class_property_set_description(oc, "dtb",
  668. "Linux kernel device tree file");
  669. object_class_property_add_str(oc, "dumpdtb",
  670. machine_get_dumpdtb, machine_set_dumpdtb);
  671. object_class_property_set_description(oc, "dumpdtb",
  672. "Dump current dtb to a file and quit");
  673. object_class_property_add(oc, "phandle-start", "int",
  674. machine_get_phandle_start, machine_set_phandle_start,
  675. NULL, NULL);
  676. object_class_property_set_description(oc, "phandle-start",
  677. "The first phandle ID we may generate dynamically");
  678. object_class_property_add_str(oc, "dt-compatible",
  679. machine_get_dt_compatible, machine_set_dt_compatible);
  680. object_class_property_set_description(oc, "dt-compatible",
  681. "Overrides the \"compatible\" property of the dt root node");
  682. object_class_property_add_bool(oc, "dump-guest-core",
  683. machine_get_dump_guest_core, machine_set_dump_guest_core);
  684. object_class_property_set_description(oc, "dump-guest-core",
  685. "Include guest memory in a core dump");
  686. object_class_property_add_bool(oc, "mem-merge",
  687. machine_get_mem_merge, machine_set_mem_merge);
  688. object_class_property_set_description(oc, "mem-merge",
  689. "Enable/disable memory merge support");
  690. object_class_property_add_bool(oc, "usb",
  691. machine_get_usb, machine_set_usb);
  692. object_class_property_set_description(oc, "usb",
  693. "Set on/off to enable/disable usb");
  694. object_class_property_add_bool(oc, "graphics",
  695. machine_get_graphics, machine_set_graphics);
  696. object_class_property_set_description(oc, "graphics",
  697. "Set on/off to enable/disable graphics emulation");
  698. object_class_property_add_str(oc, "firmware",
  699. machine_get_firmware, machine_set_firmware);
  700. object_class_property_set_description(oc, "firmware",
  701. "Firmware image");
  702. object_class_property_add_bool(oc, "suppress-vmdesc",
  703. machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
  704. object_class_property_set_description(oc, "suppress-vmdesc",
  705. "Set on to disable self-describing migration");
  706. object_class_property_add_bool(oc, "enforce-config-section",
  707. machine_get_enforce_config_section, machine_set_enforce_config_section);
  708. object_class_property_set_description(oc, "enforce-config-section",
  709. "Set on to enforce configuration section migration");
  710. object_class_property_add_str(oc, "memory-encryption",
  711. machine_get_memory_encryption, machine_set_memory_encryption);
  712. object_class_property_set_description(oc, "memory-encryption",
  713. "Set memory encryption object to use");
  714. object_class_property_add_str(oc, "memory-backend",
  715. machine_get_memdev, machine_set_memdev);
  716. object_class_property_set_description(oc, "memory-backend",
  717. "Set RAM backend"
  718. "Valid value is ID of hostmem based backend");
  719. }
  720. static void machine_class_base_init(ObjectClass *oc, void *data)
  721. {
  722. if (!object_class_is_abstract(oc)) {
  723. MachineClass *mc = MACHINE_CLASS(oc);
  724. const char *cname = object_class_get_name(oc);
  725. assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
  726. mc->name = g_strndup(cname,
  727. strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
  728. mc->compat_props = g_ptr_array_new();
  729. }
  730. }
  731. static void machine_initfn(Object *obj)
  732. {
  733. MachineState *ms = MACHINE(obj);
  734. MachineClass *mc = MACHINE_GET_CLASS(obj);
  735. ms->dump_guest_core = true;
  736. ms->mem_merge = true;
  737. ms->enable_graphics = true;
  738. if (mc->nvdimm_supported) {
  739. Object *obj = OBJECT(ms);
  740. ms->nvdimms_state = g_new0(NVDIMMState, 1);
  741. object_property_add_bool(obj, "nvdimm",
  742. machine_get_nvdimm, machine_set_nvdimm);
  743. object_property_set_description(obj, "nvdimm",
  744. "Set on/off to enable/disable "
  745. "NVDIMM instantiation");
  746. object_property_add_str(obj, "nvdimm-persistence",
  747. machine_get_nvdimm_persistence,
  748. machine_set_nvdimm_persistence);
  749. object_property_set_description(obj, "nvdimm-persistence",
  750. "Set NVDIMM persistence"
  751. "Valid values are cpu, mem-ctrl");
  752. }
  753. if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
  754. ms->numa_state = g_new0(NumaState, 1);
  755. object_property_add_bool(obj, "hmat",
  756. machine_get_hmat, machine_set_hmat);
  757. object_property_set_description(obj, "hmat",
  758. "Set on/off to enable/disable "
  759. "ACPI Heterogeneous Memory Attribute "
  760. "Table (HMAT)");
  761. }
  762. /* Register notifier when init is done for sysbus sanity checks */
  763. ms->sysbus_notifier.notify = machine_init_notify;
  764. qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
  765. }
  766. static void machine_finalize(Object *obj)
  767. {
  768. MachineState *ms = MACHINE(obj);
  769. g_free(ms->kernel_filename);
  770. g_free(ms->initrd_filename);
  771. g_free(ms->kernel_cmdline);
  772. g_free(ms->dtb);
  773. g_free(ms->dumpdtb);
  774. g_free(ms->dt_compatible);
  775. g_free(ms->firmware);
  776. g_free(ms->device_memory);
  777. g_free(ms->nvdimms_state);
  778. g_free(ms->numa_state);
  779. }
  780. bool machine_usb(MachineState *machine)
  781. {
  782. return machine->usb;
  783. }
  784. int machine_phandle_start(MachineState *machine)
  785. {
  786. return machine->phandle_start;
  787. }
  788. bool machine_dump_guest_core(MachineState *machine)
  789. {
  790. return machine->dump_guest_core;
  791. }
  792. bool machine_mem_merge(MachineState *machine)
  793. {
  794. return machine->mem_merge;
  795. }
  796. static char *cpu_slot_to_string(const CPUArchId *cpu)
  797. {
  798. GString *s = g_string_new(NULL);
  799. if (cpu->props.has_socket_id) {
  800. g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
  801. }
  802. if (cpu->props.has_die_id) {
  803. g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
  804. }
  805. if (cpu->props.has_core_id) {
  806. if (s->len) {
  807. g_string_append_printf(s, ", ");
  808. }
  809. g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
  810. }
  811. if (cpu->props.has_thread_id) {
  812. if (s->len) {
  813. g_string_append_printf(s, ", ");
  814. }
  815. g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
  816. }
  817. return g_string_free(s, false);
  818. }
  819. static void numa_validate_initiator(NumaState *numa_state)
  820. {
  821. int i;
  822. NodeInfo *numa_info = numa_state->nodes;
  823. for (i = 0; i < numa_state->num_nodes; i++) {
  824. if (numa_info[i].initiator == MAX_NODES) {
  825. error_report("The initiator of NUMA node %d is missing, use "
  826. "'-numa node,initiator' option to declare it", i);
  827. exit(1);
  828. }
  829. if (!numa_info[numa_info[i].initiator].present) {
  830. error_report("NUMA node %" PRIu16 " is missing, use "
  831. "'-numa node' option to declare it first",
  832. numa_info[i].initiator);
  833. exit(1);
  834. }
  835. if (!numa_info[numa_info[i].initiator].has_cpu) {
  836. error_report("The initiator of NUMA node %d is invalid", i);
  837. exit(1);
  838. }
  839. }
  840. }
  841. static void machine_numa_finish_cpu_init(MachineState *machine)
  842. {
  843. int i;
  844. bool default_mapping;
  845. GString *s = g_string_new(NULL);
  846. MachineClass *mc = MACHINE_GET_CLASS(machine);
  847. const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
  848. assert(machine->numa_state->num_nodes);
  849. for (i = 0; i < possible_cpus->len; i++) {
  850. if (possible_cpus->cpus[i].props.has_node_id) {
  851. break;
  852. }
  853. }
  854. default_mapping = (i == possible_cpus->len);
  855. for (i = 0; i < possible_cpus->len; i++) {
  856. const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
  857. if (!cpu_slot->props.has_node_id) {
  858. /* fetch default mapping from board and enable it */
  859. CpuInstanceProperties props = cpu_slot->props;
  860. props.node_id = mc->get_default_cpu_node_id(machine, i);
  861. if (!default_mapping) {
  862. /* record slots with not set mapping,
  863. * TODO: make it hard error in future */
  864. char *cpu_str = cpu_slot_to_string(cpu_slot);
  865. g_string_append_printf(s, "%sCPU %d [%s]",
  866. s->len ? ", " : "", i, cpu_str);
  867. g_free(cpu_str);
  868. /* non mapped cpus used to fallback to node 0 */
  869. props.node_id = 0;
  870. }
  871. props.has_node_id = true;
  872. machine_set_cpu_numa_node(machine, &props, &error_fatal);
  873. }
  874. }
  875. if (machine->numa_state->hmat_enabled) {
  876. numa_validate_initiator(machine->numa_state);
  877. }
  878. if (s->len && !qtest_enabled()) {
  879. warn_report("CPU(s) not present in any NUMA nodes: %s",
  880. s->str);
  881. warn_report("All CPU(s) up to maxcpus should be described "
  882. "in NUMA config, ability to start up with partial NUMA "
  883. "mappings is obsoleted and will be removed in future");
  884. }
  885. g_string_free(s, true);
  886. }
  887. MemoryRegion *machine_consume_memdev(MachineState *machine,
  888. HostMemoryBackend *backend)
  889. {
  890. MemoryRegion *ret = host_memory_backend_get_memory(backend);
  891. if (memory_region_is_mapped(ret)) {
  892. error_report("memory backend %s can't be used multiple times.",
  893. object_get_canonical_path_component(OBJECT(backend)));
  894. exit(EXIT_FAILURE);
  895. }
  896. host_memory_backend_set_mapped(backend, true);
  897. vmstate_register_ram_global(ret);
  898. return ret;
  899. }
  900. void machine_run_board_init(MachineState *machine)
  901. {
  902. MachineClass *machine_class = MACHINE_GET_CLASS(machine);
  903. ObjectClass *oc = object_class_by_name(machine->cpu_type);
  904. CPUClass *cc;
  905. if (machine->ram_memdev_id) {
  906. Object *o;
  907. o = object_resolve_path_type(machine->ram_memdev_id,
  908. TYPE_MEMORY_BACKEND, NULL);
  909. machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o));
  910. }
  911. if (machine->numa_state) {
  912. numa_complete_configuration(machine);
  913. if (machine->numa_state->num_nodes) {
  914. machine_numa_finish_cpu_init(machine);
  915. }
  916. }
  917. /* If the machine supports the valid_cpu_types check and the user
  918. * specified a CPU with -cpu check here that the user CPU is supported.
  919. */
  920. if (machine_class->valid_cpu_types && machine->cpu_type) {
  921. int i;
  922. for (i = 0; machine_class->valid_cpu_types[i]; i++) {
  923. if (object_class_dynamic_cast(oc,
  924. machine_class->valid_cpu_types[i])) {
  925. /* The user specificed CPU is in the valid field, we are
  926. * good to go.
  927. */
  928. break;
  929. }
  930. }
  931. if (!machine_class->valid_cpu_types[i]) {
  932. /* The user specified CPU is not valid */
  933. error_report("Invalid CPU type: %s", machine->cpu_type);
  934. error_printf("The valid types are: %s",
  935. machine_class->valid_cpu_types[0]);
  936. for (i = 1; machine_class->valid_cpu_types[i]; i++) {
  937. error_printf(", %s", machine_class->valid_cpu_types[i]);
  938. }
  939. error_printf("\n");
  940. exit(1);
  941. }
  942. }
  943. /* Check if CPU type is deprecated and warn if so */
  944. cc = CPU_CLASS(oc);
  945. if (cc && cc->deprecation_note) {
  946. warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
  947. cc->deprecation_note);
  948. }
  949. machine_class->init(machine);
  950. }
  951. static const TypeInfo machine_info = {
  952. .name = TYPE_MACHINE,
  953. .parent = TYPE_OBJECT,
  954. .abstract = true,
  955. .class_size = sizeof(MachineClass),
  956. .class_init = machine_class_init,
  957. .class_base_init = machine_class_base_init,
  958. .instance_size = sizeof(MachineState),
  959. .instance_init = machine_initfn,
  960. .instance_finalize = machine_finalize,
  961. };
  962. static void machine_register_types(void)
  963. {
  964. type_register_static(&machine_info);
  965. }
  966. type_init(machine_register_types)