2 * QEMU PC System Emulator
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
28 #include "hw/i386/pc.h"
29 #include "hw/char/serial.h"
30 #include "hw/char/parallel.h"
31 #include "hw/i386/apic.h"
32 #include "hw/i386/topology.h"
33 #include "sysemu/cpus.h"
34 #include "hw/block/fdc.h"
36 #include "hw/pci/pci.h"
37 #include "hw/pci/pci_bus.h"
38 #include "hw/nvram/fw_cfg.h"
39 #include "hw/timer/hpet.h"
40 #include "hw/firmware/smbios.h"
41 #include "hw/loader.h"
43 #include "multiboot.h"
44 #include "hw/timer/mc146818rtc.h"
45 #include "hw/dma/i8257.h"
46 #include "hw/timer/i8254.h"
47 #include "hw/input/i8042.h"
48 #include "hw/audio/pcspk.h"
49 #include "hw/pci/msi.h"
50 #include "hw/sysbus.h"
51 #include "sysemu/sysemu.h"
52 #include "sysemu/numa.h"
53 #include "sysemu/kvm.h"
54 #include "sysemu/qtest.h"
56 #include "hw/xen/xen.h"
57 #include "ui/qemu-spice.h"
58 #include "exec/memory.h"
59 #include "exec/address-spaces.h"
60 #include "sysemu/arch_init.h"
61 #include "qemu/bitmap.h"
62 #include "qemu/config-file.h"
63 #include "qemu/error-report.h"
64 #include "qemu/option.h"
65 #include "hw/acpi/acpi.h"
66 #include "hw/acpi/cpu_hotplug.h"
67 #include "hw/boards.h"
68 #include "acpi-build.h"
69 #include "hw/mem/pc-dimm.h"
70 #include "qapi/error.h"
71 #include "qapi/qapi-visit-common.h"
72 #include "qapi/visitor.h"
75 #include "hw/i386/intel_iommu.h"
76 #include "hw/net/ne2000-isa.h"
78 /* debug PC/ISA interrupts */
82 #define DPRINTF(fmt, ...) \
83 do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
85 #define DPRINTF(fmt, ...)
88 #define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
89 #define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
90 #define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
91 #define FW_CFG_E820_TABLE (FW_CFG_ARCH_LOCAL + 3)
92 #define FW_CFG_HPET (FW_CFG_ARCH_LOCAL + 4)
94 #define E820_NR_ENTRIES 16
100 } QEMU_PACKED __attribute((__aligned__(4)));
104 struct e820_entry entry[E820_NR_ENTRIES];
105 } QEMU_PACKED __attribute((__aligned__(4)));
107 static struct e820_table e820_reserve;
108 static struct e820_entry *e820_table;
109 static unsigned e820_entries;
110 struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
112 GlobalProperty pc_compat_3_1[] = {
114 .driver = "intel-iommu",
115 .property = "dma-drain",
119 const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1);
121 GlobalProperty pc_compat_3_0[] = {
123 .driver = TYPE_X86_CPU,
124 .property = "x-hv-synic-kvm-only",
127 .driver = "Skylake-Server" "-" TYPE_X86_CPU,
131 .driver = "Skylake-Server-IBRS" "-" TYPE_X86_CPU,
136 const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0);
138 GlobalProperty pc_compat_2_12[] = {
140 .driver = TYPE_X86_CPU,
141 .property = "legacy-cache",
144 .driver = TYPE_X86_CPU,
145 .property = "topoext",
148 .driver = "EPYC-" TYPE_X86_CPU,
149 .property = "xlevel",
150 .value = stringify(0x8000000a),
152 .driver = "EPYC-IBPB-" TYPE_X86_CPU,
153 .property = "xlevel",
154 .value = stringify(0x8000000a),
157 const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12);
159 GlobalProperty pc_compat_2_11[] = {
161 .driver = TYPE_X86_CPU,
162 .property = "x-migrate-smi-count",
165 .driver = "Skylake-Server" "-" TYPE_X86_CPU,
166 .property = "clflushopt",
170 const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11);
172 GlobalProperty pc_compat_2_10[] = {
174 .driver = TYPE_X86_CPU,
175 .property = "x-hv-max-vps",
178 .driver = "i440FX-pcihost",
179 .property = "x-pci-hole64-fix",
182 .driver = "q35-pcihost",
183 .property = "x-pci-hole64-fix",
187 const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10);
189 GlobalProperty pc_compat_2_9[] = {
192 .property = "extended-tseg-mbytes",
193 .value = stringify(0),
196 const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9);
198 GlobalProperty pc_compat_2_8[] = {
200 .driver = TYPE_X86_CPU,
201 .property = "tcg-cpuid",
205 .driver = "kvmclock",
206 .property = "x-mach-use-reliable-get-clock",
210 .driver = "ICH9-LPC",
211 .property = "x-smi-broadcast",
215 .driver = TYPE_X86_CPU,
216 .property = "vmware-cpuid-freq",
220 .driver = "Haswell-" TYPE_X86_CPU,
221 .property = "stepping",
225 const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8);
227 GlobalProperty pc_compat_2_7[] = {
229 .driver = TYPE_X86_CPU,
230 .property = "l3-cache",
234 .driver = TYPE_X86_CPU,
235 .property = "full-cpuid-auto-level",
239 .driver = "Opteron_G3" "-" TYPE_X86_CPU,
240 .property = "family",
244 .driver = "Opteron_G3" "-" TYPE_X86_CPU,
249 .driver = "Opteron_G3" "-" TYPE_X86_CPU,
250 .property = "stepping",
254 .driver = "isa-pcspk",
255 .property = "migrate",
259 const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7);
261 GlobalProperty pc_compat_2_6[] = {
263 .driver = TYPE_X86_CPU,
264 .property = "cpuid-0xb",
268 .property = "romfile",
272 .driver = TYPE_X86_CPU,
273 .property = "fill-mtrr-mask",
277 .driver = "apic-common",
278 .property = "legacy-instance-id",
282 const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6);
284 GlobalProperty pc_compat_2_5[] = {};
285 const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5);
287 void gsi_handler(void *opaque, int n, int level)
289 GSIState *s = opaque;
291 DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
292 if (n < ISA_NUM_IRQS) {
293 qemu_set_irq(s->i8259_irq[n], level);
295 qemu_set_irq(s->ioapic_irq[n], level);
298 static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
303 static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
305 return 0xffffffffffffffffULL;
308 /* MSDOS compatibility mode FPU exception support */
309 static qemu_irq ferr_irq;
311 void pc_register_ferr_irq(qemu_irq irq)
316 /* XXX: add IGNNE support */
317 void cpu_set_ferr(CPUX86State *s)
319 qemu_irq_raise(ferr_irq);
322 static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
325 qemu_irq_lower(ferr_irq);
328 static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
330 return 0xffffffffffffffffULL;
334 uint64_t cpu_get_tsc(CPUX86State *env)
336 return cpu_get_ticks();
340 int cpu_get_pic_interrupt(CPUX86State *env)
342 X86CPU *cpu = x86_env_get_cpu(env);
345 if (!kvm_irqchip_in_kernel()) {
346 intno = apic_get_interrupt(cpu->apic_state);
350 /* read the irq from the PIC */
351 if (!apic_accept_pic_intr(cpu->apic_state)) {
356 intno = pic_read_irq(isa_pic);
360 static void pic_irq_request(void *opaque, int irq, int level)
362 CPUState *cs = first_cpu;
363 X86CPU *cpu = X86_CPU(cs);
365 DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
366 if (cpu->apic_state && !kvm_irqchip_in_kernel()) {
369 if (apic_accept_pic_intr(cpu->apic_state)) {
370 apic_deliver_pic_intr(cpu->apic_state, level);
375 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
377 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
382 /* PC cmos mappings */
384 #define REG_EQUIPMENT_BYTE 0x14
386 int cmos_get_fd_drive_type(FloppyDriveType fd0)
391 case FLOPPY_DRIVE_TYPE_144:
392 /* 1.44 Mb 3"5 drive */
395 case FLOPPY_DRIVE_TYPE_288:
396 /* 2.88 Mb 3"5 drive */
399 case FLOPPY_DRIVE_TYPE_120:
400 /* 1.2 Mb 5"5 drive */
403 case FLOPPY_DRIVE_TYPE_NONE:
411 static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
412 int16_t cylinders, int8_t heads, int8_t sectors)
414 rtc_set_memory(s, type_ofs, 47);
415 rtc_set_memory(s, info_ofs, cylinders);
416 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
417 rtc_set_memory(s, info_ofs + 2, heads);
418 rtc_set_memory(s, info_ofs + 3, 0xff);
419 rtc_set_memory(s, info_ofs + 4, 0xff);
420 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
421 rtc_set_memory(s, info_ofs + 6, cylinders);
422 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
423 rtc_set_memory(s, info_ofs + 8, sectors);
426 /* convert boot_device letter to something recognizable by the bios */
427 static int boot_device2nibble(char boot_device)
429 switch(boot_device) {
432 return 0x01; /* floppy boot */
434 return 0x02; /* hard drive boot */
436 return 0x03; /* CD-ROM boot */
438 return 0x04; /* Network boot */
443 static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp)
445 #define PC_MAX_BOOT_DEVICES 3
446 int nbds, bds[3] = { 0, };
449 nbds = strlen(boot_device);
450 if (nbds > PC_MAX_BOOT_DEVICES) {
451 error_setg(errp, "Too many boot devices for PC");
454 for (i = 0; i < nbds; i++) {
455 bds[i] = boot_device2nibble(boot_device[i]);
457 error_setg(errp, "Invalid boot device for PC: '%c'",
462 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
463 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
466 static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
468 set_boot_dev(opaque, boot_device, errp);
471 static void pc_cmos_init_floppy(ISADevice *rtc_state, ISADevice *floppy)
474 FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE,
475 FLOPPY_DRIVE_TYPE_NONE };
479 for (i = 0; i < 2; i++) {
480 fd_type[i] = isa_fdc_get_drive_type(floppy, i);
483 val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
484 cmos_get_fd_drive_type(fd_type[1]);
485 rtc_set_memory(rtc_state, 0x10, val);
487 val = rtc_get_memory(rtc_state, REG_EQUIPMENT_BYTE);
489 if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) {
492 if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) {
499 val |= 0x01; /* 1 drive, ready for boot */
502 val |= 0x41; /* 2 drives, ready for boot */
505 rtc_set_memory(rtc_state, REG_EQUIPMENT_BYTE, val);
508 typedef struct pc_cmos_init_late_arg {
509 ISADevice *rtc_state;
511 } pc_cmos_init_late_arg;
513 typedef struct check_fdc_state {
518 static int check_fdc(Object *obj, void *opaque)
520 CheckFdcState *state = opaque;
523 Error *local_err = NULL;
525 fdc = object_dynamic_cast(obj, TYPE_ISA_FDC);
530 iobase = object_property_get_uint(obj, "iobase", &local_err);
531 if (local_err || iobase != 0x3f0) {
532 error_free(local_err);
537 state->multiple = true;
539 state->floppy = ISA_DEVICE(obj);
544 static const char * const fdc_container_path[] = {
545 "/unattached", "/peripheral", "/peripheral-anon"
549 * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
552 ISADevice *pc_find_fdc0(void)
556 CheckFdcState state = { 0 };
558 for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
559 container = container_get(qdev_get_machine(), fdc_container_path[i]);
560 object_child_foreach(container, check_fdc, &state);
563 if (state.multiple) {
564 warn_report("multiple floppy disk controllers with "
565 "iobase=0x3f0 have been found");
566 error_printf("the one being picked for CMOS setup might not reflect "
573 static void pc_cmos_init_late(void *opaque)
575 pc_cmos_init_late_arg *arg = opaque;
576 ISADevice *s = arg->rtc_state;
578 int8_t heads, sectors;
583 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 0,
584 &cylinders, &heads, §ors) >= 0) {
585 cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
588 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 1,
589 &cylinders, &heads, §ors) >= 0) {
590 cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
593 rtc_set_memory(s, 0x12, val);
596 for (i = 0; i < 4; i++) {
597 /* NOTE: ide_get_geometry() returns the physical
598 geometry. It is always such that: 1 <= sects <= 63, 1
599 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
600 geometry can be different if a translation is done. */
601 if (arg->idebus[i / 2] &&
602 ide_get_geometry(arg->idebus[i / 2], i % 2,
603 &cylinders, &heads, §ors) >= 0) {
604 trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
605 assert((trans & ~3) == 0);
606 val |= trans << (i * 2);
609 rtc_set_memory(s, 0x39, val);
611 pc_cmos_init_floppy(s, pc_find_fdc0());
613 qemu_unregister_reset(pc_cmos_init_late, opaque);
616 void pc_cmos_init(PCMachineState *pcms,
617 BusState *idebus0, BusState *idebus1,
621 static pc_cmos_init_late_arg arg;
623 /* various important CMOS locations needed by PC/Bochs bios */
626 /* base memory (first MiB) */
627 val = MIN(pcms->below_4g_mem_size / KiB, 640);
628 rtc_set_memory(s, 0x15, val);
629 rtc_set_memory(s, 0x16, val >> 8);
630 /* extended memory (next 64MiB) */
631 if (pcms->below_4g_mem_size > 1 * MiB) {
632 val = (pcms->below_4g_mem_size - 1 * MiB) / KiB;
638 rtc_set_memory(s, 0x17, val);
639 rtc_set_memory(s, 0x18, val >> 8);
640 rtc_set_memory(s, 0x30, val);
641 rtc_set_memory(s, 0x31, val >> 8);
642 /* memory between 16MiB and 4GiB */
643 if (pcms->below_4g_mem_size > 16 * MiB) {
644 val = (pcms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
650 rtc_set_memory(s, 0x34, val);
651 rtc_set_memory(s, 0x35, val >> 8);
652 /* memory above 4GiB */
653 val = pcms->above_4g_mem_size / 65536;
654 rtc_set_memory(s, 0x5b, val);
655 rtc_set_memory(s, 0x5c, val >> 8);
656 rtc_set_memory(s, 0x5d, val >> 16);
658 object_property_add_link(OBJECT(pcms), "rtc_state",
660 (Object **)&pcms->rtc,
661 object_property_allow_set_link,
662 OBJ_PROP_LINK_STRONG, &error_abort);
663 object_property_set_link(OBJECT(pcms), OBJECT(s),
664 "rtc_state", &error_abort);
666 set_boot_dev(s, MACHINE(pcms)->boot_order, &error_fatal);
669 val |= 0x02; /* FPU is there */
670 val |= 0x04; /* PS/2 mouse installed */
671 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
673 /* hard drives and FDC */
675 arg.idebus[0] = idebus0;
676 arg.idebus[1] = idebus1;
677 qemu_register_reset(pc_cmos_init_late, &arg);
680 #define TYPE_PORT92 "port92"
681 #define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92)
683 /* port 92 stuff: could be split off */
684 typedef struct Port92State {
685 ISADevice parent_obj;
692 static void port92_write(void *opaque, hwaddr addr, uint64_t val,
695 Port92State *s = opaque;
696 int oldval = s->outport;
698 DPRINTF("port92: write 0x%02" PRIx64 "\n", val);
700 qemu_set_irq(s->a20_out, (val >> 1) & 1);
701 if ((val & 1) && !(oldval & 1)) {
702 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
706 static uint64_t port92_read(void *opaque, hwaddr addr,
709 Port92State *s = opaque;
713 DPRINTF("port92: read 0x%02x\n", ret);
717 static void port92_init(ISADevice *dev, qemu_irq a20_out)
719 qdev_connect_gpio_out_named(DEVICE(dev), PORT92_A20_LINE, 0, a20_out);
722 static const VMStateDescription vmstate_port92_isa = {
725 .minimum_version_id = 1,
726 .fields = (VMStateField[]) {
727 VMSTATE_UINT8(outport, Port92State),
728 VMSTATE_END_OF_LIST()
732 static void port92_reset(DeviceState *d)
734 Port92State *s = PORT92(d);
739 static const MemoryRegionOps port92_ops = {
741 .write = port92_write,
743 .min_access_size = 1,
744 .max_access_size = 1,
746 .endianness = DEVICE_LITTLE_ENDIAN,
749 static void port92_initfn(Object *obj)
751 Port92State *s = PORT92(obj);
753 memory_region_init_io(&s->io, OBJECT(s), &port92_ops, s, "port92", 1);
757 qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, PORT92_A20_LINE, 1);
760 static void port92_realizefn(DeviceState *dev, Error **errp)
762 ISADevice *isadev = ISA_DEVICE(dev);
763 Port92State *s = PORT92(dev);
765 isa_register_ioport(isadev, &s->io, 0x92);
768 static void port92_class_initfn(ObjectClass *klass, void *data)
770 DeviceClass *dc = DEVICE_CLASS(klass);
772 dc->realize = port92_realizefn;
773 dc->reset = port92_reset;
774 dc->vmsd = &vmstate_port92_isa;
776 * Reason: unlike ordinary ISA devices, this one needs additional
777 * wiring: its A20 output line needs to be wired up by
780 dc->user_creatable = false;
783 static const TypeInfo port92_info = {
785 .parent = TYPE_ISA_DEVICE,
786 .instance_size = sizeof(Port92State),
787 .instance_init = port92_initfn,
788 .class_init = port92_class_initfn,
791 static void port92_register_types(void)
793 type_register_static(&port92_info);
796 type_init(port92_register_types)
798 static void handle_a20_line_change(void *opaque, int irq, int level)
800 X86CPU *cpu = opaque;
802 /* XXX: send to all CPUs ? */
803 /* XXX: add logic to handle multiple A20 line sources */
804 x86_cpu_set_a20(cpu, level);
807 int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
809 int index = le32_to_cpu(e820_reserve.count);
810 struct e820_entry *entry;
812 if (type != E820_RAM) {
813 /* old FW_CFG_E820_TABLE entry -- reservations only */
814 if (index >= E820_NR_ENTRIES) {
817 entry = &e820_reserve.entry[index++];
819 entry->address = cpu_to_le64(address);
820 entry->length = cpu_to_le64(length);
821 entry->type = cpu_to_le32(type);
823 e820_reserve.count = cpu_to_le32(index);
826 /* new "etc/e820" file -- include ram too */
827 e820_table = g_renew(struct e820_entry, e820_table, e820_entries + 1);
828 e820_table[e820_entries].address = cpu_to_le64(address);
829 e820_table[e820_entries].length = cpu_to_le64(length);
830 e820_table[e820_entries].type = cpu_to_le32(type);
836 int e820_get_num_entries(void)
841 bool e820_get_entry(int idx, uint32_t type, uint64_t *address, uint64_t *length)
843 if (idx < e820_entries && e820_table[idx].type == cpu_to_le32(type)) {
844 *address = le64_to_cpu(e820_table[idx].address);
845 *length = le64_to_cpu(e820_table[idx].length);
851 /* Enables contiguous-apic-ID mode, for compatibility */
852 static bool compat_apic_id_mode;
854 void enable_compat_apic_id_mode(void)
856 compat_apic_id_mode = true;
859 /* Calculates initial APIC ID for a specific CPU index
861 * Currently we need to be able to calculate the APIC ID from the CPU index
862 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
863 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
864 * all CPUs up to max_cpus.
866 static uint32_t x86_cpu_apic_id_from_index(unsigned int cpu_index)
871 correct_id = x86_apicid_from_cpu_idx(smp_cores, smp_threads, cpu_index);
872 if (compat_apic_id_mode) {
873 if (cpu_index != correct_id && !warned && !qtest_enabled()) {
874 error_report("APIC IDs set in compatibility mode, "
875 "CPU topology won't match the configuration");
884 static void pc_build_smbios(PCMachineState *pcms)
886 uint8_t *smbios_tables, *smbios_anchor;
887 size_t smbios_tables_len, smbios_anchor_len;
888 struct smbios_phys_mem_area *mem_array;
889 unsigned i, array_count;
890 MachineState *ms = MACHINE(pcms);
891 X86CPU *cpu = X86_CPU(ms->possible_cpus->cpus[0].cpu);
893 /* tell smbios about cpuid version and features */
894 smbios_set_cpuid(cpu->env.cpuid_version, cpu->env.features[FEAT_1_EDX]);
896 smbios_tables = smbios_get_table_legacy(&smbios_tables_len);
898 fw_cfg_add_bytes(pcms->fw_cfg, FW_CFG_SMBIOS_ENTRIES,
899 smbios_tables, smbios_tables_len);
902 /* build the array of physical mem area from e820 table */
903 mem_array = g_malloc0(sizeof(*mem_array) * e820_get_num_entries());
904 for (i = 0, array_count = 0; i < e820_get_num_entries(); i++) {
907 if (e820_get_entry(i, E820_RAM, &addr, &len)) {
908 mem_array[array_count].address = addr;
909 mem_array[array_count].length = len;
913 smbios_get_tables(mem_array, array_count,
914 &smbios_tables, &smbios_tables_len,
915 &smbios_anchor, &smbios_anchor_len);
919 fw_cfg_add_file(pcms->fw_cfg, "etc/smbios/smbios-tables",
920 smbios_tables, smbios_tables_len);
921 fw_cfg_add_file(pcms->fw_cfg, "etc/smbios/smbios-anchor",
922 smbios_anchor, smbios_anchor_len);
926 static FWCfgState *bochs_bios_init(AddressSpace *as, PCMachineState *pcms)
929 uint64_t *numa_fw_cfg;
931 const CPUArchIdList *cpus;
932 MachineClass *mc = MACHINE_GET_CLASS(pcms);
934 fw_cfg = fw_cfg_init_io_dma(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4, as);
935 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
937 /* FW_CFG_MAX_CPUS is a bit confusing/problematic on x86:
939 * For machine types prior to 1.8, SeaBIOS needs FW_CFG_MAX_CPUS for
940 * building MPTable, ACPI MADT, ACPI CPU hotplug and ACPI SRAT table,
941 * that tables are based on xAPIC ID and QEMU<->SeaBIOS interface
942 * for CPU hotplug also uses APIC ID and not "CPU index".
943 * This means that FW_CFG_MAX_CPUS is not the "maximum number of CPUs",
944 * but the "limit to the APIC ID values SeaBIOS may see".
946 * So for compatibility reasons with old BIOSes we are stuck with
947 * "etc/max-cpus" actually being apic_id_limit
949 fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)pcms->apic_id_limit);
950 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
951 fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES,
952 acpi_tables, acpi_tables_len);
953 fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, kvm_allows_irq0_override());
955 fw_cfg_add_bytes(fw_cfg, FW_CFG_E820_TABLE,
956 &e820_reserve, sizeof(e820_reserve));
957 fw_cfg_add_file(fw_cfg, "etc/e820", e820_table,
958 sizeof(struct e820_entry) * e820_entries);
960 fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, &hpet_cfg, sizeof(hpet_cfg));
961 /* allocate memory for the NUMA channel: one (64bit) word for the number
962 * of nodes, one word for each VCPU->node and one word for each node to
963 * hold the amount of memory.
965 numa_fw_cfg = g_new0(uint64_t, 1 + pcms->apic_id_limit + nb_numa_nodes);
966 numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
967 cpus = mc->possible_cpu_arch_ids(MACHINE(pcms));
968 for (i = 0; i < cpus->len; i++) {
969 unsigned int apic_id = cpus->cpus[i].arch_id;
970 assert(apic_id < pcms->apic_id_limit);
971 numa_fw_cfg[apic_id + 1] = cpu_to_le64(cpus->cpus[i].props.node_id);
973 for (i = 0; i < nb_numa_nodes; i++) {
974 numa_fw_cfg[pcms->apic_id_limit + 1 + i] =
975 cpu_to_le64(numa_info[i].node_mem);
977 fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, numa_fw_cfg,
978 (1 + pcms->apic_id_limit + nb_numa_nodes) *
979 sizeof(*numa_fw_cfg));
984 static long get_file_size(FILE *f)
988 /* XXX: on Unix systems, using fstat() probably makes more sense */
991 fseek(f, 0, SEEK_END);
993 fseek(f, where, SEEK_SET);
998 /* setup_data types */
1000 #define SETUP_E820_EXT 1
1010 } __attribute__((packed));
1012 static void load_linux(PCMachineState *pcms,
1016 int setup_size, kernel_size, cmdline_size;
1017 int dtb_size, setup_data_offset;
1018 uint32_t initrd_max;
1019 uint8_t header[8192], *setup, *kernel;
1020 hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
1023 MachineState *machine = MACHINE(pcms);
1024 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1025 struct setup_data *setup_data;
1026 const char *kernel_filename = machine->kernel_filename;
1027 const char *initrd_filename = machine->initrd_filename;
1028 const char *dtb_filename = machine->dtb;
1029 const char *kernel_cmdline = machine->kernel_cmdline;
1031 /* Align to 16 bytes as a paranoia measure */
1032 cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
1034 /* load the kernel header */
1035 f = fopen(kernel_filename, "rb");
1036 if (!f || !(kernel_size = get_file_size(f)) ||
1037 fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
1038 MIN(ARRAY_SIZE(header), kernel_size)) {
1039 fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
1040 kernel_filename, strerror(errno));
1044 /* kernel protocol version */
1046 fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
1048 if (ldl_p(header+0x202) == 0x53726448) {
1049 protocol = lduw_p(header+0x206);
1051 /* This looks like a multiboot kernel. If it is, let's stop
1052 treating it like a Linux kernel. */
1053 if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
1054 kernel_cmdline, kernel_size, header)) {
1060 if (protocol < 0x200 || !(header[0x211] & 0x01)) {
1062 real_addr = 0x90000;
1063 cmdline_addr = 0x9a000 - cmdline_size;
1064 prot_addr = 0x10000;
1065 } else if (protocol < 0x202) {
1066 /* High but ancient kernel */
1067 real_addr = 0x90000;
1068 cmdline_addr = 0x9a000 - cmdline_size;
1069 prot_addr = 0x100000;
1071 /* High and recent kernel */
1072 real_addr = 0x10000;
1073 cmdline_addr = 0x20000;
1074 prot_addr = 0x100000;
1079 "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
1080 "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
1081 "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
1087 /* highest address for loading the initrd */
1088 if (protocol >= 0x203) {
1089 initrd_max = ldl_p(header+0x22c);
1091 initrd_max = 0x37ffffff;
1094 if (initrd_max >= pcms->below_4g_mem_size - pcmc->acpi_data_size) {
1095 initrd_max = pcms->below_4g_mem_size - pcmc->acpi_data_size - 1;
1098 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
1099 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);
1100 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
1102 if (protocol >= 0x202) {
1103 stl_p(header+0x228, cmdline_addr);
1105 stw_p(header+0x20, 0xA33F);
1106 stw_p(header+0x22, cmdline_addr-real_addr);
1109 /* handle vga= parameter */
1110 vmode = strstr(kernel_cmdline, "vga=");
1112 unsigned int video_mode;
1115 if (!strncmp(vmode, "normal", 6)) {
1116 video_mode = 0xffff;
1117 } else if (!strncmp(vmode, "ext", 3)) {
1118 video_mode = 0xfffe;
1119 } else if (!strncmp(vmode, "ask", 3)) {
1120 video_mode = 0xfffd;
1122 video_mode = strtol(vmode, NULL, 0);
1124 stw_p(header+0x1fa, video_mode);
1128 /* High nybble = B reserved for QEMU; low nybble is revision number.
1129 If this code is substantially changed, you may want to consider
1130 incrementing the revision. */
1131 if (protocol >= 0x200) {
1132 header[0x210] = 0xB0;
1135 if (protocol >= 0x201) {
1136 header[0x211] |= 0x80; /* CAN_USE_HEAP */
1137 stw_p(header+0x224, cmdline_addr-real_addr-0x200);
1141 if (initrd_filename) {
1144 GError *gerr = NULL;
1146 if (protocol < 0x200) {
1147 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
1151 if (!g_file_get_contents(initrd_filename, &initrd_data,
1152 &initrd_size, &gerr)) {
1153 fprintf(stderr, "qemu: error reading initrd %s: %s\n",
1154 initrd_filename, gerr->message);
1157 if (initrd_size >= initrd_max) {
1158 fprintf(stderr, "qemu: initrd is too large, cannot support."
1159 "(max: %"PRIu32", need %"PRId64")\n",
1160 initrd_max, (uint64_t)initrd_size);
1164 initrd_addr = (initrd_max-initrd_size) & ~4095;
1166 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
1167 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
1168 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
1170 stl_p(header+0x218, initrd_addr);
1171 stl_p(header+0x21c, initrd_size);
1174 /* load kernel and setup */
1175 setup_size = header[0x1f1];
1176 if (setup_size == 0) {
1179 setup_size = (setup_size+1)*512;
1180 if (setup_size > kernel_size) {
1181 fprintf(stderr, "qemu: invalid kernel header\n");
1184 kernel_size -= setup_size;
1186 setup = g_malloc(setup_size);
1187 kernel = g_malloc(kernel_size);
1188 fseek(f, 0, SEEK_SET);
1189 if (fread(setup, 1, setup_size, f) != setup_size) {
1190 fprintf(stderr, "fread() failed\n");
1193 if (fread(kernel, 1, kernel_size, f) != kernel_size) {
1194 fprintf(stderr, "fread() failed\n");
1199 /* append dtb to kernel */
1201 if (protocol < 0x209) {
1202 fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n");
1206 dtb_size = get_image_size(dtb_filename);
1207 if (dtb_size <= 0) {
1208 fprintf(stderr, "qemu: error reading dtb %s: %s\n",
1209 dtb_filename, strerror(errno));
1213 setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16);
1214 kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size;
1215 kernel = g_realloc(kernel, kernel_size);
1217 stq_p(header+0x250, prot_addr + setup_data_offset);
1219 setup_data = (struct setup_data *)(kernel + setup_data_offset);
1220 setup_data->next = 0;
1221 setup_data->type = cpu_to_le32(SETUP_DTB);
1222 setup_data->len = cpu_to_le32(dtb_size);
1224 load_image_size(dtb_filename, setup_data->data, dtb_size);
1227 memcpy(setup, header, MIN(sizeof(header), setup_size));
1229 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
1230 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
1231 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
1233 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
1234 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
1235 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
1237 option_rom[nb_option_roms].bootindex = 0;
1238 option_rom[nb_option_roms].name = "linuxboot.bin";
1239 if (pcmc->linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) {
1240 option_rom[nb_option_roms].name = "linuxboot_dma.bin";
1245 #define NE2000_NB_MAX 6
1247 static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
1249 static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
1251 void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
1253 static int nb_ne2k = 0;
1255 if (nb_ne2k == NE2000_NB_MAX)
1257 isa_ne2000_init(bus, ne2000_io[nb_ne2k],
1258 ne2000_irq[nb_ne2k], nd);
1262 DeviceState *cpu_get_current_apic(void)
1265 X86CPU *cpu = X86_CPU(current_cpu);
1266 return cpu->apic_state;
1272 void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
1274 X86CPU *cpu = opaque;
1277 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
1281 static void pc_new_cpu(const char *typename, int64_t apic_id, Error **errp)
1284 Error *local_err = NULL;
1286 cpu = object_new(typename);
1288 object_property_set_uint(cpu, apic_id, "apic-id", &local_err);
1289 object_property_set_bool(cpu, true, "realized", &local_err);
1292 error_propagate(errp, local_err);
1295 void pc_hot_add_cpu(const int64_t id, Error **errp)
1297 MachineState *ms = MACHINE(qdev_get_machine());
1298 int64_t apic_id = x86_cpu_apic_id_from_index(id);
1299 Error *local_err = NULL;
1302 error_setg(errp, "Invalid CPU id: %" PRIi64, id);
1306 if (apic_id >= ACPI_CPU_HOTPLUG_ID_LIMIT) {
1307 error_setg(errp, "Unable to add CPU: %" PRIi64
1308 ", resulting APIC ID (%" PRIi64 ") is too large",
1313 pc_new_cpu(ms->cpu_type, apic_id, &local_err);
1315 error_propagate(errp, local_err);
1320 void pc_cpus_init(PCMachineState *pcms)
1323 const CPUArchIdList *possible_cpus;
1324 MachineState *ms = MACHINE(pcms);
1325 MachineClass *mc = MACHINE_GET_CLASS(pcms);
1327 /* Calculates the limit to CPU APIC ID values
1329 * Limit for the APIC ID value, so that all
1330 * CPU APIC IDs are < pcms->apic_id_limit.
1332 * This is used for FW_CFG_MAX_CPUS. See comments on bochs_bios_init().
1334 pcms->apic_id_limit = x86_cpu_apic_id_from_index(max_cpus - 1) + 1;
1335 possible_cpus = mc->possible_cpu_arch_ids(ms);
1336 for (i = 0; i < smp_cpus; i++) {
1337 pc_new_cpu(possible_cpus->cpus[i].type, possible_cpus->cpus[i].arch_id,
1342 static void pc_build_feature_control_file(PCMachineState *pcms)
1344 MachineState *ms = MACHINE(pcms);
1345 X86CPU *cpu = X86_CPU(ms->possible_cpus->cpus[0].cpu);
1346 CPUX86State *env = &cpu->env;
1347 uint32_t unused, ecx, edx;
1348 uint64_t feature_control_bits = 0;
1351 cpu_x86_cpuid(env, 1, 0, &unused, &unused, &ecx, &edx);
1352 if (ecx & CPUID_EXT_VMX) {
1353 feature_control_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1356 if ((edx & (CPUID_EXT2_MCE | CPUID_EXT2_MCA)) ==
1357 (CPUID_EXT2_MCE | CPUID_EXT2_MCA) &&
1358 (env->mcg_cap & MCG_LMCE_P)) {
1359 feature_control_bits |= FEATURE_CONTROL_LMCE;
1362 if (!feature_control_bits) {
1366 val = g_malloc(sizeof(*val));
1367 *val = cpu_to_le64(feature_control_bits | FEATURE_CONTROL_LOCKED);
1368 fw_cfg_add_file(pcms->fw_cfg, "etc/msr_feature_control", val, sizeof(*val));
1371 static void rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count)
1373 if (cpus_count > 0xff) {
1374 /* If the number of CPUs can't be represented in 8 bits, the
1375 * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
1376 * to make old BIOSes fail more predictably.
1378 rtc_set_memory(rtc, 0x5f, 0);
1380 rtc_set_memory(rtc, 0x5f, cpus_count - 1);
1385 void pc_machine_done(Notifier *notifier, void *data)
1387 PCMachineState *pcms = container_of(notifier,
1388 PCMachineState, machine_done);
1389 PCIBus *bus = pcms->bus;
1391 /* set the number of CPUs */
1392 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
1395 int extra_hosts = 0;
1397 QLIST_FOREACH(bus, &bus->child, sibling) {
1398 /* look for expander root buses */
1399 if (pci_bus_is_root(bus)) {
1403 if (extra_hosts && pcms->fw_cfg) {
1404 uint64_t *val = g_malloc(sizeof(*val));
1405 *val = cpu_to_le64(extra_hosts);
1406 fw_cfg_add_file(pcms->fw_cfg,
1407 "etc/extra-pci-roots", val, sizeof(*val));
1413 pc_build_smbios(pcms);
1414 pc_build_feature_control_file(pcms);
1415 /* update FW_CFG_NB_CPUS to account for -device added CPUs */
1416 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1419 if (pcms->apic_id_limit > 255 && !xen_enabled()) {
1420 IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
1422 if (!iommu || !x86_iommu_ir_supported(X86_IOMMU_DEVICE(iommu)) ||
1423 iommu->intr_eim != ON_OFF_AUTO_ON) {
1424 error_report("current -smp configuration requires "
1425 "Extended Interrupt Mode enabled. "
1426 "You can add an IOMMU using: "
1427 "-device intel-iommu,intremap=on,eim=on");
1433 void pc_guest_info_init(PCMachineState *pcms)
1437 pcms->apic_xrupt_override = kvm_allows_irq0_override();
1438 pcms->numa_nodes = nb_numa_nodes;
1439 pcms->node_mem = g_malloc0(pcms->numa_nodes *
1440 sizeof *pcms->node_mem);
1441 for (i = 0; i < nb_numa_nodes; i++) {
1442 pcms->node_mem[i] = numa_info[i].node_mem;
1445 pcms->machine_done.notify = pc_machine_done;
1446 qemu_add_machine_init_done_notifier(&pcms->machine_done);
1449 /* setup pci memory address space mapping into system address space */
1450 void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory,
1451 MemoryRegion *pci_address_space)
1453 /* Set to lower priority than RAM */
1454 memory_region_add_subregion_overlap(system_memory, 0x0,
1455 pci_address_space, -1);
1458 void pc_acpi_init(const char *default_dsdt)
1462 if (acpi_tables != NULL) {
1463 /* manually set via -acpitable, leave it alone */
1467 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, default_dsdt);
1468 if (filename == NULL) {
1469 warn_report("failed to find %s", default_dsdt);
1471 QemuOpts *opts = qemu_opts_create(qemu_find_opts("acpi"), NULL, 0,
1475 qemu_opt_set(opts, "file", filename, &error_abort);
1477 acpi_table_add_builtin(opts, &err);
1479 warn_reportf_err(err, "failed to load %s: ", filename);
1485 void xen_load_linux(PCMachineState *pcms)
1490 assert(MACHINE(pcms)->kernel_filename != NULL);
1492 fw_cfg = fw_cfg_init_io(FW_CFG_IO_BASE);
1493 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1496 load_linux(pcms, fw_cfg);
1497 for (i = 0; i < nb_option_roms; i++) {
1498 assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
1499 !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
1500 !strcmp(option_rom[i].name, "multiboot.bin"));
1501 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1503 pcms->fw_cfg = fw_cfg;
1506 void pc_memory_init(PCMachineState *pcms,
1507 MemoryRegion *system_memory,
1508 MemoryRegion *rom_memory,
1509 MemoryRegion **ram_memory)
1512 MemoryRegion *ram, *option_rom_mr;
1513 MemoryRegion *ram_below_4g, *ram_above_4g;
1515 MachineState *machine = MACHINE(pcms);
1516 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1518 assert(machine->ram_size == pcms->below_4g_mem_size +
1519 pcms->above_4g_mem_size);
1521 linux_boot = (machine->kernel_filename != NULL);
1523 /* Allocate RAM. We allocate it as a single memory region and use
1524 * aliases to address portions of it, mostly for backwards compatibility
1525 * with older qemus that used qemu_ram_alloc().
1527 ram = g_malloc(sizeof(*ram));
1528 memory_region_allocate_system_memory(ram, NULL, "pc.ram",
1531 ram_below_4g = g_malloc(sizeof(*ram_below_4g));
1532 memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", ram,
1533 0, pcms->below_4g_mem_size);
1534 memory_region_add_subregion(system_memory, 0, ram_below_4g);
1535 e820_add_entry(0, pcms->below_4g_mem_size, E820_RAM);
1536 if (pcms->above_4g_mem_size > 0) {
1537 ram_above_4g = g_malloc(sizeof(*ram_above_4g));
1538 memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", ram,
1539 pcms->below_4g_mem_size,
1540 pcms->above_4g_mem_size);
1541 memory_region_add_subregion(system_memory, 0x100000000ULL,
1543 e820_add_entry(0x100000000ULL, pcms->above_4g_mem_size, E820_RAM);
1546 if (!pcmc->has_reserved_memory &&
1547 (machine->ram_slots ||
1548 (machine->maxram_size > machine->ram_size))) {
1549 MachineClass *mc = MACHINE_GET_CLASS(machine);
1551 error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
1556 /* always allocate the device memory information */
1557 machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
1559 /* initialize device memory address space */
1560 if (pcmc->has_reserved_memory &&
1561 (machine->ram_size < machine->maxram_size)) {
1562 ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
1564 if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
1565 error_report("unsupported amount of memory slots: %"PRIu64,
1566 machine->ram_slots);
1570 if (QEMU_ALIGN_UP(machine->maxram_size,
1571 TARGET_PAGE_SIZE) != machine->maxram_size) {
1572 error_report("maximum memory size must by aligned to multiple of "
1573 "%d bytes", TARGET_PAGE_SIZE);
1577 machine->device_memory->base =
1578 ROUND_UP(0x100000000ULL + pcms->above_4g_mem_size, 1 * GiB);
1580 if (pcmc->enforce_aligned_dimm) {
1581 /* size device region assuming 1G page max alignment per slot */
1582 device_mem_size += (1 * GiB) * machine->ram_slots;
1585 if ((machine->device_memory->base + device_mem_size) <
1587 error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
1588 machine->maxram_size);
1592 memory_region_init(&machine->device_memory->mr, OBJECT(pcms),
1593 "device-memory", device_mem_size);
1594 memory_region_add_subregion(system_memory, machine->device_memory->base,
1595 &machine->device_memory->mr);
1598 /* Initialize PC system firmware */
1599 pc_system_firmware_init(rom_memory, !pcmc->pci_enabled);
1601 option_rom_mr = g_malloc(sizeof(*option_rom_mr));
1602 memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
1604 if (pcmc->pci_enabled) {
1605 memory_region_set_readonly(option_rom_mr, true);
1607 memory_region_add_subregion_overlap(rom_memory,
1612 fw_cfg = bochs_bios_init(&address_space_memory, pcms);
1616 if (pcmc->has_reserved_memory && machine->device_memory->base) {
1617 uint64_t *val = g_malloc(sizeof(*val));
1618 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1619 uint64_t res_mem_end = machine->device_memory->base;
1621 if (!pcmc->broken_reserved_end) {
1622 res_mem_end += memory_region_size(&machine->device_memory->mr);
1624 *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB));
1625 fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
1629 load_linux(pcms, fw_cfg);
1632 for (i = 0; i < nb_option_roms; i++) {
1633 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1635 pcms->fw_cfg = fw_cfg;
1637 /* Init default IOAPIC address space */
1638 pcms->ioapic_as = &address_space_memory;
1642 * The 64bit pci hole starts after "above 4G RAM" and
1643 * potentially the space reserved for memory hotplug.
1645 uint64_t pc_pci_hole64_start(void)
1647 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1648 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1649 MachineState *ms = MACHINE(pcms);
1650 uint64_t hole64_start = 0;
1652 if (pcmc->has_reserved_memory && ms->device_memory->base) {
1653 hole64_start = ms->device_memory->base;
1654 if (!pcmc->broken_reserved_end) {
1655 hole64_start += memory_region_size(&ms->device_memory->mr);
1658 hole64_start = 0x100000000ULL + pcms->above_4g_mem_size;
1661 return ROUND_UP(hole64_start, 1 * GiB);
1664 qemu_irq pc_allocate_cpu_irq(void)
1666 return qemu_allocate_irq(pic_irq_request, NULL, 0);
1669 DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
1671 DeviceState *dev = NULL;
1673 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA);
1675 PCIDevice *pcidev = pci_vga_init(pci_bus);
1676 dev = pcidev ? &pcidev->qdev : NULL;
1677 } else if (isa_bus) {
1678 ISADevice *isadev = isa_vga_init(isa_bus);
1679 dev = isadev ? DEVICE(isadev) : NULL;
1681 rom_reset_order_override();
1685 static const MemoryRegionOps ioport80_io_ops = {
1686 .write = ioport80_write,
1687 .read = ioport80_read,
1688 .endianness = DEVICE_NATIVE_ENDIAN,
1690 .min_access_size = 1,
1691 .max_access_size = 1,
1695 static const MemoryRegionOps ioportF0_io_ops = {
1696 .write = ioportF0_write,
1697 .read = ioportF0_read,
1698 .endianness = DEVICE_NATIVE_ENDIAN,
1700 .min_access_size = 1,
1701 .max_access_size = 1,
1705 static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl, bool no_vmport)
1708 DriveInfo *fd[MAX_FD];
1710 ISADevice *i8042, *port92, *vmmouse;
1712 serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
1713 parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
1715 for (i = 0; i < MAX_FD; i++) {
1716 fd[i] = drive_get(IF_FLOPPY, 0, i);
1717 create_fdctrl |= !!fd[i];
1719 if (create_fdctrl) {
1720 fdctrl_init_isa(isa_bus, fd);
1723 i8042 = isa_create_simple(isa_bus, "i8042");
1725 vmport_init(isa_bus);
1726 vmmouse = isa_try_create(isa_bus, "vmmouse");
1731 DeviceState *dev = DEVICE(vmmouse);
1732 qdev_prop_set_ptr(dev, "ps2_mouse", i8042);
1733 qdev_init_nofail(dev);
1735 port92 = isa_create_simple(isa_bus, "port92");
1737 a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
1738 i8042_setup_a20_line(i8042, a20_line[0]);
1739 port92_init(port92, a20_line[1]);
1743 void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
1744 ISADevice **rtc_state,
1751 DeviceState *hpet = NULL;
1752 int pit_isa_irq = 0;
1753 qemu_irq pit_alt_irq = NULL;
1754 qemu_irq rtc_irq = NULL;
1755 ISADevice *pit = NULL;
1756 MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
1757 MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
1759 memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
1760 memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
1762 memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
1763 memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
1766 * Check if an HPET shall be created.
1768 * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1769 * when the HPET wants to take over. Thus we have to disable the latter.
1771 if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
1772 /* In order to set property, here not using sysbus_try_create_simple */
1773 hpet = qdev_try_create(NULL, TYPE_HPET);
1775 /* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7
1776 * and earlier, use IRQ2 for compat. Otherwise, use IRQ16~23,
1779 uint8_t compat = object_property_get_uint(OBJECT(hpet),
1782 qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
1784 qdev_init_nofail(hpet);
1785 sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
1787 for (i = 0; i < GSI_NUM_PINS; i++) {
1788 sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
1791 pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
1792 rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
1795 *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq);
1797 qemu_register_boot_set(pc_boot_set, *rtc_state);
1799 if (!xen_enabled() && has_pit) {
1800 if (kvm_pit_in_kernel()) {
1801 pit = kvm_pit_init(isa_bus, 0x40);
1803 pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
1806 /* connect PIT to output control line of the HPET */
1807 qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
1809 pcspk_init(isa_bus, pit);
1812 i8257_dma_init(isa_bus, 0);
1815 pc_superio_init(isa_bus, create_fdctrl, no_vmport);
1818 void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
1822 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC);
1823 for (i = 0; i < nb_nics; i++) {
1824 NICInfo *nd = &nd_table[i];
1825 const char *model = nd->model ? nd->model : pcmc->default_nic_model;
1827 if (g_str_equal(model, "ne2k_isa")) {
1828 pc_init_ne2k_isa(isa_bus, nd);
1830 pci_nic_init_nofail(nd, pci_bus, model, NULL);
1833 rom_reset_order_override();
1836 void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
1842 if (kvm_ioapic_in_kernel()) {
1843 dev = qdev_create(NULL, "kvm-ioapic");
1845 dev = qdev_create(NULL, "ioapic");
1848 object_property_add_child(object_resolve_path(parent_name, NULL),
1849 "ioapic", OBJECT(dev), NULL);
1851 qdev_init_nofail(dev);
1852 d = SYS_BUS_DEVICE(dev);
1853 sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
1855 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1856 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
1860 static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
1863 const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1864 const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1865 const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1866 const uint64_t legacy_align = TARGET_PAGE_SIZE;
1869 * When -no-acpi is used with Q35 machine type, no ACPI is built,
1870 * but pcms->acpi_dev is still created. Check !acpi_enabled in
1871 * addition to cover this case.
1873 if (!pcms->acpi_dev || !acpi_enabled) {
1875 "memory hotplug is not enabled: missing acpi device or acpi disabled");
1879 if (is_nvdimm && !pcms->acpi_nvdimm_state.is_enabled) {
1880 error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
1884 pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev),
1885 pcmc->enforce_aligned_dimm ? NULL : &legacy_align, errp);
1888 static void pc_memory_plug(HotplugHandler *hotplug_dev,
1889 DeviceState *dev, Error **errp)
1891 HotplugHandlerClass *hhc;
1892 Error *local_err = NULL;
1893 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1894 bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1896 pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms), &local_err);
1902 nvdimm_plug(&pcms->acpi_nvdimm_state);
1905 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
1906 hhc->plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &error_abort);
1908 error_propagate(errp, local_err);
1911 static void pc_memory_unplug_request(HotplugHandler *hotplug_dev,
1912 DeviceState *dev, Error **errp)
1914 HotplugHandlerClass *hhc;
1915 Error *local_err = NULL;
1916 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1919 * When -no-acpi is used with Q35 machine type, no ACPI is built,
1920 * but pcms->acpi_dev is still created. Check !acpi_enabled in
1921 * addition to cover this case.
1923 if (!pcms->acpi_dev || !acpi_enabled) {
1924 error_setg(&local_err,
1925 "memory hotplug is not enabled: missing acpi device or acpi disabled");
1929 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
1930 error_setg(&local_err,
1931 "nvdimm device hot unplug is not supported yet.");
1935 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
1936 hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1939 error_propagate(errp, local_err);
1942 static void pc_memory_unplug(HotplugHandler *hotplug_dev,
1943 DeviceState *dev, Error **errp)
1945 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1946 HotplugHandlerClass *hhc;
1947 Error *local_err = NULL;
1949 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
1950 hhc->unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1956 pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms));
1957 object_unparent(OBJECT(dev));
1960 error_propagate(errp, local_err);
1963 static int pc_apic_cmp(const void *a, const void *b)
1965 CPUArchId *apic_a = (CPUArchId *)a;
1966 CPUArchId *apic_b = (CPUArchId *)b;
1968 return apic_a->arch_id - apic_b->arch_id;
1971 /* returns pointer to CPUArchId descriptor that matches CPU's apic_id
1972 * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
1973 * entry corresponding to CPU's apic_id returns NULL.
1975 static CPUArchId *pc_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
1977 CPUArchId apic_id, *found_cpu;
1979 apic_id.arch_id = id;
1980 found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus,
1981 ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus),
1983 if (found_cpu && idx) {
1984 *idx = found_cpu - ms->possible_cpus->cpus;
1989 static void pc_cpu_plug(HotplugHandler *hotplug_dev,
1990 DeviceState *dev, Error **errp)
1992 CPUArchId *found_cpu;
1993 HotplugHandlerClass *hhc;
1994 Error *local_err = NULL;
1995 X86CPU *cpu = X86_CPU(dev);
1996 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1998 if (pcms->acpi_dev) {
1999 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
2000 hhc->plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2006 /* increment the number of CPUs */
2009 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
2012 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
2015 found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
2016 found_cpu->cpu = OBJECT(dev);
2018 error_propagate(errp, local_err);
2020 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
2021 DeviceState *dev, Error **errp)
2024 HotplugHandlerClass *hhc;
2025 Error *local_err = NULL;
2026 X86CPU *cpu = X86_CPU(dev);
2027 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2029 if (!pcms->acpi_dev) {
2030 error_setg(&local_err, "CPU hot unplug not supported without ACPI");
2034 pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
2037 error_setg(&local_err, "Boot CPU is unpluggable");
2041 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
2042 hhc->unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2049 error_propagate(errp, local_err);
2053 static void pc_cpu_unplug_cb(HotplugHandler *hotplug_dev,
2054 DeviceState *dev, Error **errp)
2056 CPUArchId *found_cpu;
2057 HotplugHandlerClass *hhc;
2058 Error *local_err = NULL;
2059 X86CPU *cpu = X86_CPU(dev);
2060 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2062 hhc = HOTPLUG_HANDLER_GET_CLASS(pcms->acpi_dev);
2063 hhc->unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
2069 found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
2070 found_cpu->cpu = NULL;
2071 object_unparent(OBJECT(dev));
2073 /* decrement the number of CPUs */
2075 /* Update the number of CPUs in CMOS */
2076 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
2077 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
2079 error_propagate(errp, local_err);
2082 static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
2083 DeviceState *dev, Error **errp)
2087 CPUArchId *cpu_slot;
2088 X86CPUTopoInfo topo;
2089 X86CPU *cpu = X86_CPU(dev);
2090 MachineState *ms = MACHINE(hotplug_dev);
2091 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2093 if(!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
2094 error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
2099 /* if APIC ID is not set, set it based on socket/core/thread properties */
2100 if (cpu->apic_id == UNASSIGNED_APIC_ID) {
2101 int max_socket = (max_cpus - 1) / smp_threads / smp_cores;
2103 if (cpu->socket_id < 0) {
2104 error_setg(errp, "CPU socket-id is not set");
2106 } else if (cpu->socket_id > max_socket) {
2107 error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
2108 cpu->socket_id, max_socket);
2111 if (cpu->core_id < 0) {
2112 error_setg(errp, "CPU core-id is not set");
2114 } else if (cpu->core_id > (smp_cores - 1)) {
2115 error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
2116 cpu->core_id, smp_cores - 1);
2119 if (cpu->thread_id < 0) {
2120 error_setg(errp, "CPU thread-id is not set");
2122 } else if (cpu->thread_id > (smp_threads - 1)) {
2123 error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
2124 cpu->thread_id, smp_threads - 1);
2128 topo.pkg_id = cpu->socket_id;
2129 topo.core_id = cpu->core_id;
2130 topo.smt_id = cpu->thread_id;
2131 cpu->apic_id = apicid_from_topo_ids(smp_cores, smp_threads, &topo);
2134 cpu_slot = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
2136 MachineState *ms = MACHINE(pcms);
2138 x86_topo_ids_from_apicid(cpu->apic_id, smp_cores, smp_threads, &topo);
2139 error_setg(errp, "Invalid CPU [socket: %u, core: %u, thread: %u] with"
2140 " APIC ID %" PRIu32 ", valid index range 0:%d",
2141 topo.pkg_id, topo.core_id, topo.smt_id, cpu->apic_id,
2142 ms->possible_cpus->len - 1);
2146 if (cpu_slot->cpu) {
2147 error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists",
2152 /* if 'address' properties socket-id/core-id/thread-id are not set, set them
2153 * so that machine_query_hotpluggable_cpus would show correct values
2155 /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
2156 * once -smp refactoring is complete and there will be CPU private
2157 * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
2158 x86_topo_ids_from_apicid(cpu->apic_id, smp_cores, smp_threads, &topo);
2159 if (cpu->socket_id != -1 && cpu->socket_id != topo.pkg_id) {
2160 error_setg(errp, "property socket-id: %u doesn't match set apic-id:"
2161 " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id, topo.pkg_id);
2164 cpu->socket_id = topo.pkg_id;
2166 if (cpu->core_id != -1 && cpu->core_id != topo.core_id) {
2167 error_setg(errp, "property core-id: %u doesn't match set apic-id:"
2168 " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id, topo.core_id);
2171 cpu->core_id = topo.core_id;
2173 if (cpu->thread_id != -1 && cpu->thread_id != topo.smt_id) {
2174 error_setg(errp, "property thread-id: %u doesn't match set apic-id:"
2175 " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id, topo.smt_id);
2178 cpu->thread_id = topo.smt_id;
2180 if (cpu->hyperv_vpindex && !kvm_hv_vpindex_settable()) {
2181 error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX");
2186 cs->cpu_index = idx;
2188 numa_cpu_pre_plug(cpu_slot, dev, errp);
2191 static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
2192 DeviceState *dev, Error **errp)
2194 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2195 pc_memory_pre_plug(hotplug_dev, dev, errp);
2196 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2197 pc_cpu_pre_plug(hotplug_dev, dev, errp);
2201 static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
2202 DeviceState *dev, Error **errp)
2204 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2205 pc_memory_plug(hotplug_dev, dev, errp);
2206 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2207 pc_cpu_plug(hotplug_dev, dev, errp);
2211 static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
2212 DeviceState *dev, Error **errp)
2214 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2215 pc_memory_unplug_request(hotplug_dev, dev, errp);
2216 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2217 pc_cpu_unplug_request_cb(hotplug_dev, dev, errp);
2219 error_setg(errp, "acpi: device unplug request for not supported device"
2220 " type: %s", object_get_typename(OBJECT(dev)));
2224 static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
2225 DeviceState *dev, Error **errp)
2227 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2228 pc_memory_unplug(hotplug_dev, dev, errp);
2229 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2230 pc_cpu_unplug_cb(hotplug_dev, dev, errp);
2232 error_setg(errp, "acpi: device unplug for not supported device"
2233 " type: %s", object_get_typename(OBJECT(dev)));
2237 static HotplugHandler *pc_get_hotpug_handler(MachineState *machine,
2240 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
2241 object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2242 return HOTPLUG_HANDLER(machine);
2249 pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
2250 const char *name, void *opaque,
2253 MachineState *ms = MACHINE(obj);
2254 int64_t value = memory_region_size(&ms->device_memory->mr);
2256 visit_type_int(v, name, &value, errp);
2259 static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
2260 const char *name, void *opaque,
2263 PCMachineState *pcms = PC_MACHINE(obj);
2264 uint64_t value = pcms->max_ram_below_4g;
2266 visit_type_size(v, name, &value, errp);
2269 static void pc_machine_set_max_ram_below_4g(Object *obj, Visitor *v,
2270 const char *name, void *opaque,
2273 PCMachineState *pcms = PC_MACHINE(obj);
2274 Error *error = NULL;
2277 visit_type_size(v, name, &value, &error);
2279 error_propagate(errp, error);
2282 if (value > 4 * GiB) {
2284 "Machine option 'max-ram-below-4g=%"PRIu64
2285 "' expects size less than or equal to 4G", value);
2286 error_propagate(errp, error);
2290 if (value < 1 * MiB) {
2291 warn_report("Only %" PRIu64 " bytes of RAM below the 4GiB boundary,"
2292 "BIOS may not work with less than 1MiB", value);
2295 pcms->max_ram_below_4g = value;
2298 static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name,
2299 void *opaque, Error **errp)
2301 PCMachineState *pcms = PC_MACHINE(obj);
2302 OnOffAuto vmport = pcms->vmport;
2304 visit_type_OnOffAuto(v, name, &vmport, errp);
2307 static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
2308 void *opaque, Error **errp)
2310 PCMachineState *pcms = PC_MACHINE(obj);
2312 visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
2315 bool pc_machine_is_smm_enabled(PCMachineState *pcms)
2317 bool smm_available = false;
2319 if (pcms->smm == ON_OFF_AUTO_OFF) {
2323 if (tcg_enabled() || qtest_enabled()) {
2324 smm_available = true;
2325 } else if (kvm_enabled()) {
2326 smm_available = kvm_has_smm();
2329 if (smm_available) {
2333 if (pcms->smm == ON_OFF_AUTO_ON) {
2334 error_report("System Management Mode not supported by this hypervisor.");
2340 static void pc_machine_get_smm(Object *obj, Visitor *v, const char *name,
2341 void *opaque, Error **errp)
2343 PCMachineState *pcms = PC_MACHINE(obj);
2344 OnOffAuto smm = pcms->smm;
2346 visit_type_OnOffAuto(v, name, &smm, errp);
2349 static void pc_machine_set_smm(Object *obj, Visitor *v, const char *name,
2350 void *opaque, Error **errp)
2352 PCMachineState *pcms = PC_MACHINE(obj);
2354 visit_type_OnOffAuto(v, name, &pcms->smm, errp);
2357 static bool pc_machine_get_nvdimm(Object *obj, Error **errp)
2359 PCMachineState *pcms = PC_MACHINE(obj);
2361 return pcms->acpi_nvdimm_state.is_enabled;
2364 static void pc_machine_set_nvdimm(Object *obj, bool value, Error **errp)
2366 PCMachineState *pcms = PC_MACHINE(obj);
2368 pcms->acpi_nvdimm_state.is_enabled = value;
2371 static char *pc_machine_get_nvdimm_persistence(Object *obj, Error **errp)
2373 PCMachineState *pcms = PC_MACHINE(obj);
2375 return g_strdup(pcms->acpi_nvdimm_state.persistence_string);
2378 static void pc_machine_set_nvdimm_persistence(Object *obj, const char *value,
2381 PCMachineState *pcms = PC_MACHINE(obj);
2382 AcpiNVDIMMState *nvdimm_state = &pcms->acpi_nvdimm_state;
2384 if (strcmp(value, "cpu") == 0)
2385 nvdimm_state->persistence = 3;
2386 else if (strcmp(value, "mem-ctrl") == 0)
2387 nvdimm_state->persistence = 2;
2389 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
2394 g_free(nvdimm_state->persistence_string);
2395 nvdimm_state->persistence_string = g_strdup(value);
2398 static bool pc_machine_get_smbus(Object *obj, Error **errp)
2400 PCMachineState *pcms = PC_MACHINE(obj);
2402 return pcms->smbus_enabled;
2405 static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
2407 PCMachineState *pcms = PC_MACHINE(obj);
2409 pcms->smbus_enabled = value;
2412 static bool pc_machine_get_sata(Object *obj, Error **errp)
2414 PCMachineState *pcms = PC_MACHINE(obj);
2416 return pcms->sata_enabled;
2419 static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
2421 PCMachineState *pcms = PC_MACHINE(obj);
2423 pcms->sata_enabled = value;
2426 static bool pc_machine_get_pit(Object *obj, Error **errp)
2428 PCMachineState *pcms = PC_MACHINE(obj);
2430 return pcms->pit_enabled;
2433 static void pc_machine_set_pit(Object *obj, bool value, Error **errp)
2435 PCMachineState *pcms = PC_MACHINE(obj);
2437 pcms->pit_enabled = value;
2440 static void pc_machine_initfn(Object *obj)
2442 PCMachineState *pcms = PC_MACHINE(obj);
2444 pcms->max_ram_below_4g = 0; /* use default */
2445 pcms->smm = ON_OFF_AUTO_AUTO;
2446 pcms->vmport = ON_OFF_AUTO_AUTO;
2447 /* nvdimm is disabled on default. */
2448 pcms->acpi_nvdimm_state.is_enabled = false;
2449 /* acpi build is enabled by default if machine supports it */
2450 pcms->acpi_build_enabled = PC_MACHINE_GET_CLASS(pcms)->has_acpi_build;
2451 pcms->smbus_enabled = true;
2452 pcms->sata_enabled = true;
2453 pcms->pit_enabled = true;
2456 static void pc_machine_reset(void)
2461 qemu_devices_reset();
2463 /* Reset APIC after devices have been reset to cancel
2464 * any changes that qemu_devices_reset() might have done.
2469 if (cpu->apic_state) {
2470 device_reset(cpu->apic_state);
2475 static CpuInstanceProperties
2476 pc_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
2478 MachineClass *mc = MACHINE_GET_CLASS(ms);
2479 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
2481 assert(cpu_index < possible_cpus->len);
2482 return possible_cpus->cpus[cpu_index].props;
2485 static int64_t pc_get_default_cpu_node_id(const MachineState *ms, int idx)
2487 X86CPUTopoInfo topo;
2489 assert(idx < ms->possible_cpus->len);
2490 x86_topo_ids_from_apicid(ms->possible_cpus->cpus[idx].arch_id,
2491 smp_cores, smp_threads, &topo);
2492 return topo.pkg_id % nb_numa_nodes;
2495 static const CPUArchIdList *pc_possible_cpu_arch_ids(MachineState *ms)
2499 if (ms->possible_cpus) {
2501 * make sure that max_cpus hasn't changed since the first use, i.e.
2502 * -smp hasn't been parsed after it
2504 assert(ms->possible_cpus->len == max_cpus);
2505 return ms->possible_cpus;
2508 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
2509 sizeof(CPUArchId) * max_cpus);
2510 ms->possible_cpus->len = max_cpus;
2511 for (i = 0; i < ms->possible_cpus->len; i++) {
2512 X86CPUTopoInfo topo;
2514 ms->possible_cpus->cpus[i].type = ms->cpu_type;
2515 ms->possible_cpus->cpus[i].vcpus_count = 1;
2516 ms->possible_cpus->cpus[i].arch_id = x86_cpu_apic_id_from_index(i);
2517 x86_topo_ids_from_apicid(ms->possible_cpus->cpus[i].arch_id,
2518 smp_cores, smp_threads, &topo);
2519 ms->possible_cpus->cpus[i].props.has_socket_id = true;
2520 ms->possible_cpus->cpus[i].props.socket_id = topo.pkg_id;
2521 ms->possible_cpus->cpus[i].props.has_core_id = true;
2522 ms->possible_cpus->cpus[i].props.core_id = topo.core_id;
2523 ms->possible_cpus->cpus[i].props.has_thread_id = true;
2524 ms->possible_cpus->cpus[i].props.thread_id = topo.smt_id;
2526 return ms->possible_cpus;
2529 static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
2531 /* cpu index isn't used */
2535 X86CPU *cpu = X86_CPU(cs);
2537 if (!cpu->apic_state) {
2538 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
2540 apic_deliver_nmi(cpu->apic_state);
2545 static void pc_machine_class_init(ObjectClass *oc, void *data)
2547 MachineClass *mc = MACHINE_CLASS(oc);
2548 PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
2549 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
2550 NMIClass *nc = NMI_CLASS(oc);
2552 pcmc->pci_enabled = true;
2553 pcmc->has_acpi_build = true;
2554 pcmc->rsdp_in_ram = true;
2555 pcmc->smbios_defaults = true;
2556 pcmc->smbios_uuid_encoded = true;
2557 pcmc->gigabyte_align = true;
2558 pcmc->has_reserved_memory = true;
2559 pcmc->kvmclock_enabled = true;
2560 pcmc->enforce_aligned_dimm = true;
2561 /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
2562 * to be used at the moment, 32K should be enough for a while. */
2563 pcmc->acpi_data_size = 0x20000 + 0x8000;
2564 pcmc->save_tsc_khz = true;
2565 pcmc->linuxboot_dma_enabled = true;
2566 assert(!mc->get_hotplug_handler);
2567 mc->get_hotplug_handler = pc_get_hotpug_handler;
2568 mc->cpu_index_to_instance_props = pc_cpu_index_to_props;
2569 mc->get_default_cpu_node_id = pc_get_default_cpu_node_id;
2570 mc->possible_cpu_arch_ids = pc_possible_cpu_arch_ids;
2571 mc->auto_enable_numa_with_memhp = true;
2572 mc->has_hotpluggable_cpus = true;
2573 mc->default_boot_order = "cad";
2574 mc->hot_add_cpu = pc_hot_add_cpu;
2575 mc->block_default_type = IF_IDE;
2577 mc->reset = pc_machine_reset;
2578 hc->pre_plug = pc_machine_device_pre_plug_cb;
2579 hc->plug = pc_machine_device_plug_cb;
2580 hc->unplug_request = pc_machine_device_unplug_request_cb;
2581 hc->unplug = pc_machine_device_unplug_cb;
2582 nc->nmi_monitor_handler = x86_nmi;
2583 mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
2585 object_class_property_add(oc, PC_MACHINE_DEVMEM_REGION_SIZE, "int",
2586 pc_machine_get_device_memory_region_size, NULL,
2587 NULL, NULL, &error_abort);
2589 object_class_property_add(oc, PC_MACHINE_MAX_RAM_BELOW_4G, "size",
2590 pc_machine_get_max_ram_below_4g, pc_machine_set_max_ram_below_4g,
2591 NULL, NULL, &error_abort);
2593 object_class_property_set_description(oc, PC_MACHINE_MAX_RAM_BELOW_4G,
2594 "Maximum ram below the 4G boundary (32bit boundary)", &error_abort);
2596 object_class_property_add(oc, PC_MACHINE_SMM, "OnOffAuto",
2597 pc_machine_get_smm, pc_machine_set_smm,
2598 NULL, NULL, &error_abort);
2599 object_class_property_set_description(oc, PC_MACHINE_SMM,
2600 "Enable SMM (pc & q35)", &error_abort);
2602 object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto",
2603 pc_machine_get_vmport, pc_machine_set_vmport,
2604 NULL, NULL, &error_abort);
2605 object_class_property_set_description(oc, PC_MACHINE_VMPORT,
2606 "Enable vmport (pc & q35)", &error_abort);
2608 object_class_property_add_bool(oc, PC_MACHINE_NVDIMM,
2609 pc_machine_get_nvdimm, pc_machine_set_nvdimm, &error_abort);
2611 object_class_property_add_str(oc, PC_MACHINE_NVDIMM_PERSIST,
2612 pc_machine_get_nvdimm_persistence,
2613 pc_machine_set_nvdimm_persistence, &error_abort);
2615 object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
2616 pc_machine_get_smbus, pc_machine_set_smbus, &error_abort);
2618 object_class_property_add_bool(oc, PC_MACHINE_SATA,
2619 pc_machine_get_sata, pc_machine_set_sata, &error_abort);
2621 object_class_property_add_bool(oc, PC_MACHINE_PIT,
2622 pc_machine_get_pit, pc_machine_set_pit, &error_abort);
2625 static const TypeInfo pc_machine_info = {
2626 .name = TYPE_PC_MACHINE,
2627 .parent = TYPE_MACHINE,
2629 .instance_size = sizeof(PCMachineState),
2630 .instance_init = pc_machine_initfn,
2631 .class_size = sizeof(PCMachineClass),
2632 .class_init = pc_machine_class_init,
2633 .interfaces = (InterfaceInfo[]) {
2634 { TYPE_HOTPLUG_HANDLER },
2640 static void pc_machine_register_types(void)
2642 type_register_static(&pc_machine_info);
2645 type_init(pc_machine_register_types)