2
0

machine.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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 "qapi/error.h"
  19. #include "qapi/qapi-visit-common.h"
  20. #include "qapi/visitor.h"
  21. #include "hw/sysbus.h"
  22. #include "sysemu/sysemu.h"
  23. #include "sysemu/numa.h"
  24. #include "qemu/error-report.h"
  25. #include "sysemu/qtest.h"
  26. #include "hw/pci/pci.h"
  27. #include "hw/mem/nvdimm.h"
  28. GlobalProperty hw_compat_4_1[] = {
  29. { "virtio-pci", "x-pcie-flr-init", "off" },
  30. };
  31. const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
  32. GlobalProperty hw_compat_4_0[] = {
  33. { "VGA", "edid", "false" },
  34. { "secondary-vga", "edid", "false" },
  35. { "bochs-display", "edid", "false" },
  36. { "virtio-vga", "edid", "false" },
  37. { "virtio-gpu", "edid", "false" },
  38. { "virtio-device", "use-started", "false" },
  39. { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
  40. { "pl031", "migrate-tick-offset", "false" },
  41. };
  42. const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
  43. GlobalProperty hw_compat_3_1[] = {
  44. { "pcie-root-port", "x-speed", "2_5" },
  45. { "pcie-root-port", "x-width", "1" },
  46. { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
  47. { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
  48. { "tpm-crb", "ppi", "false" },
  49. { "tpm-tis", "ppi", "false" },
  50. { "usb-kbd", "serial", "42" },
  51. { "usb-mouse", "serial", "42" },
  52. { "usb-tablet", "serial", "42" },
  53. { "virtio-blk-device", "discard", "false" },
  54. { "virtio-blk-device", "write-zeroes", "false" },
  55. { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
  56. { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
  57. };
  58. const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
  59. GlobalProperty hw_compat_3_0[] = {};
  60. const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
  61. GlobalProperty hw_compat_2_12[] = {
  62. { "migration", "decompress-error-check", "off" },
  63. { "hda-audio", "use-timer", "false" },
  64. { "cirrus-vga", "global-vmstate", "true" },
  65. { "VGA", "global-vmstate", "true" },
  66. { "vmware-svga", "global-vmstate", "true" },
  67. { "qxl-vga", "global-vmstate", "true" },
  68. };
  69. const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
  70. GlobalProperty hw_compat_2_11[] = {
  71. { "hpet", "hpet-offset-saved", "false" },
  72. { "virtio-blk-pci", "vectors", "2" },
  73. { "vhost-user-blk-pci", "vectors", "2" },
  74. { "e1000", "migrate_tso_props", "off" },
  75. };
  76. const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
  77. GlobalProperty hw_compat_2_10[] = {
  78. { "virtio-mouse-device", "wheel-axis", "false" },
  79. { "virtio-tablet-device", "wheel-axis", "false" },
  80. };
  81. const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
  82. GlobalProperty hw_compat_2_9[] = {
  83. { "pci-bridge", "shpc", "off" },
  84. { "intel-iommu", "pt", "off" },
  85. { "virtio-net-device", "x-mtu-bypass-backend", "off" },
  86. { "pcie-root-port", "x-migrate-msix", "false" },
  87. };
  88. const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
  89. GlobalProperty hw_compat_2_8[] = {
  90. { "fw_cfg_mem", "x-file-slots", "0x10" },
  91. { "fw_cfg_io", "x-file-slots", "0x10" },
  92. { "pflash_cfi01", "old-multiple-chip-handling", "on" },
  93. { "pci-bridge", "shpc", "on" },
  94. { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
  95. { "virtio-pci", "x-pcie-deverr-init", "off" },
  96. { "virtio-pci", "x-pcie-lnkctl-init", "off" },
  97. { "virtio-pci", "x-pcie-pm-init", "off" },
  98. { "cirrus-vga", "vgamem_mb", "8" },
  99. { "isa-cirrus-vga", "vgamem_mb", "8" },
  100. };
  101. const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
  102. GlobalProperty hw_compat_2_7[] = {
  103. { "virtio-pci", "page-per-vq", "on" },
  104. { "virtio-serial-device", "emergency-write", "off" },
  105. { "ioapic", "version", "0x11" },
  106. { "intel-iommu", "x-buggy-eim", "true" },
  107. { "virtio-pci", "x-ignore-backend-features", "on" },
  108. };
  109. const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
  110. GlobalProperty hw_compat_2_6[] = {
  111. { "virtio-mmio", "format_transport_address", "off" },
  112. /* Optional because not all virtio-pci devices support legacy mode */
  113. { "virtio-pci", "disable-modern", "on", .optional = true },
  114. { "virtio-pci", "disable-legacy", "off", .optional = true },
  115. };
  116. const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
  117. GlobalProperty hw_compat_2_5[] = {
  118. { "isa-fdc", "fallback", "144" },
  119. { "pvscsi", "x-old-pci-configuration", "on" },
  120. { "pvscsi", "x-disable-pcie", "on" },
  121. { "vmxnet3", "x-old-msi-offsets", "on" },
  122. { "vmxnet3", "x-disable-pcie", "on" },
  123. };
  124. const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
  125. GlobalProperty hw_compat_2_4[] = {
  126. { "virtio-blk-device", "scsi", "true" },
  127. { "e1000", "extra_mac_registers", "off" },
  128. { "virtio-pci", "x-disable-pcie", "on" },
  129. { "virtio-pci", "migrate-extra", "off" },
  130. { "fw_cfg_mem", "dma_enabled", "off" },
  131. { "fw_cfg_io", "dma_enabled", "off" }
  132. };
  133. const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
  134. GlobalProperty hw_compat_2_3[] = {
  135. { "virtio-blk-pci", "any_layout", "off" },
  136. { "virtio-balloon-pci", "any_layout", "off" },
  137. { "virtio-serial-pci", "any_layout", "off" },
  138. { "virtio-9p-pci", "any_layout", "off" },
  139. { "virtio-rng-pci", "any_layout", "off" },
  140. { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
  141. { "migration", "send-configuration", "off" },
  142. { "migration", "send-section-footer", "off" },
  143. { "migration", "store-global-state", "off" },
  144. };
  145. const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
  146. GlobalProperty hw_compat_2_2[] = {};
  147. const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
  148. GlobalProperty hw_compat_2_1[] = {
  149. { "intel-hda", "old_msi_addr", "on" },
  150. { "VGA", "qemu-extended-regs", "off" },
  151. { "secondary-vga", "qemu-extended-regs", "off" },
  152. { "virtio-scsi-pci", "any_layout", "off" },
  153. { "usb-mouse", "usb_version", "1" },
  154. { "usb-kbd", "usb_version", "1" },
  155. { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
  156. };
  157. const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
  158. static char *machine_get_accel(Object *obj, Error **errp)
  159. {
  160. MachineState *ms = MACHINE(obj);
  161. return g_strdup(ms->accel);
  162. }
  163. static void machine_set_accel(Object *obj, const char *value, Error **errp)
  164. {
  165. MachineState *ms = MACHINE(obj);
  166. g_free(ms->accel);
  167. ms->accel = g_strdup(value);
  168. }
  169. static void machine_set_kernel_irqchip(Object *obj, Visitor *v,
  170. const char *name, void *opaque,
  171. Error **errp)
  172. {
  173. Error *err = NULL;
  174. MachineState *ms = MACHINE(obj);
  175. OnOffSplit mode;
  176. visit_type_OnOffSplit(v, name, &mode, &err);
  177. if (err) {
  178. error_propagate(errp, err);
  179. return;
  180. } else {
  181. switch (mode) {
  182. case ON_OFF_SPLIT_ON:
  183. ms->kernel_irqchip_allowed = true;
  184. ms->kernel_irqchip_required = true;
  185. ms->kernel_irqchip_split = false;
  186. break;
  187. case ON_OFF_SPLIT_OFF:
  188. ms->kernel_irqchip_allowed = false;
  189. ms->kernel_irqchip_required = false;
  190. ms->kernel_irqchip_split = false;
  191. break;
  192. case ON_OFF_SPLIT_SPLIT:
  193. ms->kernel_irqchip_allowed = true;
  194. ms->kernel_irqchip_required = true;
  195. ms->kernel_irqchip_split = true;
  196. break;
  197. default:
  198. /* The value was checked in visit_type_OnOffSplit() above. If
  199. * we get here, then something is wrong in QEMU.
  200. */
  201. abort();
  202. }
  203. }
  204. }
  205. static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v,
  206. const char *name, void *opaque,
  207. Error **errp)
  208. {
  209. MachineState *ms = MACHINE(obj);
  210. int64_t value = ms->kvm_shadow_mem;
  211. visit_type_int(v, name, &value, errp);
  212. }
  213. static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v,
  214. const char *name, void *opaque,
  215. Error **errp)
  216. {
  217. MachineState *ms = MACHINE(obj);
  218. Error *error = NULL;
  219. int64_t value;
  220. visit_type_int(v, name, &value, &error);
  221. if (error) {
  222. error_propagate(errp, error);
  223. return;
  224. }
  225. ms->kvm_shadow_mem = value;
  226. }
  227. static char *machine_get_kernel(Object *obj, Error **errp)
  228. {
  229. MachineState *ms = MACHINE(obj);
  230. return g_strdup(ms->kernel_filename);
  231. }
  232. static void machine_set_kernel(Object *obj, const char *value, Error **errp)
  233. {
  234. MachineState *ms = MACHINE(obj);
  235. g_free(ms->kernel_filename);
  236. ms->kernel_filename = g_strdup(value);
  237. }
  238. static char *machine_get_initrd(Object *obj, Error **errp)
  239. {
  240. MachineState *ms = MACHINE(obj);
  241. return g_strdup(ms->initrd_filename);
  242. }
  243. static void machine_set_initrd(Object *obj, const char *value, Error **errp)
  244. {
  245. MachineState *ms = MACHINE(obj);
  246. g_free(ms->initrd_filename);
  247. ms->initrd_filename = g_strdup(value);
  248. }
  249. static char *machine_get_append(Object *obj, Error **errp)
  250. {
  251. MachineState *ms = MACHINE(obj);
  252. return g_strdup(ms->kernel_cmdline);
  253. }
  254. static void machine_set_append(Object *obj, const char *value, Error **errp)
  255. {
  256. MachineState *ms = MACHINE(obj);
  257. g_free(ms->kernel_cmdline);
  258. ms->kernel_cmdline = g_strdup(value);
  259. }
  260. static char *machine_get_dtb(Object *obj, Error **errp)
  261. {
  262. MachineState *ms = MACHINE(obj);
  263. return g_strdup(ms->dtb);
  264. }
  265. static void machine_set_dtb(Object *obj, const char *value, Error **errp)
  266. {
  267. MachineState *ms = MACHINE(obj);
  268. g_free(ms->dtb);
  269. ms->dtb = g_strdup(value);
  270. }
  271. static char *machine_get_dumpdtb(Object *obj, Error **errp)
  272. {
  273. MachineState *ms = MACHINE(obj);
  274. return g_strdup(ms->dumpdtb);
  275. }
  276. static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
  277. {
  278. MachineState *ms = MACHINE(obj);
  279. g_free(ms->dumpdtb);
  280. ms->dumpdtb = g_strdup(value);
  281. }
  282. static void machine_get_phandle_start(Object *obj, Visitor *v,
  283. const char *name, void *opaque,
  284. Error **errp)
  285. {
  286. MachineState *ms = MACHINE(obj);
  287. int64_t value = ms->phandle_start;
  288. visit_type_int(v, name, &value, errp);
  289. }
  290. static void machine_set_phandle_start(Object *obj, Visitor *v,
  291. const char *name, void *opaque,
  292. Error **errp)
  293. {
  294. MachineState *ms = MACHINE(obj);
  295. Error *error = NULL;
  296. int64_t value;
  297. visit_type_int(v, name, &value, &error);
  298. if (error) {
  299. error_propagate(errp, error);
  300. return;
  301. }
  302. ms->phandle_start = value;
  303. }
  304. static char *machine_get_dt_compatible(Object *obj, Error **errp)
  305. {
  306. MachineState *ms = MACHINE(obj);
  307. return g_strdup(ms->dt_compatible);
  308. }
  309. static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
  310. {
  311. MachineState *ms = MACHINE(obj);
  312. g_free(ms->dt_compatible);
  313. ms->dt_compatible = g_strdup(value);
  314. }
  315. static bool machine_get_dump_guest_core(Object *obj, Error **errp)
  316. {
  317. MachineState *ms = MACHINE(obj);
  318. return ms->dump_guest_core;
  319. }
  320. static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
  321. {
  322. MachineState *ms = MACHINE(obj);
  323. ms->dump_guest_core = value;
  324. }
  325. static bool machine_get_mem_merge(Object *obj, Error **errp)
  326. {
  327. MachineState *ms = MACHINE(obj);
  328. return ms->mem_merge;
  329. }
  330. static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
  331. {
  332. MachineState *ms = MACHINE(obj);
  333. ms->mem_merge = value;
  334. }
  335. static bool machine_get_usb(Object *obj, Error **errp)
  336. {
  337. MachineState *ms = MACHINE(obj);
  338. return ms->usb;
  339. }
  340. static void machine_set_usb(Object *obj, bool value, Error **errp)
  341. {
  342. MachineState *ms = MACHINE(obj);
  343. ms->usb = value;
  344. ms->usb_disabled = !value;
  345. }
  346. static bool machine_get_graphics(Object *obj, Error **errp)
  347. {
  348. MachineState *ms = MACHINE(obj);
  349. return ms->enable_graphics;
  350. }
  351. static void machine_set_graphics(Object *obj, bool value, Error **errp)
  352. {
  353. MachineState *ms = MACHINE(obj);
  354. ms->enable_graphics = value;
  355. }
  356. static bool machine_get_igd_gfx_passthru(Object *obj, Error **errp)
  357. {
  358. MachineState *ms = MACHINE(obj);
  359. return ms->igd_gfx_passthru;
  360. }
  361. static void machine_set_igd_gfx_passthru(Object *obj, bool value, Error **errp)
  362. {
  363. MachineState *ms = MACHINE(obj);
  364. ms->igd_gfx_passthru = value;
  365. }
  366. static char *machine_get_firmware(Object *obj, Error **errp)
  367. {
  368. MachineState *ms = MACHINE(obj);
  369. return g_strdup(ms->firmware);
  370. }
  371. static void machine_set_firmware(Object *obj, const char *value, Error **errp)
  372. {
  373. MachineState *ms = MACHINE(obj);
  374. g_free(ms->firmware);
  375. ms->firmware = g_strdup(value);
  376. }
  377. static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
  378. {
  379. MachineState *ms = MACHINE(obj);
  380. ms->suppress_vmdesc = value;
  381. }
  382. static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
  383. {
  384. MachineState *ms = MACHINE(obj);
  385. return ms->suppress_vmdesc;
  386. }
  387. static void machine_set_enforce_config_section(Object *obj, bool value,
  388. Error **errp)
  389. {
  390. MachineState *ms = MACHINE(obj);
  391. warn_report("enforce-config-section is deprecated, please use "
  392. "-global migration.send-configuration=on|off instead");
  393. ms->enforce_config_section = value;
  394. }
  395. static bool machine_get_enforce_config_section(Object *obj, Error **errp)
  396. {
  397. MachineState *ms = MACHINE(obj);
  398. return ms->enforce_config_section;
  399. }
  400. static char *machine_get_memory_encryption(Object *obj, Error **errp)
  401. {
  402. MachineState *ms = MACHINE(obj);
  403. return g_strdup(ms->memory_encryption);
  404. }
  405. static void machine_set_memory_encryption(Object *obj, const char *value,
  406. Error **errp)
  407. {
  408. MachineState *ms = MACHINE(obj);
  409. g_free(ms->memory_encryption);
  410. ms->memory_encryption = g_strdup(value);
  411. }
  412. static bool machine_get_nvdimm(Object *obj, Error **errp)
  413. {
  414. MachineState *ms = MACHINE(obj);
  415. return ms->nvdimms_state->is_enabled;
  416. }
  417. static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
  418. {
  419. MachineState *ms = MACHINE(obj);
  420. ms->nvdimms_state->is_enabled = value;
  421. }
  422. static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
  423. {
  424. MachineState *ms = MACHINE(obj);
  425. return g_strdup(ms->nvdimms_state->persistence_string);
  426. }
  427. static void machine_set_nvdimm_persistence(Object *obj, const char *value,
  428. Error **errp)
  429. {
  430. MachineState *ms = MACHINE(obj);
  431. NVDIMMState *nvdimms_state = ms->nvdimms_state;
  432. if (strcmp(value, "cpu") == 0) {
  433. nvdimms_state->persistence = 3;
  434. } else if (strcmp(value, "mem-ctrl") == 0) {
  435. nvdimms_state->persistence = 2;
  436. } else {
  437. error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
  438. value);
  439. return;
  440. }
  441. g_free(nvdimms_state->persistence_string);
  442. nvdimms_state->persistence_string = g_strdup(value);
  443. }
  444. void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
  445. {
  446. strList *item = g_new0(strList, 1);
  447. item->value = g_strdup(type);
  448. item->next = mc->allowed_dynamic_sysbus_devices;
  449. mc->allowed_dynamic_sysbus_devices = item;
  450. }
  451. static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
  452. {
  453. MachineState *machine = opaque;
  454. MachineClass *mc = MACHINE_GET_CLASS(machine);
  455. bool allowed = false;
  456. strList *wl;
  457. for (wl = mc->allowed_dynamic_sysbus_devices;
  458. !allowed && wl;
  459. wl = wl->next) {
  460. allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
  461. }
  462. if (!allowed) {
  463. error_report("Option '-device %s' cannot be handled by this machine",
  464. object_class_get_name(object_get_class(OBJECT(sbdev))));
  465. exit(1);
  466. }
  467. }
  468. static void machine_init_notify(Notifier *notifier, void *data)
  469. {
  470. MachineState *machine = MACHINE(qdev_get_machine());
  471. /*
  472. * Loop through all dynamically created sysbus devices and check if they are
  473. * all allowed. If a device is not allowed, error out.
  474. */
  475. foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
  476. }
  477. HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
  478. {
  479. int i;
  480. HotpluggableCPUList *head = NULL;
  481. MachineClass *mc = MACHINE_GET_CLASS(machine);
  482. /* force board to initialize possible_cpus if it hasn't been done yet */
  483. mc->possible_cpu_arch_ids(machine);
  484. for (i = 0; i < machine->possible_cpus->len; i++) {
  485. Object *cpu;
  486. HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
  487. HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
  488. cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
  489. cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
  490. cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
  491. sizeof(*cpu_item->props));
  492. cpu = machine->possible_cpus->cpus[i].cpu;
  493. if (cpu) {
  494. cpu_item->has_qom_path = true;
  495. cpu_item->qom_path = object_get_canonical_path(cpu);
  496. }
  497. list_item->value = cpu_item;
  498. list_item->next = head;
  499. head = list_item;
  500. }
  501. return head;
  502. }
  503. /**
  504. * machine_set_cpu_numa_node:
  505. * @machine: machine object to modify
  506. * @props: specifies which cpu objects to assign to
  507. * numa node specified by @props.node_id
  508. * @errp: if an error occurs, a pointer to an area to store the error
  509. *
  510. * Associate NUMA node specified by @props.node_id with cpu slots that
  511. * match socket/core/thread-ids specified by @props. It's recommended to use
  512. * query-hotpluggable-cpus.props values to specify affected cpu slots,
  513. * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
  514. *
  515. * However for CLI convenience it's possible to pass in subset of properties,
  516. * which would affect all cpu slots that match it.
  517. * Ex for pc machine:
  518. * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
  519. * -numa cpu,node-id=0,socket_id=0 \
  520. * -numa cpu,node-id=1,socket_id=1
  521. * will assign all child cores of socket 0 to node 0 and
  522. * of socket 1 to node 1.
  523. *
  524. * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
  525. * return error.
  526. * Empty subset is disallowed and function will return with error in this case.
  527. */
  528. void machine_set_cpu_numa_node(MachineState *machine,
  529. const CpuInstanceProperties *props, Error **errp)
  530. {
  531. MachineClass *mc = MACHINE_GET_CLASS(machine);
  532. bool match = false;
  533. int i;
  534. if (!mc->possible_cpu_arch_ids) {
  535. error_setg(errp, "mapping of CPUs to NUMA node is not supported");
  536. return;
  537. }
  538. /* disabling node mapping is not supported, forbid it */
  539. assert(props->has_node_id);
  540. /* force board to initialize possible_cpus if it hasn't been done yet */
  541. mc->possible_cpu_arch_ids(machine);
  542. for (i = 0; i < machine->possible_cpus->len; i++) {
  543. CPUArchId *slot = &machine->possible_cpus->cpus[i];
  544. /* reject unsupported by board properties */
  545. if (props->has_thread_id && !slot->props.has_thread_id) {
  546. error_setg(errp, "thread-id is not supported");
  547. return;
  548. }
  549. if (props->has_core_id && !slot->props.has_core_id) {
  550. error_setg(errp, "core-id is not supported");
  551. return;
  552. }
  553. if (props->has_socket_id && !slot->props.has_socket_id) {
  554. error_setg(errp, "socket-id is not supported");
  555. return;
  556. }
  557. if (props->has_die_id && !slot->props.has_die_id) {
  558. error_setg(errp, "die-id is not supported");
  559. return;
  560. }
  561. /* skip slots with explicit mismatch */
  562. if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
  563. continue;
  564. }
  565. if (props->has_core_id && props->core_id != slot->props.core_id) {
  566. continue;
  567. }
  568. if (props->has_die_id && props->die_id != slot->props.die_id) {
  569. continue;
  570. }
  571. if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
  572. continue;
  573. }
  574. /* reject assignment if slot is already assigned, for compatibility
  575. * of legacy cpu_index mapping with SPAPR core based mapping do not
  576. * error out if cpu thread and matched core have the same node-id */
  577. if (slot->props.has_node_id &&
  578. slot->props.node_id != props->node_id) {
  579. error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
  580. slot->props.node_id);
  581. return;
  582. }
  583. /* assign slot to node as it's matched '-numa cpu' key */
  584. match = true;
  585. slot->props.node_id = props->node_id;
  586. slot->props.has_node_id = props->has_node_id;
  587. }
  588. if (!match) {
  589. error_setg(errp, "no match found");
  590. }
  591. }
  592. static void smp_parse(MachineState *ms, QemuOpts *opts)
  593. {
  594. if (opts) {
  595. unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
  596. unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
  597. unsigned cores = qemu_opt_get_number(opts, "cores", 0);
  598. unsigned threads = qemu_opt_get_number(opts, "threads", 0);
  599. /* compute missing values, prefer sockets over cores over threads */
  600. if (cpus == 0 || sockets == 0) {
  601. cores = cores > 0 ? cores : 1;
  602. threads = threads > 0 ? threads : 1;
  603. if (cpus == 0) {
  604. sockets = sockets > 0 ? sockets : 1;
  605. cpus = cores * threads * sockets;
  606. } else {
  607. ms->smp.max_cpus =
  608. qemu_opt_get_number(opts, "maxcpus", cpus);
  609. sockets = ms->smp.max_cpus / (cores * threads);
  610. }
  611. } else if (cores == 0) {
  612. threads = threads > 0 ? threads : 1;
  613. cores = cpus / (sockets * threads);
  614. cores = cores > 0 ? cores : 1;
  615. } else if (threads == 0) {
  616. threads = cpus / (cores * sockets);
  617. threads = threads > 0 ? threads : 1;
  618. } else if (sockets * cores * threads < cpus) {
  619. error_report("cpu topology: "
  620. "sockets (%u) * cores (%u) * threads (%u) < "
  621. "smp_cpus (%u)",
  622. sockets, cores, threads, cpus);
  623. exit(1);
  624. }
  625. ms->smp.max_cpus =
  626. qemu_opt_get_number(opts, "maxcpus", cpus);
  627. if (ms->smp.max_cpus < cpus) {
  628. error_report("maxcpus must be equal to or greater than smp");
  629. exit(1);
  630. }
  631. if (sockets * cores * threads > ms->smp.max_cpus) {
  632. error_report("cpu topology: "
  633. "sockets (%u) * cores (%u) * threads (%u) > "
  634. "maxcpus (%u)",
  635. sockets, cores, threads,
  636. ms->smp.max_cpus);
  637. exit(1);
  638. }
  639. if (sockets * cores * threads != ms->smp.max_cpus) {
  640. warn_report("Invalid CPU topology deprecated: "
  641. "sockets (%u) * cores (%u) * threads (%u) "
  642. "!= maxcpus (%u)",
  643. sockets, cores, threads,
  644. ms->smp.max_cpus);
  645. }
  646. ms->smp.cpus = cpus;
  647. ms->smp.cores = cores;
  648. ms->smp.threads = threads;
  649. }
  650. if (ms->smp.cpus > 1) {
  651. Error *blocker = NULL;
  652. error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
  653. replay_add_blocker(blocker);
  654. }
  655. }
  656. static void machine_class_init(ObjectClass *oc, void *data)
  657. {
  658. MachineClass *mc = MACHINE_CLASS(oc);
  659. /* Default 128 MB as guest ram size */
  660. mc->default_ram_size = 128 * MiB;
  661. mc->rom_file_has_mr = true;
  662. mc->smp_parse = smp_parse;
  663. /* numa node memory size aligned on 8MB by default.
  664. * On Linux, each node's border has to be 8MB aligned
  665. */
  666. mc->numa_mem_align_shift = 23;
  667. mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
  668. object_class_property_add_str(oc, "accel",
  669. machine_get_accel, machine_set_accel, &error_abort);
  670. object_class_property_set_description(oc, "accel",
  671. "Accelerator list", &error_abort);
  672. object_class_property_add(oc, "kernel-irqchip", "on|off|split",
  673. NULL, machine_set_kernel_irqchip,
  674. NULL, NULL, &error_abort);
  675. object_class_property_set_description(oc, "kernel-irqchip",
  676. "Configure KVM in-kernel irqchip", &error_abort);
  677. object_class_property_add(oc, "kvm-shadow-mem", "int",
  678. machine_get_kvm_shadow_mem, machine_set_kvm_shadow_mem,
  679. NULL, NULL, &error_abort);
  680. object_class_property_set_description(oc, "kvm-shadow-mem",
  681. "KVM shadow MMU size", &error_abort);
  682. object_class_property_add_str(oc, "kernel",
  683. machine_get_kernel, machine_set_kernel, &error_abort);
  684. object_class_property_set_description(oc, "kernel",
  685. "Linux kernel image file", &error_abort);
  686. object_class_property_add_str(oc, "initrd",
  687. machine_get_initrd, machine_set_initrd, &error_abort);
  688. object_class_property_set_description(oc, "initrd",
  689. "Linux initial ramdisk file", &error_abort);
  690. object_class_property_add_str(oc, "append",
  691. machine_get_append, machine_set_append, &error_abort);
  692. object_class_property_set_description(oc, "append",
  693. "Linux kernel command line", &error_abort);
  694. object_class_property_add_str(oc, "dtb",
  695. machine_get_dtb, machine_set_dtb, &error_abort);
  696. object_class_property_set_description(oc, "dtb",
  697. "Linux kernel device tree file", &error_abort);
  698. object_class_property_add_str(oc, "dumpdtb",
  699. machine_get_dumpdtb, machine_set_dumpdtb, &error_abort);
  700. object_class_property_set_description(oc, "dumpdtb",
  701. "Dump current dtb to a file and quit", &error_abort);
  702. object_class_property_add(oc, "phandle-start", "int",
  703. machine_get_phandle_start, machine_set_phandle_start,
  704. NULL, NULL, &error_abort);
  705. object_class_property_set_description(oc, "phandle-start",
  706. "The first phandle ID we may generate dynamically", &error_abort);
  707. object_class_property_add_str(oc, "dt-compatible",
  708. machine_get_dt_compatible, machine_set_dt_compatible, &error_abort);
  709. object_class_property_set_description(oc, "dt-compatible",
  710. "Overrides the \"compatible\" property of the dt root node",
  711. &error_abort);
  712. object_class_property_add_bool(oc, "dump-guest-core",
  713. machine_get_dump_guest_core, machine_set_dump_guest_core, &error_abort);
  714. object_class_property_set_description(oc, "dump-guest-core",
  715. "Include guest memory in a core dump", &error_abort);
  716. object_class_property_add_bool(oc, "mem-merge",
  717. machine_get_mem_merge, machine_set_mem_merge, &error_abort);
  718. object_class_property_set_description(oc, "mem-merge",
  719. "Enable/disable memory merge support", &error_abort);
  720. object_class_property_add_bool(oc, "usb",
  721. machine_get_usb, machine_set_usb, &error_abort);
  722. object_class_property_set_description(oc, "usb",
  723. "Set on/off to enable/disable usb", &error_abort);
  724. object_class_property_add_bool(oc, "graphics",
  725. machine_get_graphics, machine_set_graphics, &error_abort);
  726. object_class_property_set_description(oc, "graphics",
  727. "Set on/off to enable/disable graphics emulation", &error_abort);
  728. object_class_property_add_bool(oc, "igd-passthru",
  729. machine_get_igd_gfx_passthru, machine_set_igd_gfx_passthru,
  730. &error_abort);
  731. object_class_property_set_description(oc, "igd-passthru",
  732. "Set on/off to enable/disable igd passthrou", &error_abort);
  733. object_class_property_add_str(oc, "firmware",
  734. machine_get_firmware, machine_set_firmware,
  735. &error_abort);
  736. object_class_property_set_description(oc, "firmware",
  737. "Firmware image", &error_abort);
  738. object_class_property_add_bool(oc, "suppress-vmdesc",
  739. machine_get_suppress_vmdesc, machine_set_suppress_vmdesc,
  740. &error_abort);
  741. object_class_property_set_description(oc, "suppress-vmdesc",
  742. "Set on to disable self-describing migration", &error_abort);
  743. object_class_property_add_bool(oc, "enforce-config-section",
  744. machine_get_enforce_config_section, machine_set_enforce_config_section,
  745. &error_abort);
  746. object_class_property_set_description(oc, "enforce-config-section",
  747. "Set on to enforce configuration section migration", &error_abort);
  748. object_class_property_add_str(oc, "memory-encryption",
  749. machine_get_memory_encryption, machine_set_memory_encryption,
  750. &error_abort);
  751. object_class_property_set_description(oc, "memory-encryption",
  752. "Set memory encryption object to use", &error_abort);
  753. }
  754. static void machine_class_base_init(ObjectClass *oc, void *data)
  755. {
  756. if (!object_class_is_abstract(oc)) {
  757. MachineClass *mc = MACHINE_CLASS(oc);
  758. const char *cname = object_class_get_name(oc);
  759. assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
  760. mc->name = g_strndup(cname,
  761. strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
  762. mc->compat_props = g_ptr_array_new();
  763. }
  764. }
  765. static void machine_initfn(Object *obj)
  766. {
  767. MachineState *ms = MACHINE(obj);
  768. MachineClass *mc = MACHINE_GET_CLASS(obj);
  769. ms->kernel_irqchip_allowed = true;
  770. ms->kernel_irqchip_split = mc->default_kernel_irqchip_split;
  771. ms->kvm_shadow_mem = -1;
  772. ms->dump_guest_core = true;
  773. ms->mem_merge = true;
  774. ms->enable_graphics = true;
  775. if (mc->nvdimm_supported) {
  776. Object *obj = OBJECT(ms);
  777. ms->nvdimms_state = g_new0(NVDIMMState, 1);
  778. object_property_add_bool(obj, "nvdimm",
  779. machine_get_nvdimm, machine_set_nvdimm,
  780. &error_abort);
  781. object_property_set_description(obj, "nvdimm",
  782. "Set on/off to enable/disable "
  783. "NVDIMM instantiation", NULL);
  784. object_property_add_str(obj, "nvdimm-persistence",
  785. machine_get_nvdimm_persistence,
  786. machine_set_nvdimm_persistence,
  787. &error_abort);
  788. object_property_set_description(obj, "nvdimm-persistence",
  789. "Set NVDIMM persistence"
  790. "Valid values are cpu, mem-ctrl",
  791. NULL);
  792. }
  793. if (mc->numa_mem_supported) {
  794. ms->numa_state = g_new0(NumaState, 1);
  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. }
  800. static void machine_finalize(Object *obj)
  801. {
  802. MachineState *ms = MACHINE(obj);
  803. g_free(ms->accel);
  804. g_free(ms->kernel_filename);
  805. g_free(ms->initrd_filename);
  806. g_free(ms->kernel_cmdline);
  807. g_free(ms->dtb);
  808. g_free(ms->dumpdtb);
  809. g_free(ms->dt_compatible);
  810. g_free(ms->firmware);
  811. g_free(ms->device_memory);
  812. g_free(ms->nvdimms_state);
  813. g_free(ms->numa_state);
  814. }
  815. bool machine_usb(MachineState *machine)
  816. {
  817. return machine->usb;
  818. }
  819. bool machine_kernel_irqchip_allowed(MachineState *machine)
  820. {
  821. return machine->kernel_irqchip_allowed;
  822. }
  823. bool machine_kernel_irqchip_required(MachineState *machine)
  824. {
  825. return machine->kernel_irqchip_required;
  826. }
  827. bool machine_kernel_irqchip_split(MachineState *machine)
  828. {
  829. return machine->kernel_irqchip_split;
  830. }
  831. int machine_kvm_shadow_mem(MachineState *machine)
  832. {
  833. return machine->kvm_shadow_mem;
  834. }
  835. int machine_phandle_start(MachineState *machine)
  836. {
  837. return machine->phandle_start;
  838. }
  839. bool machine_dump_guest_core(MachineState *machine)
  840. {
  841. return machine->dump_guest_core;
  842. }
  843. bool machine_mem_merge(MachineState *machine)
  844. {
  845. return machine->mem_merge;
  846. }
  847. static char *cpu_slot_to_string(const CPUArchId *cpu)
  848. {
  849. GString *s = g_string_new(NULL);
  850. if (cpu->props.has_socket_id) {
  851. g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
  852. }
  853. if (cpu->props.has_die_id) {
  854. g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
  855. }
  856. if (cpu->props.has_core_id) {
  857. if (s->len) {
  858. g_string_append_printf(s, ", ");
  859. }
  860. g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
  861. }
  862. if (cpu->props.has_thread_id) {
  863. if (s->len) {
  864. g_string_append_printf(s, ", ");
  865. }
  866. g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
  867. }
  868. return g_string_free(s, false);
  869. }
  870. static void machine_numa_finish_cpu_init(MachineState *machine)
  871. {
  872. int i;
  873. bool default_mapping;
  874. GString *s = g_string_new(NULL);
  875. MachineClass *mc = MACHINE_GET_CLASS(machine);
  876. const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
  877. assert(machine->numa_state->num_nodes);
  878. for (i = 0; i < possible_cpus->len; i++) {
  879. if (possible_cpus->cpus[i].props.has_node_id) {
  880. break;
  881. }
  882. }
  883. default_mapping = (i == possible_cpus->len);
  884. for (i = 0; i < possible_cpus->len; i++) {
  885. const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
  886. if (!cpu_slot->props.has_node_id) {
  887. /* fetch default mapping from board and enable it */
  888. CpuInstanceProperties props = cpu_slot->props;
  889. props.node_id = mc->get_default_cpu_node_id(machine, i);
  890. if (!default_mapping) {
  891. /* record slots with not set mapping,
  892. * TODO: make it hard error in future */
  893. char *cpu_str = cpu_slot_to_string(cpu_slot);
  894. g_string_append_printf(s, "%sCPU %d [%s]",
  895. s->len ? ", " : "", i, cpu_str);
  896. g_free(cpu_str);
  897. /* non mapped cpus used to fallback to node 0 */
  898. props.node_id = 0;
  899. }
  900. props.has_node_id = true;
  901. machine_set_cpu_numa_node(machine, &props, &error_fatal);
  902. }
  903. }
  904. if (s->len && !qtest_enabled()) {
  905. warn_report("CPU(s) not present in any NUMA nodes: %s",
  906. s->str);
  907. warn_report("All CPU(s) up to maxcpus should be described "
  908. "in NUMA config, ability to start up with partial NUMA "
  909. "mappings is obsoleted and will be removed in future");
  910. }
  911. g_string_free(s, true);
  912. }
  913. void machine_run_board_init(MachineState *machine)
  914. {
  915. MachineClass *machine_class = MACHINE_GET_CLASS(machine);
  916. if (machine_class->numa_mem_supported) {
  917. numa_complete_configuration(machine);
  918. if (machine->numa_state->num_nodes) {
  919. machine_numa_finish_cpu_init(machine);
  920. }
  921. }
  922. /* If the machine supports the valid_cpu_types check and the user
  923. * specified a CPU with -cpu check here that the user CPU is supported.
  924. */
  925. if (machine_class->valid_cpu_types && machine->cpu_type) {
  926. ObjectClass *class = object_class_by_name(machine->cpu_type);
  927. int i;
  928. for (i = 0; machine_class->valid_cpu_types[i]; i++) {
  929. if (object_class_dynamic_cast(class,
  930. machine_class->valid_cpu_types[i])) {
  931. /* The user specificed CPU is in the valid field, we are
  932. * good to go.
  933. */
  934. break;
  935. }
  936. }
  937. if (!machine_class->valid_cpu_types[i]) {
  938. /* The user specified CPU is not valid */
  939. error_report("Invalid CPU type: %s", machine->cpu_type);
  940. error_printf("The valid types are: %s",
  941. machine_class->valid_cpu_types[0]);
  942. for (i = 1; machine_class->valid_cpu_types[i]; i++) {
  943. error_printf(", %s", machine_class->valid_cpu_types[i]);
  944. }
  945. error_printf("\n");
  946. exit(1);
  947. }
  948. }
  949. machine_class->init(machine);
  950. }
  951. static const TypeInfo machine_info = {
  952. .name = TYPE_MACHINE,
  953. .parent = TYPE_OBJECT,
  954. .abstract = true,
  955. .class_size = sizeof(MachineClass),
  956. .class_init = machine_class_init,
  957. .class_base_init = machine_class_base_init,
  958. .instance_size = sizeof(MachineState),
  959. .instance_init = machine_initfn,
  960. .instance_finalize = machine_finalize,
  961. };
  962. static void machine_register_types(void)
  963. {
  964. type_register_static(&machine_info);
  965. }
  966. type_init(machine_register_types)