machine.c 48 KB

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