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