1 /* 2 * QEMU PC System Emulator 3 * 4 * Copyright (c) 2003-2004 Fabrice Bellard 5 * 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: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 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 22 * THE SOFTWARE. 23 */ 24 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" 36 #include "hw/ide.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" 43 #include "elf.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" 50 #include "hw/irq.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" 61 #include "kvm_i386.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" 82 #include "hw/usb.h" 83 #include "hw/i386/intel_iommu.h" 84 #include "hw/net/ne2000-isa.h" 85 #include "standard-headers/asm-x86/bootparam.h" 86 #include "hw/virtio/virtio-pmem-pci.h" 87 #include "hw/mem/memory-device.h" 88 #include "sysemu/replay.h" 89 #include "qapi/qmp/qerror.h" 90 #include "config-devices.h" 91 #include "e820_memory_layout.h" 92 #include "fw_cfg.h" 93 94 /* debug PC/ISA interrupts */ 95 //#define DEBUG_IRQ 96 97 #ifdef DEBUG_IRQ 98 #define DPRINTF(fmt, ...) \ 99 do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0) 100 #else 101 #define DPRINTF(fmt, ...) 102 #endif 103 104 struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX}; 105 106 GlobalProperty pc_compat_4_1[] = {}; 107 const size_t pc_compat_4_1_len = G_N_ELEMENTS(pc_compat_4_1); 108 109 GlobalProperty pc_compat_4_0[] = {}; 110 const size_t pc_compat_4_0_len = G_N_ELEMENTS(pc_compat_4_0); 111 112 GlobalProperty pc_compat_3_1[] = { 113 { "intel-iommu", "dma-drain", "off" }, 114 { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "off" }, 115 { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "off" }, 116 { "Opteron_G4" "-" TYPE_X86_CPU, "npt", "off" }, 117 { "Opteron_G4" "-" TYPE_X86_CPU, "nrip-save", "off" }, 118 { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "off" }, 119 { "Opteron_G5" "-" TYPE_X86_CPU, "npt", "off" }, 120 { "Opteron_G5" "-" TYPE_X86_CPU, "nrip-save", "off" }, 121 { "EPYC" "-" TYPE_X86_CPU, "npt", "off" }, 122 { "EPYC" "-" TYPE_X86_CPU, "nrip-save", "off" }, 123 { "EPYC-IBPB" "-" TYPE_X86_CPU, "npt", "off" }, 124 { "EPYC-IBPB" "-" TYPE_X86_CPU, "nrip-save", "off" }, 125 { "Skylake-Client" "-" TYPE_X86_CPU, "mpx", "on" }, 126 { "Skylake-Client-IBRS" "-" TYPE_X86_CPU, "mpx", "on" }, 127 { "Skylake-Server" "-" TYPE_X86_CPU, "mpx", "on" }, 128 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "mpx", "on" }, 129 { "Cascadelake-Server" "-" TYPE_X86_CPU, "mpx", "on" }, 130 { "Icelake-Client" "-" TYPE_X86_CPU, "mpx", "on" }, 131 { "Icelake-Server" "-" TYPE_X86_CPU, "mpx", "on" }, 132 { "Cascadelake-Server" "-" TYPE_X86_CPU, "stepping", "5" }, 133 { TYPE_X86_CPU, "x-intel-pt-auto-level", "off" }, 134 }; 135 const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1); 136 137 GlobalProperty pc_compat_3_0[] = { 138 { TYPE_X86_CPU, "x-hv-synic-kvm-only", "on" }, 139 { "Skylake-Server" "-" TYPE_X86_CPU, "pku", "off" }, 140 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "pku", "off" }, 141 }; 142 const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0); 143 144 GlobalProperty pc_compat_2_12[] = { 145 { TYPE_X86_CPU, "legacy-cache", "on" }, 146 { TYPE_X86_CPU, "topoext", "off" }, 147 { "EPYC-" TYPE_X86_CPU, "xlevel", "0x8000000a" }, 148 { "EPYC-IBPB-" TYPE_X86_CPU, "xlevel", "0x8000000a" }, 149 }; 150 const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12); 151 152 GlobalProperty pc_compat_2_11[] = { 153 { TYPE_X86_CPU, "x-migrate-smi-count", "off" }, 154 { "Skylake-Server" "-" TYPE_X86_CPU, "clflushopt", "off" }, 155 }; 156 const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11); 157 158 GlobalProperty pc_compat_2_10[] = { 159 { TYPE_X86_CPU, "x-hv-max-vps", "0x40" }, 160 { "i440FX-pcihost", "x-pci-hole64-fix", "off" }, 161 { "q35-pcihost", "x-pci-hole64-fix", "off" }, 162 }; 163 const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10); 164 165 GlobalProperty pc_compat_2_9[] = { 166 { "mch", "extended-tseg-mbytes", "0" }, 167 }; 168 const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9); 169 170 GlobalProperty pc_compat_2_8[] = { 171 { TYPE_X86_CPU, "tcg-cpuid", "off" }, 172 { "kvmclock", "x-mach-use-reliable-get-clock", "off" }, 173 { "ICH9-LPC", "x-smi-broadcast", "off" }, 174 { TYPE_X86_CPU, "vmware-cpuid-freq", "off" }, 175 { "Haswell-" TYPE_X86_CPU, "stepping", "1" }, 176 }; 177 const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8); 178 179 GlobalProperty pc_compat_2_7[] = { 180 { TYPE_X86_CPU, "l3-cache", "off" }, 181 { TYPE_X86_CPU, "full-cpuid-auto-level", "off" }, 182 { "Opteron_G3" "-" TYPE_X86_CPU, "family", "15" }, 183 { "Opteron_G3" "-" TYPE_X86_CPU, "model", "6" }, 184 { "Opteron_G3" "-" TYPE_X86_CPU, "stepping", "1" }, 185 { "isa-pcspk", "migrate", "off" }, 186 }; 187 const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7); 188 189 GlobalProperty pc_compat_2_6[] = { 190 { TYPE_X86_CPU, "cpuid-0xb", "off" }, 191 { "vmxnet3", "romfile", "" }, 192 { TYPE_X86_CPU, "fill-mtrr-mask", "off" }, 193 { "apic-common", "legacy-instance-id", "on", } 194 }; 195 const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6); 196 197 GlobalProperty pc_compat_2_5[] = {}; 198 const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5); 199 200 GlobalProperty pc_compat_2_4[] = { 201 PC_CPU_MODEL_IDS("2.4.0") 202 { "Haswell-" TYPE_X86_CPU, "abm", "off" }, 203 { "Haswell-noTSX-" TYPE_X86_CPU, "abm", "off" }, 204 { "Broadwell-" TYPE_X86_CPU, "abm", "off" }, 205 { "Broadwell-noTSX-" TYPE_X86_CPU, "abm", "off" }, 206 { "host" "-" TYPE_X86_CPU, "host-cache-info", "on" }, 207 { TYPE_X86_CPU, "check", "off" }, 208 { "qemu64" "-" TYPE_X86_CPU, "sse4a", "on" }, 209 { "qemu64" "-" TYPE_X86_CPU, "abm", "on" }, 210 { "qemu64" "-" TYPE_X86_CPU, "popcnt", "on" }, 211 { "qemu32" "-" TYPE_X86_CPU, "popcnt", "on" }, 212 { "Opteron_G2" "-" TYPE_X86_CPU, "rdtscp", "on" }, 213 { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "on" }, 214 { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "on" }, 215 { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "on", } 216 }; 217 const size_t pc_compat_2_4_len = G_N_ELEMENTS(pc_compat_2_4); 218 219 GlobalProperty pc_compat_2_3[] = { 220 PC_CPU_MODEL_IDS("2.3.0") 221 { TYPE_X86_CPU, "arat", "off" }, 222 { "qemu64" "-" TYPE_X86_CPU, "min-level", "4" }, 223 { "kvm64" "-" TYPE_X86_CPU, "min-level", "5" }, 224 { "pentium3" "-" TYPE_X86_CPU, "min-level", "2" }, 225 { "n270" "-" TYPE_X86_CPU, "min-level", "5" }, 226 { "Conroe" "-" TYPE_X86_CPU, "min-level", "4" }, 227 { "Penryn" "-" TYPE_X86_CPU, "min-level", "4" }, 228 { "Nehalem" "-" TYPE_X86_CPU, "min-level", "4" }, 229 { "n270" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 230 { "Penryn" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 231 { "Conroe" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 232 { "Nehalem" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 233 { "Westmere" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 234 { "SandyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 235 { "IvyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 236 { "Haswell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 237 { "Haswell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 238 { "Broadwell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 239 { "Broadwell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" }, 240 { TYPE_X86_CPU, "kvm-no-smi-migration", "on" }, 241 }; 242 const size_t pc_compat_2_3_len = G_N_ELEMENTS(pc_compat_2_3); 243 244 GlobalProperty pc_compat_2_2[] = { 245 PC_CPU_MODEL_IDS("2.2.0") 246 { "kvm64" "-" TYPE_X86_CPU, "vme", "off" }, 247 { "kvm32" "-" TYPE_X86_CPU, "vme", "off" }, 248 { "Conroe" "-" TYPE_X86_CPU, "vme", "off" }, 249 { "Penryn" "-" TYPE_X86_CPU, "vme", "off" }, 250 { "Nehalem" "-" TYPE_X86_CPU, "vme", "off" }, 251 { "Westmere" "-" TYPE_X86_CPU, "vme", "off" }, 252 { "SandyBridge" "-" TYPE_X86_CPU, "vme", "off" }, 253 { "Haswell" "-" TYPE_X86_CPU, "vme", "off" }, 254 { "Broadwell" "-" TYPE_X86_CPU, "vme", "off" }, 255 { "Opteron_G1" "-" TYPE_X86_CPU, "vme", "off" }, 256 { "Opteron_G2" "-" TYPE_X86_CPU, "vme", "off" }, 257 { "Opteron_G3" "-" TYPE_X86_CPU, "vme", "off" }, 258 { "Opteron_G4" "-" TYPE_X86_CPU, "vme", "off" }, 259 { "Opteron_G5" "-" TYPE_X86_CPU, "vme", "off" }, 260 { "Haswell" "-" TYPE_X86_CPU, "f16c", "off" }, 261 { "Haswell" "-" TYPE_X86_CPU, "rdrand", "off" }, 262 { "Broadwell" "-" TYPE_X86_CPU, "f16c", "off" }, 263 { "Broadwell" "-" TYPE_X86_CPU, "rdrand", "off" }, 264 }; 265 const size_t pc_compat_2_2_len = G_N_ELEMENTS(pc_compat_2_2); 266 267 GlobalProperty pc_compat_2_1[] = { 268 PC_CPU_MODEL_IDS("2.1.0") 269 { "coreduo" "-" TYPE_X86_CPU, "vmx", "on" }, 270 { "core2duo" "-" TYPE_X86_CPU, "vmx", "on" }, 271 }; 272 const size_t pc_compat_2_1_len = G_N_ELEMENTS(pc_compat_2_1); 273 274 GlobalProperty pc_compat_2_0[] = { 275 PC_CPU_MODEL_IDS("2.0.0") 276 { "virtio-scsi-pci", "any_layout", "off" }, 277 { "PIIX4_PM", "memory-hotplug-support", "off" }, 278 { "apic", "version", "0x11" }, 279 { "nec-usb-xhci", "superspeed-ports-first", "off" }, 280 { "nec-usb-xhci", "force-pcie-endcap", "on" }, 281 { "pci-serial", "prog_if", "0" }, 282 { "pci-serial-2x", "prog_if", "0" }, 283 { "pci-serial-4x", "prog_if", "0" }, 284 { "virtio-net-pci", "guest_announce", "off" }, 285 { "ICH9-LPC", "memory-hotplug-support", "off" }, 286 { "xio3130-downstream", COMPAT_PROP_PCP, "off" }, 287 { "ioh3420", COMPAT_PROP_PCP, "off" }, 288 }; 289 const size_t pc_compat_2_0_len = G_N_ELEMENTS(pc_compat_2_0); 290 291 GlobalProperty pc_compat_1_7[] = { 292 PC_CPU_MODEL_IDS("1.7.0") 293 { TYPE_USB_DEVICE, "msos-desc", "no" }, 294 { "PIIX4_PM", "acpi-pci-hotplug-with-bridge-support", "off" }, 295 { "hpet", HPET_INTCAP, "4" }, 296 }; 297 const size_t pc_compat_1_7_len = G_N_ELEMENTS(pc_compat_1_7); 298 299 GlobalProperty pc_compat_1_6[] = { 300 PC_CPU_MODEL_IDS("1.6.0") 301 { "e1000", "mitigation", "off" }, 302 { "qemu64-" TYPE_X86_CPU, "model", "2" }, 303 { "qemu32-" TYPE_X86_CPU, "model", "3" }, 304 { "i440FX-pcihost", "short_root_bus", "1" }, 305 { "q35-pcihost", "short_root_bus", "1" }, 306 }; 307 const size_t pc_compat_1_6_len = G_N_ELEMENTS(pc_compat_1_6); 308 309 GlobalProperty pc_compat_1_5[] = { 310 PC_CPU_MODEL_IDS("1.5.0") 311 { "Conroe-" TYPE_X86_CPU, "model", "2" }, 312 { "Conroe-" TYPE_X86_CPU, "min-level", "2" }, 313 { "Penryn-" TYPE_X86_CPU, "model", "2" }, 314 { "Penryn-" TYPE_X86_CPU, "min-level", "2" }, 315 { "Nehalem-" TYPE_X86_CPU, "model", "2" }, 316 { "Nehalem-" TYPE_X86_CPU, "min-level", "2" }, 317 { "virtio-net-pci", "any_layout", "off" }, 318 { TYPE_X86_CPU, "pmu", "on" }, 319 { "i440FX-pcihost", "short_root_bus", "0" }, 320 { "q35-pcihost", "short_root_bus", "0" }, 321 }; 322 const size_t pc_compat_1_5_len = G_N_ELEMENTS(pc_compat_1_5); 323 324 GlobalProperty pc_compat_1_4[] = { 325 PC_CPU_MODEL_IDS("1.4.0") 326 { "scsi-hd", "discard_granularity", "0" }, 327 { "scsi-cd", "discard_granularity", "0" }, 328 { "scsi-disk", "discard_granularity", "0" }, 329 { "ide-hd", "discard_granularity", "0" }, 330 { "ide-cd", "discard_granularity", "0" }, 331 { "ide-drive", "discard_granularity", "0" }, 332 { "virtio-blk-pci", "discard_granularity", "0" }, 333 /* DEV_NVECTORS_UNSPECIFIED as a uint32_t string: */ 334 { "virtio-serial-pci", "vectors", "0xFFFFFFFF" }, 335 { "virtio-net-pci", "ctrl_guest_offloads", "off" }, 336 { "e1000", "romfile", "pxe-e1000.rom" }, 337 { "ne2k_pci", "romfile", "pxe-ne2k_pci.rom" }, 338 { "pcnet", "romfile", "pxe-pcnet.rom" }, 339 { "rtl8139", "romfile", "pxe-rtl8139.rom" }, 340 { "virtio-net-pci", "romfile", "pxe-virtio.rom" }, 341 { "486-" TYPE_X86_CPU, "model", "0" }, 342 { "n270" "-" TYPE_X86_CPU, "movbe", "off" }, 343 { "Westmere" "-" TYPE_X86_CPU, "pclmulqdq", "off" }, 344 }; 345 const size_t pc_compat_1_4_len = G_N_ELEMENTS(pc_compat_1_4); 346 347 void gsi_handler(void *opaque, int n, int level) 348 { 349 GSIState *s = opaque; 350 351 DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n); 352 if (n < ISA_NUM_IRQS) { 353 qemu_set_irq(s->i8259_irq[n], level); 354 } 355 qemu_set_irq(s->ioapic_irq[n], level); 356 } 357 358 static void ioport80_write(void *opaque, hwaddr addr, uint64_t data, 359 unsigned size) 360 { 361 } 362 363 static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size) 364 { 365 return 0xffffffffffffffffULL; 366 } 367 368 /* MSDOS compatibility mode FPU exception support */ 369 static qemu_irq ferr_irq; 370 371 void pc_register_ferr_irq(qemu_irq irq) 372 { 373 ferr_irq = irq; 374 } 375 376 /* XXX: add IGNNE support */ 377 void cpu_set_ferr(CPUX86State *s) 378 { 379 qemu_irq_raise(ferr_irq); 380 } 381 382 static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data, 383 unsigned size) 384 { 385 qemu_irq_lower(ferr_irq); 386 } 387 388 static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size) 389 { 390 return 0xffffffffffffffffULL; 391 } 392 393 /* TSC handling */ 394 uint64_t cpu_get_tsc(CPUX86State *env) 395 { 396 return cpu_get_ticks(); 397 } 398 399 /* IRQ handling */ 400 int cpu_get_pic_interrupt(CPUX86State *env) 401 { 402 X86CPU *cpu = env_archcpu(env); 403 int intno; 404 405 if (!kvm_irqchip_in_kernel()) { 406 intno = apic_get_interrupt(cpu->apic_state); 407 if (intno >= 0) { 408 return intno; 409 } 410 /* read the irq from the PIC */ 411 if (!apic_accept_pic_intr(cpu->apic_state)) { 412 return -1; 413 } 414 } 415 416 intno = pic_read_irq(isa_pic); 417 return intno; 418 } 419 420 static void pic_irq_request(void *opaque, int irq, int level) 421 { 422 CPUState *cs = first_cpu; 423 X86CPU *cpu = X86_CPU(cs); 424 425 DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq); 426 if (cpu->apic_state && !kvm_irqchip_in_kernel()) { 427 CPU_FOREACH(cs) { 428 cpu = X86_CPU(cs); 429 if (apic_accept_pic_intr(cpu->apic_state)) { 430 apic_deliver_pic_intr(cpu->apic_state, level); 431 } 432 } 433 } else { 434 if (level) { 435 cpu_interrupt(cs, CPU_INTERRUPT_HARD); 436 } else { 437 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); 438 } 439 } 440 } 441 442 /* PC cmos mappings */ 443 444 #define REG_EQUIPMENT_BYTE 0x14 445 446 int cmos_get_fd_drive_type(FloppyDriveType fd0) 447 { 448 int val; 449 450 switch (fd0) { 451 case FLOPPY_DRIVE_TYPE_144: 452 /* 1.44 Mb 3"5 drive */ 453 val = 4; 454 break; 455 case FLOPPY_DRIVE_TYPE_288: 456 /* 2.88 Mb 3"5 drive */ 457 val = 5; 458 break; 459 case FLOPPY_DRIVE_TYPE_120: 460 /* 1.2 Mb 5"5 drive */ 461 val = 2; 462 break; 463 case FLOPPY_DRIVE_TYPE_NONE: 464 default: 465 val = 0; 466 break; 467 } 468 return val; 469 } 470 471 static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs, 472 int16_t cylinders, int8_t heads, int8_t sectors) 473 { 474 rtc_set_memory(s, type_ofs, 47); 475 rtc_set_memory(s, info_ofs, cylinders); 476 rtc_set_memory(s, info_ofs + 1, cylinders >> 8); 477 rtc_set_memory(s, info_ofs + 2, heads); 478 rtc_set_memory(s, info_ofs + 3, 0xff); 479 rtc_set_memory(s, info_ofs + 4, 0xff); 480 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3)); 481 rtc_set_memory(s, info_ofs + 6, cylinders); 482 rtc_set_memory(s, info_ofs + 7, cylinders >> 8); 483 rtc_set_memory(s, info_ofs + 8, sectors); 484 } 485 486 /* convert boot_device letter to something recognizable by the bios */ 487 static int boot_device2nibble(char boot_device) 488 { 489 switch(boot_device) { 490 case 'a': 491 case 'b': 492 return 0x01; /* floppy boot */ 493 case 'c': 494 return 0x02; /* hard drive boot */ 495 case 'd': 496 return 0x03; /* CD-ROM boot */ 497 case 'n': 498 return 0x04; /* Network boot */ 499 } 500 return 0; 501 } 502 503 static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp) 504 { 505 #define PC_MAX_BOOT_DEVICES 3 506 int nbds, bds[3] = { 0, }; 507 int i; 508 509 nbds = strlen(boot_device); 510 if (nbds > PC_MAX_BOOT_DEVICES) { 511 error_setg(errp, "Too many boot devices for PC"); 512 return; 513 } 514 for (i = 0; i < nbds; i++) { 515 bds[i] = boot_device2nibble(boot_device[i]); 516 if (bds[i] == 0) { 517 error_setg(errp, "Invalid boot device for PC: '%c'", 518 boot_device[i]); 519 return; 520 } 521 } 522 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]); 523 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1)); 524 } 525 526 static void pc_boot_set(void *opaque, const char *boot_device, Error **errp) 527 { 528 set_boot_dev(opaque, boot_device, errp); 529 } 530 531 static void pc_cmos_init_floppy(ISADevice *rtc_state, ISADevice *floppy) 532 { 533 int val, nb, i; 534 FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE, 535 FLOPPY_DRIVE_TYPE_NONE }; 536 537 /* floppy type */ 538 if (floppy) { 539 for (i = 0; i < 2; i++) { 540 fd_type[i] = isa_fdc_get_drive_type(floppy, i); 541 } 542 } 543 val = (cmos_get_fd_drive_type(fd_type[0]) << 4) | 544 cmos_get_fd_drive_type(fd_type[1]); 545 rtc_set_memory(rtc_state, 0x10, val); 546 547 val = rtc_get_memory(rtc_state, REG_EQUIPMENT_BYTE); 548 nb = 0; 549 if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) { 550 nb++; 551 } 552 if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) { 553 nb++; 554 } 555 switch (nb) { 556 case 0: 557 break; 558 case 1: 559 val |= 0x01; /* 1 drive, ready for boot */ 560 break; 561 case 2: 562 val |= 0x41; /* 2 drives, ready for boot */ 563 break; 564 } 565 rtc_set_memory(rtc_state, REG_EQUIPMENT_BYTE, val); 566 } 567 568 typedef struct pc_cmos_init_late_arg { 569 ISADevice *rtc_state; 570 BusState *idebus[2]; 571 } pc_cmos_init_late_arg; 572 573 typedef struct check_fdc_state { 574 ISADevice *floppy; 575 bool multiple; 576 } CheckFdcState; 577 578 static int check_fdc(Object *obj, void *opaque) 579 { 580 CheckFdcState *state = opaque; 581 Object *fdc; 582 uint32_t iobase; 583 Error *local_err = NULL; 584 585 fdc = object_dynamic_cast(obj, TYPE_ISA_FDC); 586 if (!fdc) { 587 return 0; 588 } 589 590 iobase = object_property_get_uint(obj, "iobase", &local_err); 591 if (local_err || iobase != 0x3f0) { 592 error_free(local_err); 593 return 0; 594 } 595 596 if (state->floppy) { 597 state->multiple = true; 598 } else { 599 state->floppy = ISA_DEVICE(obj); 600 } 601 return 0; 602 } 603 604 static const char * const fdc_container_path[] = { 605 "/unattached", "/peripheral", "/peripheral-anon" 606 }; 607 608 /* 609 * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers 610 * and ACPI objects. 611 */ 612 ISADevice *pc_find_fdc0(void) 613 { 614 int i; 615 Object *container; 616 CheckFdcState state = { 0 }; 617 618 for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) { 619 container = container_get(qdev_get_machine(), fdc_container_path[i]); 620 object_child_foreach(container, check_fdc, &state); 621 } 622 623 if (state.multiple) { 624 warn_report("multiple floppy disk controllers with " 625 "iobase=0x3f0 have been found"); 626 error_printf("the one being picked for CMOS setup might not reflect " 627 "your intent"); 628 } 629 630 return state.floppy; 631 } 632 633 static void pc_cmos_init_late(void *opaque) 634 { 635 pc_cmos_init_late_arg *arg = opaque; 636 ISADevice *s = arg->rtc_state; 637 int16_t cylinders; 638 int8_t heads, sectors; 639 int val; 640 int i, trans; 641 642 val = 0; 643 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 0, 644 &cylinders, &heads, §ors) >= 0) { 645 cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors); 646 val |= 0xf0; 647 } 648 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 1, 649 &cylinders, &heads, §ors) >= 0) { 650 cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors); 651 val |= 0x0f; 652 } 653 rtc_set_memory(s, 0x12, val); 654 655 val = 0; 656 for (i = 0; i < 4; i++) { 657 /* NOTE: ide_get_geometry() returns the physical 658 geometry. It is always such that: 1 <= sects <= 63, 1 659 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS 660 geometry can be different if a translation is done. */ 661 if (arg->idebus[i / 2] && 662 ide_get_geometry(arg->idebus[i / 2], i % 2, 663 &cylinders, &heads, §ors) >= 0) { 664 trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1; 665 assert((trans & ~3) == 0); 666 val |= trans << (i * 2); 667 } 668 } 669 rtc_set_memory(s, 0x39, val); 670 671 pc_cmos_init_floppy(s, pc_find_fdc0()); 672 673 qemu_unregister_reset(pc_cmos_init_late, opaque); 674 } 675 676 void pc_cmos_init(PCMachineState *pcms, 677 BusState *idebus0, BusState *idebus1, 678 ISADevice *s) 679 { 680 int val; 681 static pc_cmos_init_late_arg arg; 682 X86MachineState *x86ms = X86_MACHINE(pcms); 683 684 /* various important CMOS locations needed by PC/Bochs bios */ 685 686 /* memory size */ 687 /* base memory (first MiB) */ 688 val = MIN(x86ms->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 (x86ms->below_4g_mem_size > 1 * MiB) { 693 val = (x86ms->below_4g_mem_size - 1 * MiB) / KiB; 694 } else { 695 val = 0; 696 } 697 if (val > 65535) 698 val = 65535; 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 (x86ms->below_4g_mem_size > 16 * MiB) { 705 val = (x86ms->below_4g_mem_size - 16 * MiB) / (64 * KiB); 706 } else { 707 val = 0; 708 } 709 if (val > 65535) 710 val = 65535; 711 rtc_set_memory(s, 0x34, val); 712 rtc_set_memory(s, 0x35, val >> 8); 713 /* memory above 4GiB */ 714 val = x86ms->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); 718 719 object_property_add_link(OBJECT(pcms), "rtc_state", 720 TYPE_ISA_DEVICE, 721 (Object **)&x86ms->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); 726 727 set_boot_dev(s, MACHINE(pcms)->boot_order, &error_fatal); 728 729 val = 0; 730 val |= 0x02; /* FPU is there */ 731 val |= 0x04; /* PS/2 mouse installed */ 732 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val); 733 734 /* hard drives and FDC */ 735 arg.rtc_state = s; 736 arg.idebus[0] = idebus0; 737 arg.idebus[1] = idebus1; 738 qemu_register_reset(pc_cmos_init_late, &arg); 739 } 740 741 #define TYPE_PORT92 "port92" 742 #define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92) 743 744 /* port 92 stuff: could be split off */ 745 typedef struct Port92State { 746 ISADevice parent_obj; 747 748 MemoryRegion io; 749 uint8_t outport; 750 qemu_irq a20_out; 751 } Port92State; 752 753 static void port92_write(void *opaque, hwaddr addr, uint64_t val, 754 unsigned size) 755 { 756 Port92State *s = opaque; 757 int oldval = s->outport; 758 759 DPRINTF("port92: write 0x%02" PRIx64 "\n", val); 760 s->outport = 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); 764 } 765 } 766 767 static uint64_t port92_read(void *opaque, hwaddr addr, 768 unsigned size) 769 { 770 Port92State *s = opaque; 771 uint32_t ret; 772 773 ret = s->outport; 774 DPRINTF("port92: read 0x%02x\n", ret); 775 return ret; 776 } 777 778 static void port92_init(ISADevice *dev, qemu_irq a20_out) 779 { 780 qdev_connect_gpio_out_named(DEVICE(dev), PORT92_A20_LINE, 0, a20_out); 781 } 782 783 static const VMStateDescription vmstate_port92_isa = { 784 .name = "port92", 785 .version_id = 1, 786 .minimum_version_id = 1, 787 .fields = (VMStateField[]) { 788 VMSTATE_UINT8(outport, Port92State), 789 VMSTATE_END_OF_LIST() 790 } 791 }; 792 793 static void port92_reset(DeviceState *d) 794 { 795 Port92State *s = PORT92(d); 796 797 s->outport &= ~1; 798 } 799 800 static const MemoryRegionOps port92_ops = { 801 .read = port92_read, 802 .write = port92_write, 803 .impl = { 804 .min_access_size = 1, 805 .max_access_size = 1, 806 }, 807 .endianness = DEVICE_LITTLE_ENDIAN, 808 }; 809 810 static void port92_initfn(Object *obj) 811 { 812 Port92State *s = PORT92(obj); 813 814 memory_region_init_io(&s->io, OBJECT(s), &port92_ops, s, "port92", 1); 815 816 s->outport = 0; 817 818 qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, PORT92_A20_LINE, 1); 819 } 820 821 static void port92_realizefn(DeviceState *dev, Error **errp) 822 { 823 ISADevice *isadev = ISA_DEVICE(dev); 824 Port92State *s = PORT92(dev); 825 826 isa_register_ioport(isadev, &s->io, 0x92); 827 } 828 829 static void port92_class_initfn(ObjectClass *klass, void *data) 830 { 831 DeviceClass *dc = DEVICE_CLASS(klass); 832 833 dc->realize = port92_realizefn; 834 dc->reset = port92_reset; 835 dc->vmsd = &vmstate_port92_isa; 836 /* 837 * Reason: unlike ordinary ISA devices, this one needs additional 838 * wiring: its A20 output line needs to be wired up by 839 * port92_init(). 840 */ 841 dc->user_creatable = false; 842 } 843 844 static const TypeInfo port92_info = { 845 .name = TYPE_PORT92, 846 .parent = TYPE_ISA_DEVICE, 847 .instance_size = sizeof(Port92State), 848 .instance_init = port92_initfn, 849 .class_init = port92_class_initfn, 850 }; 851 852 static void port92_register_types(void) 853 { 854 type_register_static(&port92_info); 855 } 856 857 type_init(port92_register_types) 858 859 static void handle_a20_line_change(void *opaque, int irq, int level) 860 { 861 X86CPU *cpu = opaque; 862 863 /* XXX: send to all CPUs ? */ 864 /* XXX: add logic to handle multiple A20 line sources */ 865 x86_cpu_set_a20(cpu, level); 866 } 867 868 #define NE2000_NB_MAX 6 869 870 static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 871 0x280, 0x380 }; 872 static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 }; 873 874 void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd) 875 { 876 static int nb_ne2k = 0; 877 878 if (nb_ne2k == NE2000_NB_MAX) 879 return; 880 isa_ne2000_init(bus, ne2000_io[nb_ne2k], 881 ne2000_irq[nb_ne2k], nd); 882 nb_ne2k++; 883 } 884 885 DeviceState *cpu_get_current_apic(void) 886 { 887 if (current_cpu) { 888 X86CPU *cpu = X86_CPU(current_cpu); 889 return cpu->apic_state; 890 } else { 891 return NULL; 892 } 893 } 894 895 void pc_acpi_smi_interrupt(void *opaque, int irq, int level) 896 { 897 X86CPU *cpu = opaque; 898 899 if (level) { 900 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI); 901 } 902 } 903 904 /* 905 * This function is very similar to smp_parse() 906 * in hw/core/machine.c but includes CPU die support. 907 */ 908 void pc_smp_parse(MachineState *ms, QemuOpts *opts) 909 { 910 X86MachineState *x86ms = X86_MACHINE(ms); 911 912 if (opts) { 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); 918 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; 923 if (cpus == 0) { 924 sockets = sockets > 0 ? sockets : 1; 925 cpus = cores * threads * dies * sockets; 926 } else { 927 ms->smp.max_cpus = 928 qemu_opt_get_number(opts, "maxcpus", cpus); 929 sockets = ms->smp.max_cpus / (cores * threads * dies); 930 } 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) < " 941 "smp_cpus (%u)", 942 sockets, dies, cores, threads, cpus); 943 exit(1); 944 } 945 946 ms->smp.max_cpus = 947 qemu_opt_get_number(opts, "maxcpus", cpus); 948 949 if (ms->smp.max_cpus < cpus) { 950 error_report("maxcpus must be equal to or greater than smp"); 951 exit(1); 952 } 953 954 if (sockets * dies * cores * threads > ms->smp.max_cpus) { 955 error_report("cpu topology: " 956 "sockets (%u) * dies (%u) * cores (%u) * threads (%u) > " 957 "maxcpus (%u)", 958 sockets, dies, cores, threads, 959 ms->smp.max_cpus); 960 exit(1); 961 } 962 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) " 966 "!= maxcpus (%u)", 967 sockets, dies, cores, threads, 968 ms->smp.max_cpus); 969 } 970 971 ms->smp.cpus = cpus; 972 ms->smp.cores = cores; 973 ms->smp.threads = threads; 974 x86ms->smp_dies = dies; 975 } 976 977 if (ms->smp.cpus > 1) { 978 Error *blocker = NULL; 979 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp"); 980 replay_add_blocker(blocker); 981 } 982 } 983 984 void pc_hot_add_cpu(MachineState *ms, const int64_t id, Error **errp) 985 { 986 X86MachineState *x86ms = X86_MACHINE(ms); 987 int64_t apic_id = x86_cpu_apic_id_from_index(x86ms, id); 988 Error *local_err = NULL; 989 990 if (id < 0) { 991 error_setg(errp, "Invalid CPU id: %" PRIi64, id); 992 return; 993 } 994 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", 998 id, apic_id); 999 return; 1000 } 1001 1002 1003 x86_cpu_new(X86_MACHINE(ms), apic_id, &local_err); 1004 if (local_err) { 1005 error_propagate(errp, local_err); 1006 return; 1007 } 1008 } 1009 1010 static void rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count) 1011 { 1012 if (cpus_count > 0xff) { 1013 /* If the number of CPUs can't be represented in 8 bits, the 1014 * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just 1015 * to make old BIOSes fail more predictably. 1016 */ 1017 rtc_set_memory(rtc, 0x5f, 0); 1018 } else { 1019 rtc_set_memory(rtc, 0x5f, cpus_count - 1); 1020 } 1021 } 1022 1023 static 1024 void pc_machine_done(Notifier *notifier, void *data) 1025 { 1026 PCMachineState *pcms = container_of(notifier, 1027 PCMachineState, machine_done); 1028 X86MachineState *x86ms = X86_MACHINE(pcms); 1029 PCIBus *bus = pcms->bus; 1030 1031 /* set the number of CPUs */ 1032 rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus); 1033 1034 if (bus) { 1035 int extra_hosts = 0; 1036 1037 QLIST_FOREACH(bus, &bus->child, sibling) { 1038 /* look for expander root buses */ 1039 if (pci_bus_is_root(bus)) { 1040 extra_hosts++; 1041 } 1042 } 1043 if (extra_hosts && x86ms->fw_cfg) { 1044 uint64_t *val = g_malloc(sizeof(*val)); 1045 *val = cpu_to_le64(extra_hosts); 1046 fw_cfg_add_file(x86ms->fw_cfg, 1047 "etc/extra-pci-roots", val, sizeof(*val)); 1048 } 1049 } 1050 1051 acpi_setup(); 1052 if (x86ms->fw_cfg) { 1053 fw_cfg_build_smbios(MACHINE(pcms), x86ms->fw_cfg); 1054 fw_cfg_build_feature_control(MACHINE(pcms), x86ms->fw_cfg); 1055 /* update FW_CFG_NB_CPUS to account for -device added CPUs */ 1056 fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus); 1057 } 1058 1059 if (x86ms->apic_id_limit > 255 && !xen_enabled()) { 1060 IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default()); 1061 1062 if (!iommu || !x86_iommu_ir_supported(X86_IOMMU_DEVICE(iommu)) || 1063 iommu->intr_eim != ON_OFF_AUTO_ON) { 1064 error_report("current -smp configuration requires " 1065 "Extended Interrupt Mode enabled. " 1066 "You can add an IOMMU using: " 1067 "-device intel-iommu,intremap=on,eim=on"); 1068 exit(EXIT_FAILURE); 1069 } 1070 } 1071 } 1072 1073 void pc_guest_info_init(PCMachineState *pcms) 1074 { 1075 int i; 1076 MachineState *ms = MACHINE(pcms); 1077 X86MachineState *x86ms = X86_MACHINE(pcms); 1078 1079 x86ms->apic_xrupt_override = kvm_allows_irq0_override(); 1080 pcms->numa_nodes = ms->numa_state->num_nodes; 1081 pcms->node_mem = g_malloc0(pcms->numa_nodes * 1082 sizeof *pcms->node_mem); 1083 for (i = 0; i < ms->numa_state->num_nodes; i++) { 1084 pcms->node_mem[i] = ms->numa_state->nodes[i].node_mem; 1085 } 1086 1087 pcms->machine_done.notify = pc_machine_done; 1088 qemu_add_machine_init_done_notifier(&pcms->machine_done); 1089 } 1090 1091 /* setup pci memory address space mapping into system address space */ 1092 void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory, 1093 MemoryRegion *pci_address_space) 1094 { 1095 /* Set to lower priority than RAM */ 1096 memory_region_add_subregion_overlap(system_memory, 0x0, 1097 pci_address_space, -1); 1098 } 1099 1100 void xen_load_linux(PCMachineState *pcms) 1101 { 1102 int i; 1103 FWCfgState *fw_cfg; 1104 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 1105 X86MachineState *x86ms = X86_MACHINE(pcms); 1106 1107 assert(MACHINE(pcms)->kernel_filename != NULL); 1108 1109 fw_cfg = fw_cfg_init_io(FW_CFG_IO_BASE); 1110 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus); 1111 rom_set_fw(fw_cfg); 1112 1113 x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size, 1114 pcmc->pvh_enabled, pcmc->linuxboot_dma_enabled); 1115 for (i = 0; i < nb_option_roms; i++) { 1116 assert(!strcmp(option_rom[i].name, "linuxboot.bin") || 1117 !strcmp(option_rom[i].name, "linuxboot_dma.bin") || 1118 !strcmp(option_rom[i].name, "pvh.bin") || 1119 !strcmp(option_rom[i].name, "multiboot.bin")); 1120 rom_add_option(option_rom[i].name, option_rom[i].bootindex); 1121 } 1122 x86ms->fw_cfg = fw_cfg; 1123 } 1124 1125 void pc_memory_init(PCMachineState *pcms, 1126 MemoryRegion *system_memory, 1127 MemoryRegion *rom_memory, 1128 MemoryRegion **ram_memory) 1129 { 1130 int linux_boot, i; 1131 MemoryRegion *ram, *option_rom_mr; 1132 MemoryRegion *ram_below_4g, *ram_above_4g; 1133 FWCfgState *fw_cfg; 1134 MachineState *machine = MACHINE(pcms); 1135 MachineClass *mc = MACHINE_GET_CLASS(machine); 1136 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 1137 X86MachineState *x86ms = X86_MACHINE(pcms); 1138 1139 assert(machine->ram_size == x86ms->below_4g_mem_size + 1140 x86ms->above_4g_mem_size); 1141 1142 linux_boot = (machine->kernel_filename != NULL); 1143 1144 /* Allocate RAM. We allocate it as a single memory region and use 1145 * aliases to address portions of it, mostly for backwards compatibility 1146 * with older qemus that used qemu_ram_alloc(). 1147 */ 1148 ram = g_malloc(sizeof(*ram)); 1149 memory_region_allocate_system_memory(ram, NULL, "pc.ram", 1150 machine->ram_size); 1151 *ram_memory = ram; 1152 ram_below_4g = g_malloc(sizeof(*ram_below_4g)); 1153 memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", ram, 1154 0, x86ms->below_4g_mem_size); 1155 memory_region_add_subregion(system_memory, 0, ram_below_4g); 1156 e820_add_entry(0, x86ms->below_4g_mem_size, E820_RAM); 1157 if (x86ms->above_4g_mem_size > 0) { 1158 ram_above_4g = g_malloc(sizeof(*ram_above_4g)); 1159 memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", ram, 1160 x86ms->below_4g_mem_size, 1161 x86ms->above_4g_mem_size); 1162 memory_region_add_subregion(system_memory, 0x100000000ULL, 1163 ram_above_4g); 1164 e820_add_entry(0x100000000ULL, x86ms->above_4g_mem_size, E820_RAM); 1165 } 1166 1167 if (!pcmc->has_reserved_memory && 1168 (machine->ram_slots || 1169 (machine->maxram_size > machine->ram_size))) { 1170 1171 error_report("\"-memory 'slots|maxmem'\" is not supported by: %s", 1172 mc->name); 1173 exit(EXIT_FAILURE); 1174 } 1175 1176 /* always allocate the device memory information */ 1177 machine->device_memory = g_malloc0(sizeof(*machine->device_memory)); 1178 1179 /* initialize device memory address space */ 1180 if (pcmc->has_reserved_memory && 1181 (machine->ram_size < machine->maxram_size)) { 1182 ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size; 1183 1184 if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) { 1185 error_report("unsupported amount of memory slots: %"PRIu64, 1186 machine->ram_slots); 1187 exit(EXIT_FAILURE); 1188 } 1189 1190 if (QEMU_ALIGN_UP(machine->maxram_size, 1191 TARGET_PAGE_SIZE) != machine->maxram_size) { 1192 error_report("maximum memory size must by aligned to multiple of " 1193 "%d bytes", TARGET_PAGE_SIZE); 1194 exit(EXIT_FAILURE); 1195 } 1196 1197 machine->device_memory->base = 1198 ROUND_UP(0x100000000ULL + x86ms->above_4g_mem_size, 1 * GiB); 1199 1200 if (pcmc->enforce_aligned_dimm) { 1201 /* size device region assuming 1G page max alignment per slot */ 1202 device_mem_size += (1 * GiB) * machine->ram_slots; 1203 } 1204 1205 if ((machine->device_memory->base + device_mem_size) < 1206 device_mem_size) { 1207 error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT, 1208 machine->maxram_size); 1209 exit(EXIT_FAILURE); 1210 } 1211 1212 memory_region_init(&machine->device_memory->mr, OBJECT(pcms), 1213 "device-memory", device_mem_size); 1214 memory_region_add_subregion(system_memory, machine->device_memory->base, 1215 &machine->device_memory->mr); 1216 } 1217 1218 /* Initialize PC system firmware */ 1219 pc_system_firmware_init(pcms, rom_memory); 1220 1221 option_rom_mr = g_malloc(sizeof(*option_rom_mr)); 1222 memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE, 1223 &error_fatal); 1224 if (pcmc->pci_enabled) { 1225 memory_region_set_readonly(option_rom_mr, true); 1226 } 1227 memory_region_add_subregion_overlap(rom_memory, 1228 PC_ROM_MIN_VGA, 1229 option_rom_mr, 1230 1); 1231 1232 fw_cfg = fw_cfg_arch_create(machine, 1233 x86ms->boot_cpus, x86ms->apic_id_limit); 1234 1235 rom_set_fw(fw_cfg); 1236 1237 if (pcmc->has_reserved_memory && machine->device_memory->base) { 1238 uint64_t *val = g_malloc(sizeof(*val)); 1239 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 1240 uint64_t res_mem_end = machine->device_memory->base; 1241 1242 if (!pcmc->broken_reserved_end) { 1243 res_mem_end += memory_region_size(&machine->device_memory->mr); 1244 } 1245 *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB)); 1246 fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val)); 1247 } 1248 1249 if (linux_boot) { 1250 x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size, 1251 pcmc->pvh_enabled, pcmc->linuxboot_dma_enabled); 1252 } 1253 1254 for (i = 0; i < nb_option_roms; i++) { 1255 rom_add_option(option_rom[i].name, option_rom[i].bootindex); 1256 } 1257 x86ms->fw_cfg = fw_cfg; 1258 1259 /* Init default IOAPIC address space */ 1260 x86ms->ioapic_as = &address_space_memory; 1261 1262 /* Init ACPI memory hotplug IO base address */ 1263 pcms->memhp_io_base = ACPI_MEMORY_HOTPLUG_BASE; 1264 } 1265 1266 /* 1267 * The 64bit pci hole starts after "above 4G RAM" and 1268 * potentially the space reserved for memory hotplug. 1269 */ 1270 uint64_t pc_pci_hole64_start(void) 1271 { 1272 PCMachineState *pcms = PC_MACHINE(qdev_get_machine()); 1273 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 1274 MachineState *ms = MACHINE(pcms); 1275 X86MachineState *x86ms = X86_MACHINE(pcms); 1276 uint64_t hole64_start = 0; 1277 1278 if (pcmc->has_reserved_memory && ms->device_memory->base) { 1279 hole64_start = ms->device_memory->base; 1280 if (!pcmc->broken_reserved_end) { 1281 hole64_start += memory_region_size(&ms->device_memory->mr); 1282 } 1283 } else { 1284 hole64_start = 0x100000000ULL + x86ms->above_4g_mem_size; 1285 } 1286 1287 return ROUND_UP(hole64_start, 1 * GiB); 1288 } 1289 1290 qemu_irq pc_allocate_cpu_irq(void) 1291 { 1292 return qemu_allocate_irq(pic_irq_request, NULL, 0); 1293 } 1294 1295 DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus) 1296 { 1297 DeviceState *dev = NULL; 1298 1299 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA); 1300 if (pci_bus) { 1301 PCIDevice *pcidev = pci_vga_init(pci_bus); 1302 dev = pcidev ? &pcidev->qdev : NULL; 1303 } else if (isa_bus) { 1304 ISADevice *isadev = isa_vga_init(isa_bus); 1305 dev = isadev ? DEVICE(isadev) : NULL; 1306 } 1307 rom_reset_order_override(); 1308 return dev; 1309 } 1310 1311 static const MemoryRegionOps ioport80_io_ops = { 1312 .write = ioport80_write, 1313 .read = ioport80_read, 1314 .endianness = DEVICE_NATIVE_ENDIAN, 1315 .impl = { 1316 .min_access_size = 1, 1317 .max_access_size = 1, 1318 }, 1319 }; 1320 1321 static const MemoryRegionOps ioportF0_io_ops = { 1322 .write = ioportF0_write, 1323 .read = ioportF0_read, 1324 .endianness = DEVICE_NATIVE_ENDIAN, 1325 .impl = { 1326 .min_access_size = 1, 1327 .max_access_size = 1, 1328 }, 1329 }; 1330 1331 static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl, bool no_vmport) 1332 { 1333 int i; 1334 DriveInfo *fd[MAX_FD]; 1335 qemu_irq *a20_line; 1336 ISADevice *i8042, *port92, *vmmouse; 1337 1338 serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS); 1339 parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS); 1340 1341 for (i = 0; i < MAX_FD; i++) { 1342 fd[i] = drive_get(IF_FLOPPY, 0, i); 1343 create_fdctrl |= !!fd[i]; 1344 } 1345 if (create_fdctrl) { 1346 fdctrl_init_isa(isa_bus, fd); 1347 } 1348 1349 i8042 = isa_create_simple(isa_bus, "i8042"); 1350 if (!no_vmport) { 1351 vmport_init(isa_bus); 1352 vmmouse = isa_try_create(isa_bus, "vmmouse"); 1353 } else { 1354 vmmouse = NULL; 1355 } 1356 if (vmmouse) { 1357 DeviceState *dev = DEVICE(vmmouse); 1358 qdev_prop_set_ptr(dev, "ps2_mouse", i8042); 1359 qdev_init_nofail(dev); 1360 } 1361 port92 = isa_create_simple(isa_bus, "port92"); 1362 1363 a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2); 1364 i8042_setup_a20_line(i8042, a20_line[0]); 1365 port92_init(port92, a20_line[1]); 1366 g_free(a20_line); 1367 } 1368 1369 void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi, 1370 ISADevice **rtc_state, 1371 bool create_fdctrl, 1372 bool no_vmport, 1373 bool has_pit, 1374 uint32_t hpet_irqs) 1375 { 1376 int i; 1377 DeviceState *hpet = NULL; 1378 int pit_isa_irq = 0; 1379 qemu_irq pit_alt_irq = NULL; 1380 qemu_irq rtc_irq = NULL; 1381 ISADevice *pit = NULL; 1382 MemoryRegion *ioport80_io = g_new(MemoryRegion, 1); 1383 MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1); 1384 1385 memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1); 1386 memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io); 1387 1388 memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1); 1389 memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io); 1390 1391 /* 1392 * Check if an HPET shall be created. 1393 * 1394 * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT 1395 * when the HPET wants to take over. Thus we have to disable the latter. 1396 */ 1397 if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) { 1398 /* In order to set property, here not using sysbus_try_create_simple */ 1399 hpet = qdev_try_create(NULL, TYPE_HPET); 1400 if (hpet) { 1401 /* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7 1402 * and earlier, use IRQ2 for compat. Otherwise, use IRQ16~23, 1403 * IRQ8 and IRQ2. 1404 */ 1405 uint8_t compat = object_property_get_uint(OBJECT(hpet), 1406 HPET_INTCAP, NULL); 1407 if (!compat) { 1408 qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs); 1409 } 1410 qdev_init_nofail(hpet); 1411 sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE); 1412 1413 for (i = 0; i < GSI_NUM_PINS; i++) { 1414 sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]); 1415 } 1416 pit_isa_irq = -1; 1417 pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT); 1418 rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT); 1419 } 1420 } 1421 *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq); 1422 1423 qemu_register_boot_set(pc_boot_set, *rtc_state); 1424 1425 if (!xen_enabled() && has_pit) { 1426 if (kvm_pit_in_kernel()) { 1427 pit = kvm_pit_init(isa_bus, 0x40); 1428 } else { 1429 pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq); 1430 } 1431 if (hpet) { 1432 /* connect PIT to output control line of the HPET */ 1433 qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0)); 1434 } 1435 pcspk_init(isa_bus, pit); 1436 } 1437 1438 i8257_dma_init(isa_bus, 0); 1439 1440 /* Super I/O */ 1441 pc_superio_init(isa_bus, create_fdctrl, no_vmport); 1442 } 1443 1444 void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus) 1445 { 1446 int i; 1447 1448 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC); 1449 for (i = 0; i < nb_nics; i++) { 1450 NICInfo *nd = &nd_table[i]; 1451 const char *model = nd->model ? nd->model : pcmc->default_nic_model; 1452 1453 if (g_str_equal(model, "ne2k_isa")) { 1454 pc_init_ne2k_isa(isa_bus, nd); 1455 } else { 1456 pci_nic_init_nofail(nd, pci_bus, model, NULL); 1457 } 1458 } 1459 rom_reset_order_override(); 1460 } 1461 1462 void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name) 1463 { 1464 DeviceState *dev; 1465 SysBusDevice *d; 1466 unsigned int i; 1467 1468 if (kvm_ioapic_in_kernel()) { 1469 dev = qdev_create(NULL, TYPE_KVM_IOAPIC); 1470 } else { 1471 dev = qdev_create(NULL, TYPE_IOAPIC); 1472 } 1473 if (parent_name) { 1474 object_property_add_child(object_resolve_path(parent_name, NULL), 1475 "ioapic", OBJECT(dev), NULL); 1476 } 1477 qdev_init_nofail(dev); 1478 d = SYS_BUS_DEVICE(dev); 1479 sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS); 1480 1481 for (i = 0; i < IOAPIC_NUM_PINS; i++) { 1482 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i); 1483 } 1484 } 1485 1486 static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev, 1487 Error **errp) 1488 { 1489 const PCMachineState *pcms = PC_MACHINE(hotplug_dev); 1490 const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms); 1491 const MachineState *ms = MACHINE(hotplug_dev); 1492 const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM); 1493 const uint64_t legacy_align = TARGET_PAGE_SIZE; 1494 Error *local_err = NULL; 1495 1496 /* 1497 * When -no-acpi is used with Q35 machine type, no ACPI is built, 1498 * but pcms->acpi_dev is still created. Check !acpi_enabled in 1499 * addition to cover this case. 1500 */ 1501 if (!pcms->acpi_dev || !acpi_enabled) { 1502 error_setg(errp, 1503 "memory hotplug is not enabled: missing acpi device or acpi disabled"); 1504 return; 1505 } 1506 1507 if (is_nvdimm && !ms->nvdimms_state->is_enabled) { 1508 error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'"); 1509 return; 1510 } 1511 1512 hotplug_handler_pre_plug(pcms->acpi_dev, dev, &local_err); 1513 if (local_err) { 1514 error_propagate(errp, local_err); 1515 return; 1516 } 1517 1518 pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev), 1519 pcmc->enforce_aligned_dimm ? NULL : &legacy_align, errp); 1520 } 1521 1522 static void pc_memory_plug(HotplugHandler *hotplug_dev, 1523 DeviceState *dev, Error **errp) 1524 { 1525 Error *local_err = NULL; 1526 PCMachineState *pcms = PC_MACHINE(hotplug_dev); 1527 MachineState *ms = MACHINE(hotplug_dev); 1528 bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM); 1529 1530 pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms), &local_err); 1531 if (local_err) { 1532 goto out; 1533 } 1534 1535 if (is_nvdimm) { 1536 nvdimm_plug(ms->nvdimms_state); 1537 } 1538 1539 hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &error_abort); 1540 out: 1541 error_propagate(errp, local_err); 1542 } 1543 1544 static void pc_memory_unplug_request(HotplugHandler *hotplug_dev, 1545 DeviceState *dev, Error **errp) 1546 { 1547 Error *local_err = NULL; 1548 PCMachineState *pcms = PC_MACHINE(hotplug_dev); 1549 1550 /* 1551 * When -no-acpi is used with Q35 machine type, no ACPI is built, 1552 * but pcms->acpi_dev is still created. Check !acpi_enabled in 1553 * addition to cover this case. 1554 */ 1555 if (!pcms->acpi_dev || !acpi_enabled) { 1556 error_setg(&local_err, 1557 "memory hotplug is not enabled: missing acpi device or acpi disabled"); 1558 goto out; 1559 } 1560 1561 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) { 1562 error_setg(&local_err, 1563 "nvdimm device hot unplug is not supported yet."); 1564 goto out; 1565 } 1566 1567 hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, 1568 &local_err); 1569 out: 1570 error_propagate(errp, local_err); 1571 } 1572 1573 static void pc_memory_unplug(HotplugHandler *hotplug_dev, 1574 DeviceState *dev, Error **errp) 1575 { 1576 PCMachineState *pcms = PC_MACHINE(hotplug_dev); 1577 Error *local_err = NULL; 1578 1579 hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err); 1580 if (local_err) { 1581 goto out; 1582 } 1583 1584 pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms)); 1585 object_property_set_bool(OBJECT(dev), false, "realized", NULL); 1586 out: 1587 error_propagate(errp, local_err); 1588 } 1589 1590 static int pc_apic_cmp(const void *a, const void *b) 1591 { 1592 CPUArchId *apic_a = (CPUArchId *)a; 1593 CPUArchId *apic_b = (CPUArchId *)b; 1594 1595 return apic_a->arch_id - apic_b->arch_id; 1596 } 1597 1598 /* returns pointer to CPUArchId descriptor that matches CPU's apic_id 1599 * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no 1600 * entry corresponding to CPU's apic_id returns NULL. 1601 */ 1602 static CPUArchId *pc_find_cpu_slot(MachineState *ms, uint32_t id, int *idx) 1603 { 1604 CPUArchId apic_id, *found_cpu; 1605 1606 apic_id.arch_id = id; 1607 found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus, 1608 ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus), 1609 pc_apic_cmp); 1610 if (found_cpu && idx) { 1611 *idx = found_cpu - ms->possible_cpus->cpus; 1612 } 1613 return found_cpu; 1614 } 1615 1616 static void pc_cpu_plug(HotplugHandler *hotplug_dev, 1617 DeviceState *dev, Error **errp) 1618 { 1619 CPUArchId *found_cpu; 1620 Error *local_err = NULL; 1621 X86CPU *cpu = X86_CPU(dev); 1622 PCMachineState *pcms = PC_MACHINE(hotplug_dev); 1623 X86MachineState *x86ms = X86_MACHINE(pcms); 1624 1625 if (pcms->acpi_dev) { 1626 hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err); 1627 if (local_err) { 1628 goto out; 1629 } 1630 } 1631 1632 /* increment the number of CPUs */ 1633 x86ms->boot_cpus++; 1634 if (x86ms->rtc) { 1635 rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus); 1636 } 1637 if (x86ms->fw_cfg) { 1638 fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus); 1639 } 1640 1641 found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL); 1642 found_cpu->cpu = OBJECT(dev); 1643 out: 1644 error_propagate(errp, local_err); 1645 } 1646 static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev, 1647 DeviceState *dev, Error **errp) 1648 { 1649 int idx = -1; 1650 Error *local_err = NULL; 1651 X86CPU *cpu = X86_CPU(dev); 1652 PCMachineState *pcms = PC_MACHINE(hotplug_dev); 1653 1654 if (!pcms->acpi_dev) { 1655 error_setg(&local_err, "CPU hot unplug not supported without ACPI"); 1656 goto out; 1657 } 1658 1659 pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx); 1660 assert(idx != -1); 1661 if (idx == 0) { 1662 error_setg(&local_err, "Boot CPU is unpluggable"); 1663 goto out; 1664 } 1665 1666 hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev, 1667 &local_err); 1668 if (local_err) { 1669 goto out; 1670 } 1671 1672 out: 1673 error_propagate(errp, local_err); 1674 1675 } 1676 1677 static void pc_cpu_unplug_cb(HotplugHandler *hotplug_dev, 1678 DeviceState *dev, Error **errp) 1679 { 1680 CPUArchId *found_cpu; 1681 Error *local_err = NULL; 1682 X86CPU *cpu = X86_CPU(dev); 1683 PCMachineState *pcms = PC_MACHINE(hotplug_dev); 1684 X86MachineState *x86ms = X86_MACHINE(pcms); 1685 1686 hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err); 1687 if (local_err) { 1688 goto out; 1689 } 1690 1691 found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL); 1692 found_cpu->cpu = NULL; 1693 object_property_set_bool(OBJECT(dev), false, "realized", NULL); 1694 1695 /* decrement the number of CPUs */ 1696 x86ms->boot_cpus--; 1697 /* Update the number of CPUs in CMOS */ 1698 rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus); 1699 fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus); 1700 out: 1701 error_propagate(errp, local_err); 1702 } 1703 1704 static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev, 1705 DeviceState *dev, Error **errp) 1706 { 1707 int idx; 1708 CPUState *cs; 1709 CPUArchId *cpu_slot; 1710 X86CPUTopoInfo topo; 1711 X86CPU *cpu = X86_CPU(dev); 1712 CPUX86State *env = &cpu->env; 1713 MachineState *ms = MACHINE(hotplug_dev); 1714 PCMachineState *pcms = PC_MACHINE(hotplug_dev); 1715 X86MachineState *x86ms = X86_MACHINE(pcms); 1716 unsigned int smp_cores = ms->smp.cores; 1717 unsigned int smp_threads = ms->smp.threads; 1718 1719 if(!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) { 1720 error_setg(errp, "Invalid CPU type, expected cpu type: '%s'", 1721 ms->cpu_type); 1722 return; 1723 } 1724 1725 env->nr_dies = x86ms->smp_dies; 1726 1727 /* 1728 * If APIC ID is not set, 1729 * set it based on socket/die/core/thread properties. 1730 */ 1731 if (cpu->apic_id == UNASSIGNED_APIC_ID) { 1732 int max_socket = (ms->smp.max_cpus - 1) / 1733 smp_threads / smp_cores / x86ms->smp_dies; 1734 1735 /* 1736 * die-id was optional in QEMU 4.0 and older, so keep it optional 1737 * if there's only one die per socket. 1738 */ 1739 if (cpu->die_id < 0 && x86ms->smp_dies == 1) { 1740 cpu->die_id = 0; 1741 } 1742 1743 if (cpu->socket_id < 0) { 1744 error_setg(errp, "CPU socket-id is not set"); 1745 return; 1746 } else if (cpu->socket_id > max_socket) { 1747 error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u", 1748 cpu->socket_id, max_socket); 1749 return; 1750 } 1751 if (cpu->die_id < 0) { 1752 error_setg(errp, "CPU die-id is not set"); 1753 return; 1754 } else if (cpu->die_id > x86ms->smp_dies - 1) { 1755 error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u", 1756 cpu->die_id, x86ms->smp_dies - 1); 1757 return; 1758 } 1759 if (cpu->core_id < 0) { 1760 error_setg(errp, "CPU core-id is not set"); 1761 return; 1762 } else if (cpu->core_id > (smp_cores - 1)) { 1763 error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u", 1764 cpu->core_id, smp_cores - 1); 1765 return; 1766 } 1767 if (cpu->thread_id < 0) { 1768 error_setg(errp, "CPU thread-id is not set"); 1769 return; 1770 } else if (cpu->thread_id > (smp_threads - 1)) { 1771 error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u", 1772 cpu->thread_id, smp_threads - 1); 1773 return; 1774 } 1775 1776 topo.pkg_id = cpu->socket_id; 1777 topo.die_id = cpu->die_id; 1778 topo.core_id = cpu->core_id; 1779 topo.smt_id = cpu->thread_id; 1780 cpu->apic_id = apicid_from_topo_ids(x86ms->smp_dies, smp_cores, 1781 smp_threads, &topo); 1782 } 1783 1784 cpu_slot = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx); 1785 if (!cpu_slot) { 1786 MachineState *ms = MACHINE(pcms); 1787 1788 x86_topo_ids_from_apicid(cpu->apic_id, x86ms->smp_dies, 1789 smp_cores, smp_threads, &topo); 1790 error_setg(errp, 1791 "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with" 1792 " APIC ID %" PRIu32 ", valid index range 0:%d", 1793 topo.pkg_id, topo.die_id, topo.core_id, topo.smt_id, 1794 cpu->apic_id, ms->possible_cpus->len - 1); 1795 return; 1796 } 1797 1798 if (cpu_slot->cpu) { 1799 error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists", 1800 idx, cpu->apic_id); 1801 return; 1802 } 1803 1804 /* if 'address' properties socket-id/core-id/thread-id are not set, set them 1805 * so that machine_query_hotpluggable_cpus would show correct values 1806 */ 1807 /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn() 1808 * once -smp refactoring is complete and there will be CPU private 1809 * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */ 1810 x86_topo_ids_from_apicid(cpu->apic_id, x86ms->smp_dies, 1811 smp_cores, smp_threads, &topo); 1812 if (cpu->socket_id != -1 && cpu->socket_id != topo.pkg_id) { 1813 error_setg(errp, "property socket-id: %u doesn't match set apic-id:" 1814 " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id, topo.pkg_id); 1815 return; 1816 } 1817 cpu->socket_id = topo.pkg_id; 1818 1819 if (cpu->die_id != -1 && cpu->die_id != topo.die_id) { 1820 error_setg(errp, "property die-id: %u doesn't match set apic-id:" 1821 " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo.die_id); 1822 return; 1823 } 1824 cpu->die_id = topo.die_id; 1825 1826 if (cpu->core_id != -1 && cpu->core_id != topo.core_id) { 1827 error_setg(errp, "property core-id: %u doesn't match set apic-id:" 1828 " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id, topo.core_id); 1829 return; 1830 } 1831 cpu->core_id = topo.core_id; 1832 1833 if (cpu->thread_id != -1 && cpu->thread_id != topo.smt_id) { 1834 error_setg(errp, "property thread-id: %u doesn't match set apic-id:" 1835 " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id, topo.smt_id); 1836 return; 1837 } 1838 cpu->thread_id = topo.smt_id; 1839 1840 if (hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) && 1841 !kvm_hv_vpindex_settable()) { 1842 error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX"); 1843 return; 1844 } 1845 1846 cs = CPU(cpu); 1847 cs->cpu_index = idx; 1848 1849 numa_cpu_pre_plug(cpu_slot, dev, errp); 1850 } 1851 1852 static void pc_virtio_pmem_pci_pre_plug(HotplugHandler *hotplug_dev, 1853 DeviceState *dev, Error **errp) 1854 { 1855 HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev); 1856 Error *local_err = NULL; 1857 1858 if (!hotplug_dev2) { 1859 /* 1860 * Without a bus hotplug handler, we cannot control the plug/unplug 1861 * order. This should never be the case on x86, however better add 1862 * a safety net. 1863 */ 1864 error_setg(errp, "virtio-pmem-pci not supported on this bus."); 1865 return; 1866 } 1867 /* 1868 * First, see if we can plug this memory device at all. If that 1869 * succeeds, branch of to the actual hotplug handler. 1870 */ 1871 memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL, 1872 &local_err); 1873 if (!local_err) { 1874 hotplug_handler_pre_plug(hotplug_dev2, dev, &local_err); 1875 } 1876 error_propagate(errp, local_err); 1877 } 1878 1879 static void pc_virtio_pmem_pci_plug(HotplugHandler *hotplug_dev, 1880 DeviceState *dev, Error **errp) 1881 { 1882 HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev); 1883 Error *local_err = NULL; 1884 1885 /* 1886 * Plug the memory device first and then branch off to the actual 1887 * hotplug handler. If that one fails, we can easily undo the memory 1888 * device bits. 1889 */ 1890 memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev)); 1891 hotplug_handler_plug(hotplug_dev2, dev, &local_err); 1892 if (local_err) { 1893 memory_device_unplug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev)); 1894 } 1895 error_propagate(errp, local_err); 1896 } 1897 1898 static void pc_virtio_pmem_pci_unplug_request(HotplugHandler *hotplug_dev, 1899 DeviceState *dev, Error **errp) 1900 { 1901 /* We don't support virtio pmem hot unplug */ 1902 error_setg(errp, "virtio pmem device unplug not supported."); 1903 } 1904 1905 static void pc_virtio_pmem_pci_unplug(HotplugHandler *hotplug_dev, 1906 DeviceState *dev, Error **errp) 1907 { 1908 /* We don't support virtio pmem hot unplug */ 1909 } 1910 1911 static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev, 1912 DeviceState *dev, Error **errp) 1913 { 1914 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 1915 pc_memory_pre_plug(hotplug_dev, dev, errp); 1916 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 1917 pc_cpu_pre_plug(hotplug_dev, dev, errp); 1918 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) { 1919 pc_virtio_pmem_pci_pre_plug(hotplug_dev, dev, errp); 1920 } 1921 } 1922 1923 static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev, 1924 DeviceState *dev, Error **errp) 1925 { 1926 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 1927 pc_memory_plug(hotplug_dev, dev, errp); 1928 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 1929 pc_cpu_plug(hotplug_dev, dev, errp); 1930 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) { 1931 pc_virtio_pmem_pci_plug(hotplug_dev, dev, errp); 1932 } 1933 } 1934 1935 static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev, 1936 DeviceState *dev, Error **errp) 1937 { 1938 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 1939 pc_memory_unplug_request(hotplug_dev, dev, errp); 1940 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 1941 pc_cpu_unplug_request_cb(hotplug_dev, dev, errp); 1942 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) { 1943 pc_virtio_pmem_pci_unplug_request(hotplug_dev, dev, errp); 1944 } else { 1945 error_setg(errp, "acpi: device unplug request for not supported device" 1946 " type: %s", object_get_typename(OBJECT(dev))); 1947 } 1948 } 1949 1950 static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev, 1951 DeviceState *dev, Error **errp) 1952 { 1953 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 1954 pc_memory_unplug(hotplug_dev, dev, errp); 1955 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 1956 pc_cpu_unplug_cb(hotplug_dev, dev, errp); 1957 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) { 1958 pc_virtio_pmem_pci_unplug(hotplug_dev, dev, errp); 1959 } else { 1960 error_setg(errp, "acpi: device unplug for not supported device" 1961 " type: %s", object_get_typename(OBJECT(dev))); 1962 } 1963 } 1964 1965 static HotplugHandler *pc_get_hotplug_handler(MachineState *machine, 1966 DeviceState *dev) 1967 { 1968 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) || 1969 object_dynamic_cast(OBJECT(dev), TYPE_CPU) || 1970 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) { 1971 return HOTPLUG_HANDLER(machine); 1972 } 1973 1974 return NULL; 1975 } 1976 1977 static void 1978 pc_machine_get_device_memory_region_size(Object *obj, Visitor *v, 1979 const char *name, void *opaque, 1980 Error **errp) 1981 { 1982 MachineState *ms = MACHINE(obj); 1983 int64_t value = 0; 1984 1985 if (ms->device_memory) { 1986 value = memory_region_size(&ms->device_memory->mr); 1987 } 1988 1989 visit_type_int(v, name, &value, errp); 1990 } 1991 1992 static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name, 1993 void *opaque, Error **errp) 1994 { 1995 PCMachineState *pcms = PC_MACHINE(obj); 1996 OnOffAuto vmport = pcms->vmport; 1997 1998 visit_type_OnOffAuto(v, name, &vmport, errp); 1999 } 2000 2001 static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name, 2002 void *opaque, Error **errp) 2003 { 2004 PCMachineState *pcms = PC_MACHINE(obj); 2005 2006 visit_type_OnOffAuto(v, name, &pcms->vmport, errp); 2007 } 2008 2009 bool pc_machine_is_smm_enabled(PCMachineState *pcms) 2010 { 2011 bool smm_available = false; 2012 2013 if (pcms->smm == ON_OFF_AUTO_OFF) { 2014 return false; 2015 } 2016 2017 if (tcg_enabled() || qtest_enabled()) { 2018 smm_available = true; 2019 } else if (kvm_enabled()) { 2020 smm_available = kvm_has_smm(); 2021 } 2022 2023 if (smm_available) { 2024 return true; 2025 } 2026 2027 if (pcms->smm == ON_OFF_AUTO_ON) { 2028 error_report("System Management Mode not supported by this hypervisor."); 2029 exit(1); 2030 } 2031 return false; 2032 } 2033 2034 static void pc_machine_get_smm(Object *obj, Visitor *v, const char *name, 2035 void *opaque, Error **errp) 2036 { 2037 PCMachineState *pcms = PC_MACHINE(obj); 2038 OnOffAuto smm = pcms->smm; 2039 2040 visit_type_OnOffAuto(v, name, &smm, errp); 2041 } 2042 2043 static void pc_machine_set_smm(Object *obj, Visitor *v, const char *name, 2044 void *opaque, Error **errp) 2045 { 2046 PCMachineState *pcms = PC_MACHINE(obj); 2047 2048 visit_type_OnOffAuto(v, name, &pcms->smm, errp); 2049 } 2050 2051 static bool pc_machine_get_smbus(Object *obj, Error **errp) 2052 { 2053 PCMachineState *pcms = PC_MACHINE(obj); 2054 2055 return pcms->smbus_enabled; 2056 } 2057 2058 static void pc_machine_set_smbus(Object *obj, bool value, Error **errp) 2059 { 2060 PCMachineState *pcms = PC_MACHINE(obj); 2061 2062 pcms->smbus_enabled = value; 2063 } 2064 2065 static bool pc_machine_get_sata(Object *obj, Error **errp) 2066 { 2067 PCMachineState *pcms = PC_MACHINE(obj); 2068 2069 return pcms->sata_enabled; 2070 } 2071 2072 static void pc_machine_set_sata(Object *obj, bool value, Error **errp) 2073 { 2074 PCMachineState *pcms = PC_MACHINE(obj); 2075 2076 pcms->sata_enabled = value; 2077 } 2078 2079 static bool pc_machine_get_pit(Object *obj, Error **errp) 2080 { 2081 PCMachineState *pcms = PC_MACHINE(obj); 2082 2083 return pcms->pit_enabled; 2084 } 2085 2086 static void pc_machine_set_pit(Object *obj, bool value, Error **errp) 2087 { 2088 PCMachineState *pcms = PC_MACHINE(obj); 2089 2090 pcms->pit_enabled = value; 2091 } 2092 2093 static void pc_machine_initfn(Object *obj) 2094 { 2095 PCMachineState *pcms = PC_MACHINE(obj); 2096 2097 pcms->smm = ON_OFF_AUTO_AUTO; 2098 #ifdef CONFIG_VMPORT 2099 pcms->vmport = ON_OFF_AUTO_AUTO; 2100 #else 2101 pcms->vmport = ON_OFF_AUTO_OFF; 2102 #endif /* CONFIG_VMPORT */ 2103 /* acpi build is enabled by default if machine supports it */ 2104 pcms->acpi_build_enabled = PC_MACHINE_GET_CLASS(pcms)->has_acpi_build; 2105 pcms->smbus_enabled = true; 2106 pcms->sata_enabled = true; 2107 pcms->pit_enabled = true; 2108 2109 pc_system_flash_create(pcms); 2110 } 2111 2112 static void pc_machine_reset(MachineState *machine) 2113 { 2114 CPUState *cs; 2115 X86CPU *cpu; 2116 2117 qemu_devices_reset(); 2118 2119 /* Reset APIC after devices have been reset to cancel 2120 * any changes that qemu_devices_reset() might have done. 2121 */ 2122 CPU_FOREACH(cs) { 2123 cpu = X86_CPU(cs); 2124 2125 if (cpu->apic_state) { 2126 device_reset(cpu->apic_state); 2127 } 2128 } 2129 } 2130 2131 static void pc_machine_wakeup(MachineState *machine) 2132 { 2133 cpu_synchronize_all_states(); 2134 pc_machine_reset(machine); 2135 cpu_synchronize_all_post_reset(); 2136 } 2137 2138 static bool pc_hotplug_allowed(MachineState *ms, DeviceState *dev, Error **errp) 2139 { 2140 X86IOMMUState *iommu = x86_iommu_get_default(); 2141 IntelIOMMUState *intel_iommu; 2142 2143 if (iommu && 2144 object_dynamic_cast((Object *)iommu, TYPE_INTEL_IOMMU_DEVICE) && 2145 object_dynamic_cast((Object *)dev, "vfio-pci")) { 2146 intel_iommu = INTEL_IOMMU_DEVICE(iommu); 2147 if (!intel_iommu->caching_mode) { 2148 error_setg(errp, "Device assignment is not allowed without " 2149 "enabling caching-mode=on for Intel IOMMU."); 2150 return false; 2151 } 2152 } 2153 2154 return true; 2155 } 2156 2157 static void pc_machine_class_init(ObjectClass *oc, void *data) 2158 { 2159 MachineClass *mc = MACHINE_CLASS(oc); 2160 PCMachineClass *pcmc = PC_MACHINE_CLASS(oc); 2161 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc); 2162 2163 pcmc->pci_enabled = true; 2164 pcmc->has_acpi_build = true; 2165 pcmc->rsdp_in_ram = true; 2166 pcmc->smbios_defaults = true; 2167 pcmc->smbios_uuid_encoded = true; 2168 pcmc->gigabyte_align = true; 2169 pcmc->has_reserved_memory = true; 2170 pcmc->kvmclock_enabled = true; 2171 pcmc->enforce_aligned_dimm = true; 2172 /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported 2173 * to be used at the moment, 32K should be enough for a while. */ 2174 pcmc->acpi_data_size = 0x20000 + 0x8000; 2175 pcmc->save_tsc_khz = true; 2176 pcmc->linuxboot_dma_enabled = true; 2177 pcmc->pvh_enabled = true; 2178 assert(!mc->get_hotplug_handler); 2179 mc->get_hotplug_handler = pc_get_hotplug_handler; 2180 mc->hotplug_allowed = pc_hotplug_allowed; 2181 mc->cpu_index_to_instance_props = x86_cpu_index_to_props; 2182 mc->get_default_cpu_node_id = x86_get_default_cpu_node_id; 2183 mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids; 2184 mc->auto_enable_numa_with_memhp = true; 2185 mc->has_hotpluggable_cpus = true; 2186 mc->default_boot_order = "cad"; 2187 mc->hot_add_cpu = pc_hot_add_cpu; 2188 mc->smp_parse = pc_smp_parse; 2189 mc->block_default_type = IF_IDE; 2190 mc->max_cpus = 255; 2191 mc->reset = pc_machine_reset; 2192 mc->wakeup = pc_machine_wakeup; 2193 hc->pre_plug = pc_machine_device_pre_plug_cb; 2194 hc->plug = pc_machine_device_plug_cb; 2195 hc->unplug_request = pc_machine_device_unplug_request_cb; 2196 hc->unplug = pc_machine_device_unplug_cb; 2197 mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE; 2198 mc->nvdimm_supported = true; 2199 mc->numa_mem_supported = true; 2200 2201 object_class_property_add(oc, PC_MACHINE_DEVMEM_REGION_SIZE, "int", 2202 pc_machine_get_device_memory_region_size, NULL, 2203 NULL, NULL, &error_abort); 2204 2205 object_class_property_add(oc, PC_MACHINE_SMM, "OnOffAuto", 2206 pc_machine_get_smm, pc_machine_set_smm, 2207 NULL, NULL, &error_abort); 2208 object_class_property_set_description(oc, PC_MACHINE_SMM, 2209 "Enable SMM (pc & q35)", &error_abort); 2210 2211 object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto", 2212 pc_machine_get_vmport, pc_machine_set_vmport, 2213 NULL, NULL, &error_abort); 2214 object_class_property_set_description(oc, PC_MACHINE_VMPORT, 2215 "Enable vmport (pc & q35)", &error_abort); 2216 2217 object_class_property_add_bool(oc, PC_MACHINE_SMBUS, 2218 pc_machine_get_smbus, pc_machine_set_smbus, &error_abort); 2219 2220 object_class_property_add_bool(oc, PC_MACHINE_SATA, 2221 pc_machine_get_sata, pc_machine_set_sata, &error_abort); 2222 2223 object_class_property_add_bool(oc, PC_MACHINE_PIT, 2224 pc_machine_get_pit, pc_machine_set_pit, &error_abort); 2225 } 2226 2227 static const TypeInfo pc_machine_info = { 2228 .name = TYPE_PC_MACHINE, 2229 .parent = TYPE_X86_MACHINE, 2230 .abstract = true, 2231 .instance_size = sizeof(PCMachineState), 2232 .instance_init = pc_machine_initfn, 2233 .class_size = sizeof(PCMachineClass), 2234 .class_init = pc_machine_class_init, 2235 .interfaces = (InterfaceInfo[]) { 2236 { TYPE_HOTPLUG_HANDLER }, 2237 { } 2238 }, 2239 }; 2240 2241 static void pc_machine_register_types(void) 2242 { 2243 type_register_static(&pc_machine_info); 2244 } 2245 2246 type_init(pc_machine_register_types) 2247