machine.c 47 KB

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