machine.c 56 KB

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