machine.c 42 KB

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