]> git.proxmox.com Git - mirror_qemu.git/blob - hw/core/machine.c
9e8c06036faf9c14f203a5f31e4bd42e32523c1d
[mirror_qemu.git] / hw / core / machine.c
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", "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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
338 static bool machine_get_usb(Object *obj, Error **errp)
339 {
340 MachineState *ms = MACHINE(obj);
341
342 return ms->usb;
343 }
344
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
353 static bool machine_get_graphics(Object *obj, Error **errp)
354 {
355 MachineState *ms = MACHINE(obj);
356
357 return ms->enable_graphics;
358 }
359
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
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
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
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
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
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
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
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
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 static bool machine_get_nvdimm(Object *obj, Error **errp)
431 {
432 MachineState *ms = MACHINE(obj);
433
434 return ms->nvdimms_state->is_enabled;
435 }
436
437 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
438 {
439 MachineState *ms = MACHINE(obj);
440
441 ms->nvdimms_state->is_enabled = value;
442 }
443
444 static bool machine_get_hmat(Object *obj, Error **errp)
445 {
446 MachineState *ms = MACHINE(obj);
447
448 return ms->numa_state->hmat_enabled;
449 }
450
451 static void machine_set_hmat(Object *obj, bool value, Error **errp)
452 {
453 MachineState *ms = MACHINE(obj);
454
455 ms->numa_state->hmat_enabled = value;
456 }
457
458 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
459 {
460 MachineState *ms = MACHINE(obj);
461
462 return g_strdup(ms->nvdimms_state->persistence_string);
463 }
464
465 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
466 Error **errp)
467 {
468 MachineState *ms = MACHINE(obj);
469 NVDIMMState *nvdimms_state = ms->nvdimms_state;
470
471 if (strcmp(value, "cpu") == 0) {
472 nvdimms_state->persistence = 3;
473 } else if (strcmp(value, "mem-ctrl") == 0) {
474 nvdimms_state->persistence = 2;
475 } else {
476 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
477 value);
478 return;
479 }
480
481 g_free(nvdimms_state->persistence_string);
482 nvdimms_state->persistence_string = g_strdup(value);
483 }
484
485 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
486 {
487 strList *item = g_new0(strList, 1);
488
489 item->value = g_strdup(type);
490 item->next = mc->allowed_dynamic_sysbus_devices;
491 mc->allowed_dynamic_sysbus_devices = item;
492 }
493
494 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
495 {
496 MachineState *machine = opaque;
497 MachineClass *mc = MACHINE_GET_CLASS(machine);
498 bool allowed = false;
499 strList *wl;
500
501 for (wl = mc->allowed_dynamic_sysbus_devices;
502 !allowed && wl;
503 wl = wl->next) {
504 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
505 }
506
507 if (!allowed) {
508 error_report("Option '-device %s' cannot be handled by this machine",
509 object_class_get_name(object_get_class(OBJECT(sbdev))));
510 exit(1);
511 }
512 }
513
514 static char *machine_get_memdev(Object *obj, Error **errp)
515 {
516 MachineState *ms = MACHINE(obj);
517
518 return g_strdup(ms->ram_memdev_id);
519 }
520
521 static void machine_set_memdev(Object *obj, const char *value, Error **errp)
522 {
523 MachineState *ms = MACHINE(obj);
524
525 g_free(ms->ram_memdev_id);
526 ms->ram_memdev_id = g_strdup(value);
527 }
528
529
530 static void machine_init_notify(Notifier *notifier, void *data)
531 {
532 MachineState *machine = MACHINE(qdev_get_machine());
533
534 /*
535 * Loop through all dynamically created sysbus devices and check if they are
536 * all allowed. If a device is not allowed, error out.
537 */
538 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
539 }
540
541 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
542 {
543 int i;
544 HotpluggableCPUList *head = NULL;
545 MachineClass *mc = MACHINE_GET_CLASS(machine);
546
547 /* force board to initialize possible_cpus if it hasn't been done yet */
548 mc->possible_cpu_arch_ids(machine);
549
550 for (i = 0; i < machine->possible_cpus->len; i++) {
551 Object *cpu;
552 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
553 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
554
555 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
556 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
557 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
558 sizeof(*cpu_item->props));
559
560 cpu = machine->possible_cpus->cpus[i].cpu;
561 if (cpu) {
562 cpu_item->has_qom_path = true;
563 cpu_item->qom_path = object_get_canonical_path(cpu);
564 }
565 list_item->value = cpu_item;
566 list_item->next = head;
567 head = list_item;
568 }
569 return head;
570 }
571
572 /**
573 * machine_set_cpu_numa_node:
574 * @machine: machine object to modify
575 * @props: specifies which cpu objects to assign to
576 * numa node specified by @props.node_id
577 * @errp: if an error occurs, a pointer to an area to store the error
578 *
579 * Associate NUMA node specified by @props.node_id with cpu slots that
580 * match socket/core/thread-ids specified by @props. It's recommended to use
581 * query-hotpluggable-cpus.props values to specify affected cpu slots,
582 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
583 *
584 * However for CLI convenience it's possible to pass in subset of properties,
585 * which would affect all cpu slots that match it.
586 * Ex for pc machine:
587 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
588 * -numa cpu,node-id=0,socket_id=0 \
589 * -numa cpu,node-id=1,socket_id=1
590 * will assign all child cores of socket 0 to node 0 and
591 * of socket 1 to node 1.
592 *
593 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
594 * return error.
595 * Empty subset is disallowed and function will return with error in this case.
596 */
597 void machine_set_cpu_numa_node(MachineState *machine,
598 const CpuInstanceProperties *props, Error **errp)
599 {
600 MachineClass *mc = MACHINE_GET_CLASS(machine);
601 NodeInfo *numa_info = machine->numa_state->nodes;
602 bool match = false;
603 int i;
604
605 if (!mc->possible_cpu_arch_ids) {
606 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
607 return;
608 }
609
610 /* disabling node mapping is not supported, forbid it */
611 assert(props->has_node_id);
612
613 /* force board to initialize possible_cpus if it hasn't been done yet */
614 mc->possible_cpu_arch_ids(machine);
615
616 for (i = 0; i < machine->possible_cpus->len; i++) {
617 CPUArchId *slot = &machine->possible_cpus->cpus[i];
618
619 /* reject unsupported by board properties */
620 if (props->has_thread_id && !slot->props.has_thread_id) {
621 error_setg(errp, "thread-id is not supported");
622 return;
623 }
624
625 if (props->has_core_id && !slot->props.has_core_id) {
626 error_setg(errp, "core-id is not supported");
627 return;
628 }
629
630 if (props->has_socket_id && !slot->props.has_socket_id) {
631 error_setg(errp, "socket-id is not supported");
632 return;
633 }
634
635 if (props->has_die_id && !slot->props.has_die_id) {
636 error_setg(errp, "die-id is not supported");
637 return;
638 }
639
640 /* skip slots with explicit mismatch */
641 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
642 continue;
643 }
644
645 if (props->has_core_id && props->core_id != slot->props.core_id) {
646 continue;
647 }
648
649 if (props->has_die_id && props->die_id != slot->props.die_id) {
650 continue;
651 }
652
653 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
654 continue;
655 }
656
657 /* reject assignment if slot is already assigned, for compatibility
658 * of legacy cpu_index mapping with SPAPR core based mapping do not
659 * error out if cpu thread and matched core have the same node-id */
660 if (slot->props.has_node_id &&
661 slot->props.node_id != props->node_id) {
662 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
663 slot->props.node_id);
664 return;
665 }
666
667 /* assign slot to node as it's matched '-numa cpu' key */
668 match = true;
669 slot->props.node_id = props->node_id;
670 slot->props.has_node_id = props->has_node_id;
671
672 if (machine->numa_state->hmat_enabled) {
673 if ((numa_info[props->node_id].initiator < MAX_NODES) &&
674 (props->node_id != numa_info[props->node_id].initiator)) {
675 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
676 " should be itself", props->node_id);
677 return;
678 }
679 numa_info[props->node_id].has_cpu = true;
680 numa_info[props->node_id].initiator = props->node_id;
681 }
682 }
683
684 if (!match) {
685 error_setg(errp, "no match found");
686 }
687 }
688
689 static void smp_parse(MachineState *ms, QemuOpts *opts)
690 {
691 if (opts) {
692 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
693 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
694 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
695 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
696
697 /* compute missing values, prefer sockets over cores over threads */
698 if (cpus == 0 || sockets == 0) {
699 cores = cores > 0 ? cores : 1;
700 threads = threads > 0 ? threads : 1;
701 if (cpus == 0) {
702 sockets = sockets > 0 ? sockets : 1;
703 cpus = cores * threads * sockets;
704 } else {
705 ms->smp.max_cpus =
706 qemu_opt_get_number(opts, "maxcpus", cpus);
707 sockets = ms->smp.max_cpus / (cores * threads);
708 }
709 } else if (cores == 0) {
710 threads = threads > 0 ? threads : 1;
711 cores = cpus / (sockets * threads);
712 cores = cores > 0 ? cores : 1;
713 } else if (threads == 0) {
714 threads = cpus / (cores * sockets);
715 threads = threads > 0 ? threads : 1;
716 } else if (sockets * cores * threads < cpus) {
717 error_report("cpu topology: "
718 "sockets (%u) * cores (%u) * threads (%u) < "
719 "smp_cpus (%u)",
720 sockets, cores, threads, cpus);
721 exit(1);
722 }
723
724 ms->smp.max_cpus =
725 qemu_opt_get_number(opts, "maxcpus", cpus);
726
727 if (ms->smp.max_cpus < cpus) {
728 error_report("maxcpus must be equal to or greater than smp");
729 exit(1);
730 }
731
732 if (sockets * cores * threads > ms->smp.max_cpus) {
733 error_report("cpu topology: "
734 "sockets (%u) * cores (%u) * threads (%u) > "
735 "maxcpus (%u)",
736 sockets, cores, threads,
737 ms->smp.max_cpus);
738 exit(1);
739 }
740
741 if (sockets * cores * threads != ms->smp.max_cpus) {
742 warn_report("Invalid CPU topology deprecated: "
743 "sockets (%u) * cores (%u) * threads (%u) "
744 "!= maxcpus (%u)",
745 sockets, cores, threads,
746 ms->smp.max_cpus);
747 }
748
749 ms->smp.cpus = cpus;
750 ms->smp.cores = cores;
751 ms->smp.threads = threads;
752 }
753
754 if (ms->smp.cpus > 1) {
755 Error *blocker = NULL;
756 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
757 replay_add_blocker(blocker);
758 }
759 }
760
761 static void machine_class_init(ObjectClass *oc, void *data)
762 {
763 MachineClass *mc = MACHINE_CLASS(oc);
764
765 /* Default 128 MB as guest ram size */
766 mc->default_ram_size = 128 * MiB;
767 mc->rom_file_has_mr = true;
768 mc->smp_parse = smp_parse;
769
770 /* numa node memory size aligned on 8MB by default.
771 * On Linux, each node's border has to be 8MB aligned
772 */
773 mc->numa_mem_align_shift = 23;
774 mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
775
776 object_class_property_add_str(oc, "kernel",
777 machine_get_kernel, machine_set_kernel, &error_abort);
778 object_class_property_set_description(oc, "kernel",
779 "Linux kernel image file", &error_abort);
780
781 object_class_property_add_str(oc, "initrd",
782 machine_get_initrd, machine_set_initrd, &error_abort);
783 object_class_property_set_description(oc, "initrd",
784 "Linux initial ramdisk file", &error_abort);
785
786 object_class_property_add_str(oc, "append",
787 machine_get_append, machine_set_append, &error_abort);
788 object_class_property_set_description(oc, "append",
789 "Linux kernel command line", &error_abort);
790
791 object_class_property_add_str(oc, "dtb",
792 machine_get_dtb, machine_set_dtb, &error_abort);
793 object_class_property_set_description(oc, "dtb",
794 "Linux kernel device tree file", &error_abort);
795
796 object_class_property_add_str(oc, "dumpdtb",
797 machine_get_dumpdtb, machine_set_dumpdtb, &error_abort);
798 object_class_property_set_description(oc, "dumpdtb",
799 "Dump current dtb to a file and quit", &error_abort);
800
801 object_class_property_add(oc, "phandle-start", "int",
802 machine_get_phandle_start, machine_set_phandle_start,
803 NULL, NULL, &error_abort);
804 object_class_property_set_description(oc, "phandle-start",
805 "The first phandle ID we may generate dynamically", &error_abort);
806
807 object_class_property_add_str(oc, "dt-compatible",
808 machine_get_dt_compatible, machine_set_dt_compatible, &error_abort);
809 object_class_property_set_description(oc, "dt-compatible",
810 "Overrides the \"compatible\" property of the dt root node",
811 &error_abort);
812
813 object_class_property_add_bool(oc, "dump-guest-core",
814 machine_get_dump_guest_core, machine_set_dump_guest_core, &error_abort);
815 object_class_property_set_description(oc, "dump-guest-core",
816 "Include guest memory in a core dump", &error_abort);
817
818 object_class_property_add_bool(oc, "mem-merge",
819 machine_get_mem_merge, machine_set_mem_merge, &error_abort);
820 object_class_property_set_description(oc, "mem-merge",
821 "Enable/disable memory merge support", &error_abort);
822
823 object_class_property_add_bool(oc, "usb",
824 machine_get_usb, machine_set_usb, &error_abort);
825 object_class_property_set_description(oc, "usb",
826 "Set on/off to enable/disable usb", &error_abort);
827
828 object_class_property_add_bool(oc, "graphics",
829 machine_get_graphics, machine_set_graphics, &error_abort);
830 object_class_property_set_description(oc, "graphics",
831 "Set on/off to enable/disable graphics emulation", &error_abort);
832
833 object_class_property_add_str(oc, "firmware",
834 machine_get_firmware, machine_set_firmware,
835 &error_abort);
836 object_class_property_set_description(oc, "firmware",
837 "Firmware image", &error_abort);
838
839 object_class_property_add_bool(oc, "suppress-vmdesc",
840 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc,
841 &error_abort);
842 object_class_property_set_description(oc, "suppress-vmdesc",
843 "Set on to disable self-describing migration", &error_abort);
844
845 object_class_property_add_bool(oc, "enforce-config-section",
846 machine_get_enforce_config_section, machine_set_enforce_config_section,
847 &error_abort);
848 object_class_property_set_description(oc, "enforce-config-section",
849 "Set on to enforce configuration section migration", &error_abort);
850
851 object_class_property_add_str(oc, "memory-encryption",
852 machine_get_memory_encryption, machine_set_memory_encryption,
853 &error_abort);
854 object_class_property_set_description(oc, "memory-encryption",
855 "Set memory encryption object to use", &error_abort);
856 }
857
858 static void machine_class_base_init(ObjectClass *oc, void *data)
859 {
860 if (!object_class_is_abstract(oc)) {
861 MachineClass *mc = MACHINE_CLASS(oc);
862 const char *cname = object_class_get_name(oc);
863 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
864 mc->name = g_strndup(cname,
865 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
866 mc->compat_props = g_ptr_array_new();
867 }
868 }
869
870 static void machine_initfn(Object *obj)
871 {
872 MachineState *ms = MACHINE(obj);
873 MachineClass *mc = MACHINE_GET_CLASS(obj);
874
875 ms->dump_guest_core = true;
876 ms->mem_merge = true;
877 ms->enable_graphics = true;
878
879 if (mc->nvdimm_supported) {
880 Object *obj = OBJECT(ms);
881
882 ms->nvdimms_state = g_new0(NVDIMMState, 1);
883 object_property_add_bool(obj, "nvdimm",
884 machine_get_nvdimm, machine_set_nvdimm,
885 &error_abort);
886 object_property_set_description(obj, "nvdimm",
887 "Set on/off to enable/disable "
888 "NVDIMM instantiation", NULL);
889
890 object_property_add_str(obj, "nvdimm-persistence",
891 machine_get_nvdimm_persistence,
892 machine_set_nvdimm_persistence,
893 &error_abort);
894 object_property_set_description(obj, "nvdimm-persistence",
895 "Set NVDIMM persistence"
896 "Valid values are cpu, mem-ctrl",
897 NULL);
898 }
899
900 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
901 ms->numa_state = g_new0(NumaState, 1);
902 object_property_add_bool(obj, "hmat",
903 machine_get_hmat, machine_set_hmat,
904 &error_abort);
905 object_property_set_description(obj, "hmat",
906 "Set on/off to enable/disable "
907 "ACPI Heterogeneous Memory Attribute "
908 "Table (HMAT)", NULL);
909 }
910
911 object_property_add_str(obj, "memory-backend",
912 machine_get_memdev, machine_set_memdev,
913 &error_abort);
914 object_property_set_description(obj, "memory-backend",
915 "Set RAM backend"
916 "Valid value is ID of hostmem based backend",
917 &error_abort);
918
919 /* Register notifier when init is done for sysbus sanity checks */
920 ms->sysbus_notifier.notify = machine_init_notify;
921 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
922 }
923
924 static void machine_finalize(Object *obj)
925 {
926 MachineState *ms = MACHINE(obj);
927
928 g_free(ms->kernel_filename);
929 g_free(ms->initrd_filename);
930 g_free(ms->kernel_cmdline);
931 g_free(ms->dtb);
932 g_free(ms->dumpdtb);
933 g_free(ms->dt_compatible);
934 g_free(ms->firmware);
935 g_free(ms->device_memory);
936 g_free(ms->nvdimms_state);
937 g_free(ms->numa_state);
938 }
939
940 bool machine_usb(MachineState *machine)
941 {
942 return machine->usb;
943 }
944
945 int machine_phandle_start(MachineState *machine)
946 {
947 return machine->phandle_start;
948 }
949
950 bool machine_dump_guest_core(MachineState *machine)
951 {
952 return machine->dump_guest_core;
953 }
954
955 bool machine_mem_merge(MachineState *machine)
956 {
957 return machine->mem_merge;
958 }
959
960 static char *cpu_slot_to_string(const CPUArchId *cpu)
961 {
962 GString *s = g_string_new(NULL);
963 if (cpu->props.has_socket_id) {
964 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
965 }
966 if (cpu->props.has_die_id) {
967 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
968 }
969 if (cpu->props.has_core_id) {
970 if (s->len) {
971 g_string_append_printf(s, ", ");
972 }
973 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
974 }
975 if (cpu->props.has_thread_id) {
976 if (s->len) {
977 g_string_append_printf(s, ", ");
978 }
979 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
980 }
981 return g_string_free(s, false);
982 }
983
984 static void numa_validate_initiator(NumaState *numa_state)
985 {
986 int i;
987 NodeInfo *numa_info = numa_state->nodes;
988
989 for (i = 0; i < numa_state->num_nodes; i++) {
990 if (numa_info[i].initiator == MAX_NODES) {
991 error_report("The initiator of NUMA node %d is missing, use "
992 "'-numa node,initiator' option to declare it", i);
993 exit(1);
994 }
995
996 if (!numa_info[numa_info[i].initiator].present) {
997 error_report("NUMA node %" PRIu16 " is missing, use "
998 "'-numa node' option to declare it first",
999 numa_info[i].initiator);
1000 exit(1);
1001 }
1002
1003 if (!numa_info[numa_info[i].initiator].has_cpu) {
1004 error_report("The initiator of NUMA node %d is invalid", i);
1005 exit(1);
1006 }
1007 }
1008 }
1009
1010 static void machine_numa_finish_cpu_init(MachineState *machine)
1011 {
1012 int i;
1013 bool default_mapping;
1014 GString *s = g_string_new(NULL);
1015 MachineClass *mc = MACHINE_GET_CLASS(machine);
1016 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1017
1018 assert(machine->numa_state->num_nodes);
1019 for (i = 0; i < possible_cpus->len; i++) {
1020 if (possible_cpus->cpus[i].props.has_node_id) {
1021 break;
1022 }
1023 }
1024 default_mapping = (i == possible_cpus->len);
1025
1026 for (i = 0; i < possible_cpus->len; i++) {
1027 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1028
1029 if (!cpu_slot->props.has_node_id) {
1030 /* fetch default mapping from board and enable it */
1031 CpuInstanceProperties props = cpu_slot->props;
1032
1033 props.node_id = mc->get_default_cpu_node_id(machine, i);
1034 if (!default_mapping) {
1035 /* record slots with not set mapping,
1036 * TODO: make it hard error in future */
1037 char *cpu_str = cpu_slot_to_string(cpu_slot);
1038 g_string_append_printf(s, "%sCPU %d [%s]",
1039 s->len ? ", " : "", i, cpu_str);
1040 g_free(cpu_str);
1041
1042 /* non mapped cpus used to fallback to node 0 */
1043 props.node_id = 0;
1044 }
1045
1046 props.has_node_id = true;
1047 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1048 }
1049 }
1050
1051 if (machine->numa_state->hmat_enabled) {
1052 numa_validate_initiator(machine->numa_state);
1053 }
1054
1055 if (s->len && !qtest_enabled()) {
1056 warn_report("CPU(s) not present in any NUMA nodes: %s",
1057 s->str);
1058 warn_report("All CPU(s) up to maxcpus should be described "
1059 "in NUMA config, ability to start up with partial NUMA "
1060 "mappings is obsoleted and will be removed in future");
1061 }
1062 g_string_free(s, true);
1063 }
1064
1065 MemoryRegion *machine_consume_memdev(MachineState *machine,
1066 HostMemoryBackend *backend)
1067 {
1068 MemoryRegion *ret = host_memory_backend_get_memory(backend);
1069
1070 if (memory_region_is_mapped(ret)) {
1071 char *path = object_get_canonical_path_component(OBJECT(backend));
1072 error_report("memory backend %s can't be used multiple times.", path);
1073 g_free(path);
1074 exit(EXIT_FAILURE);
1075 }
1076 host_memory_backend_set_mapped(backend, true);
1077 vmstate_register_ram_global(ret);
1078 return ret;
1079 }
1080
1081 void machine_run_board_init(MachineState *machine)
1082 {
1083 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1084
1085 if (machine->ram_memdev_id) {
1086 Object *o;
1087 o = object_resolve_path_type(machine->ram_memdev_id,
1088 TYPE_MEMORY_BACKEND, NULL);
1089 machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o));
1090 }
1091
1092 if (machine->numa_state) {
1093 numa_complete_configuration(machine);
1094 if (machine->numa_state->num_nodes) {
1095 machine_numa_finish_cpu_init(machine);
1096 }
1097 }
1098
1099 /* If the machine supports the valid_cpu_types check and the user
1100 * specified a CPU with -cpu check here that the user CPU is supported.
1101 */
1102 if (machine_class->valid_cpu_types && machine->cpu_type) {
1103 ObjectClass *class = object_class_by_name(machine->cpu_type);
1104 int i;
1105
1106 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1107 if (object_class_dynamic_cast(class,
1108 machine_class->valid_cpu_types[i])) {
1109 /* The user specificed CPU is in the valid field, we are
1110 * good to go.
1111 */
1112 break;
1113 }
1114 }
1115
1116 if (!machine_class->valid_cpu_types[i]) {
1117 /* The user specified CPU is not valid */
1118 error_report("Invalid CPU type: %s", machine->cpu_type);
1119 error_printf("The valid types are: %s",
1120 machine_class->valid_cpu_types[0]);
1121 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1122 error_printf(", %s", machine_class->valid_cpu_types[i]);
1123 }
1124 error_printf("\n");
1125
1126 exit(1);
1127 }
1128 }
1129
1130 machine_class->init(machine);
1131 }
1132
1133 static const TypeInfo machine_info = {
1134 .name = TYPE_MACHINE,
1135 .parent = TYPE_OBJECT,
1136 .abstract = true,
1137 .class_size = sizeof(MachineClass),
1138 .class_init = machine_class_init,
1139 .class_base_init = machine_class_base_init,
1140 .instance_size = sizeof(MachineState),
1141 .instance_init = machine_initfn,
1142 .instance_finalize = machine_finalize,
1143 };
1144
1145 static void machine_register_types(void)
1146 {
1147 type_register_static(&machine_info);
1148 }
1149
1150 type_init(machine_register_types)