machine.c 53 KB

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