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"
27 #include "hw/i386/x86.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 "hw/i386/fw_cfg.h"
34 #include "sysemu/cpus.h"
35 #include "hw/block/fdc.h"
37 #include "hw/pci/pci.h"
38 #include "hw/pci/pci_bus.h"
39 #include "hw/nvram/fw_cfg.h"
40 #include "hw/timer/hpet.h"
41 #include "hw/firmware/smbios.h"
42 #include "hw/loader.h"
44 #include "migration/vmstate.h"
45 #include "multiboot.h"
46 #include "hw/timer/mc146818rtc.h"
47 #include "hw/dma/i8257.h"
48 #include "hw/timer/i8254.h"
49 #include "hw/input/i8042.h"
51 #include "hw/audio/pcspk.h"
52 #include "hw/pci/msi.h"
53 #include "hw/sysbus.h"
54 #include "sysemu/sysemu.h"
55 #include "sysemu/tcg.h"
56 #include "sysemu/numa.h"
57 #include "sysemu/kvm.h"
58 #include "sysemu/qtest.h"
59 #include "sysemu/reset.h"
60 #include "sysemu/runstate.h"
62 #include "hw/xen/xen.h"
63 #include "hw/xen/start_info.h"
64 #include "ui/qemu-spice.h"
65 #include "exec/memory.h"
66 #include "exec/address-spaces.h"
67 #include "sysemu/arch_init.h"
68 #include "qemu/bitmap.h"
69 #include "qemu/config-file.h"
70 #include "qemu/error-report.h"
71 #include "qemu/option.h"
72 #include "qemu/cutils.h"
73 #include "hw/acpi/acpi.h"
74 #include "hw/acpi/cpu_hotplug.h"
75 #include "hw/boards.h"
76 #include "acpi-build.h"
77 #include "hw/mem/pc-dimm.h"
78 #include "qapi/error.h"
79 #include "qapi/qapi-visit-common.h"
80 #include "qapi/visitor.h"
81 #include "hw/core/cpu.h"
84 #include "hw/i386/intel_iommu.h"
85 #include "hw/net/ne2000-isa.h"
86 #include "standard-headers/asm-x86/bootparam.h"
87 #include "hw/virtio/virtio-pmem-pci.h"
88 #include "hw/mem/memory-device.h"
89 #include "sysemu/replay.h"
90 #include "qapi/qmp/qerror.h"
91 #include "config-devices.h"
92 #include "e820_memory_layout.h"
95 /* debug PC/ISA interrupts */
99 #define DPRINTF(fmt, ...) \
100 do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
102 #define DPRINTF(fmt, ...)
105 struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
107 GlobalProperty pc_compat_4_1[] = {};
108 const size_t pc_compat_4_1_len = G_N_ELEMENTS(pc_compat_4_1);
110 GlobalProperty pc_compat_4_0[] = {};
111 const size_t pc_compat_4_0_len = G_N_ELEMENTS(pc_compat_4_0);
113 GlobalProperty pc_compat_3_1[] = {
114 { "intel-iommu", "dma-drain", "off" },
115 { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "off" },
116 { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "off" },
117 { "Opteron_G4" "-" TYPE_X86_CPU, "npt", "off" },
118 { "Opteron_G4" "-" TYPE_X86_CPU, "nrip-save", "off" },
119 { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "off" },
120 { "Opteron_G5" "-" TYPE_X86_CPU, "npt", "off" },
121 { "Opteron_G5" "-" TYPE_X86_CPU, "nrip-save", "off" },
122 { "EPYC" "-" TYPE_X86_CPU, "npt", "off" },
123 { "EPYC" "-" TYPE_X86_CPU, "nrip-save", "off" },
124 { "EPYC-IBPB" "-" TYPE_X86_CPU, "npt", "off" },
125 { "EPYC-IBPB" "-" TYPE_X86_CPU, "nrip-save", "off" },
126 { "Skylake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
127 { "Skylake-Client-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
128 { "Skylake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
129 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
130 { "Cascadelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
131 { "Icelake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
132 { "Icelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
133 { "Cascadelake-Server" "-" TYPE_X86_CPU, "stepping", "5" },
134 { TYPE_X86_CPU, "x-intel-pt-auto-level", "off" },
136 const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1);
138 GlobalProperty pc_compat_3_0[] = {
139 { TYPE_X86_CPU, "x-hv-synic-kvm-only", "on" },
140 { "Skylake-Server" "-" TYPE_X86_CPU, "pku", "off" },
141 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "pku", "off" },
143 const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0);
145 GlobalProperty pc_compat_2_12[] = {
146 { TYPE_X86_CPU, "legacy-cache", "on" },
147 { TYPE_X86_CPU, "topoext", "off" },
148 { "EPYC-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
149 { "EPYC-IBPB-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
151 const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12);
153 GlobalProperty pc_compat_2_11[] = {
154 { TYPE_X86_CPU, "x-migrate-smi-count", "off" },
155 { "Skylake-Server" "-" TYPE_X86_CPU, "clflushopt", "off" },
157 const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11);
159 GlobalProperty pc_compat_2_10[] = {
160 { TYPE_X86_CPU, "x-hv-max-vps", "0x40" },
161 { "i440FX-pcihost", "x-pci-hole64-fix", "off" },
162 { "q35-pcihost", "x-pci-hole64-fix", "off" },
164 const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10);
166 GlobalProperty pc_compat_2_9[] = {
167 { "mch", "extended-tseg-mbytes", "0" },
169 const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9);
171 GlobalProperty pc_compat_2_8[] = {
172 { TYPE_X86_CPU, "tcg-cpuid", "off" },
173 { "kvmclock", "x-mach-use-reliable-get-clock", "off" },
174 { "ICH9-LPC", "x-smi-broadcast", "off" },
175 { TYPE_X86_CPU, "vmware-cpuid-freq", "off" },
176 { "Haswell-" TYPE_X86_CPU, "stepping", "1" },
178 const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8);
180 GlobalProperty pc_compat_2_7[] = {
181 { TYPE_X86_CPU, "l3-cache", "off" },
182 { TYPE_X86_CPU, "full-cpuid-auto-level", "off" },
183 { "Opteron_G3" "-" TYPE_X86_CPU, "family", "15" },
184 { "Opteron_G3" "-" TYPE_X86_CPU, "model", "6" },
185 { "Opteron_G3" "-" TYPE_X86_CPU, "stepping", "1" },
186 { "isa-pcspk", "migrate", "off" },
188 const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7);
190 GlobalProperty pc_compat_2_6[] = {
191 { TYPE_X86_CPU, "cpuid-0xb", "off" },
192 { "vmxnet3", "romfile", "" },
193 { TYPE_X86_CPU, "fill-mtrr-mask", "off" },
194 { "apic-common", "legacy-instance-id", "on", }
196 const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6);
198 GlobalProperty pc_compat_2_5[] = {};
199 const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5);
201 GlobalProperty pc_compat_2_4[] = {
202 PC_CPU_MODEL_IDS("2.4.0")
203 { "Haswell-" TYPE_X86_CPU, "abm", "off" },
204 { "Haswell-noTSX-" TYPE_X86_CPU, "abm", "off" },
205 { "Broadwell-" TYPE_X86_CPU, "abm", "off" },
206 { "Broadwell-noTSX-" TYPE_X86_CPU, "abm", "off" },
207 { "host" "-" TYPE_X86_CPU, "host-cache-info", "on" },
208 { TYPE_X86_CPU, "check", "off" },
209 { "qemu64" "-" TYPE_X86_CPU, "sse4a", "on" },
210 { "qemu64" "-" TYPE_X86_CPU, "abm", "on" },
211 { "qemu64" "-" TYPE_X86_CPU, "popcnt", "on" },
212 { "qemu32" "-" TYPE_X86_CPU, "popcnt", "on" },
213 { "Opteron_G2" "-" TYPE_X86_CPU, "rdtscp", "on" },
214 { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "on" },
215 { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "on" },
216 { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "on", }
218 const size_t pc_compat_2_4_len = G_N_ELEMENTS(pc_compat_2_4);
220 GlobalProperty pc_compat_2_3[] = {
221 PC_CPU_MODEL_IDS("2.3.0")
222 { TYPE_X86_CPU, "arat", "off" },
223 { "qemu64" "-" TYPE_X86_CPU, "min-level", "4" },
224 { "kvm64" "-" TYPE_X86_CPU, "min-level", "5" },
225 { "pentium3" "-" TYPE_X86_CPU, "min-level", "2" },
226 { "n270" "-" TYPE_X86_CPU, "min-level", "5" },
227 { "Conroe" "-" TYPE_X86_CPU, "min-level", "4" },
228 { "Penryn" "-" TYPE_X86_CPU, "min-level", "4" },
229 { "Nehalem" "-" TYPE_X86_CPU, "min-level", "4" },
230 { "n270" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
231 { "Penryn" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
232 { "Conroe" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
233 { "Nehalem" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
234 { "Westmere" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
235 { "SandyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
236 { "IvyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
237 { "Haswell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
238 { "Haswell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
239 { "Broadwell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
240 { "Broadwell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
241 { TYPE_X86_CPU, "kvm-no-smi-migration", "on" },
243 const size_t pc_compat_2_3_len = G_N_ELEMENTS(pc_compat_2_3);
245 GlobalProperty pc_compat_2_2[] = {
246 PC_CPU_MODEL_IDS("2.2.0")
247 { "kvm64" "-" TYPE_X86_CPU, "vme", "off" },
248 { "kvm32" "-" TYPE_X86_CPU, "vme", "off" },
249 { "Conroe" "-" TYPE_X86_CPU, "vme", "off" },
250 { "Penryn" "-" TYPE_X86_CPU, "vme", "off" },
251 { "Nehalem" "-" TYPE_X86_CPU, "vme", "off" },
252 { "Westmere" "-" TYPE_X86_CPU, "vme", "off" },
253 { "SandyBridge" "-" TYPE_X86_CPU, "vme", "off" },
254 { "Haswell" "-" TYPE_X86_CPU, "vme", "off" },
255 { "Broadwell" "-" TYPE_X86_CPU, "vme", "off" },
256 { "Opteron_G1" "-" TYPE_X86_CPU, "vme", "off" },
257 { "Opteron_G2" "-" TYPE_X86_CPU, "vme", "off" },
258 { "Opteron_G3" "-" TYPE_X86_CPU, "vme", "off" },
259 { "Opteron_G4" "-" TYPE_X86_CPU, "vme", "off" },
260 { "Opteron_G5" "-" TYPE_X86_CPU, "vme", "off" },
261 { "Haswell" "-" TYPE_X86_CPU, "f16c", "off" },
262 { "Haswell" "-" TYPE_X86_CPU, "rdrand", "off" },
263 { "Broadwell" "-" TYPE_X86_CPU, "f16c", "off" },
264 { "Broadwell" "-" TYPE_X86_CPU, "rdrand", "off" },
266 const size_t pc_compat_2_2_len = G_N_ELEMENTS(pc_compat_2_2);
268 GlobalProperty pc_compat_2_1[] = {
269 PC_CPU_MODEL_IDS("2.1.0")
270 { "coreduo" "-" TYPE_X86_CPU, "vmx", "on" },
271 { "core2duo" "-" TYPE_X86_CPU, "vmx", "on" },
273 const size_t pc_compat_2_1_len = G_N_ELEMENTS(pc_compat_2_1);
275 GlobalProperty pc_compat_2_0[] = {
276 PC_CPU_MODEL_IDS("2.0.0")
277 { "virtio-scsi-pci", "any_layout", "off" },
278 { "PIIX4_PM", "memory-hotplug-support", "off" },
279 { "apic", "version", "0x11" },
280 { "nec-usb-xhci", "superspeed-ports-first", "off" },
281 { "nec-usb-xhci", "force-pcie-endcap", "on" },
282 { "pci-serial", "prog_if", "0" },
283 { "pci-serial-2x", "prog_if", "0" },
284 { "pci-serial-4x", "prog_if", "0" },
285 { "virtio-net-pci", "guest_announce", "off" },
286 { "ICH9-LPC", "memory-hotplug-support", "off" },
287 { "xio3130-downstream", COMPAT_PROP_PCP, "off" },
288 { "ioh3420", COMPAT_PROP_PCP, "off" },
290 const size_t pc_compat_2_0_len = G_N_ELEMENTS(pc_compat_2_0);
292 GlobalProperty pc_compat_1_7[] = {
293 PC_CPU_MODEL_IDS("1.7.0")
294 { TYPE_USB_DEVICE, "msos-desc", "no" },
295 { "PIIX4_PM", "acpi-pci-hotplug-with-bridge-support", "off" },
296 { "hpet", HPET_INTCAP, "4" },
298 const size_t pc_compat_1_7_len = G_N_ELEMENTS(pc_compat_1_7);
300 GlobalProperty pc_compat_1_6[] = {
301 PC_CPU_MODEL_IDS("1.6.0")
302 { "e1000", "mitigation", "off" },
303 { "qemu64-" TYPE_X86_CPU, "model", "2" },
304 { "qemu32-" TYPE_X86_CPU, "model", "3" },
305 { "i440FX-pcihost", "short_root_bus", "1" },
306 { "q35-pcihost", "short_root_bus", "1" },
308 const size_t pc_compat_1_6_len = G_N_ELEMENTS(pc_compat_1_6);
310 GlobalProperty pc_compat_1_5[] = {
311 PC_CPU_MODEL_IDS("1.5.0")
312 { "Conroe-" TYPE_X86_CPU, "model", "2" },
313 { "Conroe-" TYPE_X86_CPU, "min-level", "2" },
314 { "Penryn-" TYPE_X86_CPU, "model", "2" },
315 { "Penryn-" TYPE_X86_CPU, "min-level", "2" },
316 { "Nehalem-" TYPE_X86_CPU, "model", "2" },
317 { "Nehalem-" TYPE_X86_CPU, "min-level", "2" },
318 { "virtio-net-pci", "any_layout", "off" },
319 { TYPE_X86_CPU, "pmu", "on" },
320 { "i440FX-pcihost", "short_root_bus", "0" },
321 { "q35-pcihost", "short_root_bus", "0" },
323 const size_t pc_compat_1_5_len = G_N_ELEMENTS(pc_compat_1_5);
325 GlobalProperty pc_compat_1_4[] = {
326 PC_CPU_MODEL_IDS("1.4.0")
327 { "scsi-hd", "discard_granularity", "0" },
328 { "scsi-cd", "discard_granularity", "0" },
329 { "scsi-disk", "discard_granularity", "0" },
330 { "ide-hd", "discard_granularity", "0" },
331 { "ide-cd", "discard_granularity", "0" },
332 { "ide-drive", "discard_granularity", "0" },
333 { "virtio-blk-pci", "discard_granularity", "0" },
334 /* DEV_NVECTORS_UNSPECIFIED as a uint32_t string: */
335 { "virtio-serial-pci", "vectors", "0xFFFFFFFF" },
336 { "virtio-net-pci", "ctrl_guest_offloads", "off" },
337 { "e1000", "romfile", "pxe-e1000.rom" },
338 { "ne2k_pci", "romfile", "pxe-ne2k_pci.rom" },
339 { "pcnet", "romfile", "pxe-pcnet.rom" },
340 { "rtl8139", "romfile", "pxe-rtl8139.rom" },
341 { "virtio-net-pci", "romfile", "pxe-virtio.rom" },
342 { "486-" TYPE_X86_CPU, "model", "0" },
343 { "n270" "-" TYPE_X86_CPU, "movbe", "off" },
344 { "Westmere" "-" TYPE_X86_CPU, "pclmulqdq", "off" },
346 const size_t pc_compat_1_4_len = G_N_ELEMENTS(pc_compat_1_4);
348 void gsi_handler(void *opaque, int n, int level)
350 GSIState *s = opaque;
352 DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
353 if (n < ISA_NUM_IRQS) {
354 qemu_set_irq(s->i8259_irq[n], level);
356 qemu_set_irq(s->ioapic_irq[n], level);
359 static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
364 static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
366 return 0xffffffffffffffffULL;
369 /* MSDOS compatibility mode FPU exception support */
370 static qemu_irq ferr_irq;
372 void pc_register_ferr_irq(qemu_irq irq)
377 /* XXX: add IGNNE support */
378 void cpu_set_ferr(CPUX86State *s)
380 qemu_irq_raise(ferr_irq);
383 static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
386 qemu_irq_lower(ferr_irq);
389 static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
391 return 0xffffffffffffffffULL;
395 uint64_t cpu_get_tsc(CPUX86State *env)
397 return cpu_get_ticks();
401 int cpu_get_pic_interrupt(CPUX86State *env)
403 X86CPU *cpu = env_archcpu(env);
406 if (!kvm_irqchip_in_kernel()) {
407 intno = apic_get_interrupt(cpu->apic_state);
411 /* read the irq from the PIC */
412 if (!apic_accept_pic_intr(cpu->apic_state)) {
417 intno = pic_read_irq(isa_pic);
421 static void pic_irq_request(void *opaque, int irq, int level)
423 CPUState *cs = first_cpu;
424 X86CPU *cpu = X86_CPU(cs);
426 DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
427 if (cpu->apic_state && !kvm_irqchip_in_kernel()) {
430 if (apic_accept_pic_intr(cpu->apic_state)) {
431 apic_deliver_pic_intr(cpu->apic_state, level);
436 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
438 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
443 /* PC cmos mappings */
445 #define REG_EQUIPMENT_BYTE 0x14
447 int cmos_get_fd_drive_type(FloppyDriveType fd0)
452 case FLOPPY_DRIVE_TYPE_144:
453 /* 1.44 Mb 3"5 drive */
456 case FLOPPY_DRIVE_TYPE_288:
457 /* 2.88 Mb 3"5 drive */
460 case FLOPPY_DRIVE_TYPE_120:
461 /* 1.2 Mb 5"5 drive */
464 case FLOPPY_DRIVE_TYPE_NONE:
472 static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
473 int16_t cylinders, int8_t heads, int8_t sectors)
475 rtc_set_memory(s, type_ofs, 47);
476 rtc_set_memory(s, info_ofs, cylinders);
477 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
478 rtc_set_memory(s, info_ofs + 2, heads);
479 rtc_set_memory(s, info_ofs + 3, 0xff);
480 rtc_set_memory(s, info_ofs + 4, 0xff);
481 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
482 rtc_set_memory(s, info_ofs + 6, cylinders);
483 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
484 rtc_set_memory(s, info_ofs + 8, sectors);
487 /* convert boot_device letter to something recognizable by the bios */
488 static int boot_device2nibble(char boot_device)
490 switch(boot_device) {
493 return 0x01; /* floppy boot */
495 return 0x02; /* hard drive boot */
497 return 0x03; /* CD-ROM boot */
499 return 0x04; /* Network boot */
504 static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp)
506 #define PC_MAX_BOOT_DEVICES 3
507 int nbds, bds[3] = { 0, };
510 nbds = strlen(boot_device);
511 if (nbds > PC_MAX_BOOT_DEVICES) {
512 error_setg(errp, "Too many boot devices for PC");
515 for (i = 0; i < nbds; i++) {
516 bds[i] = boot_device2nibble(boot_device[i]);
518 error_setg(errp, "Invalid boot device for PC: '%c'",
523 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
524 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
527 static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
529 set_boot_dev(opaque, boot_device, errp);
532 static void pc_cmos_init_floppy(ISADevice *rtc_state, ISADevice *floppy)
535 FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE,
536 FLOPPY_DRIVE_TYPE_NONE };
540 for (i = 0; i < 2; i++) {
541 fd_type[i] = isa_fdc_get_drive_type(floppy, i);
544 val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
545 cmos_get_fd_drive_type(fd_type[1]);
546 rtc_set_memory(rtc_state, 0x10, val);
548 val = rtc_get_memory(rtc_state, REG_EQUIPMENT_BYTE);
550 if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) {
553 if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) {
560 val |= 0x01; /* 1 drive, ready for boot */
563 val |= 0x41; /* 2 drives, ready for boot */
566 rtc_set_memory(rtc_state, REG_EQUIPMENT_BYTE, val);
569 typedef struct pc_cmos_init_late_arg {
570 ISADevice *rtc_state;
572 } pc_cmos_init_late_arg;
574 typedef struct check_fdc_state {
579 static int check_fdc(Object *obj, void *opaque)
581 CheckFdcState *state = opaque;
584 Error *local_err = NULL;
586 fdc = object_dynamic_cast(obj, TYPE_ISA_FDC);
591 iobase = object_property_get_uint(obj, "iobase", &local_err);
592 if (local_err || iobase != 0x3f0) {
593 error_free(local_err);
598 state->multiple = true;
600 state->floppy = ISA_DEVICE(obj);
605 static const char * const fdc_container_path[] = {
606 "/unattached", "/peripheral", "/peripheral-anon"
610 * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
613 ISADevice *pc_find_fdc0(void)
617 CheckFdcState state = { 0 };
619 for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
620 container = container_get(qdev_get_machine(), fdc_container_path[i]);
621 object_child_foreach(container, check_fdc, &state);
624 if (state.multiple) {
625 warn_report("multiple floppy disk controllers with "
626 "iobase=0x3f0 have been found");
627 error_printf("the one being picked for CMOS setup might not reflect "
634 static void pc_cmos_init_late(void *opaque)
636 pc_cmos_init_late_arg *arg = opaque;
637 ISADevice *s = arg->rtc_state;
639 int8_t heads, sectors;
644 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 0,
645 &cylinders, &heads, §ors) >= 0) {
646 cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
649 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 1,
650 &cylinders, &heads, §ors) >= 0) {
651 cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
654 rtc_set_memory(s, 0x12, val);
657 for (i = 0; i < 4; i++) {
658 /* NOTE: ide_get_geometry() returns the physical
659 geometry. It is always such that: 1 <= sects <= 63, 1
660 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
661 geometry can be different if a translation is done. */
662 if (arg->idebus[i / 2] &&
663 ide_get_geometry(arg->idebus[i / 2], i % 2,
664 &cylinders, &heads, §ors) >= 0) {
665 trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
666 assert((trans & ~3) == 0);
667 val |= trans << (i * 2);
670 rtc_set_memory(s, 0x39, val);
672 pc_cmos_init_floppy(s, pc_find_fdc0());
674 qemu_unregister_reset(pc_cmos_init_late, opaque);
677 void pc_cmos_init(PCMachineState *pcms,
678 BusState *idebus0, BusState *idebus1,
682 static pc_cmos_init_late_arg arg;
684 /* various important CMOS locations needed by PC/Bochs bios */
687 /* base memory (first MiB) */
688 val = MIN(pcms->below_4g_mem_size / KiB, 640);
689 rtc_set_memory(s, 0x15, val);
690 rtc_set_memory(s, 0x16, val >> 8);
691 /* extended memory (next 64MiB) */
692 if (pcms->below_4g_mem_size > 1 * MiB) {
693 val = (pcms->below_4g_mem_size - 1 * MiB) / KiB;
699 rtc_set_memory(s, 0x17, val);
700 rtc_set_memory(s, 0x18, val >> 8);
701 rtc_set_memory(s, 0x30, val);
702 rtc_set_memory(s, 0x31, val >> 8);
703 /* memory between 16MiB and 4GiB */
704 if (pcms->below_4g_mem_size > 16 * MiB) {
705 val = (pcms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
711 rtc_set_memory(s, 0x34, val);
712 rtc_set_memory(s, 0x35, val >> 8);
713 /* memory above 4GiB */
714 val = pcms->above_4g_mem_size / 65536;
715 rtc_set_memory(s, 0x5b, val);
716 rtc_set_memory(s, 0x5c, val >> 8);
717 rtc_set_memory(s, 0x5d, val >> 16);
719 object_property_add_link(OBJECT(pcms), "rtc_state",
721 (Object **)&pcms->rtc,
722 object_property_allow_set_link,
723 OBJ_PROP_LINK_STRONG, &error_abort);
724 object_property_set_link(OBJECT(pcms), OBJECT(s),
725 "rtc_state", &error_abort);
727 set_boot_dev(s, MACHINE(pcms)->boot_order, &error_fatal);
730 val |= 0x02; /* FPU is there */
731 val |= 0x04; /* PS/2 mouse installed */
732 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
734 /* hard drives and FDC */
736 arg.idebus[0] = idebus0;
737 arg.idebus[1] = idebus1;
738 qemu_register_reset(pc_cmos_init_late, &arg);
741 #define TYPE_PORT92 "port92"
742 #define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92)
744 /* port 92 stuff: could be split off */
745 typedef struct Port92State {
746 ISADevice parent_obj;
753 static void port92_write(void *opaque, hwaddr addr, uint64_t val,
756 Port92State *s = opaque;
757 int oldval = s->outport;
759 DPRINTF("port92: write 0x%02" PRIx64 "\n", val);
761 qemu_set_irq(s->a20_out, (val >> 1) & 1);
762 if ((val & 1) && !(oldval & 1)) {
763 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
767 static uint64_t port92_read(void *opaque, hwaddr addr,
770 Port92State *s = opaque;
774 DPRINTF("port92: read 0x%02x\n", ret);
778 static void port92_init(ISADevice *dev, qemu_irq a20_out)
780 qdev_connect_gpio_out_named(DEVICE(dev), PORT92_A20_LINE, 0, a20_out);
783 static const VMStateDescription vmstate_port92_isa = {
786 .minimum_version_id = 1,
787 .fields = (VMStateField[]) {
788 VMSTATE_UINT8(outport, Port92State),
789 VMSTATE_END_OF_LIST()
793 static void port92_reset(DeviceState *d)
795 Port92State *s = PORT92(d);
800 static const MemoryRegionOps port92_ops = {
802 .write = port92_write,
804 .min_access_size = 1,
805 .max_access_size = 1,
807 .endianness = DEVICE_LITTLE_ENDIAN,
810 static void port92_initfn(Object *obj)
812 Port92State *s = PORT92(obj);
814 memory_region_init_io(&s->io, OBJECT(s), &port92_ops, s, "port92", 1);
818 qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, PORT92_A20_LINE, 1);
821 static void port92_realizefn(DeviceState *dev, Error **errp)
823 ISADevice *isadev = ISA_DEVICE(dev);
824 Port92State *s = PORT92(dev);
826 isa_register_ioport(isadev, &s->io, 0x92);
829 static void port92_class_initfn(ObjectClass *klass, void *data)
831 DeviceClass *dc = DEVICE_CLASS(klass);
833 dc->realize = port92_realizefn;
834 dc->reset = port92_reset;
835 dc->vmsd = &vmstate_port92_isa;
837 * Reason: unlike ordinary ISA devices, this one needs additional
838 * wiring: its A20 output line needs to be wired up by
841 dc->user_creatable = false;
844 static const TypeInfo port92_info = {
846 .parent = TYPE_ISA_DEVICE,
847 .instance_size = sizeof(Port92State),
848 .instance_init = port92_initfn,
849 .class_init = port92_class_initfn,
852 static void port92_register_types(void)
854 type_register_static(&port92_info);
857 type_init(port92_register_types)
859 static void handle_a20_line_change(void *opaque, int irq, int level)
861 X86CPU *cpu = opaque;
863 /* XXX: send to all CPUs ? */
864 /* XXX: add logic to handle multiple A20 line sources */
865 x86_cpu_set_a20(cpu, level);
868 #define NE2000_NB_MAX 6
870 static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
872 static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
874 void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
876 static int nb_ne2k = 0;
878 if (nb_ne2k == NE2000_NB_MAX)
880 isa_ne2000_init(bus, ne2000_io[nb_ne2k],
881 ne2000_irq[nb_ne2k], nd);
885 DeviceState *cpu_get_current_apic(void)
888 X86CPU *cpu = X86_CPU(current_cpu);
889 return cpu->apic_state;
895 void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
897 X86CPU *cpu = opaque;
900 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
905 * This function is very similar to smp_parse()
906 * in hw/core/machine.c but includes CPU die support.
908 void pc_smp_parse(MachineState *ms, QemuOpts *opts)
910 PCMachineState *pcms = PC_MACHINE(ms);
913 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
914 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
915 unsigned dies = qemu_opt_get_number(opts, "dies", 1);
916 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
917 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
919 /* compute missing values, prefer sockets over cores over threads */
920 if (cpus == 0 || sockets == 0) {
921 cores = cores > 0 ? cores : 1;
922 threads = threads > 0 ? threads : 1;
924 sockets = sockets > 0 ? sockets : 1;
925 cpus = cores * threads * dies * sockets;
928 qemu_opt_get_number(opts, "maxcpus", cpus);
929 sockets = ms->smp.max_cpus / (cores * threads * dies);
931 } else if (cores == 0) {
932 threads = threads > 0 ? threads : 1;
933 cores = cpus / (sockets * dies * threads);
934 cores = cores > 0 ? cores : 1;
935 } else if (threads == 0) {
936 threads = cpus / (cores * dies * sockets);
937 threads = threads > 0 ? threads : 1;
938 } else if (sockets * dies * cores * threads < cpus) {
939 error_report("cpu topology: "
940 "sockets (%u) * dies (%u) * cores (%u) * threads (%u) < "
942 sockets, dies, cores, threads, cpus);
947 qemu_opt_get_number(opts, "maxcpus", cpus);
949 if (ms->smp.max_cpus < cpus) {
950 error_report("maxcpus must be equal to or greater than smp");
954 if (sockets * dies * cores * threads > ms->smp.max_cpus) {
955 error_report("cpu topology: "
956 "sockets (%u) * dies (%u) * cores (%u) * threads (%u) > "
958 sockets, dies, cores, threads,
963 if (sockets * dies * cores * threads != ms->smp.max_cpus) {
964 warn_report("Invalid CPU topology deprecated: "
965 "sockets (%u) * dies (%u) * cores (%u) * threads (%u) "
967 sockets, dies, cores, threads,
972 ms->smp.cores = cores;
973 ms->smp.threads = threads;
974 pcms->smp_dies = dies;
977 if (ms->smp.cpus > 1) {
978 Error *blocker = NULL;
979 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
980 replay_add_blocker(blocker);
984 void pc_hot_add_cpu(MachineState *ms, const int64_t id, Error **errp)
986 PCMachineState *pcms = PC_MACHINE(ms);
987 int64_t apic_id = x86_cpu_apic_id_from_index(pcms, id);
988 Error *local_err = NULL;
991 error_setg(errp, "Invalid CPU id: %" PRIi64, id);
995 if (apic_id >= ACPI_CPU_HOTPLUG_ID_LIMIT) {
996 error_setg(errp, "Unable to add CPU: %" PRIi64
997 ", resulting APIC ID (%" PRIi64 ") is too large",
1002 x86_cpu_new(PC_MACHINE(ms), apic_id, &local_err);
1004 error_propagate(errp, local_err);
1009 static void rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count)
1011 if (cpus_count > 0xff) {
1012 /* If the number of CPUs can't be represented in 8 bits, the
1013 * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
1014 * to make old BIOSes fail more predictably.
1016 rtc_set_memory(rtc, 0x5f, 0);
1018 rtc_set_memory(rtc, 0x5f, cpus_count - 1);
1023 void pc_machine_done(Notifier *notifier, void *data)
1025 PCMachineState *pcms = container_of(notifier,
1026 PCMachineState, machine_done);
1027 PCIBus *bus = pcms->bus;
1029 /* set the number of CPUs */
1030 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
1033 int extra_hosts = 0;
1035 QLIST_FOREACH(bus, &bus->child, sibling) {
1036 /* look for expander root buses */
1037 if (pci_bus_is_root(bus)) {
1041 if (extra_hosts && pcms->fw_cfg) {
1042 uint64_t *val = g_malloc(sizeof(*val));
1043 *val = cpu_to_le64(extra_hosts);
1044 fw_cfg_add_file(pcms->fw_cfg,
1045 "etc/extra-pci-roots", val, sizeof(*val));
1051 fw_cfg_build_smbios(MACHINE(pcms), pcms->fw_cfg);
1052 fw_cfg_build_feature_control(MACHINE(pcms), pcms->fw_cfg);
1053 /* update FW_CFG_NB_CPUS to account for -device added CPUs */
1054 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1057 if (pcms->apic_id_limit > 255 && !xen_enabled()) {
1058 IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
1060 if (!iommu || !x86_iommu_ir_supported(X86_IOMMU_DEVICE(iommu)) ||
1061 iommu->intr_eim != ON_OFF_AUTO_ON) {
1062 error_report("current -smp configuration requires "
1063 "Extended Interrupt Mode enabled. "
1064 "You can add an IOMMU using: "
1065 "-device intel-iommu,intremap=on,eim=on");
1071 void pc_guest_info_init(PCMachineState *pcms)
1074 MachineState *ms = MACHINE(pcms);
1076 pcms->apic_xrupt_override = kvm_allows_irq0_override();
1077 pcms->numa_nodes = ms->numa_state->num_nodes;
1078 pcms->node_mem = g_malloc0(pcms->numa_nodes *
1079 sizeof *pcms->node_mem);
1080 for (i = 0; i < ms->numa_state->num_nodes; i++) {
1081 pcms->node_mem[i] = ms->numa_state->nodes[i].node_mem;
1084 pcms->machine_done.notify = pc_machine_done;
1085 qemu_add_machine_init_done_notifier(&pcms->machine_done);
1088 /* setup pci memory address space mapping into system address space */
1089 void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory,
1090 MemoryRegion *pci_address_space)
1092 /* Set to lower priority than RAM */
1093 memory_region_add_subregion_overlap(system_memory, 0x0,
1094 pci_address_space, -1);
1097 void xen_load_linux(PCMachineState *pcms)
1102 assert(MACHINE(pcms)->kernel_filename != NULL);
1104 fw_cfg = fw_cfg_init_io(FW_CFG_IO_BASE);
1105 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1108 x86_load_linux(pcms, fw_cfg);
1109 for (i = 0; i < nb_option_roms; i++) {
1110 assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
1111 !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
1112 !strcmp(option_rom[i].name, "pvh.bin") ||
1113 !strcmp(option_rom[i].name, "multiboot.bin"));
1114 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1116 pcms->fw_cfg = fw_cfg;
1119 void pc_memory_init(PCMachineState *pcms,
1120 MemoryRegion *system_memory,
1121 MemoryRegion *rom_memory,
1122 MemoryRegion **ram_memory)
1125 MemoryRegion *ram, *option_rom_mr;
1126 MemoryRegion *ram_below_4g, *ram_above_4g;
1128 MachineState *machine = MACHINE(pcms);
1129 MachineClass *mc = MACHINE_GET_CLASS(machine);
1130 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1132 assert(machine->ram_size == pcms->below_4g_mem_size +
1133 pcms->above_4g_mem_size);
1135 linux_boot = (machine->kernel_filename != NULL);
1137 /* Allocate RAM. We allocate it as a single memory region and use
1138 * aliases to address portions of it, mostly for backwards compatibility
1139 * with older qemus that used qemu_ram_alloc().
1141 ram = g_malloc(sizeof(*ram));
1142 memory_region_allocate_system_memory(ram, NULL, "pc.ram",
1145 ram_below_4g = g_malloc(sizeof(*ram_below_4g));
1146 memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", ram,
1147 0, pcms->below_4g_mem_size);
1148 memory_region_add_subregion(system_memory, 0, ram_below_4g);
1149 e820_add_entry(0, pcms->below_4g_mem_size, E820_RAM);
1150 if (pcms->above_4g_mem_size > 0) {
1151 ram_above_4g = g_malloc(sizeof(*ram_above_4g));
1152 memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", ram,
1153 pcms->below_4g_mem_size,
1154 pcms->above_4g_mem_size);
1155 memory_region_add_subregion(system_memory, 0x100000000ULL,
1157 e820_add_entry(0x100000000ULL, pcms->above_4g_mem_size, E820_RAM);
1160 if (!pcmc->has_reserved_memory &&
1161 (machine->ram_slots ||
1162 (machine->maxram_size > machine->ram_size))) {
1164 error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
1169 /* always allocate the device memory information */
1170 machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
1172 /* initialize device memory address space */
1173 if (pcmc->has_reserved_memory &&
1174 (machine->ram_size < machine->maxram_size)) {
1175 ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
1177 if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
1178 error_report("unsupported amount of memory slots: %"PRIu64,
1179 machine->ram_slots);
1183 if (QEMU_ALIGN_UP(machine->maxram_size,
1184 TARGET_PAGE_SIZE) != machine->maxram_size) {
1185 error_report("maximum memory size must by aligned to multiple of "
1186 "%d bytes", TARGET_PAGE_SIZE);
1190 machine->device_memory->base =
1191 ROUND_UP(0x100000000ULL + pcms->above_4g_mem_size, 1 * GiB);
1193 if (pcmc->enforce_aligned_dimm) {
1194 /* size device region assuming 1G page max alignment per slot */
1195 device_mem_size += (1 * GiB) * machine->ram_slots;
1198 if ((machine->device_memory->base + device_mem_size) <
1200 error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
1201 machine->maxram_size);
1205 memory_region_init(&machine->device_memory->mr, OBJECT(pcms),
1206 "device-memory", device_mem_size);
1207 memory_region_add_subregion(system_memory, machine->device_memory->base,
1208 &machine->device_memory->mr);
1211 /* Initialize PC system firmware */
1212 pc_system_firmware_init(pcms, rom_memory);
1214 option_rom_mr = g_malloc(sizeof(*option_rom_mr));
1215 memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
1217 if (pcmc->pci_enabled) {
1218 memory_region_set_readonly(option_rom_mr, true);
1220 memory_region_add_subregion_overlap(rom_memory,
1225 fw_cfg = fw_cfg_arch_create(machine,
1226 pcms->boot_cpus, pcms->apic_id_limit);
1230 if (pcmc->has_reserved_memory && machine->device_memory->base) {
1231 uint64_t *val = g_malloc(sizeof(*val));
1232 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1233 uint64_t res_mem_end = machine->device_memory->base;
1235 if (!pcmc->broken_reserved_end) {
1236 res_mem_end += memory_region_size(&machine->device_memory->mr);
1238 *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB));
1239 fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
1243 x86_load_linux(pcms, fw_cfg);
1246 for (i = 0; i < nb_option_roms; i++) {
1247 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1249 pcms->fw_cfg = fw_cfg;
1251 /* Init default IOAPIC address space */
1252 pcms->ioapic_as = &address_space_memory;
1254 /* Init ACPI memory hotplug IO base address */
1255 pcms->memhp_io_base = ACPI_MEMORY_HOTPLUG_BASE;
1259 * The 64bit pci hole starts after "above 4G RAM" and
1260 * potentially the space reserved for memory hotplug.
1262 uint64_t pc_pci_hole64_start(void)
1264 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1265 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1266 MachineState *ms = MACHINE(pcms);
1267 uint64_t hole64_start = 0;
1269 if (pcmc->has_reserved_memory && ms->device_memory->base) {
1270 hole64_start = ms->device_memory->base;
1271 if (!pcmc->broken_reserved_end) {
1272 hole64_start += memory_region_size(&ms->device_memory->mr);
1275 hole64_start = 0x100000000ULL + pcms->above_4g_mem_size;
1278 return ROUND_UP(hole64_start, 1 * GiB);
1281 qemu_irq pc_allocate_cpu_irq(void)
1283 return qemu_allocate_irq(pic_irq_request, NULL, 0);
1286 DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
1288 DeviceState *dev = NULL;
1290 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA);
1292 PCIDevice *pcidev = pci_vga_init(pci_bus);
1293 dev = pcidev ? &pcidev->qdev : NULL;
1294 } else if (isa_bus) {
1295 ISADevice *isadev = isa_vga_init(isa_bus);
1296 dev = isadev ? DEVICE(isadev) : NULL;
1298 rom_reset_order_override();
1302 static const MemoryRegionOps ioport80_io_ops = {
1303 .write = ioport80_write,
1304 .read = ioport80_read,
1305 .endianness = DEVICE_NATIVE_ENDIAN,
1307 .min_access_size = 1,
1308 .max_access_size = 1,
1312 static const MemoryRegionOps ioportF0_io_ops = {
1313 .write = ioportF0_write,
1314 .read = ioportF0_read,
1315 .endianness = DEVICE_NATIVE_ENDIAN,
1317 .min_access_size = 1,
1318 .max_access_size = 1,
1322 static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl, bool no_vmport)
1325 DriveInfo *fd[MAX_FD];
1327 ISADevice *i8042, *port92, *vmmouse;
1329 serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
1330 parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
1332 for (i = 0; i < MAX_FD; i++) {
1333 fd[i] = drive_get(IF_FLOPPY, 0, i);
1334 create_fdctrl |= !!fd[i];
1336 if (create_fdctrl) {
1337 fdctrl_init_isa(isa_bus, fd);
1340 i8042 = isa_create_simple(isa_bus, "i8042");
1342 vmport_init(isa_bus);
1343 vmmouse = isa_try_create(isa_bus, "vmmouse");
1348 DeviceState *dev = DEVICE(vmmouse);
1349 qdev_prop_set_ptr(dev, "ps2_mouse", i8042);
1350 qdev_init_nofail(dev);
1352 port92 = isa_create_simple(isa_bus, "port92");
1354 a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
1355 i8042_setup_a20_line(i8042, a20_line[0]);
1356 port92_init(port92, a20_line[1]);
1360 void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
1361 ISADevice **rtc_state,
1368 DeviceState *hpet = NULL;
1369 int pit_isa_irq = 0;
1370 qemu_irq pit_alt_irq = NULL;
1371 qemu_irq rtc_irq = NULL;
1372 ISADevice *pit = NULL;
1373 MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
1374 MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
1376 memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
1377 memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
1379 memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
1380 memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
1383 * Check if an HPET shall be created.
1385 * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1386 * when the HPET wants to take over. Thus we have to disable the latter.
1388 if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
1389 /* In order to set property, here not using sysbus_try_create_simple */
1390 hpet = qdev_try_create(NULL, TYPE_HPET);
1392 /* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7
1393 * and earlier, use IRQ2 for compat. Otherwise, use IRQ16~23,
1396 uint8_t compat = object_property_get_uint(OBJECT(hpet),
1399 qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
1401 qdev_init_nofail(hpet);
1402 sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
1404 for (i = 0; i < GSI_NUM_PINS; i++) {
1405 sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
1408 pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
1409 rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
1412 *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq);
1414 qemu_register_boot_set(pc_boot_set, *rtc_state);
1416 if (!xen_enabled() && has_pit) {
1417 if (kvm_pit_in_kernel()) {
1418 pit = kvm_pit_init(isa_bus, 0x40);
1420 pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
1423 /* connect PIT to output control line of the HPET */
1424 qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
1426 pcspk_init(isa_bus, pit);
1429 i8257_dma_init(isa_bus, 0);
1432 pc_superio_init(isa_bus, create_fdctrl, no_vmport);
1435 void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
1439 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC);
1440 for (i = 0; i < nb_nics; i++) {
1441 NICInfo *nd = &nd_table[i];
1442 const char *model = nd->model ? nd->model : pcmc->default_nic_model;
1444 if (g_str_equal(model, "ne2k_isa")) {
1445 pc_init_ne2k_isa(isa_bus, nd);
1447 pci_nic_init_nofail(nd, pci_bus, model, NULL);
1450 rom_reset_order_override();
1453 void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
1459 if (kvm_ioapic_in_kernel()) {
1460 dev = qdev_create(NULL, TYPE_KVM_IOAPIC);
1462 dev = qdev_create(NULL, TYPE_IOAPIC);
1465 object_property_add_child(object_resolve_path(parent_name, NULL),
1466 "ioapic", OBJECT(dev), NULL);
1468 qdev_init_nofail(dev);
1469 d = SYS_BUS_DEVICE(dev);
1470 sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
1472 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1473 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
1477 static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
1480 const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1481 const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
1482 const MachineState *ms = MACHINE(hotplug_dev);
1483 const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1484 const uint64_t legacy_align = TARGET_PAGE_SIZE;
1485 Error *local_err = NULL;
1488 * When -no-acpi is used with Q35 machine type, no ACPI is built,
1489 * but pcms->acpi_dev is still created. Check !acpi_enabled in
1490 * addition to cover this case.
1492 if (!pcms->acpi_dev || !acpi_enabled) {
1494 "memory hotplug is not enabled: missing acpi device or acpi disabled");
1498 if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
1499 error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
1503 hotplug_handler_pre_plug(pcms->acpi_dev, dev, &local_err);
1505 error_propagate(errp, local_err);
1509 pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev),
1510 pcmc->enforce_aligned_dimm ? NULL : &legacy_align, errp);
1513 static void pc_memory_plug(HotplugHandler *hotplug_dev,
1514 DeviceState *dev, Error **errp)
1516 Error *local_err = NULL;
1517 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1518 MachineState *ms = MACHINE(hotplug_dev);
1519 bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
1521 pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms), &local_err);
1527 nvdimm_plug(ms->nvdimms_state);
1530 hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &error_abort);
1532 error_propagate(errp, local_err);
1535 static void pc_memory_unplug_request(HotplugHandler *hotplug_dev,
1536 DeviceState *dev, Error **errp)
1538 Error *local_err = NULL;
1539 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1542 * When -no-acpi is used with Q35 machine type, no ACPI is built,
1543 * but pcms->acpi_dev is still created. Check !acpi_enabled in
1544 * addition to cover this case.
1546 if (!pcms->acpi_dev || !acpi_enabled) {
1547 error_setg(&local_err,
1548 "memory hotplug is not enabled: missing acpi device or acpi disabled");
1552 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
1553 error_setg(&local_err,
1554 "nvdimm device hot unplug is not supported yet.");
1558 hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev,
1561 error_propagate(errp, local_err);
1564 static void pc_memory_unplug(HotplugHandler *hotplug_dev,
1565 DeviceState *dev, Error **errp)
1567 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1568 Error *local_err = NULL;
1570 hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1575 pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms));
1576 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
1578 error_propagate(errp, local_err);
1581 static int pc_apic_cmp(const void *a, const void *b)
1583 CPUArchId *apic_a = (CPUArchId *)a;
1584 CPUArchId *apic_b = (CPUArchId *)b;
1586 return apic_a->arch_id - apic_b->arch_id;
1589 /* returns pointer to CPUArchId descriptor that matches CPU's apic_id
1590 * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
1591 * entry corresponding to CPU's apic_id returns NULL.
1593 static CPUArchId *pc_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
1595 CPUArchId apic_id, *found_cpu;
1597 apic_id.arch_id = id;
1598 found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus,
1599 ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus),
1601 if (found_cpu && idx) {
1602 *idx = found_cpu - ms->possible_cpus->cpus;
1607 static void pc_cpu_plug(HotplugHandler *hotplug_dev,
1608 DeviceState *dev, Error **errp)
1610 CPUArchId *found_cpu;
1611 Error *local_err = NULL;
1612 X86CPU *cpu = X86_CPU(dev);
1613 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1615 if (pcms->acpi_dev) {
1616 hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1622 /* increment the number of CPUs */
1625 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
1628 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1631 found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
1632 found_cpu->cpu = OBJECT(dev);
1634 error_propagate(errp, local_err);
1636 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
1637 DeviceState *dev, Error **errp)
1640 Error *local_err = NULL;
1641 X86CPU *cpu = X86_CPU(dev);
1642 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1644 if (!pcms->acpi_dev) {
1645 error_setg(&local_err, "CPU hot unplug not supported without ACPI");
1649 pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
1652 error_setg(&local_err, "Boot CPU is unpluggable");
1656 hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev,
1663 error_propagate(errp, local_err);
1667 static void pc_cpu_unplug_cb(HotplugHandler *hotplug_dev,
1668 DeviceState *dev, Error **errp)
1670 CPUArchId *found_cpu;
1671 Error *local_err = NULL;
1672 X86CPU *cpu = X86_CPU(dev);
1673 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1675 hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
1680 found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
1681 found_cpu->cpu = NULL;
1682 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
1684 /* decrement the number of CPUs */
1686 /* Update the number of CPUs in CMOS */
1687 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
1688 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
1690 error_propagate(errp, local_err);
1693 static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
1694 DeviceState *dev, Error **errp)
1698 CPUArchId *cpu_slot;
1699 X86CPUTopoInfo topo;
1700 X86CPU *cpu = X86_CPU(dev);
1701 CPUX86State *env = &cpu->env;
1702 MachineState *ms = MACHINE(hotplug_dev);
1703 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
1704 unsigned int smp_cores = ms->smp.cores;
1705 unsigned int smp_threads = ms->smp.threads;
1707 if(!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
1708 error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
1713 env->nr_dies = pcms->smp_dies;
1716 * If APIC ID is not set,
1717 * set it based on socket/die/core/thread properties.
1719 if (cpu->apic_id == UNASSIGNED_APIC_ID) {
1720 int max_socket = (ms->smp.max_cpus - 1) /
1721 smp_threads / smp_cores / pcms->smp_dies;
1724 * die-id was optional in QEMU 4.0 and older, so keep it optional
1725 * if there's only one die per socket.
1727 if (cpu->die_id < 0 && pcms->smp_dies == 1) {
1731 if (cpu->socket_id < 0) {
1732 error_setg(errp, "CPU socket-id is not set");
1734 } else if (cpu->socket_id > max_socket) {
1735 error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
1736 cpu->socket_id, max_socket);
1739 if (cpu->die_id < 0) {
1740 error_setg(errp, "CPU die-id is not set");
1742 } else if (cpu->die_id > pcms->smp_dies - 1) {
1743 error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u",
1744 cpu->die_id, pcms->smp_dies - 1);
1747 if (cpu->core_id < 0) {
1748 error_setg(errp, "CPU core-id is not set");
1750 } else if (cpu->core_id > (smp_cores - 1)) {
1751 error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
1752 cpu->core_id, smp_cores - 1);
1755 if (cpu->thread_id < 0) {
1756 error_setg(errp, "CPU thread-id is not set");
1758 } else if (cpu->thread_id > (smp_threads - 1)) {
1759 error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
1760 cpu->thread_id, smp_threads - 1);
1764 topo.pkg_id = cpu->socket_id;
1765 topo.die_id = cpu->die_id;
1766 topo.core_id = cpu->core_id;
1767 topo.smt_id = cpu->thread_id;
1768 cpu->apic_id = apicid_from_topo_ids(pcms->smp_dies, smp_cores,
1769 smp_threads, &topo);
1772 cpu_slot = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
1774 MachineState *ms = MACHINE(pcms);
1776 x86_topo_ids_from_apicid(cpu->apic_id, pcms->smp_dies,
1777 smp_cores, smp_threads, &topo);
1779 "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with"
1780 " APIC ID %" PRIu32 ", valid index range 0:%d",
1781 topo.pkg_id, topo.die_id, topo.core_id, topo.smt_id,
1782 cpu->apic_id, ms->possible_cpus->len - 1);
1786 if (cpu_slot->cpu) {
1787 error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists",
1792 /* if 'address' properties socket-id/core-id/thread-id are not set, set them
1793 * so that machine_query_hotpluggable_cpus would show correct values
1795 /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
1796 * once -smp refactoring is complete and there will be CPU private
1797 * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
1798 x86_topo_ids_from_apicid(cpu->apic_id, pcms->smp_dies,
1799 smp_cores, smp_threads, &topo);
1800 if (cpu->socket_id != -1 && cpu->socket_id != topo.pkg_id) {
1801 error_setg(errp, "property socket-id: %u doesn't match set apic-id:"
1802 " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id, topo.pkg_id);
1805 cpu->socket_id = topo.pkg_id;
1807 if (cpu->die_id != -1 && cpu->die_id != topo.die_id) {
1808 error_setg(errp, "property die-id: %u doesn't match set apic-id:"
1809 " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo.die_id);
1812 cpu->die_id = topo.die_id;
1814 if (cpu->core_id != -1 && cpu->core_id != topo.core_id) {
1815 error_setg(errp, "property core-id: %u doesn't match set apic-id:"
1816 " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id, topo.core_id);
1819 cpu->core_id = topo.core_id;
1821 if (cpu->thread_id != -1 && cpu->thread_id != topo.smt_id) {
1822 error_setg(errp, "property thread-id: %u doesn't match set apic-id:"
1823 " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id, topo.smt_id);
1826 cpu->thread_id = topo.smt_id;
1828 if (hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) &&
1829 !kvm_hv_vpindex_settable()) {
1830 error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX");
1835 cs->cpu_index = idx;
1837 numa_cpu_pre_plug(cpu_slot, dev, errp);
1840 static void pc_virtio_pmem_pci_pre_plug(HotplugHandler *hotplug_dev,
1841 DeviceState *dev, Error **errp)
1843 HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
1844 Error *local_err = NULL;
1846 if (!hotplug_dev2) {
1848 * Without a bus hotplug handler, we cannot control the plug/unplug
1849 * order. This should never be the case on x86, however better add
1852 error_setg(errp, "virtio-pmem-pci not supported on this bus.");
1856 * First, see if we can plug this memory device at all. If that
1857 * succeeds, branch of to the actual hotplug handler.
1859 memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL,
1862 hotplug_handler_pre_plug(hotplug_dev2, dev, &local_err);
1864 error_propagate(errp, local_err);
1867 static void pc_virtio_pmem_pci_plug(HotplugHandler *hotplug_dev,
1868 DeviceState *dev, Error **errp)
1870 HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
1871 Error *local_err = NULL;
1874 * Plug the memory device first and then branch off to the actual
1875 * hotplug handler. If that one fails, we can easily undo the memory
1878 memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
1879 hotplug_handler_plug(hotplug_dev2, dev, &local_err);
1881 memory_device_unplug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
1883 error_propagate(errp, local_err);
1886 static void pc_virtio_pmem_pci_unplug_request(HotplugHandler *hotplug_dev,
1887 DeviceState *dev, Error **errp)
1889 /* We don't support virtio pmem hot unplug */
1890 error_setg(errp, "virtio pmem device unplug not supported.");
1893 static void pc_virtio_pmem_pci_unplug(HotplugHandler *hotplug_dev,
1894 DeviceState *dev, Error **errp)
1896 /* We don't support virtio pmem hot unplug */
1899 static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
1900 DeviceState *dev, Error **errp)
1902 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1903 pc_memory_pre_plug(hotplug_dev, dev, errp);
1904 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1905 pc_cpu_pre_plug(hotplug_dev, dev, errp);
1906 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
1907 pc_virtio_pmem_pci_pre_plug(hotplug_dev, dev, errp);
1911 static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
1912 DeviceState *dev, Error **errp)
1914 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1915 pc_memory_plug(hotplug_dev, dev, errp);
1916 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1917 pc_cpu_plug(hotplug_dev, dev, errp);
1918 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
1919 pc_virtio_pmem_pci_plug(hotplug_dev, dev, errp);
1923 static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
1924 DeviceState *dev, Error **errp)
1926 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1927 pc_memory_unplug_request(hotplug_dev, dev, errp);
1928 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1929 pc_cpu_unplug_request_cb(hotplug_dev, dev, errp);
1930 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
1931 pc_virtio_pmem_pci_unplug_request(hotplug_dev, dev, errp);
1933 error_setg(errp, "acpi: device unplug request for not supported device"
1934 " type: %s", object_get_typename(OBJECT(dev)));
1938 static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
1939 DeviceState *dev, Error **errp)
1941 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
1942 pc_memory_unplug(hotplug_dev, dev, errp);
1943 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
1944 pc_cpu_unplug_cb(hotplug_dev, dev, errp);
1945 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
1946 pc_virtio_pmem_pci_unplug(hotplug_dev, dev, errp);
1948 error_setg(errp, "acpi: device unplug for not supported device"
1949 " type: %s", object_get_typename(OBJECT(dev)));
1953 static HotplugHandler *pc_get_hotplug_handler(MachineState *machine,
1956 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
1957 object_dynamic_cast(OBJECT(dev), TYPE_CPU) ||
1958 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
1959 return HOTPLUG_HANDLER(machine);
1966 pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
1967 const char *name, void *opaque,
1970 MachineState *ms = MACHINE(obj);
1973 if (ms->device_memory) {
1974 value = memory_region_size(&ms->device_memory->mr);
1977 visit_type_int(v, name, &value, errp);
1980 static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
1981 const char *name, void *opaque,
1984 PCMachineState *pcms = PC_MACHINE(obj);
1985 uint64_t value = pcms->max_ram_below_4g;
1987 visit_type_size(v, name, &value, errp);
1990 static void pc_machine_set_max_ram_below_4g(Object *obj, Visitor *v,
1991 const char *name, void *opaque,
1994 PCMachineState *pcms = PC_MACHINE(obj);
1995 Error *error = NULL;
1998 visit_type_size(v, name, &value, &error);
2000 error_propagate(errp, error);
2003 if (value > 4 * GiB) {
2005 "Machine option 'max-ram-below-4g=%"PRIu64
2006 "' expects size less than or equal to 4G", value);
2007 error_propagate(errp, error);
2011 if (value < 1 * MiB) {
2012 warn_report("Only %" PRIu64 " bytes of RAM below the 4GiB boundary,"
2013 "BIOS may not work with less than 1MiB", value);
2016 pcms->max_ram_below_4g = value;
2019 static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name,
2020 void *opaque, Error **errp)
2022 PCMachineState *pcms = PC_MACHINE(obj);
2023 OnOffAuto vmport = pcms->vmport;
2025 visit_type_OnOffAuto(v, name, &vmport, errp);
2028 static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
2029 void *opaque, Error **errp)
2031 PCMachineState *pcms = PC_MACHINE(obj);
2033 visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
2036 bool pc_machine_is_smm_enabled(PCMachineState *pcms)
2038 bool smm_available = false;
2040 if (pcms->smm == ON_OFF_AUTO_OFF) {
2044 if (tcg_enabled() || qtest_enabled()) {
2045 smm_available = true;
2046 } else if (kvm_enabled()) {
2047 smm_available = kvm_has_smm();
2050 if (smm_available) {
2054 if (pcms->smm == ON_OFF_AUTO_ON) {
2055 error_report("System Management Mode not supported by this hypervisor.");
2061 static void pc_machine_get_smm(Object *obj, Visitor *v, const char *name,
2062 void *opaque, Error **errp)
2064 PCMachineState *pcms = PC_MACHINE(obj);
2065 OnOffAuto smm = pcms->smm;
2067 visit_type_OnOffAuto(v, name, &smm, errp);
2070 static void pc_machine_set_smm(Object *obj, Visitor *v, const char *name,
2071 void *opaque, Error **errp)
2073 PCMachineState *pcms = PC_MACHINE(obj);
2075 visit_type_OnOffAuto(v, name, &pcms->smm, errp);
2078 static bool pc_machine_get_smbus(Object *obj, Error **errp)
2080 PCMachineState *pcms = PC_MACHINE(obj);
2082 return pcms->smbus_enabled;
2085 static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
2087 PCMachineState *pcms = PC_MACHINE(obj);
2089 pcms->smbus_enabled = value;
2092 static bool pc_machine_get_sata(Object *obj, Error **errp)
2094 PCMachineState *pcms = PC_MACHINE(obj);
2096 return pcms->sata_enabled;
2099 static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
2101 PCMachineState *pcms = PC_MACHINE(obj);
2103 pcms->sata_enabled = value;
2106 static bool pc_machine_get_pit(Object *obj, Error **errp)
2108 PCMachineState *pcms = PC_MACHINE(obj);
2110 return pcms->pit_enabled;
2113 static void pc_machine_set_pit(Object *obj, bool value, Error **errp)
2115 PCMachineState *pcms = PC_MACHINE(obj);
2117 pcms->pit_enabled = value;
2120 static void pc_machine_initfn(Object *obj)
2122 PCMachineState *pcms = PC_MACHINE(obj);
2124 pcms->max_ram_below_4g = 0; /* use default */
2125 pcms->smm = ON_OFF_AUTO_AUTO;
2126 #ifdef CONFIG_VMPORT
2127 pcms->vmport = ON_OFF_AUTO_AUTO;
2129 pcms->vmport = ON_OFF_AUTO_OFF;
2130 #endif /* CONFIG_VMPORT */
2131 /* acpi build is enabled by default if machine supports it */
2132 pcms->acpi_build_enabled = PC_MACHINE_GET_CLASS(pcms)->has_acpi_build;
2133 pcms->smbus_enabled = true;
2134 pcms->sata_enabled = true;
2135 pcms->pit_enabled = true;
2138 pc_system_flash_create(pcms);
2141 static void pc_machine_reset(MachineState *machine)
2146 qemu_devices_reset();
2148 /* Reset APIC after devices have been reset to cancel
2149 * any changes that qemu_devices_reset() might have done.
2154 if (cpu->apic_state) {
2155 device_reset(cpu->apic_state);
2160 static void pc_machine_wakeup(MachineState *machine)
2162 cpu_synchronize_all_states();
2163 pc_machine_reset(machine);
2164 cpu_synchronize_all_post_reset();
2167 static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
2169 /* cpu index isn't used */
2173 X86CPU *cpu = X86_CPU(cs);
2175 if (!cpu->apic_state) {
2176 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
2178 apic_deliver_nmi(cpu->apic_state);
2184 static bool pc_hotplug_allowed(MachineState *ms, DeviceState *dev, Error **errp)
2186 X86IOMMUState *iommu = x86_iommu_get_default();
2187 IntelIOMMUState *intel_iommu;
2190 object_dynamic_cast((Object *)iommu, TYPE_INTEL_IOMMU_DEVICE) &&
2191 object_dynamic_cast((Object *)dev, "vfio-pci")) {
2192 intel_iommu = INTEL_IOMMU_DEVICE(iommu);
2193 if (!intel_iommu->caching_mode) {
2194 error_setg(errp, "Device assignment is not allowed without "
2195 "enabling caching-mode=on for Intel IOMMU.");
2203 static void pc_machine_class_init(ObjectClass *oc, void *data)
2205 MachineClass *mc = MACHINE_CLASS(oc);
2206 PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
2207 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
2208 NMIClass *nc = NMI_CLASS(oc);
2210 pcmc->pci_enabled = true;
2211 pcmc->has_acpi_build = true;
2212 pcmc->rsdp_in_ram = true;
2213 pcmc->smbios_defaults = true;
2214 pcmc->smbios_uuid_encoded = true;
2215 pcmc->gigabyte_align = true;
2216 pcmc->has_reserved_memory = true;
2217 pcmc->kvmclock_enabled = true;
2218 pcmc->enforce_aligned_dimm = true;
2219 /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
2220 * to be used at the moment, 32K should be enough for a while. */
2221 pcmc->acpi_data_size = 0x20000 + 0x8000;
2222 pcmc->save_tsc_khz = true;
2223 pcmc->linuxboot_dma_enabled = true;
2224 pcmc->pvh_enabled = true;
2225 assert(!mc->get_hotplug_handler);
2226 mc->get_hotplug_handler = pc_get_hotplug_handler;
2227 mc->hotplug_allowed = pc_hotplug_allowed;
2228 mc->cpu_index_to_instance_props = x86_cpu_index_to_props;
2229 mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
2230 mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
2231 mc->auto_enable_numa_with_memhp = true;
2232 mc->has_hotpluggable_cpus = true;
2233 mc->default_boot_order = "cad";
2234 mc->hot_add_cpu = pc_hot_add_cpu;
2235 mc->smp_parse = pc_smp_parse;
2236 mc->block_default_type = IF_IDE;
2238 mc->reset = pc_machine_reset;
2239 mc->wakeup = pc_machine_wakeup;
2240 hc->pre_plug = pc_machine_device_pre_plug_cb;
2241 hc->plug = pc_machine_device_plug_cb;
2242 hc->unplug_request = pc_machine_device_unplug_request_cb;
2243 hc->unplug = pc_machine_device_unplug_cb;
2244 nc->nmi_monitor_handler = x86_nmi;
2245 mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
2246 mc->nvdimm_supported = true;
2247 mc->numa_mem_supported = true;
2249 object_class_property_add(oc, PC_MACHINE_DEVMEM_REGION_SIZE, "int",
2250 pc_machine_get_device_memory_region_size, NULL,
2251 NULL, NULL, &error_abort);
2253 object_class_property_add(oc, PC_MACHINE_MAX_RAM_BELOW_4G, "size",
2254 pc_machine_get_max_ram_below_4g, pc_machine_set_max_ram_below_4g,
2255 NULL, NULL, &error_abort);
2257 object_class_property_set_description(oc, PC_MACHINE_MAX_RAM_BELOW_4G,
2258 "Maximum ram below the 4G boundary (32bit boundary)", &error_abort);
2260 object_class_property_add(oc, PC_MACHINE_SMM, "OnOffAuto",
2261 pc_machine_get_smm, pc_machine_set_smm,
2262 NULL, NULL, &error_abort);
2263 object_class_property_set_description(oc, PC_MACHINE_SMM,
2264 "Enable SMM (pc & q35)", &error_abort);
2266 object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto",
2267 pc_machine_get_vmport, pc_machine_set_vmport,
2268 NULL, NULL, &error_abort);
2269 object_class_property_set_description(oc, PC_MACHINE_VMPORT,
2270 "Enable vmport (pc & q35)", &error_abort);
2272 object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
2273 pc_machine_get_smbus, pc_machine_set_smbus, &error_abort);
2275 object_class_property_add_bool(oc, PC_MACHINE_SATA,
2276 pc_machine_get_sata, pc_machine_set_sata, &error_abort);
2278 object_class_property_add_bool(oc, PC_MACHINE_PIT,
2279 pc_machine_get_pit, pc_machine_set_pit, &error_abort);
2282 static const TypeInfo pc_machine_info = {
2283 .name = TYPE_PC_MACHINE,
2284 .parent = TYPE_MACHINE,
2286 .instance_size = sizeof(PCMachineState),
2287 .instance_init = pc_machine_initfn,
2288 .class_size = sizeof(PCMachineClass),
2289 .class_init = pc_machine_class_init,
2290 .interfaces = (InterfaceInfo[]) {
2291 { TYPE_HOTPLUG_HANDLER },
2297 static void pc_machine_register_types(void)
2299 type_register_static(&pc_machine_info);
2302 type_init(pc_machine_register_types)