machine.c 37 KB

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