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