machine.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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 "qemu/accel.h"
  15. #include "sysemu/replay.h"
  16. #include "qemu/units.h"
  17. #include "hw/boards.h"
  18. #include "hw/loader.h"
  19. #include "qapi/error.h"
  20. #include "qapi/qapi-visit-common.h"
  21. #include "qapi/qapi-visit-machine.h"
  22. #include "qapi/visitor.h"
  23. #include "qom/object_interfaces.h"
  24. #include "hw/sysbus.h"
  25. #include "sysemu/cpus.h"
  26. #include "sysemu/sysemu.h"
  27. #include "sysemu/reset.h"
  28. #include "sysemu/runstate.h"
  29. #include "sysemu/numa.h"
  30. #include "sysemu/xen.h"
  31. #include "qemu/error-report.h"
  32. #include "sysemu/qtest.h"
  33. #include "hw/pci/pci.h"
  34. #include "hw/mem/nvdimm.h"
  35. #include "migration/global_state.h"
  36. #include "migration/vmstate.h"
  37. #include "exec/confidential-guest-support.h"
  38. #include "hw/virtio/virtio.h"
  39. #include "hw/virtio/virtio-pci.h"
  40. GlobalProperty hw_compat_7_2[] = {
  41. { "virtio-mem", "x-early-migration", "false" },
  42. };
  43. const size_t hw_compat_7_2_len = G_N_ELEMENTS(hw_compat_7_2);
  44. GlobalProperty hw_compat_7_1[] = {
  45. { "virtio-device", "queue_reset", "false" },
  46. { "virtio-rng-pci", "vectors", "0" },
  47. };
  48. const size_t hw_compat_7_1_len = G_N_ELEMENTS(hw_compat_7_1);
  49. GlobalProperty hw_compat_7_0[] = {
  50. { "arm-gicv3-common", "force-8-bit-prio", "on" },
  51. { "nvme-ns", "eui64-default", "on"},
  52. };
  53. const size_t hw_compat_7_0_len = G_N_ELEMENTS(hw_compat_7_0);
  54. GlobalProperty hw_compat_6_2[] = {
  55. { "PIIX4_PM", "x-not-migrate-acpi-index", "on"},
  56. };
  57. const size_t hw_compat_6_2_len = G_N_ELEMENTS(hw_compat_6_2);
  58. GlobalProperty hw_compat_6_1[] = {
  59. { "vhost-user-vsock-device", "seqpacket", "off" },
  60. { "nvme-ns", "shared", "off" },
  61. };
  62. const size_t hw_compat_6_1_len = G_N_ELEMENTS(hw_compat_6_1);
  63. GlobalProperty hw_compat_6_0[] = {
  64. { "gpex-pcihost", "allow-unmapped-accesses", "false" },
  65. { "i8042", "extended-state", "false"},
  66. { "nvme-ns", "eui64-default", "off"},
  67. { "e1000", "init-vet", "off" },
  68. { "e1000e", "init-vet", "off" },
  69. { "vhost-vsock-device", "seqpacket", "off" },
  70. };
  71. const size_t hw_compat_6_0_len = G_N_ELEMENTS(hw_compat_6_0);
  72. GlobalProperty hw_compat_5_2[] = {
  73. { "ICH9-LPC", "smm-compat", "on"},
  74. { "PIIX4_PM", "smm-compat", "on"},
  75. { "virtio-blk-device", "report-discard-granularity", "off" },
  76. { "virtio-net-pci-base", "vectors", "3"},
  77. };
  78. const size_t hw_compat_5_2_len = G_N_ELEMENTS(hw_compat_5_2);
  79. GlobalProperty hw_compat_5_1[] = {
  80. { "vhost-scsi", "num_queues", "1"},
  81. { "vhost-user-blk", "num-queues", "1"},
  82. { "vhost-user-scsi", "num_queues", "1"},
  83. { "virtio-blk-device", "num-queues", "1"},
  84. { "virtio-scsi-device", "num_queues", "1"},
  85. { "nvme", "use-intel-id", "on"},
  86. { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */
  87. { "pl011", "migrate-clk", "off" },
  88. { "virtio-pci", "x-ats-page-aligned", "off"},
  89. };
  90. const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
  91. GlobalProperty hw_compat_5_0[] = {
  92. { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
  93. { "virtio-balloon-device", "page-poison", "false" },
  94. { "vmport", "x-read-set-eax", "off" },
  95. { "vmport", "x-signal-unsupported-cmd", "off" },
  96. { "vmport", "x-report-vmx-type", "off" },
  97. { "vmport", "x-cmds-v2", "off" },
  98. { "virtio-device", "x-disable-legacy-check", "true" },
  99. };
  100. const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
  101. GlobalProperty hw_compat_4_2[] = {
  102. { "virtio-blk-device", "queue-size", "128"},
  103. { "virtio-scsi-device", "virtqueue_size", "128"},
  104. { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
  105. { "virtio-blk-device", "seg-max-adjust", "off"},
  106. { "virtio-scsi-device", "seg_max_adjust", "off"},
  107. { "vhost-blk-device", "seg_max_adjust", "off"},
  108. { "usb-host", "suppress-remote-wake", "off" },
  109. { "usb-redir", "suppress-remote-wake", "off" },
  110. { "qxl", "revision", "4" },
  111. { "qxl-vga", "revision", "4" },
  112. { "fw_cfg", "acpi-mr-restore", "false" },
  113. { "virtio-device", "use-disabled-flag", "false" },
  114. };
  115. const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
  116. GlobalProperty hw_compat_4_1[] = {
  117. { "virtio-pci", "x-pcie-flr-init", "off" },
  118. };
  119. const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
  120. GlobalProperty hw_compat_4_0[] = {
  121. { "VGA", "edid", "false" },
  122. { "secondary-vga", "edid", "false" },
  123. { "bochs-display", "edid", "false" },
  124. { "virtio-vga", "edid", "false" },
  125. { "virtio-gpu-device", "edid", "false" },
  126. { "virtio-device", "use-started", "false" },
  127. { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
  128. { "pl031", "migrate-tick-offset", "false" },
  129. };
  130. const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
  131. GlobalProperty hw_compat_3_1[] = {
  132. { "pcie-root-port", "x-speed", "2_5" },
  133. { "pcie-root-port", "x-width", "1" },
  134. { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
  135. { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
  136. { "tpm-crb", "ppi", "false" },
  137. { "tpm-tis", "ppi", "false" },
  138. { "usb-kbd", "serial", "42" },
  139. { "usb-mouse", "serial", "42" },
  140. { "usb-tablet", "serial", "42" },
  141. { "virtio-blk-device", "discard", "false" },
  142. { "virtio-blk-device", "write-zeroes", "false" },
  143. { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
  144. { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
  145. };
  146. const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
  147. GlobalProperty hw_compat_3_0[] = {};
  148. const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
  149. GlobalProperty hw_compat_2_12[] = {
  150. { "migration", "decompress-error-check", "off" },
  151. { "hda-audio", "use-timer", "false" },
  152. { "cirrus-vga", "global-vmstate", "true" },
  153. { "VGA", "global-vmstate", "true" },
  154. { "vmware-svga", "global-vmstate", "true" },
  155. { "qxl-vga", "global-vmstate", "true" },
  156. };
  157. const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
  158. GlobalProperty hw_compat_2_11[] = {
  159. { "hpet", "hpet-offset-saved", "false" },
  160. { "virtio-blk-pci", "vectors", "2" },
  161. { "vhost-user-blk-pci", "vectors", "2" },
  162. { "e1000", "migrate_tso_props", "off" },
  163. };
  164. const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
  165. GlobalProperty hw_compat_2_10[] = {
  166. { "virtio-mouse-device", "wheel-axis", "false" },
  167. { "virtio-tablet-device", "wheel-axis", "false" },
  168. };
  169. const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
  170. GlobalProperty hw_compat_2_9[] = {
  171. { "pci-bridge", "shpc", "off" },
  172. { "intel-iommu", "pt", "off" },
  173. { "virtio-net-device", "x-mtu-bypass-backend", "off" },
  174. { "pcie-root-port", "x-migrate-msix", "false" },
  175. };
  176. const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
  177. GlobalProperty hw_compat_2_8[] = {
  178. { "fw_cfg_mem", "x-file-slots", "0x10" },
  179. { "fw_cfg_io", "x-file-slots", "0x10" },
  180. { "pflash_cfi01", "old-multiple-chip-handling", "on" },
  181. { "pci-bridge", "shpc", "on" },
  182. { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
  183. { "virtio-pci", "x-pcie-deverr-init", "off" },
  184. { "virtio-pci", "x-pcie-lnkctl-init", "off" },
  185. { "virtio-pci", "x-pcie-pm-init", "off" },
  186. { "cirrus-vga", "vgamem_mb", "8" },
  187. { "isa-cirrus-vga", "vgamem_mb", "8" },
  188. };
  189. const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
  190. GlobalProperty hw_compat_2_7[] = {
  191. { "virtio-pci", "page-per-vq", "on" },
  192. { "virtio-serial-device", "emergency-write", "off" },
  193. { "ioapic", "version", "0x11" },
  194. { "intel-iommu", "x-buggy-eim", "true" },
  195. { "virtio-pci", "x-ignore-backend-features", "on" },
  196. };
  197. const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
  198. GlobalProperty hw_compat_2_6[] = {
  199. { "virtio-mmio", "format_transport_address", "off" },
  200. /* Optional because not all virtio-pci devices support legacy mode */
  201. { "virtio-pci", "disable-modern", "on", .optional = true },
  202. { "virtio-pci", "disable-legacy", "off", .optional = true },
  203. };
  204. const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
  205. GlobalProperty hw_compat_2_5[] = {
  206. { "isa-fdc", "fallback", "144" },
  207. { "pvscsi", "x-old-pci-configuration", "on" },
  208. { "pvscsi", "x-disable-pcie", "on" },
  209. { "vmxnet3", "x-old-msi-offsets", "on" },
  210. { "vmxnet3", "x-disable-pcie", "on" },
  211. };
  212. const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
  213. GlobalProperty hw_compat_2_4[] = {
  214. /* Optional because the 'scsi' property is Linux-only */
  215. { "virtio-blk-device", "scsi", "true", .optional = true },
  216. { "e1000", "extra_mac_registers", "off" },
  217. { "virtio-pci", "x-disable-pcie", "on" },
  218. { "virtio-pci", "migrate-extra", "off" },
  219. { "fw_cfg_mem", "dma_enabled", "off" },
  220. { "fw_cfg_io", "dma_enabled", "off" }
  221. };
  222. const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
  223. GlobalProperty hw_compat_2_3[] = {
  224. { "virtio-blk-pci", "any_layout", "off" },
  225. { "virtio-balloon-pci", "any_layout", "off" },
  226. { "virtio-serial-pci", "any_layout", "off" },
  227. { "virtio-9p-pci", "any_layout", "off" },
  228. { "virtio-rng-pci", "any_layout", "off" },
  229. { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
  230. { "migration", "send-configuration", "off" },
  231. { "migration", "send-section-footer", "off" },
  232. { "migration", "store-global-state", "off" },
  233. };
  234. const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
  235. GlobalProperty hw_compat_2_2[] = {};
  236. const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
  237. GlobalProperty hw_compat_2_1[] = {
  238. { "intel-hda", "old_msi_addr", "on" },
  239. { "VGA", "qemu-extended-regs", "off" },
  240. { "secondary-vga", "qemu-extended-regs", "off" },
  241. { "virtio-scsi-pci", "any_layout", "off" },
  242. { "usb-mouse", "usb_version", "1" },
  243. { "usb-kbd", "usb_version", "1" },
  244. { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
  245. };
  246. const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
  247. MachineState *current_machine;
  248. static char *machine_get_kernel(Object *obj, Error **errp)
  249. {
  250. MachineState *ms = MACHINE(obj);
  251. return g_strdup(ms->kernel_filename);
  252. }
  253. static void machine_set_kernel(Object *obj, const char *value, Error **errp)
  254. {
  255. MachineState *ms = MACHINE(obj);
  256. g_free(ms->kernel_filename);
  257. ms->kernel_filename = g_strdup(value);
  258. }
  259. static char *machine_get_initrd(Object *obj, Error **errp)
  260. {
  261. MachineState *ms = MACHINE(obj);
  262. return g_strdup(ms->initrd_filename);
  263. }
  264. static void machine_set_initrd(Object *obj, const char *value, Error **errp)
  265. {
  266. MachineState *ms = MACHINE(obj);
  267. g_free(ms->initrd_filename);
  268. ms->initrd_filename = g_strdup(value);
  269. }
  270. static char *machine_get_append(Object *obj, Error **errp)
  271. {
  272. MachineState *ms = MACHINE(obj);
  273. return g_strdup(ms->kernel_cmdline);
  274. }
  275. static void machine_set_append(Object *obj, const char *value, Error **errp)
  276. {
  277. MachineState *ms = MACHINE(obj);
  278. g_free(ms->kernel_cmdline);
  279. ms->kernel_cmdline = g_strdup(value);
  280. }
  281. static char *machine_get_dtb(Object *obj, Error **errp)
  282. {
  283. MachineState *ms = MACHINE(obj);
  284. return g_strdup(ms->dtb);
  285. }
  286. static void machine_set_dtb(Object *obj, const char *value, Error **errp)
  287. {
  288. MachineState *ms = MACHINE(obj);
  289. g_free(ms->dtb);
  290. ms->dtb = g_strdup(value);
  291. }
  292. static char *machine_get_dumpdtb(Object *obj, Error **errp)
  293. {
  294. MachineState *ms = MACHINE(obj);
  295. return g_strdup(ms->dumpdtb);
  296. }
  297. static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
  298. {
  299. MachineState *ms = MACHINE(obj);
  300. g_free(ms->dumpdtb);
  301. ms->dumpdtb = g_strdup(value);
  302. }
  303. static void machine_get_phandle_start(Object *obj, Visitor *v,
  304. const char *name, void *opaque,
  305. Error **errp)
  306. {
  307. MachineState *ms = MACHINE(obj);
  308. int64_t value = ms->phandle_start;
  309. visit_type_int(v, name, &value, errp);
  310. }
  311. static void machine_set_phandle_start(Object *obj, Visitor *v,
  312. const char *name, void *opaque,
  313. Error **errp)
  314. {
  315. MachineState *ms = MACHINE(obj);
  316. int64_t value;
  317. if (!visit_type_int(v, name, &value, errp)) {
  318. return;
  319. }
  320. ms->phandle_start = value;
  321. }
  322. static char *machine_get_dt_compatible(Object *obj, Error **errp)
  323. {
  324. MachineState *ms = MACHINE(obj);
  325. return g_strdup(ms->dt_compatible);
  326. }
  327. static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
  328. {
  329. MachineState *ms = MACHINE(obj);
  330. g_free(ms->dt_compatible);
  331. ms->dt_compatible = g_strdup(value);
  332. }
  333. static bool machine_get_dump_guest_core(Object *obj, Error **errp)
  334. {
  335. MachineState *ms = MACHINE(obj);
  336. return ms->dump_guest_core;
  337. }
  338. static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
  339. {
  340. MachineState *ms = MACHINE(obj);
  341. ms->dump_guest_core = value;
  342. }
  343. static bool machine_get_mem_merge(Object *obj, Error **errp)
  344. {
  345. MachineState *ms = MACHINE(obj);
  346. return ms->mem_merge;
  347. }
  348. static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
  349. {
  350. MachineState *ms = MACHINE(obj);
  351. ms->mem_merge = value;
  352. }
  353. static bool machine_get_usb(Object *obj, Error **errp)
  354. {
  355. MachineState *ms = MACHINE(obj);
  356. return ms->usb;
  357. }
  358. static void machine_set_usb(Object *obj, bool value, Error **errp)
  359. {
  360. MachineState *ms = MACHINE(obj);
  361. ms->usb = value;
  362. ms->usb_disabled = !value;
  363. }
  364. static bool machine_get_graphics(Object *obj, Error **errp)
  365. {
  366. MachineState *ms = MACHINE(obj);
  367. return ms->enable_graphics;
  368. }
  369. static void machine_set_graphics(Object *obj, bool value, Error **errp)
  370. {
  371. MachineState *ms = MACHINE(obj);
  372. ms->enable_graphics = value;
  373. }
  374. static char *machine_get_firmware(Object *obj, Error **errp)
  375. {
  376. MachineState *ms = MACHINE(obj);
  377. return g_strdup(ms->firmware);
  378. }
  379. static void machine_set_firmware(Object *obj, const char *value, Error **errp)
  380. {
  381. MachineState *ms = MACHINE(obj);
  382. g_free(ms->firmware);
  383. ms->firmware = g_strdup(value);
  384. }
  385. static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
  386. {
  387. MachineState *ms = MACHINE(obj);
  388. ms->suppress_vmdesc = value;
  389. }
  390. static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
  391. {
  392. MachineState *ms = MACHINE(obj);
  393. return ms->suppress_vmdesc;
  394. }
  395. static char *machine_get_memory_encryption(Object *obj, Error **errp)
  396. {
  397. MachineState *ms = MACHINE(obj);
  398. if (ms->cgs) {
  399. return g_strdup(object_get_canonical_path_component(OBJECT(ms->cgs)));
  400. }
  401. return NULL;
  402. }
  403. static void machine_set_memory_encryption(Object *obj, const char *value,
  404. Error **errp)
  405. {
  406. Object *cgs =
  407. object_resolve_path_component(object_get_objects_root(), value);
  408. if (!cgs) {
  409. error_setg(errp, "No such memory encryption object '%s'", value);
  410. return;
  411. }
  412. object_property_set_link(obj, "confidential-guest-support", cgs, errp);
  413. }
  414. static void machine_check_confidential_guest_support(const Object *obj,
  415. const char *name,
  416. Object *new_target,
  417. Error **errp)
  418. {
  419. /*
  420. * So far the only constraint is that the target has the
  421. * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked
  422. * by the QOM core
  423. */
  424. }
  425. static bool machine_get_nvdimm(Object *obj, Error **errp)
  426. {
  427. MachineState *ms = MACHINE(obj);
  428. return ms->nvdimms_state->is_enabled;
  429. }
  430. static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
  431. {
  432. MachineState *ms = MACHINE(obj);
  433. ms->nvdimms_state->is_enabled = value;
  434. }
  435. static bool machine_get_hmat(Object *obj, Error **errp)
  436. {
  437. MachineState *ms = MACHINE(obj);
  438. return ms->numa_state->hmat_enabled;
  439. }
  440. static void machine_set_hmat(Object *obj, bool value, Error **errp)
  441. {
  442. MachineState *ms = MACHINE(obj);
  443. ms->numa_state->hmat_enabled = value;
  444. }
  445. static void machine_get_mem(Object *obj, Visitor *v, const char *name,
  446. void *opaque, Error **errp)
  447. {
  448. MachineState *ms = MACHINE(obj);
  449. MemorySizeConfiguration mem = {
  450. .has_size = true,
  451. .size = ms->ram_size,
  452. .has_max_size = !!ms->ram_slots,
  453. .max_size = ms->maxram_size,
  454. .has_slots = !!ms->ram_slots,
  455. .slots = ms->ram_slots,
  456. };
  457. MemorySizeConfiguration *p_mem = &mem;
  458. visit_type_MemorySizeConfiguration(v, name, &p_mem, &error_abort);
  459. }
  460. static void machine_set_mem(Object *obj, Visitor *v, const char *name,
  461. void *opaque, Error **errp)
  462. {
  463. ERRP_GUARD();
  464. MachineState *ms = MACHINE(obj);
  465. MachineClass *mc = MACHINE_GET_CLASS(obj);
  466. MemorySizeConfiguration *mem;
  467. if (!visit_type_MemorySizeConfiguration(v, name, &mem, errp)) {
  468. return;
  469. }
  470. if (!mem->has_size) {
  471. mem->has_size = true;
  472. mem->size = mc->default_ram_size;
  473. }
  474. mem->size = QEMU_ALIGN_UP(mem->size, 8192);
  475. if (mc->fixup_ram_size) {
  476. mem->size = mc->fixup_ram_size(mem->size);
  477. }
  478. if ((ram_addr_t)mem->size != mem->size) {
  479. error_setg(errp, "ram size too large");
  480. goto out_free;
  481. }
  482. if (mem->has_max_size) {
  483. if (mem->max_size < mem->size) {
  484. error_setg(errp, "invalid value of maxmem: "
  485. "maximum memory size (0x%" PRIx64 ") must be at least "
  486. "the initial memory size (0x%" PRIx64 ")",
  487. mem->max_size, mem->size);
  488. goto out_free;
  489. }
  490. if (mem->has_slots && mem->slots && mem->max_size == mem->size) {
  491. error_setg(errp, "invalid value of maxmem: "
  492. "memory slots were specified but maximum memory size "
  493. "(0x%" PRIx64 ") is equal to the initial memory size "
  494. "(0x%" PRIx64 ")", mem->max_size, mem->size);
  495. goto out_free;
  496. }
  497. ms->maxram_size = mem->max_size;
  498. } else {
  499. if (mem->has_slots) {
  500. error_setg(errp, "slots specified but no max-size");
  501. goto out_free;
  502. }
  503. ms->maxram_size = mem->size;
  504. }
  505. ms->ram_size = mem->size;
  506. ms->ram_slots = mem->has_slots ? mem->slots : 0;
  507. out_free:
  508. qapi_free_MemorySizeConfiguration(mem);
  509. }
  510. static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
  511. {
  512. MachineState *ms = MACHINE(obj);
  513. return g_strdup(ms->nvdimms_state->persistence_string);
  514. }
  515. static void machine_set_nvdimm_persistence(Object *obj, const char *value,
  516. Error **errp)
  517. {
  518. MachineState *ms = MACHINE(obj);
  519. NVDIMMState *nvdimms_state = ms->nvdimms_state;
  520. if (strcmp(value, "cpu") == 0) {
  521. nvdimms_state->persistence = 3;
  522. } else if (strcmp(value, "mem-ctrl") == 0) {
  523. nvdimms_state->persistence = 2;
  524. } else {
  525. error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
  526. value);
  527. return;
  528. }
  529. g_free(nvdimms_state->persistence_string);
  530. nvdimms_state->persistence_string = g_strdup(value);
  531. }
  532. void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
  533. {
  534. QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
  535. }
  536. bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev)
  537. {
  538. Object *obj = OBJECT(dev);
  539. if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) {
  540. return false;
  541. }
  542. return device_type_is_dynamic_sysbus(mc, object_get_typename(obj));
  543. }
  544. bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type)
  545. {
  546. bool allowed = false;
  547. strList *wl;
  548. ObjectClass *klass = object_class_by_name(type);
  549. for (wl = mc->allowed_dynamic_sysbus_devices;
  550. !allowed && wl;
  551. wl = wl->next) {
  552. allowed |= !!object_class_dynamic_cast(klass, wl->value);
  553. }
  554. return allowed;
  555. }
  556. HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
  557. {
  558. int i;
  559. HotpluggableCPUList *head = NULL;
  560. MachineClass *mc = MACHINE_GET_CLASS(machine);
  561. /* force board to initialize possible_cpus if it hasn't been done yet */
  562. mc->possible_cpu_arch_ids(machine);
  563. for (i = 0; i < machine->possible_cpus->len; i++) {
  564. Object *cpu;
  565. HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
  566. cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
  567. cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
  568. cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
  569. sizeof(*cpu_item->props));
  570. cpu = machine->possible_cpus->cpus[i].cpu;
  571. if (cpu) {
  572. cpu_item->qom_path = object_get_canonical_path(cpu);
  573. }
  574. QAPI_LIST_PREPEND(head, cpu_item);
  575. }
  576. return head;
  577. }
  578. /**
  579. * machine_set_cpu_numa_node:
  580. * @machine: machine object to modify
  581. * @props: specifies which cpu objects to assign to
  582. * numa node specified by @props.node_id
  583. * @errp: if an error occurs, a pointer to an area to store the error
  584. *
  585. * Associate NUMA node specified by @props.node_id with cpu slots that
  586. * match socket/core/thread-ids specified by @props. It's recommended to use
  587. * query-hotpluggable-cpus.props values to specify affected cpu slots,
  588. * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
  589. *
  590. * However for CLI convenience it's possible to pass in subset of properties,
  591. * which would affect all cpu slots that match it.
  592. * Ex for pc machine:
  593. * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
  594. * -numa cpu,node-id=0,socket_id=0 \
  595. * -numa cpu,node-id=1,socket_id=1
  596. * will assign all child cores of socket 0 to node 0 and
  597. * of socket 1 to node 1.
  598. *
  599. * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
  600. * return error.
  601. * Empty subset is disallowed and function will return with error in this case.
  602. */
  603. void machine_set_cpu_numa_node(MachineState *machine,
  604. const CpuInstanceProperties *props, Error **errp)
  605. {
  606. MachineClass *mc = MACHINE_GET_CLASS(machine);
  607. NodeInfo *numa_info = machine->numa_state->nodes;
  608. bool match = false;
  609. int i;
  610. if (!mc->possible_cpu_arch_ids) {
  611. error_setg(errp, "mapping of CPUs to NUMA node is not supported");
  612. return;
  613. }
  614. /* disabling node mapping is not supported, forbid it */
  615. assert(props->has_node_id);
  616. /* force board to initialize possible_cpus if it hasn't been done yet */
  617. mc->possible_cpu_arch_ids(machine);
  618. for (i = 0; i < machine->possible_cpus->len; i++) {
  619. CPUArchId *slot = &machine->possible_cpus->cpus[i];
  620. /* reject unsupported by board properties */
  621. if (props->has_thread_id && !slot->props.has_thread_id) {
  622. error_setg(errp, "thread-id is not supported");
  623. return;
  624. }
  625. if (props->has_core_id && !slot->props.has_core_id) {
  626. error_setg(errp, "core-id is not supported");
  627. return;
  628. }
  629. if (props->has_cluster_id && !slot->props.has_cluster_id) {
  630. error_setg(errp, "cluster-id is not supported");
  631. return;
  632. }
  633. if (props->has_socket_id && !slot->props.has_socket_id) {
  634. error_setg(errp, "socket-id is not supported");
  635. return;
  636. }
  637. if (props->has_die_id && !slot->props.has_die_id) {
  638. error_setg(errp, "die-id is not supported");
  639. return;
  640. }
  641. /* skip slots with explicit mismatch */
  642. if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
  643. continue;
  644. }
  645. if (props->has_core_id && props->core_id != slot->props.core_id) {
  646. continue;
  647. }
  648. if (props->has_cluster_id &&
  649. props->cluster_id != slot->props.cluster_id) {
  650. continue;
  651. }
  652. if (props->has_die_id && props->die_id != slot->props.die_id) {
  653. continue;
  654. }
  655. if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
  656. continue;
  657. }
  658. /* reject assignment if slot is already assigned, for compatibility
  659. * of legacy cpu_index mapping with SPAPR core based mapping do not
  660. * error out if cpu thread and matched core have the same node-id */
  661. if (slot->props.has_node_id &&
  662. slot->props.node_id != props->node_id) {
  663. error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
  664. slot->props.node_id);
  665. return;
  666. }
  667. /* assign slot to node as it's matched '-numa cpu' key */
  668. match = true;
  669. slot->props.node_id = props->node_id;
  670. slot->props.has_node_id = props->has_node_id;
  671. if (machine->numa_state->hmat_enabled) {
  672. if ((numa_info[props->node_id].initiator < MAX_NODES) &&
  673. (props->node_id != numa_info[props->node_id].initiator)) {
  674. error_setg(errp, "The initiator of CPU NUMA node %" PRId64
  675. " should be itself (got %" PRIu16 ")",
  676. props->node_id, numa_info[props->node_id].initiator);
  677. return;
  678. }
  679. numa_info[props->node_id].has_cpu = true;
  680. numa_info[props->node_id].initiator = props->node_id;
  681. }
  682. }
  683. if (!match) {
  684. error_setg(errp, "no match found");
  685. }
  686. }
  687. static void machine_get_smp(Object *obj, Visitor *v, const char *name,
  688. void *opaque, Error **errp)
  689. {
  690. MachineState *ms = MACHINE(obj);
  691. SMPConfiguration *config = &(SMPConfiguration){
  692. .has_cpus = true, .cpus = ms->smp.cpus,
  693. .has_sockets = true, .sockets = ms->smp.sockets,
  694. .has_dies = true, .dies = ms->smp.dies,
  695. .has_clusters = true, .clusters = ms->smp.clusters,
  696. .has_cores = true, .cores = ms->smp.cores,
  697. .has_threads = true, .threads = ms->smp.threads,
  698. .has_maxcpus = true, .maxcpus = ms->smp.max_cpus,
  699. };
  700. if (!visit_type_SMPConfiguration(v, name, &config, &error_abort)) {
  701. return;
  702. }
  703. }
  704. static void machine_set_smp(Object *obj, Visitor *v, const char *name,
  705. void *opaque, Error **errp)
  706. {
  707. MachineState *ms = MACHINE(obj);
  708. g_autoptr(SMPConfiguration) config = NULL;
  709. if (!visit_type_SMPConfiguration(v, name, &config, errp)) {
  710. return;
  711. }
  712. machine_parse_smp_config(ms, config, errp);
  713. }
  714. static void machine_get_boot(Object *obj, Visitor *v, const char *name,
  715. void *opaque, Error **errp)
  716. {
  717. MachineState *ms = MACHINE(obj);
  718. BootConfiguration *config = &ms->boot_config;
  719. visit_type_BootConfiguration(v, name, &config, &error_abort);
  720. }
  721. static void machine_free_boot_config(MachineState *ms)
  722. {
  723. g_free(ms->boot_config.order);
  724. g_free(ms->boot_config.once);
  725. g_free(ms->boot_config.splash);
  726. }
  727. static void machine_copy_boot_config(MachineState *ms, BootConfiguration *config)
  728. {
  729. MachineClass *machine_class = MACHINE_GET_CLASS(ms);
  730. machine_free_boot_config(ms);
  731. ms->boot_config = *config;
  732. if (!config->order) {
  733. ms->boot_config.order = g_strdup(machine_class->default_boot_order);
  734. }
  735. }
  736. static void machine_set_boot(Object *obj, Visitor *v, const char *name,
  737. void *opaque, Error **errp)
  738. {
  739. ERRP_GUARD();
  740. MachineState *ms = MACHINE(obj);
  741. BootConfiguration *config = NULL;
  742. if (!visit_type_BootConfiguration(v, name, &config, errp)) {
  743. return;
  744. }
  745. if (config->order) {
  746. validate_bootdevices(config->order, errp);
  747. if (*errp) {
  748. goto out_free;
  749. }
  750. }
  751. if (config->once) {
  752. validate_bootdevices(config->once, errp);
  753. if (*errp) {
  754. goto out_free;
  755. }
  756. }
  757. machine_copy_boot_config(ms, config);
  758. /* Strings live in ms->boot_config. */
  759. free(config);
  760. return;
  761. out_free:
  762. qapi_free_BootConfiguration(config);
  763. }
  764. static void machine_class_init(ObjectClass *oc, void *data)
  765. {
  766. MachineClass *mc = MACHINE_CLASS(oc);
  767. /* Default 128 MB as guest ram size */
  768. mc->default_ram_size = 128 * MiB;
  769. mc->rom_file_has_mr = true;
  770. /* numa node memory size aligned on 8MB by default.
  771. * On Linux, each node's border has to be 8MB aligned
  772. */
  773. mc->numa_mem_align_shift = 23;
  774. object_class_property_add_str(oc, "kernel",
  775. machine_get_kernel, machine_set_kernel);
  776. object_class_property_set_description(oc, "kernel",
  777. "Linux kernel image file");
  778. object_class_property_add_str(oc, "initrd",
  779. machine_get_initrd, machine_set_initrd);
  780. object_class_property_set_description(oc, "initrd",
  781. "Linux initial ramdisk file");
  782. object_class_property_add_str(oc, "append",
  783. machine_get_append, machine_set_append);
  784. object_class_property_set_description(oc, "append",
  785. "Linux kernel command line");
  786. object_class_property_add_str(oc, "dtb",
  787. machine_get_dtb, machine_set_dtb);
  788. object_class_property_set_description(oc, "dtb",
  789. "Linux kernel device tree file");
  790. object_class_property_add_str(oc, "dumpdtb",
  791. machine_get_dumpdtb, machine_set_dumpdtb);
  792. object_class_property_set_description(oc, "dumpdtb",
  793. "Dump current dtb to a file and quit");
  794. object_class_property_add(oc, "boot", "BootConfiguration",
  795. machine_get_boot, machine_set_boot,
  796. NULL, NULL);
  797. object_class_property_set_description(oc, "boot",
  798. "Boot configuration");
  799. object_class_property_add(oc, "smp", "SMPConfiguration",
  800. machine_get_smp, machine_set_smp,
  801. NULL, NULL);
  802. object_class_property_set_description(oc, "smp",
  803. "CPU topology");
  804. object_class_property_add(oc, "phandle-start", "int",
  805. machine_get_phandle_start, machine_set_phandle_start,
  806. NULL, NULL);
  807. object_class_property_set_description(oc, "phandle-start",
  808. "The first phandle ID we may generate dynamically");
  809. object_class_property_add_str(oc, "dt-compatible",
  810. machine_get_dt_compatible, machine_set_dt_compatible);
  811. object_class_property_set_description(oc, "dt-compatible",
  812. "Overrides the \"compatible\" property of the dt root node");
  813. object_class_property_add_bool(oc, "dump-guest-core",
  814. machine_get_dump_guest_core, machine_set_dump_guest_core);
  815. object_class_property_set_description(oc, "dump-guest-core",
  816. "Include guest memory in a core dump");
  817. object_class_property_add_bool(oc, "mem-merge",
  818. machine_get_mem_merge, machine_set_mem_merge);
  819. object_class_property_set_description(oc, "mem-merge",
  820. "Enable/disable memory merge support");
  821. object_class_property_add_bool(oc, "usb",
  822. machine_get_usb, machine_set_usb);
  823. object_class_property_set_description(oc, "usb",
  824. "Set on/off to enable/disable usb");
  825. object_class_property_add_bool(oc, "graphics",
  826. machine_get_graphics, machine_set_graphics);
  827. object_class_property_set_description(oc, "graphics",
  828. "Set on/off to enable/disable graphics emulation");
  829. object_class_property_add_str(oc, "firmware",
  830. machine_get_firmware, machine_set_firmware);
  831. object_class_property_set_description(oc, "firmware",
  832. "Firmware image");
  833. object_class_property_add_bool(oc, "suppress-vmdesc",
  834. machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
  835. object_class_property_set_description(oc, "suppress-vmdesc",
  836. "Set on to disable self-describing migration");
  837. object_class_property_add_link(oc, "confidential-guest-support",
  838. TYPE_CONFIDENTIAL_GUEST_SUPPORT,
  839. offsetof(MachineState, cgs),
  840. machine_check_confidential_guest_support,
  841. OBJ_PROP_LINK_STRONG);
  842. object_class_property_set_description(oc, "confidential-guest-support",
  843. "Set confidential guest scheme to support");
  844. /* For compatibility */
  845. object_class_property_add_str(oc, "memory-encryption",
  846. machine_get_memory_encryption, machine_set_memory_encryption);
  847. object_class_property_set_description(oc, "memory-encryption",
  848. "Set memory encryption object to use");
  849. object_class_property_add_link(oc, "memory-backend", TYPE_MEMORY_BACKEND,
  850. offsetof(MachineState, memdev), object_property_allow_set_link,
  851. OBJ_PROP_LINK_STRONG);
  852. object_class_property_set_description(oc, "memory-backend",
  853. "Set RAM backend"
  854. "Valid value is ID of hostmem based backend");
  855. object_class_property_add(oc, "memory", "MemorySizeConfiguration",
  856. machine_get_mem, machine_set_mem,
  857. NULL, NULL);
  858. object_class_property_set_description(oc, "memory",
  859. "Memory size configuration");
  860. }
  861. static void machine_class_base_init(ObjectClass *oc, void *data)
  862. {
  863. MachineClass *mc = MACHINE_CLASS(oc);
  864. mc->max_cpus = mc->max_cpus ?: 1;
  865. mc->min_cpus = mc->min_cpus ?: 1;
  866. mc->default_cpus = mc->default_cpus ?: 1;
  867. if (!object_class_is_abstract(oc)) {
  868. const char *cname = object_class_get_name(oc);
  869. assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
  870. mc->name = g_strndup(cname,
  871. strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
  872. mc->compat_props = g_ptr_array_new();
  873. }
  874. }
  875. static void machine_initfn(Object *obj)
  876. {
  877. MachineState *ms = MACHINE(obj);
  878. MachineClass *mc = MACHINE_GET_CLASS(obj);
  879. container_get(obj, "/peripheral");
  880. container_get(obj, "/peripheral-anon");
  881. ms->dump_guest_core = true;
  882. ms->mem_merge = true;
  883. ms->enable_graphics = true;
  884. ms->kernel_cmdline = g_strdup("");
  885. ms->ram_size = mc->default_ram_size;
  886. ms->maxram_size = mc->default_ram_size;
  887. if (mc->nvdimm_supported) {
  888. Object *obj = OBJECT(ms);
  889. ms->nvdimms_state = g_new0(NVDIMMState, 1);
  890. object_property_add_bool(obj, "nvdimm",
  891. machine_get_nvdimm, machine_set_nvdimm);
  892. object_property_set_description(obj, "nvdimm",
  893. "Set on/off to enable/disable "
  894. "NVDIMM instantiation");
  895. object_property_add_str(obj, "nvdimm-persistence",
  896. machine_get_nvdimm_persistence,
  897. machine_set_nvdimm_persistence);
  898. object_property_set_description(obj, "nvdimm-persistence",
  899. "Set NVDIMM persistence"
  900. "Valid values are cpu, mem-ctrl");
  901. }
  902. if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
  903. ms->numa_state = g_new0(NumaState, 1);
  904. object_property_add_bool(obj, "hmat",
  905. machine_get_hmat, machine_set_hmat);
  906. object_property_set_description(obj, "hmat",
  907. "Set on/off to enable/disable "
  908. "ACPI Heterogeneous Memory Attribute "
  909. "Table (HMAT)");
  910. }
  911. /* default to mc->default_cpus */
  912. ms->smp.cpus = mc->default_cpus;
  913. ms->smp.max_cpus = mc->default_cpus;
  914. ms->smp.sockets = 1;
  915. ms->smp.dies = 1;
  916. ms->smp.clusters = 1;
  917. ms->smp.cores = 1;
  918. ms->smp.threads = 1;
  919. machine_copy_boot_config(ms, &(BootConfiguration){ 0 });
  920. }
  921. static void machine_finalize(Object *obj)
  922. {
  923. MachineState *ms = MACHINE(obj);
  924. machine_free_boot_config(ms);
  925. g_free(ms->kernel_filename);
  926. g_free(ms->initrd_filename);
  927. g_free(ms->kernel_cmdline);
  928. g_free(ms->dtb);
  929. g_free(ms->dumpdtb);
  930. g_free(ms->dt_compatible);
  931. g_free(ms->firmware);
  932. g_free(ms->device_memory);
  933. g_free(ms->nvdimms_state);
  934. g_free(ms->numa_state);
  935. }
  936. bool machine_usb(MachineState *machine)
  937. {
  938. return machine->usb;
  939. }
  940. int machine_phandle_start(MachineState *machine)
  941. {
  942. return machine->phandle_start;
  943. }
  944. bool machine_dump_guest_core(MachineState *machine)
  945. {
  946. return machine->dump_guest_core;
  947. }
  948. bool machine_mem_merge(MachineState *machine)
  949. {
  950. return machine->mem_merge;
  951. }
  952. static char *cpu_slot_to_string(const CPUArchId *cpu)
  953. {
  954. GString *s = g_string_new(NULL);
  955. if (cpu->props.has_socket_id) {
  956. g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
  957. }
  958. if (cpu->props.has_die_id) {
  959. if (s->len) {
  960. g_string_append_printf(s, ", ");
  961. }
  962. g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
  963. }
  964. if (cpu->props.has_cluster_id) {
  965. if (s->len) {
  966. g_string_append_printf(s, ", ");
  967. }
  968. g_string_append_printf(s, "cluster-id: %"PRId64, cpu->props.cluster_id);
  969. }
  970. if (cpu->props.has_core_id) {
  971. if (s->len) {
  972. g_string_append_printf(s, ", ");
  973. }
  974. g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
  975. }
  976. if (cpu->props.has_thread_id) {
  977. if (s->len) {
  978. g_string_append_printf(s, ", ");
  979. }
  980. g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
  981. }
  982. return g_string_free(s, false);
  983. }
  984. static void numa_validate_initiator(NumaState *numa_state)
  985. {
  986. int i;
  987. NodeInfo *numa_info = numa_state->nodes;
  988. for (i = 0; i < numa_state->num_nodes; i++) {
  989. if (numa_info[i].initiator == MAX_NODES) {
  990. continue;
  991. }
  992. if (!numa_info[numa_info[i].initiator].present) {
  993. error_report("NUMA node %" PRIu16 " is missing, use "
  994. "'-numa node' option to declare it first",
  995. numa_info[i].initiator);
  996. exit(1);
  997. }
  998. if (!numa_info[numa_info[i].initiator].has_cpu) {
  999. error_report("The initiator of NUMA node %d is invalid", i);
  1000. exit(1);
  1001. }
  1002. }
  1003. }
  1004. static void machine_numa_finish_cpu_init(MachineState *machine)
  1005. {
  1006. int i;
  1007. bool default_mapping;
  1008. GString *s = g_string_new(NULL);
  1009. MachineClass *mc = MACHINE_GET_CLASS(machine);
  1010. const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
  1011. assert(machine->numa_state->num_nodes);
  1012. for (i = 0; i < possible_cpus->len; i++) {
  1013. if (possible_cpus->cpus[i].props.has_node_id) {
  1014. break;
  1015. }
  1016. }
  1017. default_mapping = (i == possible_cpus->len);
  1018. for (i = 0; i < possible_cpus->len; i++) {
  1019. const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
  1020. if (!cpu_slot->props.has_node_id) {
  1021. /* fetch default mapping from board and enable it */
  1022. CpuInstanceProperties props = cpu_slot->props;
  1023. props.node_id = mc->get_default_cpu_node_id(machine, i);
  1024. if (!default_mapping) {
  1025. /* record slots with not set mapping,
  1026. * TODO: make it hard error in future */
  1027. char *cpu_str = cpu_slot_to_string(cpu_slot);
  1028. g_string_append_printf(s, "%sCPU %d [%s]",
  1029. s->len ? ", " : "", i, cpu_str);
  1030. g_free(cpu_str);
  1031. /* non mapped cpus used to fallback to node 0 */
  1032. props.node_id = 0;
  1033. }
  1034. props.has_node_id = true;
  1035. machine_set_cpu_numa_node(machine, &props, &error_fatal);
  1036. }
  1037. }
  1038. if (machine->numa_state->hmat_enabled) {
  1039. numa_validate_initiator(machine->numa_state);
  1040. }
  1041. if (s->len && !qtest_enabled()) {
  1042. warn_report("CPU(s) not present in any NUMA nodes: %s",
  1043. s->str);
  1044. warn_report("All CPU(s) up to maxcpus should be described "
  1045. "in NUMA config, ability to start up with partial NUMA "
  1046. "mappings is obsoleted and will be removed in future");
  1047. }
  1048. g_string_free(s, true);
  1049. }
  1050. MemoryRegion *machine_consume_memdev(MachineState *machine,
  1051. HostMemoryBackend *backend)
  1052. {
  1053. MemoryRegion *ret = host_memory_backend_get_memory(backend);
  1054. if (host_memory_backend_is_mapped(backend)) {
  1055. error_report("memory backend %s can't be used multiple times.",
  1056. object_get_canonical_path_component(OBJECT(backend)));
  1057. exit(EXIT_FAILURE);
  1058. }
  1059. host_memory_backend_set_mapped(backend, true);
  1060. vmstate_register_ram_global(ret);
  1061. return ret;
  1062. }
  1063. static bool create_default_memdev(MachineState *ms, const char *path, Error **errp)
  1064. {
  1065. Object *obj;
  1066. MachineClass *mc = MACHINE_GET_CLASS(ms);
  1067. bool r = false;
  1068. obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
  1069. if (path) {
  1070. if (!object_property_set_str(obj, "mem-path", path, errp)) {
  1071. goto out;
  1072. }
  1073. }
  1074. if (!object_property_set_int(obj, "size", ms->ram_size, errp)) {
  1075. goto out;
  1076. }
  1077. object_property_add_child(object_get_objects_root(), mc->default_ram_id,
  1078. obj);
  1079. /* Ensure backend's memory region name is equal to mc->default_ram_id */
  1080. if (!object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
  1081. false, errp)) {
  1082. goto out;
  1083. }
  1084. if (!user_creatable_complete(USER_CREATABLE(obj), errp)) {
  1085. goto out;
  1086. }
  1087. r = object_property_set_link(OBJECT(ms), "memory-backend", obj, errp);
  1088. out:
  1089. object_unref(obj);
  1090. return r;
  1091. }
  1092. void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp)
  1093. {
  1094. MachineClass *machine_class = MACHINE_GET_CLASS(machine);
  1095. ObjectClass *oc = object_class_by_name(machine->cpu_type);
  1096. CPUClass *cc;
  1097. /* This checkpoint is required by replay to separate prior clock
  1098. reading from the other reads, because timer polling functions query
  1099. clock values from the log. */
  1100. replay_checkpoint(CHECKPOINT_INIT);
  1101. if (!xen_enabled()) {
  1102. /* On 32-bit hosts, QEMU is limited by virtual address space */
  1103. if (machine->ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
  1104. error_setg(errp, "at most 2047 MB RAM can be simulated");
  1105. return;
  1106. }
  1107. }
  1108. if (machine->memdev) {
  1109. ram_addr_t backend_size = object_property_get_uint(OBJECT(machine->memdev),
  1110. "size", &error_abort);
  1111. if (backend_size != machine->ram_size) {
  1112. error_setg(errp, "Machine memory size does not match the size of the memory backend");
  1113. return;
  1114. }
  1115. } else if (machine_class->default_ram_id && machine->ram_size &&
  1116. numa_uses_legacy_mem()) {
  1117. if (!create_default_memdev(current_machine, mem_path, errp)) {
  1118. return;
  1119. }
  1120. }
  1121. if (machine->numa_state) {
  1122. numa_complete_configuration(machine);
  1123. if (machine->numa_state->num_nodes) {
  1124. machine_numa_finish_cpu_init(machine);
  1125. }
  1126. }
  1127. if (!machine->ram && machine->memdev) {
  1128. machine->ram = machine_consume_memdev(machine, machine->memdev);
  1129. }
  1130. /* If the machine supports the valid_cpu_types check and the user
  1131. * specified a CPU with -cpu check here that the user CPU is supported.
  1132. */
  1133. if (machine_class->valid_cpu_types && machine->cpu_type) {
  1134. int i;
  1135. for (i = 0; machine_class->valid_cpu_types[i]; i++) {
  1136. if (object_class_dynamic_cast(oc,
  1137. machine_class->valid_cpu_types[i])) {
  1138. /* The user specificed CPU is in the valid field, we are
  1139. * good to go.
  1140. */
  1141. break;
  1142. }
  1143. }
  1144. if (!machine_class->valid_cpu_types[i]) {
  1145. /* The user specified CPU is not valid */
  1146. error_report("Invalid CPU type: %s", machine->cpu_type);
  1147. error_printf("The valid types are: %s",
  1148. machine_class->valid_cpu_types[0]);
  1149. for (i = 1; machine_class->valid_cpu_types[i]; i++) {
  1150. error_printf(", %s", machine_class->valid_cpu_types[i]);
  1151. }
  1152. error_printf("\n");
  1153. exit(1);
  1154. }
  1155. }
  1156. /* Check if CPU type is deprecated and warn if so */
  1157. cc = CPU_CLASS(oc);
  1158. if (cc && cc->deprecation_note) {
  1159. warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
  1160. cc->deprecation_note);
  1161. }
  1162. if (machine->cgs) {
  1163. /*
  1164. * With confidential guests, the host can't see the real
  1165. * contents of RAM, so there's no point in it trying to merge
  1166. * areas.
  1167. */
  1168. machine_set_mem_merge(OBJECT(machine), false, &error_abort);
  1169. /*
  1170. * Virtio devices can't count on directly accessing guest
  1171. * memory, so they need iommu_platform=on to use normal DMA
  1172. * mechanisms. That requires also disabling legacy virtio
  1173. * support for those virtio pci devices which allow it.
  1174. */
  1175. object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
  1176. "on", true);
  1177. object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
  1178. "on", false);
  1179. }
  1180. accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator));
  1181. machine_class->init(machine);
  1182. phase_advance(PHASE_MACHINE_INITIALIZED);
  1183. }
  1184. static NotifierList machine_init_done_notifiers =
  1185. NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
  1186. void qemu_add_machine_init_done_notifier(Notifier *notify)
  1187. {
  1188. notifier_list_add(&machine_init_done_notifiers, notify);
  1189. if (phase_check(PHASE_MACHINE_READY)) {
  1190. notify->notify(notify, NULL);
  1191. }
  1192. }
  1193. void qemu_remove_machine_init_done_notifier(Notifier *notify)
  1194. {
  1195. notifier_remove(notify);
  1196. }
  1197. void qdev_machine_creation_done(void)
  1198. {
  1199. cpu_synchronize_all_post_init();
  1200. if (current_machine->boot_config.once) {
  1201. qemu_boot_set(current_machine->boot_config.once, &error_fatal);
  1202. qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_config.order));
  1203. }
  1204. /*
  1205. * ok, initial machine setup is done, starting from now we can
  1206. * only create hotpluggable devices
  1207. */
  1208. phase_advance(PHASE_MACHINE_READY);
  1209. qdev_assert_realized_properly();
  1210. /* TODO: once all bus devices are qdevified, this should be done
  1211. * when bus is created by qdev.c */
  1212. /*
  1213. * TODO: If we had a main 'reset container' that the whole system
  1214. * lived in, we could reset that using the multi-phase reset
  1215. * APIs. For the moment, we just reset the sysbus, which will cause
  1216. * all devices hanging off it (and all their child buses, recursively)
  1217. * to be reset. Note that this will *not* reset any Device objects
  1218. * which are not attached to some part of the qbus tree!
  1219. */
  1220. qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
  1221. notifier_list_notify(&machine_init_done_notifiers, NULL);
  1222. if (rom_check_and_register_reset() != 0) {
  1223. exit(1);
  1224. }
  1225. replay_start();
  1226. /* This checkpoint is required by replay to separate prior clock
  1227. reading from the other reads, because timer polling functions query
  1228. clock values from the log. */
  1229. replay_checkpoint(CHECKPOINT_RESET);
  1230. qemu_system_reset(SHUTDOWN_CAUSE_NONE);
  1231. register_global_state();
  1232. }
  1233. static const TypeInfo machine_info = {
  1234. .name = TYPE_MACHINE,
  1235. .parent = TYPE_OBJECT,
  1236. .abstract = true,
  1237. .class_size = sizeof(MachineClass),
  1238. .class_init = machine_class_init,
  1239. .class_base_init = machine_class_base_init,
  1240. .instance_size = sizeof(MachineState),
  1241. .instance_init = machine_initfn,
  1242. .instance_finalize = machine_finalize,
  1243. };
  1244. static void machine_register_types(void)
  1245. {
  1246. type_register_static(&machine_info);
  1247. }
  1248. type_init(machine_register_types)