machine.c 55 KB

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