machine.c 50 KB

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