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